Path: blob/main/crypto/openssl/demos/pkey/EVP_PKEY_DSA_paramvalidate.c
34914 views
/*-1* Copyright 2022-2024 The OpenSSL Project Authors. All Rights Reserved.2*3* Licensed under the Apache License 2.0 (the "License"). You may not use4* this file except in compliance with the License. You can obtain a copy5* in the file LICENSE in the source distribution or at6* https://www.openssl.org/source/license.html7*/89/*10* Example showing how to validate DSA parameters.11*12* Proper FIPS 186-4 DSA (FFC) parameter validation requires that all13* the parameters used during parameter generation are supplied14* when doing the validation. Unfortunately saving DSA parameters as15* a PEM or DER file does not write out all required fields. Because16* of this the default provider normally only does a partial17* validation. The FIPS provider will however try to do a full18* validation. To force the default provider to use full19* validation the 'seed' that is output during generation must be20* added to the key. See doc/man7/EVP_PKEY-FFC for more information.21*/2223#include <openssl/evp.h>24#include <openssl/core_names.h>25#include <openssl/pem.h>26#include "dsa.inc"2728/* The following values were output from the EVP_PKEY_DSA_paramgen demo */29static const char dsapem[] =30"-----BEGIN DSA PARAMETERS-----\n"31"MIICLAKCAQEA1pobSR1FJ3+Tvi0J6Tk1PSV2owZey1Nuo847hGw/59VCS6RPQEqr\n"32"vp5fhbvBjupBeVGA/AMH6rI4i4h6jlhurrqH1CqUHVcDhJzxV668bMLiP3mIxg5o\n"33"9Yq8x6BnSOtH5Je0tpeE0/fEvvLjCwBUbwnwWxzjANcvDUEt9XYeRrtB2v52fr56\n"34"hVYz3wMMNog4CEDOLTvx7/84eVPuUeWDRQFH1EaHMdulP34KBcatEEpEZapkepng\n"35"nohm9sFSPQhq2utpkH7pNXdG0EILBtRDCvUpF5720a48LYofdggh2VEZfgElAGFk\n"36"dW/CkvyBDmGIzil5aTz4MMsdudaVYgzt6wIhAPsSGC42Qa+X0AFGvonb5nmfUVm/\n"37"8aC+tHk7Nb2AYLHXAoIBADx5C0H1+QHsmGKvuOaY+WKUt7aWUrEivD1zBMJAQ6bL\n"38"Wv9lbCq1CFHvVzojeOVpn872NqDEpkx4HTpvqhxWL5CkbN/HaGItsQzkD59AQg3v\n"39"4YsLlkesq9Jq6x/aWetJXWO36fszFv1gpD3NY3wliBvMYHx62jfc5suh9D3ZZvu7\n"40"PLGH4X4kcfzK/R2b0oVbEBjVTe5GMRYZRqnvfSW2f2fA7BzI1OL83UxDDe58cL2M\n"41"GcAoUYXOBAfZ37qLMm2juf+o5gCrT4CXfRPu6kbapt7V/YIc1nsNgeAOKKoFBHBQ\n"42"gc5u5G6G/j79FVoSDq9DYwTJcHPsU+eHj1uWHso1AjQ=\n"43"-----END DSA PARAMETERS-----\n";4445static const char hexseed[] =46"cba30ccd905aa7675a0b81769704bf3c"47"ccf2ca1892b2eaf6b9e2b38d9bf6affc"48"42ada55986d8a1772b442770954d0b65";49static const int gindex = 42;50static const int pcounter = 363;51static const char digest[] = "SHA384";5253/*54* Create a new dsa param key that is the combination of an existing param key55* plus extra parameters.56*/57static EVP_PKEY_CTX *create_merged_key(EVP_PKEY *dsaparams, const OSSL_PARAM *newparams,58OSSL_LIB_CTX *libctx, const char *propq)59{60EVP_PKEY_CTX *out = NULL;61EVP_PKEY_CTX *ctx = NULL;62EVP_PKEY *pkey = NULL;63OSSL_PARAM *mergedparams = NULL;64OSSL_PARAM *loadedparams = NULL;6566/* Specify EVP_PKEY_KEY_PUBLIC here if you have a public key */67if (EVP_PKEY_todata(dsaparams, EVP_PKEY_KEY_PARAMETERS, &loadedparams) <= 0) {68fprintf(stderr, "EVP_PKEY_todata() failed\n");69goto cleanup;70}71mergedparams = OSSL_PARAM_merge(loadedparams, newparams);72if (mergedparams == NULL) {73fprintf(stderr, "OSSL_PARAM_merge() failed\n");74goto cleanup;75}7677ctx = EVP_PKEY_CTX_new_from_name(libctx, "DSA", propq);78if (ctx == NULL) {79fprintf(stderr, "EVP_PKEY_CTX_new_from_name() failed\n");80goto cleanup;81}82if (EVP_PKEY_fromdata_init(ctx) <= 083|| EVP_PKEY_fromdata(ctx, &pkey,84EVP_PKEY_KEY_PARAMETERS, mergedparams) <= 0) {85fprintf(stderr, "EVP_PKEY_fromdata() failed\n");86goto cleanup;87}88out = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, propq);89if (out == NULL) {90fprintf(stderr, "EVP_PKEY_CTX_new_from_pkey() failed\n");91goto cleanup;92}9394cleanup:95EVP_PKEY_free(pkey);96OSSL_PARAM_free(loadedparams);97OSSL_PARAM_free(mergedparams);98EVP_PKEY_CTX_free(ctx);99return out;100}101102int main(int argc, char **argv)103{104int ret = EXIT_FAILURE;105OSSL_LIB_CTX *libctx = NULL;106const char *propq = NULL;107EVP_PKEY *dsaparamskey = NULL;108EVP_PKEY_CTX *ctx = NULL;109EVP_PKEY_CTX *ctx1 = NULL;110EVP_PKEY_CTX *ctx2 = NULL;111BIO *in = NULL;112OSSL_PARAM params[6];113unsigned char seed[64];114size_t seedlen;115116if (!OPENSSL_hexstr2buf_ex(seed, sizeof(seed), &seedlen, hexseed, '\0'))117goto cleanup;118/*119* This example loads the PEM data from a memory buffer120* Use BIO_new_fp() to load a PEM file instead121*/122in = BIO_new_mem_buf(dsapem, strlen(dsapem));123if (in == NULL) {124fprintf(stderr, "BIO_new_mem_buf() failed\n");125goto cleanup;126}127128/* Load DSA params from pem data */129dsaparamskey = PEM_read_bio_Parameters_ex(in, NULL, libctx, propq);130if (dsaparamskey == NULL) {131fprintf(stderr, "Failed to load dsa params\n");132goto cleanup;133}134135ctx = EVP_PKEY_CTX_new_from_pkey(libctx, dsaparamskey, propq);136if (ctx == NULL) {137fprintf(stderr, "EVP_PKEY_CTX_new_from_pkey() failed\n");138goto cleanup;139}140/*141* When using the default provider this only does a partial check to142* make sure that the values of p, q and g are ok.143* This will fail however if the FIPS provider is used since it does144* a proper FIPS 186-4 key validation which requires extra parameters145*/146if (EVP_PKEY_param_check(ctx) <= 0) {147fprintf(stderr, "Simple EVP_PKEY_param_check() failed\n");148goto cleanup;149}150151/*152* Setup parameters that we want to add.153* For illustration purposes it deliberately omits a required parameter.154*/155params[0] = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_FFC_TYPE,156"fips186_4", 0);157/* Force it to do a proper validation by setting the seed */158params[1] = OSSL_PARAM_construct_octet_string(OSSL_PKEY_PARAM_FFC_SEED,159(void *)seed, seedlen);160params[2] = OSSL_PARAM_construct_int(OSSL_PKEY_PARAM_FFC_GINDEX, (int *)&gindex);161params[3] = OSSL_PARAM_construct_int(OSSL_PKEY_PARAM_FFC_PCOUNTER, (int *)&pcounter);162params[4] = OSSL_PARAM_construct_end();163164/* generate a new key that is the combination of the existing key and the new params */165ctx1 = create_merged_key(dsaparamskey, params, libctx, propq);166if (ctx1 == NULL)167goto cleanup;168/* This will fail since not all the parameters used for key generation are added */169if (EVP_PKEY_param_check(ctx1) > 0) {170fprintf(stderr, "EVP_PKEY_param_check() should fail\n");171goto cleanup;172}173174/*175* Add the missing parameters onto the end of the existing list of params176* If the default was used for the generation then this parameter is not177* needed178*/179params[4] = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_FFC_DIGEST,180(char *)digest, 0);181params[5] = OSSL_PARAM_construct_end();182ctx2 = create_merged_key(dsaparamskey, params, libctx, propq);183if (ctx2 == NULL)184goto cleanup;185if (EVP_PKEY_param_check(ctx2) <= 0) {186fprintf(stderr, "EVP_PKEY_param_check() failed\n");187goto cleanup;188}189190if (!dsa_print_key(EVP_PKEY_CTX_get0_pkey(ctx2), 0, libctx, propq))191goto cleanup;192193ret = EXIT_SUCCESS;194cleanup:195EVP_PKEY_free(dsaparamskey);196EVP_PKEY_CTX_free(ctx2);197EVP_PKEY_CTX_free(ctx1);198EVP_PKEY_CTX_free(ctx);199BIO_free(in);200return ret;201}202203204