- len_1 = (size_t)BN_num_bytes(tmp_1);
- len_2 = (size_t)BN_num_bytes(tmp_2);
-
- if (len_1 == 0)
- {
- /* len_1 == 0 => a == 0 */
- a_buf = &char_zero;
- len_1 = 1;
- }
- else
- {
- if ((buffer_1 = OPENSSL_malloc(len_1)) == NULL)
- {
- ECerr(EC_F_EC_ASN1_GROUP2CURVE,
- ERR_R_MALLOC_FAILURE);
- goto err;
- }
- if ( (len_1 = BN_bn2bin(tmp_1, buffer_1)) == 0)
- {
- ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_BN_LIB);
- goto err;
- }
- a_buf = buffer_1;
- }
-
- if (len_2 == 0)
- {
- /* len_2 == 0 => b == 0 */
- b_buf = &char_zero;
- len_2 = 1;
- }
- else
- {
- if ((buffer_2 = OPENSSL_malloc(len_2)) == NULL)
- {
- ECerr(EC_F_EC_ASN1_GROUP2CURVE,
- ERR_R_MALLOC_FAILURE);
- goto err;
- }
- if ( (len_2 = BN_bn2bin(tmp_2, buffer_2)) == 0)
- {
- ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_BN_LIB);
- goto err;
- }
- b_buf = buffer_2;
- }
-
- /* set a and b */
- if (!M_ASN1_OCTET_STRING_set(curve->a, a_buf, len_1) ||
- !M_ASN1_OCTET_STRING_set(curve->b, b_buf, len_2))
- {
- ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_ASN1_LIB);
- goto err;
- }
-
- /* set the seed (optional) */
- if (group->seed)
- {
- if (!curve->seed)
- if ((curve->seed = ASN1_BIT_STRING_new()) == NULL)
- {
- ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_MALLOC_FAILURE);
- goto err;
- }
- curve->seed->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT|0x07);
- curve->seed->flags |= ASN1_STRING_FLAG_BITS_LEFT;
- if (!ASN1_BIT_STRING_set(curve->seed, group->seed,
- (int)group->seed_len))
- {
- ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_ASN1_LIB);
- goto err;
- }
- }
- else
- {
- if (curve->seed)
- {
- ASN1_BIT_STRING_free(curve->seed);
- curve->seed = NULL;
- }
- }
-
- ok = 1;
-
-err: if (buffer_1)
- OPENSSL_free(buffer_1);
- if (buffer_2)
- OPENSSL_free(buffer_2);
- if (tmp_1)
- BN_free(tmp_1);
- if (tmp_2)
- BN_free(tmp_2);
- return(ok);
- }
-
-static ECPARAMETERS *ec_asn1_group2parameters(const EC_GROUP *group,
- ECPARAMETERS *param)
- {
- int ok=0;
- size_t len=0;
- ECPARAMETERS *ret=NULL;
- BIGNUM *tmp=NULL;
- unsigned char *buffer=NULL;
- const EC_POINT *point=NULL;
- point_conversion_form_t form;
-
- if ((tmp = BN_new()) == NULL)
- {
- ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_MALLOC_FAILURE);
- goto err;
- }
-
- if (param == NULL)
- {
- if ((ret = ECPARAMETERS_new()) == NULL)
- {
- ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS,
- ERR_R_MALLOC_FAILURE);
- goto err;
- }
- }
- else
- ret = param;
-
- /* set the version (always one) */
- ret->version = (long)0x1;
-
- /* set the fieldID */
- if (!ec_asn1_group2fieldid(group, ret->fieldID))
- {
- ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_EC_LIB);
- goto err;
- }
-
- /* set the curve */
- if (!ec_asn1_group2curve(group, ret->curve))
- {
- ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_EC_LIB);
- goto err;
- }
-
- /* set the base point */
- if ((point = EC_GROUP_get0_generator(group)) == NULL)
- {
- ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, EC_R_UNDEFINED_GENERATOR);
- goto err;
- }
-
- form = EC_GROUP_get_point_conversion_form(group);
-
- len = EC_POINT_point2oct(group, point, form, NULL, len, NULL);
- if (len == 0)
- {
- ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_EC_LIB);
- goto err;
- }
- if ((buffer = OPENSSL_malloc(len)) == NULL)
- {
- ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_MALLOC_FAILURE);
- goto err;
- }
- if (!EC_POINT_point2oct(group, point, form, buffer, len, NULL))
- {
- ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_EC_LIB);
- goto err;
- }
- if (ret->base == NULL && (ret->base = ASN1_OCTET_STRING_new()) == NULL)
- {
- ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_MALLOC_FAILURE);
- goto err;
- }
- if (!ASN1_OCTET_STRING_set(ret->base, buffer, len))
- {
- ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_ASN1_LIB);
- goto err;
- }
-
- /* set the order */
- if (!EC_GROUP_get_order(group, tmp, NULL))
- {
- ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_EC_LIB);
- goto err;
- }
- ret->order = BN_to_ASN1_INTEGER(tmp, ret->order);
- if (ret->order == NULL)
- {
- ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_ASN1_LIB);
- goto err;
- }
-
- /* set the cofactor (optional) */
- if (EC_GROUP_get_cofactor(group, tmp, NULL))
- {
- ret->cofactor = BN_to_ASN1_INTEGER(tmp, ret->cofactor);
- if (ret->cofactor == NULL)
- {
- ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_ASN1_LIB);
- goto err;
- }
- }
-
- ok = 1;
-
-err : if(!ok)
- {
- if (ret && !param)
- ECPARAMETERS_free(ret);
- ret = NULL;
- }
- if (tmp)
- BN_free(tmp);
- if (buffer)
- OPENSSL_free(buffer);
- return(ret);
- }
-
-ECPKPARAMETERS *ec_asn1_group2pkparameters(const EC_GROUP *group,
- ECPKPARAMETERS *params)
- {
- int ok = 1, tmp;
- ECPKPARAMETERS *ret = params;
-
- if (ret == NULL)
- {
- if ((ret = ECPKPARAMETERS_new()) == NULL)
- {
- ECerr(EC_F_EC_ASN1_GROUP2PKPARAMETERS,
- ERR_R_MALLOC_FAILURE);
- return NULL;
- }
- }
- else
- {
- if (ret->type == 0 && ret->value.named_curve)
- ASN1_OBJECT_free(ret->value.named_curve);
- else if (ret->type == 1 && ret->value.parameters)
- ECPARAMETERS_free(ret->value.parameters);
- }
-
- if (EC_GROUP_get_asn1_flag(group))
- {
- /* use the asn1 OID to describe the
- * the elliptic curve parameters
- */
- tmp = EC_GROUP_get_curve_name(group);
- if (tmp)
- {
- ret->type = 0;
- if ((ret->value.named_curve = OBJ_nid2obj(tmp)) == NULL)
- ok = 0;
- }
- else
- /* we don't kmow the nid => ERROR */
- ok = 0;
- }
- else
- {
- /* use the ECPARAMETERS structure */
- ret->type = 1;
- if ((ret->value.parameters = ec_asn1_group2parameters(
- group, NULL)) == NULL)
- ok = 0;
- }
-
- if (!ok)
- {
- ECPKPARAMETERS_free(ret);
- return NULL;
- }
- return ret;
- }
-
-static EC_GROUP *ec_asn1_parameters2group(const ECPARAMETERS *params)
- {
- int ok = 0, tmp;
- EC_GROUP *ret = NULL;
- BIGNUM *p = NULL, *a = NULL, *b = NULL;
- EC_POINT *point=NULL;
- long field_bits;
-
- if (!params->fieldID || !params->fieldID->fieldType ||
- !params->fieldID->p.ptr)
- {
- ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
- goto err;
- }
-
- /* now extract the curve parameters a and b */
- if (!params->curve || !params->curve->a ||
- !params->curve->a->data || !params->curve->b ||
- !params->curve->b->data)
- {
- ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
- goto err;
- }
- a = BN_bin2bn(params->curve->a->data, params->curve->a->length, NULL);
- if (a == NULL)
- {
- ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_BN_LIB);
- goto err;
- }
- b = BN_bin2bn(params->curve->b->data, params->curve->b->length, NULL);
- if (b == NULL)
- {
- ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_BN_LIB);
- goto err;
- }
-
- /* get the field parameters */
- tmp = OBJ_obj2nid(params->fieldID->fieldType);
- if (tmp == NID_X9_62_characteristic_two_field)
+ len_1 = (size_t)BN_num_bytes(tmp_1);
+ len_2 = (size_t)BN_num_bytes(tmp_2);
+
+ if (len_1 == 0) {
+ /* len_1 == 0 => a == 0 */
+ a_buf = &char_zero;
+ len_1 = 1;
+ } else {
+ if ((buffer_1 = OPENSSL_malloc(len_1)) == NULL) {
+ ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_MALLOC_FAILURE);
+ goto err;
+ }
+ if ((len_1 = BN_bn2bin(tmp_1, buffer_1)) == 0) {
+ ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_BN_LIB);
+ goto err;
+ }
+ a_buf = buffer_1;
+ }
+
+ if (len_2 == 0) {
+ /* len_2 == 0 => b == 0 */
+ b_buf = &char_zero;
+ len_2 = 1;
+ } else {
+ if ((buffer_2 = OPENSSL_malloc(len_2)) == NULL) {
+ ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_MALLOC_FAILURE);
+ goto err;
+ }
+ if ((len_2 = BN_bn2bin(tmp_2, buffer_2)) == 0) {
+ ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_BN_LIB);
+ goto err;
+ }
+ b_buf = buffer_2;
+ }
+
+ /* set a and b */
+ if (!ASN1_OCTET_STRING_set(curve->a, a_buf, len_1) ||
+ !ASN1_OCTET_STRING_set(curve->b, b_buf, len_2)) {
+ ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_ASN1_LIB);
+ goto err;
+ }
+
+ /* set the seed (optional) */
+ if (group->seed) {
+ if (!curve->seed)
+ if ((curve->seed = ASN1_BIT_STRING_new()) == NULL) {
+ ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_MALLOC_FAILURE);
+ goto err;
+ }
+ curve->seed->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
+ curve->seed->flags |= ASN1_STRING_FLAG_BITS_LEFT;
+ if (!ASN1_BIT_STRING_set(curve->seed, group->seed,
+ (int)group->seed_len)) {
+ ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_ASN1_LIB);
+ goto err;
+ }
+ } else {
+ ASN1_BIT_STRING_free(curve->seed);
+ curve->seed = NULL;
+ }
+
+ ok = 1;
+
+ err:
+ OPENSSL_free(buffer_1);
+ OPENSSL_free(buffer_2);
+ BN_free(tmp_1);
+ BN_free(tmp_2);
+ return (ok);
+}
+
+ECPARAMETERS *EC_GROUP_get_ecparameters(const EC_GROUP *group,
+ ECPARAMETERS *params)
+{
+ size_t len = 0;
+ ECPARAMETERS *ret = NULL;
+ const BIGNUM *tmp;
+ unsigned char *buffer = NULL;
+ const EC_POINT *point = NULL;
+ point_conversion_form_t form;
+
+ if (params == NULL) {
+ if ((ret = ECPARAMETERS_new()) == NULL) {
+ ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, ERR_R_MALLOC_FAILURE);
+ goto err;
+ }
+ } else
+ ret = params;
+
+ /* set the version (always one) */
+ ret->version = (long)0x1;
+
+ /* set the fieldID */
+ if (!ec_asn1_group2fieldid(group, ret->fieldID)) {
+ ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, ERR_R_EC_LIB);
+ goto err;
+ }
+
+ /* set the curve */
+ if (!ec_asn1_group2curve(group, ret->curve)) {
+ ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, ERR_R_EC_LIB);
+ goto err;
+ }
+
+ /* set the base point */
+ if ((point = EC_GROUP_get0_generator(group)) == NULL) {
+ ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, EC_R_UNDEFINED_GENERATOR);
+ goto err;
+ }
+
+ form = EC_GROUP_get_point_conversion_form(group);
+
+ len = EC_POINT_point2buf(group, point, form, &buffer, NULL);
+ if (len == 0) {
+ ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, ERR_R_EC_LIB);
+ goto err;
+ }
+ if (ret->base == NULL && (ret->base = ASN1_OCTET_STRING_new()) == NULL) {
+ OPENSSL_free(buffer);
+ ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, ERR_R_MALLOC_FAILURE);
+ goto err;
+ }
+ ASN1_STRING_set0(ret->base, buffer, len);
+
+ /* set the order */
+ tmp = EC_GROUP_get0_order(group);
+ if (tmp == NULL) {
+ ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, ERR_R_EC_LIB);
+ goto err;
+ }
+ ret->order = BN_to_ASN1_INTEGER(tmp, ret->order);
+ if (ret->order == NULL) {
+ ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, ERR_R_ASN1_LIB);
+ goto err;
+ }
+
+ /* set the cofactor (optional) */
+ tmp = EC_GROUP_get0_cofactor(group);
+ if (tmp != NULL) {
+ ret->cofactor = BN_to_ASN1_INTEGER(tmp, ret->cofactor);
+ if (ret->cofactor == NULL) {
+ ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, ERR_R_ASN1_LIB);
+ goto err;
+ }
+ }
+
+ return ret;
+
+ err:
+ if (params == NULL)
+ ECPARAMETERS_free(ret);
+ return NULL;
+}
+
+ECPKPARAMETERS *EC_GROUP_get_ecpkparameters(const EC_GROUP *group,
+ ECPKPARAMETERS *params)
+{
+ int ok = 1, tmp;
+ ECPKPARAMETERS *ret = params;
+
+ if (ret == NULL) {
+ if ((ret = ECPKPARAMETERS_new()) == NULL) {
+ ECerr(EC_F_EC_GROUP_GET_ECPKPARAMETERS, ERR_R_MALLOC_FAILURE);
+ return NULL;
+ }
+ } else {
+ if (ret->type == 0)
+ ASN1_OBJECT_free(ret->value.named_curve);
+ else if (ret->type == 1 && ret->value.parameters)
+ ECPARAMETERS_free(ret->value.parameters);
+ }
+
+ if (EC_GROUP_get_asn1_flag(group)) {
+ /*
+ * use the asn1 OID to describe the the elliptic curve parameters
+ */
+ tmp = EC_GROUP_get_curve_name(group);
+ if (tmp) {
+ ret->type = 0;
+ if ((ret->value.named_curve = OBJ_nid2obj(tmp)) == NULL)
+ ok = 0;
+ } else
+ /* we don't know the nid => ERROR */
+ ok = 0;
+ } else {
+ /* use the ECPARAMETERS structure */
+ ret->type = 1;
+ if ((ret->value.parameters =
+ EC_GROUP_get_ecparameters(group, NULL)) == NULL)
+ ok = 0;
+ }
+
+ if (!ok) {
+ ECPKPARAMETERS_free(ret);
+ return NULL;
+ }
+ return ret;
+}
+
+EC_GROUP *EC_GROUP_new_from_ecparameters(const ECPARAMETERS *params)
+{
+ int ok = 0, tmp;
+ EC_GROUP *ret = NULL;
+ BIGNUM *p = NULL, *a = NULL, *b = NULL;
+ EC_POINT *point = NULL;
+ long field_bits;
+
+ if (!params->fieldID || !params->fieldID->fieldType ||
+ !params->fieldID->p.ptr) {
+ ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_ASN1_ERROR);
+ goto err;
+ }
+
+ /* now extract the curve parameters a and b */
+ if (!params->curve || !params->curve->a ||
+ !params->curve->a->data || !params->curve->b ||
+ !params->curve->b->data) {
+ ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_ASN1_ERROR);
+ goto err;
+ }
+ a = BN_bin2bn(params->curve->a->data, params->curve->a->length, NULL);
+ if (a == NULL) {
+ ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_BN_LIB);
+ goto err;
+ }
+ b = BN_bin2bn(params->curve->b->data, params->curve->b->length, NULL);
+ if (b == NULL) {
+ ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_BN_LIB);
+ goto err;
+ }
+
+ /* get the field parameters */
+ tmp = OBJ_obj2nid(params->fieldID->fieldType);
+ if (tmp == NID_X9_62_characteristic_two_field)