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 load DSA params from raw data
12*e0c4386eSCy Schubert * using EVP_PKEY_fromdata()
13*e0c4386eSCy Schubert */
14*e0c4386eSCy Schubert
15*e0c4386eSCy Schubert #include <openssl/param_build.h>
16*e0c4386eSCy Schubert #include <openssl/evp.h>
17*e0c4386eSCy Schubert #include <openssl/core_names.h>
18*e0c4386eSCy Schubert #include "dsa.inc"
19*e0c4386eSCy Schubert
main(int argc,char ** argv)20*e0c4386eSCy Schubert int main(int argc, char **argv)
21*e0c4386eSCy Schubert {
22*e0c4386eSCy Schubert int rv = EXIT_FAILURE;
23*e0c4386eSCy Schubert OSSL_LIB_CTX *libctx = NULL;
24*e0c4386eSCy Schubert const char *propq = NULL;
25*e0c4386eSCy Schubert EVP_PKEY_CTX *ctx = NULL;
26*e0c4386eSCy Schubert EVP_PKEY *dsaparamkey = NULL;
27*e0c4386eSCy Schubert OSSL_PARAM_BLD *bld = NULL;
28*e0c4386eSCy Schubert OSSL_PARAM *params = NULL;
29*e0c4386eSCy Schubert BIGNUM *p = NULL, *q = NULL, *g = NULL;
30*e0c4386eSCy Schubert
31*e0c4386eSCy Schubert p = BN_bin2bn(dsa_p, sizeof(dsa_p), NULL);
32*e0c4386eSCy Schubert q = BN_bin2bn(dsa_q, sizeof(dsa_q), NULL);
33*e0c4386eSCy Schubert g = BN_bin2bn(dsa_g, sizeof(dsa_g), NULL);
34*e0c4386eSCy Schubert if (p == NULL || q == NULL || g == NULL)
35*e0c4386eSCy Schubert goto cleanup;
36*e0c4386eSCy Schubert
37*e0c4386eSCy Schubert /* Use OSSL_PARAM_BLD if you need to handle BIGNUM Parameters */
38*e0c4386eSCy Schubert bld = OSSL_PARAM_BLD_new();
39*e0c4386eSCy Schubert if (bld == NULL)
40*e0c4386eSCy Schubert goto cleanup;
41*e0c4386eSCy Schubert if (!OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_P, p)
42*e0c4386eSCy Schubert || !OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_Q, q)
43*e0c4386eSCy Schubert || !OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_G, g))
44*e0c4386eSCy Schubert goto cleanup;
45*e0c4386eSCy Schubert params = OSSL_PARAM_BLD_to_param(bld);
46*e0c4386eSCy Schubert if (params == NULL)
47*e0c4386eSCy Schubert goto cleanup;
48*e0c4386eSCy Schubert
49*e0c4386eSCy Schubert ctx = EVP_PKEY_CTX_new_from_name(libctx, "DSA", propq);
50*e0c4386eSCy Schubert if (ctx == NULL) {
51*e0c4386eSCy Schubert fprintf(stderr, "EVP_PKEY_CTX_new_from_name() failed\n");
52*e0c4386eSCy Schubert goto cleanup;
53*e0c4386eSCy Schubert }
54*e0c4386eSCy Schubert
55*e0c4386eSCy Schubert if (EVP_PKEY_fromdata_init(ctx) <= 0
56*e0c4386eSCy Schubert || EVP_PKEY_fromdata(ctx, &dsaparamkey, EVP_PKEY_KEY_PARAMETERS, params) <= 0) {
57*e0c4386eSCy Schubert fprintf(stderr, "EVP_PKEY_fromdata() failed\n");
58*e0c4386eSCy Schubert goto cleanup;
59*e0c4386eSCy Schubert }
60*e0c4386eSCy Schubert
61*e0c4386eSCy Schubert if (!dsa_print_key(dsaparamkey, 0, libctx, propq))
62*e0c4386eSCy Schubert goto cleanup;
63*e0c4386eSCy Schubert
64*e0c4386eSCy Schubert rv = EXIT_SUCCESS;
65*e0c4386eSCy Schubert cleanup:
66*e0c4386eSCy Schubert EVP_PKEY_free(dsaparamkey);
67*e0c4386eSCy Schubert EVP_PKEY_CTX_free(ctx);
68*e0c4386eSCy Schubert OSSL_PARAM_free(params);
69*e0c4386eSCy Schubert OSSL_PARAM_BLD_free(bld);
70*e0c4386eSCy Schubert BN_free(g);
71*e0c4386eSCy Schubert BN_free(q);
72*e0c4386eSCy Schubert BN_free(p);
73*e0c4386eSCy Schubert
74*e0c4386eSCy Schubert return rv;
75*e0c4386eSCy Schubert }
76