xref: /freebsd-src/crypto/openssl/demos/pkey/EVP_PKEY_DSA_paramvalidate.c (revision e0c4386e7e71d93b0edc0c8fa156263fc4a8b0b6)
1*e0c4386eSCy Schubert /*-
2*e0c4386eSCy Schubert  * Copyright 2022 The OpenSSL Project Authors. All Rights Reserved.
3*e0c4386eSCy Schubert  *
4*e0c4386eSCy Schubert  * Licensed under the Apache License 2.0 (the "License").  You may not use
5*e0c4386eSCy Schubert  * this file except in compliance with the License.  You can obtain a copy
6*e0c4386eSCy Schubert  * in the file LICENSE in the source distribution or at
7*e0c4386eSCy Schubert  * https://www.openssl.org/source/license.html
8*e0c4386eSCy Schubert  */
9*e0c4386eSCy Schubert 
10*e0c4386eSCy Schubert /*
11*e0c4386eSCy Schubert  * Example showing how to validate DSA parameters.
12*e0c4386eSCy Schubert  *
13*e0c4386eSCy Schubert  * Proper FIPS 186-4 DSA (FFC) parameter validation requires that all
14*e0c4386eSCy Schubert  * the parameters used during parameter generation are supplied
15*e0c4386eSCy Schubert  * when doing the validation. Unfortunately saving DSA parameters as
16*e0c4386eSCy Schubert  * a PEM or DER file does not write out all required fields. Because
17*e0c4386eSCy Schubert  * of this the default provider normally only does a partial
18*e0c4386eSCy Schubert  * validation. The FIPS provider will however try to do a full
19*e0c4386eSCy Schubert  * validation. To force the default provider to use full
20*e0c4386eSCy Schubert  * validation the 'seed' that is output during generation must be
21*e0c4386eSCy Schubert  * added to the key. See doc/man7/EVP_PKEY-FFC for more information.
22*e0c4386eSCy Schubert  */
23*e0c4386eSCy Schubert 
24*e0c4386eSCy Schubert #include <openssl/evp.h>
25*e0c4386eSCy Schubert #include <openssl/core_names.h>
26*e0c4386eSCy Schubert #include <openssl/pem.h>
27*e0c4386eSCy Schubert #include "dsa.inc"
28*e0c4386eSCy Schubert 
29*e0c4386eSCy Schubert /* The following values were output from the EVP_PKEY_DSA_paramgen demo */
30*e0c4386eSCy Schubert static const char dsapem[] =
31*e0c4386eSCy Schubert     "-----BEGIN DSA PARAMETERS-----\n"
32*e0c4386eSCy Schubert     "MIICLAKCAQEA1pobSR1FJ3+Tvi0J6Tk1PSV2owZey1Nuo847hGw/59VCS6RPQEqr\n"
33*e0c4386eSCy Schubert     "vp5fhbvBjupBeVGA/AMH6rI4i4h6jlhurrqH1CqUHVcDhJzxV668bMLiP3mIxg5o\n"
34*e0c4386eSCy Schubert     "9Yq8x6BnSOtH5Je0tpeE0/fEvvLjCwBUbwnwWxzjANcvDUEt9XYeRrtB2v52fr56\n"
35*e0c4386eSCy Schubert     "hVYz3wMMNog4CEDOLTvx7/84eVPuUeWDRQFH1EaHMdulP34KBcatEEpEZapkepng\n"
36*e0c4386eSCy Schubert     "nohm9sFSPQhq2utpkH7pNXdG0EILBtRDCvUpF5720a48LYofdggh2VEZfgElAGFk\n"
37*e0c4386eSCy Schubert     "dW/CkvyBDmGIzil5aTz4MMsdudaVYgzt6wIhAPsSGC42Qa+X0AFGvonb5nmfUVm/\n"
38*e0c4386eSCy Schubert     "8aC+tHk7Nb2AYLHXAoIBADx5C0H1+QHsmGKvuOaY+WKUt7aWUrEivD1zBMJAQ6bL\n"
39*e0c4386eSCy Schubert     "Wv9lbCq1CFHvVzojeOVpn872NqDEpkx4HTpvqhxWL5CkbN/HaGItsQzkD59AQg3v\n"
40*e0c4386eSCy Schubert     "4YsLlkesq9Jq6x/aWetJXWO36fszFv1gpD3NY3wliBvMYHx62jfc5suh9D3ZZvu7\n"
41*e0c4386eSCy Schubert     "PLGH4X4kcfzK/R2b0oVbEBjVTe5GMRYZRqnvfSW2f2fA7BzI1OL83UxDDe58cL2M\n"
42*e0c4386eSCy Schubert     "GcAoUYXOBAfZ37qLMm2juf+o5gCrT4CXfRPu6kbapt7V/YIc1nsNgeAOKKoFBHBQ\n"
43*e0c4386eSCy Schubert     "gc5u5G6G/j79FVoSDq9DYwTJcHPsU+eHj1uWHso1AjQ=\n"
44*e0c4386eSCy Schubert     "-----END DSA PARAMETERS-----\n";
45*e0c4386eSCy Schubert 
46*e0c4386eSCy Schubert static const char hexseed[] =
47*e0c4386eSCy Schubert     "cba30ccd905aa7675a0b81769704bf3c"
48*e0c4386eSCy Schubert     "ccf2ca1892b2eaf6b9e2b38d9bf6affc"
49*e0c4386eSCy Schubert     "42ada55986d8a1772b442770954d0b65";
50*e0c4386eSCy Schubert const int gindex = 42;
51*e0c4386eSCy Schubert const int pcounter = 363;
52*e0c4386eSCy Schubert static const char digest[] = "SHA384";
53*e0c4386eSCy Schubert 
54*e0c4386eSCy Schubert /*
55*e0c4386eSCy Schubert  * Create a new dsa param key that is the combination of an existing param key
56*e0c4386eSCy Schubert  * plus extra parameters.
57*e0c4386eSCy Schubert  */
create_merged_key(EVP_PKEY * dsaparams,const OSSL_PARAM * newparams,OSSL_LIB_CTX * libctx,const char * propq)58*e0c4386eSCy Schubert EVP_PKEY_CTX *create_merged_key(EVP_PKEY *dsaparams, const OSSL_PARAM *newparams,
59*e0c4386eSCy Schubert                                 OSSL_LIB_CTX *libctx, const char *propq)
60*e0c4386eSCy Schubert {
61*e0c4386eSCy Schubert     EVP_PKEY_CTX *out = NULL;
62*e0c4386eSCy Schubert     EVP_PKEY_CTX *ctx = NULL;
63*e0c4386eSCy Schubert     EVP_PKEY *pkey = NULL;
64*e0c4386eSCy Schubert     OSSL_PARAM *mergedparams = NULL;
65*e0c4386eSCy Schubert     OSSL_PARAM *loadedparams = NULL;
66*e0c4386eSCy Schubert 
67*e0c4386eSCy Schubert     /* Specify EVP_PKEY_KEY_PUBLIC here if you have a public key */
68*e0c4386eSCy Schubert     if (EVP_PKEY_todata(dsaparams, EVP_PKEY_KEY_PARAMETERS, &loadedparams) <= 0) {
69*e0c4386eSCy Schubert         fprintf(stderr, "EVP_PKEY_todata() failed\n");
70*e0c4386eSCy Schubert         goto cleanup;
71*e0c4386eSCy Schubert     }
72*e0c4386eSCy Schubert     mergedparams = OSSL_PARAM_merge(loadedparams, newparams);
73*e0c4386eSCy Schubert     if (mergedparams == NULL) {
74*e0c4386eSCy Schubert         fprintf(stderr, "OSSL_PARAM_merge() failed\n");
75*e0c4386eSCy Schubert         goto cleanup;
76*e0c4386eSCy Schubert     }
77*e0c4386eSCy Schubert 
78*e0c4386eSCy Schubert     ctx = EVP_PKEY_CTX_new_from_name(libctx, "DSA", propq);
79*e0c4386eSCy Schubert     if (ctx == NULL) {
80*e0c4386eSCy Schubert         fprintf(stderr, "EVP_PKEY_CTX_new_from_name() failed\n");
81*e0c4386eSCy Schubert         goto cleanup;
82*e0c4386eSCy Schubert     }
83*e0c4386eSCy Schubert     if (EVP_PKEY_fromdata_init(ctx) <= 0
84*e0c4386eSCy Schubert             || EVP_PKEY_fromdata(ctx, &pkey,
85*e0c4386eSCy Schubert                                  EVP_PKEY_KEY_PARAMETERS, mergedparams) <= 0) {
86*e0c4386eSCy Schubert         fprintf(stderr, "EVP_PKEY_fromdata() failed\n");
87*e0c4386eSCy Schubert         goto cleanup;
88*e0c4386eSCy Schubert     }
89*e0c4386eSCy Schubert     out = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, propq);
90*e0c4386eSCy Schubert     if (out == NULL) {
91*e0c4386eSCy Schubert         fprintf(stderr, "EVP_PKEY_CTX_new_from_pkey() failed\n");
92*e0c4386eSCy Schubert         goto cleanup;
93*e0c4386eSCy Schubert     }
94*e0c4386eSCy Schubert 
95*e0c4386eSCy Schubert cleanup:
96*e0c4386eSCy Schubert     EVP_PKEY_free(pkey);
97*e0c4386eSCy Schubert     OSSL_PARAM_free(loadedparams);
98*e0c4386eSCy Schubert     OSSL_PARAM_free(mergedparams);
99*e0c4386eSCy Schubert     EVP_PKEY_CTX_free(ctx);
100*e0c4386eSCy Schubert     return out;
101*e0c4386eSCy Schubert }
102*e0c4386eSCy Schubert 
main(int argc,char ** argv)103*e0c4386eSCy Schubert int main(int argc, char **argv)
104*e0c4386eSCy Schubert {
105*e0c4386eSCy Schubert     int rv = EXIT_FAILURE;
106*e0c4386eSCy Schubert     OSSL_LIB_CTX *libctx = NULL;
107*e0c4386eSCy Schubert     const char *propq = NULL;
108*e0c4386eSCy Schubert     EVP_PKEY *dsaparamskey = NULL;
109*e0c4386eSCy Schubert     EVP_PKEY_CTX *ctx = NULL;
110*e0c4386eSCy Schubert     EVP_PKEY_CTX *ctx1 = NULL;
111*e0c4386eSCy Schubert     EVP_PKEY_CTX *ctx2 = NULL;
112*e0c4386eSCy Schubert     BIO *in = NULL;
113*e0c4386eSCy Schubert     OSSL_PARAM params[6];
114*e0c4386eSCy Schubert     unsigned char seed[64];
115*e0c4386eSCy Schubert     size_t seedlen;
116*e0c4386eSCy Schubert 
117*e0c4386eSCy Schubert     if (!OPENSSL_hexstr2buf_ex(seed, sizeof(seed), &seedlen, hexseed, '\0'))
118*e0c4386eSCy Schubert         goto cleanup;
119*e0c4386eSCy Schubert     /*
120*e0c4386eSCy Schubert      * This example loads the PEM data from a memory buffer
121*e0c4386eSCy Schubert      * Use BIO_new_fp() to load a PEM file instead
122*e0c4386eSCy Schubert      */
123*e0c4386eSCy Schubert     in = BIO_new_mem_buf(dsapem, strlen(dsapem));
124*e0c4386eSCy Schubert     if (in == NULL) {
125*e0c4386eSCy Schubert         fprintf(stderr, "BIO_new_mem_buf() failed\n");
126*e0c4386eSCy Schubert         goto cleanup;
127*e0c4386eSCy Schubert     }
128*e0c4386eSCy Schubert 
129*e0c4386eSCy Schubert     /* Load DSA params from pem data */
130*e0c4386eSCy Schubert     dsaparamskey = PEM_read_bio_Parameters_ex(in, NULL, libctx, propq);
131*e0c4386eSCy Schubert     if (dsaparamskey == NULL) {
132*e0c4386eSCy Schubert         fprintf(stderr, "Failed to load dsa params\n");
133*e0c4386eSCy Schubert         goto cleanup;
134*e0c4386eSCy Schubert     }
135*e0c4386eSCy Schubert 
136*e0c4386eSCy Schubert     ctx = EVP_PKEY_CTX_new_from_pkey(libctx, dsaparamskey, propq);
137*e0c4386eSCy Schubert     if (ctx == NULL) {
138*e0c4386eSCy Schubert         fprintf(stderr, "EVP_PKEY_CTX_new_from_pkey() failed\n");
139*e0c4386eSCy Schubert         goto cleanup;
140*e0c4386eSCy Schubert     }
141*e0c4386eSCy Schubert     /*
142*e0c4386eSCy Schubert      * When using the default provider this only does a partial check to
143*e0c4386eSCy Schubert      * make sure that the values of p, q and g are ok.
144*e0c4386eSCy Schubert      * This will fail however if the FIPS provider is used since it does
145*e0c4386eSCy Schubert      * a proper FIPS 186-4 key validation which requires extra parameters
146*e0c4386eSCy Schubert      */
147*e0c4386eSCy Schubert     if (EVP_PKEY_param_check(ctx) <= 0) {
148*e0c4386eSCy Schubert         fprintf(stderr, "Simple EVP_PKEY_param_check() failed \n");
149*e0c4386eSCy Schubert         goto cleanup;
150*e0c4386eSCy Schubert     }
151*e0c4386eSCy Schubert 
152*e0c4386eSCy Schubert     /*
153*e0c4386eSCy Schubert      * Setup parameters that we want to add.
154*e0c4386eSCy Schubert      * For illustration purposes it deliberately omits a required parameter.
155*e0c4386eSCy Schubert      */
156*e0c4386eSCy Schubert     params[0] = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_FFC_TYPE,
157*e0c4386eSCy Schubert                                                 "fips186_4", 0);
158*e0c4386eSCy Schubert     /* Force it to do a proper validation by setting the seed */
159*e0c4386eSCy Schubert     params[1] = OSSL_PARAM_construct_octet_string(OSSL_PKEY_PARAM_FFC_SEED,
160*e0c4386eSCy Schubert                                                   (void *)seed, seedlen);
161*e0c4386eSCy Schubert     params[2] = OSSL_PARAM_construct_int(OSSL_PKEY_PARAM_FFC_GINDEX, (int *)&gindex);
162*e0c4386eSCy Schubert     params[3] = OSSL_PARAM_construct_int(OSSL_PKEY_PARAM_FFC_PCOUNTER, (int *)&pcounter);
163*e0c4386eSCy Schubert     params[4] = OSSL_PARAM_construct_end();
164*e0c4386eSCy Schubert 
165*e0c4386eSCy Schubert     /* generate a new key that is the combination of the existing key and the new params */
166*e0c4386eSCy Schubert     ctx1 = create_merged_key(dsaparamskey, params, libctx, propq);
167*e0c4386eSCy Schubert     if (ctx1 == NULL)
168*e0c4386eSCy Schubert         goto cleanup;
169*e0c4386eSCy Schubert     /* This will fail since not all the parameters used for key generation are added */
170*e0c4386eSCy Schubert     if (EVP_PKEY_param_check(ctx1) > 0) {
171*e0c4386eSCy Schubert         fprintf(stderr, "EVP_PKEY_param_check() should fail\n");
172*e0c4386eSCy Schubert         goto cleanup;
173*e0c4386eSCy Schubert     }
174*e0c4386eSCy Schubert 
175*e0c4386eSCy Schubert     /*
176*e0c4386eSCy Schubert      * Add the missing parameters onto the end of the existing list of params
177*e0c4386eSCy Schubert      * If the default was used for the generation then this parameter is not
178*e0c4386eSCy Schubert      * needed
179*e0c4386eSCy Schubert      */
180*e0c4386eSCy Schubert     params[4] = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_FFC_DIGEST,
181*e0c4386eSCy Schubert                                                  (char *)digest, 0);
182*e0c4386eSCy Schubert     params[5] = OSSL_PARAM_construct_end();
183*e0c4386eSCy Schubert     ctx2 = create_merged_key(dsaparamskey, params, libctx, propq);
184*e0c4386eSCy Schubert     if (ctx2 == NULL)
185*e0c4386eSCy Schubert         goto cleanup;
186*e0c4386eSCy Schubert     if (EVP_PKEY_param_check(ctx2) <= 0) {
187*e0c4386eSCy Schubert         fprintf(stderr, "EVP_PKEY_param_check() failed\n");
188*e0c4386eSCy Schubert         goto cleanup;
189*e0c4386eSCy Schubert     }
190*e0c4386eSCy Schubert 
191*e0c4386eSCy Schubert     if (!dsa_print_key(EVP_PKEY_CTX_get0_pkey(ctx2), 0, libctx, propq))
192*e0c4386eSCy Schubert         goto cleanup;
193*e0c4386eSCy Schubert 
194*e0c4386eSCy Schubert     rv = EXIT_SUCCESS;
195*e0c4386eSCy Schubert cleanup:
196*e0c4386eSCy Schubert     EVP_PKEY_free(dsaparamskey);
197*e0c4386eSCy Schubert     EVP_PKEY_CTX_free(ctx2);
198*e0c4386eSCy Schubert     EVP_PKEY_CTX_free(ctx1);
199*e0c4386eSCy Schubert     EVP_PKEY_CTX_free(ctx);
200*e0c4386eSCy Schubert     BIO_free(in);
201*e0c4386eSCy Schubert     return rv;
202*e0c4386eSCy Schubert }
203