From: Shane Lontis Date: Thu, 8 Apr 2021 10:05:14 +0000 (+1000) Subject: Add EVP_PKEY_todata() and EVP_PKEY_export() functions. X-Git-Tag: openssl-3.0.0-alpha15~90 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a732a4c329144f0b4c60372d9b7106c6b88ddd9f;p=thirdparty%2Fopenssl.git Add EVP_PKEY_todata() and EVP_PKEY_export() functions. Reviewed-by: Matt Caswell Reviewed-by: Tomas Mraz (Merged from https://github.com/openssl/openssl/pull/14800) --- diff --git a/crypto/evp/keymgmt_lib.c b/crypto/evp/keymgmt_lib.c index 80aea65e881..f196bc4d885 100644 --- a/crypto/evp/keymgmt_lib.c +++ b/crypto/evp/keymgmt_lib.c @@ -80,6 +80,8 @@ EVP_PKEY *evp_keymgmt_util_make_pkey(EVP_KEYMGMT *keymgmt, void *keydata) int evp_keymgmt_util_export(const EVP_PKEY *pk, int selection, OSSL_CALLBACK *export_cb, void *export_cbarg) { + if (pk == NULL || export_cb == NULL) + return 0; return evp_keymgmt_export(pk->keymgmt, pk->keydata, selection, export_cb, export_cbarg); } diff --git a/crypto/evp/pmeth_gn.c b/crypto/evp/pmeth_gn.c index 7383fbe072c..e184db26a02 100644 --- a/crypto/evp/pmeth_gn.c +++ b/crypto/evp/pmeth_gn.c @@ -395,3 +395,26 @@ const OSSL_PARAM *EVP_PKEY_fromdata_settable(EVP_PKEY_CTX *ctx, int selection) return evp_keymgmt_import_types(ctx->keymgmt, selection); return NULL; } + +static OSSL_CALLBACK ossl_pkey_todata_cb; + +static int ossl_pkey_todata_cb(const OSSL_PARAM params[], void *arg) +{ + OSSL_PARAM **ret = arg; + + *ret = OSSL_PARAM_dup(params); + return 1; +} + +int EVP_PKEY_todata(const EVP_PKEY *pkey, int selection, OSSL_PARAM **params) +{ + if (params == NULL) + return 0; + return EVP_PKEY_export(pkey, selection, ossl_pkey_todata_cb, params); +} + +int EVP_PKEY_export(const EVP_PKEY *pkey, int selection, + OSSL_CALLBACK *export_cb, void *export_cbarg) +{ + return evp_keymgmt_util_export(pkey, selection, export_cb, export_cbarg); +} diff --git a/doc/build.info b/doc/build.info index 899a142f525..d9c5d8d4fca 100644 --- a/doc/build.info +++ b/doc/build.info @@ -1266,6 +1266,10 @@ DEPEND[html/man3/EVP_PKEY_supports_digest_nid.html]=man3/EVP_PKEY_supports_diges GENERATE[html/man3/EVP_PKEY_supports_digest_nid.html]=man3/EVP_PKEY_supports_digest_nid.pod DEPEND[man/man3/EVP_PKEY_supports_digest_nid.3]=man3/EVP_PKEY_supports_digest_nid.pod GENERATE[man/man3/EVP_PKEY_supports_digest_nid.3]=man3/EVP_PKEY_supports_digest_nid.pod +DEPEND[html/man3/EVP_PKEY_todata.html]=man3/EVP_PKEY_todata.pod +GENERATE[html/man3/EVP_PKEY_todata.html]=man3/EVP_PKEY_todata.pod +DEPEND[man/man3/EVP_PKEY_todata.3]=man3/EVP_PKEY_todata.pod +GENERATE[man/man3/EVP_PKEY_todata.3]=man3/EVP_PKEY_todata.pod DEPEND[html/man3/EVP_PKEY_verify.html]=man3/EVP_PKEY_verify.pod GENERATE[html/man3/EVP_PKEY_verify.html]=man3/EVP_PKEY_verify.pod DEPEND[man/man3/EVP_PKEY_verify.3]=man3/EVP_PKEY_verify.pod @@ -2962,6 +2966,7 @@ html/man3/EVP_PKEY_settable_params.html \ html/man3/EVP_PKEY_sign.html \ html/man3/EVP_PKEY_size.html \ html/man3/EVP_PKEY_supports_digest_nid.html \ +html/man3/EVP_PKEY_todata.html \ html/man3/EVP_PKEY_verify.html \ html/man3/EVP_PKEY_verify_recover.html \ html/man3/EVP_RAND.html \ @@ -3536,6 +3541,7 @@ man/man3/EVP_PKEY_settable_params.3 \ man/man3/EVP_PKEY_sign.3 \ man/man3/EVP_PKEY_size.3 \ man/man3/EVP_PKEY_supports_digest_nid.3 \ +man/man3/EVP_PKEY_todata.3 \ man/man3/EVP_PKEY_verify.3 \ man/man3/EVP_PKEY_verify_recover.3 \ man/man3/EVP_RAND.3 \ diff --git a/doc/internal/man3/evp_keymgmt_util_export_to_provider.pod b/doc/internal/man3/evp_keymgmt_util_export_to_provider.pod index 4a6e9b31f5f..1fee9f6ff97 100644 --- a/doc/internal/man3/evp_keymgmt_util_export_to_provider.pod +++ b/doc/internal/man3/evp_keymgmt_util_export_to_provider.pod @@ -33,7 +33,8 @@ OP_CACHE_ELEM =head1 DESCRIPTION evp_keymgmt_util_export() calls L with the -I and I from I. This is a convenience function. +I and I from I. This is used as a +helper for L. evp_keymgmt_util_export_to_provider() exports cached key material (provider side key material) from the given key I to a provider diff --git a/doc/man3/EVP_PKEY_fromdata.pod b/doc/man3/EVP_PKEY_fromdata.pod index 1c854746623..d24fb34a259 100644 --- a/doc/man3/EVP_PKEY_fromdata.pod +++ b/doc/man3/EVP_PKEY_fromdata.pod @@ -78,6 +78,7 @@ public key and key parameters. =head1 NOTES These functions only work with key management methods coming from a provider. +This is the mirror function to L. =for comment We may choose to make this available for legacy methods too... @@ -259,7 +260,7 @@ example with L. =head1 SEE ALSO L, L, L, -L, +L, L, L, L, L, L, L, L, L, L diff --git a/doc/man3/EVP_PKEY_todata.pod b/doc/man3/EVP_PKEY_todata.pod new file mode 100644 index 00000000000..98ae4847554 --- /dev/null +++ b/doc/man3/EVP_PKEY_todata.pod @@ -0,0 +1,64 @@ +=pod + +=head1 NAME + +EVP_PKEY_todata, EVP_PKEY_export +- functions to return keys as an array of key parameters + +=head1 SYNOPSIS + + #include + + int EVP_PKEY_todata(const EVP_PKEY *pkey, int selection, OSSL_PARAM **params); + int EVP_PKEY_export(const EVP_PKEY *pkey, int selection, + OSSL_CALLBACK *export_cb, void *export_cbarg); + +=head1 DESCRIPTION + +The functions described here are used to extract B key values as an +array of B. + +EVP_PKEY_todata() extracts values from a key I using the I. +I is described in L. +L should be used to free the returned parameters in +I<*params>. + +EVP_PKEY_export() is similiar to EVP_PKEY_todata() but uses a callback +I that gets passed the value of I. +See L for more information about the callback. Note that the +B array that is passed to the callback is not persistent after the +callback returns. The user must preserve the items of interest, or use +EVP_PKEY_todata() if persistence is required. + +=head1 NOTES + +These functions only work with key management methods coming from a provider. +This is the mirror function to L. + +=head1 RETURN VALUES + +EVP_PKEY_todata() and EVP_PKEY_export() return 1 for success and 0 for failure. + +=head1 SEE ALSO + +L, L, +L, +L, L, L, L, +L, L, L, +L + +=head1 HISTORY + +These functions were added in OpenSSL 3.0. + +=head1 COPYRIGHT + +Copyright 2021 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 +L. + +=cut + diff --git a/doc/man7/evp.pod b/doc/man7/evp.pod index 74fc975ce15..307008f7ba1 100644 --- a/doc/man7/evp.pod +++ b/doc/man7/evp.pod @@ -32,7 +32,8 @@ with a private key of a particular algorithm by using the functions described on the L page, or new keys can be generated using L. EVP_PKEYs can be compared using L, or printed using -L. +L. L can be used to convert a +key back into an L array. The EVP_PKEY functions support the full range of asymmetric algorithm operations: @@ -91,6 +92,7 @@ L, L, L, L, +L, L, L, L, diff --git a/include/openssl/evp.h b/include/openssl/evp.h index e6d5f078ac2..35cdfe1c057 100644 --- a/include/openssl/evp.h +++ b/include/openssl/evp.h @@ -1855,6 +1855,11 @@ int EVP_PKEY_fromdata_init(EVP_PKEY_CTX *ctx); int EVP_PKEY_fromdata(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey, int selection, OSSL_PARAM param[]); const OSSL_PARAM *EVP_PKEY_fromdata_settable(EVP_PKEY_CTX *ctx, int selection); + +int EVP_PKEY_todata(const EVP_PKEY *pkey, int selection, OSSL_PARAM **params); +int EVP_PKEY_export(const EVP_PKEY *pkey, int selection, + OSSL_CALLBACK *export_cb, void *export_cbarg); + const OSSL_PARAM *EVP_PKEY_gettable_params(const EVP_PKEY *pkey); int EVP_PKEY_get_params(const EVP_PKEY *pkey, OSSL_PARAM params[]); int EVP_PKEY_get_int_param(const EVP_PKEY *pkey, const char *key_name, diff --git a/providers/implementations/keymgmt/dsa_kmgmt.c b/providers/implementations/keymgmt/dsa_kmgmt.c index 38e682f3b6b..4f05799bb38 100644 --- a/providers/implementations/keymgmt/dsa_kmgmt.c +++ b/providers/implementations/keymgmt/dsa_kmgmt.c @@ -206,7 +206,7 @@ static int dsa_export(void *keydata, int selection, OSSL_CALLBACK *param_cb, if (!ok || (params = OSSL_PARAM_BLD_to_param(tmpl)) == NULL) - goto err;; + goto err; ok = param_cb(params, cbarg); OSSL_PARAM_free(params); diff --git a/test/evp_extra_test2.c b/test/evp_extra_test2.c index 45d1ce25692..358ac6053a3 100644 --- a/test/evp_extra_test2.c +++ b/test/evp_extra_test2.c @@ -348,6 +348,368 @@ static int test_d2i_PrivateKey_ex(void) { 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, 0x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 0x88, 0xe0, 0xda, 0x6c, 0x5b, 0x89, 0xb2, 0x5b, 0x40, 0x6d, + 0xeb, 0x78, 0x7a, 0xd5, 0xaf, 0x40, 0x52, 0x46, 0x63, 0x92, 0x13, 0x0d, + 0xee, 0xee, 0xf9, 0x53, 0xca, 0x2d, 0x4e, 0x3b, 0x13, 0xd8, 0x0f, 0x50, + 0xd0, 0x44, 0x57, 0x67, 0x0f, 0x45, 0x8f, 0x21, 0x30, 0x97, 0x9e, 0x80, + 0xd9, 0xd0, 0x91, 0xb7, 0xc9, 0x5a, 0x69, 0xda, 0xeb, 0xd5, 0xea, 0x37, + 0xf6, 0xb3, 0xbe, 0x1f, 0x24, 0xf1, 0x55, 0x14, 0x28, 0x05, 0xb5, 0xd8, + 0x84, 0x0f, 0x62, 0x85, 0xaa, 0xec, 0x77, 0x64, 0xfd, 0x80, 0x7c, 0x41, + 0x00, 0x88, 0xa3, 0x79, 0x7d, 0x4f, 0x6f, 0xe3, 0x76, 0xf4, 0xb5, 0x97, + 0xb7, 0xeb, 0x67, 0x28, 0xba, 0x07, 0x1a, 0x59, 0x32, 0xc1, 0x53, 0xd9, + 0x05, 0x6b, 0x63, 0x93, 0xce, 0xa1, 0xd9, 0x7a, 0xb2, 0xff, 0x1c, 0x12, + 0x0a, 0x9a, 0xe5, 0x51, 0x1e, 0xba, 0xfc, 0x95, 0x2e, 0x28, 0xa9, 0xfc, + 0x4c, 0xed, 0x7b, 0x05, 0xca, 0x67, 0xe0, 0x2d, 0xd7, 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, 0x02, 0x82, 0x01, 0x00, + 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, 0x02, 0x1c, 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_p[] = { + 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, 0x99, 0x3d, 0xf8, 0x8b, 0xfb, 0x47, 0xf8, 0x5e, 0x05, 0x68, 0x6d, 0x8b, 0xa8, 0xad, + 0xa1, 0xc2, 0x3a, 0x4e, 0xe0, 0xad, 0xec, 0x38, 0x75, 0x21, 0x55, 0x22, 0xce, 0xa2, 0xe9, + 0xe5, 0x3b, 0xd7, 0x44, 0xeb, 0x5a, 0x03, 0x59, 0xa0, 0xc5, 0x7a, 0x92, 0x59, 0x7d, 0x7a, + 0x07, 0x80, 0xfc, 0x4e, 0xf8, 0x56, 0x7e, 0xf1, 0x06, 0xe0, 0xba, 0xb2, 0xe7, 0x5b, 0x22, + 0x55, 0xee, 0x4b, 0x42, 0x61, 0x67, 0x2c, 0x43, 0x9a, 0x38, 0x2b, 0x17, 0xc2, 0x62, 0x12, + 0x8b, 0x0b, 0x22, 0x8c, 0x0c, 0x1c, 0x1c, 0x92, 0xb1, 0xec, 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, 0xda, 0xe0, 0x71, 0xe6, 0xcf, 0x46, 0x86, 0x0a, 0x37, + 0x57, 0xb6, 0xe9, 0xcf, 0xa1, 0x78, 0x19, 0xb8, 0x72, 0x9f, 0x30, 0x8c, 0x2a, 0x04, 0x7c, + 0x2f, 0x0c, 0x27, 0xa7, 0xb3, 0x23, 0xe0, 0x46, 0xf2, 0x75, 0x0c, 0x03, 0x4c, 0xad, 0xfb, + 0xc1, 0xcb, 0x28, 0xcd, 0xa0, 0x63, 0xdb, 0x44, 0x88, 0xe0, 0xda, 0x6c, 0x5b, 0x89, 0xb2, + 0x5b, 0x40, 0x6d, 0xeb, 0x78, 0x7a, 0xd5, 0xaf, 0x40, 0x52, 0x46, 0x63, 0x92, 0x13, 0x0d, + 0xee, 0xee, 0xf9, 0x53, 0xca, 0x2d, 0x4e, 0x3b, 0x13, 0xd8, 0x0f, 0x50, 0xd0, 0x44, 0x57, + 0x67, 0x0f, 0x45, 0x8f, 0x21, 0x30, 0x97, 0x9e, 0x80, 0xd9, 0xd0, 0x91, 0xb7, 0xc9, 0x5a, + 0x69, 0xda, 0xeb, 0xd5, 0xea, 0x37, 0xf6, 0xb3, 0xbe, 0x1f, 0x24, 0xf1, 0x55, 0x14, 0x28, + 0x05, 0xb5, 0xd8, 0x84, 0x0f, 0x62, 0x85, 0xaa, 0xec, 0x77, 0x64, 0xfd, 0x80, 0x7c, 0x41, + 0x00, 0x88, 0xa3, 0x79, 0x7d, 0x4f, 0x6f, 0xe3, 0x76, 0xf4, 0xb5, 0x97, 0xb7, 0xeb, 0x67, + 0x28, 0xba, 0x07, 0x1a, 0x59, 0x32, 0xc1, 0x53, 0xd9, 0x05, 0x6b, 0x63, 0x93, 0xce, 0xa1, + 0xd9, 0x7a, 0xb2, 0xff, 0x1c, 0x12, 0x0a, 0x9a, 0xe5, 0x51, 0x1e, 0xba, 0xfc, 0x95, 0x2e, + 0x28, 0xa9, 0xfc, 0x4c, 0xed, 0x7b, 0x05, 0xca, 0x67, 0xe0, 0x2d, 0xd7, 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 +}; + +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 do_check_utf8_str(OSSL_PARAM params[], const char *key, + const char *expected) +{ + OSSL_PARAM *p; + char *bufp = 0; + int ret; + + ret = TEST_ptr(p = OSSL_PARAM_locate(params, key)) + && TEST_true(OSSL_PARAM_get_utf8_string(p, &bufp, 0)) + && TEST_str_eq(bufp, expected); + OPENSSL_free(bufp); + return ret; +} + +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_utf8_str(to_params, OSSL_PKEY_PARAM_FFC_VALIDATE_TYPE, + OSSL_FFC_PARAM_VALIDATE_PQG) + || !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; +} +#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; + 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(pkey, EVP_PKEY_KEYPAIR, + test_pkey_export_cb, pkey), 1) + && TEST_int_eq(EVP_PKEY_export(pkey, EVP_PKEY_KEYPAIR, + test_pkey_export_cb, NULL), 0); + EVP_PKEY_free(pkey); + return ret; +} + int setup_tests(void) { if (!test_get_libctx(&mainctx, &nullprov, NULL, NULL, NULL)) { @@ -359,7 +721,12 @@ int setup_tests(void) ADD_TEST(test_alternative_default); ADD_ALL_TESTS(test_d2i_AutoPrivateKey_ex, OSSL_NELEM(keydata)); ADD_TEST(test_d2i_PrivateKey_ex); - +#ifndef OPENSSL_NO_DSA + ADD_TEST(test_dsa_todata); +#endif + ADD_TEST(test_pkey_todata_null); + ADD_TEST(test_pkey_export_null); + ADD_TEST(test_pkey_export); return 1; } diff --git a/util/libcrypto.num b/util/libcrypto.num index 7e723f82632..23504384781 100644 --- a/util/libcrypto.num +++ b/util/libcrypto.num @@ -5350,3 +5350,5 @@ X509_CRL_new_ex ? 3_0_0 EXIST::FUNCTION: OSSL_PARAM_dup ? 3_0_0 EXIST::FUNCTION: OSSL_PARAM_merge ? 3_0_0 EXIST::FUNCTION: OSSL_PARAM_free ? 3_0_0 EXIST::FUNCTION: +EVP_PKEY_todata ? 3_0_0 EXIST::FUNCTION: +EVP_PKEY_export ? 3_0_0 EXIST::FUNCTION: