手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

加密、解密,以及OpenSSL建立私有CA

[日期:2015-04-28] 来源:Linux社区  作者:wuday [字体:大 中 小]

五、使用OpenSSL构建私有CA
  CA和申请证书都在同一台机器上进行的。
1、CA端操作流程
操作步骤:
    1、生成私钥文件;       
    2、生成自签署证书;         
    3、相关辅助文件
        touch/etc/pki/CA/index.txt
        echo01 > /etc/pki/CA/serial
再次说明:
  (1)私钥用于签发证书时,向证书添加数字签名使用;
  (2)证书:每个通信方都导入此证书至“受信任的证书颁发机构”;
1)、配置文件
  /etc/pki/tls/openssl.cnf,此文件格式为INI格式的
………省略……………
####################################################################
[ ca ]
default_ca      =CA_default        # The default casection
 
####################################################################
[ CA_default ]  #定义Openssl做为CA时自己的相关路径
 
dir            =/etc/pki/CA # Where everythingis kept  #指明CA自己的工作目录
certs          =$dir/certs  # Where the issuedcerts are kept #已颁发证书的存取位置
crl_dir        =$dir/crl    # Where the issuedcrl are kept #已吊销证书的吊销链表存取位置
database        =$dir/index.txt  # database indexfile.  #证书索引文件
#unique_subject = no          # Set to 'no' to allowcreation of
                    #several ctificates with same subject.
new_certs_dir  =$dir/newcerts    # default place fornew certs.  #新证书的存放位置
     
certificate    =$dir/cacert.pem  # The CAcertificate    #指CA自己证书文件名
serial          =$dir/serial  # The currentserial number #序列号,下一个要发的证书的序列号,每签一个序列号会怎么加1
crlnumber      =$dir/crlnumber  # the current crlnumber  #吊销证书列表的编号
                  # mustbe commented out to leave a V1 CRL
crl            =$dir/crl.pem # The current CRL  #当前正在使用的吊销链
private_key    =$dir/private/cakey.pem# The private key  #CA自己的私钥文件
RANDFILE        =$dir/private/.rand # private randomnumber file #随机数的获取位置
 
x509_extensions = usr_cert        # The extentions to add to thecert #定义了x.509的扩展信息中记录为“用户证书”
 
…………省略……………………

2)、工作目录
  /etc/pki/CA/
  注意:CentOS要想构建CA必须是基于/etc/pki/CA目录进行
[root@node-2 ~]# cd /etc/pki/CA
[root@node-2 CA]# ll
total 16
drwxr-xr-x  2 root root4096 Oct 15  2014 certs        #证书文件存放位置,
drwxr-xr-x  2 root root4096 Oct 15  2014 crl          #证书吊销列表存取文件
drwxr-xr-x. 2 root root 4096 Oct 15  2014 newcerts      #新创建证书存取位置
drwx------. 2 root root 4096 Oct 15  2014 private        #CA自己私钥存储位置

3)、创建CA自己的私钥
[root@node-2 CA]# pwd
/etc/pki/CA
[root@node-2 CA]# (umask 077;openssl genrsa -out private/cakey.pem 2048)
Generating RSA private key, 2048 bit longmodulus
........................................................+++
..............+++
e is 65537 (0x10001)
[root@node-2 CA]# ll ./private/
total 4
-rw------- 1 root root 1675 Apr 22 22:23cakey.pem

 
4)、生成一个自签署证书
  此自签署证书在/etc/pki/tls/openssl.cnf文件中定义必须放在CA目录下叫cacert.pem;从私钥中提取公钥,并构建成证书签署请求,然后让CA给签署;
格式:openssl req -new -x509-key  /etc/pki/CA/private/cakey.pem -out/etc/pki/CA/cacert.pem -days #
      -x509:表示自签署证书,不加表示证书签署请求;
      -key:从那个私钥中自动提取出公钥,并创建一个证书签署请求;
      -days:指明给自己签署证书的有效期
12345678910111213141516171819202122 [root@node-2 CA]# openssl req -new -x509 -key private/cakey.pem -out cacert.pem -days3360
You are about to be asked to enterinformation that will be incorporated
into your certificate request.
What you are about to enter is what is calleda Distinguished Name or a DN.
There are quite a few fields but you canleave some blank
For some fields there will be a defaultvalue,
If you enter '.', the field will be leftblank.
-----
Country Name (2 letter code) [XX]:CN            #国家代码,必须使用两位数字来描述
State or Province Name (full name) []:HA          #省名或州名
Locality Name (eg, city) [Default City]:ZZ          #城市名
Organization Name (eg, company) [DefaultCompany Ltd]:MagEdu  #组织名或公司名
Organizational Unit Name (eg, section)[]:Ops    #部门
Common Name (eg, your name or your server'shostname) []:www.magedu.com  #此项很重要,证书主题自己的名字,别人和你通信是就使用此名字来联系你,而后你证书中的名字和通信时所使用名字必须要保持一致;否则就认为证书验证无法通过;
Email Address []:caadmin@magedu.com                    #主机的联系人
[root@node-2 CA]# ll
total 20
-rw-r--r--  1 root root 1395 Apr 22 22:40 cacert.pem
drwxr-xr-x  2 root root4096 Oct 15  2014 certs
drwxr-xr-x  2 root root4096 Oct 15  2014 crl
drwxr-xr-x. 2 root root 4096 Oct 15  2014 newcerts
drwx------. 2 root root 4096 Apr 22 22:23 private

注意:构建Ca是给通信方签发证书的

5)、生成相关的辅助文件
[root@node-2 ~]# cd /etc/pki/CA/
[root@node-2 CA]# ls
cacert.pem certs  crl  newcerts private
[root@node-2 CA]# touch index.txt
[root@node-2 CA]# echo 01 > serial
[root@node-2 CA]# cat serial
01

