1.等同于使用: openssl req -new -key "key_path" -out "save_path" -subj "/emailAddress=email/CN=name/C=country"

2.代码如下:keyFilePath为RSA private key 文件。

bool MakeCsrSSL(const  char * keyFilePath, const  char *email, const  char *name, const  char *country, const  char *saveCsrFilePath) {
int ret = ;
RSA *r = NULL;
BIGNUM *bne = NULL; int nVersion = ;
int bits = ;
unsigned long e = RSA_F4; X509_REQ *x509_req = NULL;
X509_NAME *x509_name = NULL;
EVP_PKEY *pKey = NULL;
RSA *tem = NULL;
BIO *out = NULL, *keyFileBIO = NULL;
FILE *pubKeyFile = NULL; if (strlen(saveCsrFilePath) == ) {
fprintf(stderr, "MakeLocalCsrSSLApi save path is empty\n");
return false;
} //not exists public key file, create one immediately.
if (strlen(keyFilePath) == ) {
// 1. generate rsa key
bne = BN_new();
ret = BN_set_word(bne, e);
if (ret != ) {
fprintf(stderr, "MakeLocalCsrSSLApi BN_set_word err\n");
goto free_all;
} r = RSA_new();
ret = RSA_generate_key_ex(r, bits, bne, NULL);
if (ret != ) {
fprintf(stderr, "MakeLocalCsrSSLApi RSA_generate_key_ex err\n");
goto free_all;
}
} else { //open it
pubKeyFile = fopen(keyFilePath, "r");
if (pubKeyFile == NULL) {
fprintf(stderr, "MakeLocalCsrSSLApi opening file %s err\n", keyFilePath);
goto free_all;
} keyFileBIO = BIO_new_file(keyFilePath, "r");
if (keyFileBIO == NULL) {
fprintf(stderr, "MakeLocalCsrSSLApi BIO_new_file err %s\n", keyFilePath);
goto free_all;
} r = PEM_read_bio_RSAPrivateKey(keyFileBIO, NULL, NULL, NULL);
if (r == NULL) {
fprintf(stderr, "MakeLocalCsrSSLApi PEM_read_bio_RSAPrivateKey err\n");
goto free_all;
} /*
//从csr文件中获取私钥
BIO* bio = bio_open_default(csrFilePath, "r", 1);
r = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL);
if (r == NULL) {
fprintf(stderr, "Error PEM_read_RSAPublicKey file %s\n", savePrivateKeyFilePath);
return false;
}*/
} // 2. set version of x509 req
x509_req = X509_REQ_new();
ret = X509_REQ_set_version(x509_req, nVersion);
if (ret != ) {
fprintf(stderr, "MakeLocalCsrSSLApi X509_REQ_set_version err\n");
goto free_all;
} // 3. set subject of x509 req
x509_name = X509_REQ_get_subject_name(x509_req); //x509_req->req_info.subject; ret = X509_NAME_add_entry_by_txt(x509_name, "emailAddress", MBSTRING_ASC, (const unsigned char*)email, -, -, );
if (ret != ) {
fprintf(stderr, "MakeLocalCsrSSLApi X509_NAME_add_entry_by_txt emailAddress err\n");
goto free_all;
} ret = X509_NAME_add_entry_by_txt(x509_name, "CN", MBSTRING_ASC, (const unsigned char*)name, -, -, );
if (ret != ) {
fprintf(stderr, "MakeLocalCsrSSLApi X509_NAME_add_entry_by_txt CN err\n");
goto free_all;
} ret = X509_NAME_add_entry_by_txt(x509_name, "C", MBSTRING_ASC, (const unsigned char*)country, -, -, );
if (ret != ) {
fprintf(stderr, "MakeLocalCsrSSLApi X509_NAME_add_entry_by_txt C err\n");
goto free_all;
} // 4. set public key of x509 req
pKey = EVP_PKEY_new();
EVP_PKEY_assign_RSA(pKey, r);
r = NULL; // will be free rsa when EVP_PKEY_free(pKey) ret = X509_REQ_set_pubkey(x509_req, pKey);
if (ret != ) {
fprintf(stderr, "MakeLocalCsrSSLApi X509_REQ_set_pubkey err\n");
goto free_all;
} // 5. set sign key of x509 req
ret = X509_REQ_sign(x509_req, pKey, EVP_sha1()); // return x509_req->signature->length
if (ret <= ) {
fprintf(stderr, "MakeLocalCsrSSLApi X509_REQ_sign err\n");
goto free_all;
} out = BIO_new_file(saveCsrFilePath, "w");
ret = PEM_write_bio_X509_REQ(out, x509_req); // 6. free
free_all:
BIO_free_all(keyFileBIO);
X509_REQ_free(x509_req);
BIO_free_all(out); EVP_PKEY_free(pKey);
BN_free(bne);
if (pubKeyFile) fclose(pubKeyFile); return (ret == );
}

以上。

