1 /* $OpenBSD: kexecdhc.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 #ifdef OPENSSL_HAS_ECC
47 #include <openssl/ecdh.h>
50 kexecdh_client(Kex
*kex
)
53 EC_POINT
*server_public
;
54 const EC_GROUP
*group
;
55 BIGNUM
*shared_secret
;
57 u_char
*server_host_key_blob
= NULL
, *signature
= NULL
;
59 u_int klen
, slen
, sbloblen
, hashlen
;
62 if ((curve_nid
= kex_ecdh_name_to_nid(kex
->name
)) == -1)
63 fatal("%s: unsupported ECDH curve \"%s\"", __func__
, kex
->name
);
64 if ((client_key
= EC_KEY_new_by_curve_name(curve_nid
)) == NULL
)
65 fatal("%s: EC_KEY_new_by_curve_name failed", __func__
);
66 if (EC_KEY_generate_key(client_key
) != 1)
67 fatal("%s: EC_KEY_generate_key failed", __func__
);
68 group
= EC_KEY_get0_group(client_key
);
70 packet_start(SSH2_MSG_KEX_ECDH_INIT
);
71 packet_put_ecpoint(group
, EC_KEY_get0_public_key(client_key
));
73 debug("sending SSH2_MSG_KEX_ECDH_INIT");
76 fputs("client private key:\n", stderr
);
77 key_dump_ec_key(client_key
);
80 debug("expecting SSH2_MSG_KEX_ECDH_REPLY");
81 packet_read_expect(SSH2_MSG_KEX_ECDH_REPLY
);
84 server_host_key_blob
= packet_get_string(&sbloblen
);
85 server_host_key
= key_from_blob(server_host_key_blob
, sbloblen
);
86 if (server_host_key
== NULL
)
87 fatal("cannot decode server_host_key_blob");
88 if (server_host_key
->type
!= kex
->hostkey_type
)
89 fatal("type mismatch for decoded server_host_key_blob");
90 if (kex
->verify_host_key
== NULL
)
91 fatal("cannot verify server_host_key");
92 if (kex
->verify_host_key(server_host_key
) == -1)
93 fatal("server_host_key verification failed");
95 /* Q_S, server public key */
96 if ((server_public
= EC_POINT_new(group
)) == NULL
)
97 fatal("%s: EC_POINT_new failed", __func__
);
98 packet_get_ecpoint(group
, server_public
);
100 if (key_ec_validate_public(group
, server_public
) != 0)
101 fatal("%s: invalid server public key", __func__
);
104 fputs("server public key:\n", stderr
);
105 key_dump_ec_point(group
, server_public
);
109 signature
= packet_get_string(&slen
);
112 klen
= (EC_GROUP_get_degree(group
) + 7) / 8;
113 kbuf
= xmalloc(klen
);
114 if (ECDH_compute_key(kbuf
, klen
, server_public
,
115 client_key
, NULL
) != (int)klen
)
116 fatal("%s: ECDH_compute_key failed", __func__
);
119 dump_digest("shared secret", kbuf
, klen
);
121 if ((shared_secret
= BN_new()) == NULL
)
122 fatal("%s: BN_new failed", __func__
);
123 if (BN_bin2bn(kbuf
, klen
, shared_secret
) == NULL
)
124 fatal("%s: BN_bin2bn failed", __func__
);
125 memset(kbuf
, 0, klen
);
128 /* calc and verify H */
132 kex
->client_version_string
,
133 kex
->server_version_string
,
134 buffer_ptr(&kex
->my
), buffer_len(&kex
->my
),
135 buffer_ptr(&kex
->peer
), buffer_len(&kex
->peer
),
136 server_host_key_blob
, sbloblen
,
137 EC_KEY_get0_public_key(client_key
),
142 xfree(server_host_key_blob
);
143 EC_POINT_clear_free(server_public
);
144 EC_KEY_free(client_key
);
146 if (key_verify(server_host_key
, signature
, slen
, hash
, hashlen
) != 1)
147 fatal("key_verify failed for server_host_key");
148 key_free(server_host_key
);
151 /* save session id */
152 if (kex
->session_id
== NULL
) {
153 kex
->session_id_len
= hashlen
;
154 kex
->session_id
= xmalloc(kex
->session_id_len
);
155 memcpy(kex
->session_id
, hash
, kex
->session_id_len
);
158 kex_derive_keys(kex
, hash
, hashlen
, shared_secret
);
159 BN_clear_free(shared_secret
);
162 #else /* OPENSSL_HAS_ECC */
164 kexecdh_client(Kex
*kex
)
166 fatal("ECC support is not enabled");
168 #endif /* OPENSSL_HAS_ECC */