Split ameth arrays into individual methods
authortb <tb@openbsd.org>
Thu, 4 Jan 2024 16:41:56 +0000 (16:41 +0000)
committertb <tb@openbsd.org>
Thu, 4 Jan 2024 16:41:56 +0000 (16:41 +0000)
For some reason DSA, GOST, and RSA had their ASN.1 methods stored in
an array. This is clumsy and the only benefit is that one saves a few
externs in p_lib.c. They were also arranged by ascending NID because
of bsearch() madness.

Split them up and arrange the methods by name, which is much saner
and simpler.

ok jsing

lib/libcrypto/dsa/dsa_ameth.c
lib/libcrypto/evp/p_lib.c
lib/libcrypto/gost/gostr341001_ameth.c
lib/libcrypto/rsa/rsa_ameth.c

index 4c20b45..d4cce72 100644 (file)
@@ -1,4 +1,4 @@
-/* $OpenBSD: dsa_ameth.c,v 1.55 2023/08/12 07:59:48 tb Exp $ */
+/* $OpenBSD: dsa_ameth.c,v 1.56 2024/01/04 16:41:56 tb Exp $ */
 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
  * project 2006.
  */
@@ -674,64 +674,60 @@ dsa_pkey_ctrl(EVP_PKEY *pkey, int op, long arg1, void *arg2)
        }
 }
 
