Path: blob/main/crypto/openssl/demos/pkey/EVP_PKEY_DSA_paramfromdata.c
34907 views
/*-1* Copyright 2022-2023 The OpenSSL Project Authors. All Rights Reserved.2*3* Licensed under the Apache License 2.0 (the "License"). You may not use4* this file except in compliance with the License. You can obtain a copy5* in the file LICENSE in the source distribution or at6* https://www.openssl.org/source/license.html7*/89/*10* Example showing how to load DSA params from raw data11* using EVP_PKEY_fromdata()12*/1314#include <openssl/param_build.h>15#include <openssl/evp.h>16#include <openssl/core_names.h>17#include "dsa.inc"1819int main(int argc, char **argv)20{21int ret = EXIT_FAILURE;22OSSL_LIB_CTX *libctx = NULL;23const char *propq = NULL;24EVP_PKEY_CTX *ctx = NULL;25EVP_PKEY *dsaparamkey = NULL;26OSSL_PARAM_BLD *bld = NULL;27OSSL_PARAM *params = NULL;28BIGNUM *p = NULL, *q = NULL, *g = NULL;2930p = BN_bin2bn(dsa_p, sizeof(dsa_p), NULL);31q = BN_bin2bn(dsa_q, sizeof(dsa_q), NULL);32g = BN_bin2bn(dsa_g, sizeof(dsa_g), NULL);33if (p == NULL || q == NULL || g == NULL)34goto cleanup;3536/* Use OSSL_PARAM_BLD if you need to handle BIGNUM Parameters */37bld = OSSL_PARAM_BLD_new();38if (bld == NULL)39goto cleanup;40if (!OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_P, p)41|| !OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_Q, q)42|| !OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_G, g))43goto cleanup;44params = OSSL_PARAM_BLD_to_param(bld);45if (params == NULL)46goto cleanup;4748ctx = EVP_PKEY_CTX_new_from_name(libctx, "DSA", propq);49if (ctx == NULL) {50fprintf(stderr, "EVP_PKEY_CTX_new_from_name() failed\n");51goto cleanup;52}5354if (EVP_PKEY_fromdata_init(ctx) <= 055|| EVP_PKEY_fromdata(ctx, &dsaparamkey, EVP_PKEY_KEY_PARAMETERS, params) <= 0) {56fprintf(stderr, "EVP_PKEY_fromdata() failed\n");57goto cleanup;58}5960if (!dsa_print_key(dsaparamkey, 0, libctx, propq))61goto cleanup;6263ret = EXIT_SUCCESS;64cleanup:65EVP_PKEY_free(dsaparamkey);66EVP_PKEY_CTX_free(ctx);67OSSL_PARAM_free(params);68OSSL_PARAM_BLD_free(bld);69BN_free(g);70BN_free(q);71BN_free(p);7273return ret;74}757677