#include <openssl/asn1t.h>
#include <openssl/objects.h>
#include "internal/nelem.h"
+#include "crypto/asn1.h"
#include "crypto/asn1_dsa.h"
#ifndef FIPS_MODULE
if (nid == NID_X9_62_prime_field) {
if ((tmp = BN_new()) == NULL) {
- ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
+ ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
goto err;
}
/* the parameters are specified by the prime number p */
char_two = field->p.char_two;
if (char_two == NULL) {
- ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
+ ERR_raise(ERR_LIB_EC, ERR_R_ASN1_LIB);
goto err;
}
char_two->p.tpBasis = ASN1_INTEGER_new();
if (char_two->p.tpBasis == NULL) {
- ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
+ ERR_raise(ERR_LIB_EC, ERR_R_ASN1_LIB);
goto err;
}
if (!ASN1_INTEGER_set(char_two->p.tpBasis, (long)k)) {
char_two->p.ppBasis = X9_62_PENTANOMIAL_new();
if (char_two->p.ppBasis == NULL) {
- ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
+ ERR_raise(ERR_LIB_EC, ERR_R_ASN1_LIB);
goto err;
}
/* for ONB the parameters are (asn1) NULL */
char_two->p.onBasis = ASN1_NULL_new();
if (char_two->p.onBasis == NULL) {
- ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
+ ERR_raise(ERR_LIB_EC, ERR_R_ASN1_LIB);
goto err;
}
}
return 0;
if ((tmp_1 = BN_new()) == NULL || (tmp_2 = BN_new()) == NULL) {
- ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
+ ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
goto err;
}
*/
len = ((size_t)EC_GROUP_get_degree(group) + 7) / 8;
if ((a_buf = OPENSSL_malloc(len)) == NULL
- || (b_buf = OPENSSL_malloc(len)) == NULL) {
- ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
+ || (b_buf = OPENSSL_malloc(len)) == NULL)
goto err;
- }
if (BN_bn2binpad(tmp_1, a_buf, len) < 0
|| BN_bn2binpad(tmp_2, b_buf, len) < 0) {
ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
if (group->seed) {
if (!curve->seed)
if ((curve->seed = ASN1_BIT_STRING_new()) == NULL) {
- ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
+ ERR_raise(ERR_LIB_EC, ERR_R_ASN1_LIB);
goto err;
}
- curve->seed->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
- curve->seed->flags |= ASN1_STRING_FLAG_BITS_LEFT;
+ ossl_asn1_string_set_bits_left(curve->seed, 0);
if (!ASN1_BIT_STRING_set(curve->seed, group->seed,
(int)group->seed_len)) {
ERR_raise(ERR_LIB_EC, ERR_R_ASN1_LIB);
if (params == NULL) {
if ((ret = ECPARAMETERS_new()) == NULL) {
- ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
+ ERR_raise(ERR_LIB_EC, ERR_R_ASN1_LIB);
goto err;
}
} else
}
if (ret->base == NULL && (ret->base = ASN1_OCTET_STRING_new()) == NULL) {
OPENSSL_free(buffer);
- ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
+ ERR_raise(ERR_LIB_EC, ERR_R_ASN1_LIB);
goto err;
}
ASN1_STRING_set0(ret->base, buffer, len);
if (ret == NULL) {
if ((ret = ECPKPARAMETERS_new()) == NULL) {
- ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
+ ERR_raise(ERR_LIB_EC, ERR_R_ASN1_LIB);
return NULL;
}
} else {
ECPARAMETERS_free(ret->value.parameters);
}
- if (EC_GROUP_get_asn1_flag(group)) {
+ if (EC_GROUP_get_asn1_flag(group) == OPENSSL_EC_NAMED_CURVE) {
/*
* use the asn1 OID to describe the elliptic curve parameters
*/
}
if ((p = BN_new()) == NULL) {
- ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
+ ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
goto err;
}
/* extract seed (optional) */
if (params->curve->seed != NULL) {
- OPENSSL_free(ret->seed);
- if ((ret->seed = OPENSSL_malloc(params->curve->seed->length)) == NULL) {
- ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
+ /*
+ * This happens for instance with
+ * fuzz/corpora/asn1/65cf44e85614c62f10cf3b7a7184c26293a19e4a
+ * and causes the OPENSSL_malloc below to choke on the
+ * zero length allocation request.
+ */
+ if (params->curve->seed->length == 0) {
+ ERR_raise(ERR_LIB_EC, EC_R_ASN1_ERROR);
goto err;
}
+ OPENSSL_free(ret->seed);
+ if ((ret->seed = OPENSSL_malloc(params->curve->seed->length)) == NULL)
+ goto err;
memcpy(ret->seed, params->curve->seed->data,
params->curve->seed->length);
ret->seed_len = params->curve->seed->length;
if (params->order == NULL
|| params->base == NULL
- || params->base->data == NULL) {
+ || params->base->data == NULL
+ || params->base->length == 0) {
ERR_raise(ERR_LIB_EC, EC_R_ASN1_ERROR);
goto err;
}
}
/* extract the order */
- if ((a = ASN1_INTEGER_to_BN(params->order, a)) == NULL) {
+ if (ASN1_INTEGER_to_BN(params->order, a) == NULL) {
ERR_raise(ERR_LIB_EC, ERR_R_ASN1_LIB);
goto err;
}
if (params->cofactor == NULL) {
BN_free(b);
b = NULL;
- } else if ((b = ASN1_INTEGER_to_BN(params->cofactor, b)) == NULL) {
+ } else if (ASN1_INTEGER_to_BN(params->cofactor, b) == NULL) {
ERR_raise(ERR_LIB_EC, ERR_R_ASN1_LIB);
goto err;
}
if (a == NULL || *a == NULL) {
if ((ret = EC_KEY_new()) == NULL) {
- ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
+ ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
goto err;
}
} else
}
if ((priv_key = EC_PRIVATEKEY_new()) == NULL) {
- ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
+ ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
goto err;
}
if (!(a->enc_flag & EC_PKEY_NO_PUBKEY)) {
priv_key->publicKey = ASN1_BIT_STRING_new();
if (priv_key->publicKey == NULL) {
- ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
+ ERR_raise(ERR_LIB_EC, ERR_R_ASN1_LIB);
goto err;
}
goto err;
}
- priv_key->publicKey->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
- priv_key->publicKey->flags |= ASN1_STRING_FLAG_BITS_LEFT;
+ ossl_asn1_string_set_bits_left(priv_key->publicKey, 0);
ASN1_STRING_set0(priv_key->publicKey, pub, publen);
pub = NULL;
}
if (a == NULL || *a == NULL) {
if ((ret = EC_KEY_new()) == NULL) {
- ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
+ ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
return NULL;
}
} else
return buf_len;
if (*out == NULL) {
- if ((*out = OPENSSL_malloc(buf_len)) == NULL) {
- ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
+ if ((*out = OPENSSL_malloc(buf_len)) == NULL)
return 0;
- }
new_buffer = 1;
}
if (!EC_POINT_point2oct(a->group, a->pub_key, a->conv_form,
ECDSA_SIG *ECDSA_SIG_new(void)
{
ECDSA_SIG *sig = OPENSSL_zalloc(sizeof(*sig));
- if (sig == NULL)
- ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
+
return sig;
}
sig->r = BN_new();
if (sig->s == NULL)
sig->s = BN_new();
- if (ossl_decode_der_dsa_sig(sig->r, sig->s, ppin, (size_t)len) == 0) {
+ if (sig->r == NULL || sig->s == NULL
+ || ossl_decode_der_dsa_sig(sig->r, sig->s, ppin, (size_t)len) == 0) {
if (psig == NULL || *psig == NULL)
ECDSA_SIG_free(sig);
return NULL;