《C++ OpenSSL 之一:编译和使用》
《C++ OpenSSL 之二:生成RSA文件》
《C++ OpenSSL 之三:生成CSR文件》
《C++ OpenSSL 之四:CER转换为PEM》
《C++ OpenSSL 之五:生成P12文件

C++ OpenSSL 之三:生成CSR文件的更多相关文章

  1. 使用OpenSSL生成CSR文件,并申请全球通用SSL证书

    http://www.openssl.org 上只有OpenSSL的原代码下载,为了方便Windows用户使用OpenSSL,我们特地为您准备了OpenSSL 0.9.8.a for win32的可执 ...

  2. 如何制作CSR文件?

    如何制作CSR文件? 在申请数字证书之前,您必须先生成证书私钥和证书请求文件(CSR,Cerificate Signing Request),CSR是您的公钥证书原始文件,包含了您的服务器信息和您的单 ...

  3. C++ OpenSSL 之二:生成RSA文件

    1.等同于生成private key: openssl genrsa -out "save_path" 2048 2.代码如下 bool MakeRsaKeySSL(const c ...

  4. C++ OpenSSL 之五:生成P12文件

    1.等同于使用: openssl pkcs12 -export -inkey "key_path" -in "pem_path" -out "save ...

  5. openssl 生成CSR

    openssl 生成CSR 2013-12-27 15:05 3699人阅读 评论(1) 收藏 举报  分类: Security(38)  C/C++(105)  版权声明:本文为博主原创文章,未经博 ...

  6. 如何使用OpenSSL工具生成根证书与应用证书

    如何使用OpenSSL工具生成根证书与应用证书 一.步骤简记 // 生成顶级CA的公钥证书和私钥文件,有效期10年(RSA 1024bits,默认) openssl req -new -x509 -d ...

  7. 使用INF创建CSR文件

    公司要为一个英国的客户提供由HTTP升级到HTTPS的服务,于是接触到了申请SSL证书这方面的内容. 一.总的来说,申请证书需要两步,一是创建CSR文件,二是在证书提供商购买证书并将CSR文件发给证书 ...

  8. 用OpenSSL命令行生成证书文件

    用OpenSSL命令行生成证书文件 1.首先要生成服务器端的私钥(key文件): openssl genrsa -des3 -out server.key 1024 运行时会提示输入密码,此密码用于加 ...

  9. 利用keytool、openssl生成证书文件

    转载请标明出处:http://blog.csdn.net/shensky711/article/details/52225073 本文出自: [HansChen的博客] 用openssl指令逐步生成各 ...

随机推荐

  1. Zookeeper注册中心搭建-单机版(三)

    Zookeeper是一个分布式协调组件,本质是一个软件. Zookeeper常用的功能有: 发布订阅功能,把 zookeeper 当作注册中心的原因. 分布式/集群管理功能 Zookeeper是Jav ...

  2. windows上传文件到 linux的hdfs

    一.windows上传文件到 linux的hdfs 1.先在 centos 上开启 hdfs, 用 jps 可以看到下面信息, 说明完成开启 2.在win上配置 hadoop (https://www ...

  3. cookie 用户认证

    客户端浏览器上的一个文件  可认为是键值对集合 基于浏览器的功能  可以实现一个用户验证的功能 因为要在页面上显示当前用户的信息 修改 写index urls 运行直接输入index时 会自动进入lo ...

  4. GAN与VAE

    经典算法·GAN与VAE Generative Adversarial Networks 及其变体 生成对抗网络是近几年最为经典的生成模型的代表工作,Goodfellow的经典工作.通过两个神经网络结 ...

  5. 【PL/SQL】PLSQL Developer注册码

    [12.0.7] product code: 4vkjwhfeh3ufnqnmpr9brvcuyujrx3n3le serial Number:226959 password: xs374ca

  6. Vue实际中的应用开发【分页效果与购物车】

    作者 | Jeskson 来源 | 达达前端小酒馆 分页组件 首先来创建项目: 分页组件,做项目不要写动手写代码,要想想业务逻辑,怎么写,如何写才是最好的呈现方式,做项目不急,要先想好整体的框架,从底 ...

  7. [LeetCode] 926. Flip String to Monotone Increasing 翻转字符串到单调递增

    A string of '0's and '1's is monotone increasing if it consists of some number of '0's (possibly 0), ...

  8. [LeetCode] 152. Maximum Product Subarray 求最大子数组乘积

    Given an integer array nums, find the contiguous subarray within an array (containing at least one n ...

  9. docker for windows添加卷映射

    docker settings->share drivers 设置共享目录 启动docker时-v 指定目录 ··· docker run -v /d/temp:/app -it --rm co ...

  10. iphone 移动端操作记录

    iPhone和Safari浏览器的后退按钮操作,是直接载入缓存中的页面,不会加载js文件,不会执行ready,onload函数,但是加载html页面会跑pageshow事件,因此有回退动作需要重新加载 ...