如何制作SSL证书即https服务支持

Posted todo9351

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何制作SSL证书即https服务支持相关的知识,希望对你有一定的参考价值。

如何制作SSL证书即https服务支持

目的

最近需要做个网站,支持https的访问,但是,是内部使用的,不需要对外网开放。

基础知识

在制作之前先了解一下OpenSSL、HTTPS的关系。见https://www.qikegu.com/docs/2632,对SSL工作原理部分讲得比较清楚了。
再补充一点SSL证书的知识
https://zhuanlan.zhihu.com/p/371891073

通过以上的学习,基本可以了解了,要完成这个功能,必须制作出如下证书:

  • CA根证书
  • CA中间证书
  • 网站证书

制作过程

国内的制作说明很多,但是,基本都是制作出CA后,直接自签名网站的。并且相关的OPENSSL的配置也很少说明,感觉文档都写得不够完整,因此,起初制作的证书,基本都嗝屁了。也可能是我个人的问题。
看到一篇老外的教程:
https://jamielinux.com/docs/openssl-certificate-authority/create-the-root-pair.html
按照他的介绍制作出了所需要的全部证书。但是,在实际应用时,还是报了一些问题,比如出现网站的证书出现无法认证等问题。以下稍微介绍说明下,这个老外的制作过程及注意情况。制作全程root权限。

CA证书制作

# mkdir /root/ca
# cd /root/ca
# mkdir certs crl newcerts private
# chmod 700 private
# touch index.txt
# echo 1000 > serial

准备CA的配置文件 /root/ca/openssl.cnf

# OpenSSL root CA configuration file.
# Copy to `/root/ca/openssl.cnf`.

[ ca ]
# `man ca`
default_ca = CA_default

[ CA_default ]
# Directory and file locations.
dir               = /root/ca
certs             = $dir/certs
crl_dir           = $dir/crl
new_certs_dir     = $dir/newcerts
database          = $dir/index.txt
serial            = $dir/serial
RANDFILE          = $dir/private/.rand

# The root key and root certificate.
private_key       = $dir/private/ca.key.pem
certificate       = $dir/certs/ca.cert.pem

# For certificate revocation lists.
crlnumber         = $dir/crlnumber
crl               = $dir/crl/ca.crl.pem
crl_extensions    = crl_ext
default_crl_days  = 30

# SHA-1 is deprecated, so use SHA-2 instead.
default_md        = sha256

name_opt          = ca_default
cert_opt          = ca_default
default_days      = 375
preserve          = no
policy            = policy_strict

[ policy_strict ]
# The root CA should only sign intermediate certificates that match.
# See the POLICY FORMAT section of `man ca`.
countryName             = match
stateOrProvinceName     = match
organizationName        = match
organizationalUnitName  = optional
commonName              = supplied
emailAddress            = optional

[ policy_loose ]
# Allow the intermediate CA to sign a more diverse range of certificates.
# See the POLICY FORMAT section of the `ca` man page.
countryName             = optional
stateOrProvinceName     = optional
localityName            = optional
organizationName        = optional
organizationalUnitName  = optional
commonName              = supplied
emailAddress            = optional

[ req ]
# Options for the `req` tool (`man req`).
default_bits        = 2048
distinguished_name  = req_distinguished_name
string_mask         = utf8only

# SHA-1 is deprecated, so use SHA-2 instead.
default_md          = sha256

# Extension to add when the -x509 option is used.
x509_extensions     = v3_ca

[ req_distinguished_name ]
# See <https://en.wikipedia.org/wiki/Certificate_signing_request>.
countryName                     = Country Name (2 letter code)
stateOrProvinceName             = State or Province Name
localityName                    = Locality Name
0.organizationName              = Organization Name
organizationalUnitName          = Organizational Unit Name
commonName                      = Common Name
emailAddress                    = Email Address

# Optionally, specify some defaults.
countryName_default             = GB
stateOrProvinceName_default     = England
localityName_default            =
0.organizationName_default      = Alice Ltd
organizationalUnitName_default  =
emailAddress_default            =

[ v3_ca ]
# Extensions for a typical CA (`man x509v3_config`).
subjectKeyIdentifier = hash
authorityKeyIdentifier = keyid:always,issuer
basicConstraints = critical, CA:true
keyUsage = critical, digitalSignature, cRLSign, keyCertSign