2、给节点发证书
操作步骤:
    1、节点申请证书
    在证书申请的主机上进行如下步骤;
      (1)生成私钥
      (2)生成证书签署请求;               
      (3)把请求发送给CA;
    2、CA签发证书
      (1)验证请求者信息
      (2)签署证书
    (3)把签署好的证书发还给请求者
1)、以http目录为示例,本机给本地签署证书:
123 [root@node-2 ~]# cd /etc/httpd/
[root@node-2 httpd]# mkdir ssl
[root@node-2 httpd]# cd ssl/

2)、生成节点私钥:
12345678 [root@node-2 ssl]# (umask 077; openssl genrsa -out httpd.key 1024)
Generating RSA private key, 1024 bit longmodulus
....................++++++
......++++++
e is 65537 (0x10001)
[root@node-2 ssl]# ll
total 4
-rw------- 1 root root 891 Apr 23 08:19httpd.key

 
3)、生成签署证书请求:
[root@node-2 ssl]# openssl  req -new -key httpd.key-out httpd.csr
You are about to be asked to enterinformation that will be incorporated
into your certificate request.
What you are about to enter is what is calleda Distinguished Name or a DN.
There are quite a few fields but you canleave some blank
For some fields there will be a defaultvalue,
If you enter '.', the field will be leftblank.
-----
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:HA
Locality Name (eg, city) [Default City]:ZZ
Organization Name (eg, company) [DefaultCompany Ltd]:MagEdu
Organizational Unit Name (eg, section)[]:Ops 
Common Name (eg, your name or your server'shostname) []:www.zh.com  #此名称要和证书申请者的FQDN的保持一致
Email Address []:webadmin@zh.com
 
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
[root@node-2 ssl]# ls
httpd.csr httpd.key
#提示:以上的国家、省名、城市名要和CA的保持一致
3、CA签署证书
[root@node-2 ssl]# openssl ca -in /etc/httpd/ssl/httpd.csr -out /etc/httpd/ssl/httpd.crt-days 3360
Using configuration from/etc/pki/tls/openssl.cnf
Check that the request matches the signature
Signature ok
Certificate Details:
        Serial Number: 1(0x1)
        Validity
            Not Before:Apr 23 00:30:38 2015 GMT
            Not After :Jul  4 00:30:38 2024 GMT
        Subject:
          countryName          = CN
          stateOrProvinceName      = HA
          organizationName        = MagEdu
          organizationalUnitName      = Ops
          commonName            =www.zh.com
          emailAddress          =webadmin@zh.com
        X509v3 extensions:
            X509v3 BasicConstraints: 
                CA:FALSE
            NetscapeComment: 
                OpenSSLGenerated Certificate
            X509v3 SubjectKey Identifier: 
              C9:1A:C2:19:08:28:FF:1F:B4:85:90:32:00:E1:5B:E8:FF:6E:7E:95
            X509v3Authority Key Identifier: 
              keyid:66:1E:36:DB:E7:40:30:ED:B1:04:24:86:11:08:73:0C:DF:5E:DD:52
 
Certificate is to be certified until Jul  4 00:30:38 2024 GMT (3360 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

 
操作过程遇到以下问题1:
[root@node-2 ssl]# openssl ca -in/etc/httpd/ssl/httpd.csr -out /etc/httpd/ssl/httpd.crt -days 3360
Using configuration from /etc/pki/tls/openssl.cnf
/etc/pki/CA/index.txt:No such file or directory  #没有/etc/pki/CA/index.txt文件
unable to open '/etc/pki/CA/index.txt'
139960522676040:error:02001002:systemlibrary:fopen:No such file or directory:bss_file.c:398:fopen('/etc/pki/CA/index.txt','r')
139960522676040:error:20074002:BIOroutines:FILE_CTRL:system lib:bss_file.c:400:
操作过程遇到以下问题2:
[root@node-2 CA]# openssl ca -in/etc/httpd/ssl/httpd.csr -out /etc/httpd/ssl/httpd.crt -days 3360
Using configuration from/etc/pki/tls/openssl.cnf
/etc/pki/CA/serial:No such file or directory      #没有/etc/pki/CA/serial文件
error while loading serial number
140170056320840:error:02001002:systemlibrary:fopen:No such file ordirectory:bss_file.c:398:fopen('/etc/pki/CA/serial','r')
140170056320840:error:20074002:BIOroutines:FILE_CTRL:system lib:bss_file.c:400:
4、查看节点所生成的文件:
[root@node-2 ~]# cd /etc/httpd/ssl/
[root@node-2 ssl]# ll
total 8
-rw-r--r-- 1 root root  0 Apr 23 19:47 httpd.crt      #证书文件
-rw-r--r-- 1 root root 676 Apr 23 08:22  httpd.csr      #证书请求文件
-rw------- 1 root root 891 Apr 23 08:19  httpd.key      #私钥文件

OpenSSL 的详细介绍:请点这里
OpenSSL 的下载地址:请点这里

本文永久更新链接地址:http://www.linuxidc.com/Linux/2015-04/116773.htm

linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款