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