[ v3_intermediate_ca ]
# Extensions for a typical intermediate CA (`man x509v3_config`).
subjectKeyIdentifier = hash
authorityKeyIdentifier = keyid:always,issuer
basicConstraints = critical, CA:true, pathlen:0
keyUsage = critical, digitalSignature, cRLSign, keyCertSign

[ usr_cert ]
# Extensions for client certificates (`man x509v3_config`).
basicConstraints = CA:FALSE
nsCertType = client, email
nsComment = "OpenSSL Generated Client Certificate"
subjectKeyIdentifier = hash
authorityKeyIdentifier = keyid,issuer
keyUsage = critical, nonRepudiation, digitalSignature, keyEncipherment
extendedKeyUsage = clientAuth, emailProtection

[ server_cert ]
# Extensions for server certificates (`man x509v3_config`).
basicConstraints = CA:FALSE
nsCertType = server
nsComment = "OpenSSL Generated Server Certificate"
subjectKeyIdentifier = hash
authorityKeyIdentifier = keyid,issuer:always
keyUsage = critical, digitalSignature, keyEncipherment
extendedKeyUsage = serverAuth

[ crl_ext ]
# Extension for CRLs (`man x509v3_config`).
authorityKeyIdentifier=keyid:always

[ ocsp ]
# Extension for OCSP signing certificates (`man ocsp`).
basicConstraints = CA:FALSE
subjectKeyIdentifier = hash
authorityKeyIdentifier = keyid,issuer
keyUsage = critical, digitalSignature
extendedKeyUsage = critical, OCSPSigning

生成密钥

# cd /root/ca
# openssl genrsa -aes256 -out private/ca.key.pem 4096

Enter pass phrase for ca.key.pem: secretpassword
Verifying - Enter pass phrase for ca.key.pem: secretpassword

# chmod 400 private/ca.key.pem

生成CA证书

# cd /root/ca
# openssl req -config openssl.cnf \\
      -key private/ca.key.pem \\
      -new -x509 -days 7300 -sha256 -extensions v3_ca \\
      -out certs/ca.cert.pem

Enter pass phrase for ca.key.pem: secretpassword
You are about to be asked to enter information that will be incorporated
into your certificate request.
-----
Country Name (2 letter code) [XX]:GB
State or Province Name []:England
Locality Name []:
Organization Name []:Alice Ltd
Organizational Unit Name []:Alice Ltd Certificate Authority
Common Name []:Alice Ltd Root CA
Email Address []:

# chmod 444 certs/ca.cert.pem

这里要特别注意起名字,不要乱起 Organization Name 还有Country Name 等。

验证CA证书

# openssl x509 -noout -text -in certs/ca.cert.pem

中间证书

制作中间证书基本与制作CA证书是一样的。不过,openssl的配置是不同的。
中间证书配置,放到 /root/ca/intermediate/openssl.cnf

# OpenSSL intermediate CA configuration file.
# Copy to `/root/ca/intermediate/openssl.cnf`.

[ ca ]
# `man ca`
default_ca = CA_default

[ CA_default ]
# Directory and file locations.
dir               = /root/ca/intermediate
certs             = $dir/certs
crl_dir           = $dir/crl
new_certs_dir     = $dir/newcerts
database          = $dir/index.txt
serial            = $dir/serial
RANDFILE          = $dir/private/.rand

# The root key and root certificate.
private_key       = $dir/private/intermediate.key.pem
certificate       = $dir/certs/intermediate.cert.pem

# For certificate revocation lists.
crlnumber         = $dir/crlnumber
crl               = $dir/crl/intermediate.crl.pem
crl_extensions    = crl_ext
default_crl_days  = 30

# SHA-1 is deprecated, so use SHA-2 instead.
default_md        = sha256

name_opt          = ca_default
cert_opt          = ca_default
default_days      = 375
preserve          = no
policy            = policy_loose

[ policy_strict ]
# The root CA should only sign intermediate certificates that match.
# See the POLICY FORMAT section of `man ca`.
countryName             = match
stateOrProvinceName     = match
organizationName        = match
organizationalUnitName  = optional
commonName              = supplied
emailAddress            = optional

[ policy_loose ]
# Allow the intermediate CA to sign a more diverse range of certificates.
# See the POLICY FORMAT section of the `ca` man page.
countryName             = optional
stateOrProvinceName     = optional
localityName            = optional
organizationName        = optional
organizationalUnitName  = optional
commonName              = supplied
emailAddress            = optional

[ req ]
# Options for the `req` tool (`man req`).
default_bits        = 2048
distinguished_name  = req_distinguished_name
string_mask         = utf8only

