EVP_PKEY.pod 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. =pod
  2. =head1 NAME
  3. EVP_PKEY - an internal description
  4. =head1 SYNOPSIS
  5. #include "crypto/evp.h"
  6. typedef struct evp_pkey_st EVP_PKEY;
  7. =head1 DESCRIPTION
  8. I<This is not a complete description yet>
  9. B<EVP_PKEY> is a complex type that's essentially a container for
  10. private/public key pairs, but has had other uses as well.
  11. =for comment "uses" could as well be "abuses"...
  12. The private/public key pair that an B<EVP_PKEY> contains is refered to
  13. as its "internal key" or "origin" (the reason for "origin" is
  14. explained further down, in L</Export cache for provider operations>),
  15. and it can take one of the following forms:
  16. =over 4
  17. =item legacy origin
  18. This is the form that an B<EVP_PKEY> in OpenSSL prior to 3.0 had. The
  19. internal key in the B<EVP_PKEY> is a pointer to the low-level key
  20. types, such as B<RSA>, B<DSA> and B<EC>, or an engine driven
  21. structure, and is governed by an associated L<EVP_PKEY_METHOD(3)> and
  22. an L<EVP_PKEY_ASN1_METHOD(3)>.
  23. The functions available through those two method structures get full
  24. access to the B<EVP_PKEY> and therefore have a lot of freedom to
  25. modify whatever they want. This also means that an B<EVP_PKEY> is a
  26. shared structure between libcrypto and any ENGINE that serves such
  27. methods.
  28. =item provider-native origin
  29. This is a new form in OpenSSL 3.0, which permits providers to hold the
  30. key data (see L<provider-keymgmt(7)>). The internal key in the
  31. B<EVP_PKEY> is a pointer to that key data held by the provider, and
  32. is governed by an associated L<EVP_KEYMGMT(3)> method structure.
  33. The functions available through the L<EVP_KEYMGMT(3)> have no access
  34. to the B<EVP_PKEY>, and can therefore not make any direct changes.
  35. Similarly, the key data that the B<EVP_PKEY> points at is only known
  36. to the functions pointed at in the L<EVP_KEYMGMT(3)>.
  37. =back
  38. These two forms can never co-exist in the same B<EVP_PKEY>, the main
  39. reason being that having both at the same time will create problems
  40. with synchronising between the two forms, and potentially make it
  41. confusing which one of the two is the origin.
  42. =head2 Key mutability
  43. The B<EVP_PKEY> internal keys are mutable.
  44. This is especially visible with internal legacy keys, since they can
  45. be extracted with functions like L<EVP_PKEY_get0_RSA(3)> and then
  46. modified at will with functions like L<RSA_set0_key(3)>.
  47. Internal provider native keys are also possible to be modified, if the
  48. associated L<EVP_KEYMGMT(3)> implementation allows it. This is done
  49. with L<EVP_PKEY_set_params(3)> and its specialised derivatives. The
  50. OpenSSL providers allow it for the following:
  51. =over 4
  52. =item DH, EC, X25519, X448:
  53. It's possible to set the encoded public key. This is supported in
  54. particular through L<EVP_PKEY_set1_encoded_public_key(3)>.
  55. =item EC:
  56. It's possible to flip the ECDH cofactor mode.
  57. =back
  58. Every time the B<EVP_PKEY> internal key mutates, an internal dirty
  59. count is incremented. The need for a dirty count is explained further
  60. in L</Export cache for provider operations>.
  61. For provider native origin keys, this doesn't require any help from
  62. the L<EVP_KEYMGMT(3)>, the dirty count is maintained in the B<EVP_PKEY>
  63. itself, and is incremented every time L<EVP_PKEY_set_params(3)> or its
  64. specialised derivatives are called.
  65. For legacy origin keys, this requires the associated
  66. L<EVP_PKEY_ASN1_METHOD(3)> to implement the dirty_cnt() function. All
  67. of OpenSSL's built-in L<EVP_PKEY_ASN1_METHOD(3)> implement this
  68. function.
  69. =head2 Export cache for provider operations
  70. OpenSSL 3.0 can handle operations such as signing, encrypting, etc in
  71. diverse providers, potentially others than the provider of the
  72. L<EVP_KEYMGMT(3)>. Two providers, possibly from different vendors,
  73. can't be expected to share internal key structures. There are
  74. therefore instances where key data will need to be exported to the
  75. provider that is going to perform the operation (this also implies
  76. that every provider that implements a key pair based operation must
  77. also implement an L<EVP_KEYMGMT(3)>).
  78. For performance reasons, libcrypto tries to minimize the need to
  79. perform such an export, so it maintains a cache of such exports in the
  80. B<EVP_PKEY>. Each cache entry has two items, a pointer to the
  81. provider side key data and the associated L<EVP_KEYMGMT(3)>.
  82. I<This cache is often referred to as the "operation key cache", and
  83. the key data that the cached keys came from is the "origin", and since
  84. there are two forms of the latter, we have the "legacy origin" and the
  85. "provider native origin".>
  86. The export to the operation key cache can be performed independent of
  87. what form the origin has.
  88. For a legacy origin, this requires that the associated
  89. L<EVP_PKEY_ASN1_METHOD(3)> implements the functions export_to() and
  90. dirty_cnt().
  91. For a provider native origin, this requires that the associated
  92. L<EVP_KEYMGMT(3)> implements the OSSL_FUNC_keymgmt_export() function
  93. (see L<provider-keymgmt(7)>).
  94. In all cases, the receiving L<EVP_KEYMGMT(3)> (the one associated with
  95. the exported key data) must implement OSSL_FUNC_keymgmt_import().
  96. If such caching isn't supported, the operations that can be performed
  97. with that key are limited to the same backend as the origin key
  98. (ENGINE for legacy origin keys, provider for provider side origin
  99. keys).
  100. =head3 Exporting implementation details
  101. Exporting a key to the operation cache involves the following:
  102. =over 4
  103. =item 1.
  104. Check if the dirty count for the internal origin key has changed since
  105. the previous time. This is done by comparing it with a copy of the
  106. dirty count, which is maintained by the export function.
  107. If the dirty count has changed, the export cache is cleared.
  108. =item 2.
  109. Check if there's an entry in the export cache with the same
  110. L<EVP_KEYMGMT(3)> that's the same provider that an export is to be
  111. made to (which is the provider that's going to perform an operation
  112. for which the current B<EVP_PKEY> is going to be used).
  113. If such an entry is found, nothing more is done, the key data and
  114. L<EVP_KEYMGMT(3)> found in that export cache entry will be used for
  115. the operation to be performed.
  116. =item 3.
  117. Export the internal origin key to the provider, using the appropriate
  118. method.
  119. For legacy origin keys, that's done with the help of the
  120. L<EVP_PKEY_ASN1_METHOD(3)> export_to() function.
  121. For provider native origin keys, that's done by retrieving the key
  122. data in L<OSSL_PARAM(3)> form from the origin keys, using the
  123. OSSL_FUNC_keymgmt_export() functions of the associated
  124. L<EVP_KEYMGMT(3)>, and sending that data to the L<EVP_KEYMGMT(3)> of
  125. the provider that's to perform the operation, using its
  126. OSSL_FUNC_keymgmt_import() function.
  127. =back
  128. =head2 Upgrading and downgrading a key
  129. An B<EVP_PKEY> with a legacy origin will I<never> be upgraded to
  130. become an B<EVP_PKEY> with a provider native origin. Instead, we have
  131. the operation cache as described above, that takes care of the needs
  132. of the diverse operation the application may want to perform.
  133. An B<EVP_PKEY> with a provider native origin, I<may> be downgraded to
  134. be I<transformed> into an B<EVP_PKEY> with a legacy origin. Because
  135. an B<EVP_PKEY> can't have two origins, it means that it stops having a
  136. provider native origin. The previous provider native key data is
  137. moved to the operation cache. Downgrading is performed with the
  138. internal function L<evp_pkey_downgrade(3)>.
  139. I<Downgrading a key is understandably fragile>, and possibly surprising,
  140. and should therefore be done I<as little as possible>, but is needed
  141. to be able to support functions like L<EVP_PKEY_get0_RSA(3)>.
  142. The general recommendation is to use L<evp_pkey_copy_downgraded(3)>
  143. whenever possible, which it should be if the need for a legacy origin
  144. is only internal, or better yet, to remove the need for downgrade at
  145. all.
  146. =head1 SEE ALSO
  147. L<provider-keymgmt(7)>
  148. =head1 COPYRIGHT
  149. Copyright 2020-2021 The OpenSSL Project Authors. All Rights Reserved.
  150. Licensed under the Apache License 2.0 (the "License"). You may not use
  151. this file except in compliance with the License. You can obtain a copy
  152. in the file LICENSE in the source distribution or at
  153. L<https://www.openssl.org/source/license.html>.
  154. =cut