upstream: Make sshpkt_get_bignum2() allocate the bignum it is

parsing rather than make the caller do it. Saves a lot of boilerplate code.

from markus@ ok djm@

OpenBSD-Commit-ID: 576bf784f9a240f5a1401f7005364e59aed3bce9
This commit is contained in:
djm@openbsd.org 2019-01-21 09:54:11 +00:00 committed by Damien Miller
parent 803178bd5d
commit 7be8572b32
11 changed files with 68 additions and 133 deletions

View File

@ -1,4 +1,4 @@
/* $OpenBSD: kexdhc.c,v 1.24 2018/12/27 03:25:25 djm Exp $ */
/* $OpenBSD: kexdhc.c,v 1.25 2019/01/21 09:54:11 djm Exp $ */
/*
* Copyright (c) 2001 Markus Friedl. All rights reserved.
*
@ -136,13 +136,8 @@ input_kex_dh(int type, u_int32_t seq, struct ssh *ssh)
r = SSH_ERR_SIGNATURE_INVALID;
goto out;
}
/* DH parameter f, server public DH key */
if ((dh_server_pub = BN_new()) == NULL) {
r = SSH_ERR_ALLOC_FAIL;
goto out;
}
/* signed H */
if ((r = sshpkt_get_bignum2(ssh, dh_server_pub)) != 0 ||
/* DH parameter f, server public DH key, signed H */
if ((r = sshpkt_get_bignum2(ssh, &dh_server_pub)) != 0 ||
(r = sshpkt_get_string(ssh, &signature, &slen)) != 0 ||
(r = sshpkt_get_end(ssh)) != 0)
goto out;

View File

@ -1,4 +1,4 @@
/* $OpenBSD: kexdhs.c,v 1.30 2019/01/19 21:43:56 djm Exp $ */
/* $OpenBSD: kexdhs.c,v 1.31 2019/01/21 09:54:11 djm Exp $ */
/*
* Copyright (c) 2001 Markus Friedl. All rights reserved.
*
@ -120,14 +120,10 @@ input_kex_dh_init(int type, u_int32_t seq, struct ssh *ssh)
}
/* key, cert */
if ((dh_client_pub = BN_new()) == NULL) {
r = SSH_ERR_ALLOC_FAIL;
goto out;
}
DH_get0_key(kex->dh, &pub_key, NULL);
if ((r = sshpkt_get_bignum2(ssh, dh_client_pub)) != 0 ||
if ((r = sshpkt_get_bignum2(ssh, &dh_client_pub)) != 0 ||
(r = sshpkt_get_end(ssh)) != 0)
goto out;
DH_get0_key(kex->dh, &pub_key, NULL);
#ifdef DEBUG_KEXDH
fprintf(stderr, "dh_client_pub= ");

View File

@ -1,4 +1,4 @@
/* $OpenBSD: kexgexc.c,v 1.29 2018/12/27 03:25:25 djm Exp $ */
/* $OpenBSD: kexgexc.c,v 1.30 2019/01/21 09:54:11 djm Exp $ */
/*
* Copyright (c) 2000 Niels Provos. All rights reserved.
* Copyright (c) 2001 Markus Friedl. All rights reserved.
@ -100,13 +100,8 @@ input_kex_dh_gex_group(int type, u_int32_t seq, struct ssh *ssh)
debug("got SSH2_MSG_KEX_DH_GEX_GROUP");
if ((p = BN_new()) == NULL ||
(g = BN_new()) == NULL) {
r = SSH_ERR_ALLOC_FAIL;
goto out;
}
if ((r = sshpkt_get_bignum2(ssh, p)) != 0 ||
(r = sshpkt_get_bignum2(ssh, g)) != 0 ||
if ((r = sshpkt_get_bignum2(ssh, &p)) != 0 ||
(r = sshpkt_get_bignum2(ssh, &g)) != 0 ||
(r = sshpkt_get_end(ssh)) != 0)
goto out;
if ((bits = BN_num_bits(p)) < 0 ||
@ -177,13 +172,8 @@ input_kex_dh_gex_reply(int type, u_int32_t seq, struct ssh *ssh)
r = SSH_ERR_SIGNATURE_INVALID;
goto out;
}
/* DH parameter f, server public DH key */
if ((dh_server_pub = BN_new()) == NULL) {
r = SSH_ERR_ALLOC_FAIL;
goto out;
}
/* signed H */
if ((r = sshpkt_get_bignum2(ssh, dh_server_pub)) != 0 ||
/* DH parameter f, server public DH key, signed H */
if ((r = sshpkt_get_bignum2(ssh, &dh_server_pub)) != 0 ||
(r = sshpkt_get_string(ssh, &signature, &slen)) != 0 ||
(r = sshpkt_get_end(ssh)) != 0)
goto out;

View File

@ -1,4 +1,4 @@
/* $OpenBSD: kexgexs.c,v 1.37 2019/01/19 21:43:56 djm Exp $ */
/* $OpenBSD: kexgexs.c,v 1.38 2019/01/21 09:54:11 djm Exp $ */
/*
* Copyright (c) 2000 Niels Provos. All rights reserved.
* Copyright (c) 2001 Markus Friedl. All rights reserved.
@ -150,11 +150,7 @@ input_kex_dh_gex_init(int type, u_int32_t seq, struct ssh *ssh)
}
/* key, cert */
if ((dh_client_pub = BN_new()) == NULL) {
r = SSH_ERR_ALLOC_FAIL;
goto out;
}
if ((r = sshpkt_get_bignum2(ssh, dh_client_pub)) != 0 ||
if ((r = sshpkt_get_bignum2(ssh, &dh_client_pub)) != 0 ||
(r = sshpkt_get_end(ssh)) != 0)
goto out;

View File

@ -1,4 +1,4 @@
/* $OpenBSD: monitor_wrap.c,v 1.111 2019/01/19 21:43:56 djm Exp $ */
/* $OpenBSD: monitor_wrap.c,v 1.112 2019/01/21 09:54:11 djm Exp $ */
/*
* Copyright 2002 Niels Provos <provos@citi.umich.edu>
* Copyright 2002 Markus Friedl <markus@openbsd.org>
@ -202,12 +202,8 @@ mm_choose_dh(int min, int nbits, int max)
if (success == 0)
fatal("%s: MONITOR_ANS_MODULI failed", __func__);
if ((p = BN_new()) == NULL)
fatal("%s: BN_new failed", __func__);
if ((g = BN_new()) == NULL)
fatal("%s: BN_new failed", __func__);
if ((r = sshbuf_get_bignum2(m, p)) != 0 ||
(r = sshbuf_get_bignum2(m, g)) != 0)
if ((r = sshbuf_get_bignum2(m, &p)) != 0 ||
(r = sshbuf_get_bignum2(m, &g)) != 0)
fatal("%s: buffer error: %s", __func__, ssh_err(r));
debug3("%s: remaining %zu", __func__, sshbuf_len(m));

View File

@ -1,4 +1,4 @@
/* $OpenBSD: packet.c,v 1.280 2019/01/19 21:33:14 djm Exp $ */
/* $OpenBSD: packet.c,v 1.281 2019/01/21 09:54:11 djm Exp $ */
/*
* Author: Tatu Ylonen <ylo@cs.hut.fi>
* Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
@ -2559,11 +2559,10 @@ sshpkt_get_ec(struct ssh *ssh, EC_POINT *v, const EC_GROUP *g)
}
#endif /* OPENSSL_HAS_ECC */
int
sshpkt_get_bignum2(struct ssh *ssh, BIGNUM *v)
sshpkt_get_bignum2(struct ssh *ssh, BIGNUM **valp)
{
return sshbuf_get_bignum2(ssh->state->incoming_packet, v);
return sshbuf_get_bignum2(ssh->state->incoming_packet, valp);
}
#endif /* WITH_OPENSSL */

View File

@ -1,4 +1,4 @@
/* $OpenBSD: packet.h,v 1.88 2019/01/19 21:33:14 djm Exp $ */
/* $OpenBSD: packet.h,v 1.89 2019/01/21 09:54:11 djm Exp $ */
/*
* Author: Tatu Ylonen <ylo@cs.hut.fi>
@ -200,7 +200,7 @@ int sshpkt_get_string_direct(struct ssh *ssh, const u_char **valp, size_t *lenp)
int sshpkt_peek_string_direct(struct ssh *ssh, const u_char **valp, size_t *lenp);
int sshpkt_get_cstring(struct ssh *ssh, char **valp, size_t *lenp);
int sshpkt_get_ec(struct ssh *ssh, EC_POINT *v, const EC_GROUP *g);
int sshpkt_get_bignum2(struct ssh *ssh, BIGNUM *v);
int sshpkt_get_bignum2(struct ssh *ssh, BIGNUM **valp);
int sshpkt_get_end(struct ssh *ssh);
void sshpkt_fmt_connection_id(struct ssh *ssh, char *s, size_t l);
const u_char *sshpkt_ptr(struct ssh *, size_t *lenp);

View File

@ -1,4 +1,4 @@
/* $OpenBSD: ssh-ecdsa.c,v 1.14 2018/02/07 02:06:51 jsing Exp $ */
/* $OpenBSD: ssh-ecdsa.c,v 1.16 2019/01/21 09:54:11 djm Exp $ */
/*
* Copyright (c) 2000 Markus Friedl. All rights reserved.
* Copyright (c) 2010 Damien Miller. All rights reserved.
@ -151,15 +151,13 @@ ssh_ecdsa_verify(const struct sshkey *key,
}
/* parse signature */
if ((sig = ECDSA_SIG_new()) == NULL ||
(sig_r = BN_new()) == NULL ||
(sig_s = BN_new()) == NULL) {
ret = SSH_ERR_ALLOC_FAIL;
if (sshbuf_get_bignum2(sigbuf, &sig_r) != 0 ||
sshbuf_get_bignum2(sigbuf, &sig_s) != 0) {
ret = SSH_ERR_INVALID_FORMAT;
goto out;
}
if (sshbuf_get_bignum2(sigbuf, sig_r) != 0 ||
sshbuf_get_bignum2(sigbuf, sig_s) != 0) {
ret = SSH_ERR_INVALID_FORMAT;
if ((sig = ECDSA_SIG_new()) == NULL) {
ret = SSH_ERR_ALLOC_FAIL;
goto out;
}
if (!ECDSA_SIG_set0(sig, sig_r, sig_s)) {

View File

@ -1,4 +1,4 @@
/* $OpenBSD: sshbuf-getput-crypto.c,v 1.6 2019/01/21 09:52:25 djm Exp $ */
/* $OpenBSD: sshbuf-getput-crypto.c,v 1.7 2019/01/21 09:54:11 djm Exp $ */
/*
* Copyright (c) 2011 Damien Miller
*
@ -32,16 +32,25 @@
#include "sshbuf.h"
int
sshbuf_get_bignum2(struct sshbuf *buf, BIGNUM *v)
sshbuf_get_bignum2(struct sshbuf *buf, BIGNUM **valp)
{
BIGNUM *v;
const u_char *d;
size_t len;
int r;
if (valp != NULL)
*valp = NULL;
if ((r = sshbuf_get_bignum2_bytes_direct(buf, &d, &len)) != 0)
return r;
if (v != NULL && BN_bin2bn(d, len, v) == NULL)
return SSH_ERR_ALLOC_FAIL;
if (valp != NULL) {
if ((v = BN_new()) == NULL ||
BN_bin2bn(d, len, v) == NULL) {
BN_clear_free(v);
return SSH_ERR_ALLOC_FAIL;
}
*valp = v;
}
return 0;
}

View File

@ -1,4 +1,4 @@
/* $OpenBSD: sshbuf.h,v 1.12 2019/01/21 09:52:25 djm Exp $ */
/* $OpenBSD: sshbuf.h,v 1.13 2019/01/21 09:54:11 djm Exp $ */
/*
* Copyright (c) 2011 Damien Miller
*
@ -212,7 +212,7 @@ int sshbuf_put_bignum2_bytes(struct sshbuf *buf, const void *v, size_t len);
int sshbuf_get_bignum2_bytes_direct(struct sshbuf *buf,
const u_char **valp, size_t *lenp);
#ifdef WITH_OPENSSL
int sshbuf_get_bignum2(struct sshbuf *buf, BIGNUM *v);
int sshbuf_get_bignum2(struct sshbuf *buf, BIGNUM **valp);
int sshbuf_put_bignum2(struct sshbuf *buf, const BIGNUM *v);
# ifdef OPENSSL_HAS_ECC
int sshbuf_get_ec(struct sshbuf *buf, EC_POINT *v, const EC_GROUP *g);

View File

@ -1,4 +1,4 @@
/* $OpenBSD: sshkey.c,v 1.72 2018/10/11 00:52:46 djm Exp $ */
/* $OpenBSD: sshkey.c,v 1.73 2019/01/21 09:54:11 djm Exp $ */
/*
* Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
* Copyright (c) 2008 Alexander von Gernler. All rights reserved.
@ -2056,13 +2056,8 @@ sshkey_from_blob_internal(struct sshbuf *b, struct sshkey **keyp,
ret = SSH_ERR_ALLOC_FAIL;
goto out;
}
if ((rsa_e = BN_new()) == NULL ||
(rsa_n = BN_new()) == NULL) {
ret = SSH_ERR_ALLOC_FAIL;
goto out;
}
if (sshbuf_get_bignum2(b, rsa_e) != 0 ||
sshbuf_get_bignum2(b, rsa_n) != 0) {
if (sshbuf_get_bignum2(b, &rsa_e) != 0 ||
sshbuf_get_bignum2(b, &rsa_n) != 0) {
ret = SSH_ERR_INVALID_FORMAT;
goto out;
}
@ -2089,17 +2084,10 @@ sshkey_from_blob_internal(struct sshbuf *b, struct sshkey **keyp,
ret = SSH_ERR_ALLOC_FAIL;
goto out;
}
if ((dsa_p = BN_new()) == NULL ||
(dsa_q = BN_new()) == NULL ||
(dsa_g = BN_new()) == NULL ||
(dsa_pub_key = BN_new()) == NULL) {
ret = SSH_ERR_ALLOC_FAIL;
goto out;
}
if (sshbuf_get_bignum2(b, dsa_p) != 0 ||
sshbuf_get_bignum2(b, dsa_q) != 0 ||
sshbuf_get_bignum2(b, dsa_g) != 0 ||
sshbuf_get_bignum2(b, dsa_pub_key) != 0) {
if (sshbuf_get_bignum2(b, &dsa_p) != 0 ||
sshbuf_get_bignum2(b, &dsa_q) != 0 ||
sshbuf_get_bignum2(b, &dsa_g) != 0 ||
sshbuf_get_bignum2(b, &dsa_pub_key) != 0) {
ret = SSH_ERR_INVALID_FORMAT;
goto out;
}
@ -2941,19 +2929,11 @@ sshkey_private_deserialize(struct sshbuf *buf, struct sshkey **kp)
r = SSH_ERR_ALLOC_FAIL;
goto out;
}
if ((dsa_p = BN_new()) == NULL ||
(dsa_q = BN_new()) == NULL ||
(dsa_g = BN_new()) == NULL ||
(dsa_pub_key = BN_new()) == NULL ||
(dsa_priv_key = BN_new()) == NULL) {
r = SSH_ERR_ALLOC_FAIL;
goto out;
}
if ((r = sshbuf_get_bignum2(buf, dsa_p)) != 0 ||
(r = sshbuf_get_bignum2(buf, dsa_q)) != 0 ||
(r = sshbuf_get_bignum2(buf, dsa_g)) != 0 ||
(r = sshbuf_get_bignum2(buf, dsa_pub_key)) != 0 ||
(r = sshbuf_get_bignum2(buf, dsa_priv_key)) != 0)
if ((r = sshbuf_get_bignum2(buf, &dsa_p)) != 0 ||
(r = sshbuf_get_bignum2(buf, &dsa_q)) != 0 ||
(r = sshbuf_get_bignum2(buf, &dsa_g)) != 0 ||
(r = sshbuf_get_bignum2(buf, &dsa_pub_key)) != 0 ||
(r = sshbuf_get_bignum2(buf, &dsa_priv_key)) != 0)
goto out;
if (!DSA_set0_pqg(k->dsa, dsa_p, dsa_q, dsa_g)) {
r = SSH_ERR_LIBCRYPTO_ERROR;
@ -2967,12 +2947,8 @@ sshkey_private_deserialize(struct sshbuf *buf, struct sshkey **kp)
dsa_pub_key = dsa_priv_key = NULL; /* transferred */
break;
case KEY_DSA_CERT:
if ((dsa_priv_key = BN_new()) == NULL) {
r = SSH_ERR_ALLOC_FAIL;
goto out;
}
if ((r = sshkey_froms(buf, &k)) != 0 ||
(r = sshbuf_get_bignum2(buf, dsa_priv_key)) != 0)
(r = sshbuf_get_bignum2(buf, &dsa_priv_key)) != 0)
goto out;
if (!DSA_set0_key(k->dsa, NULL, dsa_priv_key)) {
r = SSH_ERR_LIBCRYPTO_ERROR;
@ -2997,12 +2973,12 @@ sshkey_private_deserialize(struct sshbuf *buf, struct sshkey **kp)
goto out;
}
k->ecdsa = EC_KEY_new_by_curve_name(k->ecdsa_nid);
if (k->ecdsa == NULL || (exponent = BN_new()) == NULL) {
if (k->ecdsa == NULL) {
r = SSH_ERR_LIBCRYPTO_ERROR;
goto out;
}
if ((r = sshbuf_get_eckey(buf, k->ecdsa)) != 0 ||
(r = sshbuf_get_bignum2(buf, exponent)))
(r = sshbuf_get_bignum2(buf, &exponent)))
goto out;
if (EC_KEY_set_private_key(k->ecdsa, exponent) != 1) {
r = SSH_ERR_LIBCRYPTO_ERROR;
@ -3014,12 +2990,8 @@ sshkey_private_deserialize(struct sshbuf *buf, struct sshkey **kp)
goto out;
break;
case KEY_ECDSA_CERT:
if ((exponent = BN_new()) == NULL) {
r = SSH_ERR_LIBCRYPTO_ERROR;
goto out;
}
if ((r = sshkey_froms(buf, &k)) != 0 ||
(r = sshbuf_get_bignum2(buf, exponent)) != 0)
(r = sshbuf_get_bignum2(buf, &exponent)) != 0)
goto out;
if (EC_KEY_set_private_key(k->ecdsa, exponent) != 1) {
r = SSH_ERR_LIBCRYPTO_ERROR;
@ -3036,21 +3008,12 @@ sshkey_private_deserialize(struct sshbuf *buf, struct sshkey **kp)
r = SSH_ERR_ALLOC_FAIL;
goto out;
}
if ((rsa_n = BN_new()) == NULL ||
(rsa_e = BN_new()) == NULL ||
(rsa_d = BN_new()) == NULL ||
(rsa_iqmp = BN_new()) == NULL ||
(rsa_p = BN_new()) == NULL ||
(rsa_q = BN_new()) == NULL) {
r = SSH_ERR_ALLOC_FAIL;
goto out;
}
if ((r = sshbuf_get_bignum2(buf, rsa_n)) != 0 ||
(r = sshbuf_get_bignum2(buf, rsa_e)) != 0 ||
(r = sshbuf_get_bignum2(buf, rsa_d)) != 0 ||
(r = sshbuf_get_bignum2(buf, rsa_iqmp)) != 0 ||
(r = sshbuf_get_bignum2(buf, rsa_p)) != 0 ||
(r = sshbuf_get_bignum2(buf, rsa_q)) != 0)
if ((r = sshbuf_get_bignum2(buf, &rsa_n)) != 0 ||
(r = sshbuf_get_bignum2(buf, &rsa_e)) != 0 ||
(r = sshbuf_get_bignum2(buf, &rsa_d)) != 0 ||
(r = sshbuf_get_bignum2(buf, &rsa_iqmp)) != 0 ||
(r = sshbuf_get_bignum2(buf, &rsa_p)) != 0 ||
(r = sshbuf_get_bignum2(buf, &rsa_q)) != 0)
goto out;
if (!RSA_set0_key(k->rsa, rsa_n, rsa_e, rsa_d)) {
r = SSH_ERR_LIBCRYPTO_ERROR;
@ -3068,18 +3031,11 @@ sshkey_private_deserialize(struct sshbuf *buf, struct sshkey **kp)
goto out;
break;
case KEY_RSA_CERT:
if ((rsa_d = BN_new()) == NULL ||
(rsa_iqmp = BN_new()) == NULL ||
(rsa_p = BN_new()) == NULL ||
(rsa_q = BN_new()) == NULL) {
r = SSH_ERR_ALLOC_FAIL;
goto out;
}
if ((r = sshkey_froms(buf, &k)) != 0 ||
(r = sshbuf_get_bignum2(buf, rsa_d)) != 0 ||
(r = sshbuf_get_bignum2(buf, rsa_iqmp)) != 0 ||
(r = sshbuf_get_bignum2(buf, rsa_p)) != 0 ||
(r = sshbuf_get_bignum2(buf, rsa_q)) != 0)
(r = sshbuf_get_bignum2(buf, &rsa_d)) != 0 ||
(r = sshbuf_get_bignum2(buf, &rsa_iqmp)) != 0 ||
(r = sshbuf_get_bignum2(buf, &rsa_p)) != 0 ||
(r = sshbuf_get_bignum2(buf, &rsa_q)) != 0)
goto out;
if (!RSA_set0_key(k->rsa, NULL, NULL, rsa_d)) {
r = SSH_ERR_LIBCRYPTO_ERROR;