# SHA-1 is deprecated, so use SHA-2 instead.
default_md          = sha256

# Extension to add when the -x509 option is used.
x509_extensions     = v3_ca

[ req_distinguished_name ]
# See <https://en.wikipedia.org/wiki/Certificate_signing_request>.
countryName                     = Country Name (2 letter code)
stateOrProvinceName             = State or Province Name
localityName                    = Locality Name
0.organizationName              = Organization Name
organizationalUnitName          = Organizational Unit Name
commonName                      = Common Name
emailAddress                    = Email Address

# Optionally, specify some defaults.
countryName_default             = GB
stateOrProvinceName_default     = England
localityName_default            =
0.organizationName_default      = Alice Ltd
organizationalUnitName_default  =
emailAddress_default            =

[ v3_ca ]
# Extensions for a typical CA (`man x509v3_config`).
subjectKeyIdentifier = hash
authorityKeyIdentifier = keyid:always,issuer
basicConstraints = critical, CA:true
keyUsage = critical, digitalSignature, cRLSign, keyCertSign

[ v3_intermediate_ca ]
# Extensions for a typical intermediate CA (`man x509v3_config`).
subjectKeyIdentifier = hash
authorityKeyIdentifier = keyid:always,issuer
basicConstraints = critical, CA:true, pathlen:0
keyUsage = critical, digitalSignature, cRLSign, keyCertSign

[ usr_cert ]
# Extensions for client certificates (`man x509v3_config`).
basicConstraints = CA:FALSE
nsCertType = client, email
nsComment = "OpenSSL Generated Client Certificate"
subjectKeyIdentifier = hash
authorityKeyIdentifier = keyid,issuer
keyUsage = critical, nonRepudiation, digitalSignature, keyEncipherment
extendedKeyUsage = clientAuth, emailProtection

[ server_cert ]
# Extensions for server certificates (`man x509v3_config`).
basicConstraints = CA:FALSE
nsCertType = server
nsComment = "OpenSSL Generated Server Certificate"
subjectKeyIdentifier = hash
authorityKeyIdentifier = keyid,issuer:always
keyUsage = critical, digitalSignature, keyEncipherment
extendedKeyUsage = serverAuth

[ crl_ext ]
# Extension for CRLs (`man x509v3_config`).
authorityKeyIdentifier=keyid:always

[ ocsp ]
# Extension for OCSP signing certificates (`man ocsp`).
basicConstraints = CA:FALSE
subjectKeyIdentifier = hash
authorityKeyIdentifier = keyid,issuer
keyUsage = critical, digitalSignature
extendedKeyUsage = critical, OCSPSigning
# mkdir /root/ca/intermediate
# cd /root/ca/intermediate
# mkdir certs crl csr newcerts private
# chmod 700 private
# touch index.txt
# echo 1000 > serial
# echo 1000 > /root/ca/intermediate/crlnumber

创建密钥

# cd /root/ca
# openssl genrsa -aes256 \\
      -out intermediate/private/intermediate.key.pem 4096

Enter pass phrase for intermediate.key.pem: secretpassword
Verifying - Enter pass phrase for intermediate.key.pem: secretpassword

# chmod 400 intermediate/private/intermediate.key.pem

自签名中间证书申请文件, 要注意Organization Name 要与CA的相同

# cd /root/ca
# openssl req -config intermediate/openssl.cnf -new -sha256 \\
      -key intermediate/private/intermediate.key.pem \\
      -out intermediate/csr/intermediate.csr.pem

Enter pass phrase for intermediate.key.pem: secretpassword
You are about to be asked to enter information that will be incorporated
into your certificate request.
-----
Country Name (2 letter code) [XX]:GB
State or Province Name []:England
Locality Name []:
Organization Name []:Alice Ltd
Organizational Unit Name []:Alice Ltd Certificate Authority
Common Name []:Alice Ltd Intermediate CA
Email Address []:

用CA去签名中间证书的申请文件 csr

# cd /root/ca
# openssl ca -config openssl.cnf -extensions v3_intermediate_ca \\
      -days 3650 -notext -md sha256 \\
      -in intermediate/csr/intermediate.csr.pem \\
      -out intermediate/certs/intermediate.cert.pem

Enter pass phrase for ca.key.pem: secretpassword
Sign the certificate? [y/n]: y

# chmod 444 intermediate/certs/intermediate.cert.pem

