xref: /openssh-portable/kexecdh.c (revision fa5bd810)
1aaca72d6Sdjm@openbsd.org /* $OpenBSD: kexecdh.c,v 1.10 2019/01/21 10:40:11 djm Exp $ */
2eb8b60e3SDamien Miller /*
3eb8b60e3SDamien Miller  * Copyright (c) 2010 Damien Miller.  All rights reserved.
492dda34eSdjm@openbsd.org  * Copyright (c) 2019 Markus Friedl.  All rights reserved.
5eb8b60e3SDamien Miller  *
6eb8b60e3SDamien Miller  * Redistribution and use in source and binary forms, with or without
7eb8b60e3SDamien Miller  * modification, are permitted provided that the following conditions
8eb8b60e3SDamien Miller  * are met:
9eb8b60e3SDamien Miller  * 1. Redistributions of source code must retain the above copyright
10eb8b60e3SDamien Miller  *    notice, this list of conditions and the following disclaimer.
11eb8b60e3SDamien Miller  * 2. Redistributions in binary form must reproduce the above copyright
12eb8b60e3SDamien Miller  *    notice, this list of conditions and the following disclaimer in the
13eb8b60e3SDamien Miller  *    documentation and/or other materials provided with the distribution.
14eb8b60e3SDamien Miller  *
15eb8b60e3SDamien Miller  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16eb8b60e3SDamien Miller  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17eb8b60e3SDamien Miller  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18eb8b60e3SDamien Miller  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19eb8b60e3SDamien Miller  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20eb8b60e3SDamien Miller  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21eb8b60e3SDamien Miller  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22eb8b60e3SDamien Miller  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23eb8b60e3SDamien Miller  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24eb8b60e3SDamien Miller  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25eb8b60e3SDamien Miller  */
26eb8b60e3SDamien Miller 
27c79ff077SDamien Miller #include "includes.h"
28c79ff077SDamien Miller 
2972ef7c14SDamien Miller #if defined(WITH_OPENSSL) && defined(OPENSSL_HAS_ECC)
306af914a1SDamien Miller 
31eb8b60e3SDamien Miller #include <sys/types.h>
32eb8b60e3SDamien Miller 
3392dda34eSdjm@openbsd.org #include <stdio.h>
34eb8b60e3SDamien Miller #include <string.h>
3592dda34eSdjm@openbsd.org #include <signal.h>
36eb8b60e3SDamien Miller 
37eb8b60e3SDamien Miller #include <openssl/ecdh.h>
38eb8b60e3SDamien Miller 
3957d10cbeSmarkus@openbsd.org #include "sshkey.h"
40eb8b60e3SDamien Miller #include "kex.h"
4157d10cbeSmarkus@openbsd.org #include "sshbuf.h"
42b3051d01SDamien Miller #include "digest.h"
4357d10cbeSmarkus@openbsd.org #include "ssherr.h"
44eb8b60e3SDamien Miller 
4592dda34eSdjm@openbsd.org static int
4671e67fffSdjm@openbsd.org kex_ecdh_dec_key_group(struct kex *, const struct sshbuf *, EC_KEY *key,
4792dda34eSdjm@openbsd.org     const EC_GROUP *, struct sshbuf **);
4892dda34eSdjm@openbsd.org 
4957d10cbeSmarkus@openbsd.org int
kex_ecdh_keypair(struct kex * kex)5092dda34eSdjm@openbsd.org kex_ecdh_keypair(struct kex *kex)
51eb8b60e3SDamien Miller {
5292dda34eSdjm@openbsd.org 	EC_KEY *client_key = NULL;
5392dda34eSdjm@openbsd.org 	const EC_GROUP *group;
5492dda34eSdjm@openbsd.org 	const EC_POINT *public_key;
5592dda34eSdjm@openbsd.org 	struct sshbuf *buf = NULL;
5657d10cbeSmarkus@openbsd.org 	int r;
57eb8b60e3SDamien Miller 
5892dda34eSdjm@openbsd.org 	if ((client_key = EC_KEY_new_by_curve_name(kex->ec_nid)) == NULL) {
5992dda34eSdjm@openbsd.org 		r = SSH_ERR_ALLOC_FAIL;
6092dda34eSdjm@openbsd.org 		goto out;
6192dda34eSdjm@openbsd.org 	}
6292dda34eSdjm@openbsd.org 	if (EC_KEY_generate_key(client_key) != 1) {
6392dda34eSdjm@openbsd.org 		r = SSH_ERR_LIBCRYPTO_ERROR;
6492dda34eSdjm@openbsd.org 		goto out;
6592dda34eSdjm@openbsd.org 	}
6692dda34eSdjm@openbsd.org 	group = EC_KEY_get0_group(client_key);
6792dda34eSdjm@openbsd.org 	public_key = EC_KEY_get0_public_key(client_key);
6892dda34eSdjm@openbsd.org 
6992dda34eSdjm@openbsd.org 	if ((buf = sshbuf_new()) == NULL) {
7092dda34eSdjm@openbsd.org 		r = SSH_ERR_ALLOC_FAIL;
7192dda34eSdjm@openbsd.org 		goto out;
7292dda34eSdjm@openbsd.org 	}
7392dda34eSdjm@openbsd.org 	if ((r = sshbuf_put_ec(buf, public_key, group)) != 0 ||
7492dda34eSdjm@openbsd.org 	    (r = sshbuf_get_u32(buf, NULL)) != 0)
7592dda34eSdjm@openbsd.org 		goto out;
7692dda34eSdjm@openbsd.org #ifdef DEBUG_KEXECDH
7792dda34eSdjm@openbsd.org 	fputs("client private key:\n", stderr);
7892dda34eSdjm@openbsd.org 	sshkey_dump_ec_key(client_key);
7992dda34eSdjm@openbsd.org #endif
8092dda34eSdjm@openbsd.org 	kex->ec_client_key = client_key;
8192dda34eSdjm@openbsd.org 	kex->ec_group = group;
8292dda34eSdjm@openbsd.org 	client_key = NULL;	/* owned by the kex */
83aaca72d6Sdjm@openbsd.org 	kex->client_pub = buf;
8492dda34eSdjm@openbsd.org 	buf = NULL;
8592dda34eSdjm@openbsd.org  out:
8692dda34eSdjm@openbsd.org 	EC_KEY_free(client_key);
8792dda34eSdjm@openbsd.org 	sshbuf_free(buf);
8857d10cbeSmarkus@openbsd.org 	return r;
8957d10cbeSmarkus@openbsd.org }
9092dda34eSdjm@openbsd.org 
9192dda34eSdjm@openbsd.org int
kex_ecdh_enc(struct kex * kex,const struct sshbuf * client_blob,struct sshbuf ** server_blobp,struct sshbuf ** shared_secretp)9271e67fffSdjm@openbsd.org kex_ecdh_enc(struct kex *kex, const struct sshbuf *client_blob,
9392dda34eSdjm@openbsd.org     struct sshbuf **server_blobp, struct sshbuf **shared_secretp)
9492dda34eSdjm@openbsd.org {
9592dda34eSdjm@openbsd.org 	const EC_GROUP *group;
9692dda34eSdjm@openbsd.org 	const EC_POINT *pub_key;
9792dda34eSdjm@openbsd.org 	EC_KEY *server_key = NULL;
9892dda34eSdjm@openbsd.org 	struct sshbuf *server_blob = NULL;
9992dda34eSdjm@openbsd.org 	int r;
10092dda34eSdjm@openbsd.org 
10192dda34eSdjm@openbsd.org 	*server_blobp = NULL;
10292dda34eSdjm@openbsd.org 	*shared_secretp = NULL;
10392dda34eSdjm@openbsd.org 
10492dda34eSdjm@openbsd.org 	if ((server_key = EC_KEY_new_by_curve_name(kex->ec_nid)) == NULL) {
10592dda34eSdjm@openbsd.org 		r = SSH_ERR_ALLOC_FAIL;
10692dda34eSdjm@openbsd.org 		goto out;
10757d10cbeSmarkus@openbsd.org 	}
10892dda34eSdjm@openbsd.org 	if (EC_KEY_generate_key(server_key) != 1) {
10992dda34eSdjm@openbsd.org 		r = SSH_ERR_LIBCRYPTO_ERROR;
11092dda34eSdjm@openbsd.org 		goto out;
11192dda34eSdjm@openbsd.org 	}
11292dda34eSdjm@openbsd.org 	group = EC_KEY_get0_group(server_key);
11392dda34eSdjm@openbsd.org 
11492dda34eSdjm@openbsd.org #ifdef DEBUG_KEXECDH
11592dda34eSdjm@openbsd.org 	fputs("server private key:\n", stderr);
11692dda34eSdjm@openbsd.org 	sshkey_dump_ec_key(server_key);
11757d10cbeSmarkus@openbsd.org #endif
11892dda34eSdjm@openbsd.org 	pub_key = EC_KEY_get0_public_key(server_key);
11992dda34eSdjm@openbsd.org 	if ((server_blob = sshbuf_new()) == NULL) {
12092dda34eSdjm@openbsd.org 		r = SSH_ERR_ALLOC_FAIL;
12192dda34eSdjm@openbsd.org 		goto out;
12292dda34eSdjm@openbsd.org 	}
12392dda34eSdjm@openbsd.org 	if ((r = sshbuf_put_ec(server_blob, pub_key, group)) != 0 ||
12492dda34eSdjm@openbsd.org 	    (r = sshbuf_get_u32(server_blob, NULL)) != 0)
12592dda34eSdjm@openbsd.org 		goto out;
12671e67fffSdjm@openbsd.org 	if ((r = kex_ecdh_dec_key_group(kex, client_blob, server_key, group,
12792dda34eSdjm@openbsd.org 	    shared_secretp)) != 0)
12892dda34eSdjm@openbsd.org 		goto out;
12992dda34eSdjm@openbsd.org 	*server_blobp = server_blob;
13092dda34eSdjm@openbsd.org 	server_blob = NULL;
13192dda34eSdjm@openbsd.org  out:
13292dda34eSdjm@openbsd.org 	EC_KEY_free(server_key);
13392dda34eSdjm@openbsd.org 	sshbuf_free(server_blob);
13492dda34eSdjm@openbsd.org 	return r;
13592dda34eSdjm@openbsd.org }
13692dda34eSdjm@openbsd.org 
13792dda34eSdjm@openbsd.org static int
kex_ecdh_dec_key_group(struct kex * kex,const struct sshbuf * ec_blob,EC_KEY * key,const EC_GROUP * group,struct sshbuf ** shared_secretp)13871e67fffSdjm@openbsd.org kex_ecdh_dec_key_group(struct kex *kex, const struct sshbuf *ec_blob,
13992dda34eSdjm@openbsd.org     EC_KEY *key, const EC_GROUP *group, struct sshbuf **shared_secretp)
14092dda34eSdjm@openbsd.org {
14192dda34eSdjm@openbsd.org 	struct sshbuf *buf = NULL;
14292dda34eSdjm@openbsd.org 	BIGNUM *shared_secret = NULL;
14392dda34eSdjm@openbsd.org 	EC_POINT *dh_pub = NULL;
14492dda34eSdjm@openbsd.org 	u_char *kbuf = NULL;
14592dda34eSdjm@openbsd.org 	size_t klen = 0;
14692dda34eSdjm@openbsd.org 	int r;
14792dda34eSdjm@openbsd.org 
14892dda34eSdjm@openbsd.org 	*shared_secretp = NULL;
14992dda34eSdjm@openbsd.org 
15092dda34eSdjm@openbsd.org 	if ((buf = sshbuf_new()) == NULL) {
15192dda34eSdjm@openbsd.org 		r = SSH_ERR_ALLOC_FAIL;
15292dda34eSdjm@openbsd.org 		goto out;
15392dda34eSdjm@openbsd.org 	}
15471e67fffSdjm@openbsd.org 	if ((r = sshbuf_put_stringb(buf, ec_blob)) != 0)
15592dda34eSdjm@openbsd.org 		goto out;
15692dda34eSdjm@openbsd.org 	if ((dh_pub = EC_POINT_new(group)) == NULL) {
15792dda34eSdjm@openbsd.org 		r = SSH_ERR_ALLOC_FAIL;
15892dda34eSdjm@openbsd.org 		goto out;
15992dda34eSdjm@openbsd.org 	}
16092dda34eSdjm@openbsd.org 	if ((r = sshbuf_get_ec(buf, dh_pub, group)) != 0) {
16192dda34eSdjm@openbsd.org 		goto out;
16292dda34eSdjm@openbsd.org 	}
16392dda34eSdjm@openbsd.org 	sshbuf_reset(buf);
16492dda34eSdjm@openbsd.org 
16592dda34eSdjm@openbsd.org #ifdef DEBUG_KEXECDH
16692dda34eSdjm@openbsd.org 	fputs("public key:\n", stderr);
16792dda34eSdjm@openbsd.org 	sshkey_dump_ec_point(group, dh_pub);
16892dda34eSdjm@openbsd.org #endif
16992dda34eSdjm@openbsd.org 	if (sshkey_ec_validate_public(group, dh_pub) != 0) {
17092dda34eSdjm@openbsd.org 		r = SSH_ERR_MESSAGE_INCOMPLETE;
17192dda34eSdjm@openbsd.org 		goto out;
17292dda34eSdjm@openbsd.org 	}
17392dda34eSdjm@openbsd.org 	klen = (EC_GROUP_get_degree(group) + 7) / 8;
17492dda34eSdjm@openbsd.org 	if ((kbuf = malloc(klen)) == NULL ||
17592dda34eSdjm@openbsd.org 	    (shared_secret = BN_new()) == NULL) {
17692dda34eSdjm@openbsd.org 		r = SSH_ERR_ALLOC_FAIL;
17792dda34eSdjm@openbsd.org 		goto out;
17892dda34eSdjm@openbsd.org 	}
17992dda34eSdjm@openbsd.org 	if (ECDH_compute_key(kbuf, klen, dh_pub, key, NULL) != (int)klen ||
18092dda34eSdjm@openbsd.org 	    BN_bin2bn(kbuf, klen, shared_secret) == NULL) {
18192dda34eSdjm@openbsd.org 		r = SSH_ERR_LIBCRYPTO_ERROR;
18292dda34eSdjm@openbsd.org 		goto out;
18392dda34eSdjm@openbsd.org 	}
18492dda34eSdjm@openbsd.org #ifdef DEBUG_KEXECDH
18592dda34eSdjm@openbsd.org 	dump_digest("shared secret", kbuf, klen);
18692dda34eSdjm@openbsd.org #endif
18792dda34eSdjm@openbsd.org 	if ((r = sshbuf_put_bignum2(buf, shared_secret)) != 0)
18892dda34eSdjm@openbsd.org 		goto out;
18992dda34eSdjm@openbsd.org 	*shared_secretp = buf;
19092dda34eSdjm@openbsd.org 	buf = NULL;
19192dda34eSdjm@openbsd.org  out:
19292dda34eSdjm@openbsd.org 	EC_POINT_clear_free(dh_pub);
19392dda34eSdjm@openbsd.org 	BN_clear_free(shared_secret);
19492dda34eSdjm@openbsd.org 	freezero(kbuf, klen);
19592dda34eSdjm@openbsd.org 	sshbuf_free(buf);
19692dda34eSdjm@openbsd.org 	return r;
19792dda34eSdjm@openbsd.org }
19892dda34eSdjm@openbsd.org 
19992dda34eSdjm@openbsd.org int
kex_ecdh_dec(struct kex * kex,const struct sshbuf * server_blob,struct sshbuf ** shared_secretp)20071e67fffSdjm@openbsd.org kex_ecdh_dec(struct kex *kex, const struct sshbuf *server_blob,
20192dda34eSdjm@openbsd.org     struct sshbuf **shared_secretp)
20292dda34eSdjm@openbsd.org {
20392dda34eSdjm@openbsd.org 	int r;
20492dda34eSdjm@openbsd.org 
20571e67fffSdjm@openbsd.org 	r = kex_ecdh_dec_key_group(kex, server_blob, kex->ec_client_key,
20692dda34eSdjm@openbsd.org 	    kex->ec_group, shared_secretp);
20792dda34eSdjm@openbsd.org 	EC_KEY_free(kex->ec_client_key);
20892dda34eSdjm@openbsd.org 	kex->ec_client_key = NULL;
20992dda34eSdjm@openbsd.org 	return r;
210eb8b60e3SDamien Miller }
21197370f6cSDarren Tucker 
21297370f6cSDarren Tucker #else
21397370f6cSDarren Tucker 
214*fa5bd810SDarren Tucker #include "ssherr.h"
215*fa5bd810SDarren Tucker 
216edd1d3a6SDamien Miller struct kex;
217edd1d3a6SDamien Miller struct sshbuf;
218edd1d3a6SDamien Miller struct sshkey;
21997370f6cSDarren Tucker 
22097370f6cSDarren Tucker int
kex_ecdh_keypair(struct kex * kex)22197370f6cSDarren Tucker kex_ecdh_keypair(struct kex *kex)
22297370f6cSDarren Tucker {
22397370f6cSDarren Tucker 	return SSH_ERR_SIGN_ALG_UNSUPPORTED;
22497370f6cSDarren Tucker }
22597370f6cSDarren Tucker 
22697370f6cSDarren Tucker int
kex_ecdh_enc(struct kex * kex,const struct sshbuf * client_blob,struct sshbuf ** server_blobp,struct sshbuf ** shared_secretp)22797370f6cSDarren Tucker kex_ecdh_enc(struct kex *kex, const struct sshbuf *client_blob,
22897370f6cSDarren Tucker     struct sshbuf **server_blobp, struct sshbuf **shared_secretp)
22997370f6cSDarren Tucker {
23097370f6cSDarren Tucker 	return SSH_ERR_SIGN_ALG_UNSUPPORTED;
23197370f6cSDarren Tucker }
23297370f6cSDarren Tucker 
23397370f6cSDarren Tucker int
kex_ecdh_dec(struct kex * kex,const struct sshbuf * server_blob,struct sshbuf ** shared_secretp)23497370f6cSDarren Tucker kex_ecdh_dec(struct kex *kex, const struct sshbuf *server_blob,
23597370f6cSDarren Tucker     struct sshbuf **shared_secretp)
23697370f6cSDarren Tucker {
23797370f6cSDarren Tucker 	return SSH_ERR_SIGN_ALG_UNSUPPORTED;
23897370f6cSDarren Tucker }
23972ef7c14SDamien Miller #endif /* defined(WITH_OPENSSL) && defined(OPENSSL_HAS_ECC) */
240