使用OpenSSL搭建CA

1、html

SSL/TLSweb

1.機密性: 數據加密(對稱加密)vim

2.完整性:保證數據不會被篡改(或是由於數據傳輸丟失) (單向加密,提升特徵碼,附加到數據後)瀏覽器

3. 可靠性:安全

 

2、概述服務器

如何實現「身份驗證和數據加密」框架

發送方:dom

    計算數據特徵值 --》 使用發送方的私鑰進行加密特徵值 --》隨機生成密鑰對加密整個數據 --》使用接受方公鑰加密curl

    {用隨機碼加密整個數據(數據自己)【用發送方的私鑰加密(特徵值)】}【用接受方的公鑰進行加密{隨機碼}】ui

 

接受方:

    使用私鑰解密(能得到隨機碼)  --》用隨機碼進行解密,得到整個數據 --》 使用發送公鑰解密,驗證身份 --》 比較數據特徵值

 

 

誰來管理公鑰,任何在互聯網上傳播的數據都不安全,更不用說傳遞公鑰,它若是被篡改,那麼就沒法驗證身份了。因此不可能用戶本身頒發公鑰

    CA:正式頒發機構

    PKI:公鑰基礎設施,公鑰基礎框架

    證書:裏面存放用戶各類信息,最核心就是公鑰

 

誰來給CA頒發公鑰 =》 CA本身給本身頒發公鑰

3、

1.安裝openssl

[root@hf0001 conf.d]# rpm -qa|grep openssl
openssl-1.0.1e-16.el6_5.15.x86_64

2.構建CA

hf0001做爲CA服務器

[root@hf0001 conf.d]# cd /etc/pki/CA
[root@hf0001 CA]# pwd
/etc/pki/CA

首先CA服務器本身給本身頒發證書

1.CA服務器生成
生成private/cakey.pem 目錄能夠隨意,默認在/etc/pki/CA/private目錄下,其餘目錄須要配置
[root@hf0001 CA]# (umask 077;openssl genrsa -out private/cakey.pem 2048)
...
...
2.CA服務器給本身頒發證書(須要填寫必要休息,如國家、城市、機構)
[root@hf0001 CA]# openssl req -new -x509 -key private/cakey.pem -out cacert.pem -days 3650
...
...
    2.1 也能夠修改配置文件,添加默認參數(找到req_distinguished_name節點)
    [root@hf0001 CA]# vim /etc/pki/tls/openssl.conf
    ...
    ...
    [ req_distinguished_name ]
    countryName                     = Country Name (2 letter code)
    countryName_default             = CN
    countryName_min                 = 2
    countryName_max                 = 2
    
    stateOrProvinceName             = State or Province Name (full name)
    #stateOrProvinceName_default    = Default Province
    stateOrProvinceName_default     = JiangSu
    ...
    
3.新建相關文件
[root@hf0001 CA]# pwd
/etc/pki/CA

index.txt相關於db,記錄必要的信息,
serial 序列號,只要頒發一個證書,則++
[root@hf0001 CA]# touch index.txt serial crlnumber
[root@hf0001 CA]# echo 00 > serial

