blob: e49a0ef373466ac71871ce32a7d0011d573a90fe [file] [log] [blame]
Damien Millereb8b60e2010-08-31 22:41:14 +10001/* $OpenBSD: kexecdhs.c,v 1.1 2010/08/31 11:54:45 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
Damien Millerc79ff072010-08-31 22:50:48 +100027#include "includes.h"
28
Damien Millereb8b60e2010-08-31 22:41:14 +100029#include <sys/types.h>
30#include <string.h>
31#include <signal.h>
32
33#include <openssl/ecdh.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#ifdef GSSAPI
45#include "ssh-gss.h"
46#endif
47#include "monitor_wrap.h"
48
49void
50kexecdh_server(Kex *kex)
51{
52 EC_POINT *client_public;
53 EC_KEY *server_key;
54 const EC_GROUP *group;
55 BIGNUM *shared_secret;
56 Key *server_host_private, *server_host_public;
57 u_char *server_host_key_blob = NULL, *signature = NULL;
58 u_char *kbuf, *hash;
59 u_int klen, slen, sbloblen, hashlen;
60 int curve_nid;
61
62 curve_nid = kex_ecdh_name_to_nid(kex->name);
63 if ((server_key = EC_KEY_new_by_curve_name(curve_nid)) == NULL)
64 fatal("%s: EC_KEY_new_by_curve_name failed", __func__);
65 if (EC_KEY_generate_key(server_key) != 1)
66 fatal("%s: EC_KEY_generate_key failed", __func__);
67 group = EC_KEY_get0_group(server_key);
68
69#ifdef DEBUG_KEXECDH
70 fputs("server private key:\n", stderr);
71 key_dump_ec_key(server_key);
72#endif
73
74 if (kex->load_host_public_key == NULL ||
75 kex->load_host_private_key == NULL)
76 fatal("Cannot load hostkey");
77 server_host_public = kex->load_host_public_key(kex->hostkey_type);
78 if (server_host_public == NULL)
79 fatal("Unsupported hostkey type %d", kex->hostkey_type);
80 server_host_private = kex->load_host_private_key(kex->hostkey_type);
81 if (server_host_private == NULL)
82 fatal("Missing private key for hostkey type %d",
83 kex->hostkey_type);
84
85 debug("expecting SSH2_MSG_KEX_ECDH_INIT");
86 packet_read_expect(SSH2_MSG_KEX_ECDH_INIT);
87 if ((client_public = EC_POINT_new(group)) == NULL)
88 fatal("%s: EC_POINT_new failed", __func__);
89 packet_get_ecpoint(group, client_public);
90 packet_check_eom();
91
92 if (key_ec_validate_public(group, client_public) != 0)
93 fatal("%s: invalid client public key", __func__);
94
95#ifdef DEBUG_KEXECDH
96 fputs("client public key:\n", stderr);
97 key_dump_ec_point(group, client_public);
98#endif
99
100 /* Calculate shared_secret */
101 klen = (EC_GROUP_get_degree(group) + 7) / 8;
102 kbuf = xmalloc(klen);
103 if (ECDH_compute_key(kbuf, klen, client_public,
104 server_key, NULL) != (int)klen)
105 fatal("%s: ECDH_compute_key failed", __func__);
106
107#ifdef DEBUG_KEXDH
108 dump_digest("shared secret", kbuf, klen);
109#endif
110 if ((shared_secret = BN_new()) == NULL)
111 fatal("%s: BN_new failed", __func__);
112 if (BN_bin2bn(kbuf, klen, shared_secret) == NULL)
113 fatal("%s: BN_bin2bn failed", __func__);
114 memset(kbuf, 0, klen);
115 xfree(kbuf);
116
117 /* calc H */
118 key_to_blob(server_host_public, &server_host_key_blob, &sbloblen);
119 kex_ecdh_hash(
120 kex->evp_md,
121 group,
122 kex->client_version_string,
123 kex->server_version_string,
124 buffer_ptr(&kex->peer), buffer_len(&kex->peer),
125 buffer_ptr(&kex->my), buffer_len(&kex->my),
126 server_host_key_blob, sbloblen,
127 client_public,
128 EC_KEY_get0_public_key(server_key),
129 shared_secret,
130 &hash, &hashlen
131 );
132 EC_POINT_clear_free(client_public);
133
134 /* save session id := H */
135 if (kex->session_id == NULL) {
136 kex->session_id_len = hashlen;
137 kex->session_id = xmalloc(kex->session_id_len);
138 memcpy(kex->session_id, hash, kex->session_id_len);
139 }
140
141 /* sign H */
142 if (PRIVSEP(key_sign(server_host_private, &signature, &slen,
143 hash, hashlen)) < 0)
144 fatal("kexdh_server: key_sign failed");
145
146 /* destroy_sensitive_data(); */
147
148 /* send server hostkey, ECDH pubkey 'Q_S' and signed H */
149 packet_start(SSH2_MSG_KEX_ECDH_REPLY);
150 packet_put_string(server_host_key_blob, sbloblen);
151 packet_put_ecpoint(group, EC_KEY_get0_public_key(server_key));
152 packet_put_string(signature, slen);
153 packet_send();
154
155 xfree(signature);
156 xfree(server_host_key_blob);
157 /* have keys, free server key */
158 EC_KEY_free(server_key);
159
160 kex_derive_keys(kex, hash, hashlen, shared_secret);
161 BN_clear_free(shared_secret);
162 kex_finish(kex);
163}