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