openssl 生成CSR
C/C++(105) 
版权声明:本文为博主原创文章,未经博主允许不得转载。
将openssl如何生成CSR写了一上DEMO,支持扩展属性,同时增加了通过DN字符串转X509_NAME的方法。
- #include <string.h>
- #include <openssl/x509.h>
- #include <openssl/rsa.h>
- #pragma comment(lib, "libeay32.lib")
- /*
- * subject is expected to be in the format /type0=value0/type1=value1/type2=...
- * where characters may be escaped by \
- */
- X509_NAME *parse_name(char *subject, long chtype, int multirdn)
- {
- size_t buflen = strlen(subject)+1; /* to copy the types and values into. due to escaping, the copy can only become shorter */
- char *buf = OPENSSL_malloc(buflen);
- size_t max_ne = buflen / 2 + 1; /* maximum number of name elements */
- char **ne_types = OPENSSL_malloc(max_ne * sizeof (char *));
- char **ne_values = OPENSSL_malloc(max_ne * sizeof (char *));
- int *mval = OPENSSL_malloc (max_ne * sizeof (int));
- char *sp = subject, *bp = buf;
- int i, ne_num = 0;
- X509_NAME *n = NULL;
- int nid;
- if (!buf || !ne_types || !ne_values || !mval)
- {
- //BIO_printf(bio_err, "malloc error\n");
- goto error;
- }
- if (*subject != '/')
- {
- //BIO_printf(bio_err, "Subject does not start with '/'.\n");
- goto error;
- }
- sp++; /* skip leading / */
- /* no multivalued RDN by default */
- mval[ne_num] = 0;
- while (*sp)
- {
- /* collect type */
- ne_types[ne_num] = bp;
- while (*sp)
- {
- if (*sp == '\\') /* is there anything to escape in the type...? */
- {
- if (*++sp)
- *bp++ = *sp++;
- else
- {
- //BIO_printf(bio_err, "escape character at end of string\n");
- goto error;
- }
- }
- else if (*sp == '=')
- {
- sp++;
- *bp++ = '\0';
- break;
- }
- else
- *bp++ = *sp++;
- }
- if (!*sp)
- {
- //BIO_printf(bio_err, "end of string encountered while processing type of subject name element #%d\n", ne_num);
- goto error;
- }
- ne_values[ne_num] = bp;
- while (*sp)
- {
- if (*sp == '\\')
- {
- if (*++sp)
- *bp++ = *sp++;
- else
- {
- //BIO_printf(bio_err, "escape character at end of string\n");
- goto error;
- }
- }
- else if (*sp == '/')
- {
- sp++;
- /* no multivalued RDN by default */
- mval[ne_num+1] = 0;
- break;
- }
- else if (*sp == '+' && multirdn)
- {
- /* a not escaped + signals a mutlivalued RDN */
- sp++;
- mval[ne_num+1] = -1;
- break;
- }
- else
- *bp++ = *sp++;
- }
- *bp++ = '\0';
- ne_num++;
- }
- if (!(n = X509_NAME_new()))
- goto error;
- for (i = 0; i < ne_num; i++)
- {
- if ((nid=OBJ_txt2nid(ne_types[i])) == NID_undef)
- {
- //BIO_printf(bio_err, "Subject Attribute %s has no known NID, skipped\n", ne_types[i]);
- continue;
- }
- if (!*ne_values[i])
- {
- //BIO_printf(bio_err, "No value provided for Subject Attribute %s, skipped\n", ne_types[i]);
- continue;
- }
- if (!X509_NAME_add_entry_by_NID(n, nid, chtype, (unsigned char*)ne_values[i], -1,-1,mval[i]))
- goto error;
- }
- OPENSSL_free(ne_values);
- OPENSSL_free(ne_types);
- OPENSSL_free(buf);
- OPENSSL_free(mval);
- return n;
- error:
- X509_NAME_free(n);
- if (ne_values)
- OPENSSL_free(ne_values);
- if (ne_types)
- OPENSSL_free(ne_types);
- if (mval)
- OPENSSL_free(mval);
- if (buf)
- OPENSSL_free(buf);
- return NULL;
- }
- X509_NAME *CreateDN(char *pbEmail, char *pbCN, char *pbOU, char *pbO, char *pbL, char *pbST, char *pbC)
- {
- X509_NAME *pX509Name = NULL;
- if(pbCN == NULL)
- {
- return NULL;
- }
- if (!(pX509Name = X509_NAME_new()))
- {
- return NULL;
- }
- X509_NAME_add_entry_by_txt(pX509Name, "emailAddress", V_ASN1_UTF8STRING, pbEmail, -1, -1, 0);
- X509_NAME_add_entry_by_txt(pX509Name, "CN", V_ASN1_UTF8STRING, pbCN, -1, -1, 0);
- X509_NAME_add_entry_by_txt(pX509Name, "OU", V_ASN1_UTF8STRING, pbOU, -1, -1, 0);
- X509_NAME_add_entry_by_txt(pX509Name, "O", V_ASN1_UTF8STRING, pbO, -1, -1, 0);
- X509_NAME_add_entry_by_txt(pX509Name, "L", V_ASN1_UTF8STRING, pbL, -1, -1, 0);
- X509_NAME_add_entry_by_txt(pX509Name, "ST", V_ASN1_UTF8STRING, pbST, -1, -1, 0);
- X509_NAME_add_entry_by_txt(pX509Name, "C", V_ASN1_UTF8STRING, pbC, -1, -1, 0);
- return pX509Name;
- }
- int GenCSR(char *pbDN, int nDNLen, char *pCSR, size_t nCSRSize)
- {
- char szAltName[] = "DNS:www.jinhill.com";
- char szComment[] = "Create by Jinhill";
- char szKeyUsage[] = "digitalSignature, nonRepudiation";
- char szExKeyUsage[] = "serverAuth, clientAuth";
- X509_REQ *pX509Req = NULL;
- int iRV = 0;
- long lVer = 3;
- X509_NAME *pX509DN = NULL;
- EVP_PKEY *pEVPKey = NULL;
- RSA *pRSA = NULL;
- X509_NAME_ENTRY *pX509Entry = NULL;
- char szBuf[255] = {0};
- char mdout[20];
- int nLen, nModLen;
- int bits = 2048;
- unsigned long E = RSA_3;
- unsigned char *pDer = NULL;
- unsigned char *p = NULL;
- FILE *fp = NULL;
- const EVP_MD *md = NULL;
- X509 *pX509 = NULL;
- BIO *pBIO = NULL;
- BIO *pPemBIO = NULL;
- BUF_MEM *pBMem = NULL;
- //STACK_OF(X509_EXTENSION) *pX509Ext;
- if(pbDN == NULL)
- {
- return -1;
- }
- pX509DN = parse_name(pbDN, V_ASN1_UTF8STRING, 0);
- pX509Req = X509_REQ_new();
- iRV = X509_REQ_set_version(pX509Req, lVer);
- // subject pX509Name
- iRV = X509_REQ_set_subject_name(pX509Req, pX509DN);
- /* pub key */
- pEVPKey = EVP_PKEY_new();
- pRSA = RSA_generate_key(bits, E, NULL, NULL);
- EVP_PKEY_assign_RSA(pEVPKey, pRSA);
- iRV = X509_REQ_set_pubkey(pX509Req, pEVPKey);
- /* attribute */
- strcpy(szBuf, szAltName);
- nLen = strlen(szBuf);
- iRV = X509_REQ_add1_attr_by_txt(pX509Req, "subjectAltName", V_ASN1_UTF8STRING, szBuf, nLen);
- strcpy(szBuf, szKeyUsage);
- nLen = strlen(szBuf);
- iRV = X509_REQ_add1_attr_by_txt(pX509Req, "keyUsage", V_ASN1_UTF8STRING, szBuf, nLen);
- strcpy(szBuf, szExKeyUsage);
- nLen = strlen(szBuf);
- iRV = X509_REQ_add1_attr_by_txt(pX509Req, "extendedKeyUsage", V_ASN1_UTF8STRING, szBuf, nLen);
- strcpy(szBuf, szComment);
- nLen = strlen(szBuf);
- iRV = X509_REQ_add1_attr_by_txt(pX509Req, "nsComment", V_ASN1_UTF8STRING, szBuf, nLen);
- md = EVP_sha1();
- iRV = X509_REQ_digest(pX509Req, md, mdout, &nModLen);
- iRV = X509_REQ_sign(pX509Req, pEVPKey, md);
- if(!iRV)
- {
- printf("sign err!\n");
- X509_REQ_free(pX509Req);
- return -1;
- }
- // 写入文件PEM格式
- // pBIO = BIO_new_file("certreq.txt", "w");
- // PEM_write_bio_X509_REQ(pBIO, pX509Req, NULL, NULL);
- // BIO_free(pBIO);
- //返回PEM字符
- pPemBIO = BIO_new(BIO_s_mem());
- PEM_write_bio_X509_REQ(pPemBIO, pX509Req, NULL, NULL);
- BIO_get_mem_ptr(pPemBIO,&pBMem);
- if(pBMem->length <= nCSRSize)
- {
- memcpy(pCSR, pBMem->data, pBMem->length);
- }
- BIO_free(pPemBIO);
- /* DER编码 */
- //nLen = i2d_X509_REQ(pX509Req, NULL);
- //pDer = (unsigned char *)malloc(nLen);
- //p = pDer;
- //nLen = i2d_X509_REQ(pX509Req, &p);
- //free(pDer);
- // 验证CSR
- OpenSSL_add_all_algorithms();
- iRV = X509_REQ_verify(pX509Req, pEVPKey);
- if(iRV<0)
- {
- printf("verify err.\n");
- }
- X509_REQ_free(pX509Req);
- return nCSRSize;
- }
- int main()
- {
- char chDN[255] = "/CN=www.jinhill.com/O=Beijing Jinhill Inc./C=CN";
- char chCSR[2048] = {0};
- int rv = GenCSR(chDN, strlen(chDN), chCSR, sizeof(chCSR));
- printf("CSR:\n%s", chCSR);
- }
openssl 生成CSR的更多相关文章
- 使用OpenSSL生成CSR文件,并申请全球通用SSL证书
http://www.openssl.org 上只有OpenSSL的原代码下载,为了方便Windows用户使用OpenSSL,我们特地为您准备了OpenSSL 0.9.8.a for win32的可执 ...
- linux下使用openssl生成 csr crt CA证书
证书文件生成:一.服务器端1.生成服务器端 私钥(key文件);openssl genrsa -des3 -out server.key 1024运行时会提示输入密码,此密码用于加密key文件( ...
- C++ OpenSSL 之三:生成CSR文件
1.等同于使用: openssl req -new -key "key_path" -out "save_path" -subj "/emailAdd ...
- 对称、非对称加密算,openssl生成证书(笔记)
对称加密算法 1.密钥只有一个,加密和解密都需要同一个密钥2.DES,IDEA,AES3.明文+密钥=密文, 密文+密钥=明文4.加密速度快,系统开销小,适用大量数据的加密 非对称加密算法1.密钥由公 ...
- 使用 openssl 生成证书
一.openssl 简介 目前最流行的 SSL 密码库工具官网:https://www.openssl.org/source/ 构成部分 密码算法库 密钥和证书封装管理功能 SSL通信API接口 用途 ...
- openssl生成ssl证书
openssl生成ssl证书 x509证书一般会用到三类文,key,csr,crt. Key 是私用密钥openssl格,通常是rsa算法. Csr 是证书请求文件,用于申请证书.在制作csr文件的时 ...
- CentOS6系统openssl生成证书和自签证书
CentOS6系统openssl生成证书和自签证书的过程,记录一下,本文基于CentOS 6 64bit.$ yum install openssl openssl-devel 1,生成服务器端的私钥 ...
- 用Keytool和OpenSSL生成和签发数字证书
一)keytool生成私钥文件(.key)和签名请求文件(.csr),openssl签发数字证书 J2SDK在目录%JAVA_HOME%/bin提供了密钥库管理工具Keytool,用于管理密 ...
- 使用OpenSSL生成证书
使用OpenSSL生成证书 下载安装openssl,进入/bin/下面,执行命令(把ssl目录下的openssl.cnf 拷贝到bin目录下)1.首先要生成服务器端的私钥(key文件):openssl ...
随机推荐
- Jquery_Ajax文件上传
如何实现jQuery的Ajax文件上传,PHP如实文件上传.AJAX上传文件,PHP上传文件. [PHP文件上传] 在开始之前,我觉得是有必要把通WEB上传文件的原理简单说一下的.实际上,在这里不管是 ...
- ARC 工作原理
自动引用计数(Automatic Reference Counting),是一个编译期间工作的能够帮你管理内存的技术. ARC在编译期间为每个Objective-C指针变量添加合适的retain, r ...
- (转)Apache+Tomcat集群配置
本文Apache+Tomcat集群配置 基于最新的Apache和Tomcat,具体是2011年4月20日最新的Tomcat和Apache集群和负载均衡配置. 准备环境 Apache Apache是ht ...
- [转]Laravel 4之验证
Laravel 4之验证 http://dingjiannan.com/2013/laravel-validation/ 基本验证 使用Validator::make($data, $rules)验证 ...
- Dynamics CRM2013 missing prvReadComplexControl privilege
左右ComplexControl 权限设置,SDK例如,在以下的说明,仅供内部使用的实体,但是你可以没有找到这个叫配置安全角色ComplexControl的东西的. 在msdn上面查下就会发现这么一段 ...
- Linux下安装软件的错误
1. make configure GEN configure/bin/sh: 1: autoconf: not foundmake: *** [configure] Error 127 解决:sud ...
- 同一DataTable下创建多个结构数据相同的DataView的小问题
昨天在根据经理的要求修改公司后台的时候,遇到了一个很奇怪的问题 DataView dvFocus = ]); DataView dvLook = ]); DataView dvNewUser = ]) ...
- jquery mouseout和mouseleave区别
mouseout()和mouseleave() 都是鼠标移出元素时触发,但是这两者又有所区别,需要大家留意: 不论鼠标指针离开指定元素还是该元素子元素,都会触发 mouseout 事件. 只有在鼠标指 ...
- jquery列表动画
//新闻导航 (function (){ //获取分类名称 var _text = $('.news .news-wrapper .news-left .news-left-title .positi ...
- Android5.0+(CollapsingToolbarLayout)
CollapsingToolbarLayout作用是提供了一个可以折叠的Toolbar,它继承至FrameLayout,给它设置layout_scrollFlags,它可以控制包含在Collapsin ...