如何使用openssl工具创建私有CA

一、CA及证书

非对称加密是为了保证互联网中通讯信息安全使用的一种算法,密钥是成对出现(公钥和私钥),它的特点是发送方A使用接收方B的公钥加密数据,所有只有B拥有与之配对的私钥解密该数据,反之亦然。那么,A和B之间怎么交换得到对方的真实安全的公钥呢?此时就需要一个权威的机构来验证公钥的合法性,这个机构称之为CA(Certification Authority)。CA为每个使用公开密钥的客户发放数字证书,数字证书的作用是证明证书中列出的客户合法拥有证书中列出的公开密钥。

二、获取证书两种方法
• 使用证书授权机构:生成签名请求(csr) –>将csr发送给CA –> 从CA处接收签名

   如何使用openssl工具创建私有CA

                                                                     图一 CA证书颁发(假设只有一级CA)

很多权威的根CA会被内置到操作系统里面,用户安装系统之后也就会拥有根CA的公钥,所以可以获得上级CA的公钥,进而可以申请证书

如何使用openssl工具创建私有CA

                                                                     图二 主机通过RootCA获得上级CA的公钥

• 自签名的证书: 自已创建根CA并签发自己的公钥
OpenSSL是一个免费开源的库,它提供了构建数字证书的命令行工具,其中一些可以用来自建RootCA

1.创建私有CA

创建之前要了解一下openssl的配置文件: /etc/pki/tls/openssl.cnf 

[ ca ] default_ca      = CA_default            # The default ca section           <--启用的CA名字

[ CA_default ]
dir             = /etc/pki/CA           # 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.             <--证书索引数据库
#unique_subject = no                    # Set to 'no' to allow creation of <--是否允许创建具有相同主题的多个证书
                                        # several certificates with same subject.

new_certs_dir   = $dir/newcerts         # default place for new certs.    
certificate     = $dir/cacert.pem       # The CA certificate               <--自签名的证书
serial          = $dir/serial           # The current serial number        <--当前可用的序列号(下一个要颁发证书的序列号)
crlnumber       = $dir/crlnumber        # the current crl number           <--吊销证书编号
                                        # must be 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 random number file

default_days    = 365                   # how long to certify for          <--证书有效期
default_crl_days= 30                    # how long before next CRL         <--发布吊销证书列表周期
default_md      = sha256                # use SHA-256 by default           <--算法

policy          = policy_match                                             <--使用哪个策略

# For the CA policy
[ policy_match ]
countryName             = match                                            <--CA与客户端的申请信息必须一致
stateOrProvinceName     = match
organizationName        = match
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

a.在CentOS7上创建CA的私钥

[root@centos7 ~]#(umask 066;openssl genrsa -out /etc/pki/CA/private/cakey.pem 2048) <--私钥文件只对属主有权限
Generating RSA private key, 2048 bit long modulus
...+++
.............+++
e is 65537 (0x10001)
[root@centos7 ~]#tree /etc/pki/CA
/etc/pki/CA
├── certs
├── crl
├── newcerts
└── private
    └── cakey.pem
4 directories, 1 file

b.生成自签名证书

