]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/asn1/ameth_lib.c
Update copyright year
[thirdparty/openssl.git] / crypto / asn1 / ameth_lib.c
CommitLineData
0f113f3e 1/*
f5afac4b 2 * Copyright 2006-2021 The OpenSSL Project Authors. All Rights Reserved.
448be743 3 *
365a2d99 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
b1322259
RS
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
448be743
DSH
8 */
9
e4468e6d
P
10/* We need to use some engine deprecated APIs */
11#define OPENSSL_SUPPRESS_DEPRECATED
12
677963e5 13#include "e_os.h" /* for strncasecmp */
b39fc560 14#include "internal/cryptlib.h"
677963e5 15#include <stdio.h>
448be743
DSH
16#include <openssl/asn1t.h>
17#include <openssl/x509.h>
3c27208f 18#include <openssl/engine.h>
25f2138b
DMSP
19#include "crypto/asn1.h"
20#include "crypto/evp.h"
448be743 21
2c166171 22#include "standard_methods.h"
448be743 23
0f113f3e 24typedef int sk_cmp_fn_type(const char *const *a, const char *const *b);
5ce278a7 25static STACK_OF(EVP_PKEY_ASN1_METHOD) *app_methods = NULL;
18e377b4 26
606f6c47 27DECLARE_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_ASN1_METHOD *,
0f113f3e 28 const EVP_PKEY_ASN1_METHOD *, ameth);
babb3798 29
0f113f3e
MC
30static int ameth_cmp(const EVP_PKEY_ASN1_METHOD *const *a,
31 const EVP_PKEY_ASN1_METHOD *const *b)
32{
33 return ((*a)->pkey_id - (*b)->pkey_id);
34}
448be743 35
606f6c47 36IMPLEMENT_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_ASN1_METHOD *,
0f113f3e 37 const EVP_PKEY_ASN1_METHOD *, ameth);
babb3798 38
e4263314 39int EVP_PKEY_asn1_get_count(void)
0f113f3e 40{
b6eb9827 41 int num = OSSL_NELEM(standard_methods);
0f113f3e
MC
42 if (app_methods)
43 num += sk_EVP_PKEY_ASN1_METHOD_num(app_methods);
44 return num;
45}
e4263314
DSH
46
47const EVP_PKEY_ASN1_METHOD *EVP_PKEY_asn1_get0(int idx)
0f113f3e 48{
b6eb9827 49 int num = OSSL_NELEM(standard_methods);
0f113f3e
MC
50 if (idx < 0)
51 return NULL;
52 if (idx < num)
53 return standard_methods[idx];
54 idx -= num;
55 return sk_EVP_PKEY_ASN1_METHOD_value(app_methods, idx);
56}
e4263314 57
01b8b3c7 58static const EVP_PKEY_ASN1_METHOD *pkey_asn1_find(int type)
0f113f3e
MC
59{
60 EVP_PKEY_ASN1_METHOD tmp;
61 const EVP_PKEY_ASN1_METHOD *t = &tmp, **ret;
12a765a5 62
0f113f3e
MC
63 tmp.pkey_id = type;
64 if (app_methods) {
65 int idx;
66 idx = sk_EVP_PKEY_ASN1_METHOD_find(app_methods, &tmp);
67 if (idx >= 0)
68 return sk_EVP_PKEY_ASN1_METHOD_value(app_methods, idx);
69 }
b6eb9827 70 ret = OBJ_bsearch_ameth(&t, standard_methods, OSSL_NELEM(standard_methods));
12a765a5 71 if (ret == NULL || *ret == NULL)
0f113f3e
MC
72 return NULL;
73 return *ret;
74}
75
76/*
77 * Find an implementation of an ASN1 algorithm. If 'pe' is not NULL also
78 * search through engines and set *pe to a functional reference to the engine
79 * implementing 'type' or NULL if no engine implements it.
01b8b3c7
DSH
80 */
81
82const EVP_PKEY_ASN1_METHOD *EVP_PKEY_asn1_find(ENGINE **pe, int type)
0f113f3e
MC
83{
84 const EVP_PKEY_ASN1_METHOD *t;
85
86 for (;;) {
87 t = pkey_asn1_find(type);
88 if (!t || !(t->pkey_flags & ASN1_PKEY_ALIAS))
89 break;
90 type = t->pkey_base_id;
91 }
92 if (pe) {
01b8b3c7 93#ifndef OPENSSL_NO_ENGINE
0f113f3e
MC
94 ENGINE *e;
95 /* type will contain the final unaliased type */
96 e = ENGINE_get_pkey_asn1_meth_engine(type);
97 if (e) {
98 *pe = e;
99 return ENGINE_get_pkey_asn1_meth(e, type);
100 }
01b8b3c7 101#endif
0f113f3e
MC
102 *pe = NULL;
103 }
104 return t;
105}
01b8b3c7
DSH
106
107const EVP_PKEY_ASN1_METHOD *EVP_PKEY_asn1_find_str(ENGINE **pe,
0f113f3e
MC
108 const char *str, int len)
109{
110 int i;
3bf0c3fe
RL
111 const EVP_PKEY_ASN1_METHOD *ameth = NULL;
112
0f113f3e
MC
113 if (len == -1)
114 len = strlen(str);
115 if (pe) {
01b8b3c7 116#ifndef OPENSSL_NO_ENGINE
0f113f3e
MC
117 ENGINE *e;
118 ameth = ENGINE_pkey_asn1_find_str(&e, str, len);
119 if (ameth) {
120 /*
121 * Convert structural into functional reference
122 */
123 if (!ENGINE_init(e))
124 ameth = NULL;
125 ENGINE_free(e);
126 *pe = e;
127 return ameth;
128 }
01b8b3c7 129#endif
0f113f3e
MC
130 *pe = NULL;
131 }
3bf0c3fe 132 for (i = EVP_PKEY_asn1_get_count(); i-- > 0; ) {
0f113f3e
MC
133 ameth = EVP_PKEY_asn1_get0(i);
134 if (ameth->pkey_flags & ASN1_PKEY_ALIAS)
135 continue;
3bf0c3fe
RL
136 if ((int)strlen(ameth->pem_str) == len
137 && strncasecmp(ameth->pem_str, str, len) == 0)
0f113f3e
MC
138 return ameth;
139 }
140 return NULL;
141}
e4263314 142
e46691a0 143int EVP_PKEY_asn1_add0(const EVP_PKEY_ASN1_METHOD *ameth)
0f113f3e 144{
3bf0c3fe
RL
145 EVP_PKEY_ASN1_METHOD tmp = { 0, };
146
a8600316
RL
147 /*
148 * One of the following must be true:
149 *
150 * pem_str == NULL AND ASN1_PKEY_ALIAS is set
151 * pem_str != NULL AND ASN1_PKEY_ALIAS is clear
152 *
153 * Anything else is an error and may lead to a corrupt ASN1 method table
154 */
155 if (!((ameth->pem_str == NULL
156 && (ameth->pkey_flags & ASN1_PKEY_ALIAS) != 0)
157 || (ameth->pem_str != NULL
158 && (ameth->pkey_flags & ASN1_PKEY_ALIAS) == 0))) {
9311d0c4 159 ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_INVALID_ARGUMENT);
a8600316
RL
160 return 0;
161 }
162
0f113f3e
MC
163 if (app_methods == NULL) {
164 app_methods = sk_EVP_PKEY_ASN1_METHOD_new(ameth_cmp);
90945fa3 165 if (app_methods == NULL)
0f113f3e
MC
166 return 0;
167 }
3bf0c3fe
RL
168
169 tmp.pkey_id = ameth->pkey_id;
170 if (sk_EVP_PKEY_ASN1_METHOD_find(app_methods, &tmp) >= 0) {
9311d0c4
RL
171 ERR_raise(ERR_LIB_EVP,
172 EVP_R_PKEY_APPLICATION_ASN1_METHOD_ALREADY_REGISTERED);
3bf0c3fe
RL
173 return 0;
174 }
175
0f113f3e
MC
176 if (!sk_EVP_PKEY_ASN1_METHOD_push(app_methods, ameth))
177 return 0;
178 sk_EVP_PKEY_ASN1_METHOD_sort(app_methods);
179 return 1;
180}
18e377b4 181
e46691a0 182int EVP_PKEY_asn1_add_alias(int to, int from)
0f113f3e
MC
183{
184 EVP_PKEY_ASN1_METHOD *ameth;
185 ameth = EVP_PKEY_asn1_new(from, ASN1_PKEY_ALIAS, NULL, NULL);
90945fa3 186 if (ameth == NULL)
0f113f3e
MC
187 return 0;
188 ameth->pkey_base_id = to;
189 if (!EVP_PKEY_asn1_add0(ameth)) {
190 EVP_PKEY_asn1_free(ameth);
191 return 0;
192 }
193 return 1;
194}
195
196int EVP_PKEY_asn1_get0_info(int *ppkey_id, int *ppkey_base_id,
197 int *ppkey_flags, const char **pinfo,
198 const char **ppem_str,
199 const EVP_PKEY_ASN1_METHOD *ameth)
200{
201 if (!ameth)
202 return 0;
203 if (ppkey_id)
204 *ppkey_id = ameth->pkey_id;
205 if (ppkey_base_id)
206 *ppkey_base_id = ameth->pkey_base_id;
207 if (ppkey_flags)
208 *ppkey_flags = ameth->pkey_flags;
209 if (pinfo)
210 *pinfo = ameth->info;
211 if (ppem_str)
212 *ppem_str = ameth->pem_str;
213 return 1;
214}
215
8900f3e3 216const EVP_PKEY_ASN1_METHOD *EVP_PKEY_get0_asn1(const EVP_PKEY *pkey)
0f113f3e
MC
217{
218 return pkey->ameth;
219}
220
221EVP_PKEY_ASN1_METHOD *EVP_PKEY_asn1_new(int id, int flags,
222 const char *pem_str, const char *info)
223{
b51bce94
RS
224 EVP_PKEY_ASN1_METHOD *ameth = OPENSSL_zalloc(sizeof(*ameth));
225
89947af2
F
226 if (ameth == NULL) {
227 ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
0f113f3e 228 return NULL;
89947af2 229 }
0f113f3e 230
0f113f3e
MC
231 ameth->pkey_id = id;
232 ameth->pkey_base_id = id;
233 ameth->pkey_flags = flags | ASN1_PKEY_DYNAMIC;
234
235 if (info) {
7644a9ae 236 ameth->info = OPENSSL_strdup(info);
89947af2 237 if (ameth->info == NULL)
0f113f3e 238 goto err;
64b25758 239 }
0f113f3e
MC
240
241 if (pem_str) {
7644a9ae 242 ameth->pem_str = OPENSSL_strdup(pem_str);
89947af2 243 if (ameth->pem_str == NULL)
0f113f3e 244 goto err;
64b25758 245 }
0f113f3e
MC
246
247 return ameth;
248
249 err:
0f113f3e 250 EVP_PKEY_asn1_free(ameth);
89947af2 251 ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
0f113f3e 252 return NULL;
0f113f3e
MC
253}
254
255void EVP_PKEY_asn1_copy(EVP_PKEY_ASN1_METHOD *dst,
256 const EVP_PKEY_ASN1_METHOD *src)
257{
472a88b7
MC
258 int pkey_id = dst->pkey_id;
259 int pkey_base_id = dst->pkey_base_id;
260 unsigned long pkey_flags = dst->pkey_flags;
261 char *pem_str = dst->pem_str;
262 char *info = dst->info;
263
264 *dst = *src;
265
266 /* We only copy the function pointers so restore the other values */
267 dst->pkey_id = pkey_id;
268 dst->pkey_base_id = pkey_base_id;
269 dst->pkey_flags = pkey_flags;
270 dst->pem_str = pem_str;
271 dst->info = info;
0f113f3e 272}
e69adea5 273
d82e2718 274void EVP_PKEY_asn1_free(EVP_PKEY_ASN1_METHOD *ameth)
0f113f3e
MC
275{
276 if (ameth && (ameth->pkey_flags & ASN1_PKEY_DYNAMIC)) {
b548a1f1
RS
277 OPENSSL_free(ameth->pem_str);
278 OPENSSL_free(ameth->info);
0f113f3e
MC
279 OPENSSL_free(ameth);
280 }
281}
18e377b4
DSH
282
283void EVP_PKEY_asn1_set_public(EVP_PKEY_ASN1_METHOD *ameth,
0f113f3e 284 int (*pub_decode) (EVP_PKEY *pk,
7674e923 285 const X509_PUBKEY *pub),
0f113f3e
MC
286 int (*pub_encode) (X509_PUBKEY *pub,
287 const EVP_PKEY *pk),
288 int (*pub_cmp) (const EVP_PKEY *a,
289 const EVP_PKEY *b),
290 int (*pub_print) (BIO *out,
291 const EVP_PKEY *pkey,
292 int indent, ASN1_PCTX *pctx),
293 int (*pkey_size) (const EVP_PKEY *pk),
294 int (*pkey_bits) (const EVP_PKEY *pk))
295{
296 ameth->pub_decode = pub_decode;
297 ameth->pub_encode = pub_encode;
298 ameth->pub_cmp = pub_cmp;
299 ameth->pub_print = pub_print;
300 ameth->pkey_size = pkey_size;
301 ameth->pkey_bits = pkey_bits;
302}
18e377b4
DSH
303
304void EVP_PKEY_asn1_set_private(EVP_PKEY_ASN1_METHOD *ameth,
0f113f3e 305 int (*priv_decode) (EVP_PKEY *pk,
245c6bc3 306 const PKCS8_PRIV_KEY_INFO
0f113f3e
MC
307 *p8inf),
308 int (*priv_encode) (PKCS8_PRIV_KEY_INFO *p8,
309 const EVP_PKEY *pk),
310 int (*priv_print) (BIO *out,
311 const EVP_PKEY *pkey,
312 int indent,
313 ASN1_PCTX *pctx))
314{
315 ameth->priv_decode = priv_decode;
316 ameth->priv_encode = priv_encode;
317 ameth->priv_print = priv_print;
318}
18e377b4
DSH
319
320void EVP_PKEY_asn1_set_param(EVP_PKEY_ASN1_METHOD *ameth,
0f113f3e
MC
321 int (*param_decode) (EVP_PKEY *pkey,
322 const unsigned char **pder,
323 int derlen),
324 int (*param_encode) (const EVP_PKEY *pkey,
325 unsigned char **pder),
326 int (*param_missing) (const EVP_PKEY *pk),
327 int (*param_copy) (EVP_PKEY *to,
328 const EVP_PKEY *from),
329 int (*param_cmp) (const EVP_PKEY *a,
330 const EVP_PKEY *b),
331 int (*param_print) (BIO *out,
332 const EVP_PKEY *pkey,
333 int indent, ASN1_PCTX *pctx))
334{
335 ameth->param_decode = param_decode;
336 ameth->param_encode = param_encode;
337 ameth->param_missing = param_missing;
338 ameth->param_copy = param_copy;
339 ameth->param_cmp = param_cmp;
340 ameth->param_print = param_print;
341}
18e377b4
DSH
342
343void EVP_PKEY_asn1_set_free(EVP_PKEY_ASN1_METHOD *ameth,
0f113f3e
MC
344 void (*pkey_free) (EVP_PKEY *pkey))
345{
346 ameth->pkey_free = pkey_free;
347}
18e377b4
DSH
348
349void EVP_PKEY_asn1_set_ctrl(EVP_PKEY_ASN1_METHOD *ameth,
0f113f3e
MC
350 int (*pkey_ctrl) (EVP_PKEY *pkey, int op,
351 long arg1, void *arg2))
352{
353 ameth->pkey_ctrl = pkey_ctrl;
354}
2514fa79
DSH
355
356void EVP_PKEY_asn1_set_security_bits(EVP_PKEY_ASN1_METHOD *ameth,
0f113f3e
MC
357 int (*pkey_security_bits) (const EVP_PKEY
358 *pk))
359{
360 ameth->pkey_security_bits = pkey_security_bits;
361}
3418f7b7
SA
362
363void EVP_PKEY_asn1_set_item(EVP_PKEY_ASN1_METHOD *ameth,
364 int (*item_verify) (EVP_MD_CTX *ctx,
365 const ASN1_ITEM *it,
ded346fa
DDO
366 const void *data,
367 const X509_ALGOR *a,
368 const ASN1_BIT_STRING *sig,
3418f7b7
SA
369 EVP_PKEY *pkey),
370 int (*item_sign) (EVP_MD_CTX *ctx,
371 const ASN1_ITEM *it,
ded346fa 372 const void *data,
3418f7b7
SA
373 X509_ALGOR *alg1,
374 X509_ALGOR *alg2,
375 ASN1_BIT_STRING *sig))
376{
377 ameth->item_sign = item_sign;
378 ameth->item_verify = item_verify;
379}
5e006082
RL
380
381void EVP_PKEY_asn1_set_siginf(EVP_PKEY_ASN1_METHOD *ameth,
382 int (*siginf_set) (X509_SIG_INFO *siginf,
383 const X509_ALGOR *alg,
384 const ASN1_STRING *sig))
385{
386 ameth->siginf_set = siginf_set;
387}
388
389void EVP_PKEY_asn1_set_check(EVP_PKEY_ASN1_METHOD *ameth,
390 int (*pkey_check) (const EVP_PKEY *pk))
391{
392 ameth->pkey_check = pkey_check;
393}
b0004708
PY
394
395void EVP_PKEY_asn1_set_public_check(EVP_PKEY_ASN1_METHOD *ameth,
396 int (*pkey_pub_check) (const EVP_PKEY *pk))
397{
398 ameth->pkey_public_check = pkey_pub_check;
399}
400
401void EVP_PKEY_asn1_set_param_check(EVP_PKEY_ASN1_METHOD *ameth,
402 int (*pkey_param_check) (const EVP_PKEY *pk))
403{
404 ameth->pkey_param_check = pkey_param_check;
405}
e8f9f08f
MC
406
407void EVP_PKEY_asn1_set_set_priv_key(EVP_PKEY_ASN1_METHOD *ameth,
408 int (*set_priv_key) (EVP_PKEY *pk,
409 const unsigned char
410 *priv,
411 size_t len))
412{
413 ameth->set_priv_key = set_priv_key;
414}
415
416void EVP_PKEY_asn1_set_set_pub_key(EVP_PKEY_ASN1_METHOD *ameth,
417 int (*set_pub_key) (EVP_PKEY *pk,
418 const unsigned char *pub,
419 size_t len))
420{
421 ameth->set_pub_key = set_pub_key;
422}
72ff0a54
MC
423
424void EVP_PKEY_asn1_set_get_priv_key(EVP_PKEY_ASN1_METHOD *ameth,
425 int (*get_priv_key) (const EVP_PKEY *pk,
426 unsigned char *priv,
427 size_t *len))
428{
429 ameth->get_priv_key = get_priv_key;
430}
431
432void EVP_PKEY_asn1_set_get_pub_key(EVP_PKEY_ASN1_METHOD *ameth,
433 int (*get_pub_key) (const EVP_PKEY *pk,
434 unsigned char *pub,
435 size_t *len))
436{
437 ameth->get_pub_key = get_pub_key;
438}