Line | Source | Count |
1 | | - |
2 | | - |
3 | | - |
4 | | - |
5 | | - |
6 | | - |
7 | | - |
8 | | - |
9 | | - |
10 | | - |
11 | | - |
12 | | - |
13 | | - |
14 | | - |
15 | | - |
16 | | - |
17 | | - |
18 | | - |
19 | | - |
20 | | - |
21 | | - |
22 | | - |
23 | | - |
24 | | - |
25 | | - |
26 | | - |
27 | #include "includes.h" | - |
28 | | - |
29 | #if defined(WITH_OPENSSL) && defined(OPENSSL_HAS_ECC) | - |
30 | | - |
31 | #include <sys/types.h> | - |
32 | | - |
33 | #include <stdio.h> | - |
34 | #include <string.h> | - |
35 | #include <signal.h> | - |
36 | | - |
37 | #include <openssl/ecdh.h> | - |
38 | | - |
39 | #include "sshkey.h" | - |
40 | #include "cipher.h" | - |
41 | #include "digest.h" | - |
42 | #include "kex.h" | - |
43 | #include "log.h" | - |
44 | #include "packet.h" | - |
45 | #include "dh.h" | - |
46 | #include "ssh2.h" | - |
47 | #include "dispatch.h" | - |
48 | #include "compat.h" | - |
49 | #include "ssherr.h" | - |
50 | #include "sshbuf.h" | - |
51 | | - |
52 | static int input_kex_ecdh_reply(int, u_int32_t, struct ssh *); | - |
53 | | - |
54 | int | - |
55 | kexecdh_client(struct ssh *ssh) | - |
56 | { | - |
57 | struct kex *kex = ssh->kex; | - |
58 | EC_KEY *client_key = NULL; | - |
59 | const EC_GROUP *group; | - |
60 | const EC_POINT *public_key; | - |
61 | int r; | - |
62 | | - |
63 | if ((client_key = EC_KEY_new_by_curve_name(kex->ec_nid)) == NULL) {TRUE | never evaluated | FALSE | evaluated 60 times by 1 test |
| 0-60 |
64 | r = SSH_ERR_ALLOC_FAIL; | - |
65 | goto out; never executed: goto out; | 0 |
66 | } | - |
67 | if (EC_KEY_generate_key(client_key) != 1) {TRUE | never evaluated | FALSE | evaluated 60 times by 1 test |
| 0-60 |
68 | r = SSH_ERR_LIBCRYPTO_ERROR; | - |
69 | goto out; never executed: goto out; | 0 |
70 | } | - |
71 | group = EC_KEY_get0_group(client_key); | - |
72 | public_key = EC_KEY_get0_public_key(client_key); | - |
73 | | - |
74 | if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_ECDH_INIT)) != 0 ||TRUE | never evaluated | FALSE | evaluated 60 times by 1 test |
| 0-60 |
75 | (r = sshpkt_put_ec(ssh, public_key, group)) != 0 ||TRUE | never evaluated | FALSE | evaluated 60 times by 1 test |
| 0-60 |
76 | (r = sshpkt_send(ssh)) != 0)TRUE | never evaluated | FALSE | evaluated 60 times by 1 test |
| 0-60 |
77 | goto out; never executed: goto out; | 0 |
78 | debug("sending SSH2_MSG_KEX_ECDH_INIT"); | - |
79 | | - |
80 | #ifdef DEBUG_KEXECDH | - |
81 | fputs("client private key:\n", stderr); | - |
82 | sshkey_dump_ec_key(client_key); | - |
83 | #endif | - |
84 | kex->ec_client_key = client_key; | - |
85 | kex->ec_group = group; | - |
86 | client_key = NULL; | - |
87 | | - |
88 | debug("expecting SSH2_MSG_KEX_ECDH_REPLY"); | - |
89 | ssh_dispatch_set(ssh, SSH2_MSG_KEX_ECDH_REPLY, &input_kex_ecdh_reply); | - |
90 | r = 0; | - |
91 | out:code before this statement executed 60 times by 1 test: out: | 60 |
92 | EC_KEY_free(client_key); | - |
93 | return r;executed 60 times by 1 test: return r; | 60 |
94 | } | - |
95 | | - |
96 | static int | - |
97 | input_kex_ecdh_reply(int type, u_int32_t seq, struct ssh *ssh) | - |
98 | { | - |
99 | struct kex *kex = ssh->kex; | - |
100 | const EC_GROUP *group; | - |
101 | EC_POINT *server_public = NULL; | - |
102 | EC_KEY *client_key; | - |
103 | BIGNUM *shared_secret = NULL; | - |
104 | struct sshkey *server_host_key = NULL; | - |
105 | u_char *server_host_key_blob = NULL, *signature = NULL; | - |
106 | u_char *kbuf = NULL; | - |
107 | u_char hash[SSH_DIGEST_MAX_LENGTH]; | - |
108 | size_t slen, sbloblen; | - |
109 | size_t klen = 0, hashlen; | - |
110 | int r; | - |
111 | | - |
112 | if (kex->verify_host_key == NULL) {TRUE | never evaluated | FALSE | evaluated 60 times by 1 test |
| 0-60 |
113 | r = SSH_ERR_INVALID_ARGUMENT; | - |
114 | goto out; never executed: goto out; | 0 |
115 | } | - |
116 | group = kex->ec_group; | - |
117 | client_key = kex->ec_client_key; | - |
118 | | - |
119 | | - |
120 | if ((r = sshpkt_get_string(ssh, &server_host_key_blob,TRUE | never evaluated | FALSE | evaluated 60 times by 1 test |
| 0-60 |
121 | &sbloblen)) != 0 ||TRUE | never evaluated | FALSE | evaluated 60 times by 1 test |
| 0-60 |
122 | (r = sshkey_from_blob(server_host_key_blob, sbloblen,TRUE | never evaluated | FALSE | evaluated 60 times by 1 test |
| 0-60 |
123 | &server_host_key)) != 0)TRUE | never evaluated | FALSE | evaluated 60 times by 1 test |
| 0-60 |
124 | goto out; never executed: goto out; | 0 |
125 | if (server_host_key->type != kex->hostkey_type ||TRUE | never evaluated | FALSE | evaluated 60 times by 1 test |
| 0-60 |
126 | (kex->hostkey_type == KEY_ECDSA &&TRUE | evaluated 15 times by 1 test | FALSE | evaluated 45 times by 1 test |
| 15-45 |
127 | server_host_key->ecdsa_nid != kex->hostkey_nid)) {TRUE | never evaluated | FALSE | evaluated 15 times by 1 test |
| 0-15 |
128 | r = SSH_ERR_KEY_TYPE_MISMATCH; | - |
129 | goto out; never executed: goto out; | 0 |
130 | } | - |
131 | if (kex->verify_host_key(server_host_key, ssh) == -1) {TRUE | never evaluated | FALSE | evaluated 60 times by 1 test |
| 0-60 |
132 | r = SSH_ERR_SIGNATURE_INVALID; | - |
133 | goto out; never executed: goto out; | 0 |
134 | } | - |
135 | | - |
136 | | - |
137 | | - |
138 | if ((server_public = EC_POINT_new(group)) == NULL) {TRUE | never evaluated | FALSE | evaluated 60 times by 1 test |
| 0-60 |
139 | r = SSH_ERR_ALLOC_FAIL; | - |
140 | goto out; never executed: goto out; | 0 |
141 | } | - |
142 | if ((r = sshpkt_get_ec(ssh, server_public, group)) != 0 ||TRUE | never evaluated | FALSE | evaluated 60 times by 1 test |
| 0-60 |
143 | (r = sshpkt_get_string(ssh, &signature, &slen)) != 0 ||TRUE | never evaluated | FALSE | evaluated 60 times by 1 test |
| 0-60 |
144 | (r = sshpkt_get_end(ssh)) != 0)TRUE | never evaluated | FALSE | evaluated 60 times by 1 test |
| 0-60 |
145 | goto out; never executed: goto out; | 0 |
146 | | - |
147 | #ifdef DEBUG_KEXECDH | - |
148 | fputs("server public key:\n", stderr); | - |
149 | sshkey_dump_ec_point(group, server_public); | - |
150 | #endif | - |
151 | if (sshkey_ec_validate_public(group, server_public) != 0) {TRUE | never evaluated | FALSE | evaluated 60 times by 1 test |
| 0-60 |
152 | sshpkt_disconnect(ssh, "invalid server public key"); | - |
153 | r = SSH_ERR_MESSAGE_INCOMPLETE; | - |
154 | goto out; never executed: goto out; | 0 |
155 | } | - |
156 | | - |
157 | klen = (EC_GROUP_get_degree(group) + 7) / 8; | - |
158 | if ((kbuf = malloc(klen)) == NULL ||TRUE | never evaluated | FALSE | evaluated 60 times by 1 test |
| 0-60 |
159 | (shared_secret = BN_new()) == NULL) {TRUE | never evaluated | FALSE | evaluated 60 times by 1 test |
| 0-60 |
160 | r = SSH_ERR_ALLOC_FAIL; | - |
161 | goto out; never executed: goto out; | 0 |
162 | } | - |
163 | if (ECDH_compute_key(kbuf, klen, server_public,TRUE | never evaluated | FALSE | evaluated 60 times by 1 test |
| 0-60 |
164 | client_key, NULL) != (int)klen ||TRUE | never evaluated | FALSE | evaluated 60 times by 1 test |
| 0-60 |
165 | BN_bin2bn(kbuf, klen, shared_secret) == NULL) {TRUE | never evaluated | FALSE | evaluated 60 times by 1 test |
| 0-60 |
166 | r = SSH_ERR_LIBCRYPTO_ERROR; | - |
167 | goto out; never executed: goto out; | 0 |
168 | } | - |
169 | | - |
170 | #ifdef DEBUG_KEXECDH | - |
171 | dump_digest("shared secret", kbuf, klen); | - |
172 | #endif | - |
173 | | - |
174 | hashlen = sizeof(hash); | - |
175 | if ((r = kex_ecdh_hash(TRUE | never evaluated | FALSE | evaluated 60 times by 1 test |
| 0-60 |
176 | kex->hash_alg,TRUE | never evaluated | FALSE | evaluated 60 times by 1 test |
| 0-60 |
177 | group,TRUE | never evaluated | FALSE | evaluated 60 times by 1 test |
| 0-60 |
178 | kex->client_version_string,TRUE | never evaluated | FALSE | evaluated 60 times by 1 test |
| 0-60 |
179 | kex->server_version_string,TRUE | never evaluated | FALSE | evaluated 60 times by 1 test |
| 0-60 |
180 | sshbuf_ptr(kex->my), sshbuf_len(kex->my),TRUE | never evaluated | FALSE | evaluated 60 times by 1 test |
| 0-60 |
181 | sshbuf_ptr(kex->peer), sshbuf_len(kex->peer),TRUE | never evaluated | FALSE | evaluated 60 times by 1 test |
| 0-60 |
182 | server_host_key_blob, sbloblen,TRUE | never evaluated | FALSE | evaluated 60 times by 1 test |
| 0-60 |
183 | EC_KEY_get0_public_key(client_key),TRUE | never evaluated | FALSE | evaluated 60 times by 1 test |
| 0-60 |
184 | server_public,TRUE | never evaluated | FALSE | evaluated 60 times by 1 test |
| 0-60 |
185 | shared_secret,TRUE | never evaluated | FALSE | evaluated 60 times by 1 test |
| 0-60 |
186 | hash, &hashlen)) != 0)TRUE | never evaluated | FALSE | evaluated 60 times by 1 test |
| 0-60 |
187 | goto out; never executed: goto out; | 0 |
188 | | - |
189 | if ((r = sshkey_verify(server_host_key, signature, slen, hash,TRUE | never evaluated | FALSE | evaluated 60 times by 1 test |
| 0-60 |
190 | hashlen, kex->hostkey_alg, ssh->compat)) != 0)TRUE | never evaluated | FALSE | evaluated 60 times by 1 test |
| 0-60 |
191 | goto out; never executed: goto out; | 0 |
192 | | - |
193 | | - |
194 | if (kex->session_id == NULL) {TRUE | evaluated 12 times by 1 test | FALSE | evaluated 48 times by 1 test |
| 12-48 |
195 | kex->session_id_len = hashlen; | - |
196 | kex->session_id = malloc(kex->session_id_len); | - |
197 | if (kex->session_id == NULL) {TRUE | never evaluated | FALSE | evaluated 12 times by 1 test |
| 0-12 |
198 | r = SSH_ERR_ALLOC_FAIL; | - |
199 | goto out; never executed: goto out; | 0 |
200 | } | - |
201 | memcpy(kex->session_id, hash, kex->session_id_len); | - |
202 | }executed 12 times by 1 test: end of block | 12 |
203 | | - |
204 | if ((r = kex_derive_keys_bn(ssh, hash, hashlen, shared_secret)) == 0)TRUE | evaluated 60 times by 1 test | FALSE | never evaluated |
| 0-60 |
205 | r = kex_send_newkeys(ssh);executed 60 times by 1 test: r = kex_send_newkeys(ssh); | 60 |
206 | out:code before this statement executed 60 times by 1 test: out: | 60 |
207 | explicit_bzero(hash, sizeof(hash)); | - |
208 | EC_KEY_free(kex->ec_client_key); | - |
209 | kex->ec_client_key = NULL; | - |
210 | EC_POINT_clear_free(server_public); | - |
211 | if (kbuf) {TRUE | evaluated 60 times by 1 test | FALSE | never evaluated |
| 0-60 |
212 | explicit_bzero(kbuf, klen); | - |
213 | free(kbuf); | - |
214 | }executed 60 times by 1 test: end of block | 60 |
215 | BN_clear_free(shared_secret); | - |
216 | sshkey_free(server_host_key); | - |
217 | free(server_host_key_blob); | - |
218 | free(signature); | - |
219 | return r;executed 60 times by 1 test: return r; | 60 |
220 | } | - |
221 | #endif /* defined(WITH_OPENSSL) && defined(OPENSSL_HAS_ECC) */ | - |
222 | | - |
| | |