Annotation of src/usr.bin/ssh/kexecdhc.c, Revision 1.12
1.12 ! djm 1: /* $OpenBSD: kexecdhc.c,v 1.11 2017/05/30 14:23:52 markus Exp $ */
1.1 djm 2: /*
3: * Copyright (c) 2001 Markus Friedl. All rights reserved.
4: * Copyright (c) 2010 Damien Miller. All rights reserved.
5: *
6: * Redistribution and use in source and binary forms, with or without
7: * modification, are permitted provided that the following conditions
8: * are met:
9: * 1. Redistributions of source code must retain the above copyright
10: * notice, this list of conditions and the following disclaimer.
11: * 2. Redistributions in binary form must reproduce the above copyright
12: * notice, this list of conditions and the following disclaimer in the
13: * documentation and/or other materials provided with the distribution.
14: *
15: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25: */
26:
27: #include <sys/types.h>
28:
29: #include <stdio.h>
30: #include <string.h>
31: #include <signal.h>
32:
33: #include <openssl/ecdh.h>
34:
1.9 markus 35: #include "sshkey.h"
1.1 djm 36: #include "cipher.h"
1.9 markus 37: #include "digest.h"
1.1 djm 38: #include "kex.h"
39: #include "log.h"
40: #include "packet.h"
41: #include "dh.h"
42: #include "ssh2.h"
1.9 markus 43: #include "dispatch.h"
44: #include "compat.h"
45: #include "ssherr.h"
46: #include "sshbuf.h"
1.1 djm 47:
1.11 markus 48: static int input_kex_ecdh_reply(int, u_int32_t, struct ssh *);
1.9 markus 49:
50: int
51: kexecdh_client(struct ssh *ssh)
1.1 djm 52: {
1.9 markus 53: struct kex *kex = ssh->kex;
54: EC_KEY *client_key = NULL;
1.1 djm 55: const EC_GROUP *group;
1.9 markus 56: const EC_POINT *public_key;
57: int r;
1.1 djm 58:
1.9 markus 59: if ((client_key = EC_KEY_new_by_curve_name(kex->ec_nid)) == NULL) {
60: r = SSH_ERR_ALLOC_FAIL;
61: goto out;
62: }
63: if (EC_KEY_generate_key(client_key) != 1) {
64: r = SSH_ERR_LIBCRYPTO_ERROR;
65: goto out;
66: }
1.1 djm 67: group = EC_KEY_get0_group(client_key);
1.9 markus 68: public_key = EC_KEY_get0_public_key(client_key);
1.1 djm 69:
1.9 markus 70: if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_ECDH_INIT)) != 0 ||
71: (r = sshpkt_put_ec(ssh, public_key, group)) != 0 ||
72: (r = sshpkt_send(ssh)) != 0)
73: goto out;
1.1 djm 74: debug("sending SSH2_MSG_KEX_ECDH_INIT");
75:
76: #ifdef DEBUG_KEXECDH
77: fputs("client private key:\n", stderr);
1.9 markus 78: sshkey_dump_ec_key(client_key);
1.1 djm 79: #endif
1.9 markus 80: kex->ec_client_key = client_key;
81: kex->ec_group = group;
82: client_key = NULL; /* owned by the kex */
1.1 djm 83:
84: debug("expecting SSH2_MSG_KEX_ECDH_REPLY");
1.9 markus 85: ssh_dispatch_set(ssh, SSH2_MSG_KEX_ECDH_REPLY, &input_kex_ecdh_reply);
86: r = 0;
87: out:
88: if (client_key)
89: EC_KEY_free(client_key);
90: return r;
91: }
92:
93: static int
1.11 markus 94: input_kex_ecdh_reply(int type, u_int32_t seq, struct ssh *ssh)
1.9 markus 95: {
96: struct kex *kex = ssh->kex;
97: const EC_GROUP *group;
98: EC_POINT *server_public = NULL;
99: EC_KEY *client_key;
100: BIGNUM *shared_secret = NULL;
101: struct sshkey *server_host_key = NULL;
102: u_char *server_host_key_blob = NULL, *signature = NULL;
103: u_char *kbuf = NULL;
104: u_char hash[SSH_DIGEST_MAX_LENGTH];
105: size_t slen, sbloblen;
106: size_t klen = 0, hashlen;
107: int r;
108:
109: if (kex->verify_host_key == NULL) {
110: r = SSH_ERR_INVALID_ARGUMENT;
111: goto out;
112: }
113: group = kex->ec_group;
114: client_key = kex->ec_client_key;
1.1 djm 115:
116: /* hostkey */
1.9 markus 117: if ((r = sshpkt_get_string(ssh, &server_host_key_blob,
118: &sbloblen)) != 0 ||
119: (r = sshkey_from_blob(server_host_key_blob, sbloblen,
120: &server_host_key)) != 0)
121: goto out;
1.10 djm 122: if (server_host_key->type != kex->hostkey_type ||
123: (kex->hostkey_type == KEY_ECDSA &&
124: server_host_key->ecdsa_nid != kex->hostkey_nid)) {
1.9 markus 125: r = SSH_ERR_KEY_TYPE_MISMATCH;
126: goto out;
127: }
128: if (kex->verify_host_key(server_host_key, ssh) == -1) {
129: r = SSH_ERR_SIGNATURE_INVALID;
130: goto out;
131: }
1.1 djm 132:
133: /* Q_S, server public key */
1.9 markus 134: /* signed H */
135: if ((server_public = EC_POINT_new(group)) == NULL) {
136: r = SSH_ERR_ALLOC_FAIL;
137: goto out;
138: }
139: if ((r = sshpkt_get_ec(ssh, server_public, group)) != 0 ||
140: (r = sshpkt_get_string(ssh, &signature, &slen)) != 0 ||
141: (r = sshpkt_get_end(ssh)) != 0)
142: goto out;
1.1 djm 143:
144: #ifdef DEBUG_KEXECDH
145: fputs("server public key:\n", stderr);
1.9 markus 146: sshkey_dump_ec_point(group, server_public);
1.1 djm 147: #endif
1.9 markus 148: if (sshkey_ec_validate_public(group, server_public) != 0) {
149: sshpkt_disconnect(ssh, "invalid server public key");
150: r = SSH_ERR_MESSAGE_INCOMPLETE;
151: goto out;
152: }
1.1 djm 153:
154: klen = (EC_GROUP_get_degree(group) + 7) / 8;
1.9 markus 155: if ((kbuf = malloc(klen)) == NULL ||
156: (shared_secret = BN_new()) == NULL) {
157: r = SSH_ERR_ALLOC_FAIL;
158: goto out;
159: }
1.1 djm 160: if (ECDH_compute_key(kbuf, klen, server_public,
1.9 markus 161: client_key, NULL) != (int)klen ||
162: BN_bin2bn(kbuf, klen, shared_secret) == NULL) {
163: r = SSH_ERR_LIBCRYPTO_ERROR;
164: goto out;
165: }
1.1 djm 166:
167: #ifdef DEBUG_KEXECDH
168: dump_digest("shared secret", kbuf, klen);
169: #endif
170: /* calc and verify H */
1.9 markus 171: hashlen = sizeof(hash);
172: if ((r = kex_ecdh_hash(
1.5 djm 173: kex->hash_alg,
1.1 djm 174: group,
175: kex->client_version_string,
176: kex->server_version_string,
1.9 markus 177: sshbuf_ptr(kex->my), sshbuf_len(kex->my),
178: sshbuf_ptr(kex->peer), sshbuf_len(kex->peer),
1.1 djm 179: server_host_key_blob, sbloblen,
180: EC_KEY_get0_public_key(client_key),
181: server_public,
182: shared_secret,
1.9 markus 183: hash, &hashlen)) != 0)
184: goto out;
1.1 djm 185:
1.9 markus 186: if ((r = sshkey_verify(server_host_key, signature, slen, hash,
1.12 ! djm 187: hashlen, kex->hostkey_alg, ssh->compat)) != 0)
1.9 markus 188: goto out;
1.1 djm 189:
190: /* save session id */
191: if (kex->session_id == NULL) {
192: kex->session_id_len = hashlen;
1.9 markus 193: kex->session_id = malloc(kex->session_id_len);
194: if (kex->session_id == NULL) {
195: r = SSH_ERR_ALLOC_FAIL;
196: goto out;
197: }
1.1 djm 198: memcpy(kex->session_id, hash, kex->session_id_len);
199: }
200:
1.9 markus 201: if ((r = kex_derive_keys_bn(ssh, hash, hashlen, shared_secret)) == 0)
202: r = kex_send_newkeys(ssh);
203: out:
204: explicit_bzero(hash, sizeof(hash));
205: if (kex->ec_client_key) {
206: EC_KEY_free(kex->ec_client_key);
207: kex->ec_client_key = NULL;
208: }
209: if (server_public)
210: EC_POINT_clear_free(server_public);
211: if (kbuf) {
212: explicit_bzero(kbuf, klen);
213: free(kbuf);
214: }
215: if (shared_secret)
216: BN_clear_free(shared_secret);
217: sshkey_free(server_host_key);
218: free(server_host_key_blob);
219: free(signature);
220: return r;
1.1 djm 221: }