6 - manipulating and registering EVP_PKEY_ASN1_METHOD structure
10 #include <openssl/evp.h>
12 typedef struct evp_pkey_asn1_method_st EVP_PKEY_ASN1_METHOD;
14 EVP_PKEY_ASN1_METHOD *EVP_PKEY_asn1_new(int id, int flags,
17 void EVP_PKEY_asn1_copy(EVP_PKEY_ASN1_METHOD *dst,
18 const EVP_PKEY_ASN1_METHOD *src);
19 void EVP_PKEY_asn1_free(EVP_PKEY_ASN1_METHOD *ameth);
20 int EVP_PKEY_asn1_add0(const EVP_PKEY_ASN1_METHOD *ameth);
21 int EVP_PKEY_asn1_add_alias(int to, int from);
23 void EVP_PKEY_asn1_set_public(EVP_PKEY_ASN1_METHOD *ameth,
24 int (*pub_decode) (EVP_PKEY *pk,
26 int (*pub_encode) (X509_PUBKEY *pub,
28 int (*pub_cmp) (const EVP_PKEY *a,
30 int (*pub_print) (BIO *out,
32 int indent, ASN1_PCTX *pctx),
33 int (*pkey_size) (const EVP_PKEY *pk),
34 int (*pkey_bits) (const EVP_PKEY *pk));
35 void EVP_PKEY_asn1_set_private(EVP_PKEY_ASN1_METHOD *ameth,
36 int (*priv_decode) (EVP_PKEY *pk,
37 const PKCS8_PRIV_KEY_INFO
39 int (*priv_encode) (PKCS8_PRIV_KEY_INFO *p8,
41 int (*priv_print) (BIO *out,
45 void EVP_PKEY_asn1_set_param(EVP_PKEY_ASN1_METHOD *ameth,
46 int (*param_decode) (EVP_PKEY *pkey,
47 const unsigned char **pder,
49 int (*param_encode) (const EVP_PKEY *pkey,
50 unsigned char **pder),
51 int (*param_missing) (const EVP_PKEY *pk),
52 int (*param_copy) (EVP_PKEY *to,
53 const EVP_PKEY *from),
54 int (*param_cmp) (const EVP_PKEY *a,
56 int (*param_print) (BIO *out,
61 void EVP_PKEY_asn1_set_free(EVP_PKEY_ASN1_METHOD *ameth,
62 void (*pkey_free) (EVP_PKEY *pkey));
63 void EVP_PKEY_asn1_set_ctrl(EVP_PKEY_ASN1_METHOD *ameth,
64 int (*pkey_ctrl) (EVP_PKEY *pkey, int op,
65 long arg1, void *arg2));
66 void EVP_PKEY_asn1_set_item(EVP_PKEY_ASN1_METHOD *ameth,
67 int (*item_verify) (EVP_MD_CTX *ctx,
73 int (*item_sign) (EVP_MD_CTX *ctx,
78 ASN1_BIT_STRING *sig));
80 void EVP_PKEY_asn1_set_siginf(EVP_PKEY_ASN1_METHOD *ameth,
81 int (*siginf_set) (X509_SIG_INFO *siginf,
82 const X509_ALGOR *alg,
83 const ASN1_STRING *sig));
85 void EVP_PKEY_asn1_set_check(EVP_PKEY_ASN1_METHOD *ameth,
86 int (*pkey_check) (const EVP_PKEY *pk));
88 void EVP_PKEY_asn1_set_security_bits(EVP_PKEY_ASN1_METHOD *ameth,
89 int (*pkey_security_bits) (const EVP_PKEY
92 const EVP_PKEY_ASN1_METHOD *EVP_PKEY_get0_asn1(const EVP_PKEY *pkey);
96 B<EVP_PKEY_ASN1_METHOD> is a structure which holds a set of ASN.1
97 conversion, printing and information methods for a specific public key
100 There are two places where the B<EVP_PKEY_ASN1_METHOD> objects are
101 stored: one is a built-in array representing the standard methods for
102 different algorithms, and the other one is a stack of user-defined
103 application-specific methods, which can be manipulated by using
104 L<EVP_PKEY_asn1_add0(3)>.
108 The methods are the underlying implementations of a particular public
109 key algorithm present by the B<EVP_PKEY> object.
111 int (*pub_decode) (EVP_PKEY *pk, X509_PUBKEY *pub);
112 int (*pub_encode) (X509_PUBKEY *pub, const EVP_PKEY *pk);
113 int (*pub_cmp) (const EVP_PKEY *a, const EVP_PKEY *b);
114 int (*pub_print) (BIO *out, const EVP_PKEY *pkey, int indent,
117 The pub_decode() and pub_encode() methods are called to decode /
118 encode B<X509_PUBKEY> ASN.1 parameters to / from B<pk>.
119 They MUST return 0 on error, 1 on success.
120 They're called by L<X509_PUBKEY_get0(3)> and L<X509_PUBKEY_set(3)>.
122 The pub_cmp() method is called when two public keys are to be
124 It MUST return 1 when the keys are equal, 0 otherwise.
125 It's called by L<EVP_PKEY_cmp(3)>.
127 The pub_print() method is called to print a public key in humanly
128 readable text to B<out>, indented B<indent> spaces.
129 It MUST return 0 on error, 1 on success.
130 It's called by L<EVP_PKEY_print_public(3)>.
132 int (*priv_decode) (EVP_PKEY *pk, const PKCS8_PRIV_KEY_INFO *p8inf);
133 int (*priv_encode) (PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pk);
134 int (*priv_print) (BIO *out, const EVP_PKEY *pkey, int indent,
137 The priv_decode() and priv_encode() methods are called to decode /
138 encode B<PKCS8_PRIV_KEY_INFO> form private key to / from B<pk>.
139 They MUST return 0 on error, 1 on success.
140 They're called by L<EVP_PKCS82PKEY(3)> and L<EVP_PKEY2PKCS8(3)>.
142 The priv_print() method is called to print a private key in humanly
143 readable text to B<out>, indented B<indent> spaces.
144 It MUST return 0 on error, 1 on success.
145 It's called by L<EVP_PKEY_print_private(3)>.
147 int (*pkey_size) (const EVP_PKEY *pk);
148 int (*pkey_bits) (const EVP_PKEY *pk);
149 int (*pkey_security_bits) (const EVP_PKEY *pk);
151 The pkey_size() method returns the key size in bytes.
152 It's called by L<EVP_PKEY_size(3)>.
154 The pkey_bits() method returns the key size in bits.
155 It's called by L<EVP_PKEY_bits(3)>.
157 int (*param_decode) (EVP_PKEY *pkey,
158 const unsigned char **pder, int derlen);
159 int (*param_encode) (const EVP_PKEY *pkey, unsigned char **pder);
160 int (*param_missing) (const EVP_PKEY *pk);
161 int (*param_copy) (EVP_PKEY *to, const EVP_PKEY *from);
162 int (*param_cmp) (const EVP_PKEY *a, const EVP_PKEY *b);
163 int (*param_print) (BIO *out, const EVP_PKEY *pkey, int indent,
166 The param_decode() and param_encode() methods are called to decode /
167 encode DER formatted parameters to / from B<pk>.
168 They MUST return 0 on error, 1 on success.
169 They're called by L<PEM_read_bio_Parameters(3)> and the B<file:>
170 L<OSSL_STORE_LOADER(3)>.
172 The param_missing() method returns 0 if a key parameter is missing,
174 It's called by L<EVP_PKEY_missing_parameters(3)>.
176 The param_copy() method copies key parameters from B<from> to B<to>.
177 It MUST return 0 on error, 1 on success.
178 It's called by L<EVP_PKEY_copy_parameters(3)>.
180 The param_cmp() method compares the parameters of keys B<a> and B<b>.
181 It MUST return 1 when the keys are equal, 0 when not equal, or a
182 negative number on error.
183 It's called by L<EVP_PKEY_cmp_parameters(3)>.
185 The param_print() method prints the private key parameters in humanly
186 readable text to B<out>, indented B<indent> spaces.
187 It MUST return 0 on error, 1 on success.
188 It's called by L<EVP_PKEY_print_params(3)>.
190 int (*sig_print) (BIO *out,
191 const X509_ALGOR *sigalg, const ASN1_STRING *sig,
192 int indent, ASN1_PCTX *pctx);
194 The sig_print() method prints a signature in humanly readable text to
195 B<out>, indented B<indent> spaces.
196 B<sigalg> contains the exact signature algorithm.
197 If the signature in B<sig> doesn't correspond to what this method
198 expects, X509_signature_dump() must be used as a last resort.
199 It MUST return 0 on error, 1 on success.
200 It's called by L<X509_signature_print(3)>.
202 void (*pkey_free) (EVP_PKEY *pkey);
204 The pkey_free() method helps freeing the internals of B<pkey>.
205 It's called by L<EVP_PKEY_free(3)>, L<EVP_PKEY_set_type(3)>,
206 L<EVP_PKEY_set_type_str(3)>, and L<EVP_PKEY_assign(3)>.
208 int (*pkey_ctrl) (EVP_PKEY *pkey, int op, long arg1, void *arg2);
210 The pkey_ctrl() method adds extra algorithm specific control.
211 It's called by L<EVP_PKEY_get_default_digest_nid(3)>,
212 L<EVP_PKEY_set1_tls_encodedpoint(3)>,
213 L<EVP_PKEY_get1_tls_encodedpoint(3)>, L<PKCS7_SIGNER_INFO_set(3)>,
214 L<PKCS7_RECIP_INFO_set(3)>, ...
216 int (*old_priv_decode) (EVP_PKEY *pkey,
217 const unsigned char **pder, int derlen);
218 int (*old_priv_encode) (const EVP_PKEY *pkey, unsigned char **pder);
220 The old_priv_decode() and old_priv_encode() methods decode / encode
221 they private key B<pkey> from / to a DER formatted array.
222 These are exclusively used to help decoding / encoding older (pre
223 PKCS#8) PEM formatted encrypted private keys.
224 old_priv_decode() MUST return 0 on error, 1 on success.
225 old_priv_encode() MUST the return same kind of values as
227 They're called by L<d2i_PrivateKey(3)> and L<i2d_PrivateKey(3)>.
229 int (*item_verify) (EVP_MD_CTX *ctx, const ASN1_ITEM *it, void *asn,
230 X509_ALGOR *a, ASN1_BIT_STRING *sig, EVP_PKEY *pkey);
231 int (*item_sign) (EVP_MD_CTX *ctx, const ASN1_ITEM *it, void *asn,
232 X509_ALGOR *alg1, X509_ALGOR *alg2,
233 ASN1_BIT_STRING *sig);
235 The item_sign() and item_verify() methods make it possible to have
236 algorithm specific signatures and verification of them.
238 item_sign() MUST return one of:
248 item_sign() did everything, OpenSSL internals just needs to pass the
249 signature length back.
253 item_sign() did nothing, OpenSSL internal standard routines are
254 expected to continue with the default signature production.
258 item_sign() set the algorithm identifier B<algor1> and B<algor2>,
259 OpenSSL internals should just sign using those algorithms.
263 item_verify() MUST return one of:
273 item_sign() did everything, OpenSSL internals just needs to pass the
274 signature length back.
278 item_sign() did nothing, OpenSSL internal standard routines are
279 expected to continue with the default signature production.
283 item_verify() and item_sign() are called by L<ASN1_item_verify(3)> and
284 L<ASN1_item_sign(3)>, and by extension, L<X509_verify(3)>,
285 L<X509_REQ_verify(3)>, L<X509_sign(3)>, L<X509_REQ_sign(3)>, ...
287 int (*siginf_set) (X509_SIG_INFO *siginf, const X509_ALGOR *alg,
288 const ASN1_STRING *sig);
290 The siginf_set() method is used to set custom B<X509_SIG_INFO>
292 It MUST return 0 on error, or 1 on success.
293 It's called as part of L<X509_check_purpose(3)>, L<X509_check_ca(3)>
294 and L<X509_check_issued(3)>.
296 int (*pkey_check) (const EVP_PKEY *pk);
298 The pkey_check() method is used to check the validity of B<pk>.
299 It MUST return 0 for an invalid key, or 1 for a valid key.
300 It's called by L<EVP_PKEY_check(3)>.
304 EVP_PKEY_asn1_new() creates and returns a new B<EVP_PKEY_ASN1_METHOD>
305 object, and associates the given B<id>, B<flags>, B<pem_str> and
307 B<id> is a NID, B<pem_str> is the PEM type string, B<info> is a
309 The following B<flags> are supported:
311 ASN1_PKEY_SIGPARAM_NULL
313 If B<ASN1_PKEY_SIGPARAM_NULL> is set, then the signature algorithm
314 parameters are given the type B<V_ASN1_NULL> by default, otherwise
315 they will be given the type B<V_ASN1_UNDEF> (i.e. the parameter is
317 See L<X509_ALGOR_set0(3)> for more information.
319 EVP_PKEY_asn1_copy() copies an B<EVP_PKEY_ASN1_METHOD> object from
321 This function is not thread safe, it's recommended to only use this
322 when initializing the application.
324 EVP_PKEY_asn1_free() frees an existing B<EVP_PKEY_ASN1_METHOD> pointed
327 EVP_PKEY_asn1_add0() adds B<ameth> to the user defined stack of
328 methods unless another B<EVP_PKEY_ASN1_METHOD> with the same NID is
330 This function is not thread safe, it's recommended to only use this
331 when initializing the application.
333 EVP_PKEY_asn1_add_alias() creates an alias with the NID B<to> for the
334 B<EVP_PKEY_ASN1_METHOD> with NID B<from> unless another
335 B<EVP_PKEY_ASN1_METHOD> with the same NID is already added.
336 This function is not thread safe, it's recommended to only use this
337 when initializing the application.
339 EVP_PKEY_asn1_set_public(), EVP_PKEY_asn1_set_private(),
340 EVP_PKEY_asn1_set_param(), EVP_PKEY_asn1_set_free(),
341 EVP_PKEY_asn1_set_ctrl(), EVP_PKEY_asn1_set_item(),
342 EVP_PKEY_asn1_set_siginf(), EVP_PKEY_asn1_set_check(), and
343 EVP_PKEY_asn1_set_security_bits() set the diverse methods of the given
344 B<EVP_PKEY_ASN1_METHOD> object.
346 EVP_PKEY_get0_asn1() finds the B<EVP_PKEY_ASN1_METHOD> associated
347 with the key B<pkey>.
351 EVP_PKEY_asn1_new() returns NULL on error, or a pointer to an
352 B<EVP_PKEY_ASN1_METHOD> object otherwise.
354 EVP_PKEY_asn1_add0() and EVP_PKEY_asn1_add_alias() return 0 on error,
357 EVP_PKEY_get0_asn1() returns NULL on error, or a pointer to a constant
358 B<EVP_PKEY_ASN1_METHOD> object otherwise.
362 Copyright 2017 The OpenSSL Project Authors. All Rights Reserved.
364 Licensed under the OpenSSL license (the "License"). You may not use
365 this file except in compliance with the License. You can obtain a copy
366 in the file LICENSE in the source distribution or at
367 L<https://www.openssl.org/source/license.html>.