EVP_PKEY_new.pod 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. =pod
  2. =head1 NAME
  3. EVP_PKEY_new,
  4. EVP_PKEY_up_ref,
  5. EVP_PKEY_free,
  6. EVP_PKEY_new_raw_private_key_ex,
  7. EVP_PKEY_new_raw_private_key,
  8. EVP_PKEY_new_raw_public_key_ex,
  9. EVP_PKEY_new_raw_public_key,
  10. EVP_PKEY_new_CMAC_key_ex,
  11. EVP_PKEY_new_CMAC_key,
  12. EVP_PKEY_new_mac_key,
  13. EVP_PKEY_get_raw_private_key,
  14. EVP_PKEY_get_raw_public_key
  15. - public/private key allocation and raw key handling functions
  16. =head1 SYNOPSIS
  17. #include <openssl/evp.h>
  18. EVP_PKEY *EVP_PKEY_new(void);
  19. int EVP_PKEY_up_ref(EVP_PKEY *key);
  20. void EVP_PKEY_free(EVP_PKEY *key);
  21. EVP_PKEY *EVP_PKEY_new_raw_private_key_ex(OPENSSL_CTX *libctx,
  22. const char *keytype,
  23. const char *propq,
  24. const unsigned char *key,
  25. size_t keylen);
  26. EVP_PKEY *EVP_PKEY_new_raw_private_key(int type, ENGINE *e,
  27. const unsigned char *key, size_t keylen);
  28. EVP_PKEY *EVP_PKEY_new_raw_public_key_ex(OPENSSL_CTX *libctx,
  29. const char *keytype,
  30. const char *propq,
  31. const unsigned char *key,
  32. size_t keylen);
  33. EVP_PKEY *EVP_PKEY_new_raw_public_key(int type, ENGINE *e,
  34. const unsigned char *key, size_t keylen);
  35. EVP_PKEY *EVP_PKEY_new_CMAC_key_ex(const unsigned char *priv, size_t len,
  36. const char *cipher_name,
  37. OPENSSL_CTX *libctx, const char *propq);
  38. EVP_PKEY *EVP_PKEY_new_CMAC_key(ENGINE *e, const unsigned char *priv,
  39. size_t len, const EVP_CIPHER *cipher);
  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. =head1 DESCRIPTION
  47. The EVP_PKEY_new() function allocates an empty B<EVP_PKEY> structure which is
  48. used by OpenSSL to store public and private keys. The reference count is set to
  49. B<1>.
  50. EVP_PKEY_up_ref() increments the reference count of I<key>.
  51. EVP_PKEY_free() decrements the reference count of I<key> and, if the reference
  52. count is zero, frees it up. If I<key> is NULL, nothing is done.
  53. EVP_PKEY_new_raw_private_key_ex() allocates a new B<EVP_PKEY>. Unless
  54. an engine should be used for the key type, a provider for the key is found using
  55. the library context I<libctx> and the property query string I<propq>. The
  56. I<keytype> argument indicates what kind of key this is. The value should be a
  57. string for a public key algorithm that supports raw private keys, i.e one of
  58. "POLY1305", "SIPHASH", "X25519", "ED25519", "X448" or "ED448". Note that you may
  59. also use "HMAC" which is not a public key algorithm but is treated as such by
  60. some OpenSSL APIs. You are encouraged to use the EVP_MAC APIs instead for HMAC
  61. (see L<EVP_MAC(3)>). I<key> points to the raw private key data for this
  62. B<EVP_PKEY> which should be of length I<keylen>. The length should be
  63. appropriate for the type of the key. The public key data will be automatically
  64. derived from the given private key data (if appropriate for the algorithm type).
  65. EVP_PKEY_new_raw_private_key() does the same as
  66. EVP_PKEY_new_raw_private_key_ex() except that the default library
  67. context and default property query are used instead. If I<e> is non-NULL then
  68. the new B<EVP_PKEY> structure is associated with the engine I<e>. The I<type>
  69. argument indicates what kind of key this is. The value should be a NID for a
  70. public key algorithm that supports raw private keys, i.e. one of
  71. B<EVP_PKEY_POLY1305>, B<EVP_PKEY_SIPHASH>, B<EVP_PKEY_X25519>,
  72. B<EVP_PKEY_ED25519>, B<EVP_PKEY_X448> or B<EVP_PKEY_ED448>. As for
  73. EVP_PKEY_new_raw_private_key_ex() you may also use B<EVP_PKEY_HMAC>.
  74. EVP_PKEY_new_raw_public_key_ex() works in the same way as
  75. EVP_PKEY_new_raw_private_key_ex() except that I<key> points to the raw
  76. public key data. The B<EVP_PKEY> structure will be initialised without any
  77. private key information. Algorithm types that support raw public keys are
  78. "X25519", "ED25519", "X448" or "ED448".
  79. EVP_PKEY_new_raw_public_key() works in the same way as
  80. EVP_PKEY_new_raw_private_key() except that I<key> points to the raw public key
  81. data. The B<EVP_PKEY> structure will be initialised without any private key
  82. information. Algorithm types that support raw public keys are
  83. B<EVP_PKEY_X25519>, B<EVP_PKEY_ED25519>, B<EVP_PKEY_X448> or B<EVP_PKEY_ED448>.
  84. EVP_PKEY_new_CMAC_key_ex() works in the same way as
  85. EVP_PKEY_new_raw_private_key() except it is only for the B<EVP_PKEY_CMAC>
  86. algorithm type. In addition to the raw private key data, it also takes a cipher
  87. algorithm to be used during creation of a CMAC in the I<cipher> argument. The
  88. cipher should be a standard encryption only cipher. For example AEAD and XTS
  89. ciphers should not be used. Finally it also takes a library context I<libctx>
  90. and property query I<propq> which are used when fetching any cryptographic
  91. algorithms which may be NULL to use the default values.
  92. EVP_PKEY_new_CMAC_key() is the same as EVP_PKEY_new_CMAC_key_ex()
  93. except that the default values are used for I<libctx> and I<propq>.
  94. EVP_PKEY_new_mac_key() works in the same way as EVP_PKEY_new_raw_private_key().
  95. New applications should use EVP_PKEY_new_raw_private_key() instead.
  96. EVP_PKEY_get_raw_private_key() fills the buffer provided by I<priv> with raw
  97. private key data. The size of the I<priv> buffer should be in I<*len> on entry
  98. to the function, and on exit I<*len> is updated with the number of bytes
  99. actually written. If the buffer I<priv> is NULL then I<*len> is populated with
  100. the number of bytes required to hold the key. The calling application is
  101. responsible for ensuring that the buffer is large enough to receive the private
  102. key data. This function only works for algorithms that support raw private keys.
  103. Currently this is: B<EVP_PKEY_HMAC>, B<EVP_PKEY_POLY1305>, B<EVP_PKEY_SIPHASH>,
  104. B<EVP_PKEY_X25519>, B<EVP_PKEY_ED25519>, B<EVP_PKEY_X448> or B<EVP_PKEY_ED448>.
  105. EVP_PKEY_get_raw_public_key() fills the buffer provided by I<pub> with raw
  106. public key data. The size of the I<pub> buffer should be in I<*len> on entry
  107. to the function, and on exit I<*len> is updated with the number of bytes
  108. actually written. If the buffer I<pub> is NULL then I<*len> is populated with
  109. the number of bytes required to hold the key. The calling application is
  110. responsible for ensuring that the buffer is large enough to receive the public
  111. key data. This function only works for algorithms that support raw public keys.
  112. Currently this is: B<EVP_PKEY_X25519>, B<EVP_PKEY_ED25519>, B<EVP_PKEY_X448> or
  113. B<EVP_PKEY_ED448>.
  114. =head1 NOTES
  115. The B<EVP_PKEY> structure is used by various OpenSSL functions which require a
  116. general private key without reference to any particular algorithm.
  117. The structure returned by EVP_PKEY_new() is empty. To add a private or public
  118. key to this empty structure use the appropriate functions described in
  119. L<EVP_PKEY_set1_RSA(3)>, L<EVP_PKEY_set1_DSA(3)>, L<EVP_PKEY_set1_DH(3)> or
  120. L<EVP_PKEY_set1_EC_KEY(3)>.
  121. =head1 RETURN VALUES
  122. EVP_PKEY_new(), EVP_PKEY_new_raw_private_key(), EVP_PKEY_new_raw_public_key(),
  123. EVP_PKEY_new_CMAC_key() and EVP_PKEY_new_mac_key() return either the newly
  124. allocated B<EVP_PKEY> structure or B<NULL> if an error occurred.
  125. EVP_PKEY_up_ref(), EVP_PKEY_get_raw_private_key() and
  126. EVP_PKEY_get_raw_public_key() return 1 for success and 0 for failure.
  127. =head1 SEE ALSO
  128. L<EVP_PKEY_set1_RSA(3)>, L<EVP_PKEY_set1_DSA(3)>, L<EVP_PKEY_set1_DH(3)> or
  129. L<EVP_PKEY_set1_EC_KEY(3)>
  130. =head1 HISTORY
  131. The
  132. EVP_PKEY_new() and EVP_PKEY_free() functions exist in all versions of OpenSSL.
  133. The EVP_PKEY_up_ref() function was added in OpenSSL 1.1.0.
  134. The
  135. EVP_PKEY_new_raw_private_key(), EVP_PKEY_new_raw_public_key(),
  136. EVP_PKEY_new_CMAC_key(), EVP_PKEY_new_raw_private_key() and
  137. EVP_PKEY_get_raw_public_key() functions were added in OpenSSL 1.1.1.
  138. The EVP_PKEY_new_raw_private_key_ex(),
  139. EVP_PKEY_new_raw_public_key_ex() and
  140. EVP_PKEY_new_CMAC_key_ex() functions were added in OpenSSL 3.0.
  141. =head1 COPYRIGHT
  142. Copyright 2002-2020 The OpenSSL Project Authors. All Rights Reserved.
  143. Licensed under the Apache License 2.0 (the "License"). You may not use
  144. this file except in compliance with the License. You can obtain a copy
  145. in the file LICENSE in the source distribution or at
  146. L<https://www.openssl.org/source/license.html>.
  147. =cut