CA 认证过程及 https 实现方法

本文详细介绍了CA认证过程,包括CA的功能、证书的作用及证书的生成步骤。并提供了实战教程,涵盖如何搭建CA认证中心,以及在Apache和Nginx上配置HTTPS,包括Apache的80端口跳转443端口和Nginx的配置方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

CA 认证过程

CA 概述:Certificate Authority 的缩写,通常翻译成认证权威或者认证中心,主要用途是为用户发放数字证书。CA 认证的流程和公安局派出所颁发身份证的流程一样
认证中心(CA)的功能有:证书发放、证书更新、证书撤销和证书验证。
CA 证书作用:身份认证,实现数据的不可否认性。
我们先回顾一下身份证的办理过程:
带上户口本(证明你合法)-》当地派出所(认证机构)-》签发证书(审核有效信息)-》你去领证书
接下来我们回顾数字证书认证过程如下图:

D686-E0CF-D309-349E 8A05-3594-ECA5-D0EA B101-B6E7-8A9E-79B8 3698-89DD-C9E3-F045

证书请求文件:CSR 是 Cerificate Signing Request 的英文缩写,即证书请求文件,也就是证书申请者在申请数字证书时由 CSP(加密服务提供者)在生成私钥的同时也生成证书请求文件,证书申请者只要把CSR 文件提交给证书颁发机构后,证书颁发机构使用其根证书的私钥签名就生成了证书文件,也就是颁发给用户的证书。

总结:证书签名过程
1、 生成请求文件
2、 CA 确认申请者的身份真实性
3、 CA 使用根证书的私钥加密请求文件,生成证书
4、 把证书传给申请者

用户使用 CA 证书确认服务器身份过程如下图

1、请求文件发给 centos60 CA 认证中心
2、CA 认证 (用 CA 的私钥加密 centos70 的请求文件), 得到加密后的文件,这个文件就是centos70 的证书 ),CA 认证中心颁发给 centos70 加密后的数字证书
3、用户访问 centos70 并向 centos70 索要数字证书
4、用户拿到数字证书后,用浏览器中 CA 的公钥解密 centos70 的证书,解开后得到 centos70 的公钥和标识(主机名,国家,省,组织等信息),从而确认centos70 的身份。

证书价格:

https://buy.wosign.com/

https://www.shuzizhengshu.com/html/chanpin/SSLzhengshu/

https://freessl.cn/

实战 1:搭建 CA 认证中心

1、安装 CA 认证软件包中心:
[root@centos60 ~]# rpm -qf `which openssl`
openssl-1.0.2k-16.el7.x86_64

2、配置一个自己的 CA 认证中心。生成 CA 的根证书和私钥。 根证书中包括:CA 的公钥
[root@centos60 ~]# vim /etc/pki/tls/openssl.cnf

3、生成 CA 的公钥证书和私钥。
[root@centos60 ~]# /etc/pki/tls/misc/CA -h
usage: /etc/pki/tls/misc/CA -newcert|-newreq|-newreq-nodes|-newca|-sign|-verify

选项 :
-newcert 新证书
-newreq 新请求
-newreq-nodes  新请求节点
-newca  新的 CA 证书
-sign 签证
-verify  验证

[root@centos60 ~]# /etc/pki/tls/misc/CA -newca

