---
title: "基于SSL的电子证书配置"
author: "ralin"
date: "2010-05-20T16:28:52Z"
tags: []
categories: ["研发管理"]
slug: "ssl"
draft: false
lastmod: "2010-05-20T16:28:52Z"
id: 670937
url: "https://ralin.iteye.com/blog/670937"
---

基于SSL的电子证书配置
1) 创建root CA
如果你有自己的CA(Certificate Authority)，或者打算购买商业CA证书，那么这个步骤可以省略了，后面的步骤也会不同，但是不管如何，我们假定现在我们得到的CA私钥名字是 rootca.key。
现在我们自己来创建自己的root CA
第一步，创建root CA的私钥，为了安全，我们采取2048bit加密方式，设置的密码也应该足够复杂

# openssl genrsa -des3 2048 >rootca.key
Generating RSA private key, 2048 bit long modulus            
......................................+++                    
......+++                                                   
e is 65537 (0x10001)                                         
Enter pass phrase:    <------输入自己的密码，最好足够复杂   
                                   
Verifying - Enter pass phrase:           <----- 再次输入密码  


这样就创建root CA的密钥。
解析来我们用这个私钥对CA的证书做自认证(self-certificte)签名

         
# openssl req -new -x509 -key caroot.key -days 3650 -out caroot.cert
Enter pass phrase for caroot.key: <----输入私钥的密码
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) :
State or Province Name (full name) :
Locality Name (eg, city) :
Organization Name (eg, company) :
Organizational Unit Name (eg, section) :
Common Name (eg, your name or your server's hostname) :
Email Address :




现在我们创建存放CA证书的路径和一些必要文件



mkdir -p /etc/pki/CA/{certs,newcerts,private}
touch /etc/pki/CA/index.txt
echo "01" >/etc/pki/CA/serial


将生成的证书拷贝到对应的位置


cp rootca.key /etc/pki/CA/private/cakey.pem
cp rootca.cert /etc/pki/CA/rootca.cert



第二步：创建服务端证书
首先创建服务端私钥


# openssl genrsa -des3 2048 >server.key
Generating RSA private key, 2048 bit long modulus
..............................................................+++
......................+++
e is 65537 (0x10001)
Enter pass phrase:
Verifying - Enter pass phrase:



由密钥产生认证的申请文件


#openssl req -days 3650 -key server.key -new -out server.csr
Enter pass phrase for server.key:     <---- 输入服务端私钥的密码                                             
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) :
State or Province Name (full name) :
Locality Name (eg, city) :
Organization Name (eg, company) :
Organizational Unit Name (eg, section) :
Common Name (eg, your name or your server's hostname) :
Email Address :

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password :
An optional company name :


上面需要提供的信息中，最关键的是Common Name字段，这里要填写的是用过是FQDN(Full Quality Domain Name)，也应该和你将来访问的域名一致，否则访问域名的时候，SSL会报错。
接着使用root CA给申请文件签名产生证书文件


# openssl  ca -out server.cert -days 3650 -infiles server.csr
Using configuration from /etc/pki/tls/openssl.cnf                             
Enter pass phrase for ../../CA/private/cakey.pem:   <---输入root CA的私钥密码                           
Check that the request matches the signature                                 
Signature ok                                                                  
Certificate Details:                                                         
        Serial Number:                                                        
            ab:0d:d5:fe:d0:d3:eb:22                                          
        Validity                                                              
            Not Before: Jun 23 04:26:40 2008 GMT                              
            Not After : Jun 21 04:26:40 2018 GMT                              
        Subject:                                                              
            countryName               = CN                                    
            stateOrProvinceName       = HuNan                                 
            organizationName          = RedFlag Software                     
            organizationalUnitName    = Technical Support Depart
            commonName                = wgzhao-demo.rflinux.com
        X509v3 extensions:
            X509v3 Basic Constraints:
                CA:FALSE
            Netscape Comment:
                OpenSSL Generated Certificate
            X509v3 Subject Key Identifier:
                EB:77:E0:22:DB:6C:A0:95:54:A9:90:BB:41:B5:DE:3F:AA:BA:EA:9A
            X509v3 Authority Key Identifier:
                keyid:80:8D:F3:90:A2:D0:3E:C2:B7:70:7B:A8:D3:A9:31:DA:5A:01:C2:63

Certificate is to be certified until Jun 21 04:26:40 2018 GMT (3650 days)
Sign the certificate? :y


1 out of 1 certificate requests certified, commit? y
Write out database with 1 new entries
Data Base Updated

你可以把服务端私钥和证书合并，也可以分开，取决于ssl.conf配置文件的写法，后面会提到。

第三步，从安全角度考虑，我们单独给客户端生成有一个密钥
步骤和第二步生成服务端证书一样，只是在生成csr文件的时候，对Common Name没有要求。
假定你已经生成了client.key,client.csr,client.cert文件。
接着我们需要制作一张可便于携带的证书，即PKCS#12格式


# openssl pkcs12 -export -in client.cert -inkey client.key -out client.pfx
Enter pass phrase for client.key: <---客户端私钥密码
Enter Export Password: <---导出密码，在导入的时候需要用到
Verifying - Enter Export Password:


这样，我们就生成了一张可导入到浏览器的证书。

第四步 配置Apache
主要是配置/etc/httpd/conf.d/ssl.conf
要配置的主要是下面几行：


SSLCertificateFile /etc/pki/tls/certs/server.cert
SSLCertificateKeyFile  /etc/pki/tls/certs/server.key

SSLCACertificateFile /etc/pki/CA/rootca.cert
SSLCACertificatePath /etc/pki/CA

SSLVerifyClient require
SSLVerifyDepth  1


上面的配置可以分为三个部分，第一步分最开始两行，配置服务端的证书
如果在第二个步骤的时候，你已经把私钥和证书综合到了一起(cat server.key server.cert >server.pem)，那么就只需要第一行就可以了，后面指向综合在一起的文件（这里是server.pem)。否则你可以像第二行那样指定私钥的位置。
接下来的两行是配置root CA的，其实，只要配置第一行就可以了。

