openssl生成自签证书

xiaohai 2021-06-07 09:47:00 1101人围观 标签: openssl 
简介openssl生成自签证书

本文主要介绍使用openssl生成自签证书

生成CA证书私钥

[root@localhost openssl]# openssl genrsa -out ca.key 4096
Generating RSA private key, 4096 bit long modulus
....................................++
.............................................................................................++
e is 65537 (0x10001)

[root@localhost openssl]# ll
total 4
-rw-r--r-- 1 root root 3243 May  1 22:46 ca.key

生成CA证书

#这里需要调整-subj中的选项,CN可以只指定一个名称,固定的即可,但是这里指定了域名,这里有消息为3650天,也就是10年
[root@localhost openssl]# openssl req -x509 -new -nodes -sha512 -days 3650 -subj "/C=CN/ST=Sichuan/L=Chengdu/O=hi/OU=Personal/CN=hi-host.com" -key ca.key -out ca.crt

[root@localhost openssl]# ll
total 8
-rw-r--r-- 1 root root 2025 May  1 22:48 ca.crt
-rw-r--r-- 1 root root 3243 May  1 22:46 ca.key

生成服务器证书

证书通常包含一个.crt文件和一个.key文件,例如yourdomain.com.crt和yourdomain.com.key

#生成私钥
[root@localhost openssl]# openssl genrsa -out test.hi-host.com.key 4096
Generating RSA private key, 4096 bit long modulus
.............................................................................................................................................++
......................................................................................................................................................................................................++
e is 65537 (0x10001)

[root@localhost openssl]# ll
total 12
-rw-r--r-- 1 root root 2013 May  1 22:50 ca.crt
-rw-r--r-- 1 root root 3243 May  1 22:46 ca.key
-rw-r--r-- 1 root root 3243 May  1 22:50 test.hi-host.com.key


#生成证书签名请求(CSR),这里需要设置对应的域名
[root@localhost openssl]# openssl req -sha512 -new -subj "/C=CN/ST=SiChuan/L=Chengdu/O=example/OU=Personal/CN=hi-host.com" -key test.hi-host.com.key -out test.hi-host.com.csr

[root@localhost openssl]# ll
total 16
-rw-r--r-- 1 root root 2013 May  1 22:50 ca.crt
-rw-r--r-- 1 root root 3243 May  1 22:46 ca.key
-rw-r--r-- 1 root root 1704 May  1 22:53 test.hi-host.com.csr
-rw-r--r-- 1 root root 3243 May  1 22:50 test.hi-host.com.key

#生成一个x509 v3扩展文件
[root@localhost openssl]# cat > v3.ext <<-EOF
authorityKeyIdentifier=keyid,issuer
basicConstraints=CA:FALSE
keyUsage = digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment
extendedKeyUsage = serverAuth
subjectAltName = @alt_names

[alt_names]
DNS.1=test.hi-host.com
EOF

#上面可以设置多个域名,DNS.2,DNS.3等,可以是不同的域名

[root@localhost openssl]# ll
total 20
-rw-r--r-- 1 root root 2013 May  1 22:50 ca.crt
-rw-r--r-- 1 root root 3243 May  1 22:46 ca.key
-rw-r--r-- 1 root root 1704 May  1 22:53 test.hi-host.com.csr
-rw-r--r-- 1 root root 3243 May  1 22:50 test.hi-host.com.key
-rw-r--r-- 1 root root  235 May  1 22:55 v3.ext


#使用该v3.ext文件为您的主机生成证书
[root@localhost openssl]# openssl x509 -req -sha512 -days 3650 -extfile v3.ext -CA ca.crt -CAkey ca.key -CAcreateserial -in test.hi-host.com.csr -out test.hi-host.com.crt
Signature ok
subject=/C=CN/ST=SiChuan/L=Chengdu/O=example/OU=Personal/CN=hi-host.com
Getting CA Private Key
[root@localhost openssl]# ll
total 28
-rw-r--r-- 1 root root 2013 May  1 22:50 ca.crt
-rw-r--r-- 1 root root 3243 May  1 22:46 ca.key
-rw-r--r-- 1 root root   17 May  1 22:56 ca.srl
-rw-r--r-- 1 root root 2057 May  1 22:56 test.hi-host.com.crt
-rw-r--r-- 1 root root 1704 May  1 22:53 test.hi-host.com.csr
-rw-r--r-- 1 root root 3243 May  1 22:50 test.hi-host.com.key
-rw-r--r-- 1 root root  235 May  1 22:55 v3.ext

配置nginx

[root@localhost openssl]# vim /etc/nginx/conf.d/test.hi-host.com.conf 
server {
    listen       443 ssl;
    server_name test.hi-host.com;
    client_max_body_size 1000m;

    ssl_certificate      /root/openssl/test.hi-host.com.crt;
    ssl_certificate_key  /root/openssl/test.hi-host.com.key;

    location / {
        proxy_pass http://127.0.0.1:180;
    }
}

#重启nginx

解决浏览器访问https不安全问题

以上配置好后,就可以访问域名https://test.hi-host.com ,但是这里访问的时候还是提示不安全,但是能看到有证书,证书无效,这就是自签证书会有这样的问题。所以我们需要将ca.crt证书导入到浏览器中,这里以chrome为例,设置步骤如下:

  • 设置
  • 搜索https—->管理证书
  • 选择受信任根证书颁发机构
  • 导入ca.crt
  • 重启浏览器
  • 重新访问