Annotation of src/usr.bin/ssh/kexecdhc.c, Revision 1.16
1.16 ! djm 1: /* $OpenBSD: kexecdhc.c,v 1.15 2019/01/21 09:55:52 djm 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:
1.13 jsing 88: EC_KEY_free(client_key);
1.9 markus 89: return r;
90: }
91:
92: static int
1.11 markus 93: input_kex_ecdh_reply(int type, u_int32_t seq, struct ssh *ssh)
1.9 markus 94: {
95: struct kex *kex = ssh->kex;
96: const EC_GROUP *group;
97: EC_POINT *server_public = NULL;
98: EC_KEY *client_key;
99: BIGNUM *shared_secret = NULL;
100: struct sshkey *server_host_key = NULL;
101: u_char *server_host_key_blob = NULL, *signature = NULL;
102: u_char *kbuf = NULL;
103: u_char hash[SSH_DIGEST_MAX_LENGTH];
104: size_t slen, sbloblen;
105: size_t klen = 0, hashlen;
106: int r;
107:
108: group = kex->ec_group;
109: client_key = kex->ec_client_key;
1.1 djm 110:
111: /* hostkey */
1.9 markus 112: if ((r = sshpkt_get_string(ssh, &server_host_key_blob,
113: &sbloblen)) != 0 ||
114: (r = sshkey_from_blob(server_host_key_blob, sbloblen,
115: &server_host_key)) != 0)
116: goto out;
1.16 ! djm 117: if ((r = kex_verify_host_key(ssh, server_host_key)) != 0)
1.9 markus 118: goto out;
1.1 djm 119:
120: /* Q_S, server public key */
1.9 markus 121: /* signed H */
122: if ((server_public = EC_POINT_new(group)) == NULL) {
123: r = SSH_ERR_ALLOC_FAIL;
124: goto out;
125: }
126: if ((r = sshpkt_get_ec(ssh, server_public, group)) != 0 ||
127: (r = sshpkt_get_string(ssh, &signature, &slen)) != 0 ||
128: (r = sshpkt_get_end(ssh)) != 0)
129: goto out;
1.1 djm 130:
131: #ifdef DEBUG_KEXECDH
132: fputs("server public key:\n", stderr);
1.9 markus 133: sshkey_dump_ec_point(group, server_public);
1.1 djm 134: #endif
1.9 markus 135: if (sshkey_ec_validate_public(group, server_public) != 0) {
136: sshpkt_disconnect(ssh, "invalid server public key");
137: r = SSH_ERR_MESSAGE_INCOMPLETE;
138: goto out;
139: }
1.1 djm 140:
141: klen = (EC_GROUP_get_degree(group) + 7) / 8;
1.9 markus 142: if ((kbuf = malloc(klen)) == NULL ||
143: (shared_secret = BN_new()) == NULL) {
144: r = SSH_ERR_ALLOC_FAIL;
145: goto out;
146: }
1.1 djm 147: if (ECDH_compute_key(kbuf, klen, server_public,
1.9 markus 148: client_key, NULL) != (int)klen ||
149: BN_bin2bn(kbuf, klen, shared_secret) == NULL) {
150: r = SSH_ERR_LIBCRYPTO_ERROR;
151: goto out;
152: }
1.1 djm 153:
154: #ifdef DEBUG_KEXECDH
155: dump_digest("shared secret", kbuf, klen);
156: #endif
157: /* calc and verify H */
1.9 markus 158: hashlen = sizeof(hash);
159: if ((r = kex_ecdh_hash(
1.5 djm 160: kex->hash_alg,
1.1 djm 161: group,
1.14 djm 162: kex->client_version,
163: kex->server_version,
1.9 markus 164: sshbuf_ptr(kex->my), sshbuf_len(kex->my),
165: sshbuf_ptr(kex->peer), sshbuf_len(kex->peer),
1.1 djm 166: server_host_key_blob, sbloblen,
167: EC_KEY_get0_public_key(client_key),
168: server_public,
169: shared_secret,
1.9 markus 170: hash, &hashlen)) != 0)
171: goto out;
1.1 djm 172:
1.9 markus 173: if ((r = sshkey_verify(server_host_key, signature, slen, hash,
1.12 djm 174: hashlen, kex->hostkey_alg, ssh->compat)) != 0)
1.9 markus 175: goto out;
1.1 djm 176:
1.9 markus 177: if ((r = kex_derive_keys_bn(ssh, hash, hashlen, shared_secret)) == 0)
178: r = kex_send_newkeys(ssh);
179: out:
180: explicit_bzero(hash, sizeof(hash));
1.13 jsing 181: EC_KEY_free(kex->ec_client_key);
182: kex->ec_client_key = NULL;
183: EC_POINT_clear_free(server_public);
1.9 markus 184: if (kbuf) {
185: explicit_bzero(kbuf, klen);
186: free(kbuf);
187: }
1.13 jsing 188: BN_clear_free(shared_secret);
1.9 markus 189: sshkey_free(server_host_key);
190: free(server_host_key_blob);
191: free(signature);
192: return r;
1.1 djm 193: }