openssl3.2 - 官方demo学习 - pkey - EVP_PKEY_DSA_paramfromdata.c

文章目录

    • [openssl3.2 - 官方demo学习 - pkey - EVP_PKEY_DSA_paramfromdata.c](#openssl3.2 - 官方demo学习 - pkey - EVP_PKEY_DSA_paramfromdata.c)
    • 概述
    • 笔记
    • END

openssl3.2 - 官方demo学习 - pkey - EVP_PKEY_DSA_paramfromdata.c

概述

开源工程包含的头文件类型是.inc, 要重命名为.h, 否则没有c++头文件语法提示

从数组中载入大数p,q,g

建立ossl_param, 设置参数的p,q,g

用参数初始化_evp_pkey_ctx, 得到_evp_pkey_dsa

打印_evp_pkey_dsa的共有数据(p,q,g ...) + 参数

笔记

c 复制代码
/*!
\file EVP_PKEY_DSA_paramfromdata.c
\note
openssl3.2 - 官方demo学习 - pkey - EVP_PKEY_DSA_paramfromdata.c

开源工程包含的头文件类型是.inc, 要重命名为.h, 否则没有c++头文件语法提示

从数组中载入大数p,q,g
建立ossl_param, 设置参数的p,q,g 
用参数初始化_evp_pkey_ctx, 得到_evp_pkey_dsa
打印_evp_pkey_dsa的共有数据(p,q,g ...) + 参数
*/

/*-
 * Copyright 2022-2023 The OpenSSL Project Authors. All Rights Reserved.
 *
 * Licensed under the Apache License 2.0 (the "License").  You may not use
 * this file except in compliance with the License.  You can obtain a copy
 * in the file LICENSE in the source distribution or at
 * https://www.openssl.org/source/license.html
 */

 /*
  * Example showing how to load DSA params from raw data
  * using EVP_PKEY_fromdata()
  */

#include <openssl/param_build.h>
#include <openssl/evp.h>
#include <openssl/core_names.h>
#include "dsa.h"

#include "my_openSSL_lib.h"

int main(int argc, char** argv)
{
	int ret = EXIT_FAILURE;
	OSSL_LIB_CTX* _ossl_lib_ctx = NULL;
	const char* propq = NULL;
	EVP_PKEY_CTX* _evp_pkey_ctx = NULL;
	EVP_PKEY* _evp_pkey_dsa = NULL;
	OSSL_PARAM_BLD* _ossl_param_bld = NULL;
	OSSL_PARAM* _ossl_param = NULL;
	BIGNUM* p = NULL, * q = NULL, * g = NULL;

	p = BN_bin2bn(dsa_p, sizeof(dsa_p), NULL);
	q = BN_bin2bn(dsa_q, sizeof(dsa_q), NULL);
	g = BN_bin2bn(dsa_g, sizeof(dsa_g), NULL);
	if (p == NULL || q == NULL || g == NULL)
		goto cleanup;

	/* Use OSSL_PARAM_BLD if you need to handle BIGNUM Parameters */
	_ossl_param_bld = OSSL_PARAM_BLD_new();
	if (_ossl_param_bld == NULL)
		goto cleanup;
	if (!OSSL_PARAM_BLD_push_BN(_ossl_param_bld, OSSL_PKEY_PARAM_FFC_P, p)
		|| !OSSL_PARAM_BLD_push_BN(_ossl_param_bld, OSSL_PKEY_PARAM_FFC_Q, q)
		|| !OSSL_PARAM_BLD_push_BN(_ossl_param_bld, OSSL_PKEY_PARAM_FFC_G, g))
		goto cleanup;
	_ossl_param = OSSL_PARAM_BLD_to_param(_ossl_param_bld);
	if (_ossl_param == NULL)
		goto cleanup;

	_evp_pkey_ctx = EVP_PKEY_CTX_new_from_name(_ossl_lib_ctx, "DSA", propq);
	if (_evp_pkey_ctx == NULL) {
		fprintf(stderr, "EVP_PKEY_CTX_new_from_name() failed\n");
		goto cleanup;
	}

	if (EVP_PKEY_fromdata_init(_evp_pkey_ctx) <= 0
		|| EVP_PKEY_fromdata(_evp_pkey_ctx, &_evp_pkey_dsa, EVP_PKEY_KEY_PARAMETERS, _ossl_param) <= 0) {
		fprintf(stderr, "EVP_PKEY_fromdata() failed\n");
		goto cleanup;
	}

	if (!dsa_print_key(_evp_pkey_dsa, 0, _ossl_lib_ctx, propq))
		goto cleanup;

	ret = EXIT_SUCCESS;
cleanup:
	EVP_PKEY_free(_evp_pkey_dsa);
	EVP_PKEY_CTX_free(_evp_pkey_ctx);
	OSSL_PARAM_free(_ossl_param);
	OSSL_PARAM_BLD_free(_ossl_param_bld);
	BN_free(g);
	BN_free(q);
	BN_free(p);

	return ret;
}

END

相关推荐
石牌桥网管8 小时前
OpenSSL 生成根证书、中间证书和网站证书
网络协议·https·openssl
雨雪飘零8 小时前
Windows系统使用OpenSSL生成自签名证书
nginx·证书·openssl
许野平9 天前
OpenSSL:生成 DER 格式的 RSA 密钥对
服务器·网络·openssl·rsa·pem·der
Xnah_10 天前
ubuntu 20.4 安装 openssl 3.x
ubuntu·openssl
redwingz16 天前
openssl签名报错
openssl·random
Anlige1 个月前
PHP实现OPENSSL的EVP_BytesToKey
开发语言·php·openssl·evp
cooldream20091 个月前
升级 OpenSSL 的详细步骤(解决 SSH 漏洞的前提)
运维·ssh·openssl
年薪丰厚1 个月前
如何手动安装libcrypto.so.10和libssl.so.10这两个库?
openssl·libcrypto.so.10·libssl.so.10
恋喵大鲤鱼2 个月前
openssl(1) command
openssl
pzs02212 个月前
openssl的使用
openssl