验证一下生成的证书,如果不放心的话

# openssl x509 -noout -text \\
      -in intermediate/certs/intermediate.cert.pem
# openssl verify -CAfile certs/ca.cert.pem \\
      intermediate/certs/intermediate.cert.pem

intermediate.cert.pem: OK

如果有必要的话,生成证书链。没必要就算了。

# cat intermediate/certs/intermediate.cert.pem \\
      certs/ca.cert.pem > intermediate/certs/ca-chain.cert.pem
# chmod 444 intermediate/certs/ca-chain.cert.pem

制作网站证书

这个是重点,出问题最多的就是这里。

生成密钥, 不要带 -aes256 参数.

# cd /root/ca
# openssl genrsa \\
      -out intermediate/private/www.example.com.key.pem 2048
# chmod 400 intermediate/private/www.example.com.key.pem

制作网站的SSL申请文件csr, 注意,一定要加上subjectAltName!!!

# cd /root/ca
# openssl req -config intermediate/openssl.cnf \\
      -key intermediate/private/www.example.com.key.pem \\
      -new -sha256 -out intermediate/csr/www.example.com.csr.pem \\
	  -addext "subjectAltName = IP:127.0.0.1,DNS:*.cct.com"

Enter pass phrase for www.example.com.key.pem: secretpassword
You are about to be asked to enter information that will be incorporated
into your certificate request.
-----
Country Name (2 letter code) [XX]:US
State or Province Name []:California
Locality Name []:Mountain View
Organization Name []:Alice Ltd
Organizational Unit Name []:Alice Ltd Web Services
Common Name []:www.example.com
Email Address []:

可以用如下命令看是否有生成subjectAltName的申请

# openssl req -noout -text -in intermediate/csr/www.example.com.csr.pem

一定要看到这个

        Requested Extensions:
            X509v3 Subject Alternative Name:
                IP Address:127.0.0.1, DNS:*.cct.com

接下来是认证,需要修改中间证书的 openssl.conf

[ server_cert ]
# Extensions for server certificates (`man x509v3_config`).
basicConstraints = CA:FALSE
nsCertType = server
nsComment = "OpenSSL Generated Server Certificate"
subjectKeyIdentifier = hash
authorityKeyIdentifier = keyid,issuer:always
keyUsage = critical, digitalSignature, keyEncipherment
extendedKeyUsage = serverAuth
subjectAltName = IP:127.0.0.1,DNS:*.cct.com

其中 subjectAltName 是新增的。

再用中间证书签名网站申请文件生成网站证书

# cd /root/ca
# openssl ca -config intermediate/openssl.cnf \\
      -extensions server_cert -days 375 -notext -md sha256 \\
      -in intermediate/csr/www.example.com.csr.pem \\
      -out intermediate/certs/www.example.com.cert.pem
# chmod 444 intermediate/certs/www.example.com.cert.pem

验证证书信息

# openssl x509 -noout -text  -in intermediate/certs/www.example.com.cert.pem

一定要看到

            X509v3 Subject Alternative Name:
                IP Address:127.0.0.1, DNS:*.cct.com

就说明添加 subjectAltName 成功了。

最终成果

最终,得到了如下有用的证书及网站私钥文件

# cd /root/ca
# find
./certs/ca.cert.pem     # 根证书   
./intermediate/private/www.example.com.key.pem     # 网站密钥
./intermediate/certs/intermediate.cert.pem        # 中间证书
./intermediate/certs/www.example.com.cert.pem     # 网站证书
./intermediate/certs/ca-chain.cert.pem            # 证书链

证书验证

安装 nginx 进行验证

nginx在/etc/nginx/sites-enabled 下新建 127.0.0.1 文件,文件内容

server 
    listen 443 ssl;
    server_name 127.0.0.1;
    root /var/www/html;
    index index.html index.htm;
    ssl_certificate  /home/chenct/myssl/127.0.0.1.cert.pem;
    ssl_certificate_key /home/chenct/myssl/127.0.0.1.key.pem;
    ssl_session_timeout 5m;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;

特别声明

感谢开源大佬的奉献,本说明仅作一点点实战补充。

以上是关于如何制作SSL证书即https服务支持的主要内容,如果未能解决你的问题,请参考以下文章

Nginx 配置https证书认证

Nginx 配置https证书认证

如何使用https 客户端证书

https怎么申请啊?

如何单独为子域名启用 SSL,即使用 HTTPS 访问

如何制作weblogic单向ssl证书