diff options
author | Dr. Stephen Henson <steve@openssl.org> | 2016-02-04 23:18:57 +0000 |
---|---|---|
committer | Dr. Stephen Henson <steve@openssl.org> | 2016-02-05 00:33:33 +0000 |
commit | 7fc7d1a7bdeda7e448c13e6fecce96a53b7a62d2 (patch) | |
tree | 617025d4cebdd22a19d36618cfd7f69b17d3e9c2 /crypto/ec/ec_asn1.c | |
parent | ac3e3665016e4441475276461d5f910eb9e9ea15 (diff) |
Add EC_KEY_priv2buf()
Add new function EC_KEY_priv2buf() to allocated and encode private
key octet in one call. Update and simplify ASN.1 and print routines.
Reviewed-by: Viktor Dukhovni <viktor@openssl.org>
Diffstat (limited to 'crypto/ec/ec_asn1.c')
-rw-r--r-- | crypto/ec/ec_asn1.c | 51 |
1 files changed, 13 insertions, 38 deletions
diff --git a/crypto/ec/ec_asn1.c b/crypto/ec/ec_asn1.c index 92bfe06ec5..fe2a979892 100644 --- a/crypto/ec/ec_asn1.c +++ b/crypto/ec/ec_asn1.c @@ -1075,8 +1075,9 @@ EC_KEY *d2i_ECPrivateKey(EC_KEY **a, const unsigned char **in, long len) int i2d_ECPrivateKey(EC_KEY *a, unsigned char **out) { int ret = 0, ok = 0; - unsigned char *buffer = NULL; - size_t buf_len = 0, tmp_len; + unsigned char *priv= NULL, *pub= NULL; + size_t privlen, publen; + EC_PRIVATEKEY *priv_key = NULL; if (a == NULL || a->group == NULL || @@ -1092,28 +1093,15 @@ int i2d_ECPrivateKey(EC_KEY *a, unsigned char **out) priv_key->version = a->version; - buf_len = EC_KEY_priv2oct(a, NULL, 0); + privlen = EC_KEY_priv2buf(a, &priv); - if (buf_len == 0) { + if (privlen == 0) { ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_EC_LIB); goto err; } - buffer = OPENSSL_malloc(buf_len); - if (buffer == NULL) { - ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_MALLOC_FAILURE); - goto err; - } - - if (EC_KEY_priv2oct(a, buffer, buf_len) == 0) { - ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_EC_LIB); - goto err; - } - - if (!ASN1_OCTET_STRING_set(priv_key->privateKey, buffer, buf_len)) { - ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_ASN1_LIB); - goto err; - } + ASN1_STRING_set0(priv_key->privateKey, priv, privlen); + priv = NULL; if (!(a->enc_flag & EC_PKEY_NO_PARAMETERS)) { if ((priv_key->parameters = @@ -1131,31 +1119,17 @@ int i2d_ECPrivateKey(EC_KEY *a, unsigned char **out) goto err; } - tmp_len = EC_POINT_point2oct(a->group, a->pub_key, - a->conv_form, NULL, 0, NULL); + publen = EC_KEY_key2buf(a, a->conv_form, &pub, NULL); - if (tmp_len > buf_len) { - unsigned char *tmp_buffer = OPENSSL_realloc(buffer, tmp_len); - if (!tmp_buffer) { - ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_MALLOC_FAILURE); - goto err; - } - buffer = tmp_buffer; - buf_len = tmp_len; - } - - if (!EC_POINT_point2oct(a->group, a->pub_key, - a->conv_form, buffer, buf_len, NULL)) { + if (publen == 0) { ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_EC_LIB); goto err; } priv_key->publicKey->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07); priv_key->publicKey->flags |= ASN1_STRING_FLAG_BITS_LEFT; - if (!ASN1_BIT_STRING_set(priv_key->publicKey, buffer, buf_len)) { - ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_ASN1_LIB); - goto err; - } + ASN1_STRING_set0(priv_key->publicKey, pub, publen); + pub = NULL; } if ((ret = i2d_EC_PRIVATEKEY(priv_key, out)) == 0) { @@ -1164,7 +1138,8 @@ int i2d_ECPrivateKey(EC_KEY *a, unsigned char **out) } ok = 1; err: - OPENSSL_free(buffer); + OPENSSL_clear_free(priv, privlen); + OPENSSL_free(pub); EC_PRIVATEKEY_free(priv_key); return (ok ? ret : 0); } |