-/* NB these are sorted in pkey_id order, lowest first */
-
-const EVP_PKEY_ASN1_METHOD dsa_asn1_meths[] = {
-       {
-               .pkey_id = EVP_PKEY_DSA2,
-               .pkey_base_id = EVP_PKEY_DSA,
-               .pkey_flags = ASN1_PKEY_ALIAS
-       },
-
-       {
-               .pkey_id = EVP_PKEY_DSA1,
-               .pkey_base_id = EVP_PKEY_DSA,
-               .pkey_flags = ASN1_PKEY_ALIAS
-       },
-
-       {
-               .pkey_id = EVP_PKEY_DSA4,
-               .pkey_base_id = EVP_PKEY_DSA,
-               .pkey_flags = ASN1_PKEY_ALIAS
-       },
-
-       {
-               .pkey_id = EVP_PKEY_DSA3,
-               .pkey_base_id = EVP_PKEY_DSA,
-               .pkey_flags = ASN1_PKEY_ALIAS
-       },
-
-       {
-               .pkey_id = EVP_PKEY_DSA,
-               .pkey_base_id = EVP_PKEY_DSA,
-
-               .pem_str = "DSA",
-               .info = "OpenSSL DSA method",
-
-               .pub_decode = dsa_pub_decode,
-               .pub_encode = dsa_pub_encode,
-               .pub_cmp = dsa_pub_cmp,
-               .pub_print = dsa_pub_print,
-
-               .priv_decode = dsa_priv_decode,
-               .priv_encode = dsa_priv_encode,
-               .priv_print = dsa_priv_print,
-
-               .pkey_size = dsa_size,
-               .pkey_bits = dsa_bits,
-               .pkey_security_bits = dsa_security_bits,
-
-               .param_decode = dsa_param_decode,
-               .param_encode = dsa_param_encode,
-               .param_missing = dsa_missing_parameters,
-               .param_copy = dsa_copy_parameters,
-               .param_cmp = dsa_cmp_parameters,
-               .param_print = dsa_param_print,
-               .sig_print = dsa_sig_print,
-
-               .pkey_free = dsa_free,
-               .pkey_ctrl = dsa_pkey_ctrl,
-               .old_priv_decode = old_dsa_priv_decode,
-               .old_priv_encode = old_dsa_priv_encode
-       }
+const EVP_PKEY_ASN1_METHOD dsa_asn1_meth = {
+       .pkey_id = EVP_PKEY_DSA,
+       .pkey_base_id = EVP_PKEY_DSA,
+
+       .pem_str = "DSA",
+       .info = "OpenSSL DSA method",
+
+       .pub_decode = dsa_pub_decode,
+       .pub_encode = dsa_pub_encode,
+       .pub_cmp = dsa_pub_cmp,
+       .pub_print = dsa_pub_print,
+
+       .priv_decode = dsa_priv_decode,
+       .priv_encode = dsa_priv_encode,
+       .priv_print = dsa_priv_print,
+
+       .pkey_size = dsa_size,
+       .pkey_bits = dsa_bits,
+       .pkey_security_bits = dsa_security_bits,
+
+       .param_decode = dsa_param_decode,
+       .param_encode = dsa_param_encode,
+       .param_missing = dsa_missing_parameters,
+       .param_copy = dsa_copy_parameters,
+       .param_cmp = dsa_cmp_parameters,
+       .param_print = dsa_param_print,
+       .sig_print = dsa_sig_print,
+
+       .pkey_free = dsa_free,
+       .pkey_ctrl = dsa_pkey_ctrl,
+       .old_priv_decode = old_dsa_priv_decode,
+       .old_priv_encode = old_dsa_priv_encode
+};
+
+const EVP_PKEY_ASN1_METHOD dsa1_asn1_meth = {
+       .pkey_id = EVP_PKEY_DSA1,
+       .pkey_base_id = EVP_PKEY_DSA,
+       .pkey_flags = ASN1_PKEY_ALIAS,
+};
+
+const EVP_PKEY_ASN1_METHOD dsa2_asn1_meth = {
+       .pkey_id = EVP_PKEY_DSA2,
+       .pkey_base_id = EVP_PKEY_DSA,
+       .pkey_flags = ASN1_PKEY_ALIAS,
+};
+
+const EVP_PKEY_ASN1_METHOD dsa3_asn1_meth = {
+       .pkey_id = EVP_PKEY_DSA3,
+       .pkey_base_id = EVP_PKEY_DSA,
+       .pkey_flags = ASN1_PKEY_ALIAS,
+};
+
+const EVP_PKEY_ASN1_METHOD dsa4_asn1_meth = {
+       .pkey_id = EVP_PKEY_DSA4,
+       .pkey_base_id = EVP_PKEY_DSA,
+       .pkey_flags = ASN1_PKEY_ALIAS,
 };
index b64977b..f46666a 100644 (file)
@@ -1,4 +1,4 @@
-/* $OpenBSD: p_lib.c,v 1.52 2024/01/01 15:23:00 tb Exp $ */
+/* $OpenBSD: p_lib.c,v 1.53 2024/01/04 16:41:56 tb Exp $ */
 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  * All rights reserved.
  *
 
 extern const EVP_PKEY_ASN1_METHOD cmac_asn1_meth;
 extern const EVP_PKEY_ASN1_METHOD dh_asn1_meth;
-extern const EVP_PKEY_ASN1_METHOD dsa_asn1_meths[];
+extern const EVP_PKEY_ASN1_METHOD dsa_asn1_meth;
+extern const EVP_PKEY_ASN1_METHOD dsa1_asn1_meth;
+extern const EVP_PKEY_ASN1_METHOD dsa2_asn1_meth;
+extern const EVP_PKEY_ASN1_METHOD dsa3_asn1_meth;
+extern const EVP_PKEY_ASN1_METHOD dsa4_asn1_meth;
 extern const EVP_PKEY_ASN1_METHOD eckey_asn1_meth;
 extern const EVP_PKEY_ASN1_METHOD ed25519_asn1_meth;
 extern const EVP_PKEY_ASN1_METHOD gostimit_asn1_meth;
-extern const EVP_PKEY_ASN1_METHOD gostr01_asn1_meths[];
+extern const EVP_PKEY_ASN1_METHOD gostr01_asn1_meth;
+extern const EVP_PKEY_ASN1_METHOD gostr12_256_asn1_meth;
+extern const EVP_PKEY_ASN1_METHOD gostr12_512_asn1_meth;
 extern const EVP_PKEY_ASN1_METHOD hmac_asn1_meth;
-extern const EVP_PKEY_ASN1_METHOD rsa_asn1_meths[];
+extern const EVP_PKEY_ASN1_METHOD rsa_asn1_meth;
+extern const EVP_PKEY_ASN1_METHOD rsa2_asn1_meth;
 extern const EVP_PKEY_ASN1_METHOD rsa_pss_asn1_meth;
 extern const EVP_PKEY_ASN1_METHOD x25519_asn1_meth;
 
 static const EVP_PKEY_ASN1_METHOD *asn1_methods[] = {
        &cmac_asn1_meth,
        &dh_asn1_meth,
-       &dsa_asn1_meths[0],
-       &dsa_asn1_meths[1],
-       &dsa_asn1_meths[2],
-       &dsa_asn1_meths[3],
-       &dsa_asn1_meths[4],
+       &dsa_asn1_meth,
+       &dsa1_asn1_meth,
+       &dsa2_asn1_meth,
+       &dsa3_asn1_meth,
+       &dsa4_asn1_meth,
        &eckey_asn1_meth,
        &ed25519_asn1_meth,
        &gostimit_asn1_meth,
-       &gostr01_asn1_meths[0],
-       &gostr01_asn1_meths[1],
-       &gostr01_asn1_meths[2],
+       &gostr01_asn1_meth,
+       &gostr12_256_asn1_meth,
+       &gostr12_512_asn1_meth,
        &hmac_asn1_meth,
-       &rsa_asn1_meths[0],
-       &rsa_asn1_meths[1],
+       &rsa_asn1_meth,
+       &rsa2_asn1_meth,
        &rsa_pss_asn1_meth,
        &x25519_asn1_meth,
 };
index fc3bce4..f917827 100644 (file)
@@ -1,4 +1,4 @@
-/* $OpenBSD: gostr341001_ameth.c,v 1.22 2023/12/28 21:53:09 tb Exp $ */
+/* $OpenBSD: gostr341001_ameth.c,v 1.23 2024/01/04 16:41:56 tb Exp $ */
 /*
  * Copyright (c) 2014 Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
  * Copyright (c) 2005-2006 Cryptocom LTD
@@ -682,46 +682,46 @@ pkey_ctrl_gost01(EVP_PKEY *pkey, int op, long arg1, void *arg2)
        return 1;
 }
 
-const EVP_PKEY_ASN1_METHOD gostr01_asn1_meths[] = {
-       {
-               .pkey_id = EVP_PKEY_GOSTR01,
-               .pkey_base_id = EVP_PKEY_GOSTR01,
-               .pkey_flags = ASN1_PKEY_SIGPARAM_NULL,
-
-               .pem_str = "GOST2001",
-               .info = "GOST R 34.10-2001",
-
-               .pkey_free = pkey_free_gost01,
-               .pkey_ctrl = pkey_ctrl_gost01,
-
-               .priv_decode = priv_decode_gost01,
-               .priv_encode = priv_encode_gost01,
-               .priv_print = priv_print_gost01,
-
-               .param_decode = param_decode_gost01,
-               .param_encode = param_encode_gost01,
-               .param_missing = param_missing_gost01,
-               .param_copy = param_copy_gost01,
-               .param_cmp = param_cmp_gost01,
-               .param_print = param_print_gost01,
-
-               .pub_decode = pub_decode_gost01,
-               .pub_encode = pub_encode_gost01,
-               .pub_cmp = pub_cmp_gost01,
-               .pub_print = pub_print_gost01,
-               .pkey_size = pkey_size_gost01,
-               .pkey_bits = pkey_bits_gost01,
-       },
-       {
-               .pkey_id = EVP_PKEY_GOSTR12_256,
-               .pkey_base_id = EVP_PKEY_GOSTR01,
-               .pkey_flags = ASN1_PKEY_ALIAS
-       },
-       {
-               .pkey_id = EVP_PKEY_GOSTR12_512,
-               .pkey_base_id = EVP_PKEY_GOSTR01,
-               .pkey_flags = ASN1_PKEY_ALIAS
-       },
+const EVP_PKEY_ASN1_METHOD gostr01_asn1_meth = {
+       .pkey_id = EVP_PKEY_GOSTR01,
+       .pkey_base_id = EVP_PKEY_GOSTR01,
+       .pkey_flags = ASN1_PKEY_SIGPARAM_NULL,
+
+       .pem_str = "GOST2001",
+       .info = "GOST R 34.10-2001",
+
+       .pkey_free = pkey_free_gost01,
+       .pkey_ctrl = pkey_ctrl_gost01,
+
+       .priv_decode = priv_decode_gost01,
+       .priv_encode = priv_encode_gost01,
+       .priv_print = priv_print_gost01,
+
+       .param_decode = param_decode_gost01,
+       .param_encode = param_encode_gost01,
+       .param_missing = param_missing_gost01,
+       .param_copy = param_copy_gost01,
+       .param_cmp = param_cmp_gost01,
+       .param_print = param_print_gost01,
+
+       .pub_decode = pub_decode_gost01,
+       .pub_encode = pub_encode_gost01,
+       .pub_cmp = pub_cmp_gost01,
+       .pub_print = pub_print_gost01,
+       .pkey_size = pkey_size_gost01,
+       .pkey_bits = pkey_bits_gost01,
+};
+
+const EVP_PKEY_ASN1_METHOD gostr12_256_asn1_meth = {
+       .pkey_id = EVP_PKEY_GOSTR12_256,
+       .pkey_base_id = EVP_PKEY_GOSTR01,
+       .pkey_flags = ASN1_PKEY_ALIAS,
+};
+
+const EVP_PKEY_ASN1_METHOD gostr12_512_asn1_meth = {
+       .pkey_id = EVP_PKEY_GOSTR12_512,
+       .pkey_base_id = EVP_PKEY_GOSTR01,
+       .pkey_flags = ASN1_PKEY_ALIAS,
 };
 
 #endif
index 2266f0c..918e7b8 100644 (file)
@@ -1,4 +1,4 @@
-/* $OpenBSD: rsa_ameth.c,v 1.54 2024/01/01 15:43:02 tb Exp $ */
+/* $OpenBSD: rsa_ameth.c,v 1.55 2024/01/04 16:41:56 tb Exp $ */
 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
  * project 2006.
  */
@@ -1148,47 +1148,45 @@ rsa_cms_encrypt(CMS_RecipientInfo *ri)
 }
 #endif
 
