diff options
author | markus@openbsd.org <markus@openbsd.org> | 2015-01-19 20:16:15 +0000 |
---|---|---|
committer | Damien Miller <djm@mindrot.org> | 2015-01-20 09:19:39 +1100 |
commit | 57d10cbe861a235dd269c74fb2fe248469ecee9d (patch) | |
tree | c65deed24700490bd3b20300c4829d4d5466ff6d /kexc25519c.c | |
parent | 3fdc88a0def4f86aa88a5846ac079dc964c0546a (diff) |
upstream commit
adapt kex to sshbuf and struct ssh; ok djm@
Diffstat (limited to 'kexc25519c.c')
-rw-r--r-- | kexc25519c.c | 159 |
1 files changed, 99 insertions, 60 deletions
diff --git a/kexc25519c.c b/kexc25519c.c index ffb537ef6..833ce0544 100644 --- a/kexc25519c.c +++ b/kexc25519c.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: kexc25519c.c,v 1.5 2015/01/19 19:52:16 markus Exp $ */ | 1 | /* $OpenBSD: kexc25519c.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. |
@@ -33,97 +33,136 @@ | |||
33 | #include <string.h> | 33 | #include <string.h> |
34 | #include <signal.h> | 34 | #include <signal.h> |
35 | 35 | ||
36 | #include "xmalloc.h" | 36 | #include "sshkey.h" |
37 | #include "buffer.h" | ||
38 | #include "key.h" | ||
39 | #include "cipher.h" | 37 | #include "cipher.h" |
40 | #include "kex.h" | 38 | #include "kex.h" |
41 | #include "log.h" | 39 | #include "log.h" |
42 | #include "packet.h" | 40 | #include "packet.h" |
43 | #include "ssh2.h" | 41 | #include "ssh2.h" |
42 | #include "sshbuf.h" | ||
43 | #include "digest.h" | ||
44 | #include "ssherr.h" | ||
44 | 45 | ||
45 | void | 46 | static int |
46 | kexc25519_client(Kex *kex) | 47 | input_kex_c25519_reply(int type, u_int32_t seq, void *ctxt); |
47 | { | ||
48 | Key *server_host_key; | ||
49 | u_char client_key[CURVE25519_SIZE]; | ||
50 | u_char client_pubkey[CURVE25519_SIZE]; | ||
51 | u_char *server_pubkey = NULL; | ||
52 | u_char *server_host_key_blob = NULL, *signature = NULL; | ||
53 | u_char *hash; | ||
54 | u_int slen, sbloblen, hashlen; | ||
55 | Buffer shared_secret; | ||
56 | |||
57 | kexc25519_keygen(client_key, client_pubkey); | ||
58 | 48 | ||
59 | packet_start(SSH2_MSG_KEX_ECDH_INIT); | 49 | int |
60 | packet_put_string(client_pubkey, sizeof(client_pubkey)); | 50 | kexc25519_client(struct ssh *ssh) |
61 | packet_send(); | 51 | { |
62 | debug("sending SSH2_MSG_KEX_ECDH_INIT"); | 52 | struct kex *kex = ssh->kex; |
53 | int r; | ||
63 | 54 | ||
55 | kexc25519_keygen(kex->c25519_client_key, kex->c25519_client_pubkey); | ||
64 | #ifdef DEBUG_KEXECDH | 56 | #ifdef DEBUG_KEXECDH |
65 | dump_digest("client private key:", client_key, sizeof(client_key)); | 57 | dump_digest("client private key:", kex->c25519_client_key, |
58 | sizeof(kex->c25519_client_key)); | ||
66 | #endif | 59 | #endif |
60 | if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_ECDH_INIT)) != 0 || | ||
61 | (r = sshpkt_put_string(ssh, kex->c25519_client_pubkey, | ||
62 | sizeof(kex->c25519_client_pubkey))) != 0 || | ||
63 | (r = sshpkt_send(ssh)) != 0) | ||
64 | return r; | ||
67 | 65 | ||
68 | debug("expecting SSH2_MSG_KEX_ECDH_REPLY"); | 66 | debug("expecting SSH2_MSG_KEX_ECDH_REPLY"); |
69 | packet_read_expect(SSH2_MSG_KEX_ECDH_REPLY); | 67 | ssh_dispatch_set(ssh, SSH2_MSG_KEX_ECDH_REPLY, &input_kex_c25519_reply); |
68 | return 0; | ||
69 | } | ||
70 | |||
71 | static int | ||
72 | input_kex_c25519_reply(int type, u_int32_t seq, void *ctxt) | ||
73 | { | ||
74 | struct ssh *ssh = ctxt; | ||
75 | struct kex *kex = ssh->kex; | ||
76 | struct sshkey *server_host_key = NULL; | ||
77 | struct sshbuf *shared_secret = NULL; | ||
78 | u_char *server_pubkey = NULL; | ||
79 | u_char *server_host_key_blob = NULL, *signature = NULL; | ||
80 | u_char hash[SSH_DIGEST_MAX_LENGTH]; | ||
81 | size_t slen, pklen, sbloblen, hashlen; | ||
82 | int r; | ||
83 | |||
84 | if (kex->verify_host_key == NULL) { | ||
85 | r = SSH_ERR_INVALID_ARGUMENT; | ||
86 | goto out; | ||
87 | } | ||
70 | 88 | ||
71 | /* hostkey */ | 89 | /* hostkey */ |
72 | server_host_key_blob = packet_get_string(&sbloblen); | 90 | if ((r = sshpkt_get_string(ssh, &server_host_key_blob, |
73 | server_host_key = key_from_blob(server_host_key_blob, sbloblen); | 91 | &sbloblen)) != 0 || |
74 | if (server_host_key == NULL) | 92 | (r = sshkey_from_blob(server_host_key_blob, sbloblen, |
75 | fatal("cannot decode server_host_key_blob"); | 93 | &server_host_key)) != 0) |
76 | if (server_host_key->type != kex->hostkey_type) | 94 | goto out; |
77 | fatal("type mismatch for decoded server_host_key_blob"); | 95 | if (server_host_key->type != kex->hostkey_type) { |
78 | if (kex->verify_host_key == NULL) | 96 | r = SSH_ERR_KEY_TYPE_MISMATCH; |
79 | fatal("cannot verify server_host_key"); | 97 | goto out; |
80 | if (kex->verify_host_key(server_host_key) == -1) | 98 | } |
81 | fatal("server_host_key verification failed"); | 99 | if (kex->verify_host_key(server_host_key, ssh) == -1) { |
100 | r = SSH_ERR_SIGNATURE_INVALID; | ||
101 | goto out; | ||
102 | } | ||
82 | 103 | ||
83 | /* Q_S, server public key */ | 104 | /* Q_S, server public key */ |
84 | server_pubkey = packet_get_string(&slen); | 105 | /* signed H */ |
85 | if (slen != CURVE25519_SIZE) | 106 | if ((r = sshpkt_get_string(ssh, &server_pubkey, &pklen)) != 0 || |
86 | fatal("Incorrect size for server Curve25519 pubkey: %d", slen); | 107 | (r = sshpkt_get_string(ssh, &signature, &slen)) != 0 || |
108 | (r = sshpkt_get_end(ssh)) != 0) | ||
109 | goto out; | ||
110 | if (pklen != CURVE25519_SIZE) { | ||
111 | r = SSH_ERR_SIGNATURE_INVALID; | ||
112 | goto out; | ||
113 | } | ||
87 | 114 | ||
88 | #ifdef DEBUG_KEXECDH | 115 | #ifdef DEBUG_KEXECDH |
89 | dump_digest("server public key:", server_pubkey, CURVE25519_SIZE); | 116 | dump_digest("server public key:", server_pubkey, CURVE25519_SIZE); |
90 | #endif | 117 | #endif |
91 | 118 | ||
92 | /* signed H */ | 119 | if ((shared_secret = sshbuf_new()) == NULL) { |
93 | signature = packet_get_string(&slen); | 120 | r = SSH_ERR_ALLOC_FAIL; |
94 | packet_check_eom(); | 121 | goto out; |
95 | 122 | } | |
96 | buffer_init(&shared_secret); | 123 | if ((r = kexc25519_shared_key(kex->c25519_client_key, server_pubkey, |
97 | kexc25519_shared_key(client_key, server_pubkey, &shared_secret); | 124 | shared_secret)) < 0) |
125 | goto out; | ||
98 | 126 | ||
99 | /* calc and verify H */ | 127 | /* calc and verify H */ |
100 | kex_c25519_hash( | 128 | hashlen = sizeof(hash); |
129 | if ((r = kex_c25519_hash( | ||
101 | kex->hash_alg, | 130 | kex->hash_alg, |
102 | kex->client_version_string, | 131 | kex->client_version_string, |
103 | kex->server_version_string, | 132 | kex->server_version_string, |
104 | buffer_ptr(kex->my), buffer_len(kex->my), | 133 | sshbuf_ptr(kex->my), sshbuf_len(kex->my), |
105 | buffer_ptr(kex->peer), buffer_len(kex->peer), | 134 | sshbuf_ptr(kex->peer), sshbuf_len(kex->peer), |
106 | server_host_key_blob, sbloblen, | 135 | server_host_key_blob, sbloblen, |
107 | client_pubkey, | 136 | kex->c25519_client_pubkey, |
108 | server_pubkey, | 137 | server_pubkey, |
109 | buffer_ptr(&shared_secret), buffer_len(&shared_secret), | 138 | sshbuf_ptr(shared_secret), sshbuf_len(shared_secret), |
110 | &hash, &hashlen | 139 | hash, &hashlen)) < 0) |
111 | ); | 140 | goto out; |
112 | free(server_host_key_blob); | 141 | |
113 | free(server_pubkey); | 142 | if ((r = sshkey_verify(server_host_key, signature, slen, hash, hashlen, |
114 | if (key_verify(server_host_key, signature, slen, hash, hashlen) != 1) | 143 | ssh->compat)) != 0) |
115 | fatal("key_verify failed for server_host_key"); | 144 | goto out; |
116 | key_free(server_host_key); | ||
117 | free(signature); | ||
118 | 145 | ||
119 | /* save session id */ | 146 | /* save session id */ |
120 | if (kex->session_id == NULL) { | 147 | if (kex->session_id == NULL) { |
121 | kex->session_id_len = hashlen; | 148 | kex->session_id_len = hashlen; |
122 | kex->session_id = xmalloc(kex->session_id_len); | 149 | kex->session_id = malloc(kex->session_id_len); |
150 | if (kex->session_id == NULL) { | ||
151 | r = SSH_ERR_ALLOC_FAIL; | ||
152 | goto out; | ||
153 | } | ||
123 | memcpy(kex->session_id, hash, kex->session_id_len); | 154 | memcpy(kex->session_id, hash, kex->session_id_len); |
124 | } | 155 | } |
125 | kex_derive_keys(kex, hash, hashlen, | 156 | |
126 | buffer_ptr(&shared_secret), buffer_len(&shared_secret)); | 157 | if ((r = kex_derive_keys(ssh, hash, hashlen, shared_secret)) == 0) |
127 | buffer_free(&shared_secret); | 158 | r = kex_send_newkeys(ssh); |
128 | kex_finish(kex); | 159 | out: |
160 | explicit_bzero(hash, sizeof(hash)); | ||
161 | explicit_bzero(kex->c25519_client_key, sizeof(kex->c25519_client_key)); | ||
162 | free(server_host_key_blob); | ||
163 | free(server_pubkey); | ||
164 | free(signature); | ||
165 | sshkey_free(server_host_key); | ||
166 | sshbuf_free(shared_secret); | ||
167 | return r; | ||
129 | } | 168 | } |