[root@centos60 ~]# /etc/pki/tls/misc/CA -newca
CA certificate filename (or enter to create) 直接回车
Making CA certificate ...
Generating a 2048 bit RSA private key
................+++
...............+++
writing new private key to '/etc/pki/CA/private/./cakey.pem'
Enter PEM pass phrase:123456  输入密码,保护私钥
Verifying - Enter PEM pass phrase:  再次输入密码
-----
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]:centos 公司名
Organizational Unit Name (eg, section) []:IT 部门
Common Name (eg, your name or your server's hostname) []:centos.com 您的姓名或您的服务器的主机名用来 指定 CA 认证中心服务器的名字
Email Address []:man@163.com
Please enter the following 'extra' attributes
to be sent with your certificate request添加一个“额外”的属性,让客户端发送 CA 证
书,请求文件时,要输入的密码
A challenge password []:直接回车
An optional company name []:直接回车
Using configuration from /etc/pki/tls/openssl.cnf CA服务器的配置文件。上面修改的
内容会添加到这个配置文件中
Enter pass phrase for /etc/pki/CA/private/./cakey.pem:123456 输入刚才保护 CA
私钥的密码
Write out database with 1 new entries
Data Base Updated 到此 CA 认证中心就搭建好了。

4、查看生成的 CA 根证书,根证书中包括 CA 公钥
[root@centos60 ~]# vim /etc/pki/CA/cacert.pem

5、查看根证书的私钥
[root@centos60 ~]# vim /etc/pki/CA/private/cakey.pem

实战 2:在 apache 上搭建 https

1、基于 apache 搭建 https 的整体流程如下:
(1)、在 centos70上安装 httpd
(2)、centos70 生成证书请求文件,发给 centos60 CA 认证中心进行签名,centos60 下发证书  给 centos70
(3)、把证书和 httpd 相结合,实现 https
(4)、测试 https 认证效果

2、安装:httpd web 服务器
[root@centos70 ~]# yum -y install httpd

[root@centos70 ~]# vim /etc/httpd/conf/httpd.conf

[root@centos70 ~]# systemctl start httpd


3、xuegod64 生成证书请求文件,获得证书
[root@centos70 ~]# openssl -h
[root@centos70 ~]# openssl genrsa -des3 -out /etc/httpd/conf.d/server.key

Generating RSA private key, 2048 bit long modulus
...................................+++
...........................................................+++
e is 65537 (0x10001)
Enter pass phrase for /etc/httpd/conf.d/server.key:123456 输入保护私钥的密码,保护私
钥时,使用的加密算法是 -des3
Verifying - Enter pass phrase for /etc/httpd/conf.d/server.key:123456 重复输入
有私钥可以推出来公钥,但是公钥不可以推出私钥。公钥由私钥生成

4、使用私钥生成证书请求文件

[root@centos70 ~]# openssl req -new -key /etc/httpd/conf.d/server.key -out /server.csr

Enter pass phrase for /etc/httpd/conf.d/server.key:123456 输入私钥的密码
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 注意添加的国家,省,组织等信息要和 CA 保持一致
State or Province Name (full name) []:beijing
Locality Name (eg, city) [Default City]:haidian
Organization Name (eg, company) [Default Company Ltd]:centos
Organizational Unit Name (eg, section) []:IT
Common Name (eg, your name or your server's hostname) []:centos.com这里要求输入的 Common Name 必须与通过浏览器访问您网站的 URL 完全相同,否则用户会发现您服务器证书的通用名与站点的名字不匹配,用户就会怀疑您的证书的真实性。可以使域名也可以使 IP 地址。
Email Address []:MAN@163.com
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:不输密码直接回车
An optional company name []:直接回车
证书请求文件中有 centos70 的公钥。 这个公钥是在生成证书请求文件时,通过指定的私钥/etc/httpd/conf.d/server.key 生成的。
常识: 通过私钥可以生成公钥的,通过公钥不可以推出来私钥。

5、将证书请求文件发给 CA 服务器:
[root@centos70 ~]# scp /server.csr 192.168.0.60:/tmp/ 

6、CA 签名
[root@centos60 ~]# openssl ca -keyfile /etc/pki/CA/private/cakey.pem -cert /etc/pki/CA/cacert.pem -in /tmp/server.csr -out /server.crt

Using configuration from /etc/pki/tls/openssl.cnf
Enter pass phrase for /etc/pki/CA/private/cakey.pem:123456
Check that the request matches the signature
Signature ok
Certificate Details:
        Serial Number:
            81:23:1f:d4:a9:6c:8d:18
        Validity
            Not Before: Nov 20 09:22:58 2020 GMT
            Not After : Nov 20 09:22:58 2021 GMT
        Subject:
            countryName               = CN
            stateOrProvinceName       = guangzhou
            organizationName          = centos
            organizationalUnitName    = IT
            commonName                = centos.com
            emailAddress              = man@163.com
        X509v3 extensions:
            X509v3 Basic Constraints: 
                CA:FALSE
            Netscape Comment: 
                OpenSSL Generated Certificate
            X509v3 Subject Key Identifier: 
                4D:20:79:9C:91:BD:FE:0A:47:70:EB:3B:1F:5B:D1:56:3C:4D:7D:11
            X509v3 Authority Key Identifier: 
                keyid:41:E0:EC:04:07:00:23:76:74:B8:50:67:45:98:D6:0E:67:1A:CC:E8

Certificate is to be certified until Nov 20 09:22:58 2021 GMT (365 days)#证书有效期是
365 天。证书进行认证,直到 11 月 20 日09时 22 分 58 秒格林尼治标准时间 2021 年(365 天)
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

7、将证书复制到 xuegod64
[root@centos60 ~]# scp /server.crt 192.168.0.70:/

创建证书出现如下报错。

failed to update database
TXT_DB error number 2

[root@centos60 CA]# > /etc/pki/CA/index.txt  #清空该index.txt文件即可

实战 3:使用证书实现 https

SSL 四次握手实现安全传输数据

1、SSL 概述:(Secure Socket Layer)安全套接字层,通过一种机制在互联网上提供密钥传输。其主要目标是保证两个应用间通信数据的保密性和可靠性,可在服务器端和用户端同时支持的一种加密算法。目前主流版本 SSLV2、SSLV3(常用)。

注:SSL作用,可以让你在一个不安全的公网环境中,安全传输你的密钥。

2、SSL 应用情景:

 

3、SSL 四次握手安全传输流程如下:

C(client 客户端) ------------------------------> S(server 服务端)
(1)、 客户端请求一个安全的会话,协商加密算法
C <------------------------------------------------- S
(2)、服务端将自己的证书传给客户端
C -------------------------------------------------> S
(3)、客户端用浏览中存放CA的根证书检测centos70证书,确认centos70是我要访问的网站。客户端使用CA根证书中的公钥解密centos70的证书,从而得到centos70的公钥;然后客户端生成一把对称的加密密钥,用centos70的公钥加密这个对称加密的密钥发给centos70。
后期使用对称密钥加密数据
C <------------------------------------------------> S
(4)、centos70使用私钥解密,得到对称加密的密钥。从而,使用对称加密密钥来进行安全快速传输数据。这里使用对称加密数据,是因为对称加密和解密速度快
总结 SSL 四次握手流程整体分两个过程:
过程 1: 确认身份;过程 2:生成一把对称加密密钥,传输数据。

在 centos70 上配置 HTTPS web 服务器

1、安装 SSL 模块
[root@centos70 ~]# yum -y install mod_ssl

2、配置 apache 加载证书文件 (假如,要修改443端口为8443,那查找443,找到的都需要修改为8443)
[root@centos70 ~]# cp /server.crt /etc/httpd/conf.d/
[root@centos70 ~]# ls /etc/httpd/conf.d/server.key
/etc/httpd/conf.d/server.key

[root@centos70 ~]# vim /etc/httpd/conf.d/ssl.conf

SSLCertificateFile /etc/httpd/conf.d/server.crt
SSLCertificateKeyFile /etc/httpd/conf.d/server.key

3、启动服务:

[root@centos70 ~]# systemctl restart httpd
Enter SSL pass phrase for 192.168.0.70:443 (RSA) : ******           #httpd 私钥密码

4、测试 https,查看端口

[root@centos70 ~]# netstat -anput | grep 443
tcp6       0      0 :::443                  :::*                    LISTEN      6962/httpd

5、通过浏览器测试 https 效果

apache 80 跳转 443

[root@centos70 conf]# vim httpd.conf

Include conf.modules.d/*.conf
RewriteEngine on
RewriteCond %{SERVER_PORT} !^443$
RewriteCond %{REQUEST_URI} !^/exception.html
RewriteRule (.*) https://%{SERVER_NAME}$1 [R]

或者

Include conf.modules.d/*.conf
RewriteEngine on
RewriteCond %{SERVER_PORT} !^443$
RewriteCond %{REQUEST_URI} !^/expection.html
RewriteRule (.*) https://%{SERVER_NAME}%{REQUEST_URI} [R]

或者

RewriteEngine on
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]

实战 4:使用 Nginx 实现 https

[root@centos70 ~]# systemctl stop httpd
[root@centos70 ~]# ss -lnput

安装 Nginx
[root@centos70 ~]# vim /etc/yum.repos.d/nginx.repo

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/rhel/7/$basearch/
gpgcheck=0
enabled=1

[root@centos70 ~]# yum -y install nginx
[root@centos70 ~]# rpm -ql nginx
[root@centos70 ~]# cd /etc/nginx

启动 Nginx:
[root@centos70 nginx]# systemctl start nginx

测试 Nginx:在浏览器中输入:http://192.168.0.70

配置 Nginx 实现 https

[root@centos70 nginx]# vim nginx.conf

多个网站可以单独写多个配置文件,比较容易管理。

[root@centos70 nginx]# vim conf.d/ssl.conf

server {
    listen       443 ssl;
    keepalive_timeout 70;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers AES128-SHA:AES256-SHA:RC4-SHA:DES-CBC3-SHA:RC4-MD5;
    ssl_certificate /etc/httpd/conf.d/server.crt;
    ssl_certificate_key /etc/httpd/conf.d/server.key;
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 10m;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

[root@centos70 nginx]# nginx -t
Enter PEM pass phrase:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

重启 Nginx:
[root@centos70 nginx]# systemctl restart nginx
Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details.

[root@centos70 nginx]# journalctl -xe

错误:Enter PEM pass phrase:
是 systemctl 没有给出输入密码的接口

[root@centos70 nginx]# yum -y install psmisc
[root@centos70 nginx]# killall nginx

[root@centos70 nginx]# nginx
Enter PEM pass phrase:123456 #私钥的密码

[root@centos70 nginx]# nginx -s reload
Enter PEM pass phrase:123456 #私钥的密码
 

[root@centos70 nginx]# yum -y install lsof

[root@centos70 nginx]# lsof -i:443
COMMAND  PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
nginx   7203  root    6u  IPv4  51894      0t0  TCP *:https (LISTEN)
nginx   7204 nginx    6u  IPv4  51894      0t0  TCP *:https (LISTEN)
[root@centos70 nginx]# netstat -anput | grep 443
tcp        0      0 0.0.0.0:443             0.0.0.0:*               LISTEN      7203/nginx: master

测试 https:
在浏览器中输入:https://192.168.0.70/

nginx 80 跳转 443

[root@centos70 conf.d]# vim default.conf

server {
    listen       80;
    server_name  www.centos.com;
    rewrite (.*) https://$server_name$request_uri;

[root@centos70 conf.d]# nginx -s stop
Enter PEM pass phrase:
[root@centos70 conf.d]# netstat -anput | grep nginx
[root@centos70 conf.d]# nginx
Enter PEM pass phrase:

tomcat 80 跳转 443

链接:https://pan.baidu.com/s/1SyiR-i5YL4flKgfrMmdzeQ 
提取码:1234 

[root@centos70 conf]# vim web.xml

[root@centos70 conf]# vim server.xml

https://blog.51cto.com/37003839/2427385

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值