/* * Copyright 2015-2026 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ /* We need to use some deprecated APIs */ #define OPENSSL_SUPPRESS_DEPRECATED /* * Really these tests should be in evp_extra_test - but that doesn't * yet support testing with a non-default libctx. Once it does we should move * everything into one file. Consequently some things are duplicated between * the two files. */ #include #include #include #include #include #include #include #include "testutil.h" #include "internal/nelem.h" #include "crypto/evp.h" #include "../crypto/evp/evp_local.h" /* Defined in tls-provider.c */ int tls_provider_init(const OSSL_CORE_HANDLE *handle, const OSSL_DISPATCH *in, const OSSL_DISPATCH **out, void **provctx); static OSSL_LIB_CTX *mainctx = NULL; static OSSL_PROVIDER *nullprov = NULL; /* * kExampleRSAKeyDER is an RSA private key in ASN.1, DER format. Of course, you * should never use this key anywhere but in an example. */ static const unsigned char kExampleRSAKeyDER[] = { 0x30, 0x82, 0x02, 0x5c, 0x02, 0x01, 0x00, 0x02, 0x81, 0x81, 0x00, 0xf8, 0xb8, 0x6c, 0x83, 0xb4, 0xbc, 0xd9, 0xa8, 0x57, 0xc0, 0xa5, 0xb4, 0x59, 0x76, 0x8c, 0x54, 0x1d, 0x79, 0xeb, 0x22, 0x52, 0x04, 0x7e, 0xd3, 0x37, 0xeb, 0x41, 0xfd, 0x83, 0xf9, 0xf0, 0xa6, 0x85, 0x15, 0x34, 0x75, 0x71, 0x5a, 0x84, 0xa8, 0x3c, 0xd2, 0xef, 0x5a, 0x4e, 0xd3, 0xde, 0x97, 0x8a, 0xdd, 0xff, 0xbb, 0xcf, 0x0a, 0xaa, 0x86, 0x92, 0xbe, 0xb8, 0x50, 0xe4, 0xcd, 0x6f, 0x80, 0x33, 0x30, 0x76, 0x13, 0x8f, 0xca, 0x7b, 0xdc, 0xec, 0x5a, 0xca, 0x63, 0xc7, 0x03, 0x25, 0xef, 0xa8, 0x8a, 0x83, 0x58, 0x76,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0x7d, 0xf1, 0xdb, 0xd9, 0xb1, 0x41, 0x41, 0x68, 0x2f, 0x4e, 0x39, 0x02, 0x40, 0x5a, 0x34, 0x66, 0xd8, 0xf5, 0xe2, 0x7f, 0x18, 0xb5, 0x00, 0x6e, 0x26, 0x84, 0x27, 0x14, 0x93, 0xfb, 0xfc, 0xc6, 0x0f, 0x5e, 0x27, 0xe6, 0xe1, 0xe9, 0xc0, 0x8a, 0xe4, 0x34, 0xda, 0xe9, 0xa2, 0x4b, 0x73, 0xbc, 0x8c, 0xb9, 0xba, 0x13, 0x6c, 0x7a, 0x2b, 0x51, 0x84, 0xa3, 0x4a, 0xe0, 0x30, 0x10, 0x06, 0x7e, 0xed, 0x17, 0x5a, 0x14, 0x00, 0xc9, 0xef, 0x85, 0xea, 0x52, 0x2c, 0xbc, 0x65, 0x02, 0x40, 0x51, 0xe3, 0xf2, 0x83, 0x19, 0x9b, 0xc4, 0x1e, 0x2f, 0x50, 0x3d, 0xdf, 0x5a, 0xa2, 0x18, 0xca, 0x5f, 0x2e, 0x49, 0xaf, 0x6f, 0xcc, 0xfa, 0x65, 0x77, 0x94, 0xb5, 0xa1, 0x0a, 0xa9, 0xd1, 0x8a, 0x39, 0x37, 0xf4, 0x0b, 0xa0, 0xd7, 0x82, 0x27, 0x5e, 0xae, 0x17, 0x17, 0xa1, 0x1e, 0x54, 0x34, 0xbf, 0x6e, 0xc4, 0x8e, 0x99, 0x5d, 0x08, 0xf1, 0x2d, 0x86, 0x9d, 0xa5, 0x20, 0x1b, 0xe5, 0xdf }; /* * kExampleRSAKeyPKCS8 is kExampleRSAKeyDER encoded in a PKCS #8 * PrivateKeyInfo. */ static const unsigned char kExampleRSAKeyPKCS8[] = { 0x30, 0x82, 0x02, 0x76, 0x02, 0x01, 0x00, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x04, 0x82, 0x02, 0x60, 0x30, 0x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x5a, 0xa2, 0x18, 0xca, 0x5f, 0x2e, 0x49, 0xaf, 0x6f, 0xcc, 0xfa, 0x65, 0x77, 0x94, 0xb5, 0xa1, 0x0a, 0xa9, 0xd1, 0x8a, 0x39, 0x37, 0xf4, 0x0b, 0xa0, 0xd7, 0x82, 0x27, 0x5e, 0xae, 0x17, 0x17, 0xa1, 0x1e, 0x54, 0x34, 0xbf, 0x6e, 0xc4, 0x8e, 0x99, 0x5d, 0x08, 0xf1, 0x2d, 0x86, 0x9d, 0xa5, 0x20, 0x1b, 0xe5, 0xdf }; /* * An RSA-PSS 2048-bit private key in ASN.1 DER form. Used by the * table-driven checks instead of keygen to save CI cycles (RSA keygen * at this size is slow). Generated with: * > openssl genpkey -quiet -algorithm rsa-pss -outform DER > rsa-pss.der * > xxd -i < rsa-pss.der * Of course, never use this key for anything but tests. */ static const unsigned char kExampleRSAPSSKeyDER[] = { 0x30, 0x82, 0x04, 0xba, 0x02, 0x01, 0x00, 0x30, 0x0b, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0a, 0x04, 0x82, 0x04, 0xa6, 0x30, 0x82, 0x04, 0xa2, 0x02, 0x01, 0x00, 0x02, 0x82, 0x01, 0x01, 0x00, 0xa3, 0x32, 0x24, 0xdb, 0x9b, 0xec, 0x8f, 0x18, 0xbe, 0xeb, 0xef, 0x9b, 0x0e, 0x06, 0x11, 0x78, 0x26, 0x66, 0x2b, 0x75, 0xff, 0x79, 0x50, 0xa9, 0x96, 0xb2, 0xdf, 0xe1, 0x71, 0x2f, 0x8f, 0x3f, 0x08, 0x16, 0x3f, 0x53, 0x5a, 0x2c, 0x7c, 0xbb, 0xb9, 0xa2, 0x54, 0x2c, 0x81, 0xaf, 0x58, 0xd1, 0xb4, 0xfe, 0xd6, 0x2a, 0x5c, 0x47, 0xfe, 0x20, 0xf0, 0xce, 0xdc, 0xfe, 0x08, 0x7f, 0x3a, 0x24, 0xcd, 0xa0, 0x87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0xed, 0xed, 0x7d, 0xa8 }; #ifndef OPENSSL_NO_DH static char group_ffdhe2048[] = "ffdhe2048"; static const OSSL_PARAM dhx_keygen_params[] = { OSSL_PARAM_utf8_string("group", group_ffdhe2048, sizeof(group_ffdhe2048) - 1), OSSL_PARAM_END }; #endif #ifndef OPENSSL_NO_EC static char group_p256[] = "P-256"; static const OSSL_PARAM ec_keygen_params[] = { OSSL_PARAM_utf8_string("group", group_p256, sizeof(group_p256) - 1), OSSL_PARAM_END }; #endif #ifndef OPENSSL_NO_DH static const unsigned char kExampleDHPrivateKeyDER[] = { 0x30, 0x82, 0x02, 0x26, 0x02, 0x01, 0x00, 0x30, 0x82, 0x01, 0x17, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x03, 0x01, 0x30, 0x82, 0x01, 0x08, 0x02, 0x82, 0x01, 0x01, 0x00, 0xD8, 0x4B, 0x0F, 0x0E, 0x6B, 0x79, 0xE9, 0x23, 0x4E, 0xE4, 0xBE, 0x9A, 0x8F, 0x7A, 0x5C, 0xA3, 0x20, 0xD0, 0x86, 0x6B, 0x95, 0x78, 0x39, 0x59, 0x7A, 0x11, 0x2A, 0x5B, 0x87, 0xA4, 0xFB, 0x2F, 0x99, 0xD0, 0x57, 0xF5, 0xE1, 0xA3, 0xAF, 0x41, 0xD1, 0xCD, 0xA3, 0x94, 0xBB, 0xE5, 0x5A, 0x68, 0xE2, 0xEE, 0x69, 0x56, 0x51, 0xB2, 0xEE, 0xF2, 0xFE, 0x10, 0xC9, 0x55, 0xE3, 0x82, 0x3C, 0x50, 0x0D, 0xF5, 0x82, 0x73, 0xE4, 0xD6, 0x3E, 0x45, 0xB4, 0x89, 0x80, 0xE4, 0xF0, 0x99, 0x85, 0x2B, 0x4B, 0xF9, 0xB8, 0xFD, 0x2C, 0x3C, 0x49, 0x2E, 0xB3, 0x56, 0x7E, 0x99, 0x07, 0xD3, 0xF7, 0xD9, 0xE4, 0x0C, 0x64, 0xC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, 0xFF, 0x92, 0xC1, 0x42, 0x1C, 0x30, 0x87, 0x74, 0x1E, 0x53, 0x57, 0x7C, 0xF8, 0x77, 0x51, 0xF1, 0x74, 0x16, 0xF4, 0x45, 0x26, 0x77, 0x0A, 0x05, 0x96, 0x13, 0x12, 0x06, 0x86, 0x2B, 0xB8, 0x49, 0x82, 0x69, 0x43, 0x0A, 0x57, 0xA7, 0x30, 0x19, 0x4C, 0xB8, 0x47, 0x82, 0x6E, 0x64, 0x7A, 0x06, 0x13, 0x5A, 0x82, 0x98, 0xD6, 0x7A, 0x09, 0xEC, 0x03, 0x8D, 0x03 }; #endif /* OPENSSL_NO_DH */ #ifndef OPENSSL_NO_EC /* * kExampleECKeyDER is a sample EC private key encoded as an ECPrivateKey * structure. */ static const unsigned char kExampleECKeyDER[] = { 0x30, 0x77, 0x02, 0x01, 0x01, 0x04, 0x20, 0x07, 0x0f, 0x08, 0x72, 0x7a, 0xd4, 0xa0, 0x4a, 0x9c, 0xdd, 0x59, 0xc9, 0x4d, 0x89, 0x68, 0x77, 0x08, 0xb5, 0x6f, 0xc9, 0x5d, 0x30, 0x77, 0x0e, 0xe8, 0xd1, 0xc9, 0xce, 0x0a, 0x8b, 0xb4, 0x6a, 0xa0, 0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0xa1, 0x44, 0x03, 0x42, 0x00, 0x04, 0xe6, 0x2b, 0x69, 0xe2, 0xbf, 0x65, 0x9f, 0x97, 0xbe, 0x2f, 0x1e, 0x0d, 0x94, 0x8a, 0x4c, 0xd5, 0x97, 0x6b, 0xb7, 0xa9, 0x1e, 0x0d, 0x46, 0xfb, 0xdd, 0xa9, 0xa9, 0x1e, 0x9d, 0xdc, 0xba, 0x5a, 0x01, 0xe7, 0xd6, 0x97, 0xa8, 0x0a, 0x18, 0xf9, 0xc3, 0xc4, 0xa3, 0x1e, 0x56, 0xe2, 0x7c, 0x83, 0x48, 0xdb, 0x16, 0x1a, 0x1c, 0xf5, 0x1d, 0x7e, 0xf1, 0x94, 0x2d, 0x4b, 0xcf, 0x72, 0x22, 0xc1 }; /* P-384 sample EC private key in PKCS8 format (no public key) */ static const unsigned char kExampleECKey2DER[] = { 0x30, 0x4E, 0x02, 0x01, 0x00, 0x30, 0x10, 0x06, 0x07, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x02, 0x01, 0x06, 0x05, 0x2B, 0x81, 0x04, 0x00, 0x22, 0x04, 0x37, 0x30, 0x35, 0x02, 0x01, 0x01, 0x04, 0x30, 0x73, 0xE3, 0x3A, 0x05, 0xF2, 0xB6, 0x99, 0x6D, 0x0C, 0x33, 0x7F, 0x15, 0x9E, 0x10, 0xA9, 0x17, 0x4C, 0x0A, 0x82, 0x57, 0x71, 0x13, 0x7A, 0xAC, 0x46, 0xA2, 0x5E, 0x1C, 0xE0, 0xC7, 0xB2, 0xF8, 0x20, 0x40, 0xC2, 0x27, 0xC8, 0xBE, 0x02, 0x7E, 0x96, 0x69, 0xE0, 0x04, 0xCB, 0x89, 0x0B, 0x42 }; #ifndef OPENSSL_NO_ECX static const unsigned char kExampleECXKey2DER[] = { 0x30, 0x2E, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65, 0x6e, 0x04, 0x22, 0x04, 0x20, 0xc8, 0xa9, 0xd5, 0xa9, 0x10, 0x91, 0xad, 0x85, 0x1c, 0x66, 0x8b, 0x07, 0x36, 0xc1, 0xc9, 0xa0, 0x29, 0x36, 0xc0, 0xd3, 0xad, 0x62, 0x67, 0x08, 0x58, 0x08, 0x80, 0x47, 0xba, 0x05, 0x74, 0x75 }; #endif #endif typedef struct APK_DATA_st { const unsigned char *kder; size_t size; int evptype; } APK_DATA; static APK_DATA keydata[] = { { kExampleRSAKeyDER, sizeof(kExampleRSAKeyDER), EVP_PKEY_RSA }, { kExampleRSAKeyPKCS8, sizeof(kExampleRSAKeyPKCS8), EVP_PKEY_RSA }, #ifndef OPENSSL_NO_EC #ifndef OPENSSL_NO_ECX { kExampleECXKey2DER, sizeof(kExampleECXKey2DER), EVP_PKEY_X25519 }, #endif { kExampleECKeyDER, sizeof(kExampleECKeyDER), EVP_PKEY_EC }, { kExampleECKey2DER, sizeof(kExampleECKey2DER), EVP_PKEY_EC }, #endif #ifndef OPENSSL_NO_DH { kExampleDHPrivateKeyDER, sizeof(kExampleDHPrivateKeyDER), EVP_PKEY_DH }, #endif }; static int pkey_has_private(EVP_PKEY *key, const char *privtag, int use_octstring) { if (use_octstring) { /* * Existence probe only: pass NULL buffer, ignore the returned size. * Avoids hard-coded fixed buffers that would cap at 64 bytes and * fail for PQC private keys (ML-DSA-44 is ~2.5 KB). */ return EVP_PKEY_get_octet_string_param(key, privtag, NULL, 0, NULL); } else { BIGNUM *bn = NULL; int ret = EVP_PKEY_get_bn_param(key, privtag, &bn); BN_free(bn); return ret; } } /* * Private-component tags we know about, in lookup order. Extend this * list if a new algorithm adopts a different convention; do_pkey_tofrom * _data_select() scans it on each key and only needs one match. */ static const char *const pkey_priv_tags[] = { OSSL_PKEY_PARAM_PRIV_KEY, /* DH, DSA, EC, ECX, SM2, ML-*, SLH-DSA */ OSSL_PKEY_PARAM_RSA_D, /* RSA, RSA-PSS */ }; static int do_pkey_tofrom_data_select(EVP_PKEY *key, const char *keytype) { int ret = 0; OSSL_PARAM *pub_params = NULL, *keypair_params = NULL; const OSSL_PARAM *priv = NULL; EVP_PKEY *fromkey = NULL, *fromkeypair = NULL; EVP_PKEY_CTX *fromctx = NULL; const char *privtag; int use_octstring; size_t t; /* * Export the full keypair first so we can discover the algorithm's * private-component tag from the emitted params rather than hand-coding * it per keytype. */ if (!TEST_int_eq(EVP_PKEY_todata(key, EVP_PKEY_KEYPAIR, &keypair_params), 1)) goto end; for (t = 0; t < OSSL_NELEM(pkey_priv_tags); t++) if ((priv = OSSL_PARAM_locate(keypair_params, pkey_priv_tags[t])) != NULL) break; if (priv == NULL) { TEST_error("%s: KEYPAIR todata() emitted no known private component", keytype); goto end; } privtag = priv->key; use_octstring = priv->data_type == OSSL_PARAM_OCTET_STRING; /* * Select only the public key via EVP_PKEY_todata(): the private tag we * just learned must be absent. */ if (!TEST_int_eq(EVP_PKEY_todata(key, EVP_PKEY_PUBLIC_KEY, &pub_params), 1) || !TEST_ptr_null(OSSL_PARAM_locate(pub_params, privtag))) goto end; /* * Round-trip: importing only the public selection from the full keypair * params must yield a key that has no private component. */ if (!TEST_ptr(fromctx = EVP_PKEY_CTX_new_from_name(mainctx, keytype, NULL)) || !TEST_int_eq(EVP_PKEY_fromdata_init(fromctx), 1) || !TEST_int_eq(EVP_PKEY_fromdata(fromctx, &fromkey, EVP_PKEY_PUBLIC_KEY, keypair_params), 1) || !TEST_false(pkey_has_private(fromkey, privtag, use_octstring))) goto end; /* Round-trip: a full keypair import must carry the private component. */ if (!TEST_int_eq(EVP_PKEY_fromdata(fromctx, &fromkeypair, EVP_PKEY_KEYPAIR, keypair_params), 1) || !TEST_true(pkey_has_private(fromkeypair, privtag, use_octstring))) goto end; ret = 1; end: EVP_PKEY_free(fromkeypair); EVP_PKEY_free(fromkey); EVP_PKEY_CTX_free(fromctx); OSSL_PARAM_free(keypair_params); OSSL_PARAM_free(pub_params); return ret; } /* * Pass every advertised gettable parameter through EVP_PKEY_get_params() * as a probe, twice: once with NULL data (size negotiation), then again * with storage allocated for the advertised type. Either fetch must * succeed as a whole. Pass 2 is what exercises the type-matching * branches in the per-type OSSL_PARAM setters: a NULL-data probe * succeeds before those branches are reached, which is why this check * would otherwise miss gettable-table / getter mismatches (an entry * advertised as OCTET_STRING whose getter populates it as a BIGNUM). * * The provider is free to leave individual entries unmodified, e.g. * private-key data on a public-only key; only whole-fetch failure * is treated as a regression. */ static int do_pkey_all_gettables_probe(EVP_PKEY *pkey, const char *keytype) { int ret = 0; const OSSL_PARAM *gettable; OSSL_PARAM *probe = NULL; void **storage = NULL; size_t n = 0, i; int need_refetch = 0; if (!TEST_ptr(gettable = EVP_PKEY_gettable_params(pkey))) goto err; while (gettable[n].key != NULL) n++; if (!TEST_ptr(probe = OPENSSL_zalloc((n + 1) * sizeof(*probe))) || (n > 0 && !TEST_ptr(storage = OPENSSL_zalloc(n * sizeof(*storage))))) goto err; for (i = 0; i < n; i++) { /* * Copy the advertised entry as a template, then reset the * data fields to the "probe" state. For OCTET_PTR / UTF8_PTR * the setter writes *data directly (no size negotiation), * so hand it a pointer slot up front. */ probe[i] = gettable[i]; probe[i].data = NULL; probe[i].data_size = 0; probe[i].return_size = OSSL_PARAM_UNMODIFIED; if (gettable[i].data_type == OSSL_PARAM_OCTET_PTR || gettable[i].data_type == OSSL_PARAM_UTF8_PTR) { if (!TEST_ptr(storage[i] = OPENSSL_zalloc(sizeof(void *)))) goto err; probe[i].data = storage[i]; } } probe[n] = OSSL_PARAM_construct_end(); /* Pass 1: NULL-data probe populates return_size for sized entries. */ if (!TEST_true(EVP_PKEY_get_params(pkey, probe))) { TEST_info("%s: Pass 1 (size probe) failed", keytype); goto err; } /* Allocate storage for each populated sized entry. */ for (i = 0; i < n; i++) { size_t need; if (!OSSL_PARAM_modified(&probe[i])) continue; switch (probe[i].data_type) { case OSSL_PARAM_INTEGER: case OSSL_PARAM_UNSIGNED_INTEGER: case OSSL_PARAM_OCTET_STRING: case OSSL_PARAM_REAL: need = probe[i].return_size; if (!TEST_ptr(storage[i] = OPENSSL_malloc(need == 0 ? 1 : need))) goto err; probe[i].data = storage[i]; probe[i].data_size = need; probe[i].return_size = OSSL_PARAM_UNMODIFIED; need_refetch = 1; break; case OSSL_PARAM_UTF8_STRING: /* Leave room for a trailing NUL beyond return_size. */ need = probe[i].return_size + 1; if (!TEST_ptr(storage[i] = OPENSSL_malloc(need))) goto err; probe[i].data = storage[i]; probe[i].data_size = need; probe[i].return_size = OSSL_PARAM_UNMODIFIED; need_refetch = 1; break; default: /* PTR types were populated in Pass 1; unknown types left as-is. */ break; } } /* * Pass 2: real fetch, with typed storage. This is what actually * catches advertised-vs-populated type mismatches: the per-type * OSSL_PARAM_set_*() setters only enforce data_type when data is * non-NULL. */ if (need_refetch && !TEST_true(EVP_PKEY_get_params(pkey, probe))) { TEST_info("%s: Pass 2 (real fetch) failed", keytype); goto err; } ret = 1; err: if (storage != NULL) for (i = 0; i < n; i++) OPENSSL_free(storage[i]); OPENSSL_free(storage); OPENSSL_free(probe); return ret; } /* * Composite check: exercise both the selection-mask semantics of * todata/fromdata and the advertised-vs-populated type consistency of * every gettable parameter on the given key. The individual helpers are * algorithm-agnostic, so this driver works for any asymmetric EVP_PKEY. * Each keytype-specific test function reduces to (a) key generation and * (b) invoking this driver. */ static int do_pkey_checks(EVP_PKEY *key, const char *keytype) { return do_pkey_tofrom_data_select(key, keytype) && do_pkey_all_gettables_probe(key, keytype); } #ifndef OPENSSL_NO_DH static int test_dh_paramgen(void) { int ret; OSSL_PARAM params[3]; EVP_PKEY *pkey = NULL; EVP_PKEY_CTX *gctx = NULL; unsigned int pbits = 512; /* minimum allowed for speed */ params[0] = OSSL_PARAM_construct_uint(OSSL_PKEY_PARAM_FFC_PBITS, &pbits); params[1] = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_FFC_TYPE, "generator", 0); params[2] = OSSL_PARAM_construct_end(); ret = TEST_ptr(gctx = EVP_PKEY_CTX_new_from_name(mainctx, "DH", NULL)) && TEST_int_gt(EVP_PKEY_paramgen_init(gctx), 0) && TEST_true(EVP_PKEY_CTX_set_params(gctx, params)) && TEST_true(EVP_PKEY_paramgen(gctx, &pkey)) && TEST_ptr(pkey); EVP_PKEY_CTX_free(gctx); gctx = NULL; ret = ret && TEST_ptr(gctx = EVP_PKEY_CTX_new_from_pkey(mainctx, pkey, NULL)) && TEST_int_eq(EVP_PKEY_param_check(gctx), 1) && TEST_int_eq(EVP_PKEY_param_check_quick(gctx), 1); EVP_PKEY_CTX_free(gctx); EVP_PKEY_free(pkey); return ret; } static int set_fromdata_string(EVP_PKEY_CTX *ctx, const char *name, char *value) { int ret; OSSL_PARAM params[2]; EVP_PKEY *pkey = NULL; if (EVP_PKEY_fromdata_init(ctx) != 1) return -1; params[0] = OSSL_PARAM_construct_utf8_string(name, value, 0); params[1] = OSSL_PARAM_construct_end(); ret = EVP_PKEY_fromdata(ctx, &pkey, EVP_PKEY_KEY_PARAMETERS, params); EVP_PKEY_free(pkey); return ret; } static int set_fromdata_uint(EVP_PKEY_CTX *ctx, const char *name) { int ret; unsigned int tmp = 0; OSSL_PARAM params[2]; EVP_PKEY *pkey = NULL; if (EVP_PKEY_fromdata_init(ctx) != 1) return -1; params[0] = OSSL_PARAM_construct_uint(name, &tmp); params[1] = OSSL_PARAM_construct_end(); ret = EVP_PKEY_fromdata(ctx, &pkey, EVP_PKEY_KEY_PARAMETERS, params); EVP_PKEY_free(pkey); return ret; } static int test_dh_paramfromdata(void) { EVP_PKEY_CTX *ctx = NULL; int ret = 0; /* Test failure paths for FFC - mainly due to setting the wrong param type */ ret = TEST_ptr(ctx = EVP_PKEY_CTX_new_from_name(mainctx, "DH", NULL)) && TEST_int_eq(set_fromdata_uint(ctx, OSSL_PKEY_PARAM_GROUP_NAME), 0) && TEST_int_eq(set_fromdata_string(ctx, OSSL_PKEY_PARAM_GROUP_NAME, "bad"), 0) && TEST_int_eq(set_fromdata_string(ctx, OSSL_PKEY_PARAM_FFC_P, "bad"), 0) && TEST_int_eq(set_fromdata_string(ctx, OSSL_PKEY_PARAM_FFC_GINDEX, "bad"), 0) && TEST_int_eq(set_fromdata_string(ctx, OSSL_PKEY_PARAM_FFC_PCOUNTER, "bad"), 0) && TEST_int_eq(set_fromdata_string(ctx, OSSL_PKEY_PARAM_FFC_COFACTOR, "bad"), 0) && TEST_int_eq(set_fromdata_string(ctx, OSSL_PKEY_PARAM_FFC_H, "bad"), 0) && TEST_int_eq(set_fromdata_uint(ctx, OSSL_PKEY_PARAM_FFC_SEED), 0) && TEST_int_eq(set_fromdata_string(ctx, OSSL_PKEY_PARAM_FFC_VALIDATE_PQ, "bad"), 0) && TEST_int_eq(set_fromdata_string(ctx, OSSL_PKEY_PARAM_FFC_VALIDATE_G, "bad"), 0) && TEST_int_eq(set_fromdata_string(ctx, OSSL_PKEY_PARAM_FFC_VALIDATE_LEGACY, "bad"), 0) && TEST_int_eq(set_fromdata_uint(ctx, OSSL_PKEY_PARAM_FFC_DIGEST), 0); EVP_PKEY_CTX_free(ctx); return ret; } #endif /* Test that calling EVP_PKEY_Q_keygen() for a non-standard keytype works as expected */ static int test_new_keytype(void) { int ret = 0; EVP_PKEY *key = NULL; OSSL_PROVIDER *tlsprov = NULL; EVP_PKEY_CTX *ctx = NULL; size_t outlen, secretlen, secretlen2; unsigned char *out = NULL, *secret = NULL, *secret2 = NULL; /* without tls-provider key should not be create-able */ if (!TEST_ptr_null(key = EVP_PKEY_Q_keygen(mainctx, NULL, "XOR"))) goto err; /* prepare & load tls-provider */ if (!TEST_true(OSSL_PROVIDER_add_builtin(mainctx, "tls-provider", tls_provider_init)) || !TEST_ptr(tlsprov = OSSL_PROVIDER_load(mainctx, "tls-provider"))) goto err; /* now try creating key again, should work this time */ if (!TEST_ptr(key = EVP_PKEY_Q_keygen(mainctx, NULL, "XOR"))) goto err; /* now do encaps/decaps to validate all is good */ if (!TEST_ptr(ctx = EVP_PKEY_CTX_new(key, NULL)) || !TEST_int_eq(EVP_PKEY_encapsulate_init(ctx, NULL), 1) || !TEST_int_eq(EVP_PKEY_encapsulate(ctx, NULL, &outlen, NULL, &secretlen), 1)) goto err; out = OPENSSL_malloc(outlen); secret = OPENSSL_malloc(secretlen); secret2 = OPENSSL_malloc(secretlen); if (out == NULL || secret == NULL || secret2 == NULL || !TEST_int_eq(EVP_PKEY_encapsulate(ctx, out, &outlen, secret, &secretlen), 1) || !TEST_int_eq(EVP_PKEY_decapsulate_init(ctx, NULL), 1) || !TEST_int_eq(EVP_PKEY_decapsulate(ctx, secret2, &secretlen2, out, outlen), 1) || !TEST_mem_eq(secret, secretlen, secret2, secretlen2)) goto err; ret = OSSL_PROVIDER_unload(tlsprov); err: OPENSSL_free(out); OPENSSL_free(secret); OPENSSL_free(secret2); EVP_PKEY_CTX_free(ctx); EVP_PKEY_free(key); return ret; } #ifndef OPENSSL_NO_EC static int test_ec_d2i_i2d_pubkey(void) { int ret = 0; FILE *fp = NULL; EVP_PKEY *key = NULL, *outkey = NULL; static const char *filename = "pubkey.der"; if (!TEST_ptr(fp = fopen(filename, "wb")) || !TEST_ptr(key = EVP_PKEY_Q_keygen(mainctx, NULL, "EC", "P-256")) || !TEST_true(i2d_PUBKEY_fp(fp, key)) || !TEST_int_eq(fclose(fp), 0)) goto err; fp = NULL; if (!TEST_ptr(fp = fopen(filename, "rb")) || !TEST_ptr(outkey = d2i_PUBKEY_ex_fp(fp, NULL, mainctx, NULL)) || !TEST_int_eq(EVP_PKEY_eq(key, outkey), 1)) goto err; ret = 1; err: EVP_PKEY_free(outkey); EVP_PKEY_free(key); fclose(fp); return ret; } #endif /* OPENSSL_NO_EC */ /* This is the equivalent of test_d2i_AutoPrivateKey in evp_extra_test */ static int test_d2i_AutoPrivateKey_ex(int i) { int ret = 0; const unsigned char *p; EVP_PKEY *pkey = NULL; const APK_DATA *ak = &keydata[i]; const unsigned char *input = ak->kder; size_t input_len = ak->size; int expected_id = ak->evptype; BIGNUM *p_bn = NULL; BIGNUM *g_bn = NULL; BIGNUM *priv_bn = NULL; p = input; if (!TEST_ptr(pkey = d2i_AutoPrivateKey_ex(NULL, &p, input_len, mainctx, NULL)) || !TEST_ptr_eq(p, input + input_len) || !TEST_int_eq(EVP_PKEY_get_id(pkey), expected_id)) goto done; if (ak->evptype == EVP_PKEY_RSA) { if (!TEST_true(EVP_PKEY_get_bn_param(pkey, OSSL_PKEY_PARAM_RSA_D, &priv_bn))) goto done; } else if (ak->evptype == EVP_PKEY_X25519) { unsigned char buffer[32]; size_t len; if (!TEST_true(EVP_PKEY_get_octet_string_param(pkey, OSSL_PKEY_PARAM_PRIV_KEY, buffer, sizeof(buffer), &len))) goto done; } else { if (!TEST_true(EVP_PKEY_get_bn_param(pkey, OSSL_PKEY_PARAM_PRIV_KEY, &priv_bn))) goto done; } if (ak->evptype == EVP_PKEY_DH) { if (!TEST_true(EVP_PKEY_get_bn_param(pkey, OSSL_PKEY_PARAM_FFC_P, &p_bn)) || !TEST_true(EVP_PKEY_get_bn_param(pkey, OSSL_PKEY_PARAM_FFC_G, &g_bn))) goto done; } ret = 1; done: BN_free(p_bn); BN_free(g_bn); BN_free(priv_bn); EVP_PKEY_free(pkey); return ret; } #ifndef OPENSSL_NO_DES static int test_pkcs8key_nid_bio(void) { int ret; const int nid = NID_pbe_WithSHA1And3_Key_TripleDES_CBC; static const char pwd[] = "PASSWORD"; EVP_PKEY *pkey = NULL, *pkey_dec = NULL; BIO *in = NULL, *enc_bio = NULL; char *enc_data = NULL; long enc_datalen = 0; OSSL_PROVIDER *provider = NULL; ret = TEST_ptr(provider = OSSL_PROVIDER_load(NULL, "default")) && TEST_ptr(enc_bio = BIO_new(BIO_s_mem())) && TEST_ptr(in = BIO_new_mem_buf(kExampleRSAKeyPKCS8, sizeof(kExampleRSAKeyPKCS8))) && TEST_ptr(pkey = d2i_PrivateKey_ex_bio(in, NULL, NULL, NULL)) && TEST_int_eq(i2d_PKCS8PrivateKey_nid_bio(enc_bio, pkey, nid, pwd, sizeof(pwd) - 1, NULL, NULL), 1) && TEST_int_gt(enc_datalen = BIO_get_mem_data(enc_bio, &enc_data), 0) && TEST_ptr(pkey_dec = d2i_PKCS8PrivateKey_bio(enc_bio, NULL, NULL, (void *)pwd)) && TEST_true(EVP_PKEY_eq(pkey, pkey_dec)); EVP_PKEY_free(pkey_dec); EVP_PKEY_free(pkey); BIO_free(in); BIO_free(enc_bio); OSSL_PROVIDER_unload(provider); return ret; } #endif /* OPENSSL_NO_DES */ static int test_alternative_default(void) { OSSL_LIB_CTX *oldctx; EVP_MD *sha256; int ok = 0; /* * setup_tests() loaded the "null" provider in the current default, so * we know this fetch should fail. */ if (!TEST_ptr_null(sha256 = EVP_MD_fetch(NULL, "SHA2-256", NULL))) goto err; /* * Now we switch to our main library context, and try again. Since no * providers are loaded in this one, it should fall back to the default. */ if (!TEST_ptr(oldctx = OSSL_LIB_CTX_set0_default(mainctx)) || !TEST_ptr(sha256 = EVP_MD_fetch(NULL, "SHA2-256", NULL))) goto err; EVP_MD_free(sha256); sha256 = NULL; /* * Switching back should give us our main library context back, and * fetching SHA2-256 should fail again. */ if (!TEST_ptr_eq(OSSL_LIB_CTX_set0_default(oldctx), mainctx) || !TEST_ptr_null(sha256 = EVP_MD_fetch(NULL, "SHA2-256", NULL))) goto err; ok = 1; err: EVP_MD_free(sha256); return ok; } static int test_provider_unload_effective(int testid) { EVP_MD *sha256 = NULL; OSSL_PROVIDER *provider = NULL; int ok = 0; if (!TEST_ptr(provider = OSSL_PROVIDER_load(NULL, "default")) || !TEST_ptr(sha256 = EVP_MD_fetch(NULL, "SHA2-256", NULL))) goto err; if (testid > 0) { OSSL_PROVIDER_unload(provider); provider = NULL; EVP_MD_free(sha256); sha256 = NULL; } else { EVP_MD_free(sha256); sha256 = NULL; OSSL_PROVIDER_unload(provider); provider = NULL; } /* * setup_tests() loaded the "null" provider in the current default, and * we unloaded it above after the load so we know this fetch should fail. */ if (!TEST_ptr_null(sha256 = EVP_MD_fetch(NULL, "SHA2-256", NULL))) goto err; ok = 1; err: EVP_MD_free(sha256); OSSL_PROVIDER_unload(provider); return ok; } static int test_d2i_PrivateKey_ex(int testid) { int ok = 0; OSSL_PROVIDER *provider = NULL; BIO *key_bio = NULL; EVP_PKEY *pkey = NULL; int id = (testid == 0) ? 0 : 2; if (!TEST_ptr(provider = OSSL_PROVIDER_load(NULL, "default"))) goto err; if (!TEST_ptr(key_bio = BIO_new_mem_buf(keydata[id].kder, keydata[id].size))) goto err; if (!TEST_ptr_null(pkey = PEM_read_bio_PrivateKey(key_bio, NULL, NULL, NULL))) goto err; ERR_clear_error(); if (!TEST_int_ge(BIO_seek(key_bio, 0), 0)) goto err; ok = TEST_ptr(pkey = d2i_PrivateKey_bio(key_bio, NULL)); TEST_int_eq(ERR_peek_error(), 0); test_openssl_errors(); err: EVP_PKEY_free(pkey); BIO_free(key_bio); OSSL_PROVIDER_unload(provider); return ok; } static int test_PEM_read_bio_negative(int testid) { int ok = 0; OSSL_PROVIDER *provider = NULL; BIO *key_bio = NULL; EVP_PKEY *pkey = NULL; if (!TEST_ptr(key_bio = BIO_new_mem_buf(keydata[testid].kder, keydata[testid].size))) goto err; ERR_clear_error(); if (!TEST_ptr_null(pkey = PEM_read_bio_PrivateKey(key_bio, NULL, NULL, NULL))) goto err; if (!TEST_int_ne(ERR_peek_error(), 0)) goto err; if (!TEST_ptr(provider = OSSL_PROVIDER_load(NULL, "default"))) goto err; if (!TEST_int_ge(BIO_seek(key_bio, 0), 0)) goto err; ERR_clear_error(); if (!TEST_ptr_null(pkey = PEM_read_bio_PrivateKey(key_bio, NULL, NULL, NULL))) goto err; if (!TEST_int_ne(ERR_peek_error(), 0)) goto err; ok = 1; err: test_openssl_errors(); EVP_PKEY_free(pkey); BIO_free(key_bio); OSSL_PROVIDER_unload(provider); return ok; } static int test_PEM_read_bio_negative_wrong_password(int testid) { int ok = 0; OSSL_PROVIDER *provider = OSSL_PROVIDER_load(NULL, "default"); EVP_PKEY *read_pkey = NULL; EVP_PKEY *write_pkey = EVP_RSA_gen(1024); BIO *key_bio = BIO_new(BIO_s_mem()); const UI_METHOD *undo_ui_method = NULL; const UI_METHOD *ui_method = NULL; if (testid > 0) ui_method = UI_null(); if (!TEST_ptr(provider)) goto err; if (!TEST_ptr(key_bio)) goto err; if (!TEST_ptr(write_pkey)) goto err; undo_ui_method = UI_get_default_method(); UI_set_default_method(ui_method); if (/* Output Encrypted private key in PEM form */ !TEST_true(PEM_write_bio_PrivateKey(key_bio, write_pkey, EVP_aes_256_cbc(), NULL, 0, NULL, "pass"))) goto err; ERR_clear_error(); read_pkey = PEM_read_bio_PrivateKey(key_bio, NULL, NULL, NULL); if (!TEST_ptr_null(read_pkey)) goto err; if (!TEST_int_eq(ERR_GET_REASON(ERR_get_error()), PEM_R_PROBLEMS_GETTING_PASSWORD)) goto err; ok = 1; err: test_openssl_errors(); EVP_PKEY_free(read_pkey); EVP_PKEY_free(write_pkey); BIO_free(key_bio); OSSL_PROVIDER_unload(provider); UI_set_default_method(undo_ui_method); return ok; } static int do_fromdata_key_is_equal(const OSSL_PARAM params[], const EVP_PKEY *expected, const char *type) { EVP_PKEY_CTX *ctx = NULL; EVP_PKEY *pkey = NULL; int ret; ret = TEST_ptr(ctx = EVP_PKEY_CTX_new_from_name(mainctx, type, NULL)) && TEST_int_eq(EVP_PKEY_fromdata_init(ctx), 1) && TEST_int_eq(EVP_PKEY_fromdata(ctx, &pkey, EVP_PKEY_KEYPAIR, (OSSL_PARAM *)params), 1) && TEST_true(EVP_PKEY_eq(pkey, expected)); EVP_PKEY_CTX_free(ctx); EVP_PKEY_free(pkey); return ret; } #ifndef OPENSSL_NO_DSA /* * This data was generated using: * > openssl genpkey \ * -genparam -algorithm DSA -pkeyopt type:fips186_4 -text \ * -pkeyopt gindex:5 -out dsa_param.pem * > openssl genpkey \ * -paramfile dsa_param.pem -pkeyopt type:fips186_4 -out dsa_priv.pem */ static const unsigned char dsa_key[] = { 0x30, 0x82, 0x03, 0x4e, 0x02, 0x01, 0x00, 0x02, 0x82, 0x01, 0x01, 0x00, 0xda, 0xb3, 0x46, 0x4d, 0x54, 0x57, 0xc7, 0xb4, 0x61, 0xa0, 0x6f, 0x66, 0x17, 0xda, 0xeb, 0x90, 0xf0, 0xa3, 0xd1, 0x29, 0xc9, 0x5f, 0xf2, 0x21, 0x3d, 0x85, 0xa3, 0x4a, 0xf0, 0xf8, 0x36, 0x39, 0x1b, 0xe3, 0xee, 0x37, 0x70, 0x06, 0x9b, 0xe8, 0xe3, 0x0a, 0xd2, 0xf1, 0xf6, 0xc4, 0x42, 0x23, 0x1f, 0x74, 0x78, 0xc2, 0x16, 0xf5, 0xce, 0xd6, 0xab, 0xa0, 0xc6, 0xe8, 0x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 0x70, 0xce, 0x0f, 0x8c, 0xff, 0x8d, 0x21, 0xf9, 0x19, 0x68, 0x4d, 0x32, 0x59, 0x78, 0x42, 0x1d, 0x0c, 0xc5, 0x1a, 0xcb, 0x28, 0xe2, 0xc1, 0x1a, 0x35, 0xf1, 0x42, 0x0a, 0x19, 0x39, 0xfa, 0x83, 0xd1, 0xb4, 0xaa, 0x69, 0x0f, 0xc2, 0x8e, 0xf9, 0x59, 0x2c, 0xee, 0x11, 0xfc, 0x3e, 0x4b, 0x44, 0xfb, 0x9a, 0x32, 0xc8, 0x78, 0x23, 0x56, 0x85, 0x49, 0x21, 0x43, 0x12, 0x79, 0xbd, 0xa0, 0x70, 0x47, 0x2f, 0xae, 0xb6, 0xd7, 0x6c, 0xc6, 0x07, 0x76, 0xa9, 0x8a, 0xa2, 0x16, 0x02, 0x89, 0x1f, 0x1a, 0xd1, 0xa2, 0x96, 0x56, 0xd1, 0x1f, 0x10, 0xe1, 0xe5, 0x9f, 0x3f, 0xdd, 0x09, 0x0c, 0x40, 0x90, 0x71, 0xef, 0x14, 0x41, 0x02, 0x82, 0x3a, 0x6b, 0xe1, 0xf8, 0x2c, 0x5d, 0xbe, 0xfd, 0x1b }; static const unsigned char dsa_q[] = { 0x00, 0xe0, 0x20, 0xe0, 0x7c, 0x02, 0x16, 0xa7, 0x6c, 0x6a, 0x19, 0xba, 0xd5, 0x83, 0x73, 0xf3, 0x7d, 0x31, 0xef, 0xa7, 0xe1, 0x5d, 0x5b, 0x7f, 0xf3, 0xfc, 0xda, 0x84, 0x31 }; static const unsigned char dsa_g[] = { 0x00, 0x83, 0xdb, 0xa1, 0xbc, 0x3e, 0xc7, 0x29, 0xa5, 0x6a, 0x5c, 0x2c, 0xe8, 0x7a, 0x8c, 0x7e, 0xe8, 0xb8, 0x3e, 0x13, 0x47, 0xcd, 0x36, 0x7e, 0x79, 0x30, 0x7a, 0x28, 0x03, 0xd3, 0xd4, 0xd2, 0xe3, 0xee, 0x3b, 0x46, 0x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 0x54, 0xb3, 0x05, 0x1c, 0x23, 0x2b, 0x35, 0x2e, 0x19, 0x48, 0x59, 0x0e, 0x58, 0xa8, 0x01, 0x56, 0xfb, 0x78, 0x90, 0xba, 0x08, 0x77, 0x94, 0x45, 0x05, 0x13, 0xc7, 0x6b, 0x96, 0xd2, 0xa3, 0xa6, 0x01, 0x9f, 0x34 }; static const unsigned char dsa_priv[] = { 0x2f, 0x68, 0x44, 0xcb, 0xfb, 0x6b, 0xcb, 0x8d, 0x02, 0x49, 0x7c, 0xee, 0xd2, 0xa6, 0xd3, 0x43, 0xb8, 0xa4, 0x09, 0xb7, 0xc1, 0xd4, 0x4b, 0xc3, 0x66, 0xa7, 0xe0, 0x21 }; static const unsigned char dsa_pub[] = { 0x16, 0x1a, 0xb4, 0x6d, 0x9f, 0x16, 0x6c, 0xcc, 0x91, 0x66, 0xfe, 0x30, 0xeb, 0x8e, 0x44, 0xba, 0x2b, 0x7a, 0xc9, 0xa8, 0x95, 0xf2, 0xa6, 0x38, 0xd8, 0xaf, 0x3e, 0x91, 0x68, 0xe8, 0x52, 0xf3, 0x97, 0x37, 0x70, 0xf2, 0x47, 0xa3, 0xf4, 0x62, 0x26, 0xf5, 0x3b, 0x71, 0x52, 0x50, 0x15, 0x9c, 0x6d, 0xa6, 0x6d, 0x92, 0x4c, 0x48, 0x76, 0x31, 0x54, 0x48, 0xa5, 0x99, 0x7a, 0xd4, 0x61, 0xf7, 0x21, 0x44, 0xe7, 0xd8, 0x82, 0xc3, 0x50, 0xd3, 0xd9, 0xd4, 0x66, 0x20, 0xab, 0x70, 0x4c, 0x97, 0x9b, 0x8d, 0xac, 0x1f, 0x78, 0x27, 0x1e, 0x47, 0xf8, 0x3b, 0xd1, 0x55, 0x73, 0xf3, 0xb4, 0x8e, 0x6d, 0x45, 0x40, 0x54, 0xc6, 0xd8, 0x95, 0x15, 0x27, 0xb7, 0x5f, 0x65, 0xaa, 0xcb, 0x24, 0xc9, 0x49, 0x87, 0x32, 0xad, 0xcb, 0xf8, 0x35, 0x63, 0x56, 0x72, 0x7c, 0x4e, 0x6c, 0xad, 0x5f, 0x26, 0x8c, 0xd2, 0x80, 0x41, 0xaf, 0x88, 0x23, 0x20, 0x03, 0xa4, 0xd5, 0x3c, 0x53, 0x54, 0xb0, 0x3d, 0xed, 0x0e, 0x9e, 0x53, 0x0a, 0x63, 0x5f, 0xfd, 0x28, 0x57, 0x09, 0x07, 0x73, 0xf4, 0x0c, 0xd4, 0x71, 0x5d, 0x6b, 0xa0, 0xd7, 0x86, 0x99, 0x29, 0x9b, 0xca, 0xfb, 0xcc, 0xd6, 0x2f, 0xfe, 0xbe, 0x94, 0xef, 0x1a, 0x0e, 0x55, 0x84, 0xa7, 0xaf, 0x7b, 0xfa, 0xed, 0x77, 0x61, 0x28, 0x22, 0xee, 0x6b, 0x11, 0xdd, 0xb0, 0x17, 0x1e, 0x06, 0xe4, 0x29, 0x4c, 0xc2, 0x3f, 0xd6, 0x75, 0xb6, 0x08, 0x04, 0x55, 0x13, 0x48, 0x4f, 0x44, 0xea, 0x8d, 0xaf, 0xcb, 0xac, 0x22, 0xc4, 0x6a, 0xb3, 0x86, 0xe5, 0x47, 0xa9, 0xb5, 0x72, 0x17, 0x23, 0x11, 0x81, 0x7f, 0x00, 0x00, 0x67, 0x5c, 0xf4, 0x58, 0xcc, 0xe2, 0x46, 0xce, 0xf5, 0x6d, 0xd8, 0x18, 0x91, 0xc4, 0x20, 0xbf, 0x07, 0x48, 0x45, 0xfd }; #endif #ifndef OPENSSL_NO_DSA static int do_check_params(OSSL_PARAM key_params[], int expected) { EVP_PKEY_CTX *gen_ctx = NULL, *check_ctx = NULL; EVP_PKEY *pkey = NULL; int ret; ret = TEST_ptr(gen_ctx = EVP_PKEY_CTX_new_from_name(mainctx, "DSA", NULL)) && TEST_int_eq(EVP_PKEY_fromdata_init(gen_ctx), 1) && TEST_int_eq(EVP_PKEY_fromdata(gen_ctx, &pkey, EVP_PKEY_KEYPAIR, key_params), 1) && TEST_ptr(check_ctx = EVP_PKEY_CTX_new_from_pkey(mainctx, pkey, NULL)) && TEST_int_eq(EVP_PKEY_param_check(check_ctx), expected); EVP_PKEY_CTX_free(check_ctx); EVP_PKEY_CTX_free(gen_ctx); EVP_PKEY_free(pkey); return ret; } static int do_check_bn(OSSL_PARAM params[], const char *key, const unsigned char *expected, size_t expected_len) { OSSL_PARAM *p; BIGNUM *bn = NULL; unsigned char buffer[256 + 1]; int ret, len; ret = TEST_ptr(p = OSSL_PARAM_locate(params, key)) && TEST_true(OSSL_PARAM_get_BN(p, &bn)) && TEST_int_gt(len = BN_bn2binpad(bn, buffer, expected_len), 0) && TEST_mem_eq(expected, expected_len, buffer, len); BN_free(bn); return ret; } static int do_check_int(OSSL_PARAM params[], const char *key, int expected) { OSSL_PARAM *p; int val = 0; return TEST_ptr(p = OSSL_PARAM_locate(params, key)) && TEST_true(OSSL_PARAM_get_int(p, &val)) && TEST_int_eq(val, expected); } static int test_dsa_todata(void) { EVP_PKEY *pkey = NULL; OSSL_PARAM *to_params = NULL, *all_params = NULL; OSSL_PARAM gen_params[4]; int ret = 0; const unsigned char *pkeydata = dsa_key; unsigned char dsa_seed[] = { 0xbc, 0x8a, 0x81, 0x64, 0x9e, 0x9d, 0x63, 0xa7, 0xa3, 0x5d, 0x87, 0xdd, 0x32, 0xf3, 0xc1, 0x9f, 0x18, 0x22, 0xeb, 0x73, 0x63, 0xad, 0x5e, 0x7b, 0x90, 0xc1, 0xe3, 0xe0 }; int dsa_pcounter = 319; int dsa_gindex = 5; gen_params[0] = OSSL_PARAM_construct_octet_string(OSSL_PKEY_PARAM_FFC_SEED, (void *)dsa_seed, sizeof(dsa_seed)); gen_params[1] = OSSL_PARAM_construct_int(OSSL_PKEY_PARAM_FFC_GINDEX, &dsa_gindex); gen_params[2] = OSSL_PARAM_construct_int(OSSL_PKEY_PARAM_FFC_PCOUNTER, &dsa_pcounter); gen_params[3] = OSSL_PARAM_construct_end(); if (!TEST_ptr(pkey = d2i_AutoPrivateKey_ex(NULL, &pkeydata, sizeof(dsa_key), mainctx, NULL)) || !TEST_int_eq(EVP_PKEY_todata(pkey, EVP_PKEY_KEYPAIR, &to_params), 1) || !do_check_bn(to_params, OSSL_PKEY_PARAM_FFC_P, dsa_p, sizeof(dsa_p)) || !do_check_bn(to_params, OSSL_PKEY_PARAM_FFC_Q, dsa_q, sizeof(dsa_q)) || !do_check_bn(to_params, OSSL_PKEY_PARAM_FFC_G, dsa_g, sizeof(dsa_g)) || !do_check_bn(to_params, OSSL_PKEY_PARAM_PUB_KEY, dsa_pub, sizeof(dsa_pub)) || !do_check_bn(to_params, OSSL_PKEY_PARAM_PRIV_KEY, dsa_priv, sizeof(dsa_priv)) || !do_check_int(to_params, OSSL_PKEY_PARAM_FFC_GINDEX, -1) || !do_check_int(to_params, OSSL_PKEY_PARAM_FFC_PCOUNTER, -1) || !do_check_int(to_params, OSSL_PKEY_PARAM_FFC_H, 0) || !do_check_int(to_params, OSSL_PKEY_PARAM_FFC_VALIDATE_PQ, 1) || !do_check_int(to_params, OSSL_PKEY_PARAM_FFC_VALIDATE_G, 1) || !do_check_int(to_params, OSSL_PKEY_PARAM_FFC_VALIDATE_LEGACY, 0) || !TEST_ptr_null(OSSL_PARAM_locate(to_params, OSSL_PKEY_PARAM_FFC_SEED))) goto err; if (!do_fromdata_key_is_equal(to_params, pkey, "DSA")) goto err; if (!TEST_ptr(all_params = OSSL_PARAM_merge(to_params, gen_params)) || !do_check_params(all_params, 1)) goto err; gen_params[1] = OSSL_PARAM_construct_int(OSSL_PKEY_PARAM_FFC_GINDEX, &dsa_gindex); gen_params[2] = OSSL_PARAM_construct_int(OSSL_PKEY_PARAM_FFC_PCOUNTER, &dsa_pcounter); /* * Check that modifying the shallow copy values used in OSSL_PARAM_merge() * results in an invalid key. This also verifies that the fips186-4 * validation code is running. */ dsa_gindex++; if (!do_check_params(all_params, 0)) goto err; dsa_gindex--; dsa_pcounter++; if (!do_check_params(all_params, 0)) goto err; dsa_pcounter--; dsa_seed[0] = 0xb0; if (!do_check_params(all_params, 0)) goto err; ret = 1; err: EVP_PKEY_free(pkey); OSSL_PARAM_free(all_params); OSSL_PARAM_free(to_params); return ret; } /* * Test that OSSL_PKEY_PARAM_FFC_DIGEST_PROPS is set properly when using fromdata * This test: * checks for failure when the property query is bad (tstid == 0) * checks for success when the property query is valid (tstid == 1) */ static int test_dsa_fromdata_digest_prop(int tstid) { EVP_PKEY_CTX *ctx = NULL, *gctx = NULL; EVP_PKEY *pkey = NULL, *pkey2 = NULL; OSSL_PARAM params[4], *p = params; int ret = 0; int expected = (tstid == 0 ? 0 : 1); unsigned int pbits = 512; /* minimum allowed for speed */ *p++ = OSSL_PARAM_construct_uint(OSSL_PKEY_PARAM_FFC_PBITS, &pbits); *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_FFC_DIGEST, "SHA512", 0); /* Setting a bad prop query here should fail during paramgen - when it tries to do a fetch */ *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_FFC_DIGEST_PROPS, tstid == 0 ? "provider=unknown" : "provider=default", 0); *p++ = OSSL_PARAM_construct_end(); if (!TEST_ptr(ctx = EVP_PKEY_CTX_new_from_name(mainctx, "DSA", NULL)) || !TEST_int_eq(EVP_PKEY_fromdata_init(ctx), 1) || !TEST_int_eq(EVP_PKEY_fromdata(ctx, &pkey, EVP_PKEY_KEY_PARAMETERS, params), 1)) goto err; if (!TEST_ptr(gctx = EVP_PKEY_CTX_new_from_pkey(mainctx, pkey, NULL)) || !TEST_int_eq(EVP_PKEY_paramgen_init(gctx), 1) || !TEST_int_eq(EVP_PKEY_paramgen(gctx, &pkey2), expected)) goto err; ret = 1; err: EVP_PKEY_free(pkey2); EVP_PKEY_free(pkey); EVP_PKEY_CTX_free(ctx); EVP_PKEY_CTX_free(gctx); return ret; } #endif /* OPENSSL_NO_DSA */ static int test_pkey_todata_null(void) { OSSL_PARAM *params = NULL; EVP_PKEY *pkey = NULL; int ret = 0; const unsigned char *pdata = keydata[0].kder; ret = TEST_ptr(pkey = d2i_AutoPrivateKey_ex(NULL, &pdata, keydata[0].size, mainctx, NULL)) && TEST_int_eq(EVP_PKEY_todata(NULL, EVP_PKEY_KEYPAIR, ¶ms), 0) && TEST_int_eq(EVP_PKEY_todata(pkey, EVP_PKEY_KEYPAIR, NULL), 0); EVP_PKEY_free(pkey); return ret; } static OSSL_CALLBACK test_pkey_export_cb; static int test_pkey_export_cb(const OSSL_PARAM params[], void *arg) { if (arg == NULL) return 0; return do_fromdata_key_is_equal(params, (EVP_PKEY *)arg, "RSA"); } static int test_pkey_export_null(void) { EVP_PKEY *pkey = NULL; int ret = 0; const unsigned char *pdata = keydata[0].kder; ret = TEST_ptr(pkey = d2i_AutoPrivateKey_ex(NULL, &pdata, keydata[0].size, mainctx, NULL)) && TEST_int_eq(EVP_PKEY_export(NULL, EVP_PKEY_KEYPAIR, test_pkey_export_cb, NULL), 0) && TEST_int_eq(EVP_PKEY_export(pkey, EVP_PKEY_KEYPAIR, NULL, NULL), 0); EVP_PKEY_free(pkey); return ret; } static int test_pkey_export(void) { EVP_PKEY *pkey = NULL; #ifndef OPENSSL_NO_DEPRECATED_3_0 RSA *rsa = NULL; #endif int ret = 1; const unsigned char *pdata = keydata[0].kder; int pdata_len = keydata[0].size; if (!TEST_ptr(pkey = d2i_AutoPrivateKey_ex(NULL, &pdata, pdata_len, mainctx, NULL)) || !TEST_true(EVP_PKEY_export(pkey, EVP_PKEY_KEYPAIR, test_pkey_export_cb, pkey)) || !TEST_false(EVP_PKEY_export(pkey, EVP_PKEY_KEYPAIR, test_pkey_export_cb, NULL))) ret = 0; EVP_PKEY_free(pkey); #ifndef OPENSSL_NO_DEPRECATED_3_0 /* Now, try with a legacy key */ pdata = keydata[0].kder; pdata_len = keydata[0].size; if (!TEST_ptr(rsa = d2i_RSAPrivateKey(NULL, &pdata, pdata_len)) || !TEST_ptr(pkey = EVP_PKEY_new()) || !TEST_true(EVP_PKEY_assign_RSA(pkey, rsa)) || !TEST_true(EVP_PKEY_export(pkey, EVP_PKEY_KEYPAIR, test_pkey_export_cb, pkey)) || !TEST_false(EVP_PKEY_export(pkey, EVP_PKEY_KEYPAIR, test_pkey_export_cb, NULL))) ret = 0; EVP_PKEY_free(pkey); #endif return ret; } static int test_rsa_pss_sign(void) { EVP_PKEY *pkey = NULL; EVP_PKEY_CTX *pctx = NULL; int ret = 0; const unsigned char *pdata = keydata[0].kder; const char *mdname = "SHA2-256"; OSSL_PARAM sig_params[3]; unsigned char mdbuf[256 / 8] = { 0 }; int padding = RSA_PKCS1_PSS_PADDING; unsigned char *sig = NULL; size_t sig_len = 0; sig_params[0] = OSSL_PARAM_construct_int(OSSL_PKEY_PARAM_PAD_MODE, &padding); sig_params[1] = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST, (char *)mdname, 0); sig_params[2] = OSSL_PARAM_construct_end(); ret = TEST_ptr(pkey = d2i_AutoPrivateKey_ex(NULL, &pdata, keydata[0].size, mainctx, NULL)) && TEST_ptr(pctx = EVP_PKEY_CTX_new_from_pkey(mainctx, pkey, NULL)) && TEST_int_gt(EVP_PKEY_sign_init_ex(pctx, sig_params), 0) && TEST_int_gt(EVP_PKEY_sign(pctx, NULL, &sig_len, mdbuf, sizeof(mdbuf)), 0) && TEST_int_gt(sig_len, 0) && TEST_ptr(sig = OPENSSL_malloc(sig_len)) && TEST_int_gt(EVP_PKEY_sign(pctx, sig, &sig_len, mdbuf, sizeof(mdbuf)), 0); EVP_PKEY_CTX_free(pctx); OPENSSL_free(sig); EVP_PKEY_free(pkey); return ret; } static int test_evp_md_ctx_dup(void) { EVP_MD_CTX *mdctx; EVP_MD_CTX *copyctx = NULL; int ret; /* test copying freshly initialized context */ ret = TEST_ptr(mdctx = EVP_MD_CTX_new()) && TEST_ptr(copyctx = EVP_MD_CTX_dup(mdctx)); EVP_MD_CTX_free(mdctx); EVP_MD_CTX_free(copyctx); return ret; } static int test_evp_md_ctx_copy(void) { EVP_MD_CTX *mdctx = NULL; EVP_MD_CTX *copyctx = NULL; int ret; /* test copying freshly initialized context */ ret = TEST_ptr(mdctx = EVP_MD_CTX_new()) && TEST_ptr(copyctx = EVP_MD_CTX_new()) && TEST_true(EVP_MD_CTX_copy_ex(copyctx, mdctx)); EVP_MD_CTX_free(mdctx); EVP_MD_CTX_free(copyctx); return ret; } static int test_evp_md_ctx_copy2(void) { int ret = 0; EVP_MD *md = NULL; OSSL_LIB_CTX *ctx = NULL; EVP_MD_CTX *inctx = NULL, *outctx = NULL; void *origin_algctx = NULL; if (!TEST_ptr(ctx = OSSL_LIB_CTX_new()) || !TEST_ptr(md = EVP_MD_fetch(ctx, "sha256", NULL))) goto end; inctx = EVP_MD_CTX_new(); outctx = EVP_MD_CTX_new(); if (!TEST_ptr(inctx) || !TEST_ptr(outctx)) goto end; /* init inctx and outctx, now the contexts are from same providers */ if (!TEST_true(EVP_DigestInit_ex2(inctx, md, NULL))) goto end; if (!TEST_true(EVP_DigestInit_ex2(outctx, md, NULL))) goto end; /* * Test the EVP_MD_CTX_copy_ex function. After copying, * outctx->algctx should be the same as the original. */ origin_algctx = outctx->algctx; ret = TEST_true(EVP_MD_CTX_copy_ex(outctx, inctx)) && TEST_true(outctx->algctx == origin_algctx); end: EVP_MD_free(md); EVP_MD_CTX_free(inctx); EVP_MD_CTX_free(outctx); OSSL_LIB_CTX_free(ctx); return ret; } #if !defined OPENSSL_NO_DES && !defined OPENSSL_NO_MD5 static int test_evp_pbe_alg_add(void) { int ret = 0; int cipher_nid = 0, md_nid = 0; EVP_PBE_KEYGEN_EX *keygen_ex = NULL; EVP_PBE_KEYGEN *keygen = NULL; if (!TEST_true(EVP_PBE_alg_add(NID_pbeWithMD5AndDES_CBC, EVP_des_cbc(), EVP_md5(), PKCS5_PBE_keyivgen))) goto err; if (!TEST_true(EVP_PBE_find_ex(EVP_PBE_TYPE_OUTER, NID_pbeWithMD5AndDES_CBC, &cipher_nid, &md_nid, &keygen, &keygen_ex))) goto err; if (!TEST_true(keygen != NULL)) goto err; if (!TEST_true(keygen_ex == NULL)) goto err; ret = 1; err: return ret; } #endif /* * Currently, EVP__fetch doesn't support * colon separated alternative names for lookup * so add a test here to ensure that when one is provided * libcrypto returns an error */ static int evp_test_name_parsing(void) { EVP_MD *md; if (!TEST_ptr_null(md = EVP_MD_fetch(mainctx, "SHA256:BogusName", NULL))) { EVP_MD_free(md); return 0; } return 1; } /* * Table-driven driver for do_pkey_checks(). Each entry names the * algorithm and supplies either (a) a prefabricated DER blob, loaded * via d2i_AutoPrivateKey_ex(), or (b) an optional OSSL_PARAM array * passed to EVP_PKEY_CTX_set_params() before EVP_PKEY_generate(). * The downstream checks are algorithm-agnostic; only the per-algorithm * inputs live in this table. */ typedef struct pkey_test_spec_st { const char *name; /* algorithm name for keygen or logging */ const unsigned char *der; /* prefab private key DER, or NULL */ uint32_t der_len; /* size of the DER blob when set */ const OSSL_PARAM *params; /* keygen params, or NULL for defaults */ } PKEY_TEST_SPEC; static const PKEY_TEST_SPEC pkey_test_specs[] = { #ifndef OPENSSL_NO_DH { "DHX", NULL, 0, dhx_keygen_params }, #endif #ifndef OPENSSL_NO_DSA { "DSA", dsa_key, sizeof(dsa_key), NULL }, #endif { "RSA", kExampleRSAKeyDER, sizeof(kExampleRSAKeyDER), NULL }, { "RSA-PSS", kExampleRSAPSSKeyDER, sizeof(kExampleRSAPSSKeyDER), NULL }, #ifndef OPENSSL_NO_EC { "EC", NULL, 0, ec_keygen_params }, #ifndef OPENSSL_NO_ECX { "X25519", NULL, 0, NULL }, { "ED25519", NULL, 0, NULL }, #endif #ifndef OPENSSL_NO_SM2 { "SM2", NULL, 0, NULL }, #endif #endif #ifndef OPENSSL_NO_ML_KEM { "ML-KEM-512", NULL, 0, NULL }, #endif #ifndef OPENSSL_NO_ML_DSA { "ML-DSA-44", NULL, 0, NULL }, #endif #ifndef OPENSSL_NO_SLH_DSA { "SLH-DSA-SHA2-128f", NULL, 0, NULL }, #endif }; static EVP_PKEY *pkey_from_spec(OSSL_LIB_CTX *libctx, const PKEY_TEST_SPEC *spec) { EVP_PKEY *key = NULL; EVP_PKEY_CTX *gctx = NULL; if (spec->der != NULL) { const unsigned char *p = spec->der; return d2i_AutoPrivateKey_ex(NULL, &p, spec->der_len, libctx, NULL); } if ((gctx = EVP_PKEY_CTX_new_from_name(libctx, spec->name, NULL)) == NULL || EVP_PKEY_keygen_init(gctx) <= 0 || (spec->params != NULL && EVP_PKEY_CTX_set_params(gctx, spec->params) <= 0) || EVP_PKEY_generate(gctx, &key) <= 0) { EVP_PKEY_free(key); key = NULL; } EVP_PKEY_CTX_free(gctx); return key; } static int test_pkey_by_spec(int i) { EVP_PKEY *key = pkey_from_spec(mainctx, &pkey_test_specs[i]); int ret = TEST_ptr(key) && do_pkey_checks(key, pkey_test_specs[i].name); EVP_PKEY_free(key); return ret; } #if !defined(OPENSSL_NO_DH) && !defined(OPENSSL_NO_DEPRECATED_3_0) /* * DHX keygen with group "ffdhe2048" reduces the private exponent modulo * the sub-group order, so the exposed BN_num_bits() should not exceed * the sub-group size. This tail check is DHX-specific and therefore * does not fit the generic spec-table driver. */ static int test_dh_priv_bits(void) { static const PKEY_TEST_SPEC dhx_spec = { "DHX", NULL, 0, dhx_keygen_params }; int ret; EVP_PKEY *key; const DH *dhkey; const BIGNUM *privkey; if (!TEST_ptr(key = pkey_from_spec(mainctx, &dhx_spec))) return 0; ret = TEST_ptr(dhkey = EVP_PKEY_get0_DH(key)) && TEST_ptr(privkey = DH_get0_priv_key(dhkey)) && TEST_int_le(BN_num_bits(privkey), 225); EVP_PKEY_free(key); return ret; } #endif int setup_tests(void) { if (!test_get_libctx(&mainctx, &nullprov, NULL, NULL, NULL)) { OSSL_LIB_CTX_free(mainctx); mainctx = NULL; return 0; } ADD_TEST(evp_test_name_parsing); ADD_TEST(test_alternative_default); ADD_ALL_TESTS(test_d2i_AutoPrivateKey_ex, OSSL_NELEM(keydata)); ADD_TEST(test_new_keytype); #ifndef OPENSSL_NO_EC ADD_ALL_TESTS(test_d2i_PrivateKey_ex, 2); ADD_TEST(test_ec_d2i_i2d_pubkey); #else ADD_ALL_TESTS(test_d2i_PrivateKey_ex, 1); #endif #ifndef OPENSSL_NO_DSA ADD_TEST(test_dsa_todata); ADD_ALL_TESTS(test_dsa_fromdata_digest_prop, 2); #endif #ifndef OPENSSL_NO_DH ADD_TEST(test_dh_paramgen); ADD_TEST(test_dh_paramfromdata); #ifndef OPENSSL_NO_DEPRECATED_3_0 ADD_TEST(test_dh_priv_bits); #endif #endif ADD_ALL_TESTS(test_pkey_by_spec, OSSL_NELEM(pkey_test_specs)); ADD_TEST(test_pkey_todata_null); ADD_TEST(test_pkey_export_null); ADD_TEST(test_pkey_export); #ifndef OPENSSL_NO_DES ADD_TEST(test_pkcs8key_nid_bio); #endif ADD_ALL_TESTS(test_PEM_read_bio_negative, OSSL_NELEM(keydata)); ADD_ALL_TESTS(test_PEM_read_bio_negative_wrong_password, 2); ADD_TEST(test_rsa_pss_sign); ADD_TEST(test_evp_md_ctx_dup); ADD_TEST(test_evp_md_ctx_copy); ADD_TEST(test_evp_md_ctx_copy2); ADD_ALL_TESTS(test_provider_unload_effective, 2); #if !defined OPENSSL_NO_DES && !defined OPENSSL_NO_MD5 ADD_TEST(test_evp_pbe_alg_add); #endif return 1; } void cleanup_tests(void) { OSSL_LIB_CTX_free(mainctx); OSSL_PROVIDER_unload(nullprov); }