Branch data Line data Source code
1 : : /* $OpenBSD: kexecdhc.c,v 1.7 2014/02/02 03:44:31 djm Exp $ */
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 "includes.h"
28 : :
29 : : #include <sys/types.h>
30 : :
31 : : #include <stdio.h>
32 : : #include <string.h>
33 : : #include <signal.h>
34 : :
35 : : #include "xmalloc.h"
36 : : #include "buffer.h"
37 : : #include "key.h"
38 : : #include "cipher.h"
39 : : #include "kex.h"
40 : : #include "log.h"
41 : : #include "packet.h"
42 : : #include "dh.h"
43 : : #include "ssh2.h"
44 : :
45 : : #ifdef OPENSSL_HAS_ECC
46 : :
47 : : #include <openssl/ecdh.h>
48 : :
49 : : void
50 : 72 : kexecdh_client(Kex *kex)
51 : : {
52 : : EC_KEY *client_key;
53 : : EC_POINT *server_public;
54 : : const EC_GROUP *group;
55 : : BIGNUM *shared_secret;
56 : : Key *server_host_key;
57 : 72 : u_char *server_host_key_blob = NULL, *signature = NULL;
58 : : u_char *kbuf, *hash;
59 : : u_int klen, slen, sbloblen, hashlen;
60 : :
61 [ - + ]: 72 : if ((client_key = EC_KEY_new_by_curve_name(kex->ec_nid)) == NULL)
62 : 0 : fatal("%s: EC_KEY_new_by_curve_name failed", __func__);
63 [ - + ]: 72 : if (EC_KEY_generate_key(client_key) != 1)
64 : 0 : fatal("%s: EC_KEY_generate_key failed", __func__);
65 : 72 : group = EC_KEY_get0_group(client_key);
66 : :
67 : 72 : packet_start(SSH2_MSG_KEX_ECDH_INIT);
68 : 72 : packet_put_ecpoint(group, EC_KEY_get0_public_key(client_key));
69 : 72 : packet_send();
70 : 72 : debug("sending SSH2_MSG_KEX_ECDH_INIT");
71 : :
72 : : #ifdef DEBUG_KEXECDH
73 : : fputs("client private key:\n", stderr);
74 : : key_dump_ec_key(client_key);
75 : : #endif
76 : :
77 : 72 : debug("expecting SSH2_MSG_KEX_ECDH_REPLY");
78 : 72 : packet_read_expect(SSH2_MSG_KEX_ECDH_REPLY);
79 : :
80 : : /* hostkey */
81 : 72 : server_host_key_blob = packet_get_string(&sbloblen);
82 : 72 : server_host_key = key_from_blob(server_host_key_blob, sbloblen);
83 [ - + ]: 72 : if (server_host_key == NULL)
84 : 0 : fatal("cannot decode server_host_key_blob");
85 [ - + ]: 72 : if (server_host_key->type != kex->hostkey_type)
86 : 0 : fatal("type mismatch for decoded server_host_key_blob");
87 [ - + ]: 72 : if (kex->verify_host_key == NULL)
88 : 0 : fatal("cannot verify server_host_key");
89 [ - + ]: 72 : if (kex->verify_host_key(server_host_key) == -1)
90 : 0 : fatal("server_host_key verification failed");
91 : :
92 : : /* Q_S, server public key */
93 [ - + ]: 72 : if ((server_public = EC_POINT_new(group)) == NULL)
94 : 0 : fatal("%s: EC_POINT_new failed", __func__);
95 : 72 : packet_get_ecpoint(group, server_public);
96 : :
97 [ - + ]: 72 : if (key_ec_validate_public(group, server_public) != 0)
98 : 0 : fatal("%s: invalid server public key", __func__);
99 : :
100 : : #ifdef DEBUG_KEXECDH
101 : : fputs("server public key:\n", stderr);
102 : : key_dump_ec_point(group, server_public);
103 : : #endif
104 : :
105 : : /* signed H */
106 : 72 : signature = packet_get_string(&slen);
107 [ - + ]: 72 : packet_check_eom();
108 : :
109 : 72 : klen = (EC_GROUP_get_degree(group) + 7) / 8;
110 : 72 : kbuf = xmalloc(klen);
111 [ - + ]: 72 : if (ECDH_compute_key(kbuf, klen, server_public,
112 : : client_key, NULL) != (int)klen)
113 : 0 : fatal("%s: ECDH_compute_key failed", __func__);
114 : :
115 : : #ifdef DEBUG_KEXECDH
116 : : dump_digest("shared secret", kbuf, klen);
117 : : #endif
118 [ - + ]: 72 : if ((shared_secret = BN_new()) == NULL)
119 : 0 : fatal("%s: BN_new failed", __func__);
120 [ - + ]: 72 : if (BN_bin2bn(kbuf, klen, shared_secret) == NULL)
121 : 0 : fatal("%s: BN_bin2bn failed", __func__);
122 : 72 : explicit_bzero(kbuf, klen);
123 : 72 : free(kbuf);
124 : :
125 : : /* calc and verify H */
126 : 144 : kex_ecdh_hash(
127 : : kex->hash_alg,
128 : : group,
129 : : kex->client_version_string,
130 : : kex->server_version_string,
131 : 72 : buffer_ptr(&kex->my), buffer_len(&kex->my),
132 : 72 : buffer_ptr(&kex->peer), buffer_len(&kex->peer),
133 : : server_host_key_blob, sbloblen,
134 : : EC_KEY_get0_public_key(client_key),
135 : : server_public,
136 : : shared_secret,
137 : : &hash, &hashlen
138 : : );
139 : 72 : free(server_host_key_blob);
140 : 72 : EC_POINT_clear_free(server_public);
141 : 72 : EC_KEY_free(client_key);
142 : :
143 [ - + ]: 72 : if (key_verify(server_host_key, signature, slen, hash, hashlen) != 1)
144 : 0 : fatal("key_verify failed for server_host_key");
145 : 72 : key_free(server_host_key);
146 : 72 : free(signature);
147 : :
148 : : /* save session id */
149 [ + + ]: 72 : if (kex->session_id == NULL) {
150 : 24 : kex->session_id_len = hashlen;
151 : 24 : kex->session_id = xmalloc(kex->session_id_len);
152 : 24 : memcpy(kex->session_id, hash, kex->session_id_len);
153 : : }
154 : :
155 : 72 : kex_derive_keys_bn(kex, hash, hashlen, shared_secret);
156 : 72 : BN_clear_free(shared_secret);
157 : 72 : kex_finish(kex);
158 : 72 : }
159 : : #else /* OPENSSL_HAS_ECC */
160 : : void
161 : : kexecdh_client(Kex *kex)
162 : : {
163 : : fatal("ECC support is not enabled");
164 : : }
165 : : #endif /* OPENSSL_HAS_ECC */
|