| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | #include <openssl/evp.h> |
| 16 |
| 17 | #include <assert.h> |
| 18 | #include <string.h> |
| 19 |
| 20 | #include <openssl/err.h> |
| 21 | #include <openssl/mem.h> |
| 22 | #include <openssl/nid.h> |
| 23 | #include <openssl/thread.h> |
| 24 |
| 25 | #include "../internal.h" |
| 26 | #include "internal.h" |
| 27 |
| 28 |
| 29 | // Node depends on |EVP_R_NOT_XOF_OR_INVALID_LENGTH|. |
| 30 | // |
| 31 | // TODO(davidben): Fix Node to not touch the error queue itself and remove this. |
| 32 | OPENSSL_DECLARE_ERROR_REASON(EVP, NOT_XOF_OR_INVALID_LENGTH) |
| 33 |
| 34 | // The HPKE module uses the EVP error namespace, but it lives in another |
| 35 | // directory. |
| 36 | OPENSSL_DECLARE_ERROR_REASON(EVP, EMPTY_PSK) |
| 37 |
| 38 | EVP_PKEY *EVP_PKEY_new(void) { |
| 39 | EVP_PKEY *ret = |
| 40 | reinterpret_cast<EVP_PKEY *>(OPENSSL_zalloc(sizeof(EVP_PKEY))); |
| 41 | if (ret == NULL) { |
| 42 | return NULL; |
| 43 | } |
| 44 |
| 45 | ret->type = EVP_PKEY_NONE; |
| 46 | ret->references = 1; |
| 47 | return ret; |
| 48 | } |
| 49 |
| 50 | static void free_it(EVP_PKEY *pkey) { |
| 51 | if (pkey->ameth && pkey->ameth->pkey_free) { |
| 52 | pkey->ameth->pkey_free(pkey); |
| 53 | pkey->pkey = NULL; |
| 54 | pkey->type = EVP_PKEY_NONE; |
| 55 | } |
| 56 | } |
| 57 |
| 58 | void EVP_PKEY_free(EVP_PKEY *pkey) { |
| 59 | if (pkey == NULL) { |
| 60 | return; |
| 61 | } |
| 62 |
| 63 | if (!CRYPTO_refcount_dec_and_test_zero(&pkey->references)) { |
| 64 | return; |
| 65 | } |
| 66 |
| 67 | free_it(pkey); |
| 68 | OPENSSL_free(pkey); |
| 69 | } |
| 70 |
| 71 | int EVP_PKEY_up_ref(EVP_PKEY *pkey) { |
| 72 | CRYPTO_refcount_inc(&pkey->references); |
| 73 | return 1; |
| 74 | } |
| 75 |
| 76 | int EVP_PKEY_is_opaque(const EVP_PKEY *pkey) { |
| 77 | if (pkey->ameth && pkey->ameth->pkey_opaque) { |
| 78 | return pkey->ameth->pkey_opaque(pkey); |
| 79 | } |
| 80 | return 0; |
| 81 | } |
| 82 |
| 83 | int EVP_PKEY_cmp(const EVP_PKEY *a, const EVP_PKEY *b) { |
| 84 | if (a->type != b->type) { |
| 85 | return -1; |
| 86 | } |
| 87 |
| 88 | if (a->ameth) { |
| 89 | int ret; |
| 90 | // Compare parameters if the algorithm has them |
| 91 | if (a->ameth->param_cmp) { |
| 92 | ret = a->ameth->param_cmp(a, b); |
| 93 | if (ret <= 0) { |
| 94 | return ret; |
| 95 | } |
| 96 | } |
| 97 |
| 98 | if (a->ameth->pub_cmp) { |
| 99 | return a->ameth->pub_cmp(a, b); |
| 100 | } |
| 101 | } |
| 102 |
| 103 | return -2; |
| 104 | } |
| 105 |
| 106 | int EVP_PKEY_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from) { |
| 107 | if (to->type == EVP_PKEY_NONE) { |
| 108 | evp_pkey_set_method(to, from->ameth); |
| 109 | } else if (to->type != from->type) { |
| 110 | OPENSSL_PUT_ERROR(EVP, EVP_R_DIFFERENT_KEY_TYPES); |
| 111 | return 0; |
| 112 | } |
| 113 |
| 114 | if (EVP_PKEY_missing_parameters(from)) { |
| 115 | OPENSSL_PUT_ERROR(EVP, EVP_R_MISSING_PARAMETERS); |
| 116 | return 0; |
| 117 | } |
| 118 |
| 119 | // Once set, parameters may not change. |
| 120 | if (!EVP_PKEY_missing_parameters(to)) { |
| 121 | if (EVP_PKEY_cmp_parameters(to, from) == 1) { |
| 122 | return 1; |
| 123 | } |
| 124 | OPENSSL_PUT_ERROR(EVP, EVP_R_DIFFERENT_PARAMETERS); |
| 125 | return 0; |
| 126 | } |
| 127 |
| 128 | if (from->ameth && from->ameth->param_copy) { |
| 129 | return from->ameth->param_copy(to, from); |
| 130 | } |
| 131 |
| 132 | // TODO(https://crbug.com/boringssl/536): If the algorithm takes no |
| 133 | // parameters, copying them should vacuously succeed. |
| 134 | return 0; |
| 135 | } |
| 136 |
| 137 | int EVP_PKEY_missing_parameters(const EVP_PKEY *pkey) { |
| 138 | if (pkey->ameth && pkey->ameth->param_missing) { |
| 139 | return pkey->ameth->param_missing(pkey); |
| 140 | } |
| 141 | return 0; |
| 142 | } |
| 143 |
| 144 | int EVP_PKEY_size(const EVP_PKEY *pkey) { |
| 145 | if (pkey && pkey->ameth && pkey->ameth->pkey_size) { |
| 146 | return pkey->ameth->pkey_size(pkey); |
| 147 | } |
| 148 | return 0; |
| 149 | } |
| 150 |
| 151 | int EVP_PKEY_bits(const EVP_PKEY *pkey) { |
| 152 | if (pkey && pkey->ameth && pkey->ameth->pkey_bits) { |
| 153 | return pkey->ameth->pkey_bits(pkey); |
| 154 | } |
| 155 | return 0; |
| 156 | } |
| 157 |
| 158 | int EVP_PKEY_id(const EVP_PKEY *pkey) { return pkey->type; } |
| 159 |
| 160 | // evp_pkey_asn1_find returns the ASN.1 method table for the given |nid|, which |
| 161 | // should be one of the |EVP_PKEY_*| values. It returns NULL if |nid| is |
| 162 | // unknown. |
| 163 | static const EVP_PKEY_ASN1_METHOD *evp_pkey_asn1_find(int nid) { |
| 164 | switch (nid) { |
| 165 | case EVP_PKEY_RSA: |
| 166 | return &rsa_asn1_meth; |
| 167 | case EVP_PKEY_EC: |
| 168 | return &ec_asn1_meth; |
| 169 | case EVP_PKEY_DSA: |
| 170 | return &dsa_asn1_meth; |
| 171 | case EVP_PKEY_ED25519: |
| 172 | return &ed25519_asn1_meth; |
| 173 | case EVP_PKEY_X25519: |
| 174 | return &x25519_asn1_meth; |
| 175 | default: |
| 176 | return NULL; |
| 177 | } |
| 178 | } |
| 179 |
| 180 | void evp_pkey_set_method(EVP_PKEY *pkey, const EVP_PKEY_ASN1_METHOD *method) { |
| 181 | free_it(pkey); |
| 182 | pkey->ameth = method; |
| 183 | pkey->type = pkey->ameth->pkey_id; |
| 184 | } |
| 185 |
| 186 | int EVP_PKEY_type(int nid) { |
| 187 | // In OpenSSL, this was used to map between type aliases. BoringSSL supports |
| 188 | // no type aliases, so this function is just the identity. |
| 189 | return nid; |
| 190 | } |
| 191 |
| 192 | int EVP_PKEY_assign(EVP_PKEY *pkey, int type, void *key) { |
| 193 | // This function can only be used to assign RSA, DSA, EC, and DH keys. Other |
| 194 | // key types have internal representations which are not exposed through the |
| 195 | // public API. |
| 196 | switch (type) { |
| 197 | case EVP_PKEY_RSA: |
| 198 | return EVP_PKEY_assign_RSA(pkey, reinterpret_cast<RSA *>(key)); |
| 199 | case EVP_PKEY_DSA: |
| 200 | return EVP_PKEY_assign_DSA(pkey, reinterpret_cast<DSA *>(key)); |
| 201 | case EVP_PKEY_EC: |
| 202 | return EVP_PKEY_assign_EC_KEY(pkey, reinterpret_cast<EC_KEY *>(key)); |
| 203 | case EVP_PKEY_DH: |
| 204 | return EVP_PKEY_assign_DH(pkey, reinterpret_cast<DH *>(key)); |
| 205 | } |
| 206 |
| 207 | OPENSSL_PUT_ERROR(EVP, EVP_R_UNSUPPORTED_ALGORITHM); |
| 208 | ERR_add_error_dataf("algorithm %d", type); |
| 209 | return 0; |
| 210 | } |
| 211 |
| 212 | int EVP_PKEY_set_type(EVP_PKEY *pkey, int type) { |
| 213 | if (pkey && pkey->pkey) { |
| 214 | // This isn't strictly necessary, but historically |EVP_PKEY_set_type| would |
| 215 | // clear |pkey| even if |evp_pkey_asn1_find| failed, so we preserve that |
| 216 | // behavior. |
| 217 | free_it(pkey); |
| 218 | } |
| 219 |
| 220 | const EVP_PKEY_ASN1_METHOD *ameth = evp_pkey_asn1_find(type); |
| 221 | if (ameth == NULL) { |
| 222 | OPENSSL_PUT_ERROR(EVP, EVP_R_UNSUPPORTED_ALGORITHM); |
| 223 | ERR_add_error_dataf("algorithm %d", type); |
| 224 | return 0; |
| 225 | } |
| 226 |
| 227 | if (pkey) { |
| 228 | evp_pkey_set_method(pkey, ameth); |
| 229 | } |
| 230 |
| 231 | return 1; |
| 232 | } |
| 233 |
| 234 | EVP_PKEY *EVP_PKEY_new_raw_private_key(int type, ENGINE *unused, |
| 235 | const uint8_t *in, size_t len) { |
| 236 | // To avoid pulling in all key types, look for specifically the key types that |
| 237 | // support |set_priv_raw|. |
| 238 | const EVP_PKEY_ASN1_METHOD *method; |
| 239 | switch (type) { |
| 240 | case EVP_PKEY_X25519: |
| 241 | method = &x25519_asn1_meth; |
| 242 | break; |
| 243 | case EVP_PKEY_ED25519: |
| 244 | method = &ed25519_asn1_meth; |
| 245 | break; |
| 246 | default: |
| 247 | OPENSSL_PUT_ERROR(EVP, EVP_R_UNSUPPORTED_ALGORITHM); |
| 248 | return nullptr; |
| 249 | } |
| 250 |
| 251 | bssl::UniquePtr<EVP_PKEY> ret(EVP_PKEY_new()); |
| 252 | if (ret == nullptr) { |
| 253 | return nullptr; |
| 254 | } |
| 255 | evp_pkey_set_method(ret.get(), method); |
| 256 |
| 257 | if (!ret->ameth->set_priv_raw(ret.get(), in, len)) { |
| 258 | return nullptr; |
| 259 | } |
| 260 |
| 261 | return ret.release(); |
| 262 | } |
| 263 |
| 264 | EVP_PKEY *EVP_PKEY_new_raw_public_key(int type, ENGINE *unused, |
| 265 | const uint8_t *in, size_t len) { |
| 266 | // To avoid pulling in all key types, look for specifically the key types that |
| 267 | // support |set_pub_raw|. |
| 268 | const EVP_PKEY_ASN1_METHOD *method; |
| 269 | switch (type) { |
| 270 | case EVP_PKEY_X25519: |
| 271 | method = &x25519_asn1_meth; |
| 272 | break; |
| 273 | case EVP_PKEY_ED25519: |
| 274 | method = &ed25519_asn1_meth; |
| 275 | break; |
| 276 | default: |
| 277 | OPENSSL_PUT_ERROR(EVP, EVP_R_UNSUPPORTED_ALGORITHM); |
| 278 | return nullptr; |
| 279 | } |
| 280 |
| 281 | bssl::UniquePtr<EVP_PKEY> ret(EVP_PKEY_new()); |
| 282 | if (ret == nullptr) { |
| 283 | return nullptr; |
| 284 | } |
| 285 | evp_pkey_set_method(ret.get(), method); |
| 286 |
| 287 | if (!ret->ameth->set_pub_raw(ret.get(), in, len)) { |
| 288 | return nullptr; |
| 289 | } |
| 290 |
| 291 | return ret.release(); |
| 292 | } |
| 293 |
| 294 | int EVP_PKEY_get_raw_private_key(const EVP_PKEY *pkey, uint8_t *out, |
| 295 | size_t *out_len) { |
| 296 | if (pkey->ameth->get_priv_raw == NULL) { |
| 297 | OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
| 298 | return 0; |
| 299 | } |
| 300 |
| 301 | return pkey->ameth->get_priv_raw(pkey, out, out_len); |
| 302 | } |
| 303 |
| 304 | int EVP_PKEY_get_raw_public_key(const EVP_PKEY *pkey, uint8_t *out, |
| 305 | size_t *out_len) { |
| 306 | if (pkey->ameth->get_pub_raw == NULL) { |
| 307 | OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
| 308 | return 0; |
| 309 | } |
| 310 |
| 311 | return pkey->ameth->get_pub_raw(pkey, out, out_len); |
| 312 | } |
| 313 |
| 314 | int EVP_PKEY_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b) { |
| 315 | if (a->type != b->type) { |
| 316 | return -1; |
| 317 | } |
| 318 | if (a->ameth && a->ameth->param_cmp) { |
| 319 | return a->ameth->param_cmp(a, b); |
| 320 | } |
| 321 | // TODO(https://crbug.com/boringssl/536): If the algorithm doesn't use |
| 322 | // parameters, they should compare as vacuously equal. |
| 323 | return -2; |
| 324 | } |
| 325 |
| 326 | int EVP_PKEY_CTX_set_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD *md) { |
| 327 | return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_TYPE_SIG, EVP_PKEY_CTRL_MD, 0, |
| 328 | (void *)md); |
| 329 | } |
| 330 |
| 331 | int EVP_PKEY_CTX_get_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD **out_md) { |
| 332 | return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_TYPE_SIG, EVP_PKEY_CTRL_GET_MD, |
| 333 | 0, (void *)out_md); |
| 334 | } |
| 335 |
| 336 | void *EVP_PKEY_get0(const EVP_PKEY *pkey) { |
| 337 | // Node references, but never calls this function, so for now we return NULL. |
| 338 | // If other projects require complete support, call |EVP_PKEY_get0_RSA|, etc., |
| 339 | // rather than reading |pkey->pkey| directly. This avoids problems if our |
| 340 | // internal representation does not match the type the caller expects from |
| 341 | // OpenSSL. |
| 342 | return NULL; |
| 343 | } |
| 344 |
| 345 | void OpenSSL_add_all_algorithms(void) {} |
| 346 |
| 347 | void OPENSSL_add_all_algorithms_conf(void) {} |
| 348 |
| 349 | void OpenSSL_add_all_ciphers(void) {} |
| 350 |
| 351 | void OpenSSL_add_all_digests(void) {} |
| 352 |
| 353 | void EVP_cleanup(void) {} |
| 354 |
| 355 | int EVP_PKEY_set1_tls_encodedpoint(EVP_PKEY *pkey, const uint8_t *in, |
| 356 | size_t len) { |
| 357 | if (pkey->ameth->set1_tls_encodedpoint == NULL) { |
| 358 | OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
| 359 | return 0; |
| 360 | } |
| 361 |
| 362 | return pkey->ameth->set1_tls_encodedpoint(pkey, in, len); |
| 363 | } |
| 364 |
| 365 | size_t EVP_PKEY_get1_tls_encodedpoint(const EVP_PKEY *pkey, uint8_t **out_ptr) { |
| 366 | if (pkey->ameth->get1_tls_encodedpoint == NULL) { |
| 367 | OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
| 368 | return 0; |
| 369 | } |
| 370 |
| 371 | return pkey->ameth->get1_tls_encodedpoint(pkey, out_ptr); |
| 372 | } |
| 373 |
| 374 | int EVP_PKEY_base_id(const EVP_PKEY *pkey) { |
| 375 | // OpenSSL has two notions of key type because it supports multiple OIDs for |
| 376 | // the same algorithm: NID_rsa vs NID_rsaEncryption and five distinct spelling |
| 377 | // of DSA. We do not support these, so the base ID is simply the ID. |
| 378 | return EVP_PKEY_id(pkey); |
| 379 | } |
| 380 |