-const EVP_PKEY_ASN1_METHOD rsa_asn1_meths[] = {
-       {
-               .pkey_id = EVP_PKEY_RSA,
-               .pkey_base_id = EVP_PKEY_RSA,
-               .pkey_flags = ASN1_PKEY_SIGPARAM_NULL,
+const EVP_PKEY_ASN1_METHOD rsa_asn1_meth = {
+       .pkey_id = EVP_PKEY_RSA,
+       .pkey_base_id = EVP_PKEY_RSA,
+       .pkey_flags = ASN1_PKEY_SIGPARAM_NULL,
 
-               .pem_str = "RSA",
-               .info = "OpenSSL RSA method",
+       .pem_str = "RSA",
+       .info = "OpenSSL RSA method",
 
-               .pub_decode = rsa_pub_decode,
-               .pub_encode = rsa_pub_encode,
-               .pub_cmp = rsa_pub_cmp,
-               .pub_print = rsa_pub_print,
+       .pub_decode = rsa_pub_decode,
+       .pub_encode = rsa_pub_encode,
+       .pub_cmp = rsa_pub_cmp,
+       .pub_print = rsa_pub_print,
 
-               .priv_decode = rsa_priv_decode,
-               .priv_encode = rsa_priv_encode,
-               .priv_print = rsa_priv_print,
+       .priv_decode = rsa_priv_decode,
+       .priv_encode = rsa_priv_encode,
+       .priv_print = rsa_priv_print,
 
-               .pkey_size = rsa_size,
-               .pkey_bits = rsa_bits,
-               .pkey_security_bits = rsa_security_bits,
+       .pkey_size = rsa_size,
+       .pkey_bits = rsa_bits,
+       .pkey_security_bits = rsa_security_bits,
 
-               .sig_print = rsa_sig_print,
+       .sig_print = rsa_sig_print,
 
-               .pkey_free = rsa_free,
-               .pkey_ctrl = rsa_pkey_ctrl,
-               .old_priv_decode = old_rsa_priv_decode,
-               .old_priv_encode = old_rsa_priv_encode,
-               .item_verify = rsa_item_verify,
-               .item_sign = rsa_item_sign,
+       .pkey_free = rsa_free,
+       .pkey_ctrl = rsa_pkey_ctrl,
+       .old_priv_decode = old_rsa_priv_decode,
+       .old_priv_encode = old_rsa_priv_encode,
+       .item_verify = rsa_item_verify,
+       .item_sign = rsa_item_sign,
 
-               .pkey_check = rsa_pkey_check,
-       },
+       .pkey_check = rsa_pkey_check,
+};
 
-       {
-               .pkey_id = EVP_PKEY_RSA2,
-               .pkey_base_id = EVP_PKEY_RSA,
-               .pkey_flags = ASN1_PKEY_ALIAS,
+const EVP_PKEY_ASN1_METHOD rsa2_asn1_meth = {
+       .pkey_id = EVP_PKEY_RSA2,
+       .pkey_base_id = EVP_PKEY_RSA,
+       .pkey_flags = ASN1_PKEY_ALIAS,
 
-               .pkey_check = rsa_pkey_check,
-       },
+       .pkey_check = rsa_pkey_check,
 };
 
 const EVP_PKEY_ASN1_METHOD rsa_pss_asn1_meth = {