2
0

EVP_PKEY_ASN1_METHOD.pod 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. =pod
  2. =head1 NAME
  3. EVP_PKEY_ASN1_METHOD,
  4. EVP_PKEY_asn1_new,
  5. EVP_PKEY_asn1_copy,
  6. EVP_PKEY_asn1_free,
  7. EVP_PKEY_asn1_add0,
  8. EVP_PKEY_asn1_add_alias,
  9. EVP_PKEY_asn1_set_public,
  10. EVP_PKEY_asn1_set_private,
  11. EVP_PKEY_asn1_set_param,
  12. EVP_PKEY_asn1_set_free,
  13. EVP_PKEY_asn1_set_ctrl,
  14. EVP_PKEY_asn1_set_item,
  15. EVP_PKEY_asn1_set_siginf,
  16. EVP_PKEY_asn1_set_check,
  17. EVP_PKEY_asn1_set_public_check,
  18. EVP_PKEY_asn1_set_param_check,
  19. EVP_PKEY_asn1_set_security_bits,
  20. EVP_PKEY_asn1_set_set_priv_key,
  21. EVP_PKEY_asn1_set_set_pub_key,
  22. EVP_PKEY_asn1_set_get_priv_key,
  23. EVP_PKEY_asn1_set_get_pub_key,
  24. EVP_PKEY_get0_asn1
  25. - manipulating and registering EVP_PKEY_ASN1_METHOD structure
  26. =head1 SYNOPSIS
  27. #include <openssl/evp.h>
  28. typedef struct evp_pkey_asn1_method_st EVP_PKEY_ASN1_METHOD;
  29. EVP_PKEY_ASN1_METHOD *EVP_PKEY_asn1_new(int id, int flags,
  30. const char *pem_str,
  31. const char *info);
  32. void EVP_PKEY_asn1_copy(EVP_PKEY_ASN1_METHOD *dst,
  33. const EVP_PKEY_ASN1_METHOD *src);
  34. void EVP_PKEY_asn1_free(EVP_PKEY_ASN1_METHOD *ameth);
  35. int EVP_PKEY_asn1_add0(const EVP_PKEY_ASN1_METHOD *ameth);
  36. int EVP_PKEY_asn1_add_alias(int to, int from);
  37. void EVP_PKEY_asn1_set_public(EVP_PKEY_ASN1_METHOD *ameth,
  38. int (*pub_decode) (EVP_PKEY *pk,
  39. const X509_PUBKEY *pub),
  40. int (*pub_encode) (X509_PUBKEY *pub,
  41. const EVP_PKEY *pk),
  42. int (*pub_cmp) (const EVP_PKEY *a,
  43. const EVP_PKEY *b),
  44. int (*pub_print) (BIO *out,
  45. const EVP_PKEY *pkey,
  46. int indent, ASN1_PCTX *pctx),
  47. int (*pkey_size) (const EVP_PKEY *pk),
  48. int (*pkey_bits) (const EVP_PKEY *pk));
  49. void EVP_PKEY_asn1_set_private(EVP_PKEY_ASN1_METHOD *ameth,
  50. int (*priv_decode) (EVP_PKEY *pk,
  51. const PKCS8_PRIV_KEY_INFO
  52. *p8inf),
  53. int (*priv_encode) (PKCS8_PRIV_KEY_INFO *p8,
  54. const EVP_PKEY *pk),
  55. int (*priv_print) (BIO *out,
  56. const EVP_PKEY *pkey,
  57. int indent,
  58. ASN1_PCTX *pctx));
  59. void EVP_PKEY_asn1_set_param(EVP_PKEY_ASN1_METHOD *ameth,
  60. int (*param_decode) (EVP_PKEY *pkey,
  61. const unsigned char **pder,
  62. int derlen),
  63. int (*param_encode) (const EVP_PKEY *pkey,
  64. unsigned char **pder),
  65. int (*param_missing) (const EVP_PKEY *pk),
  66. int (*param_copy) (EVP_PKEY *to,
  67. const EVP_PKEY *from),
  68. int (*param_cmp) (const EVP_PKEY *a,
  69. const EVP_PKEY *b),
  70. int (*param_print) (BIO *out,
  71. const EVP_PKEY *pkey,
  72. int indent,
  73. ASN1_PCTX *pctx));
  74. void EVP_PKEY_asn1_set_free(EVP_PKEY_ASN1_METHOD *ameth,
  75. void (*pkey_free) (EVP_PKEY *pkey));
  76. void EVP_PKEY_asn1_set_ctrl(EVP_PKEY_ASN1_METHOD *ameth,
  77. int (*pkey_ctrl) (EVP_PKEY *pkey, int op,
  78. long arg1, void *arg2));
  79. void EVP_PKEY_asn1_set_item(EVP_PKEY_ASN1_METHOD *ameth,
  80. int (*item_verify) (EVP_MD_CTX *ctx,
  81. const ASN1_ITEM *it,
  82. void *asn,
  83. X509_ALGOR *a,
  84. ASN1_BIT_STRING *sig,
  85. EVP_PKEY *pkey),
  86. int (*item_sign) (EVP_MD_CTX *ctx,
  87. const ASN1_ITEM *it,
  88. void *asn,
  89. X509_ALGOR *alg1,
  90. X509_ALGOR *alg2,
  91. ASN1_BIT_STRING *sig));
  92. void EVP_PKEY_asn1_set_siginf(EVP_PKEY_ASN1_METHOD *ameth,
  93. int (*siginf_set) (X509_SIG_INFO *siginf,
  94. const X509_ALGOR *alg,
  95. const ASN1_STRING *sig));
  96. void EVP_PKEY_asn1_set_check(EVP_PKEY_ASN1_METHOD *ameth,
  97. int (*pkey_check) (const EVP_PKEY *pk));
  98. void EVP_PKEY_asn1_set_public_check(EVP_PKEY_ASN1_METHOD *ameth,
  99. int (*pkey_pub_check) (const EVP_PKEY *pk));
  100. void EVP_PKEY_asn1_set_param_check(EVP_PKEY_ASN1_METHOD *ameth,
  101. int (*pkey_param_check) (const EVP_PKEY *pk));
  102. void EVP_PKEY_asn1_set_security_bits(EVP_PKEY_ASN1_METHOD *ameth,
  103. int (*pkey_security_bits) (const EVP_PKEY
  104. *pk));
  105. void EVP_PKEY_asn1_set_set_priv_key(EVP_PKEY_ASN1_METHOD *ameth,
  106. int (*set_priv_key) (EVP_PKEY *pk,
  107. const unsigned char
  108. *priv,
  109. size_t len));
  110. void EVP_PKEY_asn1_set_set_pub_key(EVP_PKEY_ASN1_METHOD *ameth,
  111. int (*set_pub_key) (EVP_PKEY *pk,
  112. const unsigned char *pub,
  113. size_t len));
  114. void EVP_PKEY_asn1_set_get_priv_key(EVP_PKEY_ASN1_METHOD *ameth,
  115. int (*get_priv_key) (const EVP_PKEY *pk,
  116. unsigned char *priv,
  117. size_t *len));
  118. void EVP_PKEY_asn1_set_get_pub_key(EVP_PKEY_ASN1_METHOD *ameth,
  119. int (*get_pub_key) (const EVP_PKEY *pk,
  120. unsigned char *pub,
  121. size_t *len));
  122. const EVP_PKEY_ASN1_METHOD *EVP_PKEY_get0_asn1(const EVP_PKEY *pkey);
  123. =head1 DESCRIPTION
  124. B<EVP_PKEY_ASN1_METHOD> is a structure which holds a set of ASN.1
  125. conversion, printing and information methods for a specific public key
  126. algorithm.
  127. There are two places where the B<EVP_PKEY_ASN1_METHOD> objects are
  128. stored: one is a built-in array representing the standard methods for
  129. different algorithms, and the other one is a stack of user-defined
  130. application-specific methods, which can be manipulated by using
  131. L<EVP_PKEY_asn1_add0(3)>.
  132. =head2 Methods
  133. The methods are the underlying implementations of a particular public
  134. key algorithm present by the B<EVP_PKEY> object.
  135. int (*pub_decode) (EVP_PKEY *pk, const X509_PUBKEY *pub);
  136. int (*pub_encode) (X509_PUBKEY *pub, const EVP_PKEY *pk);
  137. int (*pub_cmp) (const EVP_PKEY *a, const EVP_PKEY *b);
  138. int (*pub_print) (BIO *out, const EVP_PKEY *pkey, int indent,
  139. ASN1_PCTX *pctx);
  140. The pub_decode() and pub_encode() methods are called to decode /
  141. encode B<X509_PUBKEY> ASN.1 parameters to / from B<pk>.
  142. They MUST return 0 on error, 1 on success.
  143. They're called by L<X509_PUBKEY_get0(3)> and L<X509_PUBKEY_set(3)>.
  144. The pub_cmp() method is called when two public keys are to be
  145. compared.
  146. It MUST return 1 when the keys are equal, 0 otherwise.
  147. It's called by L<EVP_PKEY_eq(3)>.
  148. The pub_print() method is called to print a public key in humanly
  149. readable text to B<out>, indented B<indent> spaces.
  150. It MUST return 0 on error, 1 on success.
  151. It's called by L<EVP_PKEY_print_public(3)>.
  152. int (*priv_decode) (EVP_PKEY *pk, const PKCS8_PRIV_KEY_INFO *p8inf);
  153. int (*priv_encode) (PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pk);
  154. int (*priv_print) (BIO *out, const EVP_PKEY *pkey, int indent,
  155. ASN1_PCTX *pctx);
  156. The priv_decode() and priv_encode() methods are called to decode /
  157. encode B<PKCS8_PRIV_KEY_INFO> form private key to / from B<pk>.
  158. They MUST return 0 on error, 1 on success.
  159. They're called by L<EVP_PKCS82PKEY(3)> and L<EVP_PKEY2PKCS8(3)>.
  160. The priv_print() method is called to print a private key in humanly
  161. readable text to B<out>, indented B<indent> spaces.
  162. It MUST return 0 on error, 1 on success.
  163. It's called by L<EVP_PKEY_print_private(3)>.
  164. int (*pkey_size) (const EVP_PKEY *pk);
  165. int (*pkey_bits) (const EVP_PKEY *pk);
  166. int (*pkey_security_bits) (const EVP_PKEY *pk);
  167. The pkey_size() method returns the key size in bytes.
  168. It's called by L<EVP_PKEY_get_size(3)>.
  169. The pkey_bits() method returns the key size in bits.
  170. It's called by L<EVP_PKEY_get_bits(3)>.
  171. int (*param_decode) (EVP_PKEY *pkey,
  172. const unsigned char **pder, int derlen);
  173. int (*param_encode) (const EVP_PKEY *pkey, unsigned char **pder);
  174. int (*param_missing) (const EVP_PKEY *pk);
  175. int (*param_copy) (EVP_PKEY *to, const EVP_PKEY *from);
  176. int (*param_cmp) (const EVP_PKEY *a, const EVP_PKEY *b);
  177. int (*param_print) (BIO *out, const EVP_PKEY *pkey, int indent,
  178. ASN1_PCTX *pctx);
  179. The param_decode() and param_encode() methods are called to decode /
  180. encode DER formatted parameters to / from B<pk>.
  181. They MUST return 0 on error, 1 on success.
  182. They're called by L<PEM_read_bio_Parameters(3)> and the B<file:>
  183. L<OSSL_STORE_LOADER(3)>.
  184. The param_missing() method returns 0 if a key parameter is missing,
  185. otherwise 1.
  186. It's called by L<EVP_PKEY_missing_parameters(3)>.
  187. The param_copy() method copies key parameters from B<from> to B<to>.
  188. It MUST return 0 on error, 1 on success.
  189. It's called by L<EVP_PKEY_copy_parameters(3)>.
  190. The param_cmp() method compares the parameters of keys B<a> and B<b>.
  191. It MUST return 1 when the keys are equal, 0 when not equal, or a
  192. negative number on error.
  193. It's called by L<EVP_PKEY_parameters_eq(3)>.
  194. The param_print() method prints the private key parameters in humanly
  195. readable text to B<out>, indented B<indent> spaces.
  196. It MUST return 0 on error, 1 on success.
  197. It's called by L<EVP_PKEY_print_params(3)>.
  198. int (*sig_print) (BIO *out,
  199. const X509_ALGOR *sigalg, const ASN1_STRING *sig,
  200. int indent, ASN1_PCTX *pctx);
  201. The sig_print() method prints a signature in humanly readable text to
  202. B<out>, indented B<indent> spaces.
  203. B<sigalg> contains the exact signature algorithm.
  204. If the signature in B<sig> doesn't correspond to what this method
  205. expects, X509_signature_dump() must be used as a last resort.
  206. It MUST return 0 on error, 1 on success.
  207. It's called by L<X509_signature_print(3)>.
  208. void (*pkey_free) (EVP_PKEY *pkey);
  209. The pkey_free() method helps freeing the internals of B<pkey>.
  210. It's called by L<EVP_PKEY_free(3)>, L<EVP_PKEY_set_type(3)>,
  211. L<EVP_PKEY_set_type_str(3)>, and L<EVP_PKEY_assign(3)>.
  212. int (*pkey_ctrl) (EVP_PKEY *pkey, int op, long arg1, void *arg2);
  213. The pkey_ctrl() method adds extra algorithm specific control.
  214. It's called by L<EVP_PKEY_get_default_digest_nid(3)>,
  215. L<EVP_PKEY_set1_encoded_public_key(3)>,
  216. L<EVP_PKEY_get1_encoded_public_key(3)>, L<PKCS7_SIGNER_INFO_set(3)>,
  217. L<PKCS7_RECIP_INFO_set(3)>, ...
  218. int (*old_priv_decode) (EVP_PKEY *pkey,
  219. const unsigned char **pder, int derlen);
  220. int (*old_priv_encode) (const EVP_PKEY *pkey, unsigned char **pder);
  221. The old_priv_decode() and old_priv_encode() methods decode / encode
  222. they private key B<pkey> from / to a DER formatted array.
  223. These are exclusively used to help decoding / encoding older (pre
  224. PKCS#8) PEM formatted encrypted private keys.
  225. old_priv_decode() MUST return 0 on error, 1 on success.
  226. old_priv_encode() MUST the return same kind of values as
  227. i2d_PrivateKey().
  228. 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);
  234. The item_sign() and item_verify() methods make it possible to have
  235. algorithm specific signatures and verification of them.
  236. item_sign() MUST return one of:
  237. =over 4
  238. =item <=0
  239. error
  240. =item Z<>1
  241. item_sign() did everything, OpenSSL internals just needs to pass the
  242. signature length back.
  243. =item Z<>2
  244. item_sign() did nothing, OpenSSL internal standard routines are
  245. expected to continue with the default signature production.
  246. =item Z<>3
  247. item_sign() set the algorithm identifier B<algor1> and B<algor2>,
  248. OpenSSL internals should just sign using those algorithms.
  249. =back
  250. item_verify() MUST return one of:
  251. =over 4
  252. =item <=0
  253. error
  254. =item Z<>1
  255. item_sign() did everything, OpenSSL internals just needs to pass the
  256. signature length back.
  257. =item Z<>2
  258. item_sign() did nothing, OpenSSL internal standard routines are
  259. expected to continue with the default signature production.
  260. =back
  261. item_verify() and item_sign() are called by L<ASN1_item_verify(3)> and
  262. L<ASN1_item_sign(3)>, and by extension, L<X509_verify(3)>,
  263. L<X509_REQ_verify(3)>, L<X509_sign(3)>, L<X509_REQ_sign(3)>, ...
  264. int (*siginf_set) (X509_SIG_INFO *siginf, const X509_ALGOR *alg,
  265. const ASN1_STRING *sig);
  266. The siginf_set() method is used to set custom B<X509_SIG_INFO>
  267. parameters.
  268. It MUST return 0 on error, or 1 on success.
  269. It's called as part of L<X509_check_purpose(3)>, L<X509_check_ca(3)>
  270. and L<X509_check_issued(3)>.
  271. int (*pkey_check) (const EVP_PKEY *pk);
  272. int (*pkey_public_check) (const EVP_PKEY *pk);
  273. int (*pkey_param_check) (const EVP_PKEY *pk);
  274. The pkey_check(), pkey_public_check() and pkey_param_check() methods are used
  275. to check the validity of B<pk> for key-pair, public component and parameters,
  276. respectively.
  277. They MUST return 0 for an invalid key, or 1 for a valid key.
  278. They are called by L<EVP_PKEY_check(3)>, L<EVP_PKEY_public_check(3)> and
  279. L<EVP_PKEY_param_check(3)> respectively.
  280. int (*set_priv_key) (EVP_PKEY *pk, const unsigned char *priv, size_t len);
  281. int (*set_pub_key) (EVP_PKEY *pk, const unsigned char *pub, size_t len);
  282. The set_priv_key() and set_pub_key() methods are used to set the raw private and
  283. public key data for an EVP_PKEY. They MUST return 0 on error, or 1 on success.
  284. They are called by L<EVP_PKEY_new_raw_private_key(3)>, and
  285. L<EVP_PKEY_new_raw_public_key(3)> respectively.
  286. size_t (*dirty) (const EVP_PKEY *pk);
  287. void *(*export_to) (const EVP_PKEY *pk, EVP_KEYMGMT *keymgmt);
  288. dirty_cnt() returns the internal key's dirty count.
  289. This can be used to synchronise different copies of the same keys.
  290. The export_to() method exports the key material from the given key to
  291. a provider, through the L<EVP_KEYMGMT(3)> interface, if that provider
  292. supports importing key material.
  293. =head2 Functions
  294. EVP_PKEY_asn1_new() creates and returns a new B<EVP_PKEY_ASN1_METHOD>
  295. object, and associates the given B<id>, B<flags>, B<pem_str> and
  296. B<info>.
  297. B<id> is a NID, B<pem_str> is the PEM type string, B<info> is a
  298. descriptive string.
  299. The following B<flags> are supported:
  300. ASN1_PKEY_SIGPARAM_NULL
  301. If B<ASN1_PKEY_SIGPARAM_NULL> is set, then the signature algorithm
  302. parameters are given the type B<V_ASN1_NULL> by default, otherwise
  303. they will be given the type B<V_ASN1_UNDEF> (i.e. the parameter is
  304. omitted).
  305. See L<X509_ALGOR_set0(3)> for more information.
  306. EVP_PKEY_asn1_copy() copies an B<EVP_PKEY_ASN1_METHOD> object from
  307. B<src> to B<dst>.
  308. This function is not thread safe, it's recommended to only use this
  309. when initializing the application.
  310. EVP_PKEY_asn1_free() frees an existing B<EVP_PKEY_ASN1_METHOD> pointed
  311. by B<ameth>.
  312. EVP_PKEY_asn1_add0() adds B<ameth> to the user defined stack of
  313. methods unless another B<EVP_PKEY_ASN1_METHOD> with the same NID is
  314. already there.
  315. This function is not thread safe, it's recommended to only use this
  316. when initializing the application.
  317. EVP_PKEY_asn1_add_alias() creates an alias with the NID B<to> for the
  318. B<EVP_PKEY_ASN1_METHOD> with NID B<from> unless another
  319. B<EVP_PKEY_ASN1_METHOD> with the same NID is already added.
  320. This function is not thread safe, it's recommended to only use this
  321. when initializing the application.
  322. EVP_PKEY_asn1_set_public(), EVP_PKEY_asn1_set_private(),
  323. EVP_PKEY_asn1_set_param(), EVP_PKEY_asn1_set_free(),
  324. EVP_PKEY_asn1_set_ctrl(), EVP_PKEY_asn1_set_item(),
  325. EVP_PKEY_asn1_set_siginf(), EVP_PKEY_asn1_set_check(),
  326. EVP_PKEY_asn1_set_public_check(), EVP_PKEY_asn1_set_param_check(),
  327. EVP_PKEY_asn1_set_security_bits(), EVP_PKEY_asn1_set_set_priv_key(),
  328. EVP_PKEY_asn1_set_set_pub_key(), EVP_PKEY_asn1_set_get_priv_key() and
  329. EVP_PKEY_asn1_set_get_pub_key() set the diverse methods of the given
  330. B<EVP_PKEY_ASN1_METHOD> object.
  331. EVP_PKEY_get0_asn1() finds the B<EVP_PKEY_ASN1_METHOD> associated
  332. with the key B<pkey>.
  333. =head1 RETURN VALUES
  334. EVP_PKEY_asn1_new() returns NULL on error, or a pointer to an
  335. B<EVP_PKEY_ASN1_METHOD> object otherwise.
  336. EVP_PKEY_asn1_add0() and EVP_PKEY_asn1_add_alias() return 0 on error,
  337. or 1 on success.
  338. EVP_PKEY_get0_asn1() returns NULL on error, or a pointer to a constant
  339. B<EVP_PKEY_ASN1_METHOD> object otherwise.
  340. =head1 HISTORY
  341. The signature of the I<pub_decode> functional argument of
  342. EVP_PKEY_asn1_set_public() has changed in OpenSSL 3.0 so its I<pub>
  343. parameter is now constified.
  344. =head1 COPYRIGHT
  345. Copyright 2017-2021 The OpenSSL Project Authors. All Rights Reserved.
  346. Licensed under the Apache License 2.0 (the "License"). You may not use
  347. this file except in compliance with the License. You can obtain a copy
  348. in the file LICENSE in the source distribution or at
  349. L<https://www.openssl.org/source/license.html>.
  350. =cut