]> git.ipfire.org Git - thirdparty/hostap.git/commitdiff
DPP: Use crypto_ec_key_parse_priv() when possible
authorCedric Izoard <cedric.izoard@ceva-dsp.com>
Mon, 28 Jun 2021 16:25:27 +0000 (18:25 +0200)
committerJouni Malinen <j@w1.fi>
Tue, 26 Oct 2021 19:49:44 +0000 (22:49 +0300)
Function crypto_ec_key_parse_priv() already parses ASN.1 ECPrivateKey so
use it when possible.

Signed-off-by: Cedric Izoard <cedric.izoard@ceva-dsp.com>
src/common/dpp_backup.c
src/common/dpp_crypto.c

index 836e70e672f4670359d3906e9ce7dc37fc3e0ce7..fb3f776d2ea2eb0c7046fae65ca196cfb3a36854 100644 (file)
@@ -7,8 +7,6 @@
  */
 
 #include "utils/includes.h"
-#include <openssl/opensslv.h>
-#include <openssl/err.h>
 
 #include "utils/common.h"
 #include "crypto/aes.h"
@@ -866,7 +864,6 @@ dpp_parse_one_asymmetric_key(const u8 *buf, size_t len)
        struct asn1_oid oid;
        char txt[80];
        struct dpp_asymmetric_key *key;
-       EC_KEY *eckey;
 
        wpa_hexdump_key(MSG_MSGDUMP, "DPP: OneAsymmetricKey", buf, len);
 
@@ -941,19 +938,9 @@ dpp_parse_one_asymmetric_key(const u8 *buf, size_t len)
        wpa_hexdump_key(MSG_MSGDUMP, "DPP: PrivateKey",
                        hdr.payload, hdr.length);
        pos = hdr.payload + hdr.length;
-       eckey = d2i_ECPrivateKey(NULL, &hdr.payload, hdr.length);
-       if (!eckey) {
-               wpa_printf(MSG_INFO,
-                          "DPP: OpenSSL: d2i_ECPrivateKey() failed: %s",
-                          ERR_error_string(ERR_get_error(), NULL));
+       key->csign = crypto_ec_key_parse_priv(hdr.payload, hdr.length);
+       if (!key->csign)
                goto fail;
-       }
-       key->csign = (struct crypto_ec_key *) EVP_PKEY_new();
-       if (!key->csign ||
-           EVP_PKEY_assign_EC_KEY((EVP_PKEY *) key->csign, eckey) != 1) {
-               EC_KEY_free(eckey);
-               goto fail;
-       }
        if (wpa_debug_show_keys)
                dpp_debug_print_key("DPP: Received c-sign-key", key->csign);
 
@@ -1063,19 +1050,9 @@ dpp_parse_one_asymmetric_key(const u8 *buf, size_t len)
        wpa_hexdump_key(MSG_MSGDUMP, "DPP: privacyProtectionKey",
                        hdr.payload, hdr.length);
        pos = hdr.payload + hdr.length;
-       eckey = d2i_ECPrivateKey(NULL, &hdr.payload, hdr.length);
-       if (!eckey) {
-               wpa_printf(MSG_INFO,
-                          "DPP: OpenSSL: d2i_ECPrivateKey() failed: %s",
-                          ERR_error_string(ERR_get_error(), NULL));
+       key->pp_key = crypto_ec_key_parse_priv(hdr.payload, hdr.length);
+       if (!key->pp_key)
                goto fail;
-       }
-       key->pp_key = (struct crypto_ec_key *) EVP_PKEY_new();
-       if (!key->pp_key ||
-           EVP_PKEY_assign_EC_KEY((EVP_PKEY *) key->pp_key, eckey) != 1) {
-               EC_KEY_free(eckey);
-               goto fail;
-       }
        if (wpa_debug_show_keys)
                dpp_debug_print_key("DPP: Received privacyProtectionKey",
                                    key->pp_key);
index 3119ef9f0408ff24c192484cf1d811e78ed0d0c8..5c899b664440466ee67b2b753a7aa2a6f37cd5df 100644 (file)
@@ -393,45 +393,31 @@ struct crypto_ec_key * dpp_gen_keypair(const struct dpp_curve_params *curve)
 struct crypto_ec_key * dpp_set_keypair(const struct dpp_curve_params **curve,
                                       const u8 *privkey, size_t privkey_len)
 {
-       EVP_PKEY *pkey;
-       EC_KEY *eckey;
-       const EC_GROUP *group;
-       int nid;
+       struct crypto_ec_key *key;
+       int group;
 
-       pkey = EVP_PKEY_new();
-       if (!pkey)
-               return NULL;
-       eckey = d2i_ECPrivateKey(NULL, &privkey, privkey_len);
-       if (!eckey) {
-               wpa_printf(MSG_INFO,
-                          "DPP: OpenSSL: d2i_ECPrivateKey() failed: %s",
-                          ERR_error_string(ERR_get_error(), NULL));
-               EVP_PKEY_free(pkey);
+       key = crypto_ec_key_parse_priv(privkey, privkey_len);
+       if (!key) {
+               wpa_printf(MSG_INFO, "DPP: Failed to parse private key");
                return NULL;
        }
-       group = EC_KEY_get0_group(eckey);
-       if (!group) {
-               EC_KEY_free(eckey);
-               EVP_PKEY_free(pkey);
+
+       group = crypto_ec_key_group(key);
+       if (group < 0) {
+               crypto_ec_key_deinit(key);
                return NULL;
        }
-       nid = EC_GROUP_get_curve_name(group);
-       *curve = dpp_get_curve_nid(nid);
+
+       *curve = dpp_get_curve_ike_group(group);
        if (!*curve) {
                wpa_printf(MSG_INFO,
-                          "DPP: Unsupported curve (nid=%d) in pre-assigned key",
-                          nid);
-               EC_KEY_free(eckey);
-               EVP_PKEY_free(pkey);
+                          "DPP: Unsupported curve (group=%d) in pre-assigned key",
+                          group);
+               crypto_ec_key_deinit(key);
                return NULL;
        }
 
-       if (EVP_PKEY_assign_EC_KEY(pkey, eckey) != 1) {
-               EC_KEY_free(eckey);
-               EVP_PKEY_free(pkey);
-               return NULL;
-       }
-       return (struct crypto_ec_key *) pkey;
+       return key;
 }