Path: blob/main/crypto/openssl/demos/pkey/EVP_PKEY_DSA_paramvalidate.c
107115 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[] = "-----BEGIN DSA PARAMETERS-----\n"30"MIICLAKCAQEA1pobSR1FJ3+Tvi0J6Tk1PSV2owZey1Nuo847hGw/59VCS6RPQEqr\n"31"vp5fhbvBjupBeVGA/AMH6rI4i4h6jlhurrqH1CqUHVcDhJzxV668bMLiP3mIxg5o\n"32"9Yq8x6BnSOtH5Je0tpeE0/fEvvLjCwBUbwnwWxzjANcvDUEt9XYeRrtB2v52fr56\n"33"hVYz3wMMNog4CEDOLTvx7/84eVPuUeWDRQFH1EaHMdulP34KBcatEEpEZapkepng\n"34"nohm9sFSPQhq2utpkH7pNXdG0EILBtRDCvUpF5720a48LYofdggh2VEZfgElAGFk\n"35"dW/CkvyBDmGIzil5aTz4MMsdudaVYgzt6wIhAPsSGC42Qa+X0AFGvonb5nmfUVm/\n"36"8aC+tHk7Nb2AYLHXAoIBADx5C0H1+QHsmGKvuOaY+WKUt7aWUrEivD1zBMJAQ6bL\n"37"Wv9lbCq1CFHvVzojeOVpn872NqDEpkx4HTpvqhxWL5CkbN/HaGItsQzkD59AQg3v\n"38"4YsLlkesq9Jq6x/aWetJXWO36fszFv1gpD3NY3wliBvMYHx62jfc5suh9D3ZZvu7\n"39"PLGH4X4kcfzK/R2b0oVbEBjVTe5GMRYZRqnvfSW2f2fA7BzI1OL83UxDDe58cL2M\n"40"GcAoUYXOBAfZ37qLMm2juf+o5gCrT4CXfRPu6kbapt7V/YIc1nsNgeAOKKoFBHBQ\n"41"gc5u5G6G/j79FVoSDq9DYwTJcHPsU+eHj1uWHso1AjQ=\n"42"-----END DSA PARAMETERS-----\n";4344static const char hexseed[] = "cba30ccd905aa7675a0b81769704bf3c"45"ccf2ca1892b2eaf6b9e2b38d9bf6affc"46"42ada55986d8a1772b442770954d0b65";47static const int gindex = 42;48static const int pcounter = 363;49static const char digest[] = "SHA384";5051/*52* Create a new dsa param key that is the combination of an existing param key53* plus extra parameters.54*/55static EVP_PKEY_CTX *create_merged_key(EVP_PKEY *dsaparams, const OSSL_PARAM *newparams,56OSSL_LIB_CTX *libctx, const char *propq)57{58EVP_PKEY_CTX *out = NULL;59EVP_PKEY_CTX *ctx = NULL;60EVP_PKEY *pkey = NULL;61OSSL_PARAM *mergedparams = NULL;62OSSL_PARAM *loadedparams = NULL;6364/* Specify EVP_PKEY_KEY_PUBLIC here if you have a public key */65if (EVP_PKEY_todata(dsaparams, EVP_PKEY_KEY_PARAMETERS, &loadedparams) <= 0) {66fprintf(stderr, "EVP_PKEY_todata() failed\n");67goto cleanup;68}69mergedparams = OSSL_PARAM_merge(loadedparams, newparams);70if (mergedparams == NULL) {71fprintf(stderr, "OSSL_PARAM_merge() failed\n");72goto cleanup;73}7475ctx = EVP_PKEY_CTX_new_from_name(libctx, "DSA", propq);76if (ctx == NULL) {77fprintf(stderr, "EVP_PKEY_CTX_new_from_name() failed\n");78goto cleanup;79}80if (EVP_PKEY_fromdata_init(ctx) <= 081|| EVP_PKEY_fromdata(ctx, &pkey,82EVP_PKEY_KEY_PARAMETERS, mergedparams)83<= 0) {84fprintf(stderr, "EVP_PKEY_fromdata() failed\n");85goto cleanup;86}87out = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, propq);88if (out == NULL) {89fprintf(stderr, "EVP_PKEY_CTX_new_from_pkey() failed\n");90goto cleanup;91}9293cleanup:94EVP_PKEY_free(pkey);95OSSL_PARAM_free(loadedparams);96OSSL_PARAM_free(mergedparams);97EVP_PKEY_CTX_free(ctx);98return out;99}100101int main(int argc, char **argv)102{103int ret = EXIT_FAILURE;104OSSL_LIB_CTX *libctx = NULL;105const char *propq = NULL;106EVP_PKEY *dsaparamskey = NULL;107EVP_PKEY_CTX *ctx = NULL;108EVP_PKEY_CTX *ctx1 = NULL;109EVP_PKEY_CTX *ctx2 = NULL;110BIO *in = NULL;111OSSL_PARAM params[6];112unsigned char seed[64];113size_t seedlen;114115if (!OPENSSL_hexstr2buf_ex(seed, sizeof(seed), &seedlen, hexseed, '\0'))116goto cleanup;117/*118* This example loads the PEM data from a memory buffer119* Use BIO_new_fp() to load a PEM file instead120*/121in = BIO_new_mem_buf(dsapem, strlen(dsapem));122if (in == NULL) {123fprintf(stderr, "BIO_new_mem_buf() failed\n");124goto cleanup;125}126127/* Load DSA params from pem data */128dsaparamskey = PEM_read_bio_Parameters_ex(in, NULL, libctx, propq);129if (dsaparamskey == NULL) {130fprintf(stderr, "Failed to load dsa params\n");131goto cleanup;132}133134ctx = EVP_PKEY_CTX_new_from_pkey(libctx, dsaparamskey, propq);135if (ctx == NULL) {136fprintf(stderr, "EVP_PKEY_CTX_new_from_pkey() failed\n");137goto cleanup;138}139/*140* When using the default provider this only does a partial check to141* make sure that the values of p, q and g are ok.142* This will fail however if the FIPS provider is used since it does143* a proper FIPS 186-4 key validation which requires extra parameters144*/145if (EVP_PKEY_param_check(ctx) <= 0) {146fprintf(stderr, "Simple EVP_PKEY_param_check() failed\n");147goto cleanup;148}149150/*151* Setup parameters that we want to add.152* For illustration purposes it deliberately omits a required parameter.153*/154params[0] = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_FFC_TYPE,155"fips186_4", 0);156/* Force it to do a proper validation by setting the seed */157params[1] = OSSL_PARAM_construct_octet_string(OSSL_PKEY_PARAM_FFC_SEED,158(void *)seed, seedlen);159params[2] = OSSL_PARAM_construct_int(OSSL_PKEY_PARAM_FFC_GINDEX, (int *)&gindex);160params[3] = OSSL_PARAM_construct_int(OSSL_PKEY_PARAM_FFC_PCOUNTER, (int *)&pcounter);161params[4] = OSSL_PARAM_construct_end();162163/* generate a new key that is the combination of the existing key and the new params */164ctx1 = create_merged_key(dsaparamskey, params, libctx, propq);165if (ctx1 == NULL)166goto cleanup;167/* This will fail since not all the parameters used for key generation are added */168if (EVP_PKEY_param_check(ctx1) > 0) {169fprintf(stderr, "EVP_PKEY_param_check() should fail\n");170goto cleanup;171}172173/*174* Add the missing parameters onto the end of the existing list of params175* If the default was used for the generation then this parameter is not176* needed177*/178params[4] = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_FFC_DIGEST,179(char *)digest, 0);180params[5] = OSSL_PARAM_construct_end();181ctx2 = create_merged_key(dsaparamskey, params, libctx, propq);182if (ctx2 == NULL)183goto cleanup;184if (EVP_PKEY_param_check(ctx2) <= 0) {185fprintf(stderr, "EVP_PKEY_param_check() failed\n");186goto cleanup;187}188189if (!dsa_print_key(EVP_PKEY_CTX_get0_pkey(ctx2), 0, libctx, propq))190goto cleanup;191192ret = EXIT_SUCCESS;193cleanup:194EVP_PKEY_free(dsaparamskey);195EVP_PKEY_CTX_free(ctx2);196EVP_PKEY_CTX_free(ctx1);197EVP_PKEY_CTX_free(ctx);198BIO_free(in);199return ret;200}201202203