openssl建立私有CA和申請證書

實驗環境:

虛擬機:VMware? Workstation 12 Pro
主機A:ip為10.1.255.55/16,創建CA并給其他主機提供CA服務
主機B:為httpd服務器,ip為10.1.249.115/16

1、查看openssl的配置文件/etc/pki/tls/openssl.cnf

[root@localhost ~]# cat /etc/pki/tls/openssl.cnf (查看配置文件的ca部分的內容)

......

[ ca ]
default_ca      = CA_default            # The default ca section

####################################################################
[ 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
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
crl             = $dir/crl.pem          # The current CRL
private_key     = $dir/private/cakey.pem# The private key
RANDFILE        = $dir/private/.rand    # private random number file

x509_extensions = usr_cert              # The extentions to add to the cert

# Comment out the following two lines for the "traditional"
# (and highly broken) format.
name_opt        = ca_default            # Subject Name options
cert_opt        = ca_default            # Certificate field options

# Extension copying option: use with caution.
# copy_extensions = copy

# Extensions to add to a CRL. Note: Netscape communicator chokes on V2 CRLs
# so this is commented out by default to leave a V1 CRL.
# crlnumber must also be commented out to leave a V1 CRL.
# crl_extensions        = crl_ext

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
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             = match
stateOrProvinceName     = match
organizationName        = match
organizationalUnitName  = optional
commonName              = supplied
emailAddress            = optional

......

2、根據配置文件創建所需要的文件

[root@localhost ~]# touch /etc/pki/CA/index.txt
[root@localhost ~]# echo 01 > /etc/pki/CA/serial
[root@localhost ~]# ls /etc/pki/CA/
certs  crl  index.txt  newcerts  private  serial

注意:文件名要與配置文件中的名字一樣

3、在主機A上創建CA服務,并自簽

(1)生成私鑰

[root@localhost ~]# (umask 077 ; openssl genrsa -out /etc/pki/CA/private/cakey.pem 2048)
Generating RSA private key, 2048 bit long modulus
......................................................................................................................................................................................+++
....................................................................................................................+++
e is 65537 (0x10001)

用小括號說明是在子shell中執行括號內的命令,不會影響父shell的設置;將umask設置為077是為了防止其他人有權限查看和修改生成的私鑰;在2048前還可以給私鑰加上加密算法,比如3des rsa等,此例子不加密。

(2)生成自簽證書

[root@localhost ~]# openssl req -new -x509 -key /etc/pki/CA/private/cakey.pem -days 7300
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]:haidian  
Organization Name (eg, company) [Default Company Ltd]:linuxca.org       
Organizational Unit Name (eg, section) []:ops
Common Name (eg, your name or your server's hostname) []:linuxCA  
Email Address []:admin@linuxca.org

-new : 生成新證書簽署請求
-x509 : 專用于CA生成自簽證書
-key : 生成請求時用到的私鑰文件
-days: 證書的有效期限
-out /PATH/TO/SOMECERTFILE : 證書的保存路徑

[root@localhost ~]# ls /etc/pki/CA/
cacert.pem  certs  crl  index.txt  newcerts  private  serial
[root@localhost ~]# cat /etc/pki/CA/cacert.pem 
-----BEGIN CERTIFICATE-----
MIID5zCCAs+gAwIBAgIJAJrY1Gr0+l+fMA0GCSqGSIb3DQEBCwUAMIGJMQswCQYD
VQQGEwJjbjEQMA4GA1UECAwHYmVpamluZzEQMA4GA1UEBwwHaGFpZGlhbjEUMBIG
A1UECgwLbGludXhjYS5vcmcxDDAKBgNVBAsMA29wczEQMA4GA1UEAwwHbGludXhD
QTEgMB4GCSqGSIb3DQEJARYRYWRtaW5AbGludXhjYS5vcmcwHhcNMTYwOTIzMDEw
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enN9l7ETtYf1pfP+j6uTVF3Cd5TEpxKuRxQ==
-----END CERTIFICATE-----

4、在需要使用證書的主機(B)生成證書請求

(1)給httpd服務器生成私鑰

[root@localhost ~]# mkdir /etc/httpd/ssl
[root@localhost ~]# (umask 077 ; openssl genrsa -out /etc/httpd/ssl/httpd.key 2048)
Generating RSA private key, 2048 bit long modulus
...........+++
...............+++
e is 65537 (0x10001)

(2)生成證書申請文件

[root@localhost ~]# oepnssl req -new -key /etc/httpd/ssl/httpd.key -days 365 -out /etc/httpd/ssl/httpd.csr
-bash: oepnssl: command not found
[root@localhost ~]# openssl req -new -key /etc/httpd/ssl/httpd.key -days 365 -out /etc/httpd/ssl/httpd.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
State or Province Name (full name) []:beijing
Locality Name (eg, city) [Default City]:chaoyang
Organization Name (eg, company) [Default Company Ltd]:linuxca.org
Organizational Unit Name (eg, section) []:cwb  
Common Name (eg, your name or your server's hostname) []:lovelinux
Email Address []:lovelinux@163.com

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:   此處不加密證書申請文件  
An optional company name []:

注意:默認國家,省,公司名稱必須和CA一致

(3)將證書請求文件傳輸給CA

[root@localhost ~]# ls /etc/httpd/ssl/httpd.
httpd.csr  httpd.key  
[root@localhost ~]# scp /etc/httpd/ssl/httpd.csr 10.1.252.55:/testdir/
root@10.1.252.55's password: 
httpd.csr                                   100% 1054     1.0KB/s   00:00

5、在主機A上簽署證書,并頒發給請求者

(1)簽署證書

[root@localhost testdir]# openssl ca -in /testdir/httpd.csr -out /etc/pki/CA/certs/httpd.crt -days 365
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: Sep 23 01:31:29 2016 GMT
            Not After : Sep 23 01:31:29 2017 GMT
        Subject:
            countryName               = cn
            stateOrProvinceName       = beijing
            organizationName          = linuxca.org
            organizationalUnitName    = cwb
            commonName                = lovelinux
            emailAddress              = lovelinux@163.com
        X509v3 extensions:
            X509v3 Basic Constraints: 
                CA:FALSE
            Netscape Comment: 
                OpenSSL Generated Certificate
            X509v3 Subject Key Identifier: 
                D0:73:48:EE:41:62:D5:61:30:16:09:8D:9B:04:BD:5B:B3:5F:FD:1D
            X509v3 Authority Key Identifier: 
                keyid:1F:16:80:FF:36:CA:A4:33:A2:77:DF:ED:AF:DD:D6:CB:AE:AE:88:5B

Certificate is to be certified until Sep 23 01:31:29 2017 GMT (365 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

(2)頒發證書

[root@localhost testdir]# ls /etc/pki/CA/certs/
httpd.crt
[root@localhost testdir]# scp /etc/pki/CA/certs/httpd.crt 10.1.249.115:/etc/httpd/ssl
root@10.1.249.115's password: 
httpd.crt                                   100% 4613     4.5KB/s   00:00

至此,CA的創建和申請實驗已完成。

原創文章,作者:pao,如若轉載,請注明出處:http://www.www58058.com/48639

(0)
paopao
上一篇 2016-09-23
下一篇 2016-09-23

相關推薦

  • RPM程序包管理器及其前端工具YUM

    RPM:     RPM,Redhat Package Manager,RPM is Package Manager 功能:將編譯好的應用程序的各組成文件打包一個或幾個程序包文件,從而方便快捷地實現程序包的安裝、卸載、查詢、升級和校驗等管理操作 1)RPM包的命名 2)RPM包文件的組成   &nb…

    Linux干貨 2016-08-21
  • 馬哥網絡班21期-第四周作業

    1、復制/etc/skel目錄為/home/tuser1,要求/home/tuser1及其內部文件的屬組和其它用戶均沒有任何訪問權限。        cp -r /etc/skel /home/tuser1        chmod 700 /home/tuser1  …

    Linux干貨 2016-08-02
  • 磁盤管理之MBR,GPT的概念及MBR和GPT分區工具的使用

    一,概述 設備文件 I/O ports: I/O設備地址 一切皆文件 open() read() write()close() 設備類型: 塊設備:block,存取單位“塊”,磁盤 字符設備:char,存取單位”字符“,鍵盤 塊設備和字符設備的區別 塊設備可以隨機訪問,字符設備必須連續訪問,如,/dev/null 塊設備有緩存,字符設備沒有緩存,因為字符設備…

    Linux干貨 2016-09-06
  • mysql5.6 GTID的實現以及maridb 10.9多主一從的架構

    一、MySQL 5.6 以后出現的GTID:     GTID概念:          1. GTID是一個由服務器的UUID和事務序號組成的唯一事務序號       &…

    Linux干貨 2015-12-18
  • HA Cluste 的配置:keepalived:vrrp協議 主備模型

    keepalived:vrrp協議 主備模型 第一步配置出始環境: 準備兩節點: 主節點:172.18.57.7 long1 備節點:172.18.57.8 long2 (1) 各節點時間必須同步; 同步時間: ~]# yum -y install chrony ~]# vim /etc/chrony.conf ~]# systemctl start chr…

    Linux干貨 2017-05-15
  • 第三周博客作業

    1、列出當前系統上所有已經登錄的用戶的用戶名,注意:同一個用戶登錄多次,則只顯示一次即可。 [root@localhost ~]# who (unknown) :0           2016-12-15 2…

    Linux干貨 2016-12-20

評論列表(1條)

  • 馬哥教育
    馬哥教育 2016-09-23 11:38

    自建CA與頒發證書過程很完整?想不想挑戰一下自己,寫腳本來實現這些步驟呢?

欧美性久久久久