使用OpenSSL建立根CA及自签名证书制作过程 [转载]

作者: 囧囧有神  链接:http://www.javaeye.com/topic/235905  发表时间: 2008年09月02日


Openssl版本:0.9.8 版本,可从http://www.openssl.org 处下载。

  1. 先建立如下目录结构

$home/testca                            # testca 是待建CA的主目录
                 ├─newcerts/            # newcerts子目录将存放CA签发过的数组证书(备份目录)
                 ├─private/                # private目录用来存放CA私钥
                 └─conf/                       # conf目录用来存放简化openssl命令行参数用的配置文件

 此外使用命令

 echo "01" > serial  

 touch index.txt 

 ca根目录下创建文件serial (用来存放下一个证书的序列号) indext.txt (证书信息数据库文件)

 

  1. 生成CA的私钥和自签名证书 (根证书)

创建配置文件:vi "$HOME/testca/conf/gentestca.conf"

文件内容如下:

####################################
[ req ]

default_keyfile = /home/cx/testCA/private/cakey.pem

default_md = md5

prompt = no

distinguished_name = ca_distinguished_name

x509_extensions = ca_extensions

 

[ ca_distinguished_name ]

organizationName = ss

organizationalUnitName  = sstc

commonName = sstcCA

emailAddress = GChen2@statestreet.com  #自己的邮件地址

 

[ ca_extensions ]

basicConstraints = CA:true
########################################

 

然后执行命令如下:
cd "$HOME/testca"
openssl req -x509 -newkey rsa:2048 -out cacert.pem -outform PEM -days 2190 -config "$HOME/testca/conf/gentestca.conf"
执行过程中需要输入CA私钥的保护密码,假设我们输入密码: 888888
 
可以用如下命令查看一下CA自己证书的内容

openssl x509 -in cacert.pem -text –noout

 

创建一个配置文件,以便后续CA日常操作中使用
vi "$HOME/testca/conf/testca.conf"
文件内容如下:

########################################

[ ca ]

default_ca     = testca                               # The default ca section

 

[ testca ]

dir            = /home/cx/testCA                   # top dir

database       = $dir/index.txt                    # index file.

new_certs_dir  = $dir/newcerts                 # new certs dir

 

certificate    = $dir/cacert.pem                  # The CA cert

serial         = $dir/serial                            # serial no file

private_key    = $dir/private/cakey.pem    # CA private key

RANDFILE       = $dir/private/.rand           # random number file

 

default_days   = 365                           # how long to certify for

default_crl_days= 30                           # how long before next CRL

default_md     = md5                          # message digest method to use

unique_subject = no                           # Set to 'no' to allow creation of

                                                           # several ctificates with same subject.

Policy = policy_any                            # default policy

 

[ policy_any ]

countryName             = optional

stateOrProvinceName     = optional

localityName            = optional

organizationName        = optional

organizationalUnitName  = optional

commonName              = supplied

emailAddress            = optional

########################################

 

  1. 自己生成公钥密钥,并用测试CA签发数字证书

我们在平时测试时,可以自己用openssl为服务器或用户生成公钥密钥,并用上面创建的CA签发对应私钥(密钥)的数字证书。
假设,我们就用刚才创建CA的操作系统用户为名为testuser的用户创建数字证书,我们要把待创建的私钥、证书等都放在目录$HOME/testuser下:

Ø       创建密钥和证书请求(证书请求里包含了公钥)

创建$HOME/testuser目录并执行命令:
mkdir $HOME/testuser
cd $HOME/testuser
openssl req -newkey rsa:1024 -keyout testkey.pem -keyform PEM -out testreq.pem -outform PEM -subj "/O=TestCom/OU=TestOU/CN=testuser"
执行过程中需要输入私钥的保护密码,假设我们输入密码: 222222

其中的CN最好和服务器名或IP地址一致。

执行完后,testkey.pem即为用户的密钥,而testreq.pem即为证书请求。
可以用openssl req -in testreq.pem -text -noout查看证书请求的内容。

 

Ø       用测试CAtestuser签发证书

同样还在$HOME/testuser目录下执行命令:
openssl ca -in testreq.pem -out testcert.pem -config "$HOME/testca/conf/testca.conf"
执行过程中需要输入CA的密钥保护密码(刚才设置的888888),并且最后询问你是否要给该用户签发证书时要选y
 
执行完后,testcert.pem即为证书,
可以用命令openssl x509 -in testcert.pem -text -noout查看证书内容

 

Ø       制作一个PKCS12格式的文档(个人数字证书)

我们制作的这个PKCS#12文件将包含密钥、证书和颁发该证书的CA证书。该文件可以直接用于服务器数字证书或个人数字证书。
把前几步生成的密钥和证书制作成一个pkcs12文件的方法执行命令:
openssl pkcs12 -export -in testcert.pem -inkey testkey.pem -out testuser.p12 -name testuser -chain -CAfile "$HOME/testca/cacert.pem"
执行过程中需要输入保护密钥的密码(222222),以及新的保护pkcs12文件的密码。

     

  1. CA 的日常操作

