diff options
Diffstat (limited to 'kexc25519s.c')
-rw-r--r-- | kexc25519s.c | 134 |
1 files changed, 83 insertions, 51 deletions
diff --git a/kexc25519s.c b/kexc25519s.c index 2b8e8efa1..b2d2c858f 100644 --- a/kexc25519s.c +++ b/kexc25519s.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: kexc25519s.c,v 1.4 2014/01/12 08:13:13 djm Exp $ */ | 1 | /* $OpenBSD: kexc25519s.c,v 1.8 2015/01/26 06:10:03 djm 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,129 @@ | |||
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 | ||
42 | void | 43 | static int input_kex_c25519_init(int, u_int32_t, void *); |
43 | kexc25519_server(Kex *kex) | 44 | |
45 | int | ||
46 | kexc25519_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 | |||
53 | static int | ||
54 | input_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 | server_host_public = kex->load_host_public_key(kex->hostkey_type, |
66 | server_host_private = kex->load_host_private_key(kex->hostkey_type); | 79 | kex->hostkey_nid, ssh); |
67 | 80 | server_host_private = kex->load_host_private_key(kex->hostkey_type, | |
68 | debug("expecting SSH2_MSG_KEX_ECDH_INIT"); | 81 | kex->hostkey_nid, ssh); |
69 | packet_read_expect(SSH2_MSG_KEX_ECDH_INIT); | 82 | if (server_host_public == NULL) { |
70 | client_pubkey = packet_get_string(&slen); | 83 | r = SSH_ERR_NO_HOSTKEY_LOADED; |
71 | if (slen != CURVE25519_SIZE) | 84 | goto out; |
72 | fatal("Incorrect size for server Curve25519 pubkey: %d", slen); | 85 | } |
73 | packet_check_eom(); | ||
74 | 86 | ||
87 | if ((r = sshpkt_get_string(ssh, &client_pubkey, &pklen)) != 0 || | ||
88 | (r = sshpkt_get_end(ssh)) != 0) | ||
89 | goto out; | ||
90 | if (pklen != CURVE25519_SIZE) { | ||
91 | r = SSH_ERR_SIGNATURE_INVALID; | ||
92 | goto out; | ||
93 | } | ||
75 | #ifdef DEBUG_KEXECDH | 94 | #ifdef DEBUG_KEXECDH |
76 | dump_digest("client public key:", client_pubkey, CURVE25519_SIZE); | 95 | dump_digest("client public key:", client_pubkey, CURVE25519_SIZE); |
77 | #endif | 96 | #endif |
78 | 97 | ||
79 | buffer_init(&shared_secret); | 98 | if ((shared_secret = sshbuf_new()) == NULL) { |
80 | kexc25519_shared_key(server_key, client_pubkey, &shared_secret); | 99 | r = SSH_ERR_ALLOC_FAIL; |
100 | goto out; | ||
101 | } | ||
102 | if ((r = kexc25519_shared_key(server_key, client_pubkey, | ||
103 | shared_secret)) < 0) | ||
104 | goto out; | ||
81 | 105 | ||
82 | /* calc H */ | 106 | /* calc H */ |
83 | key_to_blob(server_host_public, &server_host_key_blob, &sbloblen); | 107 | if ((r = sshkey_to_blob(server_host_public, &server_host_key_blob, |
84 | kex_c25519_hash( | 108 | &sbloblen)) != 0) |
109 | goto out; | ||
110 | hashlen = sizeof(hash); | ||
111 | if ((r = kex_c25519_hash( | ||
85 | kex->hash_alg, | 112 | kex->hash_alg, |
86 | kex->client_version_string, | 113 | kex->client_version_string, |
87 | kex->server_version_string, | 114 | kex->server_version_string, |
88 | buffer_ptr(&kex->peer), buffer_len(&kex->peer), | 115 | sshbuf_ptr(kex->peer), sshbuf_len(kex->peer), |
89 | buffer_ptr(&kex->my), buffer_len(&kex->my), | 116 | sshbuf_ptr(kex->my), sshbuf_len(kex->my), |
90 | server_host_key_blob, sbloblen, | 117 | server_host_key_blob, sbloblen, |
91 | client_pubkey, | 118 | client_pubkey, |
92 | server_pubkey, | 119 | server_pubkey, |
93 | buffer_ptr(&shared_secret), buffer_len(&shared_secret), | 120 | sshbuf_ptr(shared_secret), sshbuf_len(shared_secret), |
94 | &hash, &hashlen | 121 | hash, &hashlen)) < 0) |
95 | ); | 122 | goto out; |
96 | 123 | ||
97 | /* save session id := H */ | 124 | /* save session id := H */ |
98 | if (kex->session_id == NULL) { | 125 | if (kex->session_id == NULL) { |
99 | kex->session_id_len = hashlen; | 126 | kex->session_id_len = hashlen; |
100 | kex->session_id = xmalloc(kex->session_id_len); | 127 | kex->session_id = malloc(kex->session_id_len); |
128 | if (kex->session_id == NULL) { | ||
129 | r = SSH_ERR_ALLOC_FAIL; | ||
130 | goto out; | ||
131 | } | ||
101 | memcpy(kex->session_id, hash, kex->session_id_len); | 132 | memcpy(kex->session_id, hash, kex->session_id_len); |
102 | } | 133 | } |
103 | 134 | ||
104 | /* sign H */ | 135 | /* sign H */ |
105 | kex->sign(server_host_private, server_host_public, &signature, &slen, | 136 | if ((r = kex->sign(server_host_private, server_host_public, |
106 | hash, hashlen); | 137 | &signature, &slen, hash, hashlen, ssh->compat)) < 0) |
107 | 138 | goto out; | |
108 | /* destroy_sensitive_data(); */ | ||
109 | 139 | ||
110 | /* send server hostkey, ECDH pubkey 'Q_S' and signed H */ | 140 | /* send server hostkey, ECDH pubkey 'Q_S' and signed H */ |
111 | packet_start(SSH2_MSG_KEX_ECDH_REPLY); | 141 | if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_ECDH_REPLY)) != 0 || |
112 | packet_put_string(server_host_key_blob, sbloblen); | 142 | (r = sshpkt_put_string(ssh, server_host_key_blob, sbloblen)) != 0 || |
113 | packet_put_string(server_pubkey, sizeof(server_pubkey)); | 143 | (r = sshpkt_put_string(ssh, server_pubkey, sizeof(server_pubkey))) != 0 || |
114 | packet_put_string(signature, slen); | 144 | (r = sshpkt_put_string(ssh, signature, slen)) != 0 || |
115 | packet_send(); | 145 | (r = sshpkt_send(ssh)) != 0) |
116 | 146 | goto out; | |
117 | free(signature); | 147 | |
148 | if ((r = kex_derive_keys(ssh, hash, hashlen, shared_secret)) == 0) | ||
149 | r = kex_send_newkeys(ssh); | ||
150 | out: | ||
151 | explicit_bzero(hash, sizeof(hash)); | ||
152 | explicit_bzero(server_key, sizeof(server_key)); | ||
118 | free(server_host_key_blob); | 153 | free(server_host_key_blob); |
119 | /* have keys, free server key */ | 154 | free(signature); |
120 | free(client_pubkey); | 155 | free(client_pubkey); |
121 | 156 | sshbuf_free(shared_secret); | |
122 | kex_derive_keys(kex, hash, hashlen, | 157 | return r; |
123 | buffer_ptr(&shared_secret), buffer_len(&shared_secret)); | ||
124 | buffer_free(&shared_secret); | ||
125 | kex_finish(kex); | ||
126 | } | 158 | } |