From 05e82c3b963c33048128baf72a6f6b3a1c10b4c1 Mon Sep 17 00:00:00 2001 From: Damien Miller Date: Thu, 15 May 2014 14:33:43 +1000 Subject: - djm@cvs.openbsd.org 2014/04/30 05:29:56 [bufaux.c bufbn.c bufec.c buffer.c buffer.h sshbuf-getput-basic.c] [sshbuf-getput-crypto.c sshbuf-misc.c sshbuf.c sshbuf.h ssherr.c] [ssherr.h] New buffer API; the first installment of the conversion/replacement of OpenSSH's internals to make them usable as a standalone library. This includes a set of wrappers to make it compatible with the existing buffer API so replacement can occur incrementally. With and ok markus@ Thanks also to Ben Hawkes, David Tomaschik, Ivan Fratric, Matthew Dempsky and Ron Bowes for a detailed review. --- bufec.c | 107 +++++++++------------------------------------------------------- 1 file changed, 15 insertions(+), 92 deletions(-) (limited to 'bufec.c') diff --git a/bufec.c b/bufec.c index 89482b906..b33ede385 100644 --- a/bufec.c +++ b/bufec.c @@ -1,6 +1,7 @@ -/* $OpenBSD: bufec.c,v 1.3 2014/01/31 16:39:19 tedu Exp $ */ +/* $OpenBSD: bufec.c,v 1.4 2014/04/30 05:29:56 djm Exp $ */ + /* - * Copyright (c) 2010 Damien Miller + * Copyright (c) 2012 Damien Miller * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -15,73 +16,25 @@ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ -#include "includes.h" - -#ifdef OPENSSL_HAS_ECC +/* Emulation wrappers for legacy OpenSSH buffer API atop sshbuf */ #include -#include -#include - -#include -#include - -#include "xmalloc.h" #include "buffer.h" #include "log.h" -#include "misc.h" +#include "ssherr.h" -/* - * Maximum supported EC GFp field length is 528 bits. SEC1 uncompressed - * encoding represents this as two bitstring points that should each - * be no longer than the field length, SEC1 specifies a 1 byte - * point type header. - * Being paranoid here may insulate us to parsing problems in - * EC_POINT_oct2point. - */ -#define BUFFER_MAX_ECPOINT_LEN ((528*2 / 8) + 1) - -/* - * Append an EC_POINT to the buffer as a string containing a SEC1 encoded - * uncompressed point. Fortunately OpenSSL handles the gory details for us. - */ int buffer_put_ecpoint_ret(Buffer *buffer, const EC_GROUP *curve, const EC_POINT *point) { - u_char *buf = NULL; - size_t len; - BN_CTX *bnctx; - int ret = -1; + int ret; - /* Determine length */ - if ((bnctx = BN_CTX_new()) == NULL) - fatal("%s: BN_CTX_new failed", __func__); - len = EC_POINT_point2oct(curve, point, POINT_CONVERSION_UNCOMPRESSED, - NULL, 0, bnctx); - if (len > BUFFER_MAX_ECPOINT_LEN) { - error("%s: giant EC point: len = %lu (max %u)", - __func__, (u_long)len, BUFFER_MAX_ECPOINT_LEN); - goto out; - } - /* Convert */ - buf = xmalloc(len); - if (EC_POINT_point2oct(curve, point, POINT_CONVERSION_UNCOMPRESSED, - buf, len, bnctx) != len) { - error("%s: EC_POINT_point2oct length mismatch", __func__); - goto out; - } - /* Append */ - buffer_put_string(buffer, buf, len); - ret = 0; - out: - if (buf != NULL) { - explicit_bzero(buf, len); - free(buf); + if ((ret = sshbuf_put_ec(buffer, point, curve)) != 0) { + error("%s: %s", __func__, ssh_err(ret)); + return -1; } - BN_CTX_free(bnctx); - return ret; + return 0; } void @@ -96,43 +49,13 @@ int buffer_get_ecpoint_ret(Buffer *buffer, const EC_GROUP *curve, EC_POINT *point) { - u_char *buf; - u_int len; - BN_CTX *bnctx; - int ret = -1; + int ret; - if ((buf = buffer_get_string_ret(buffer, &len)) == NULL) { - error("%s: invalid point", __func__); + if ((ret = sshbuf_get_ec(buffer, point, curve)) != 0) { + error("%s: %s", __func__, ssh_err(ret)); return -1; } - if ((bnctx = BN_CTX_new()) == NULL) - fatal("%s: BN_CTX_new failed", __func__); - if (len > BUFFER_MAX_ECPOINT_LEN) { - error("%s: EC_POINT too long: %u > max %u", __func__, - len, BUFFER_MAX_ECPOINT_LEN); - goto out; - } - if (len == 0) { - error("%s: EC_POINT buffer is empty", __func__); - goto out; - } - if (buf[0] != POINT_CONVERSION_UNCOMPRESSED) { - error("%s: EC_POINT is in an incorrect form: " - "0x%02x (want 0x%02x)", __func__, buf[0], - POINT_CONVERSION_UNCOMPRESSED); - goto out; - } - if (EC_POINT_oct2point(curve, point, buf, len, bnctx) != 1) { - error("buffer_get_bignum2_ret: BN_bin2bn failed"); - goto out; - } - /* EC_POINT_oct2point verifies that the point is on the curve for us */ - ret = 0; - out: - BN_CTX_free(bnctx); - explicit_bzero(buf, len); - free(buf); - return ret; + return 0; } void @@ -143,4 +66,4 @@ buffer_get_ecpoint(Buffer *buffer, const EC_GROUP *curve, fatal("%s: buffer error", __func__); } -#endif /* OPENSSL_HAS_ECC */ + -- cgit v1.2.3 From 58538d795e0b662f2f4e5a7193f1204bbe992ddd Mon Sep 17 00:00:00 2001 From: Darren Tucker Date: Wed, 11 Jun 2014 13:39:24 +1000 Subject: - (dtucker) [bufaux.c bufbn.c bufec.c buffer.c] Pull in includes.h for compat stuff, specifically whether or not OpenSSL has ECC. --- ChangeLog | 2 ++ bufaux.c | 2 ++ bufbn.c | 2 ++ bufec.c | 2 ++ buffer.c | 2 ++ 5 files changed, 10 insertions(+) (limited to 'bufec.c') diff --git a/ChangeLog b/ChangeLog index e42d95a7c..00c8b2775 100644 --- a/ChangeLog +++ b/ChangeLog @@ -19,6 +19,8 @@ OpenSSL or if OpenSSL has ECC. - (dtucker) [openbsd-compat/arc4random.c] Use explicit_bzero instead of an assigment that might get optimized out. ok djm@ + - (dtucker) [bufaux.c bufbn.c bufec.c buffer.c] Pull in includes.h for + compat stuff, specifically whether or not OpenSSL has ECC. 20140527 - (djm) [cipher.c] Fix merge botch. diff --git a/bufaux.c b/bufaux.c index aa9b892d9..3976896a9 100644 --- a/bufaux.c +++ b/bufaux.c @@ -17,6 +17,8 @@ /* Emulation wrappers for legacy OpenSSH buffer API atop sshbuf */ +#include "includes.h" + #include #include "buffer.h" diff --git a/bufbn.c b/bufbn.c index 0a519ed2d..b7f7cb122 100644 --- a/bufbn.c +++ b/bufbn.c @@ -18,6 +18,8 @@ /* Emulation wrappers for legacy OpenSSH buffer API atop sshbuf */ +#include "includes.h" + #include #include "buffer.h" diff --git a/bufec.c b/bufec.c index b33ede385..127e56d43 100644 --- a/bufec.c +++ b/bufec.c @@ -18,6 +18,8 @@ /* Emulation wrappers for legacy OpenSSH buffer API atop sshbuf */ +#include "includes.h" + #include #include "buffer.h" diff --git a/buffer.c b/buffer.c index 07bc186d0..c5f708ab2 100644 --- a/buffer.c +++ b/buffer.c @@ -18,6 +18,8 @@ /* Emulation wrappers for legacy OpenSSH buffer API atop sshbuf */ +#include "includes.h" + #include #include "buffer.h" -- cgit v1.2.3 From ed126de8ee04c66640a0ea2697c4aaf36801f100 Mon Sep 17 00:00:00 2001 From: Damien Miller Date: Tue, 26 Aug 2014 08:37:47 +1000 Subject: - (djm) [bufec.c] Skip this file on !ECC OpenSSL --- ChangeLog | 5 ++++- bufec.c | 3 +++ 2 files changed, 7 insertions(+), 1 deletion(-) (limited to 'bufec.c') diff --git a/ChangeLog b/ChangeLog index 5d19ca630..43dab6905 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,4 +1,7 @@ -20140823 +20140825 + - (djm) [bufec.c] Skip this file on !ECC OpenSSL + +20140824 - (djm) [sftp-server.c] Some systems (e.g. Irix) have prctl() but not PR_SET_DUMPABLE, so adjust ifdef; reported by Tom Christensen diff --git a/bufec.c b/bufec.c index 127e56d43..749ce9d4c 100644 --- a/bufec.c +++ b/bufec.c @@ -26,6 +26,8 @@ #include "log.h" #include "ssherr.h" +#ifdef OPENSSL_HAS_ECC + int buffer_put_ecpoint_ret(Buffer *buffer, const EC_GROUP *curve, const EC_POINT *point) @@ -68,4 +70,5 @@ buffer_get_ecpoint(Buffer *buffer, const EC_GROUP *curve, fatal("%s: buffer error", __func__); } +#endif /* OPENSSL_HAS_ECC */ -- cgit v1.2.3