summaryrefslogtreecommitdiff
path: root/kexc25519s.c
diff options
context:
space:
mode:
authormarkus@openbsd.org <markus@openbsd.org>2015-01-19 20:16:15 +0000
committerDamien Miller <djm@mindrot.org>2015-01-20 09:19:39 +1100
commit57d10cbe861a235dd269c74fb2fe248469ecee9d (patch)
treec65deed24700490bd3b20300c4829d4d5466ff6d /kexc25519s.c
parent3fdc88a0def4f86aa88a5846ac079dc964c0546a (diff)
upstream commit
adapt kex to sshbuf and struct ssh; ok djm@
Diffstat (limited to 'kexc25519s.c')
-rw-r--r--kexc25519s.c133
1 files changed, 82 insertions, 51 deletions
diff --git a/kexc25519s.c b/kexc25519s.c
index ba6f546f4..912b0afb1 100644
--- a/kexc25519s.c
+++ b/kexc25519s.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: kexc25519s.c,v 1.5 2015/01/19 19:52:16 markus Exp $ */ 1/* $OpenBSD: kexc25519s.c,v 1.6 2015/01/19 20:16:15 markus Exp $ */
2/* 2/*
3 * Copyright (c) 2001 Markus Friedl. All rights reserved. 3 * Copyright (c) 2001 Markus Friedl. All rights reserved.
4 * Copyright (c) 2010 Damien Miller. All rights reserved. 4 * Copyright (c) 2010 Damien Miller. All rights reserved.
@@ -30,97 +30,128 @@
30#include <string.h> 30#include <string.h>
31#include <signal.h> 31#include <signal.h>
32 32
33#include "xmalloc.h" 33#include "sshkey.h"
34#include "buffer.h"
35#include "key.h"
36#include "cipher.h" 34#include "cipher.h"
35#include "digest.h"
37#include "kex.h" 36#include "kex.h"
38#include "log.h" 37#include "log.h"
39#include "packet.h" 38#include "packet.h"
40#include "ssh2.h" 39#include "ssh2.h"
40#include "sshbuf.h"
41#include "ssherr.h"
41 42
42void 43static int input_kex_c25519_init(int, u_int32_t, void *);
43kexc25519_server(Kex *kex) 44
45int
46kexc25519_server(struct ssh *ssh)
47{
48 debug("expecting SSH2_MSG_KEX_ECDH_INIT");
49 ssh_dispatch_set(ssh, SSH2_MSG_KEX_ECDH_INIT, &input_kex_c25519_init);
50 return 0;
51}
52
53static int
54input_kex_c25519_init(int type, u_int32_t seq, void *ctxt)
44{ 55{
45 Key *server_host_private, *server_host_public; 56 struct ssh *ssh = ctxt;
57 struct kex *kex = ssh->kex;
58 struct sshkey *server_host_private, *server_host_public;
59 struct sshbuf *shared_secret = NULL;
46 u_char *server_host_key_blob = NULL, *signature = NULL; 60 u_char *server_host_key_blob = NULL, *signature = NULL;
47 u_char server_key[CURVE25519_SIZE]; 61 u_char server_key[CURVE25519_SIZE];
48 u_char *client_pubkey = NULL; 62 u_char *client_pubkey = NULL;
49 u_char server_pubkey[CURVE25519_SIZE]; 63 u_char server_pubkey[CURVE25519_SIZE];
50 u_char *hash; 64 u_char hash[SSH_DIGEST_MAX_LENGTH];
51 u_int slen, sbloblen, hashlen; 65 size_t slen, pklen, sbloblen, hashlen;
52 Buffer shared_secret; 66 int r;
53 67
54 /* generate private key */ 68 /* generate private key */
55 kexc25519_keygen(server_key, server_pubkey); 69 kexc25519_keygen(server_key, server_pubkey);
56#ifdef DEBUG_KEXECDH 70#ifdef DEBUG_KEXECDH
57 dump_digest("server private key:", server_key, sizeof(server_key)); 71 dump_digest("server private key:", server_key, sizeof(server_key));
58#endif 72#endif
59
60 if (kex->load_host_public_key == NULL || 73 if (kex->load_host_public_key == NULL ||
61 kex->load_host_private_key == NULL) 74 kex->load_host_private_key == NULL) {
62 fatal("Cannot load hostkey"); 75 r = SSH_ERR_INVALID_ARGUMENT;
63 server_host_public = kex->load_host_public_key(kex->hostkey_type); 76 goto out;
64 if (server_host_public == NULL) 77 }
65 fatal("Unsupported hostkey type %d", kex->hostkey_type); 78 if ((server_host_public = kex->load_host_public_key(kex->hostkey_type,
66 server_host_private = kex->load_host_private_key(kex->hostkey_type); 79 ssh)) == NULL ||
67 80 (server_host_private = kex->load_host_private_key(kex->hostkey_type,
68 debug("expecting SSH2_MSG_KEX_ECDH_INIT"); 81 ssh)) == NULL) {
69 packet_read_expect(SSH2_MSG_KEX_ECDH_INIT); 82 r = SSH_ERR_NO_HOSTKEY_LOADED;
70 client_pubkey = packet_get_string(&slen); 83 goto out;
71 if (slen != CURVE25519_SIZE) 84 }
72 fatal("Incorrect size for server Curve25519 pubkey: %d", slen);
73 packet_check_eom();
74 85
86 if ((r = sshpkt_get_string(ssh, &client_pubkey, &pklen)) != 0 ||
87 (r = sshpkt_get_end(ssh)) != 0)
88 goto out;
89 if (pklen != CURVE25519_SIZE) {
90 r = SSH_ERR_SIGNATURE_INVALID;
91 goto out;
92 }
75#ifdef DEBUG_KEXECDH 93#ifdef DEBUG_KEXECDH
76 dump_digest("client public key:", client_pubkey, CURVE25519_SIZE); 94 dump_digest("client public key:", client_pubkey, CURVE25519_SIZE);
77#endif 95#endif
78 96
79 buffer_init(&shared_secret); 97 if ((shared_secret = sshbuf_new()) == NULL) {
80 kexc25519_shared_key(server_key, client_pubkey, &shared_secret); 98 r = SSH_ERR_ALLOC_FAIL;
99 goto out;
100 }
101 if ((r = kexc25519_shared_key(server_key, client_pubkey,
102 shared_secret)) < 0)
103 goto out;
81 104
82 /* calc H */ 105 /* calc H */
83 key_to_blob(server_host_public, &server_host_key_blob, &sbloblen); 106 if ((r = sshkey_to_blob(server_host_public, &server_host_key_blob,
84 kex_c25519_hash( 107 &sbloblen)) != 0)
108 goto out;
109 hashlen = sizeof(hash);
110 if ((r = kex_c25519_hash(
85 kex->hash_alg, 111 kex->hash_alg,
86 kex->client_version_string, 112 kex->client_version_string,
87 kex->server_version_string, 113 kex->server_version_string,
88 buffer_ptr(kex->peer), buffer_len(kex->peer), 114 sshbuf_ptr(kex->peer), sshbuf_len(kex->peer),
89 buffer_ptr(kex->my), buffer_len(kex->my), 115 sshbuf_ptr(kex->my), sshbuf_len(kex->my),
90 server_host_key_blob, sbloblen, 116 server_host_key_blob, sbloblen,
91 client_pubkey, 117 client_pubkey,
92 server_pubkey, 118 server_pubkey,
93 buffer_ptr(&shared_secret), buffer_len(&shared_secret), 119 sshbuf_ptr(shared_secret), sshbuf_len(shared_secret),
94 &hash, &hashlen 120 hash, &hashlen)) < 0)
95 ); 121 goto out;
96 122
97 /* save session id := H */ 123 /* save session id := H */
98 if (kex->session_id == NULL) { 124 if (kex->session_id == NULL) {
99 kex->session_id_len = hashlen; 125 kex->session_id_len = hashlen;
100 kex->session_id = xmalloc(kex->session_id_len); 126 kex->session_id = malloc(kex->session_id_len);
127 if (kex->session_id == NULL) {
128 r = SSH_ERR_ALLOC_FAIL;
129 goto out;
130 }
101 memcpy(kex->session_id, hash, kex->session_id_len); 131 memcpy(kex->session_id, hash, kex->session_id_len);
102 } 132 }
103 133
104 /* sign H */ 134 /* sign H */
105 kex->sign(server_host_private, server_host_public, &signature, &slen, 135 if ((r = kex->sign(server_host_private, server_host_public,
106 hash, hashlen); 136 &signature, &slen, hash, hashlen, ssh->compat)) < 0)
107 137 goto out;
108 /* destroy_sensitive_data(); */
109 138
110 /* send server hostkey, ECDH pubkey 'Q_S' and signed H */ 139 /* send server hostkey, ECDH pubkey 'Q_S' and signed H */
111 packet_start(SSH2_MSG_KEX_ECDH_REPLY); 140 if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_ECDH_REPLY)) != 0 ||
112 packet_put_string(server_host_key_blob, sbloblen); 141 (r = sshpkt_put_string(ssh, server_host_key_blob, sbloblen)) != 0 ||
113 packet_put_string(server_pubkey, sizeof(server_pubkey)); 142 (r = sshpkt_put_string(ssh, server_pubkey, sizeof(server_pubkey))) != 0 ||
114 packet_put_string(signature, slen); 143 (r = sshpkt_put_string(ssh, signature, slen)) != 0 ||
115 packet_send(); 144 (r = sshpkt_send(ssh)) != 0)
116 145 goto out;
117 free(signature); 146
147 if ((r = kex_derive_keys(ssh, hash, hashlen, shared_secret)) == 0)
148 r = kex_send_newkeys(ssh);
149out:
150 explicit_bzero(hash, sizeof(hash));
151 explicit_bzero(server_key, sizeof(server_key));
118 free(server_host_key_blob); 152 free(server_host_key_blob);
119 /* have keys, free server key */ 153 free(signature);
120 free(client_pubkey); 154 free(client_pubkey);
121 155 sshbuf_free(shared_secret);
122 kex_derive_keys(kex, hash, hashlen, 156 return r;
123 buffer_ptr(&shared_secret), buffer_len(&shared_secret));
124 buffer_free(&shared_secret);
125 kex_finish(kex);
126} 157}