1 /* $OpenBSD: kexecdhs.c,v 1.2 2010/09/22 05:01:29 djm Exp $ */
3 * Copyright (c) 2001 Markus Friedl. All rights reserved.
4 * Copyright (c) 2010 Damien Miller. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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.
29 #include <sys/types.h>
45 #include "monitor_wrap.h"
47 #ifdef OPENSSL_HAS_ECC
49 #include <openssl/ecdh.h>
52 kexecdh_server(Kex
*kex
)
54 EC_POINT
*client_public
;
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
;
61 u_int klen
, slen
, sbloblen
, hashlen
;
64 if ((curve_nid
= kex_ecdh_name_to_nid(kex
->name
)) == -1)
65 fatal("%s: unsupported ECDH curve \"%s\"", __func__
, kex
->name
);
66 if ((server_key
= EC_KEY_new_by_curve_name(curve_nid
)) == NULL
)
67 fatal("%s: EC_KEY_new_by_curve_name failed", __func__
);
68 if (EC_KEY_generate_key(server_key
) != 1)
69 fatal("%s: EC_KEY_generate_key failed", __func__
);
70 group
= EC_KEY_get0_group(server_key
);
73 fputs("server private key:\n", stderr
);
74 key_dump_ec_key(server_key
);
77 if (kex
->load_host_public_key
== NULL
||
78 kex
->load_host_private_key
== NULL
)
79 fatal("Cannot load hostkey");
80 server_host_public
= kex
->load_host_public_key(kex
->hostkey_type
);
81 if (server_host_public
== NULL
)
82 fatal("Unsupported hostkey type %d", kex
->hostkey_type
);
83 server_host_private
= kex
->load_host_private_key(kex
->hostkey_type
);
84 if (server_host_private
== NULL
)
85 fatal("Missing private key for hostkey type %d",
88 debug("expecting SSH2_MSG_KEX_ECDH_INIT");
89 packet_read_expect(SSH2_MSG_KEX_ECDH_INIT
);
90 if ((client_public
= EC_POINT_new(group
)) == NULL
)
91 fatal("%s: EC_POINT_new failed", __func__
);
92 packet_get_ecpoint(group
, client_public
);
95 if (key_ec_validate_public(group
, client_public
) != 0)
96 fatal("%s: invalid client public key", __func__
);
99 fputs("client public key:\n", stderr
);
100 key_dump_ec_point(group
, client_public
);
103 /* Calculate shared_secret */
104 klen
= (EC_GROUP_get_degree(group
) + 7) / 8;
105 kbuf
= xmalloc(klen
);
106 if (ECDH_compute_key(kbuf
, klen
, client_public
,
107 server_key
, NULL
) != (int)klen
)
108 fatal("%s: ECDH_compute_key failed", __func__
);
111 dump_digest("shared secret", kbuf
, klen
);
113 if ((shared_secret
= BN_new()) == NULL
)
114 fatal("%s: BN_new failed", __func__
);
115 if (BN_bin2bn(kbuf
, klen
, shared_secret
) == NULL
)
116 fatal("%s: BN_bin2bn failed", __func__
);
117 memset(kbuf
, 0, klen
);
121 key_to_blob(server_host_public
, &server_host_key_blob
, &sbloblen
);
125 kex
->client_version_string
,
126 kex
->server_version_string
,
127 buffer_ptr(&kex
->peer
), buffer_len(&kex
->peer
),
128 buffer_ptr(&kex
->my
), buffer_len(&kex
->my
),
129 server_host_key_blob
, sbloblen
,
131 EC_KEY_get0_public_key(server_key
),
135 EC_POINT_clear_free(client_public
);
137 /* save session id := H */
138 if (kex
->session_id
== NULL
) {
139 kex
->session_id_len
= hashlen
;
140 kex
->session_id
= xmalloc(kex
->session_id_len
);
141 memcpy(kex
->session_id
, hash
, kex
->session_id_len
);
145 if (PRIVSEP(key_sign(server_host_private
, &signature
, &slen
,
147 fatal("kexdh_server: key_sign failed");
149 /* destroy_sensitive_data(); */
151 /* send server hostkey, ECDH pubkey 'Q_S' and signed H */
152 packet_start(SSH2_MSG_KEX_ECDH_REPLY
);
153 packet_put_string(server_host_key_blob
, sbloblen
);
154 packet_put_ecpoint(group
, EC_KEY_get0_public_key(server_key
));
155 packet_put_string(signature
, slen
);
159 xfree(server_host_key_blob
);
160 /* have keys, free server key */
161 EC_KEY_free(server_key
);
163 kex_derive_keys(kex
, hash
, hashlen
, shared_secret
);
164 BN_clear_free(shared_secret
);
167 #else /* OPENSSL_HAS_ECC */
169 kexecdh_server(Kex
*kex
)
171 fatal("ECC support is not enabled");
173 #endif /* OPENSSL_HAS_ECC */