1 /*
2  * Copyright 1995-2025 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9 
10 #define OPENSSL_SUPPRESS_DEPRECATED
11 
12 #include "crypto/evp.h"
13 #include <openssl/core_names.h>
14 #include <openssl/macros.h>
15 #ifndef OPENSSL_NO_DEPRECATED_3_6
16 # include <openssl/engine.h>
17 # include "crypto/asn1.h"
18 #include <openssl/types.h>
19 #else
20 # include "internal/nelem.h"
21 #endif
22 
23 #ifdef OPENSSL_NO_DEPRECATED_3_6
24 /*
25  * This is a hardcoded conversion table for legacy ASN1_METHOD and pkey type.
26  * As the deprecated ASN1 should not enable to add any asn1 method, therefore
27  * this should work.
28  */
29 struct pkid2bid {
30     int pkey_id;
31     int pkey_base_id;
32 };
33 
34 static const struct pkid2bid base_id_conversion[] = {
35     {EVP_PKEY_RSA, EVP_PKEY_RSA},
36     {EVP_PKEY_RSA2, EVP_PKEY_RSA},
37     {EVP_PKEY_RSA_PSS, EVP_PKEY_RSA_PSS},
38 #ifndef OPENSSL_NO_DH
39     {EVP_PKEY_DH, EVP_PKEY_DH},
40     {EVP_PKEY_DHX, EVP_PKEY_DHX},
41 #endif
42 #ifndef OPENSSL_NO_DSA
43     {EVP_PKEY_DSA1, EVP_PKEY_DSA},
44     {EVP_PKEY_DSA4, EVP_PKEY_DSA2},
45     {EVP_PKEY_DSA3, EVP_PKEY_DSA2},
46     {EVP_PKEY_DSA, EVP_PKEY_DSA},
47 #endif
48 #ifndef OPENSSL_NO_EC
49     {EVP_PKEY_EC, EVP_PKEY_EC},
50 #endif
51 #ifndef OPENSSL_NO_ECX
52     {EVP_PKEY_X25519, EVP_PKEY_X25519},
53     {EVP_PKEY_X448, EVP_PKEY_X448},
54     {EVP_PKEY_ED25519, EVP_PKEY_ED25519},
55     {EVP_PKEY_ED448, EVP_PKEY_ED448},
56 #endif
57 #ifndef OPENSSL_NO_SM2
58     {EVP_PKEY_SM2, EVP_PKEY_EC},
59 #endif
60 };
61 #endif
62 
EVP_PKEY_type(int type)63 int EVP_PKEY_type(int type)
64 {
65 #ifndef OPENSSL_NO_DEPRECATED_3_6
66     int ret;
67     const EVP_PKEY_ASN1_METHOD *ameth;
68     ENGINE *e;
69 
70     ameth = EVP_PKEY_asn1_find(&e, type);
71     if (ameth)
72         ret = ameth->pkey_id;
73     else
74         ret = NID_undef;
75 # ifndef OPENSSL_NO_ENGINE
76     ENGINE_finish(e);
77 # endif
78     return ret;
79 #else
80     size_t i;
81 
82     for (i = 0; i < OSSL_NELEM(base_id_conversion); i++) {
83         if (type == base_id_conversion[i].pkey_id)
84             return base_id_conversion[i].pkey_base_id;
85     }
86     return NID_undef;
87 #endif
88 }
89