2 * Copyright 2006-2018 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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
10 #include "e_os.h" /* for strncasecmp */
11 #include "internal/cryptlib.h"
13 #include <openssl/asn1t.h>
14 #include <openssl/x509.h>
15 #include <openssl/engine.h>
16 #include "internal/asn1_int.h"
17 #include "internal/evp_int.h"
19 #include "standard_methods.h"
21 typedef int sk_cmp_fn_type(const char *const *a
, const char *const *b
);
22 static STACK_OF(EVP_PKEY_ASN1_METHOD
) *app_methods
= NULL
;
24 DECLARE_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_ASN1_METHOD
*,
25 const EVP_PKEY_ASN1_METHOD
*, ameth
);
27 static int ameth_cmp(const EVP_PKEY_ASN1_METHOD
*const *a
,
28 const EVP_PKEY_ASN1_METHOD
*const *b
)
30 return ((*a
)->pkey_id
- (*b
)->pkey_id
);
33 IMPLEMENT_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_ASN1_METHOD
*,
34 const EVP_PKEY_ASN1_METHOD
*, ameth
);
36 int EVP_PKEY_asn1_get_count(void)
38 int num
= OSSL_NELEM(standard_methods
);
40 num
+= sk_EVP_PKEY_ASN1_METHOD_num(app_methods
);
44 const EVP_PKEY_ASN1_METHOD
*EVP_PKEY_asn1_get0(int idx
)
46 int num
= OSSL_NELEM(standard_methods
);
50 return standard_methods
[idx
];
52 return sk_EVP_PKEY_ASN1_METHOD_value(app_methods
, idx
);
55 static const EVP_PKEY_ASN1_METHOD
*pkey_asn1_find(int type
)
57 EVP_PKEY_ASN1_METHOD tmp
;
58 const EVP_PKEY_ASN1_METHOD
*t
= &tmp
, **ret
;
62 idx
= sk_EVP_PKEY_ASN1_METHOD_find(app_methods
, &tmp
);
64 return sk_EVP_PKEY_ASN1_METHOD_value(app_methods
, idx
);
66 ret
= OBJ_bsearch_ameth(&t
, standard_methods
, OSSL_NELEM(standard_methods
));
73 * Find an implementation of an ASN1 algorithm. If 'pe' is not NULL also
74 * search through engines and set *pe to a functional reference to the engine
75 * implementing 'type' or NULL if no engine implements it.
78 const EVP_PKEY_ASN1_METHOD
*EVP_PKEY_asn1_find(ENGINE
**pe
, int type
)
80 const EVP_PKEY_ASN1_METHOD
*t
;
83 t
= pkey_asn1_find(type
);
84 if (!t
|| !(t
->pkey_flags
& ASN1_PKEY_ALIAS
))
86 type
= t
->pkey_base_id
;
89 #ifndef OPENSSL_NO_ENGINE
91 /* type will contain the final unaliased type */
92 e
= ENGINE_get_pkey_asn1_meth_engine(type
);
95 return ENGINE_get_pkey_asn1_meth(e
, type
);
103 const EVP_PKEY_ASN1_METHOD
*EVP_PKEY_asn1_find_str(ENGINE
**pe
,
104 const char *str
, int len
)
107 const EVP_PKEY_ASN1_METHOD
*ameth
= NULL
;
112 #ifndef OPENSSL_NO_ENGINE
114 ameth
= ENGINE_pkey_asn1_find_str(&e
, str
, len
);
117 * Convert structural into functional reference
128 for (i
= EVP_PKEY_asn1_get_count(); i
-- > 0; ) {
129 ameth
= EVP_PKEY_asn1_get0(i
);
130 if (ameth
->pkey_flags
& ASN1_PKEY_ALIAS
)
132 if ((int)strlen(ameth
->pem_str
) == len
133 && strncasecmp(ameth
->pem_str
, str
, len
) == 0)
139 int EVP_PKEY_asn1_add0(const EVP_PKEY_ASN1_METHOD
*ameth
)
141 EVP_PKEY_ASN1_METHOD tmp
= { 0, };
143 if (app_methods
== NULL
) {
144 app_methods
= sk_EVP_PKEY_ASN1_METHOD_new(ameth_cmp
);
145 if (app_methods
== NULL
)
149 tmp
.pkey_id
= ameth
->pkey_id
;
150 if (sk_EVP_PKEY_ASN1_METHOD_find(app_methods
, &tmp
) >= 0) {
151 EVPerr(EVP_F_EVP_PKEY_ASN1_ADD0
,
152 EVP_R_PKEY_APPLICATION_ASN1_METHOD_ALREADY_REGISTERED
);
156 if (!sk_EVP_PKEY_ASN1_METHOD_push(app_methods
, ameth
))
158 sk_EVP_PKEY_ASN1_METHOD_sort(app_methods
);
162 int EVP_PKEY_asn1_add_alias(int to
, int from
)
164 EVP_PKEY_ASN1_METHOD
*ameth
;
165 ameth
= EVP_PKEY_asn1_new(from
, ASN1_PKEY_ALIAS
, NULL
, NULL
);
168 ameth
->pkey_base_id
= to
;
169 if (!EVP_PKEY_asn1_add0(ameth
)) {
170 EVP_PKEY_asn1_free(ameth
);
176 int EVP_PKEY_asn1_get0_info(int *ppkey_id
, int *ppkey_base_id
,
177 int *ppkey_flags
, const char **pinfo
,
178 const char **ppem_str
,
179 const EVP_PKEY_ASN1_METHOD
*ameth
)
184 *ppkey_id
= ameth
->pkey_id
;
186 *ppkey_base_id
= ameth
->pkey_base_id
;
188 *ppkey_flags
= ameth
->pkey_flags
;
190 *pinfo
= ameth
->info
;
192 *ppem_str
= ameth
->pem_str
;
196 const EVP_PKEY_ASN1_METHOD
*EVP_PKEY_get0_asn1(const EVP_PKEY
*pkey
)
201 EVP_PKEY_ASN1_METHOD
*EVP_PKEY_asn1_new(int id
, int flags
,
202 const char *pem_str
, const char *info
)
204 EVP_PKEY_ASN1_METHOD
*ameth
= OPENSSL_zalloc(sizeof(*ameth
));
210 ameth
->pkey_base_id
= id
;
211 ameth
->pkey_flags
= flags
| ASN1_PKEY_DYNAMIC
;
214 ameth
->info
= OPENSSL_strdup(info
);
220 ameth
->pem_str
= OPENSSL_strdup(pem_str
);
228 EVP_PKEY_asn1_free(ameth
);
233 void EVP_PKEY_asn1_copy(EVP_PKEY_ASN1_METHOD
*dst
,
234 const EVP_PKEY_ASN1_METHOD
*src
)
237 dst
->pub_decode
= src
->pub_decode
;
238 dst
->pub_encode
= src
->pub_encode
;
239 dst
->pub_cmp
= src
->pub_cmp
;
240 dst
->pub_print
= src
->pub_print
;
242 dst
->priv_decode
= src
->priv_decode
;
243 dst
->priv_encode
= src
->priv_encode
;
244 dst
->priv_print
= src
->priv_print
;
246 dst
->old_priv_encode
= src
->old_priv_encode
;
247 dst
->old_priv_decode
= src
->old_priv_decode
;
249 dst
->pkey_size
= src
->pkey_size
;
250 dst
->pkey_bits
= src
->pkey_bits
;
252 dst
->param_decode
= src
->param_decode
;
253 dst
->param_encode
= src
->param_encode
;
254 dst
->param_missing
= src
->param_missing
;
255 dst
->param_copy
= src
->param_copy
;
256 dst
->param_cmp
= src
->param_cmp
;
257 dst
->param_print
= src
->param_print
;
259 dst
->pkey_free
= src
->pkey_free
;
260 dst
->pkey_ctrl
= src
->pkey_ctrl
;
262 dst
->item_sign
= src
->item_sign
;
263 dst
->item_verify
= src
->item_verify
;
265 dst
->siginf_set
= src
->siginf_set
;
267 dst
->pkey_check
= src
->pkey_check
;
271 void EVP_PKEY_asn1_free(EVP_PKEY_ASN1_METHOD
*ameth
)
273 if (ameth
&& (ameth
->pkey_flags
& ASN1_PKEY_DYNAMIC
)) {
274 OPENSSL_free(ameth
->pem_str
);
275 OPENSSL_free(ameth
->info
);
280 void EVP_PKEY_asn1_set_public(EVP_PKEY_ASN1_METHOD
*ameth
,
281 int (*pub_decode
) (EVP_PKEY
*pk
,
283 int (*pub_encode
) (X509_PUBKEY
*pub
,
285 int (*pub_cmp
) (const EVP_PKEY
*a
,
287 int (*pub_print
) (BIO
*out
,
288 const EVP_PKEY
*pkey
,
289 int indent
, ASN1_PCTX
*pctx
),
290 int (*pkey_size
) (const EVP_PKEY
*pk
),
291 int (*pkey_bits
) (const EVP_PKEY
*pk
))
293 ameth
->pub_decode
= pub_decode
;
294 ameth
->pub_encode
= pub_encode
;
295 ameth
->pub_cmp
= pub_cmp
;
296 ameth
->pub_print
= pub_print
;
297 ameth
->pkey_size
= pkey_size
;
298 ameth
->pkey_bits
= pkey_bits
;
301 void EVP_PKEY_asn1_set_private(EVP_PKEY_ASN1_METHOD
*ameth
,
302 int (*priv_decode
) (EVP_PKEY
*pk
,
303 const PKCS8_PRIV_KEY_INFO
305 int (*priv_encode
) (PKCS8_PRIV_KEY_INFO
*p8
,
307 int (*priv_print
) (BIO
*out
,
308 const EVP_PKEY
*pkey
,
312 ameth
->priv_decode
= priv_decode
;
313 ameth
->priv_encode
= priv_encode
;
314 ameth
->priv_print
= priv_print
;
317 void EVP_PKEY_asn1_set_param(EVP_PKEY_ASN1_METHOD
*ameth
,
318 int (*param_decode
) (EVP_PKEY
*pkey
,
319 const unsigned char **pder
,
321 int (*param_encode
) (const EVP_PKEY
*pkey
,
322 unsigned char **pder
),
323 int (*param_missing
) (const EVP_PKEY
*pk
),
324 int (*param_copy
) (EVP_PKEY
*to
,
325 const EVP_PKEY
*from
),
326 int (*param_cmp
) (const EVP_PKEY
*a
,
328 int (*param_print
) (BIO
*out
,
329 const EVP_PKEY
*pkey
,
330 int indent
, ASN1_PCTX
*pctx
))
332 ameth
->param_decode
= param_decode
;
333 ameth
->param_encode
= param_encode
;
334 ameth
->param_missing
= param_missing
;
335 ameth
->param_copy
= param_copy
;
336 ameth
->param_cmp
= param_cmp
;
337 ameth
->param_print
= param_print
;
340 void EVP_PKEY_asn1_set_free(EVP_PKEY_ASN1_METHOD
*ameth
,
341 void (*pkey_free
) (EVP_PKEY
*pkey
))
343 ameth
->pkey_free
= pkey_free
;
346 void EVP_PKEY_asn1_set_ctrl(EVP_PKEY_ASN1_METHOD
*ameth
,
347 int (*pkey_ctrl
) (EVP_PKEY
*pkey
, int op
,
348 long arg1
, void *arg2
))
350 ameth
->pkey_ctrl
= pkey_ctrl
;
353 void EVP_PKEY_asn1_set_security_bits(EVP_PKEY_ASN1_METHOD
*ameth
,
354 int (*pkey_security_bits
) (const EVP_PKEY
357 ameth
->pkey_security_bits
= pkey_security_bits
;
360 void EVP_PKEY_asn1_set_item(EVP_PKEY_ASN1_METHOD
*ameth
,
361 int (*item_verify
) (EVP_MD_CTX
*ctx
,
365 ASN1_BIT_STRING
*sig
,
367 int (*item_sign
) (EVP_MD_CTX
*ctx
,
372 ASN1_BIT_STRING
*sig
))
374 ameth
->item_sign
= item_sign
;
375 ameth
->item_verify
= item_verify
;
378 void EVP_PKEY_asn1_set_siginf(EVP_PKEY_ASN1_METHOD
*ameth
,
379 int (*siginf_set
) (X509_SIG_INFO
*siginf
,
380 const X509_ALGOR
*alg
,
381 const ASN1_STRING
*sig
))
383 ameth
->siginf_set
= siginf_set
;
386 void EVP_PKEY_asn1_set_check(EVP_PKEY_ASN1_METHOD
*ameth
,
387 int (*pkey_check
) (const EVP_PKEY
*pk
))
389 ameth
->pkey_check
= pkey_check
;
392 void EVP_PKEY_asn1_set_public_check(EVP_PKEY_ASN1_METHOD
*ameth
,
393 int (*pkey_pub_check
) (const EVP_PKEY
*pk
))
395 ameth
->pkey_public_check
= pkey_pub_check
;
398 void EVP_PKEY_asn1_set_param_check(EVP_PKEY_ASN1_METHOD
*ameth
,
399 int (*pkey_param_check
) (const EVP_PKEY
*pk
))
401 ameth
->pkey_param_check
= pkey_param_check
;