4.修改openssl.conf的配置文件
[root@hf0001 CA]# vim /etc/pki/tls/openssl.conf
...
...
[ 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 ctificates 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                              
....

3.hf0002做爲web服務器,

3.1安裝httpd

3.2 安裝ssl_module

3.3 配置基於域名的虛擬主機 

[root@hf0002 conf.d]# pwd
/etc/httpd/conf.d

[root@hf0001 conf.d]# vim virtual-domain.conf 
Listen 80
NameVirtualHost 10.224.243.57:80

<VirtualHost 10.224.243.57:80>
        ServerName www.harry.com
        DocumentRoot "/var/www/html/harry.com"
</VirtualHost>

<VirtualHost 10.224.243.57:80>
        ServerName www.harry3.com
        DocumentRoot "/var/www/html/harry3.com"
</VirtualHost>

<VirtualHost 10.224.243.57:80>
        ServerName www.harry4.com
        DocumentRoot "/var/www/html/harry4.com"
</VirtualHost>

3.4 web服務器生成祕鑰,CA給該服務器頒發證書

    3.4.1 安裝openssl

    3.4.2 生成祕鑰

[root@hf0002 httpd]# pwd
/etc/httpd

[root@hf0002 httpd]# mkdir ssl

[root@hf0002 httpd]# cd ssl
[root@hf0002 ssl]# pwd
/etc/httpd/ssl

生成祕鑰
[root@hf0002 httpd]# (umask 077;openssl genrsa -out httpd.key 1024)
生成證書請求信息
[root@hf0002 ssl]# openssl req -new -key httpd.key -out httpd.cst 
注意這裏也要添加必要的信息,這樣信息必須和CA服務的保持一致。 其中有一項須要添加域名   添加本身的SSL服務的域名 
...
...

把證書請求信息發送給hf0001
[root@hf0002 ssl]# scp httpd.crt ocp@10.224.243.57:/home/ocp
....

   3.4.3 CA服務器給該請求進行認證,並頒發證書

對以前hf0002發送的httpd.crt進行認證
[root@hf0001 ssl]# openssl ca -in httpd.cst -out httpd.crt -days 365

把證書發送給hf0002
[root@hf0001 ssl]# scp httpd.crt ocp@10.224.243.58:/home/ocp

   3.4.4 這樣hf002web服務器就有了CA服務器頒發的證書了,

   3.4.5 配置SSL服務

[root@hf0002 conf.d]# pwd
/etc/httpd/conf.d

[root@hf0002 conf.d]# vim ssl.conf
...
Listen 443
...

<VirtualHost 10.224.243.58:443>

# General setup for the virtual host, inherited from global configuration
DocumentRoot "/var/www/html/harry.com"            => 根路徑
ServerName 
...
</ 

...
#   Server Certificate:
# Point SSLCertificateFile at a PEM encoded certificate.  If
# the certificate is encrypted, then you will be prompted for a
# pass phrase.  Note that a kill -HUP will prompt again.  A new
# certificate can be generated using the genkey(1) command.
SSLCertificateFile /etc/httpd/ssl/httpd.crt                    => 指向CA服務器頒發的證書文件

#   Server Private Key:
#   If the key is not combined with the certificate, use this
#   directive to point at the key file.  Keep in mind that if
#   you've both a RSA and a DSA private key you can configure
#   both in parallel (to also allow the use of DSA ciphers, etc.)
SSLCertificateKeyFile /etc/httpd/ssl/httpd.key                => 指向私鑰文件
...

3.5 驗證

由於我既配置了http 也配置了 https 

[root@hf0001 conf.d]# curl 'http://www.harry.com'
<h1>SSL www.harry.com:443</h1>                        => 目錄須要本身定義,index.html須要本身寫

[root@hf0001 conf.d]# curl 'https://www.harry.com'    => 須要提供證書
curl: (60) Peer certificate cannot be authenticated with known CA certificates
More details here: http://curl.haxx.se/docs/sslcerts.html

curl performs SSL certificate verification by default, using a "bundle"
 of Certificate Authority (CA) public keys (CA certs). If the default
 bundle file isn't adequate, you can specify an alternate file
 using the --cacert option.
If this HTTPS server uses a certificate signed by a CA represented in
 the bundle, the certificate verification probably failed due to a
 problem with the certificate (it might be expired, or the name might
 not match the domain name in the URL).
If you'd like to turn off curl's verification of the certificate, use
 the -k (or --insecure) option.
 
 注意:客戶端須要誰的證書CA服務器仍是Httpd服務的?
 
 理解過程:CA服務器提供了加密服務,以及身份驗證.可是CA服務器自己的身份誰來認證?
 
 沒有人。咱們必須前提認爲CA服務器是權威的。因此咱們客戶端須要CA服務器的證書
 
 CA生成的證書發送給客戶端
 [root@hf0001 conf.d]# curl --cacert /etc/pki/CA/cacert.pem 'https://www.harry.com'
 <h1>SSL www.harry.com:443</h1>
 
 
 對於瀏覽器,須要把cacert.pem => cacert.crt 並導入到瀏覽器中,便可。並信任該證書
相關文章
相關標籤/搜索