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