[转]使用openssl创建CA

使用openssl创建CA

来源:http://blog.chinaunix.net/uid-20639449-id-1908987.html

openssl应用--创建个人CA

 首先安装openssl-0.9.7,安装成功以后,为了方便执行openssl指令,将openssl路径加入PATH中
$OPENSSL_HOME\out32dll\release加入PATH

使用openssl创建CA

1.生成目录树
 CA认证中心需要下列相关目录,请手动建立下列目录。
CARoot
|__certs
|__newcerts
|__private
|__crl

2.生成随机数文件
 在private目录下生成随机数文件.rnd,(可将一个文件内容拷贝.rnd,如将一exe文件拷贝成.rnd)。
3.生成文本数据库文件
 CARoot根目录下手动创建一个空的文本数据库文件index.txt。
4.生成证书序列号文件
 在CARoot下创建证书序列号文件serial,使用文本编辑器打开,在文件中输入"01";(文件内容无银号),或者使用命令$echo 01>serial (注意确认serial文件内容为01,并且无引号)。
5.产生CA私钥
 genrsa -out private\ca.key -rand private\.rnd 2048
 genrsa -out private\ca.key -rand private\.rnd -des3 2048  (生成des3加密的私钥文件)
 生成2048位的私钥文件,为保障安全使用第二种,用des3加密。

其他相关命令
 rsa -noout -text -in ca.key //查看私钥
 rsa -in ca.key -out ca.key.unsecure //生成一个不需要口令保护的私钥,不推荐

6.修改配置文件

 将$OPENSSL_HOME下的apps\openssl.cnf拷贝到CARoot目录下,然后修改CA的配置文件
修改openssl.cnf文件

将下面部分修改:
[ CA_default ]

dir  = ./demoCA  # Where everything is kept
certs  = $dir/certs  # Where the issued certs are kept
crl_dir  = $dir/crl  # Where the issued crl are kept
database = $dir/index.txt # database index file.
new_certs_dir = $dir/newcerts  # default place for new certs.

certificate = $dir/cacert.pem  # The CA certificate
serial  = $dir/serial   # The current serial number
crl  = $dir/crl.pem   # The current CRL
private_key = $dir/private/cakey.pem# The private key
RANDFILE = $dir/private/.rand # private random number file

修改后
RANDFILE  = $ENV::HOME/private/.rnd
(修改默认的随机数文件的位置)

[ CA_default ]

dir  = ./  # Where everything is kept
certs  = $dir/certs  # Where the issued certs are kept
crl_dir  = $dir/crl  # Where the issued crl are kept
database = $dir/index.txt # database index file.
new_certs_dir = $dir/newcerts  # default place for new certs.

certificate = $dir/private/ca.crt  # The CA certificate
serial  = $dir/serial   # The current serial number
crl  = $dir/crl.cer  # The current CRL
private_key = $dir/private/ca.key# The private key
RANDFILE = $dir/private/.rnd # private random number file

7.生成CA证书
 req -new -x509 -days 3650 -key private\ca.key -out private\ca.crt -config openssl.cnf
(查看证书文件:x509 -in cacert.pem -text -noout)

8.创建CA的自签名证书
 genrsa -out certs\cert.key -rand private\.rnd -des3 2048
(查看私钥文件rsa -in cert.key -noout -text)
 req -new -x509 -days 3650 -key certs\cert.key -out certs\cert.crt -config openssl.cnf
  ca -ss_cert certs\cert.crt -config openssl.cnf -policy policy_anything -out certs\signedcert.crt
选项ss_cert 表明是自签名证书

证书格式说明:
 windows不能识别pem文件格式,能够识别crt文件,单纯的将后缀名改变,windows也是不能识别的,
打开签名后的证书(pem文件格式,尽管后缀名为crt),保留BEGIN CERTIFICATE and the END CERTIFICATE之间的内容,删除其他内容,windows就能够识别了。
(windows的证书crt格式就是
-----BEGIN CERTIFICATE-----
证书内容
-----END CERTIFICATE-----

将PEM证书装换成DER证书
$ openssl x509 -in tom.crt -out cert.der -outform DER


 如果创建证书失败,失败后重做的方法:
 serial文件中删除证书序列号
 index.txt数据库文件中删除该证书的条目
 重新创建证书就可以了

===================================================================
生成其他用户证书
1、生成证书请求
OpenSSL> req -newkey rsa:1024 -keyout testkey.pem -out testreq.pem -rand private\.rnd -config openssl.cnf
(查看证书请求:req -in testreq.pem -text -noout)


2、使用证书请求签发证书
ca -in testreq.pem -policy policy_anything -config openssl.cnf
 -policy policy_anything 使用这个CP签发证书
 默认的是policy_match 要求countryName,State,Organization必须匹配
ca -in testreq.pem -days 730 policy policy_anything -config openssl.cnf
 生成的证书在newcerts目录下  xxx.pem
===================================================================
其他可能会用到的相关操作:

撤销证书
copy newcerts\02.pem test.pem (拷贝一个证书副本进行撤销)
ca -revoke test.pem -config openssl.cnf

产生CRL
OpenSSL> ca -gencrl -out crl041012.crl -config openssl.cnf
(查看CRL文件:crl -in crl041012.crl -text -noout)

使用CA根证书验证CRL
OpenSSL> crl -in crl041012.crl -noout -CAfile private\ca.crt

posted @ 2014-09-29 00:30  jesselk  阅读(175)  评论(0编辑  收藏  举报