EVP_PKEY_ASN1_METHOD.pod 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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, 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_size(3)>.
  169. The pkey_bits() method returns the key size in bits.
  170. It's called by L<EVP_PKEY_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_supports_digest_nid(3)>,
  216. L<EVP_PKEY_set1_tls_encodedpoint(3)>,
  217. L<EVP_PKEY_get1_tls_encodedpoint(3)>, L<PKCS7_SIGNER_INFO_set(3)>,
  218. L<PKCS7_RECIP_INFO_set(3)>, ...
  219. int (*old_priv_decode) (EVP_PKEY *pkey,
  220. const unsigned char **pder, int derlen);
  221. int (*old_priv_encode) (const EVP_PKEY *pkey, unsigned char **pder);
  222. The old_priv_decode() and old_priv_encode() methods decode / encode
  223. they private key B<pkey> from / to a DER formatted array.
  224. These are exclusively used to help decoding / encoding older (pre
  225. PKCS#8) PEM formatted encrypted private keys.
  226. old_priv_decode() MUST return 0 on error, 1 on success.
  227. old_priv_encode() MUST the return same kind of values as
  228. i2d_PrivateKey().
  229. They're called by L<d2i_PrivateKey(3)> and L<i2d_PrivateKey(3)>.
  230. int (*item_verify) (EVP_MD_CTX *ctx, const ASN1_ITEM *it, void *asn,
  231. X509_ALGOR *a, ASN1_BIT_STRING *sig, EVP_PKEY *pkey);
  232. int (*item_sign) (EVP_MD_CTX *ctx, const ASN1_ITEM *it, void *asn,
  233. X509_ALGOR *alg1, X509_ALGOR *alg2,
  234. 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.
  237. item_sign() MUST return one of:
  238. =over 4
  239. =item <=0
  240. error
  241. =item Z<>1
  242. item_sign() did everything, OpenSSL internals just needs to pass the
  243. signature length back.
  244. =item Z<>2
  245. item_sign() did nothing, OpenSSL internal standard routines are
  246. expected to continue with the default signature production.
  247. =item Z<>3
  248. item_sign() set the algorithm identifier B<algor1> and B<algor2>,
  249. OpenSSL internals should just sign using those algorithms.
  250. =back
  251. item_verify() MUST return one of:
  252. =over 4
  253. =item <=0
  254. error
  255. =item Z<>1
  256. item_sign() did everything, OpenSSL internals just needs to pass the
  257. signature length back.
  258. =item Z<>2
  259. item_sign() did nothing, OpenSSL internal standard routines are
  260. expected to continue with the default signature production.
  261. =back
  262. item_verify() and item_sign() are called by L<ASN1_item_verify(3)> and
  263. L<ASN1_item_sign(3)>, and by extension, L<X509_verify(3)>,
  264. L<X509_REQ_verify(3)>, L<X509_sign(3)>, L<X509_REQ_sign(3)>, ...
  265. int (*siginf_set) (X509_SIG_INFO *siginf, const X509_ALGOR *alg,
  266. const ASN1_STRING *sig);
  267. The siginf_set() method is used to set custom B<X509_SIG_INFO>
  268. parameters.
  269. It MUST return 0 on error, or 1 on success.
  270. It's called as part of L<X509_check_purpose(3)>, L<X509_check_ca(3)>
  271. and L<X509_check_issued(3)>.
  272. int (*pkey_check) (const EVP_PKEY *pk);
  273. int (*pkey_public_check) (const EVP_PKEY *pk);
  274. int (*pkey_param_check) (const EVP_PKEY *pk);
  275. The pkey_check(), pkey_public_check() and pkey_param_check() methods are used
  276. to check the validity of B<pk> for key-pair, public component and parameters,
  277. respectively.
  278. They MUST return 0 for an invalid key, or 1 for a valid key.
  279. They are called by L<EVP_PKEY_check(3)>, L<EVP_PKEY_public_check(3)> and
  280. L<EVP_PKEY_param_check(3)> respectively.
  281. int (*set_priv_key) (EVP_PKEY *pk, const unsigned char *priv, size_t len);
  282. int (*set_pub_key) (EVP_PKEY *pk, const unsigned char *pub, size_t len);
  283. The set_priv_key() and set_pub_key() methods are used to set the raw private and
  284. public key data for an EVP_PKEY. They MUST return 0 on error, or 1 on success.
  285. They are called by L<EVP_PKEY_new_raw_private_key(3)>, and
  286. L<EVP_PKEY_new_raw_public_key(3)> respectively.
  287. size_t (*dirty) (const EVP_PKEY *pk);
  288. void *(*export_to) (const EVP_PKEY *pk, EVP_KEYMGMT *keymgmt);
  289. dirty_cnt() returns the internal key's dirty count.
  290. This can be used to synchronise different copies of the same keys.
  291. The export_to() method exports the key material from the given key to
  292. a provider, through the L<EVP_KEYMGMT(3)> interface, if that provider
  293. supports importing key material.
  294. =head2 Functions
  295. EVP_PKEY_asn1_new() creates and returns a new B<EVP_PKEY_ASN1_METHOD>
  296. object, and associates the given B<id>, B<flags>, B<pem_str> and
  297. B<info>.
  298. B<id> is a NID, B<pem_str> is the PEM type string, B<info> is a
  299. descriptive string.
  300. The following B<flags> are supported:
  301. ASN1_PKEY_SIGPARAM_NULL
  302. If B<ASN1_PKEY_SIGPARAM_NULL> is set, then the signature algorithm
  303. parameters are given the type B<V_ASN1_NULL> by default, otherwise
  304. they will be given the type B<V_ASN1_UNDEF> (i.e. the parameter is
  305. omitted).
  306. See L<X509_ALGOR_set0(3)> for more information.
  307. EVP_PKEY_asn1_copy() copies an B<EVP_PKEY_ASN1_METHOD> object from
  308. B<src> to B<dst>.
  309. This function is not thread safe, it's recommended to only use this
  310. when initializing the application.
  311. EVP_PKEY_asn1_free() frees an existing B<EVP_PKEY_ASN1_METHOD> pointed
  312. by B<ameth>.
  313. EVP_PKEY_asn1_add0() adds B<ameth> to the user defined stack of
  314. methods unless another B<EVP_PKEY_ASN1_METHOD> with the same NID is
  315. already there.
  316. This function is not thread safe, it's recommended to only use this
  317. when initializing the application.
  318. EVP_PKEY_asn1_add_alias() creates an alias with the NID B<to> for the
  319. B<EVP_PKEY_ASN1_METHOD> with NID B<from> unless another
  320. B<EVP_PKEY_ASN1_METHOD> with the same NID is already added.
  321. This function is not thread safe, it's recommended to only use this
  322. when initializing the application.
  323. EVP_PKEY_asn1_set_public(), EVP_PKEY_asn1_set_private(),
  324. EVP_PKEY_asn1_set_param(), EVP_PKEY_asn1_set_free(),
  325. EVP_PKEY_asn1_set_ctrl(), EVP_PKEY_asn1_set_item(),
  326. EVP_PKEY_asn1_set_siginf(), EVP_PKEY_asn1_set_check(),
  327. EVP_PKEY_asn1_set_public_check(), EVP_PKEY_asn1_set_param_check(),
  328. EVP_PKEY_asn1_set_security_bits(), EVP_PKEY_asn1_set_set_priv_key(),
  329. EVP_PKEY_asn1_set_set_pub_key(), EVP_PKEY_asn1_set_get_priv_key() and
  330. EVP_PKEY_asn1_set_get_pub_key() set the diverse methods of the given
  331. B<EVP_PKEY_ASN1_METHOD> object.
  332. EVP_PKEY_get0_asn1() finds the B<EVP_PKEY_ASN1_METHOD> associated
  333. with the key B<pkey>.
  334. =head1 RETURN VALUES
  335. EVP_PKEY_asn1_new() returns NULL on error, or a pointer to an
  336. B<EVP_PKEY_ASN1_METHOD> object otherwise.
  337. EVP_PKEY_asn1_add0() and EVP_PKEY_asn1_add_alias() return 0 on error,
  338. or 1 on success.
  339. EVP_PKEY_get0_asn1() returns NULL on error, or a pointer to a constant
  340. B<EVP_PKEY_ASN1_METHOD> object otherwise.
  341. =head1 COPYRIGHT
  342. Copyright 2017-2020 The OpenSSL Project Authors. All Rights Reserved.
  343. Licensed under the Apache License 2.0 (the "License"). You may not use
  344. this file except in compliance with the License. You can obtain a copy
  345. in the file LICENSE in the source distribution or at
  346. L<https://www.openssl.org/source/license.html>.
  347. =cut