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