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