]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - crypto/openssh/kexecdhs.c
Copy head (r256279) to stable/10 as part of the 10.0-RELEASE cycle.
[FreeBSD/stable/10.git] / crypto / openssh / kexecdhs.c
1 /* $OpenBSD: kexecdhs.c,v 1.5 2013/07/19 07:37:48 markus 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 #include <string.h>
31 #include <signal.h>
32
33 #include "xmalloc.h"
34 #include "buffer.h"
35 #include "key.h"
36 #include "cipher.h"
37 #include "kex.h"
38 #include "log.h"
39 #include "packet.h"
40 #include "dh.h"
41 #include "ssh2.h"
42 #ifdef GSSAPI
43 #include "ssh-gss.h"
44 #endif
45 #include "monitor_wrap.h"
46
47 #ifdef OPENSSL_HAS_ECC
48
49 #include <openssl/ecdh.h>
50
51 void
52 kexecdh_server(Kex *kex)
53 {
54         EC_POINT *client_public;
55         EC_KEY *server_key;
56         const EC_GROUP *group;
57         BIGNUM *shared_secret;
58         Key *server_host_private, *server_host_public;
59         u_char *server_host_key_blob = NULL, *signature = NULL;
60         u_char *kbuf, *hash;
61         u_int klen, slen, sbloblen, hashlen;
62
63         if ((server_key = EC_KEY_new_by_curve_name(kex->ec_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
82         debug("expecting SSH2_MSG_KEX_ECDH_INIT");
83         packet_read_expect(SSH2_MSG_KEX_ECDH_INIT);
84         if ((client_public = EC_POINT_new(group)) == NULL)
85                 fatal("%s: EC_POINT_new failed", __func__);
86         packet_get_ecpoint(group, client_public);
87         packet_check_eom();
88
89         if (key_ec_validate_public(group, client_public) != 0)
90                 fatal("%s: invalid client public key", __func__);
91
92 #ifdef DEBUG_KEXECDH
93         fputs("client public key:\n", stderr);
94         key_dump_ec_point(group, client_public);
95 #endif
96
97         /* Calculate shared_secret */
98         klen = (EC_GROUP_get_degree(group) + 7) / 8;
99         kbuf = xmalloc(klen);
100         if (ECDH_compute_key(kbuf, klen, client_public,
101             server_key, NULL) != (int)klen)
102                 fatal("%s: ECDH_compute_key failed", __func__);
103
104 #ifdef DEBUG_KEXDH
105         dump_digest("shared secret", kbuf, klen);
106 #endif
107         if ((shared_secret = BN_new()) == NULL)
108                 fatal("%s: BN_new failed", __func__);
109         if (BN_bin2bn(kbuf, klen, shared_secret) == NULL)
110                 fatal("%s: BN_bin2bn failed", __func__);
111         memset(kbuf, 0, klen);
112         free(kbuf);
113
114         /* calc H */
115         key_to_blob(server_host_public, &server_host_key_blob, &sbloblen);
116         kex_ecdh_hash(
117             kex->evp_md,
118             group,
119             kex->client_version_string,
120             kex->server_version_string,
121             buffer_ptr(&kex->peer), buffer_len(&kex->peer),
122             buffer_ptr(&kex->my), buffer_len(&kex->my),
123             server_host_key_blob, sbloblen,
124             client_public,
125             EC_KEY_get0_public_key(server_key),
126             shared_secret,
127             &hash, &hashlen
128         );
129         EC_POINT_clear_free(client_public);
130
131         /* save session id := H */
132         if (kex->session_id == NULL) {
133                 kex->session_id_len = hashlen;
134                 kex->session_id = xmalloc(kex->session_id_len);
135                 memcpy(kex->session_id, hash, kex->session_id_len);
136         }
137
138         /* sign H */
139         kex->sign(server_host_private, server_host_public, &signature, &slen,
140             hash, hashlen);
141
142         /* destroy_sensitive_data(); */
143
144         /* send server hostkey, ECDH pubkey 'Q_S' and signed H */
145         packet_start(SSH2_MSG_KEX_ECDH_REPLY);
146         packet_put_string(server_host_key_blob, sbloblen);
147         packet_put_ecpoint(group, EC_KEY_get0_public_key(server_key));
148         packet_put_string(signature, slen);
149         packet_send();
150
151         free(signature);
152         free(server_host_key_blob);
153         /* have keys, free server key */
154         EC_KEY_free(server_key);
155
156         kex_derive_keys(kex, hash, hashlen, shared_secret);
157         BN_clear_free(shared_secret);
158         kex_finish(kex);
159 }
160 #else /* OPENSSL_HAS_ECC */
161 void
162 kexecdh_server(Kex *kex)
163 {
164         fatal("ECC support is not enabled");
165 }
166 #endif /* OPENSSL_HAS_ECC */