]> git.ipfire.org Git - thirdparty/openssl.git/commitdiff
Extend EVP_PKEY_copy_parameters()
authorDr. Stephen Henson <steve@openssl.org>
Sun, 13 Dec 2015 17:28:40 +0000 (17:28 +0000)
committerDr. Stephen Henson <steve@openssl.org>
Mon, 14 Dec 2015 23:06:14 +0000 (23:06 +0000)
Make EVP_PKEY_copy_parameters() work if the destination has no type
(e.g. if obtained from EVP_PKEY_new()) or the underlying key is NULL.
This is useful where we want to copy the parameters from an existing
key to a new key.

Reviewed-by: Viktor Dukhovni <viktor@openssl.org>
crypto/dh/dh_ameth.c
crypto/dsa/dsa_ameth.c
crypto/ec/ec_ameth.c
crypto/evp/p_lib.c

index 43cba87a1ecb19710da9e7aa33d29840835bf891..7a3d9235cff187410788fabf444a396119b6fc77 100644 (file)
@@ -507,6 +507,11 @@ DH *DHparams_dup(DH *dh)
 
 static int dh_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from)
 {
+    if (to->pkey.dh == NULL) {
+        to->pkey.dh = DH_new();
+        if (to->pkey.dh == NULL)
+            return 0;
+    }
     return int_dh_param_copy(to->pkey.dh, from->pkey.dh,
                              from->ameth == &dhx_asn1_meth);
 }
index d1d32c69598a4dae3d9df931c23b1cd335b3ecf7..92976bc3d522a34ea16f22273949c12c078909bd 100644 (file)
@@ -364,6 +364,12 @@ static int dsa_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from)
 {
     BIGNUM *a;
 
+    if (to->pkey.dsa == NULL) {
+        to->pkey.dsa = DSA_new();
+        if (to->pkey.dsa == NULL)
+            return 0;
+    }
+
     if ((a = BN_dup(from->pkey.dsa->p)) == NULL)
         return 0;
     BN_free(to->pkey.dsa->p);
index 19932d541cf515bbf40f0a9179afb7ffcaf185e0..fb0726246612e095bb9b0ef80be4c7c855e10f9b 100644 (file)
@@ -402,6 +402,11 @@ static int ec_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from)
     EC_GROUP *group = EC_GROUP_dup(EC_KEY_get0_group(from->pkey.ec));
     if (group == NULL)
         return 0;
+    if (to->pkey.ec == NULL) {
+        to->pkey.ec = EC_KEY_new();
+        if (to->pkey.ec == NULL)
+            return 0;
+    }
     if (EC_KEY_set_group(to->pkey.ec, group) == 0)
         return 0;
     EC_GROUP_free(group);
index f07d7e5e4fe6bb536301e8f39bfc0ab8699fbc9c..01f8a728be16b9f26599a252bc400af96def7b6f 100644 (file)
@@ -129,7 +129,10 @@ int EVP_PKEY_save_parameters(EVP_PKEY *pkey, int mode)
 
 int EVP_PKEY_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from)
 {
-    if (to->type != from->type) {
+    if (to->type == EVP_PKEY_NONE) {
+        if (EVP_PKEY_set_type(to, from->type) == 0)
+            return 0;
+    } else if (to->type != from->type) {
         EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS, EVP_R_DIFFERENT_KEY_TYPES);
         goto err;
     }