最后两行很重要，SSLVerifyClient表示需要验证客户端，是参数是none，那就不验证客户端了，客户端和服务器端通信仅仅是通过SSL，但是谁也知道要通讯的对方是不是就是你需要的对方。
SSLVerifyDepth是验证深度，默认是10,改成1。这个参数的具体含义，我还是不太了解。

另外，你还需要把ssl.conf的虚拟主机配置成你填写Common Name时的域名，总而言之如果你Common Name填写的是yourdomin，那么浏览器访问的时候就只能是https://yourdomain，其他的否会报错。
配置完成后，重启Apache服务


# /etc/init.d/httpd start
Apache/2.2.3 mod_ssl/2.2.3 (Pass Phrase Dialog)
Some of your private key files are encrypted for security reasons.
In order to read them you have to provide the pass phrases.

Server wgzhao-demo.rflinux.com:443 (RSA)
Enter pass phrase: <!--输入服务端证书的密钥

OK: Pass Phrase Dialog successful.




第五步，为客户端导入证书
以firefox 3.0浏览器为例子，打开浏览器，选择“编辑”--》“首先项”--》“高级”
点击“查看证书”按钮，在弹出的对话框里，点击“导入”按钮，然后导入前面生成的pfx文件。
接着会问你要密码，输入导出pfx文件时键入的导出密码。
然后重启浏览器，浏览你的域名，如果正常的话，会太初一个对话框，要你选择证书。
确定后，就可以看到网站内容。

上面的那些命令，如果你觉得参数太多，很难记住，那么openssl自带的那个Makefile文件倒是很有用，可以制作key文件，csr文件，cert文件。
不过制作cert文件需要注意，它采取的是自认证方式的，而不是采取root CA认证。

如果你觉得制作CA的那个步骤要创建文件也麻烦的话（是不是太懒了点？），那也有脚本，CA.pl可以除了创建CA外，也能创建cert等文件。如果你的 openssl包里没有自带这个perl脚本的话，你可以到这里来下载。


我遇到的一些错误及解决办法：

Q： 浏览器访问的时候，报错说超过了可协商的长度，错误号 -12227 ，然后页面上显示“ssl_error_rx_record_too_log”
A：在制作csr文件时填写的地址与ssl中虚拟主机不一致导致的

Q：SSL日志里有：  Certificate Verification: Error (20): unable to get local issuer certificate的报错。
fiefox浏览器浏览时给出下面的错误页面：

节点没有识别并信任向你颁发证书的CA。

（错误码： ssl_error_unknown_ca_alert）


A：ssl.conf配置文件中没有定义SSLCACertificateFile，导致给出上面的错误。

Q： SSL日志里下面的错误：
 Certificate Verification: Certificate Chain too long (chain has 2 certificates, but maximum allowed are only 1)

A：这是校验深度的问题，将ssl.conf文件中的SSLVerfiyDepth值改大一点，比如改成6