RT3065: automatically generate a missing EC public key
[openssl.git] / crypto / ec / ec_asn1.c
index 175eec53428baa0e51a1817294b3a6dd7ee7e128..26d6360454f95e0c84d33db75e29e98fcea33529 100644 (file)
@@ -89,7 +89,8 @@ int EC_GROUP_get_trinomial_basis(const EC_GROUP *group, unsigned int *k)
        if (group == NULL)
                return 0;
 
-       if (EC_GROUP_method_of(group)->group_set_curve != ec_GF2m_simple_group_set_curve
+       if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) !=
+           NID_X9_62_characteristic_two_field
            || !((group->poly[0] != 0) && (group->poly[1] != 0) && (group->poly[2] == 0)))
                {
                ECerr(EC_F_EC_GROUP_GET_TRINOMIAL_BASIS, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
@@ -107,7 +108,8 @@ int EC_GROUP_get_pentanomial_basis(const EC_GROUP *group, unsigned int *k1,
        if (group == NULL)
                return 0;
 
-       if (EC_GROUP_method_of(group)->group_set_curve != ec_GF2m_simple_group_set_curve
+       if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) !=
+           NID_X9_62_characteristic_two_field
            || !((group->poly[0] != 0) && (group->poly[1] != 0) && (group->poly[2] != 0) && (group->poly[3] != 0) && (group->poly[4] == 0)))
                {
                ECerr(EC_F_EC_GROUP_GET_PENTANOMIAL_BASIS, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
@@ -1181,19 +1183,20 @@ EC_KEY *d2i_ECPrivateKey(EC_KEY **a, const unsigned char **in, long len)
                goto err;
                }
 
+       if (ret->pub_key)
+               EC_POINT_clear_free(ret->pub_key);
+       ret->pub_key = EC_POINT_new(ret->group);
+       if (ret->pub_key == NULL)
+               {
+               ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);
+               goto err;
+               }
+
        if (priv_key->publicKey)
                {
                const unsigned char *pub_oct;
                size_t pub_oct_len;
 
-               if (ret->pub_key)
-                       EC_POINT_clear_free(ret->pub_key);
-               ret->pub_key = EC_POINT_new(ret->group);
-               if (ret->pub_key == NULL)
-                       {
-                       ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);
-                       goto err;
-                       }
                pub_oct     = M_ASN1_STRING_data(priv_key->publicKey);
                pub_oct_len = M_ASN1_STRING_length(priv_key->publicKey);
                /* save the point conversion form */
@@ -1205,6 +1208,16 @@ EC_KEY *d2i_ECPrivateKey(EC_KEY **a, const unsigned char **in, long len)
                        goto err;
                        }
                }
+       else
+               {
+               if (!EC_POINT_mul(ret->group, ret->pub_key, ret->priv_key, NULL, NULL, NULL))
+                       {
+                       ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);
+                       goto err;
+                       }
+               /* Remember the original private-key-only encoding. */
+               ret->enc_flag |= EC_PKEY_NO_PUBKEY;
+               }
 
        ok = 1;
 err:
@@ -1228,7 +1241,8 @@ int       i2d_ECPrivateKey(EC_KEY *a, unsigned char **out)
        size_t          buf_len=0, tmp_len;
        EC_PRIVATEKEY   *priv_key=NULL;
 
-       if (a == NULL || a->group == NULL || a->priv_key == NULL)
+       if (a == NULL || a->group == NULL || a->priv_key == NULL ||
+           (!(a->enc_flag & EC_PKEY_NO_PUBKEY) && a->pub_key == NULL))
                {
                ECerr(EC_F_I2D_ECPRIVATEKEY,
                       ERR_R_PASSED_NULL_PARAMETER);
@@ -1433,8 +1447,11 @@ int i2o_ECPublicKey(EC_KEY *a, unsigned char **out)
                                *out, buf_len, NULL))
                {
                ECerr(EC_F_I2O_ECPUBLICKEY, ERR_R_EC_LIB);
-               OPENSSL_free(*out);
-               *out = NULL;
+               if (new_buffer)
+                       {
+                       OPENSSL_free(*out);
+                       *out = NULL;
+                       }
                return 0;
                }
        if (!new_buffer)