Linux下如何颁发证书:学习使用openssl搭建一个CA

来源:互联网 发布:寻找大数据外包项目 编辑:程序博客网 时间:2024/04/27 13:12

这两天学习了openssl在LInux中的使用,openssl是一款开源的加密工具,在Linux环境下,我们能够利用它来搭建一个CA来实现证书的发放,可以用于企业内部使用的加密工具。在介绍openssl之前,首先描述一下关于“身份认证+数据加密”的实现方法原理。


如何实现“身份验证+数据加密”,请看下面的一张流程图(自己画得,比较简陋)

整个加密过程:

发送方: 计算数据特征值----> 使用私钥加密特征值 ---> 随机生成密码对称加密整个数据 ---> 使用接受方公钥加密密码
接收方: 使用私钥解密密码 ----> 解密整个数据 ----> 使用公钥验证身份 ----> 比较数据特征值

但是存在一个问题,谁来管理公钥,任何在互联网上传播的数据都不安全,更不用说传递公钥,它如果被篡改,那就无法验证身份,所以不可能由用户自己颁发公钥。

这个时候需要有一个具有公信力的中间机构来做这份工作,那就是CA,由此引发了两个概念:

CA : 证书颁发机构

PKI : 公钥基础设施,公钥基础构架

证书: 里面存放了用户的各种信息,最核心的部分就是公钥

但是还有一个问题,谁来给CA颁发公钥,解决方法是,CA自己给自己颁发公钥。。。


下面是用openssl这个强大的工具,在linux下构建一个CA,来实现证书管理,我们用一个web服务器端作为需要证书的客户端

1.首先我们来给CA生成一个私钥

切换到/etc/pki/CA/目录,使用openssl命令给自己生成一个私钥

[cpp] view plaincopy
  1. [root@server56 openssl]# cd /etc/pki/CA/  
  2. [root@server56 CA]# ls  
  3. private  
  4. [root@server56 CA]# (umak 66;openssl genrsa 2046 > private/cakey.pem)  
  5. -bash: umak: command not found  
  6. Generating RSA private key, 2046 bit long modulus  
  7. .............................+++  
  8. ..+++  
  9. e is 65537 (0x10001)  

2. CA需要一个自签证书,所以我们给它使用openssl命令生成一个自签证书