[root@centos7 ~]#openssl req -new -x509 \                                    <-- -x509 专用于CA生成自签证书
>         -key  /etc/pki/CA/private/cakey.pem \                              <-- 生成请求时用到的私钥文件
>         -out  /etc/pki/CA/cacert.pem \                                     <-- 证书的保存路径
>         -days 365                                                          <-- 证书的有效期限
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) [XX]:CN
State or Province Name (full name) []:BeiJing   
Locality Name (eg, city) [Default City]:BeiJing
Organization Name (eg, company) [Default Company Ltd]:ffu
Organizational Unit Name (eg, section) []:IT
Common Name (eg, your name or your server's hostname) []:ca.ffu.com      
Email Address []:ffu@outlook.com

c.查看自签名证书信息

[root@centos7 ~]#openssl x509 -in /etc/pki/CA/cacert.pem -noout -text
Certificate:
    Data:
        Version: 3 (0x2)
        Serial Number: 14141409927417363425 (0xc440616792e4fbe1)
    Signature Algorithm: sha256WithRSAEncryption
        Issuer: C=CN, ST=BeiJing, L=BeiJing, O=ffu, OU=IT, CN=ca.ffu.com/emailAddress=ffu@outlook.com
        Validity
            Not Before: Jul 16 08:57:27 2017 GMT
            Not After : Jul 16 08:57:27 2018 GMT
        Subject: C=CN, ST=BeiJing, L=BeiJing, O=ffu, OU=IT, CN=ca.ffu.com/emailAddress=ffu@outlook.com
        Subject Public Key Info:
            Public Key Algorithm: rsaEncryption
                Public-Key: (2048 bit)
                 ....后面省略....

d.创建所需数据库文件

[root@centos7 CA]#touch /etc/pki/CA/index.txt                    <--生成证书索引数据库文件
[root@centos7 CA]#echo 01 > /etc/pki/CA/serial                   <--指定第一个颁发证书的序列号;十六进制,必须是两位数

2.颁发证书

a.生成CentOS6主机的私钥

[root@centos6 ~]#(umask 066;openssl genrsa -out /app/service.key 2048)
Generating RSA private key, 2048 bit long modulus
.............+++
.................................+++
e is 65537 (0x10001)

b.生成证书申请文件

[root@centos6 app]#openssl req -new -key /app/service.key -out /app/service.csr
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) [XX]:CN                                       <--按照所选policy,必须和申请CA的信息一致
State or Province Name (full name) []:BeiJing                              <--按照所选policy,必须和申请CA的信息一致
Locality Name (eg, city) [Default City]:Zhengzhou   
Organization Name (eg, company) [Default Company Ltd]:ffu                  <--按照所选policy,必须和申请CA的信息一致
Organizational Unit Name (eg, section) []:cs
Common Name (eg, your name or your server's hostname) []:*.ffu.com        
Email Address []:

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:

c.将证书请求文件传输给CA

[root@centos6 app]#scp service.csr 192.168.196.166:/etc/pki/CA/

d.CA签署证书,并将证书颁发给请求者

[root@centos7 CA]#openssl ca -in /etc/pki/CA/service.csr -out /etc/pki/CA/certs/service.crt -days 100
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: Jul 16 09:44:51 2017 GMT
            Not After : Oct 24 09:44:51 2017 GMT
        Subject:
            countryName               = CN
            stateOrProvinceName       = BeiJing
            organizationName          = ffu
            organizationalUnitName    = cs
            commonName                = *.ffu.com
        X509v3 extensions:
            X509v3 Basic Constraints: 
                CA:FALSE
            Netscape Comment: 
                OpenSSL Generated Certificate
            X509v3 Subject Key Identifier: 
    89:01:83:51:84:C8:1F:A9:1F:E7:F5:60:6E:6E:5D:5A:2B:59:5A:F2
            X509v3 Authority Key Identifier: 
keyid:A9:5F:1B:D6:F6:7E:99:5D:2F:EE:7D:40:F7:DA:61:AE:29:EE:D1:6F
Certificate is to be certified until Oct 24 09:44:51 2017 GMT (100 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         
[root@centos7 CA]#ll certs/service.crt newcerts/01.pem 
-rw-r--r--. 1 root root 4456 Jul 16 17:45 certs/service.crt
-rw-r--r--. 1 root root 4456 Jul 16 17:45 newcerts/01.pem         <--自动生成以证书序列号命名的文件,内容与证书一致
[root@centos7 CA]#cat index.txt  serial
V       171024094451Z           01      unknown /C=CN/ST=BeiJing/O=ffu/OU=cs/CN=ffu     <--自动生成数据库
02  <--自动更新下一个颁发证书的序列号       

然后,CA就可以把证书发送给主机,主机相关Web服务就可以使用了

3.如何吊销证书

a.在客户端上先查看证书serial–>#openssl x509 -in /etc/pki/CA/service.crt -noout -text

b. 在CA上,根据客户提交的serial与subject信息,对比检验是否与index.txt文件中的信息一致,吊销证书

[root@centos7 CA]#openssl ca -revoke /etc/pki/CA/newcerts/01.pem 
Using configuration from /etc/pki/tls/openssl.cnf
Revoking Certificate 01.
Data Base Updated
[root@centos7 CA]#cat index.txt
R       171024094451Z   170716112929Z   01      unknown /C=CN/ST=BeiJing/O=ffu/OU=cs/CN=ffu  <--R代表removed

c.指定第一个吊销证书的编号

[root@centos7 CA]#echo 01 > /etc/pki/CA/crlnumber    <--第一次更新证书吊销列表前,才需要执行

d.更新证书吊销列表

[root@centos7 CA]#openssl ca -gencrl -out /etc/pki/CA/crl/crl.pem  
Using configuration from /etc/pki/tls/openssl.cnf
[root@centos7 CA]#cat crlnumber
02                                                                                      <--自动更新下一个吊销证书的序列号
[root@centos7 CA]#openssl crl -in /etc/pki/CA/crl/crl.pem -noout -text <--查看吊销证书文件详情
Certificate Revocation List (CRL):
        Version 2 (0x1)
    Signature Algorithm: sha256WithRSAEncryption
        Issuer: /C=CN/ST=BeiJing/L=BeiJing/O=ffu/OU=IT/CN=ffu/emailAddress=ffu@outloo.co
        Last Update: Jul 16 11:35:48 2017 GMT
        Next Update: Aug 15 11:35:48 2017 GMT
        CRL extensions:
            X509v3 CRL Number: 
                1
Revoked Certificates:
    Serial Number: 01
        Revocation Date: Jul 16 11:29:29 2017 GMT
    Signature Algorithm: sha256WithRSAEncryption
            .....后面省略.....

原创文章,作者:ffu,如若转载,请注明出处:http://www.178linux.com/81319

(0)
ffuffu
上一篇 2017-07-16
下一篇 2017-07-16

相关推荐

  • 计算机的组成及Linux简单介绍

    马哥教育网络班22期第一周练习 计算机概述 进入计算机的世界 计算机 计算机(computer)俗称电脑,是能够按照程序运行,自动、高速出炉海量数据的现代化智能电子设备。由硬件系统和软件系统所组成,计算机的组成遵循冯诺依曼体系。可分为超级计算机、工业控制计算机、网络计算机、个人计算机和嵌入式计算机等五类,较先进的计算机有生物计算机、光子计算机、量子计算机等。…

    Linux干货 2016-08-14
  • linux挂载的基本使用

    挂载   挂载是指将一个设备(通常是存储设备)挂接到一个已存在的目录上。 我们要访问存储设备中的文件,必须将文件所在的分区(已有文件系统)挂载到一个已存在的目录上, 然后通过访问这个目录来访问存储设备。 挂载条件 1、挂载点必须是一个目录。 2、一个分区挂载在一个已存在的目录上,这个目录可以不为空,但挂载后这个目录下以前的内容将隐藏不可用。对于其他…

    Linux干货 2016-09-07
  • N25-第一周 总结

    一、描述计算机的组成及其功能     CPU:包括运算器、控制器、寄存器、缓存,计算枢纽,网络的包处理、磁盘读写、数学计算等。     内存:加载数据,提高计算速度,程序被加载到内存成为进程运行。     输入:键盘、鼠标     输出:打印机、显示器 二、按系列罗列linux的…

    Linux干货 2016-12-05
  • N26-第六周作业

    vim编辑器的使用 编辑模式:默认模式编辑模式–>输入模式;i:insert,在光标所在处输入;a:append,在光标所在处后方输入o:在光标所在处下方打开的一个新行;光标处在行首;I:在光标所在行的行首输入A:在光标所在处的行尾输入;O:在光标所在处的上方打开一个新行; 输入模式–>编辑模式ESC 编辑模式&#8211…

    Linux干货 2017-03-13
  • 高级文件系统之-LVM管理

    lvm应用 lvm的重点在于可以弹性的调整filesystem的容量! 而并非在于数据的存储效率及安全上面。 需要文件的读写效能或者是数据的可靠性是RAID所考虑的问题。 lvm:逻辑卷管理器 允许对卷进行方便操作的抽象层,包括重新设定文件系统的大小 允许在多个物理设备间重新组织文件系统 将设备指定为物理卷 用一个或者多个物理卷来创建一个卷组 物理卷是用固定…

    Linux干货 2016-09-02
  • N26-第十一周

    1、详细描述一次加密通讯的过程,结合图示最佳。     发送方:1、使用单项加密算法计算数据文件的特征码2、使用发送方私钥加密特征码3、使用对称加密算法生成一对临时密钥4、使用临时密钥加密数据文件和加密后的特征码5、使用接收方的公钥加密使用临时密钥加密后的数据和特征码和临时密钥的解密密码,并将之发送给接收方 接收方1、使用接收方的私钥解密…

    2017-04-09