EVP_PKEY_new.pod 9.9 KB

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