Commit d0561b5c authored by Bodo Möller's avatar Bodo Möller
Browse files

fix ECDSA handling

Submitted by: Nils Larsch
parent 4f4b1924
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -113,7 +113,7 @@ EVP_PKEY *d2i_PublicKey(int type, EVP_PKEY **a, unsigned char **pp,
#endif
#ifndef OPENSSL_NO_ECDSA
	case EVP_PKEY_ECDSA:
		if ((ret->pkey.ecdsa = d2i_ECDSAPublicKey(&(ret->pkey.ecdsa), 
		if ((ret->pkey.ecdsa = ECDSAPublicKey_set_octet_string(&(ret->pkey.ecdsa), 
			(const unsigned char **)pp, length)) == NULL)
			{
			ASN1err(ASN1_F_D2I_PUBLICKEY, ERR_R_ASN1_LIB);
+1 −1
Original line number Diff line number Diff line
@@ -85,7 +85,7 @@ int i2d_PublicKey(EVP_PKEY *a, unsigned char **pp)
#endif
#ifndef OPENSSL_NO_ECDSA
	case EVP_PKEY_ECDSA:
		return(i2d_ECDSAPublicKey(a->pkey.ecdsa, pp));
		return(ECDSAPublicKey_get_octet_string(a->pkey.ecdsa, pp));
#endif
	default:
		ASN1err(ASN1_F_I2D_PUBLICKEY,ASN1_R_UNSUPPORTED_PUBLIC_KEY_TYPE);
+19 −8
Original line number Diff line number Diff line
@@ -237,7 +237,8 @@ int ECDSA_print(BIO *bp, const ECDSA *x, int off)
	unsigned char *buffer=NULL;
	int     i, buf_len=0, ret=0, reason=ERR_R_BIO_LIB;
	BIGNUM  *tmp_1=NULL, *tmp_2=NULL, *tmp_3=NULL,
		*tmp_4=NULL, *tmp_5=NULL;
		*tmp_4=NULL, *tmp_5=NULL, *tmp_6=NULL,
		*tmp_7=NULL;
	BN_CTX  *ctx=NULL;
	EC_POINT *point=NULL;
 
@@ -249,7 +250,8 @@ int ECDSA_print(BIO *bp, const ECDSA *x, int off)
		goto err;
		}
	if ((tmp_1 = BN_new()) == NULL || (tmp_2 = BN_new()) == NULL ||
		(tmp_3 = BN_new()) == NULL || (ctx = BN_CTX_new()) == NULL)
		(tmp_3 = BN_new()) == NULL || (ctx = BN_CTX_new()) == NULL ||
		(tmp_6 = BN_new()) == NULL || (tmp_7 = BN_new()) == NULL)
		{
		reason = ERR_R_MALLOC_FAILURE;
		goto err;
@@ -264,7 +266,12 @@ int ECDSA_print(BIO *bp, const ECDSA *x, int off)
		reason = ERR_R_EC_LIB;
		goto err;
		}
	if ((buf_len = EC_POINT_point2oct(x->group, point, POINT_CONVERSION_COMPRESSED, NULL, 0, ctx)) == 0)
	if (!EC_GROUP_get_order(x->group, tmp_6, NULL) || !EC_GROUP_get_cofactor(x->group, tmp_7, NULL))
		{
		reason = ERR_R_EC_LIB;
		goto err;
		}
	if ((buf_len = EC_POINT_point2oct(x->group, point, ECDSA_get_conversion_form(x), NULL, 0, ctx)) == 0)
		{
		reason = ECDSA_R_UNEXPECTED_PARAMETER_LENGTH;
		goto err;
@@ -274,14 +281,14 @@ int ECDSA_print(BIO *bp, const ECDSA *x, int off)
		reason = ERR_R_MALLOC_FAILURE;
		goto err;
		}
	if (!EC_POINT_point2oct(x->group, point, POINT_CONVERSION_COMPRESSED, 
	if (!EC_POINT_point2oct(x->group, point, ECDSA_get_conversion_form(x), 
				buffer, buf_len, ctx)) goto err;
	if ((tmp_4 = BN_bin2bn(buffer, buf_len, NULL)) == NULL)
		{
		reason = ERR_R_BN_LIB;
		goto err;
		}
	if ((i = EC_POINT_point2oct(x->group, x->pub_key, POINT_CONVERSION_COMPRESSED, NULL, 0, ctx)) == 0)
	if ((i = EC_POINT_point2oct(x->group, x->pub_key, ECDSA_get_conversion_form(x), NULL, 0, ctx)) == 0)
		{
		reason = ECDSA_R_UNEXPECTED_PARAMETER_LENGTH;
		goto err;
@@ -292,7 +299,7 @@ int ECDSA_print(BIO *bp, const ECDSA *x, int off)
		buf_len = i;
		goto err;
		}
	if (!EC_POINT_point2oct(x->group, x->pub_key, POINT_CONVERSION_COMPRESSED, 
	if (!EC_POINT_point2oct(x->group, x->pub_key, ECDSA_get_conversion_form(x), 
				buffer, buf_len, ctx))
		{
		reason = ERR_R_EC_LIB;
@@ -330,6 +337,8 @@ int ECDSA_print(BIO *bp, const ECDSA *x, int off)
	if ((tmp_2 != NULL) && !print(bp, "A:   ", tmp_2, buffer, off)) goto err;
	if ((tmp_3 != NULL) && !print(bp, "B:   ", tmp_3, buffer, off)) goto err;
	if ((tmp_4 != NULL) && !print(bp, "Gen: ", tmp_4, buffer, off)) goto err;
	if ((tmp_6 != NULL) && !print(bp, "Order: ", tmp_6, buffer, off)) goto err;
	if ((tmp_7 != NULL) && !print(bp, "Cofactor: ", tmp_7, buffer, off)) goto err;
	ret=1;
err:
	if (!ret)
@@ -339,6 +348,8 @@ err:
	if (tmp_3) BN_free(tmp_3);
	if (tmp_4) BN_free(tmp_4);
	if (tmp_5) BN_free(tmp_5);
	if (tmp_6) BN_free(tmp_6);
	if (tmp_7) BN_free(tmp_7);
	if (ctx)   BN_CTX_free(ctx);
	if (buffer != NULL) OPENSSL_free(buffer);
	return(ret);
@@ -541,13 +552,13 @@ int ECDSAParameters_print(BIO *bp, const ECDSA *x)
	if ((point = EC_GROUP_get0_generator(x->group)) == NULL) goto err;
	if (!EC_GROUP_get_order(x->group, tmp_5, ctx)) goto err;
	if (!EC_GROUP_get_cofactor(x->group, tmp_6, ctx)) goto err;	
	buf_len = EC_POINT_point2oct(x->group, point, POINT_CONVERSION_COMPRESSED, NULL, 0, ctx);
	buf_len = EC_POINT_point2oct(x->group, point, ECDSA_get_conversion_form(x), NULL, 0, ctx);
	if (!buf_len || (buffer = OPENSSL_malloc(buf_len)) == NULL)
	{
		reason = ERR_R_MALLOC_FAILURE;
		goto err;
	}
	if (!EC_POINT_point2oct(x->group, point, POINT_CONVERSION_COMPRESSED, buffer, buf_len, ctx))
	if (!EC_POINT_point2oct(x->group, point, ECDSA_get_conversion_form(x), buffer, buf_len, ctx))
	{
		reason = ERR_R_EC_LIB;
		goto err;
+0 −2
Original line number Diff line number Diff line
@@ -136,7 +136,6 @@ int X509_PUBKEY_set(X509_PUBKEY **x, EVP_PKEY *pkey)
		ECDSA *ecdsa;
		
		ecdsa = pkey->pkey.ecdsa;
		ecdsa->write_params=0;
		ASN1_TYPE_free(a->parameter);
		if ((i = i2d_ECDSAParameters(ecdsa, NULL)) == 0)
			{
@@ -272,7 +271,6 @@ EVP_PKEY *X509_PUBKEY_get(X509_PUBKEY *key)
				X509err(X509_F_X509_PUBKEY_GET, ERR_R_MALLOC_FAILURE);
				goto err;
				}
			ret->pkey.ecdsa->write_params = 0;
			cp = p = a->parameter->value.sequence->data;
			j = a->parameter->value.sequence->length;
			if (!d2i_ECDSAParameters(&ret->pkey.ecdsa, &cp, (long)j))
+18 −3
Original line number Diff line number Diff line
@@ -99,7 +99,7 @@ typedef struct ecdsa_method
struct ecdsa_st
{
	int version;
	int write_params;
	point_conversion_form_t conversion_form;

	EC_GROUP *group;

@@ -164,6 +164,15 @@ int ECDSA_print_fp(FILE *fp, const ECDSA *x, int off);
void 	ECDSA_set_parameter_flags(ECDSA *, int);
int	ECDSA_get_parameter_flags(const ECDSA*);

/* The ECDSA_{set|get}_conversion_type() functions set/get the
 * conversion form for ec-points (see ec.h) in a ECDSA-structure */
void	ECDSA_set_conversion_form(ECDSA *, const point_conversion_form_t);
point_conversion_form_t ECDSA_get_conversion_form(const ECDSA *);
/* The ECDSA_{set|get}_default_conversion_form() functions set/get the 
 * default conversion form */
void	ECDSA_set_default_conversion_form(const point_conversion_form_t);
point_conversion_form_t ECDSA_get_default_conversion_form(void);

/* the basic de- and encode functions ( see ecs_asn1.c ) */
ECDSA   *d2i_ECDSAParameters(ECDSA **a, const unsigned char **in, long len);
int     i2d_ECDSAParameters(ECDSA *a, unsigned char **out);
@@ -171,8 +180,14 @@ int i2d_ECDSAParameters(ECDSA *a, unsigned char **out);
ECDSA   *d2i_ECDSAPrivateKey(ECDSA **a, const unsigned char **in, long len);
int     i2d_ECDSAPrivateKey(ECDSA *a, unsigned char **out);

ECDSA   *d2i_ECDSAPublicKey(ECDSA **a, const unsigned char **in, long len);
int     i2d_ECDSAPublicKey(ECDSA *a, unsigned char **out);
/* ECDSAPublicKey_set_octet_string() sets the public key in the ECDSA-structure.
 * (*a) must be a pointer to a ECDSA-structure with (*a)->group not zero 
 * (e.g. a ECDSA-structure with a valid EC_GROUP-structure) */
ECDSA 	*ECDSAPublicKey_set_octet_string(ECDSA **a, const unsigned char **in, long len);
/* ECDSAPublicKey_get_octet_string() returns the length of the octet string encoding
 * of the public key. If out != NULL then the function returns in *out 
 * a pointer to the octet string */
int 	ECDSAPublicKey_get_octet_string(ECDSA *a, unsigned char **out);


#define ECDSAParameters_dup(x) (ECDSA *)ASN1_dup((int (*)())i2d_ECDSAParameters, \
Loading