Ø       根据证书申请请求签发证书
假设收到一个证书请求文件名为req.pem,文件格式应该是PKCS#10格式(标准证书请求格式)。
 
首先可以查看一下证书请求的内容,执行命令:
openssl req -in req.pem -text -noout
将看到证书请求的内容,包括请求者唯一的名字(DN)、公钥信息(可能还有一组扩展的可选属性)。
 
执行签发命令:
openssl ca -in req.pem -out cert.pem -config "$HOME/testca/conf/testca.conf"
执行过程中会要求输入访问CA的私钥密码(刚才设置的888888)。
 
完成上一步后,签发好的证书就是cert.pem,另外$HOME/testca/newcerts里也会有一个相同的证书副本(文件名为证书序列号)。
你可以执行以下语句来查看生成的证书的内容:
openssl x509 -in cert.pem -text –noout

 

Ø        吊销证书(作废证书)
一般由于用户私钥泄露等情况才需要吊销一个未过期的证书。(当然我们用本测试CA时其时很少用到该命令,除非专门用于测试吊销证书的情况)
假设需要被吊销的证书文件为cert.pem,则执行以下命令吊销证书:
openssl ca -revoke cert.pem -config "$HOME/testca/conf/testca.conf"

 

 

 生成证书吊销列表文件(CRL
准备公开被吊销的证书列表时,可以生成证书吊销列表(CRL),执行命令如下:
openssl ca -gencrl -out testca.crl -config "$HOME/testca/conf/testca.conf"
还可以添加-crldays-crlhours参数来说明下一个吊销列表将在多少天后(或多少小时候)发布。
 
可以用以下命令检查testca.crl的内容:
openssl crl -in testca.crl -text -noout


关于OPENSSL的使用,以下链接有关于其man文档的说明,可以深入参照.
http://bbs.chinaunix.net/viewthread.php?tid=478901&page=0#pid3161562

关于证书操作的其它参照链接,如下:
http://www.bitscn.com/windows/networkbase/200604/5284.html ()


关于配置文件,以上文章中的testca.conf在环境中并不能对证书进行签名.换成以下的conf文件就好了.
#
# SSLeay example configuration file.
# This is mostly being used for generation of certificate requests.
#

RANDFILE = .rnd

####################################################################
[ ca ]
default_ca = CA_default # The default ca section

####################################################################
[ CA_default ]

dir = /home/shipfi/testca # 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 # default place for new certs.

certificate = $dir/cacert.pem # The CA certificate
serial = $dir/serial # The current serial number
crl = $dir/crl.pem # The curren CRL
private_key = $dir/private/cakey.pem # The private key
RANDFILE = $dir/private/privkey.rnd

#x509_extensions= x509v3_extensions # The extentions to add to the cert
default_days = 365 # how long to certify for
default_crl_days= 30 # how long before next CRL
default_md = md5 # which md to use.
preserve = no # keep passed DN ordering

# A few difference way of specifying how similar the request should look
# For type CA, the listed attributes must be the same, and the optional
# and supplied fields are just that :-)
policy = policy_match

# For the CA policy
[ policy_match ]
countryName = optional
stateOrProvinceName = optional
organizationName = optional
organizationalUnitName = optional
commonName = supplied
emailAddress = optional

# For the 'anything' policy
# At this point in time, you must list all acceptable 'object'
# types.
[ policy_anything ]
countryName = optional
stateOrProvinceName = optional
localityName = optional
organizationName = optional
organizationalUnitName = optional
commonName = supplied
emailAddress = optional

####################################################################
[ req ]
default_bits = 1024
default_keyfile = privkey.pem
distinguished_name = req_distinguished_name
attributes = req_attributes

[ req_distinguished_name ]
countryName = Country Name (2 letter code)
countryName_default = TW
countryName_min = 2

stateOrProvinceName = State or Province Name (full name)

localityName = Locality Name (eg, city)

0.organizationName = Organization Name (eg, company)

organizationalUnitName = Organizational Unit Name (eg, section)

commonName = Common Name (eg, your website's domain name)
commonName_max = 64

emailAddress = Email Address
emailAddress_max = 40

[ req_attributes ]
challengePassword = A challenge password
challengePassword_min = 4
challengePassword_max = 20

[ x509v3_extensions ]

# under ASN.1, the 0 bit would be encoded as 80
nsCertType = 0x40


#nsBaseUrl
#nsRevocationUrl
#nsRenewalUrl
#nsCaPolicyUrl
#nsSslServerName
#nsCertSequence
#nsCertExt
#nsDataType

posted @ 2008-10-12 14:31  shipfi  阅读(6442)  评论(0编辑  收藏  举报