EVP_PKEY_new.pod 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. =pod
  2. =head1 NAME
  3. EVP_PKEY,
  4. EVP_PKEY_new,
  5. EVP_PKEY_up_ref,
  6. EVP_PKEY_dup,
  7. EVP_PKEY_free,
  8. EVP_PKEY_get0_description,
  9. EVP_PKEY_new_raw_private_key_ex,
  10. EVP_PKEY_new_raw_private_key,
  11. EVP_PKEY_new_raw_public_key_ex,
  12. EVP_PKEY_new_raw_public_key,
  13. EVP_PKEY_new_CMAC_key,
  14. EVP_PKEY_new_mac_key,
  15. EVP_PKEY_get_raw_private_key,
  16. EVP_PKEY_get_raw_public_key
  17. - public/private key allocation and raw key handling functions
  18. =head1 SYNOPSIS
  19. #include <openssl/evp.h>
  20. typedef evp_pkey_st EVP_PKEY;
  21. EVP_PKEY *EVP_PKEY_new(void);
  22. int EVP_PKEY_up_ref(EVP_PKEY *key);
  23. EVP_PKEY *EVP_PKEY_dup(EVP_PKEY *key);
  24. void EVP_PKEY_free(EVP_PKEY *key);
  25. const char *EVP_PKEY_get0_description(const EVP_PKEY *key);
  26. EVP_PKEY *EVP_PKEY_new_raw_private_key_ex(OSSL_LIB_CTX *libctx,
  27. const char *keytype,
  28. const char *propq,
  29. const unsigned char *key,
  30. size_t keylen);
  31. EVP_PKEY *EVP_PKEY_new_raw_private_key(int type, ENGINE *e,
  32. const unsigned char *key, size_t keylen);
  33. EVP_PKEY *EVP_PKEY_new_raw_public_key_ex(OSSL_LIB_CTX *libctx,
  34. const char *keytype,
  35. const char *propq,
  36. const unsigned char *key,
  37. size_t keylen);
  38. EVP_PKEY *EVP_PKEY_new_raw_public_key(int type, ENGINE *e,
  39. const unsigned char *key, size_t keylen);
  40. EVP_PKEY *EVP_PKEY_new_mac_key(int type, ENGINE *e, const unsigned char *key,
  41. int keylen);
  42. int EVP_PKEY_get_raw_private_key(const EVP_PKEY *pkey, unsigned char *priv,
  43. size_t *len);
  44. int EVP_PKEY_get_raw_public_key(const EVP_PKEY *pkey, unsigned char *pub,
  45. size_t *len);
  46. Deprecated since OpenSSL 3.0, can be hidden entirely by defining
  47. B<OPENSSL_API_COMPAT> with a suitable version value, see
  48. L<openssl_user_macros(7)>:
  49. EVP_PKEY *EVP_PKEY_new_CMAC_key(ENGINE *e, const unsigned char *priv,
  50. size_t len, const EVP_CIPHER *cipher);
  51. =head1 DESCRIPTION
  52. B<EVP_PKEY> is a generic structure to hold diverse types of asymmetric keys
  53. (also known as "key pairs"), and can be used for diverse operations, like
  54. signing, verifying signatures, key derivation, etc. The asymmetric keys
  55. themselves are often refered to as the "internal key", and are handled by
  56. backends, such as providers (through L<EVP_KEYMGMT(3)>) or B<ENGINE>s.
  57. Conceptually, an B<EVP_PKEY> internal key may hold a private key, a public
  58. key, or both (a keypair), and along with those, key parameters if the key type
  59. requires them. The presence of these components determine what operations can
  60. be made; for example, signing normally requires the presence of a private key,
  61. and verifying normally requires the presence of a public key.
  62. =for comment ED signature require both the private and public key...
  63. B<EVP_PKEY> has also been used for MAC algorithm that were conceived as
  64. producing signatures, although not being public key algorithms; "POLY1305",
  65. "SIPHASH", "HMAC", "CMAC". This usage is considered legacy and is discouraged
  66. in favor of the L<EVP_MAC(3)> API.
  67. The EVP_PKEY_new() function allocates an empty B<EVP_PKEY> structure which is
  68. used by OpenSSL to store public and private keys. The reference count is set to
  69. B<1>.
  70. EVP_PKEY_up_ref() increments the reference count of I<key>.
  71. EVP_PKEY_dup() duplicates the I<key>. The I<key> must not be ENGINE based or
  72. a raw key, otherwise the duplication will fail.
  73. EVP_PKEY_free() decrements the reference count of I<key> and, if the reference
  74. count is zero, frees it up. If I<key> is NULL, nothing is done.
  75. EVP_PKEY_get0_description() returns a description of the type of B<EVP_PKEY>,
  76. meant for display and human consumption. The description is at the
  77. discretion of the key type implementation.
  78. EVP_PKEY_new_raw_private_key_ex() allocates a new B<EVP_PKEY>. Unless an
  79. engine should be used for the key type, a provider for the key is found using
  80. the library context I<libctx> and the property query string I<propq>. The
  81. I<keytype> argument indicates what kind of key this is. The value should be a
  82. string for a public key algorithm that supports raw private keys, i.e one of
  83. "X25519", "ED25519", "X448" or "ED448". I<key> points to the raw private key
  84. data for this B<EVP_PKEY> which should be of length I<keylen>. The length
  85. should be appropriate for the type of the key. The public key data will be
  86. automatically derived from the given private key data (if appropriate for the
  87. algorithm type).
  88. EVP_PKEY_new_raw_private_key() does the same as
  89. EVP_PKEY_new_raw_private_key_ex() except that the default library context and
  90. default property query are used instead. If I<e> is non-NULL then the new
  91. B<EVP_PKEY> structure is associated with the engine I<e>. The I<type> argument
  92. indicates what kind of key this is. The value should be a NID for a public key
  93. algorithm that supports raw private keys, i.e. one of B<EVP_PKEY_X25519>,
  94. B<EVP_PKEY_ED25519>, B<EVP_PKEY_X448> or B<EVP_PKEY_ED448>.
  95. EVP_PKEY_new_raw_private_key_ex() and EVP_PKEY_new_raw_private_key() may also
  96. be used with most MACs implemented as public key algorithms, so key types such
  97. as "HMAC", "POLY1305", "SIPHASH", or their NID form B<EVP_PKEY_POLY1305>,
  98. B<EVP_PKEY_SIPHASH>, B<EVP_PKEY_HMAC> are also accepted. This usage is,
  99. as mentioned above, discouraged in favor of the L<EVP_MAC(3)> API.
  100. EVP_PKEY_new_raw_public_key_ex() works in the same way as
  101. EVP_PKEY_new_raw_private_key_ex() except that I<key> points to the raw
  102. public key data. The B<EVP_PKEY> structure will be initialised without any
  103. private key information. Algorithm types that support raw public keys are
  104. "X25519", "ED25519", "X448" or "ED448".
  105. EVP_PKEY_new_raw_public_key() works in the same way as
  106. EVP_PKEY_new_raw_private_key() except that I<key> points to the raw public key
  107. data. The B<EVP_PKEY> structure will be initialised without any private key
  108. information. Algorithm types that support raw public keys are
  109. B<EVP_PKEY_X25519>, B<EVP_PKEY_ED25519>, B<EVP_PKEY_X448> or B<EVP_PKEY_ED448>.
  110. EVP_PKEY_new_mac_key() works in the same way as EVP_PKEY_new_raw_private_key().
  111. New applications should use EVP_PKEY_new_raw_private_key() instead.
  112. EVP_PKEY_get_raw_private_key() fills the buffer provided by I<priv> with raw
  113. private key data. The size of the I<priv> buffer should be in I<*len> on entry
  114. to the function, and on exit I<*len> is updated with the number of bytes
  115. actually written. If the buffer I<priv> is NULL then I<*len> is populated with
  116. the number of bytes required to hold the key. The calling application is
  117. responsible for ensuring that the buffer is large enough to receive the private
  118. key data. This function only works for algorithms that support raw private keys.
  119. Currently this is: B<EVP_PKEY_HMAC>, B<EVP_PKEY_POLY1305>, B<EVP_PKEY_SIPHASH>,
  120. B<EVP_PKEY_X25519>, B<EVP_PKEY_ED25519>, B<EVP_PKEY_X448> or B<EVP_PKEY_ED448>.
  121. EVP_PKEY_get_raw_public_key() fills the buffer provided by I<pub> with raw
  122. public key data. The size of the I<pub> buffer should be in I<*len> on entry
  123. to the function, and on exit I<*len> is updated with the number of bytes
  124. actually written. If the buffer I<pub> is NULL then I<*len> is populated with
  125. the number of bytes required to hold the key. The calling application is
  126. responsible for ensuring that the buffer is large enough to receive the public
  127. key data. This function only works for algorithms that support raw public keys.
  128. Currently this is: B<EVP_PKEY_X25519>, B<EVP_PKEY_ED25519>, B<EVP_PKEY_X448> or
  129. B<EVP_PKEY_ED448>.
  130. EVP_PKEY_new_CMAC_key() works in the same way as EVP_PKEY_new_raw_private_key()
  131. except it is only for the B<EVP_PKEY_CMAC> algorithm type. In addition to the
  132. raw private key data, it also takes a cipher algorithm to be used during
  133. creation of a CMAC in the B<cipher> argument. The cipher should be a standard
  134. encryption-only cipher. For example AEAD and XTS ciphers should not be used.
  135. Applications should use the L<EVP_MAC(3)> API instead
  136. and set the B<OSSL_MAC_PARAM_CIPHER> parameter on the B<EVP_MAC_CTX> object
  137. with the name of the cipher being used.
  138. =head1 NOTES
  139. The B<EVP_PKEY> structure is used by various OpenSSL functions which require a
  140. general private key without reference to any particular algorithm.
  141. The structure returned by EVP_PKEY_new() is empty. To add a private or public
  142. key to this empty structure use the appropriate functions described in
  143. L<EVP_PKEY_set1_RSA(3)>, L<EVP_PKEY_set1_DSA(3)>, L<EVP_PKEY_set1_DH(3)> or
  144. L<EVP_PKEY_set1_EC_KEY(3)>.
  145. =head1 RETURN VALUES
  146. EVP_PKEY_new(), EVP_PKEY_new_raw_private_key(), EVP_PKEY_new_raw_public_key(),
  147. EVP_PKEY_new_CMAC_key() and EVP_PKEY_new_mac_key() return either the newly
  148. allocated B<EVP_PKEY> structure or NULL if an error occurred.
  149. EVP_PKEY_dup() returns the key duplicate or NULL if an error occurred.
  150. EVP_PKEY_up_ref(), EVP_PKEY_get_raw_private_key() and
  151. EVP_PKEY_get_raw_public_key() return 1 for success and 0 for failure.
  152. =head1 SEE ALSO
  153. L<EVP_PKEY_set1_RSA(3)>, L<EVP_PKEY_set1_DSA(3)>, L<EVP_PKEY_set1_DH(3)> or
  154. L<EVP_PKEY_set1_EC_KEY(3)>
  155. =head1 HISTORY
  156. The
  157. EVP_PKEY_new() and EVP_PKEY_free() functions exist in all versions of OpenSSL.
  158. The EVP_PKEY_up_ref() function was added in OpenSSL 1.1.0.
  159. The
  160. EVP_PKEY_new_raw_private_key(), EVP_PKEY_new_raw_public_key(),
  161. EVP_PKEY_new_CMAC_key(), EVP_PKEY_new_raw_private_key() and
  162. EVP_PKEY_get_raw_public_key() functions were added in OpenSSL 1.1.1.
  163. The EVP_PKEY_dup(), EVP_PKEY_new_raw_private_key_ex(), and
  164. EVP_PKEY_new_raw_public_key_ex()
  165. functions were added in OpenSSL 3.0.
  166. The EVP_PKEY_new_CMAC_key() was deprecated in OpenSSL 3.0.
  167. The documentation of B<EVP_PKEY> was amended in OpenSSL 3.0 to allow there to
  168. be the private part of the keypair without the public part, where this was
  169. previously implied to be disallowed.
  170. =head1 COPYRIGHT
  171. Copyright 2002-2021 The OpenSSL Project Authors. All Rights Reserved.
  172. Licensed under the Apache License 2.0 (the "License"). You may not use
  173. this file except in compliance with the License. You can obtain a copy
  174. in the file LICENSE in the source distribution or at
  175. L<https://www.openssl.org/source/license.html>.
  176. =cut