基于 OpenSSL 的 CA 建立及证书签发
http://rhythm-zju.blog.163.com/blog/static/310042008015115718637/
建立 CA
建立 CA 目录结构
按照 OpenSSL 的默认配置建立 CA ,需要在文件系统中建立相应的目录结构。相关的配置内容一般位于 /usr/ssl/openssl.cnf 内,详情可参见 config (1) 。在终端中使用如下命令建立目录结构:
$ mkdir -p ./demoCA/{private,newcerts}
$ touch ./demoCA/index.txt
$ echo 01 > ./demoCA/serial
产生的目录结构如下:
.
`-- demoCA/
|-- index.txt
|-- newcerts/
|-- private/
`-- serial
生成 CA 证书的 RSA 密钥对
首先,我们要为 CA 建立 RSA 密钥对。打开终端,使用如下命令生成 RSA 密钥对:
$ openssl genrsa -des3 -out ./demoCA/private/cakey.pem 2048
参数解释
genrsa
用于生成 RSA 密钥对的 OpenSSL 命令。
-des3
使用 3-DES 对称加密算法加密密钥对,该参数需要用户在密钥生成过程中输入一个口令用于加密。今后使用该密钥对时,需要输入相应的口令。如果不加该选项,则不对密钥进行加密。
-out ./demoCA/private/cakey.pem
令生成的密钥对保存到文件 ./demoCA/private/cakey.pem 。
2048
RSA 模数位数,在一定程度上表征了密钥强度。
该命令输出如下,用户应输入自己的密钥口令并确认:
Generating RSA private key, 2048 bit long modulus
................................................+++
.........................+++
e is 65537 (0x10001)
Enter pass phrase for ./demoCA/private/cakey.pem:<enter your pass-phrase>
Verifying - Enter pass phrase for ./demoCA/private/cakey.pem:<re-enter your pass-phrase>
生成 CA 证书请求
为了获取一个 CA 根证书,我们需要先制作一份证书请求。先前生成的 CA 密钥对被用于对证书请求签名。
$ openssl req -new -days 365 -key ./demoCA/private/cakey.pem -out careq.pem
参数解释
req
用于生成证书请求的 OpenSSL 命令。
-new
生成一个新的证书请求。该参数将令 OpenSSL 在证书请求生成过程中要求用户填写一些相应的字段。
-days 365
从生成之时算起,证书时效为 365 天。
-key ./demoCA/private/cakey.pem
指定 ./demoCA/private/cakey.pem 为证书所使用的密钥对文件。
-out careq.pem
令生成的证书请求保存到文件 careq.pem 。
该命令将提示用户输入密钥口令并填写证书相关信息字段,输出如下:
Enter pass phrase for ./demoCA/private/cakey.pem:<enter you pass-phrase>
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [AU]:CN
State or Province Name (full name) [Some-State]:ZJ
Locality Name (eg, city) []:HZ
Organization Name (eg, company) [Internet Widgits Pty Ltd]:Some Ltd. Corp.
Organizational Unit Name (eg, section) []:Some Unit
Common Name (eg, YOUR name) []:Someone
Email Address []:some@email.com
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
对 CA 证书请求进行签名
在实际应用中,用户可以通过向知名 CA 递交证书请求来申请证书。但是在这里,我们需要建立的是一个根 CA ,只能由我们自己来对证书请求进行签名。所以我们让 OpenSSL 使用证书请求中附带的密钥对对该请求进行签名,也就是所谓的“ self sign ”:
$ openssl ca -selfsign -in careq.pem -out cacert.pem
参数解释
ca
用于执行 CA 相关操作的 OpenSSL 命令。
-selfsign
使用对证书请求进行签名的密钥对来签发证书。
-in careq.pem
指定 careq.pem 为证书请求文件。
-out ./demoCA/cacert.pem
指定 ./demoCA/cacert.pem 为输出的证书。
该命令要求用户输入密钥口令并输出相关证书信息,请求用户确认:
Using configuration from /usr/lib/ssl/openssl.cnf
Enter pass phrase for ./demoCA/private/cakey.pem:<enter your pass-phrase>
Check that the request matches the signature
Signature ok
Certificate Details:
Serial Number: 2 (0x2)
Validity
Not Before: Jan 16 13:05:09 2008 GMT
Not After : Jan 15 13:05:09 2009 GMT
Subject:
countryName = CN
stateOrProvinceName = ZJ
organizationName = Some Ltd. Corp.
organizationalUnitName = Some Unit
commonName = Someone
emailAddress = some@email.com
X509v3 extensions:
X509v3 Basic Constraints:
CA:FALSE
Netscape Comment:
OpenSSL Generated Certificate
X509v3 Subject Key Identifier:
75:F5:3C:CC:C1:5E:6D:C3:8B:46:A8:08:E6:EA:29:E8:22:7E:70:03
X509v3 Authority Key Identifier:
keyid:75:F5:3C:CC:C1:5E:6D:C3:8B:46:A8:08:E6:EA:29:E8:22:7E:70:03
Certificate is to be certified until Jan 15 13:05:09 2009 GMT (365 days)
Sign the certificate? [y/n]:y
1 out of 1 certificate requests certified, commit? [y/n]y
Write out database with 1 new entries
Data Base Updated
一步完成 CA 证书请求生成及签名
以上两个步骤可以合二为一。利用 ca 命令的 -x509 参数,通过以下命令同时完成证书请求生成和签名从而生成 CA 根证书:
$ openssl req -new -x509 -days 365 -key ./demoCA/private/cakey.pem -out ./demoCA/cacert.pem
参数解释
req
用于生成证书请求的 OpenSSL 命令。
-new
生成一个新的证书请求。该参数将令 OpenSSL 在证书请求生成过程中要求用户填写一些相应的字段。
-x509
生成一份 X.509 证书。
-days 365
从生成之时算起,证书时效为 365 天。
-key ./demoCA/private/cakey.pem
指定 cakey.pem 为证书所使用的密钥对文件。
-out ./demoCA/cacert.pem
令生成的证书保存到文件 ./demoCA/cacert.pem 。
该命令输出如下,用户应输入相应的字段:
Enter pass phrase for ./demoCA/private/cakey.pem:
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [AU]:CN
State or Province Name (full name) [Some-State]:ZJ
Locality Name (eg, city) []:HZ
Organization Name (eg, company) [Internet Widgits Pty Ltd]:Some Ltd. Corp.
Organizational Unit Name (eg, section) []:Some Unit
Common Name (eg, YOUR name) []:Someone
Email Address []:some@email.com
至此,我们便已成功建立了一个私有根 CA 。在这个过程中,我们获得了一份 CA 密钥对文件./demoCA/private/cakey.pem 以及一份由此密钥对签名的 CA 根证书文件./demoCA/cacert.pem ,得到的 CA 目录结构如下:
.
|-- careq.pem
`-- demoCA/
|-- cacert.pem
|-- index.txt
|-- index.txt.attr
|-- index.txt.old
|-- newcerts/
| `-- 01.pem
|-- private/
| `-- cakey.pem
|-- serial
`-- serial.old
注:如果在 CA 建立过程中跳过证书请求生成的步骤,则不会产生 careq.pem 文件。
签发证书
下面我们就可以利用建立起来的 CA 进行证书签发了。
生成用户证书 RSA 密钥对
参照 CA 的 RSA 密钥对生成过程,使用如下命令生成新的密钥对:
$ openssl genrsa -des3 -out userkey.pem
Generating RSA private key, 512 bit long modulus
....++++++++++++
...++++++++++++
e is 65537 (0x10001)
Enter pass phrase for userkey.pem:<enter your pass-phrase>
Verifying - Enter pass phrase for userkey.pem:<re-enter your pass-phrase>
生成用户证书请求
参照 CA 的证书请求生成过程,使用如下命令生成新的证书请求:
$ openssl req -new -days 365 -key userkey.pem -out userreq.pem
Enter pass phrase for userkey.pem:<enter your pass-phrase>
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [AU]:CN
State or Province Name (full name) [Some-State]:ZJ
Locality Name (eg, city) []:HZ
Organization Name (eg, company) [Internet Widgits Pty Ltd]:Some Ltd. Corp.
Organizational Unit Name (eg, section) []:Some Other Unit
Common Name (eg, YOUR name) []:Another
Email Address []:another@email.com
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
签发用户证书
现在,我们可以用先前建立的 CA 来对用户的证书请求进行签名来为用户签发证书了。使用如下命令:
$ openssl ca -in userreq.pem -out usercert.pem
参数解释
ca
用于执行 CA 相关操作的 OpenSSL 命令。
-in userreq.pem
指定用户证书请求文件为 userreq.pem 。
-out usercert.pem
指定输出的用户证书文件为 usercert.pem 。
该命令要求用户输入密钥口令并输出相关证书信息,请求用户确认:
Using configuration from /usr/lib/ssl/openssl.cnf
Enter pass phrase for ./demoCA/private/cakey.pem:<enter your pass-phrase>
Check that the request matches the signature
Signature ok
Certificate Details:
Serial Number: 2 (0x2)
Validity
Not Before: Jan 16 14:50:22 2008 GMT
Not After : Jan 15 14:50:22 2009 GMT
Subject:
countryName = CN
stateOrProvinceName = ZJ
organizationName = Some Ltd. Corp.
organizationalUnitName = Some Other Unit
commonName = Another
emailAddress = another@email.com
X509v3 extensions:
X509v3 Basic Constraints:
CA:FALSE
Netscape Comment:
OpenSSL Generated Certificate
X509v3 Subject Key Identifier:
97:E7:8E:84:B1:45:27:83:94:A0:DC:24:79:7B:83:97:99:0B:36:A9
X509v3 Authority Key Identifier:
keyid:D9:87:12:94:B2:20:C7:22:AB:D4:D5:DF:33:DB:84:F3:B0:4A:EC:A2
Certificate is to be certified until Jan 15 14:50:22 2009 GMT (365 days)
Sign the certificate? [y/n]:y
1 out of 1 certificate requests certified, commit? [y/n]y
Write out database with 1 new entries
Data Base Updated
至此,我们便完成了 CA 的建立及用户证书签发的全部工作。不妨把所有 shell 命令放到一起纵览一下:
# 建立 CA 目录结构
mkdir -p ./demoCA/{private,newcerts}
touch ./demoCA/index.txt
echo 01 > ./demoCA/serial
# 生成 CA 的 RSA 密钥对
openssl genrsa -des3 -out ./demoCA/private/cakey.pem 2048
# 生成 CA 证书请求
openssl req -new -days 365 -key ./demoCA/private/cakey.pem -out careq.pem
# 自签发 CA 证书
openssl ca -selfsign -in careq.pem -out ./demoCA/cacert.pem
# 以上两步可以合二为一
openssl req -new -x509 -days 365 -key ./demoCA/private/cakey.pem -out ./demoCA/cacert.pem
# 生成用户的 RSA 密钥对
openssl genrsa -des3 -out userkey.pem
# 生成用户证书请求
openssl req -new -days 365 -key userkey.pem -out userreq.pem
# 使用 CA 签发用户证书
openssl ca -in userreq.pem -out usercert.pem
了解了这些基础步骤之后,就可以通过脚本甚至 makefile 的方式来将这些工作自动化。 CA.pl 和CA.sh 便是对 OpenSSL 的 CA 相关功能的简单封装,在 Debian 系统中,安装了 OpenSSL 后,可以在 /usr/lib/ssl/misc/ 目录下找到这两个文件。而 makefile 的解决方案则可以参考这里。
参考文献
- The Open–source PKI Book: A guide to PKIs and Open–source Implementations, Initialisation of the Certification Authority
http://ospkibook.sourceforge.net/docs/OSPKI-2.4.7/OSPKI-html/initialisation.htm - OpenSSL: Documents, OpenSSL(1)
http://www.openssl.org/docs/apps/openssl.html - OpenSSL Certificate Authority Setup
http://sial.org/howto/openssl/ca/
基于 OpenSSL 的 CA 建立及证书签发的更多相关文章
- 基于 OpenSSL 的 CA 建立及证书签发 【转】
建立 CA 建立 CA 目录结构 按照 OpenSSL 的默认配置建立 CA ,需要在文件系统中建立相应的目录结构.相关的配置内容一般位于 /usr/ssl/openssl.cnf 内,详情可参见 c ...
- centos7.6使用openssl生成CA签署的证书个人实验笔记
准备:客户端centos6.10 服务端Centos7.6 实验:客户端生成证书请求,服务端颁发证书,最后吊销其中一个证书 1.先在服务端上的/etc/pki/CA/目录生成rsa的私钥: 2.在服 ...
- 基于OpenSSL自建CA和颁发SSL证书
关于SSL/TLS介绍见文章 SSL/TLS原理详解.关于证书授权中心CA以及数字证书等概念,请移步 OpenSSL 与 SSL 数字证书概念贴 . openssl是一个开源程序的套件.这个套件有三个 ...
- 使用openssl模拟CA和CA证书的签发
使用openssl模拟CA和CA证书的签发 当使用ssl/tls进行加密通信时,必须要有数字证书.若通信只限制在局域网内,可以不向第三方机构申请签发证书,可以通过openssl模拟CA(Cer ...
- openSSL命令、PKI、CA、SSL证书原理
相关学习资料 http://baike.baidu.com/view/7615.htm?fr=aladdin http://www.ibm.com/developerworks/cn/security ...
- OpenSSL 给自己颁发根证书,由根证书签发下级证书的步骤。
1.建立根证书 (1)生成私钥 openssl genrsa -des3 -out CAroot.key 2048.产生一个2048位的私钥,在安装的openssl目录下调用openssl命令. 需要 ...
- 使用OpenSsl自己CA根证书,二级根证书和颁发证书(亲测步骤)
---恢复内容开始--- 一.介绍 企业自用, 到证书机构签发证书的费用和时间等都可以省下..... SSl证书的背景功用.......(省略万字,不废话) 可以参考: SSL证书_百度百科 X509 ...
- 基于OpenSSL的PKI的PKI数字证书系统实现
本篇主要介绍了基于OpenSSL的PKI的PKI数字证书系统实现,利用OpenSSL建立一个CA中心的详细解决方案和建立的具体步骤. 1.PKI数字证书系统设计 PKI数字证书系统主要包括证书颁发机构 ...
- nginx反向代理cas-server之2:生成证书,centOS下使用openssl生成CA证书(根证书、server证书、client证书)
前些天搭好了cas系统,这几天一致再搞nginx和cas的反向代理,一直不成功,但是走http还是测试通过的,最终确定是ssl认证证书这一块的问题,原本我在cas服务端里的tomcat已经配置了证书, ...
随机推荐
- Java -- JDBC 学习--通过 ResultSet 执行查询操作
ResultSet: 结果集. 封装了使用 JDBC 进行查询的结果. 1. 调用 Statement 对象的 executeQuery(sql) 可以得到结果集. 2. ResultSet 返回的实 ...
- change username on ubuntu.
Below tutorial will show you how to change username in ubuntu 12.04 precise.First,we need login as r ...
- linux动态库与静态库混合连接
1, 在应用程序需要连接外部库的情况下,linux默认对库的连接是使用动态库,在找不到动态库的情况下再选择静态库.使用方式为: gcc test.cpp -L. -ltestlib 如果当前目录有 ...
- CRT&EXCRT 中国剩余定理及其扩展
前言: 中国剩余定理又名孙子定理.因孙子二字歧义,常以段子形式广泛流传. 中国剩余定理并不是很好理解,我也理解了很多次. CRT 中国剩余定理 中国剩余定理,就是一个解同余方程组的算法. 求满足n个条 ...
- A1022. Digital Library
A Digital Library contains millions of books, stored according to their titles, authors, key words o ...
- 试着用c写了一个多线程的同步
在Java中写多线程相关的程序简单很多,在多线程中需要同步的时候,使用synchronized就行了. 最近学习c的多线程与同步,感觉实现起来,要写的代码比较多一些,这也许是因为java封装的比较好吧 ...
- MyEclipse中引用的maven配置文件只访问私服的配置
MyEclipse中要用到集成的maven,公司内网有个私服,办公机不能上外网. 这时Eclipse中设置引用的外部Setting配置文件中只需如下配置即可: 1.配置本地主机的maven仓库路径 & ...
- 用c语言实现http请求
用c语言来实现一个简单的http请求,请求 www.cnblogs.com 首页 #include<stdio.h> #include<stdlib.h> #include&l ...
- 理解 PHP 依赖注入
Laravel框架的依赖注入确实很强大,并且通过容器实现依赖注入可以有选择性的加载需要的服务,减少初始化框架的开销,下面是我在网上看到的一个帖子,写的很好拿来与大家分享,文章从开始按照传统的类设计数据 ...
- JSONObject的toBean 和 fromObject
public static void main(String[] args) { Map map=new HashMap(); map.put("我","妹") ...