内部CA服务器和自签名证书

相关介绍

在HTTPS的传输过程中,有一个非常关键的角色——数字证书,那什么是数字证书?又有什么作用呢?

所谓数字证书,是一种用于电脑的身份识别机制。由数字证书颁发机构(CA)对使用私钥创建的签名请求文件做的签名(盖章),表示CA结构对证书持有者的认可。数字证书拥有以下几个优点:

  1. 使用数字证书能够提高用户的可信度
  2. 数字证书中的公钥,能够与服务端的私钥配对使用,实现数据传输过程中的加密和解密
  3. 在证认使用者身份期间,使用者的敏感个人数据并不会被传输至证书持有者的网络系统上

X.509证书包含三个文件:key,csr,crt。

  • key是服务器上的私钥文件,用于对发送给客户端数据的加密,以及对从客户端接收到数据的解密
  • csr是证书签名请求文件,用于提交给证书颁发机构(CA)对证书签名
  • crt是由证书颁发机构(CA)签名后的证书,或者是开发者自签名的证书,包含证书持有人的信息,持有人的公钥,以及签署者的签名等信息

备注:在密码学中,X.509是一个标准,规范了公开秘钥认证、证书吊销列表、授权凭证、凭证路径验证算法等。

创建CA

openssl 的配置文件:/etc/pki/tls/openssl.cnf

重要参数配置路径

dir = /etc/pki/CA # Where everything is kept

certs = /etc/pki/CA/certs # Where the issued certs are kept

database = /etc/pki/CA/index.txt # database index file.

new_certs_dir = /etc/pki/CA/newcerts # default place for new certs.

certificate = /etc/pki/CA/cacert.pem # The CA certificate

serial = /etc/pki/CA/serial # The current serial number

private_key = /etc/pki/CA/private/cakey.pem # The private key

创建所需要的文件

1
2
touch /etc/pki/CA/index.txt  生成证书索引数据库文件
echo 01 > /etc/pki/CA/serial 指定第一个颁发证书的序列号,必须是两位十六进制数,99之后是9A

生成私钥

1
2
3
cd /etc/pki/CA/
umask 066
openssl genrsa -out /etc/pki/CA/private/cakey.pem 2048

生成自签名证书

1
2
3
4
5
6
7
openssl req -new -x509 –key /etc/pki/CA/private/cakey.pem -days 7300 -out /etc/pki/CA/cacert.pem
-new: 生成新证书签署请求
-x509: 专用于 CA 生成自签证书
-key: 生成请求时用到的私钥文件
-days n:证书的有效期限
-out: 证书的保存路径
提示输入国家,省,市,公司名称,部门名称,CA主机名(颁发者名称)

查看证书(Windows下查看生成的自签名证书,需要更改上述文件名后缀为.cer即可查看)

1
openssl x509 -in /etc/pki/CA/cacert.pem -noout -text

颁发证书

在需要证书的主机生成证书请求

如给nginx服务器生成私钥

1
2
3
4
mkdir /root/key
cd /root/key
umask 066
openssl genrsa -out key/service.key 2048

生成证书申请文件

1
openssl req -new -key service.key -out service.csr

同样提示输入国家,省,市等信息。注意:国家,省,公司名称三项必须和CA一致。主机名称必须和网站域名相同,如www.centos73.com。或者使用泛域名,即*.centos73.com,匹配所有

将证书文件移动到CA服务器/etc/pki/CA/csr目录下

1
scp /app/service.csr 192.168.10.15:/etc/pki/CA/csr/

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

1
openssl ca -in /etc/pki/CA/crl/service.csr -out /etc/pki/CA/certs/service.crt -days 365

生成certs/service.crt和newcerts/xx.pem文件,两个文件相同。

查看证书中的信息

1
2
3
4
5
openssl x509 -in certs/service.crt -noout -text|issuer|subject|serial|dates
cat serial
cat index.txt //V表示当前证书的状态正常
openssl ca -status SERIAL 查看指定编号的证书状态
cat index.txt.attr //yes表示subjects信息必须是唯一的,不能重复申请

吊销证书

1
2
3
4
5
6
7
8
9
10
11
12
13
(1)在客户端获取要吊销的证书的serial
openssl x509 -in /etc/pki/CA/cacert.pem -noout -serial -subject
(2)在CA上,根据客户提交的serial与subject信息,对比检验是否与index.txt文件中的信息一致,吊销证书:
openssl ca -revoke /etc/pki/CA/newcerts/xx.pem
cat index.txt //R表示证书已经失效
(3)指定第一个吊销证书的编号
注意:第一次更新证书吊销列表前,才需要执行
echo 01 > /etc/pki/CA/crlnumber
(4)更新证书吊销列表:
openssl ca -gencrl -out /etc/pki/CA/crl.pem
linux下查看crl文件:
openssl crl -in /etc/pki/CA/crl.pem -noout -text
Windows下查看吊销列表文件,需更改文件后缀为.crl

另外一种配置nginx自签名证书

生成私钥

1
openssl genrsa  -out server.key 2048

生成CSR

1
openssl req -new -key server.key -out server.csr

生成自签名证书

1
openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt

Nginx 配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
[root@nginx logs]# cat /usr/local/nginx/conf/nginx.conf

#user nobody;
worker_processes 1;

#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid logs/nginx.pid;


events {
worker_connections 1024;
}


http {
include mime.types;
default_type application/octet-stream;

#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';

#access_log logs/access.log main;

sendfile on;
#tcp_nopush on;

#keepalive_timeout 0;
keepalive_timeout 65;

#gzip on;

server {
listen 80;
server_name localhost;

#charset koi8-r;

#access_log logs/host.access.log main;

location / {
root html;
index index.html index.htm;
}

#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}

# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}


# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;

# location / {
# root html;
# index index.html index.htm;
# }
#}


# HTTPS server
#
server {
listen 443 ssl;
server_name nginx.iecas.com;

ssl_certificate /etc/pki/CA/certs/server.crt;
ssl_certificate_key /etc/pki/CA/private/server.key;

ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;

ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;

location / {
root html;
index index.html index.htm;
}
}

}
--------------------本文结束,感谢您的阅读--------------------

本文标题:内部CA服务器和自签名证书

文章作者:弓昭

发布时间:2019年01月02日 - 22:31

最后更新:2020年04月08日 - 22:20

原始链接:https://gongzhao1.gitee.io/内部CA服务器和自签名证书/

联系邮箱:gongzhao1@foxmail.com