EVP_PKEY_ASN1_METHOD.pod 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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_security_bits,
  16. EVP_PKEY_get0_asn1
  17. - manipulating and registering EVP_PKEY_ASN1_METHOD structure
  18. =head1 SYNOPSIS
  19. #include <openssl/evp.h>
  20. typedef struct evp_pkey_asn1_method_st EVP_PKEY_ASN1_METHOD;
  21. EVP_PKEY_ASN1_METHOD *EVP_PKEY_asn1_new(int id, int flags,
  22. const char *pem_str,
  23. const char *info);
  24. void EVP_PKEY_asn1_copy(EVP_PKEY_ASN1_METHOD *dst,
  25. const EVP_PKEY_ASN1_METHOD *src);
  26. void EVP_PKEY_asn1_free(EVP_PKEY_ASN1_METHOD *ameth);
  27. int EVP_PKEY_asn1_add0(const EVP_PKEY_ASN1_METHOD *ameth);
  28. int EVP_PKEY_asn1_add_alias(int to, int from);
  29. void EVP_PKEY_asn1_set_public(EVP_PKEY_ASN1_METHOD *ameth,
  30. int (*pub_decode) (EVP_PKEY *pk,
  31. X509_PUBKEY *pub),
  32. int (*pub_encode) (X509_PUBKEY *pub,
  33. const EVP_PKEY *pk),
  34. int (*pub_cmp) (const EVP_PKEY *a,
  35. const EVP_PKEY *b),
  36. int (*pub_print) (BIO *out,
  37. const EVP_PKEY *pkey,
  38. int indent, ASN1_PCTX *pctx),
  39. int (*pkey_size) (const EVP_PKEY *pk),
  40. int (*pkey_bits) (const EVP_PKEY *pk));
  41. void EVP_PKEY_asn1_set_private(EVP_PKEY_ASN1_METHOD *ameth,
  42. int (*priv_decode) (EVP_PKEY *pk,
  43. const PKCS8_PRIV_KEY_INFO
  44. *p8inf),
  45. int (*priv_encode) (PKCS8_PRIV_KEY_INFO *p8,
  46. const EVP_PKEY *pk),
  47. int (*priv_print) (BIO *out,
  48. const EVP_PKEY *pkey,
  49. int indent,
  50. ASN1_PCTX *pctx));
  51. void EVP_PKEY_asn1_set_param(EVP_PKEY_ASN1_METHOD *ameth,
  52. int (*param_decode) (EVP_PKEY *pkey,
  53. const unsigned char **pder,
  54. int derlen),
  55. int (*param_encode) (const EVP_PKEY *pkey,
  56. unsigned char **pder),
  57. int (*param_missing) (const EVP_PKEY *pk),
  58. int (*param_copy) (EVP_PKEY *to,
  59. const EVP_PKEY *from),
  60. int (*param_cmp) (const EVP_PKEY *a,
  61. const EVP_PKEY *b),
  62. int (*param_print) (BIO *out,
  63. const EVP_PKEY *pkey,
  64. int indent,
  65. ASN1_PCTX *pctx));
  66. void EVP_PKEY_asn1_set_free(EVP_PKEY_ASN1_METHOD *ameth,
  67. void (*pkey_free) (EVP_PKEY *pkey));
  68. void EVP_PKEY_asn1_set_ctrl(EVP_PKEY_ASN1_METHOD *ameth,
  69. int (*pkey_ctrl) (EVP_PKEY *pkey, int op,
  70. long arg1, void *arg2));
  71. void EVP_PKEY_asn1_set_item(EVP_PKEY_ASN1_METHOD *ameth,
  72. int (*item_verify) (EVP_MD_CTX *ctx,
  73. const ASN1_ITEM *it,
  74. void *asn,
  75. X509_ALGOR *a,
  76. ASN1_BIT_STRING *sig,
  77. EVP_PKEY *pkey),
  78. int (*item_sign) (EVP_MD_CTX *ctx,
  79. const ASN1_ITEM *it,
  80. void *asn,
  81. X509_ALGOR *alg1,
  82. X509_ALGOR *alg2,
  83. ASN1_BIT_STRING *sig));
  84. void EVP_PKEY_asn1_set_security_bits(EVP_PKEY_ASN1_METHOD *ameth,
  85. int (*pkey_security_bits) (const EVP_PKEY
  86. *pk));
  87. const EVP_PKEY_ASN1_METHOD *EVP_PKEY_get0_asn1(const EVP_PKEY *pkey);
  88. =head1 DESCRIPTION
  89. B<EVP_PKEY_ASN1_METHOD> is a structure which holds a set of ASN.1
  90. conversion, printing and information methods for a specific public key
  91. algorithm.
  92. There are two places where the B<EVP_PKEY_ASN1_METHOD> objects are
  93. stored: one is a built-in array representing the standard methods for
  94. different algorithms, and the other one is a stack of user-defined
  95. application-specific methods, which can be manipulated by using
  96. L<EVP_PKEY_asn1_add0(3)>.
  97. =head2 Methods
  98. The methods are the underlying implementations of a particular public
  99. key algorithm present by the B<EVP_PKEY> object.
  100. int (*pub_decode) (EVP_PKEY *pk, X509_PUBKEY *pub);
  101. int (*pub_encode) (X509_PUBKEY *pub, const EVP_PKEY *pk);
  102. int (*pub_cmp) (const EVP_PKEY *a, const EVP_PKEY *b);
  103. int (*pub_print) (BIO *out, const EVP_PKEY *pkey, int indent,
  104. ASN1_PCTX *pctx);
  105. The pub_decode() and pub_encode() methods are called to decode /
  106. encode B<X509_PUBKEY> ASN.1 parameters to / from B<pk>.
  107. They MUST return 0 on error, 1 on success.
  108. They're called by L<X509_PUBKEY_get0(3)> and L<X509_PUBKEY_set(3)>.
  109. The pub_cmp() method is called when two public keys are to be
  110. compared.
  111. It MUST return 1 when the keys are equal, 0 otherwise.
  112. It's called by L<EVP_PKEY_cmp(3)>.
  113. The pub_print() method is called to print a public key in humanly
  114. readable text to B<out>, indented B<indent> spaces.
  115. It MUST return 0 on error, 1 on success.
  116. It's called by L<EVP_PKEY_print_public(3)>.
  117. int (*priv_decode) (EVP_PKEY *pk, const PKCS8_PRIV_KEY_INFO *p8inf);
  118. int (*priv_encode) (PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pk);
  119. int (*priv_print) (BIO *out, const EVP_PKEY *pkey, int indent,
  120. ASN1_PCTX *pctx);
  121. The priv_decode() and priv_encode() methods are called to decode /
  122. encode B<PKCS8_PRIV_KEY_INFO> form private key to / from B<pk>.
  123. They MUST return 0 on error, 1 on success.
  124. They're called by L<EVP_PKCS82PKEY(3)> and L<EVP_PKEY2PKCS8(3)>.
  125. The priv_print() method is called to print a private key in humanly
  126. readable text to B<out>, indented B<indent> spaces.
  127. It MUST return 0 on error, 1 on success.
  128. It's called by L<EVP_PKEY_print_private(3)>.
  129. int (*pkey_size) (const EVP_PKEY *pk);
  130. int (*pkey_bits) (const EVP_PKEY *pk);
  131. int (*pkey_security_bits) (const EVP_PKEY *pk);
  132. The pkey_size() method returns the key size in bytes.
  133. It's called by L<EVP_PKEY_size(3)>.
  134. The pkey_bits() method returns the key size in bits.
  135. It's called by L<EVP_PKEY_bits(3)>.
  136. int (*param_decode) (EVP_PKEY *pkey,
  137. const unsigned char **pder, int derlen);
  138. int (*param_encode) (const EVP_PKEY *pkey, unsigned char **pder);
  139. int (*param_missing) (const EVP_PKEY *pk);
  140. int (*param_copy) (EVP_PKEY *to, const EVP_PKEY *from);
  141. int (*param_cmp) (const EVP_PKEY *a, const EVP_PKEY *b);
  142. int (*param_print) (BIO *out, const EVP_PKEY *pkey, int indent,
  143. ASN1_PCTX *pctx);
  144. The param_decode() and param_encode() methods are called to decode /
  145. encode DER formatted parameters to / from B<pk>.
  146. They MUST return 0 on error, 1 on success.
  147. They're called by L<PEM_read_bio_Parameters(3)> and the B<file:>
  148. L<OSSL_STORE_LOADER(3)>.
  149. The param_missing() method returns 0 if a key parameter is missing,
  150. otherwise 1.
  151. It's called by L<EVP_PKEY_missing_parameters(3)>.
  152. The param_copy() method copies key parameters from B<from> to B<to>.
  153. It MUST return 0 on error, 1 on success.
  154. It's called by L<EVP_PKEY_copy_parameters(3)>.
  155. The param_cmp() method compares the parameters of keys B<a> and B<b>.
  156. It MUST return 1 when the keys are equal, 0 when not equal, or a
  157. negative number on error.
  158. It's called by L<EVP_PKEY_cmp_parameters(3)>.
  159. The param_print() method prints the private key parameters in humanly
  160. readable text to B<out>, indented B<indent> spaces.
  161. It MUST return 0 on error, 1 on success.
  162. It's called by L<EVP_PKEY_print_params(3)>.
  163. int (*sig_print) (BIO *out,
  164. const X509_ALGOR *sigalg, const ASN1_STRING *sig,
  165. int indent, ASN1_PCTX *pctx);
  166. The sig_print() method prints a signature in humanly readable text to
  167. B<out>, indented B<indent> spaces.
  168. B<sigalg> contains the exact signature algorithm.
  169. If the signature in B<sig> doesn't correspond to what this method
  170. expects, X509_signature_dump() must be used as a last resort.
  171. It MUST return 0 on error, 1 on success.
  172. It's called by L<X509_signature_print(3)>.
  173. void (*pkey_free) (EVP_PKEY *pkey);
  174. The pkey_free() method helps freeing the internals of B<pkey>.
  175. It's called by L<EVP_PKEY_free(3)>, L<EVP_PKEY_set_type(3)>,
  176. L<EVP_PKEY_set_type_str(3)>, and L<EVP_PKEY_assign(3)>.
  177. int (*pkey_ctrl) (EVP_PKEY *pkey, int op, long arg1, void *arg2);
  178. The pkey_ctrl() method adds extra algorithm specific control.
  179. It's called by L<EVP_PKEY_get_default_digest_nid(3)>,
  180. L<EVP_PKEY_set1_tls_encodedpoint(3)>,
  181. L<EVP_PKEY_get1_tls_encodedpoint(3)>, L<PKCS7_SIGNER_INFO_set(3)>,
  182. L<PKCS7_RECIP_INFO_set(3)>, ...
  183. int (*old_priv_decode) (EVP_PKEY *pkey,
  184. const unsigned char **pder, int derlen);
  185. int (*old_priv_encode) (const EVP_PKEY *pkey, unsigned char **pder);
  186. The old_priv_decode() and old_priv_encode() methods decode / encode
  187. they private key B<pkey> from / to a DER formatted array.
  188. These are exclusively used to help decoding / encoding older (pre
  189. PKCS#8) PEM formatted encrypted private keys.
  190. old_priv_decode() MUST return 0 on error, 1 on success.
  191. old_priv_encode() MUST the return same kind of values as
  192. i2d_PrivateKey().
  193. They're called by L<d2i_PrivateKey(3)> and L<i2d_PrivateKey(3)>.
  194. int (*item_verify) (EVP_MD_CTX *ctx, const ASN1_ITEM *it, void *asn,
  195. X509_ALGOR *a, ASN1_BIT_STRING *sig, EVP_PKEY *pkey);
  196. int (*item_sign) (EVP_MD_CTX *ctx, const ASN1_ITEM *it, void *asn,
  197. X509_ALGOR *alg1, X509_ALGOR *alg2,
  198. ASN1_BIT_STRING *sig);
  199. The item_sign() and item_verify() methods make it possible to have
  200. algorithm specific signatures and verification of them.
  201. item_sign() MUST return one of:
  202. =over 4
  203. =item <=0
  204. error
  205. =item Z<>1
  206. item_sign() did everything, OpenSSL internals just needs to pass the
  207. signature length back.
  208. =item Z<>2
  209. item_sign() did nothing, OpenSSL internal standard routines are
  210. expected to continue with the default signature production.
  211. =item Z<>3
  212. item_sign() set the algorithm identifier B<algor1> and B<algor2>,
  213. OpenSSL internals should just sign using those algorithms.
  214. =back
  215. item_verify() MUST return one of:
  216. =over 4
  217. =item <=0
  218. error
  219. =item Z<>1
  220. item_sign() did everything, OpenSSL internals just needs to pass the
  221. signature length back.
  222. =item Z<>2
  223. item_sign() did nothing, OpenSSL internal standard routines are
  224. expected to continue with the default signature production.
  225. =back
  226. item_verify() and item_sign() are called by L<ASN1_item_verify(3)> and
  227. L<ASN1_item_sign(3)>, and by extension, L<X509_verify(3)>,
  228. L<X509_REQ_verify(3)>, L<X509_sign(3)>, L<X509_REQ_sign(3)>, ...
  229. =head2 Functions
  230. EVP_PKEY_asn1_new() creates and returns a new B<EVP_PKEY_ASN1_METHOD>
  231. object, and associates the given B<id>, B<flags>, B<pem_str> and
  232. B<info>.
  233. B<id> is a NID, B<pem_str> is the PEM type string, B<info> is a
  234. descriptive string.
  235. The following B<flags> are supported:
  236. ASN1_PKEY_SIGPARAM_NULL
  237. If B<ASN1_PKEY_SIGPARAM_NULL> is set, then the signature algorithm
  238. parameters are given the type B<V_ASN1_NULL> by default, otherwise
  239. they will be given the type B<V_ASN1_UNDEF> (i.e. the parameter is
  240. omitted).
  241. See L<X509_ALGOR_set0(3)> for more information.
  242. EVP_PKEY_asn1_copy() copies an B<EVP_PKEY_ASN1_METHOD> object from
  243. B<src> to B<dst>.
  244. This function is not thread safe, it's recommended to only use this
  245. when initializing the application.
  246. EVP_PKEY_asn1_free() frees an existing B<EVP_PKEY_ASN1_METHOD> pointed
  247. by B<ameth>.
  248. EVP_PKEY_asn1_add0() adds B<ameth> to the user defined stack of
  249. methods unless another B<EVP_PKEY_ASN1_METHOD> with the same NID is
  250. already there.
  251. This function is not thread safe, it's recommended to only use this
  252. when initializing the application.
  253. EVP_PKEY_asn1_add_alias() creates an alias with the NID B<to> for the
  254. B<EVP_PKEY_ASN1_METHOD> with NID B<from> unless another
  255. B<EVP_PKEY_ASN1_METHOD> with the same NID is already added.
  256. This function is not thread safe, it's recommended to only use this
  257. when initializing the application.
  258. EVP_PKEY_asn1_set_public(), EVP_PKEY_asn1_set_private(),
  259. EVP_PKEY_asn1_set_param(), EVP_PKEY_asn1_set_free(),
  260. EVP_PKEY_asn1_set_ctrl(), EVP_PKEY_asn1_set_item(), and
  261. EVP_PKEY_asn1_set_security_bits() set the diverse methods of the given
  262. B<EVP_PKEY_ASN1_METHOD> object.
  263. EVP_PKEY_get0_asn1() finds the B<EVP_PKEY_ASN1_METHOD> associated
  264. with the key B<pkey>.
  265. =head1 RETURN VALUES
  266. EVP_PKEY_asn1_new() returns NULL on error, or a pointer to an
  267. B<EVP_PKEY_ASN1_METHOD> object otherwise.
  268. EVP_PKEY_asn1_add0() and EVP_PKEY_asn1_add_alias() return 0 on error,
  269. or 1 on success.
  270. EVP_PKEY_get0_asn1() returns NULL on error, or a pointer to a constant
  271. B<EVP_PKEY_ASN1_METHOD> object otherwise.
  272. =head1 COPYRIGHT
  273. Copyright 2017 The OpenSSL Project Authors. All Rights Reserved.
  274. Licensed under the OpenSSL license (the "License"). You may not use
  275. this file except in compliance with the License. You can obtain a copy
  276. in the file LICENSE in the source distribution or at
  277. L<https://www.openssl.org/source/license.html>.
  278. =cut