[cpp] view plaincopy
  1. [root@server56 CA]# openssl req -new -x509 -key private/cakey.pem -out cacert.pem  
  2. You are about to be asked to enter information that will be incorporated  
  3. into your certificate request.  
  4. What you are about to enter is what is called a Distinguished Name or a DN.  
  5. There are quite a few fields but you can leave some blank  
  6. For some fields there will be a default value,  
  7. If you enter '.', the field will be left blank.  
  8. -----  
  9. Country Name (2 letter code) [GB]:CN                                                           # 输入你的各项信息,国家 , 省或州,地区,公司,单位,域名,邮箱地址  
  10. State or Province Name (full name) [Berkshire]:Henan     
  11. Locality Name (eg, city) [Newbury]:Zhengzhou  
  12. Organization Name (eg, company) [My Company Ltd]:LINUX  
  13. Organizational Unit Name (eg, section) []:Tech  
  14. Common Name (eg, your name or your server's hostname) []:www.rhce.com   #需要注意的是,这个域名是FQDN(完全限定域名)  
  15. Email Address []:ca@rhce.com  
  16. [root@server56 CA]# ls  
  17. cacert.pem  private  

3.编辑CA配置文件,它位于etc/pki/tls/openssl.cnf,它的作用是指定你的CA所在目录,更改默认属性值

[cpp] view plaincopy
  1. [root@server56 CA]# vim /etc/pki/tls/openssl.cnf  
  2. [ CA_default ]  
  3.   
  4. dir             = ../../CA              # Where everything is kept      **************CA路径 ,修改为绝对路径  
  5. certs           = $dir/certs            # Where the issued certs are kept          ×××××××发给其他的人的证书  ,该目录需要手动创建  
  6. crl_dir         = $dir/crl              # Where the issued crl are kept   ××××××证书吊销列表  不属于必须创建的目录  
  7. database        = $dir/index.txt        # database index file.       *****************存放生成证书文件索引  需要手动创建的文件  
  8. #unique_subject = no                    # Set to 'no' to allow creation of      
  9.                                         # several ctificates with same subject.  
  10. new_certs_dir   = $dir/newcerts         # default place for new certs.   ××××××××××x新生成的证书存放地  需要手动创建                                                                                                      
  11. certificate     = $dir/cacert.pem       # The CA certificate  
  12. serial          = $dir/serial           # The current serial number               ××××××序列号,需要自己建每一个证书都有一个序列号需要自己建,并指定从几开始  
  13. crlnumber       = $dir/crlnumber        # the current crl number  
  14.                                         # must be commented out to leave a V1 CRL  
  15. crl             = $dir/crl.pem          # The current CRL  
  16. private_key     = $dir/private/cakey.pem# The private key  
  17. RANDFILE        = $dir/private/.rand    # private random number file  
  18. x509_extensions = usr_cert              # The extentions to add to the cert  
  19. # req_extensions = v3_req # The extensions to add to a certificate request  
  20. #########修改证书CSR与自己的匹配  
  21. [ req_distinguished_name ]  
  22. countryName                     = Country Name (2 letter code)  
  23. countryName_default             = CN                                                           #我修改为CN和CA的自签证书对应  
  24. countryName_min                 = 2  
  25. countryName_max                 = 2  
  26.   
  27. stateOrProvinceName             = State or Province Name (full name)  
  28. stateOrProvinceName_default     = Henan                                            #同上  
  29.   
  30. localityName                    = Locality Name (eg, city)  
  31. localityName_default            = Zhengzhou                                              #同上  
  32.   
  33. 0.organizationName              = Organization Name (eg, company)  
  34. 0.organizationName_default      = Tech                                                  #同上  

4.创建CA的相关目录和文件,指定序列号起始数字,在上一步已经说明,它们在CA所在目录创建

[cpp] view plaincopy
  1. [root@server56 ~]# cd /etc/pki/CA/  
  2. [root@server56 CA]# mkdir certs crl newcerts  
  3. [root@server56 CA]# ls  
  4. cacert.pem  certs  crl  newcerts  private  
  5. [root@server56 CA]# touch index.txt serial  
  6. [root@server56 CA]# echo 01 > serial  

5. 创建web服务器的私钥 ,因为是实验,所有并不需要安装web服务器,你可以创建一个ssl目录,我们假设它是一个web服务器              

[cpp] view plaincopy
  1. [root@server56 CA]# cd /etc/httpd/  
  2. [root@server56 httpd]# mkdir ssl  
  3. [root@server56 httpd]# cd ssl/  
  4. [root@server56 ssl]# (umask 66;openssl genrsa 2048 > web.key)  
  5. Generating RSA private key, 2048 bit long modulus  

6. 客户端(web服务器)请求获得证书,客户端如果想申请获得证书的话,需要创建一个申请证书,传递给CA

[cpp] view plaincopy
  1. [root@server56 ssl]# openssl req -new -key web.key -out web.csr  
  2. You are about to be asked to enter information that will be incorporated  
  3. into your certificate request.  
  4. What you are about to enter is what is called a Distinguished Name or a DN.  
  5. There are quite a few fields but you can leave some blank  
  6. For some fields there will be a default value,  
  7. If you enter '.', the field will be left blank.  
  8. -----  
  9. Country Name (2 letter code) [CN]:  
  10. State or Province Name (full name) [Henan]:  
  11. Locality Name (eg, city) [Zhengzhou]:  
  12. Organization Name (eg, company) [RHCE]:  
  13. Organizational Unit Name (eg, section) [Tech]:  
  14. Common Name (eg, your name or your server's hostname) []:www.web.com      
  15. Email Address []:www@web.com  
  16.   
  17. Please enter the following 'extra' attributes  
  18. to be sent with your certificate request  
  19. A challenge password []:                               # 请求证书需要在网络上传递,所以加密防止别人窥探,这里留空因为我们只是实验  
  20. An optional company name []:  

7. 在CA端给客户端颁发证书,使用openssl命令

[cpp] view plaincopy
  1. [root@server56 ssl]# openssl ca -in web.csr -out web.crt              # 这个命令执行后,会显示请求证书里的信息  
  2. Using configuration from /etc/pki/tls/openssl.cnf  
  3. Check that the request matches the signature  
  4. Signature ok  
  5. Certificate Details:  
  6.         Serial Number: 1 (0x1)  
  7.         Validity  
  8.             Not Before: Aug  9 04:46:25 2011 GMT  
  9.             Not After : Aug  8 04:46:25 2012 GMT  
  10.         Subject:  
  11.             countryName               = CN  
  12.             stateOrProvinceName       = Henan  
  13.             organizationName          = RHCE  
  14.             organizationalUnitName    = Tech  
  15.             commonName                = www.web.com  
  16.             emailAddress              = www@web.com  
  17.         X509v3 extensions:  
  18.             X509v3 Basic Constraints:   
  19.                 CA:FALSE  
  20.             Netscape Comment:   
  21.                 OpenSSL Generated Certificate  
  22.             X509v3 Subject Key Identifier:   
  23.                 B6:52:27:11:5B:BA:84:C8:56:4D:67:D7:B9:7A:CB:FE:45:CF:5A:02  
  24.             X509v3 Authority Key Identifier:   
  25.                 keyid:5C:4A:A2:EB:DD:3F:BB:08:41:A2:02:3F:98:A4:59:8B:78:47:AF:4F  
  26. Certificate is to be certified until Aug  8 04:46:25 2012 GMT (365 days)  
  27. Sign the certificate? [y/n]:y                                                                                      # 是否认同这个请求的客户端,并授予证书  
  28.   
  29. 1 out of 1 certificate requests certified, commit? [y/n]y                         # 升级证书数据库  
  30. Write out database with 1 new entries  
  31. Data Base Updated  


好了,看一下我们的证书把!就是那个.crt结尾的文件

[cpp] view plaincopy
  1. [root@server56 ssl]# ls  
  2. server.key  web.crt  web.csr  web.key  


转自:http://blog.csdn.net/deansrk/article/details/6680299
0 0
原创粉丝点击