實現https 搭建CA 頒發證書
加密模塊默認沒有安裝,需要安裝加密模塊
[root@localhost ~]# httpd -M | grep ssl
Syntax OK
安裝模塊
[root@localhost ~]# yum install mod_ssl
安裝后查看模塊
[root@localhost ~]# httpd -M | grep ssl
ssl_module (shared)
加載支持加密的模塊的配置文件
[root@localhost ~]# rpm -ql mod_ssl
/etc/httpd/conf.d/ssl.conf
[root@localhost ~]# grep mod_ssl /etc/httpd/conf.d/ssl.conf
# directives see <URL:http://httpd.apache.org/docs/2.2/mod/mod_ssl.html>
LoadModule ssl_module modules/mod_ssl.so 從配置文件中加載了支持加密的模塊
重啟服務
[root@localhost ~]# service httpd restart
SSL加密只支持一個網站,不支持多虛擬主機,刪除創建的虛擬主機
[root@localhost ~]# rm -rf /etc/httpd/conf.d/vhosts.conf
修改主配置文件,使用安裝好httpd時使用的站點目錄
DocumentRoot “/var/www/html”
復制創建一個用于被訪問的文件
[root@localhost ~]# cp /var/log/messages /var/www/html/m.html
添加apache的權限
[root@localhost ~]# chmod +r /var/www/html/m.html
重啟服務
[root@localhost ~]# service httpd restart
訪問
查看證書 [ 自簽名的證書 ]
?
配置文件中定義了加載證書文件的路徑
[root@localhost ~]# rpm -ql mod_ssl
/etc/httpd/conf.d/ssl.conf 配置文件
?
[root@localhost ~]# cat /etc/httpd/conf.d/ssl.conf
SSLCertificateKeyFile /etc/pki/tls/private/localhost.key 私鑰文件路徑
SSLCertificateFile /etc/pki/tls/certs/localhost.crt 證書文件路徑
#SSLCertificateChainFile /etc/httpd/conf.d/ssl/cacert.pem 根證書文件路徑(已經修改過)
查看證書文件
[root@localhost ~]# cat /etc/pki/tls/certs/localhost.crt
—–BEGIN CERTIFICATE—–
[root@localhost ~]# openssl x509 -in /etc/pki/tls/certs/localhost.crt -noout -text
Certificate:
Data:
Version: 3 (0x2)
Serial Number: 2088 (0x828)
Signature Algorithm: sha256WithRSAEncryption
Issuer: C=–, ST=SomeState, L=SomeCity, O=SomeOrganization, OU=SomeOrganizationalUnit, CN=localhost/emailAddress=root@localhost 發布者
Validity
Not Before: Jan 27 08:44:14 2018 GMT
Not After : Jan 27 08:44:14 2019 GMT
Subject: C=–, ST=SomeState, L=SomeCity, O=SomeOrganization, OU=SomeOrganizationalUnit, CN=localhost/emailAddress=root@localhost 頒發給誰
Subject Public Key Info:
/etc/pki/tls/certs/localhost.crt文件是在安裝mod_ssl 時,通過安裝腳本生成的,不屬于任何包
[root@localhost ~]# rpm -ql /etc/pki/tls/certs/localhost.crt
package /etc/pki/tls/certs/localhost.crt is not installed
搭建CA
CA 192.168.119.159
CA服務器
[root@localhost ~]# hostname ca
[root@localhost ~]# exec bash
[root@ca ~]#
Web服務器
[root@localhost ~]# hostname websrv
[root@localhost ~]# exec bash
[root@websrv ~]#
搭建CA
[root@ca ~]# cd /etc/pki/CA/
[root@ca /etc/pki/CA]# tree
.
├── certs
├── crl
├── newcerts
└── private
4 directories, 0 files
[root@ca /etc/pki/CA]#
[root@ca /etc/pki/CA]# touch index.txt
[root@ca /etc/pki/CA]# echo 01 > serial
[root@ca /etc/pki/CA]# (umask 077; openssl genrsa -out private/cakey.pem 2048)
[root@ca /etc/pki/CA]# tree
.
├── certs
├── crl
├── index.txt
├── newcerts
├── private
│?? └── cakey.pem
└── serial
4 directories, 3 files
[root@ca /etc/pki/CA]#
自簽名證書
[root@ca /etc/pki/CA]# openssl req -new -x509 -key private/cakey.pem -out cacert.pem -days 3650
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]:magedu.com
Organizational Unit Name (eg, section) []:opt
Common Name (eg, your name or your server’s hostname) []:ca.magedu.com
Email Address []:
[root@ca /etc/pki/CA]#
[root@ca /etc/pki/CA]# tree
.
├── cacert.pem
├── certs
├── crl
├── index.txt
├── newcerts
├── private
│?? └── cakey.pem
└── serial
4 directories, 4 files
[root@ca /etc/pki/CA]#
Web服務器申請證書
[root@websrv ~]# mkdir /etc/httpd/conf.d/ssl
[root@websrv ~]# cd /etc/httpd/conf.d/ssl
創建證書申請文件
[root@websrv /etc/httpd/conf.d/ssl]# (umask 077; openssl genrsa -out httpd.key)
Generating RSA private key, 1024 bit long modulus
…………++++++
…………………++++++
e is 65537 (0x10001)
[root@websrv /etc/httpd/conf.d/ssl]#
生成證書申請
[root@websrv /etc/httpd/conf.d/ssl]# openssl req -new -key httpd.key -out 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]:bj
Organization Name (eg, company) [Default Company Ltd]:magedu.com
Organizational Unit Name (eg, section) []:opt
Common Name (eg, your name or your server’s hostname) []:*.magedu.com
Email Address []:
Please enter the following ‘extra’ attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
[root@websrv /etc/httpd/conf.d/ssl]# ll
total 8
-rw-r–r– 1 root root 647 Jan 27 17:19 httpd.csr 證書申請文件
-rw——- 1 root root 891 Jan 27 17:16 httpd.key
把證書申請傳到CA進行簽名
[root@websrv /etc/httpd/conf.d/ssl]# scp httpd.csr 192.168.119.159:/etc/pki/CA
CA服務器查看并簽名證書申請
[root@ca /etc/pki/CA]# ls
cacert.pem ?certs ?crl ?httpd.csr??index.txt ?newcerts ?private ?serial
[root@ca /etc/pki/CA]# openssl ca -in httpd.csr -out certs/httpd.crt?-days 712
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: Jan 27 09:22:18 2018 GMT
Not After : Jan ?9 09:22:18 2020 GMT
Subject:
countryName ??????????????= CN
stateOrProvinceName ??????= beijing
organizationName ?????????= magedu.com
organizationalUnitName ???= opt
commonName ???????????????= *.magedu.com
X509v3 extensions:
X509v3 Basic Constraints:
CA:FALSE
Netscape Comment:
OpenSSL Generated Certificate
X509v3 Subject Key Identifier:
69:1C:DF:9F:18:D9:2F:98:1D:EF:71:D0:6D:DB:A3:35:CE:A3:1F:41
X509v3 Authority Key Identifier:
keyid:1E:A3:A2:DF:3E:17:6A:4E:F1:37:F5:4E:AA:E4:61:A8:D4:B5:4A:31
Certificate is to be certified until Jan ?9 09:22:18 2020 GMT (712 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@ca /etc/pki/CA]#
生成的證書文件
[root@ca /etc/pki/CA]# tree
.
├── cacert.pem
├── certs
│?? └── httpd.crt
├── crl
├── httpd.csr
├── index.txt
├── index.txt.attr
├── index.txt.old
├── newcerts
│?? └── 01.pem
├── private
│?? └── cakey.pem
├── serial
└── serial.old
4 directories, 10 files
[root@ca /etc/pki/CA]#
httpd.crt 和 01.pem是同一個文件
把簽過名的證書文件發送和申請的服務器
[root@ca /etc/pki/CA]# scp certs/httpd.crt 192.168.119.129:/etc/httpd/conf.d/ssl/
查看文件
[root@websrv /etc/httpd/conf.d/ssl]# ll
total 12
-rw-r–r– 1 root root 3721 Jan 27 17:24 httpd.crt
-rw-r–r– 1 root root ?647 Jan 27 17:19 httpd.csr 請求文件
-rw——- 1 root root ?891 Jan 27 17:16 httpd.key
修改配置文件
[root@websrv /etc/httpd/conf.d]# vim ssl.conf
ServerName www.magedu.com:443
SSLCertificateFile /etc/httpd/conf.d/ssl/httpd.crt
SSLCertificateKeyFile /etc/httpd/conf.d/ssl/httpd.key
重新啟動服務
[root@websrv /etc/httpd/conf.d]# service httpd restart
把上級CA的證書傳輸給下級CA,否則會導致證書不被信任
[root@ca /etc/pki/CA]# scp cacert.pem 192.168.119.129:/etc/httpd/conf.d/ssl/
查看
[root@websrv /etc/httpd/conf.d/ssl]# ls
cacert.pem??httpd.crt ?httpd.csr ?httpd.key
修改配置文件
[root@websrv /etc/httpd/conf.d/ssl]# vim /etc/httpd/conf.d/ssl.conf
SSLCertificateChainFile /etc/httpd/conf.d/ssl/cacert.pem CA的證書文件
?
重啟服務
[root@websrv /etc/httpd/conf.d/ssl]# service httpd restart
訪問測試
根CA不受信任,需要把CA的證書導入到計算機的受信任的CA證書列表中
[root@websrv /etc/httpd/conf.d/ssl]# ll
total 16
-rw-r–r– 1 root root 1334 Jan 27 17:33 cacert.pem
cacert.pem CA服務器的證書文件,需要導入到計算機的列表中
?
?
導出的文件后綴不對無法打開,所以需要修改文件的后綴
安裝證書
因為簽名的證書是www.magedu.com,所以需要使用FQDN訪問,修改本地的/hosts文件,訪問測試
C:\Windows\System32\drivers\etc\hosts
192.168.119.129 www.magedu.com
本文來自投稿,不代表Linux運維部落立場,如若轉載,請注明出處:http://www.www58058.com/91330