summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/asn1
diff options
context:
space:
mode:
authortb <>2023-12-29 10:59:00 +0000
committertb <>2023-12-29 10:59:00 +0000
commit74f37a921dec4d88238715b69fdd65f00d225fda (patch)
tree75f4dda3052f834420459527c235e1bb9b3f059c /src/lib/libcrypto/asn1
parentc84e468c4aa089677e98a7754e5fc8cb0cd62e54 (diff)
downloadopenbsd-74f37a921dec4d88238715b69fdd65f00d225fda.tar.gz
openbsd-74f37a921dec4d88238715b69fdd65f00d225fda.tar.bz2
openbsd-74f37a921dec4d88238715b69fdd65f00d225fda.zip
Move the EVP_PKEY_asn1_* API that will stay to evp/p_lib.c
Most of these functions are only called from this file internally apart from the pem_str lookups from pem/. In the next major bump we can then remove asn/ameth_lib.c. Also move EVP_PKEY_ASN1_METHOD to evp_local.h. While this is used to dispatch to various ASN.1 decoding routines, it doesn't fit into asn1/ at all.
Diffstat (limited to 'src/lib/libcrypto/asn1')
-rw-r--r--src/lib/libcrypto/asn1/ameth_lib.c140
-rw-r--r--src/lib/libcrypto/asn1/asn1_local.h66
-rw-r--r--src/lib/libcrypto/asn1/t_x509.c4
3 files changed, 4 insertions, 206 deletions
diff --git a/src/lib/libcrypto/asn1/ameth_lib.c b/src/lib/libcrypto/asn1/ameth_lib.c
index 2165bf06b5..cf3a965a64 100644
--- a/src/lib/libcrypto/asn1/ameth_lib.c
+++ b/src/lib/libcrypto/asn1/ameth_lib.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: ameth_lib.c,v 1.39 2023/12/29 10:17:26 tb Exp $ */ 1/* $OpenBSD: ameth_lib.c,v 1.40 2023/12/29 10:59:00 tb Exp $ */
2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 2006. 3 * project 2006.
4 */ 4 */
@@ -62,146 +62,8 @@
62#include <openssl/err.h> 62#include <openssl/err.h>
63#include <openssl/evp.h> 63#include <openssl/evp.h>
64 64
65#include "asn1_local.h"
66#include "evp_local.h" 65#include "evp_local.h"
67 66
68extern const EVP_PKEY_ASN1_METHOD cmac_asn1_meth;
69extern const EVP_PKEY_ASN1_METHOD dh_asn1_meth;
70extern const EVP_PKEY_ASN1_METHOD dsa_asn1_meths[];
71extern const EVP_PKEY_ASN1_METHOD eckey_asn1_meth;
72extern const EVP_PKEY_ASN1_METHOD ed25519_asn1_meth;
73extern const EVP_PKEY_ASN1_METHOD gostimit_asn1_meth;
74extern const EVP_PKEY_ASN1_METHOD gostr01_asn1_meths[];
75extern const EVP_PKEY_ASN1_METHOD hmac_asn1_meth;
76extern const EVP_PKEY_ASN1_METHOD rsa_asn1_meths[];
77extern const EVP_PKEY_ASN1_METHOD rsa_pss_asn1_meth;
78extern const EVP_PKEY_ASN1_METHOD x25519_asn1_meth;
79
80static const EVP_PKEY_ASN1_METHOD *asn1_methods[] = {
81 &cmac_asn1_meth,
82 &dh_asn1_meth,
83 &dsa_asn1_meths[0],
84 &dsa_asn1_meths[1],
85 &dsa_asn1_meths[2],
86 &dsa_asn1_meths[3],
87 &dsa_asn1_meths[4],
88 &eckey_asn1_meth,
89 &ed25519_asn1_meth,
90 &gostimit_asn1_meth,
91 &gostr01_asn1_meths[0],
92 &gostr01_asn1_meths[1],
93 &gostr01_asn1_meths[2],
94 &hmac_asn1_meth,
95 &rsa_asn1_meths[0],
96 &rsa_asn1_meths[1],
97 &rsa_pss_asn1_meth,
98 &x25519_asn1_meth,
99};
100
101#define N_ASN1_METHODS (sizeof(asn1_methods) / sizeof(asn1_methods[0]))
102
103int
104EVP_PKEY_asn1_get_count(void)
105{
106 return N_ASN1_METHODS;
107}
108
109const EVP_PKEY_ASN1_METHOD *
110EVP_PKEY_asn1_get0(int idx)
111{
112 if (idx < 0 || idx >= N_ASN1_METHODS)
113 return NULL;
114
115 return asn1_methods[idx];
116}
117
118static const EVP_PKEY_ASN1_METHOD *
119pkey_asn1_find(int pkey_id)
120{
121 const EVP_PKEY_ASN1_METHOD *ameth;
122 int i;
123
124 for (i = EVP_PKEY_asn1_get_count() - 1; i >= 0; i--) {
125 ameth = EVP_PKEY_asn1_get0(i);
126 if (ameth->pkey_id == pkey_id)
127 return ameth;
128 }
129
130 return NULL;
131}
132
133/*
134 * XXX - fix this. In what looks like an infinite loop, this API only makes two
135 * calls to pkey_asn1_find(): If the type resolves to an aliased ASN.1 method,
136 * the second call will find the method it aliases. Codify this in regress and
137 * make this explicit in code.
138 */
139const EVP_PKEY_ASN1_METHOD *
140EVP_PKEY_asn1_find(ENGINE **pe, int type)
141{
142 const EVP_PKEY_ASN1_METHOD *mp;
143
144 if (pe != NULL)
145 *pe = NULL;
146
147 for (;;) {
148 if ((mp = pkey_asn1_find(type)) == NULL)
149 break;
150 if ((mp->pkey_flags & ASN1_PKEY_ALIAS) == 0)
151 break;
152 type = mp->pkey_base_id;
153 }
154
155 return mp;
156}
157
158const EVP_PKEY_ASN1_METHOD *
159EVP_PKEY_asn1_find_str(ENGINE **pe, const char *str, int len)
160{
161 const EVP_PKEY_ASN1_METHOD *ameth;
162 int i;
163
164 if (len == -1)
165 len = strlen(str);
166 if (pe != NULL)
167 *pe = NULL;
168 for (i = EVP_PKEY_asn1_get_count() - 1; i >= 0; i--) {
169 ameth = EVP_PKEY_asn1_get0(i);
170 if (ameth->pkey_flags & ASN1_PKEY_ALIAS)
171 continue;
172 if (((int)strlen(ameth->pem_str) == len) &&
173 !strncasecmp(ameth->pem_str, str, len))
174 return ameth;
175 }
176 return NULL;
177}
178
179int
180EVP_PKEY_asn1_get0_info(int *ppkey_id, int *ppkey_base_id, int *ppkey_flags,
181 const char **pinfo, const char **ppem_str,
182 const EVP_PKEY_ASN1_METHOD *ameth)
183{
184 if (!ameth)
185 return 0;
186 if (ppkey_id)
187 *ppkey_id = ameth->pkey_id;
188 if (ppkey_base_id)
189 *ppkey_base_id = ameth->pkey_base_id;
190 if (ppkey_flags)
191 *ppkey_flags = ameth->pkey_flags;
192 if (pinfo)
193 *pinfo = ameth->info;
194 if (ppem_str)
195 *ppem_str = ameth->pem_str;
196 return 1;
197}
198
199const EVP_PKEY_ASN1_METHOD*
200EVP_PKEY_get0_asn1(const EVP_PKEY *pkey)
201{
202 return pkey->ameth;
203}
204
205/* 67/*
206 * XXX - remove all the API below here in the next major bump. 68 * XXX - remove all the API below here in the next major bump.
207 */ 69 */
diff --git a/src/lib/libcrypto/asn1/asn1_local.h b/src/lib/libcrypto/asn1/asn1_local.h
index 566ace798b..c1dfa6f68c 100644
--- a/src/lib/libcrypto/asn1/asn1_local.h
+++ b/src/lib/libcrypto/asn1/asn1_local.h
@@ -1,4 +1,4 @@
1/* $OpenBSD: asn1_local.h,v 1.4 2023/07/28 10:00:10 tb Exp $ */ 1/* $OpenBSD: asn1_local.h,v 1.5 2023/12/29 10:59:00 tb Exp $ */
2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 2006. 3 * project 2006.
4 */ 4 */
@@ -89,70 +89,6 @@ struct asn1_pctx_st {
89 unsigned long str_flags; 89 unsigned long str_flags;
90} /* ASN1_PCTX */; 90} /* ASN1_PCTX */;
91 91
92/* ASN1 public key method structure */
93
94struct evp_pkey_asn1_method_st {
95 int pkey_id;
96 int pkey_base_id;
97 unsigned long pkey_flags;
98
99 char *pem_str;
100 char *info;
101
102 int (*pub_decode)(EVP_PKEY *pk, X509_PUBKEY *pub);
103 int (*pub_encode)(X509_PUBKEY *pub, const EVP_PKEY *pk);
104 int (*pub_cmp)(const EVP_PKEY *a, const EVP_PKEY *b);
105 int (*pub_print)(BIO *out, const EVP_PKEY *pkey, int indent,
106 ASN1_PCTX *pctx);
107
108 int (*priv_decode)(EVP_PKEY *pk, const PKCS8_PRIV_KEY_INFO *p8inf);
109 int (*priv_encode)(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pk);
110 int (*priv_print)(BIO *out, const EVP_PKEY *pkey, int indent,
111 ASN1_PCTX *pctx);
112
113 int (*pkey_size)(const EVP_PKEY *pk);
114 int (*pkey_bits)(const EVP_PKEY *pk);
115 int (*pkey_security_bits)(const EVP_PKEY *pk);
116
117 int (*param_decode)(EVP_PKEY *pkey, const unsigned char **pder,
118 int derlen);
119 int (*param_encode)(const EVP_PKEY *pkey, unsigned char **pder);
120 int (*param_missing)(const EVP_PKEY *pk);
121 int (*param_copy)(EVP_PKEY *to, const EVP_PKEY *from);
122 int (*param_cmp)(const EVP_PKEY *a, const EVP_PKEY *b);
123 int (*param_print)(BIO *out, const EVP_PKEY *pkey, int indent,
124 ASN1_PCTX *pctx);
125 int (*sig_print)(BIO *out, const X509_ALGOR *sigalg,
126 const ASN1_STRING *sig, int indent, ASN1_PCTX *pctx);
127
128 void (*pkey_free)(EVP_PKEY *pkey);
129 int (*pkey_ctrl)(EVP_PKEY *pkey, int op, long arg1, void *arg2);
130
131 /* Legacy functions for old PEM */
132
133 int (*old_priv_decode)(EVP_PKEY *pkey, const unsigned char **pder,
134 int derlen);
135 int (*old_priv_encode)(const EVP_PKEY *pkey, unsigned char **pder);
136 /* Custom ASN1 signature verification */
137 int (*item_verify)(EVP_MD_CTX *ctx, const ASN1_ITEM *it, void *asn,
138 X509_ALGOR *a, ASN1_BIT_STRING *sig, EVP_PKEY *pkey);
139 int (*item_sign)(EVP_MD_CTX *ctx, const ASN1_ITEM *it, void *asn,
140 X509_ALGOR *alg1, X509_ALGOR *alg2, ASN1_BIT_STRING *sig);
141
142 int (*pkey_check)(const EVP_PKEY *pk);
143 int (*pkey_public_check)(const EVP_PKEY *pk);
144 int (*pkey_param_check)(const EVP_PKEY *pk);
145
146 int (*set_priv_key)(EVP_PKEY *pk, const unsigned char *private_key,
147 size_t len);
148 int (*set_pub_key)(EVP_PKEY *pk, const unsigned char *public_key,
149 size_t len);
150 int (*get_priv_key)(const EVP_PKEY *pk, unsigned char *out_private_key,
151 size_t *out_len);
152 int (*get_pub_key)(const EVP_PKEY *pk, unsigned char *out_public_key,
153 size_t *out_len);
154} /* EVP_PKEY_ASN1_METHOD */;
155
156/* Method to handle CRL access. 92/* Method to handle CRL access.
157 * In general a CRL could be very large (several Mb) and can consume large 93 * In general a CRL could be very large (several Mb) and can consume large
158 * amounts of resources if stored in memory by multiple processes. 94 * amounts of resources if stored in memory by multiple processes.
diff --git a/src/lib/libcrypto/asn1/t_x509.c b/src/lib/libcrypto/asn1/t_x509.c
index cd98997f6a..6f7bdc79fe 100644
--- a/src/lib/libcrypto/asn1/t_x509.c
+++ b/src/lib/libcrypto/asn1/t_x509.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: t_x509.c,v 1.43 2023/07/07 19:37:52 beck Exp $ */ 1/* $OpenBSD: t_x509.c,v 1.44 2023/12/29 10:59:00 tb Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -77,7 +77,7 @@
77#include <openssl/rsa.h> 77#include <openssl/rsa.h>
78#endif 78#endif
79 79
80#include "asn1_local.h" 80#include "evp_local.h"
81#include "x509_local.h" 81#include "x509_local.h"
82 82
83int 83int