概述:
本文基于本人之前的一篇文章的延伸,调用openssl api函数C代码生成证书:https://blog.csdn.net/shenweihong/article/details/125140149,
本文使用的公钥类型RSA,签名私钥类型也是RSA
软件环境:
openssl:版本1.1.1k,编译好windows x86静态库
visual stdio:版本2010
Keystore Explorer:版本5.5.2,用于查看确认证书的
CSR组成:
如下图为用Keystore Explorer查看csr文件截图:
Formate:固定的,PKCS10的格式
Subject:主题名,后面的代码参数设置
Public Key:公钥,后面的代码参数设置
Signature Algorithm:签名算法,后面的代码参数设置
生成代码:
#pragma comment(lib, "Crypt32.lib")
#pragma comment(lib, "compile_win_x86_lib\\lib\\libcrypto.lib")
#pragma comment(lib, "compile_win_x86_lib\\lib\\libssl.lib")
#include "openssl/rsa.h"
#include "openssl/pem.h"
#include "openssl/x509v3.h"
int GenX509Req(char* subjectCN, char* subjectOU, char* subjectO, char* subjectL, char* subjectST, char* subjectC,
char* cerPubE, char* cerPubN, char* signE, char* signN, char* signD, char* fileName)
{
int re = 0;
X509_REQ *req = NULL;
X509_NAME* x509Name = NULL;
RSA* rsaPubCtx = NULL;
RSA* rsaSignCtx = NULL;
EVP_PKEY* pPubKey = NULL;
EVP_PKEY* pSignKey = NULL;
BIGNUM* bigE = NULL;
BIGNUM* bigN = NULL;
BIGNUM* bigD = NULL;
FILE* pFile = NULL;
rsaPubCtx = RSA_new();
pPubKey = EVP_PKEY_new();
bigE = BN_new();
bigN = BN_new();
BN_hex2bn(&bigE, cerPubE);
BN_hex2bn(&bigN, cerPubN);
RSA_set0_key(rsaPubCtx, bigN, bigE, NULL);
EVP_PKEY_set1_RSA(pPubKey, rsaPubCtx);
rsaSignCtx = RSA_new();
pSignKey = EVP_PKEY_new();
bigE = BN_new();
bigN = BN_new();
bigD = BN_new();
BN_hex2bn(&bigE, signE);
BN_hex2bn(&bigN, signN);
BN_hex2bn(&bigD, signD);
RSA_set0_key(rsaSignCtx, bigN, bigE, bigD);
EVP_PKEY_set1_RSA(pSignKey, rsaSignCtx);
req = X509_REQ_new();
x509Name = X509_REQ_get_subject_name(req);
X509_NAME_add_entry_by_txt(x509Name, "CN", MBSTRING_ASC, (unsigned char*)subjectCN, -1, -1, 0);
X509_NAME_add_entry_by_txt(x509Name, "OU", MBSTRING_ASC, (unsigned char*)subjectOU, -1, -1, 0);
X509_NAME_add_entry_by_txt(x509Name, "O", MBSTRING_ASC, (unsigned char*)subjectO, -1, -1, 0);
X509_NAME_add_entry_by_txt(x509Name, "L", MBSTRING_ASC, (unsigned char*)subjectL, -1, -1, 0);
X509_NAME_add_entry_by_txt(x509Name, "ST", MBSTRING_ASC, (unsigned char*)subjectST, -1, -1, 0);
X509_NAME_add_entry_by_txt(x509Name, "C", MBSTRING_ASC, (unsigned char*)subjectC, -1, -1, 0);
X509_REQ_set_pubkey(req, pPubKey);
X509_REQ_sign(req, pSignKey, EVP_sha256());
if (fileName != NULL) {
pFile = fopen(fileName, "w");
if (pFile == NULL) {
re = -1;
printf("文件打开失败\n");
goto EXIT_FUN;
}
PEM_write_X509_REQ(pFile, req);
fclose(pFile);
}
re = 0;
EXIT_FUN:
if (pFile != NULL) {
fclose(pFile);
pFile = NULL;
}
RSA_free(rsaPubCtx);
RSA_free(rsaSignCtx);
X509_REQ_free(req);
return re;
}
测试代码:
void main()
{
/*
pub:
N:D394BA47C0AFDA2CDB41E5B32B815934955F3FCF8FBCAE8D82D6C482C72F7062280F8723F17F6D101EE2133F27FE0C417EA3A3ADCAEAEF3439AF74D49F3BA68B90CFA7602B16B4CEFDA865555F681AD0DEBBBFC1FA03B908CF5BB802357AE9FB5A873F2764747431F7885250016AD16864C9BB1F2A8FEC0E32CBF96376445F63D896C3DEFBC81A01E5A630E05127A2F30A75F9C8336A19868FFDA65AC3D64A2126F53233734C0DFAD5A2C7E9BA1EBD65DBD34C9F3BF86F33619983FF9B945C22695E940A8C157DAB39D3C795606FCF9BB727A7F67E7465DE428CE450620FEA71BF98AC67B4F20133B8C69C978CD6CE4B5E775C9DF65DC1712B42F99E76CDBDD7
D:047DA0A28B3D704509765198C6F09CE317D488B73016D221A0C84C2D766AD458D0FEDB6070ECC274D822F6439DE6AF7DE86132C52A8C67FD54C81B93B3B981646EF3695B0A2F16315A82EB7BAF65B743FDA64080AE7CCBBD9B21183BBBC4DB1881A86337C3D310F04A8FB3278D9025D7B063F3BBCA9C796727BEDA9138DE64A3E0FD0B10BA1E59A016DBEDB5596120B96B9BDA39362D25C1607C6879A1ECA5518A828392616EDFF79ACCBF325313835E14F1F78CF00A7093768CCE22B44FF5F1D37C35B8191A144C7F6CDDBD9322236B134189B4A93F635AF8901A0679D45937753E15F656C70D14FCBD37D2E7AF2B11AD83AD8A3F136448BAF72BCE8D9F0759
*/
/*
sign:
N:A7A75DED6CE80AF530BCF58C69262E722400C0AB82CCE92241C937E1F8F60E216441FE63E4468141237914368CA12F790E3AD9D9FAB109DE9842A24A97C18417CB1AB00D32EB9E2242682D29C873AD04E2898C1299D30015D1F4C68B2B7402F1D8D020A34085680D95C9F4557B145FDAA63115659B3ECA1C2CB5F5FF8A4BDA1364EF70589FDD46D5E50FA08DFB66DD4FD1C751F1F22B91A13AC4C10639AC5A067C0A86D67D2B6AF9B47DE60704699BB6F59C87D2B305760B9366EAA995602E84C78BCCCD85BB59F2C8A5421415E06767C1A7F73FBFE44ED0987BE1283719D20844D9AA1D86DAB2337F80B333ABDFDD490586342915CF542796E945D22A0EDCFF
D:73D3583BF89A933F73ED8802FA7E0B6685E313392AC3A0692F63F1BCA7806EA0B92032BD166F734AEA53F31800AE371AFE2EC0652D21F47C995AA7BEBAFB51E412489CCA719C808290B86243549E3E317AC4A6460F89CBAC4782414CD2B772755D61E4BA848FE6244490E2C1CFA772D59F2D97E7BB8A749D64724DC2BEEBCA7BAF64B4C59B1FE9680BEF66A6469CD9544EEDCB8B34E60F06082951D5ABCA95441E00D5984879E6B774D7A63E0AA160EC9C2AA5E3FA0948C8296E500F406EAF74EBDDC31D738565C4362330D28389BCA52E72964A7AE4FA21655D286FFEB50020D079B273FA7D90D3D8B2AFA9958499D79D993BE14AD5692DA70607411BA3C279
*/
GenX509Req("11", "22", "33", "44", "55", "66",
"010001",
"D394BA47C0AFDA2CDB41E5B32B815934955F3FCF8FBCAE8D82D6C482C72F7062280F8723F17F6D101EE2133F27FE0C417EA3A3ADCAEAEF3439AF74D49F3BA68B90CFA7602B16B4CEFDA865555F681AD0DEBBBFC1FA03B908CF5BB802357AE9FB5A873F2764747431F7885250016AD16864C9BB1F2A8FEC0E32CBF96376445F63D896C3DEFBC81A01E5A630E05127A2F30A75F9C8336A19868FFDA65AC3D64A2126F53233734C0DFAD5A2C7E9BA1EBD65DBD34C9F3BF86F33619983FF9B945C22695E940A8C157DAB39D3C795606FCF9BB727A7F67E7465DE428CE450620FEA71BF98AC67B4F20133B8C69C978CD6CE4B5E775C9DF65DC1712B42F99E76CDBDD7",
"010001",
"A7A75DED6CE80AF530BCF58C69262E722400C0AB82CCE92241C937E1F8F60E216441FE63E4468141237914368CA12F790E3AD9D9FAB109DE9842A24A97C18417CB1AB00D32EB9E2242682D29C873AD04E2898C1299D30015D1F4C68B2B7402F1D8D020A34085680D95C9F4557B145FDAA63115659B3ECA1C2CB5F5FF8A4BDA1364EF70589FDD46D5E50FA08DFB66DD4FD1C751F1F22B91A13AC4C10639AC5A067C0A86D67D2B6AF9B47DE60704699BB6F59C87D2B305760B9366EAA995602E84C78BCCCD85BB59F2C8A5421415E06767C1A7F73FBFE44ED0987BE1283719D20844D9AA1D86DAB2337F80B333ABDFDD490586342915CF542796E945D22A0EDCFF",
"73D3583BF89A933F73ED8802FA7E0B6685E313392AC3A0692F63F1BCA7806EA0B92032BD166F734AEA53F31800AE371AFE2EC0652D21F47C995AA7BEBAFB51E412489CCA719C808290B86243549E3E317AC4A6460F89CBAC4782414CD2B772755D61E4BA848FE6244490E2C1CFA772D59F2D97E7BB8A749D64724DC2BEEBCA7BAF64B4C59B1FE9680BEF66A6469CD9544EEDCB8B34E60F06082951D5ABCA95441E00D5984879E6B774D7A63E0AA160EC9C2AA5E3FA0948C8296E500F406EAF74EBDDC31D738565C4362330D28389BCA52E72964A7AE4FA21655D286FFEB50020D079B273FA7D90D3D8B2AFA9958499D79D993BE14AD5692DA70607411BA3C279",
"123456.csr"
);
}