美文网首页
(2)服务器网站准备1.0申请ssl证书10year

(2)服务器网站准备1.0申请ssl证书10year

作者: 萌新程序员 | 来源:发表于2023-06-06 09:17 被阅读0次

1,通过openssl创建CA证书

第一步:创建一个秘钥,这个便是CA证书的根本,之后所有的东西都来自这个秘钥

# 通过rsa算法生成2048位长度的秘钥


openssl genrsa -out myCA.key 2048


第二步:是通过秘钥加密机构信息形成公钥

# 公钥包含了机构信息,在输入下面的指令之后会有一系列的信息输入,这些信息便是机构信息,公司名称地址什么的

# 这里还有一个过期信息,CA证书也会过期,openssl默认是一个月,我们直接搞到10年


openssl req -new -x509 -key myCA.key -out myCA.cer -days 3650


参数名称 参数值

Country Name 国家代码,比如中国就是CN

State or Province Name 省名称

Locality Name 城市名称

Organization Name 机构名称

Organizational Unit Name 机构单位名称

Common Name 重点参数:授权给什么,因为机构是根节点所以是授权给自己

Email Address 邮件地址

2,创建服务器证书

在得到CA证书之后,需要通过openssl工具对证书进行转换得到公钥(.crt文件)和密钥(.key文件),无论CA证书是怎么来的到这里之后就没有任何区别了,服务器证书的制作流程相较CA证书要复杂一点点。

第一步:通过openssl工具创建服务器的秘钥

# 通过RSA算法生成长度2048位的秘钥


openssl genrsa -out server.key 2048


第二步:这里是创建一个签名请求

首先https证书的公钥不同于自定义情况下的加密证书,这里需要安装浏览器标准进行配置,首先openssl默认的证书版本是V1,V1在支持https时部分浏览器依旧会认为不安全,所以需要使用V3版本;同时openssl即便是使用V3版本依旧没有附带V3的subjectAltName字段数据(这里是证书对应的IP地址或者域名,可以用通配符)。但是这些东西命令行没法指定所以需要配置文件,我这里准备了一个:


tsa_policy2 = 1.2.3.4.5.6

tsa_policy3 = 1.2.3.4.5.7

[ ca ]

default_ca = CA_default  # The default ca section

[ CA_default ]

dir  = ./demoCA  # Where everything is kept

certs  = $dir/certs  # Where the issued certs are kept

crl_dir  = $dir/crl  # Where the issued crl are kept

database = $dir/index.txt # database index file.

new_certs_dir = $dir/newcerts  # default place for new certs.

certificate = $dir/cacert.pem  # The CA certificate

serial  = $dir/serial  # The current serial number

crlnumber = $dir/crlnumber # the current crl number

crl  = $dir/crl.pem  # The current CRL

private_key = $dir/private/cakey.pem# The private key

RANDFILE = $dir/private/.rand # private random number file

x509_extensions = usr_cert  # The extentions to add to the cert

name_opt  = ca_default  # Subject Name options

cert_opt  = ca_default  # Certificate field options

default_days = 365  # how long to certify for

default_crl_days= 30  # how long before next CRL

default_md = default  # use public key default MD

preserve = no  # keep passed DN ordering

policy  = policy_match

[ policy_match ]

countryName  = match

stateOrProvinceName = match

organizationName = match

organizationalUnitName = optional

commonName  = supplied

emailAddress  = optional

[ policy_anything ]

countryName  = optional

stateOrProvinceName = optional

localityName  = optional

organizationName = optional

organizationalUnitName = optional

commonName  = supplied

emailAddress  = optional

[ req ]

default_bits  = 1024

default_keyfile  = privkey.pem

distinguished_name = req_distinguished_name

attributes  = req_attributes

x509_extensions = v3_ca # The extentions to add to the self signed cert

string_mask = utf8only

req_extensions = v3_req # The extensions to add to a certificate request

[ req_distinguished_name ]

countryName  = Country Name (2 letter code)

countryName_default  = CN

countryName_min  = 2

countryName_max  = 2

stateOrProvinceName  = State or Province Name (full name)

stateOrProvinceName_default = BeiJing

localityName  = Locality Name (eg, city)

0.organizationName  = Organization Name (eg, company)

0.organizationName_default = myca

organizationalUnitName  = Organizational Unit Name (eg, section)

commonName  = Common Name (e.g. server FQDN or YOUR name)

commonName_max  = 64

emailAddress  = Email Address

emailAddress_max  = 64

[ req_attributes ]

challengePassword  = A challenge password

challengePassword_min  = 4

challengePassword_max  = 20

unstructuredName  = An optional company name

[ usr_cert ]

basicConstraints=CA:FALSE

nsCertType = client, email, objsign

keyUsage = nonRepudiation, digitalSignature, keyEncipherment

nsComment  = "OpenSSL Generated Certificate"

subjectKeyIdentifier=hash

authorityKeyIdentifier=keyid,issuer

[ svr_cert ]

basicConstraints=CA:FALSE

nsCertType  = server

keyUsage = nonRepudiation, digitalSignature, keyEncipherment, dataEncipherment, keyAgreement

subjectKeyIdentifier=hash

authorityKeyIdentifier=keyid,issuer

extendedKeyUsage = serverAuth,clientAuth

[ v3_req ]

subjectAltName = @alt_names

# 这里是重点,需要将里面配置为最终服务端需要的域名或者IP

# 这里可以写多个,能够自行添加DNS.X = XXXXXX

[ alt_names ]

DNS.1 = wangyq.top

[ v3_ca ]

subjectKeyIdentifier=hash

authorityKeyIdentifier=keyid:always,issuer

basicConstraints = CA:true

[ crl_ext ]

authorityKeyIdentifier=keyid:always

[ proxy_cert_ext ]

basicConstraints=CA:FALSE

nsComment  = "OpenSSL Generated Certificate"

subjectKeyIdentifier=hash

authorityKeyIdentifier=keyid,issuer

proxyCertInfo=critical,language:id-ppl-anyLanguage,pathlen:3,policy:foo

[ tsa ]

default_tsa = tsa_config1 # the default TSA section

[ tsa_config1 ]

dir  = ./demoCA  # TSA root directory

serial  = $dir/tsaserial # The current serial number (mandatory)

crypto_device = builtin  # OpenSSL engine to use for signing

signer_cert = $dir/tsacert.pem  # The TSA signing certificate

    # (optional)

certs  = $dir/cacert.pem # Certificate chain to include in reply

    # (optional)

signer_key = $dir/private/tsakey.pem # The TSA private key (optional)

default_policy = tsa_policy1  # Policy if request did not specify it

    # (optional)

other_policies = tsa_policy2, tsa_policy3 # acceptable policies (optional)

digests  = md5, sha1  # Acceptable message digests (mandatory)

accuracy = secs:1, millisecs:500, microsecs:100 # (optional)

clock_precision_digits  = 0 # number of digits after dot. (optional)

ordering  = yes # Is ordering defined for timestamps?

    # (optional, default: no)

tsa_name  = yes # Must the TSA name be included in the reply?

    # (optional, default: no)

ess_cert_id_chain = no # Must the ESS cert id chain be included?

    # (optional, default: no)


将上面的配置内容保存为openssl.cnf放到生成的服务器证书文件的目录下(注意:修改alt_names里面的域名或者IP为最终部署需要的地址,支持通配符),然后执行创建签名申请文件即可,执行运行:

注意:在openssl.conf中一定要加有关的域名,然后生成对应服务器的证书的时候也使用域名就可以实现正常的访问了。

# 和创建CA时一样这里需要输入一堆服务器信息,输入项也是相同的。

# 不过在输入Common Name(CN)最好直接输入服务器的IP地址或者域名。


openssl req -config openssl.cnf -new -out server.req -key server.key


第三步:通过CA机构证书对服务器证书进行签名认证

这里服务器的公钥是由CA证书的密钥配对加密来的。

# 这里没有什么需要说的,本质上就是将签名请求文件进行签名最终得到服务器的公钥


openssl x509 -req  -extfile openssl.cnf -extensions v3_req -in server.req -out server.cer -CAkey myCA.key -CA myCA.cer -days 3650 -CAcreateserial -CAserial serial


补充:如果需要将cer证书转成crt证书,需要如下命令

openssl x509 -inform PEM -in server.cer -out server.crt

第四步部署证书

我们通过Nginx部署,最终得到server.key就是秘钥,server.cer文件就是公钥只需要配置给Nginx就行了。

我们可以单独添加一个配置文件,并在默认文件中通过【include】将其引入:


server {

    listen      8080 ssl;

    server_name localhost;

    ssl_certificate      /data/sslTest/server.cer;

    ssl_certificate_key  /data/sslTest/server.key;

    ssl_protocols  SSLv2 SSLv3 TLSv1 TLSv1.1 TLSv1.2;

    ssl_session_cache    shared:SSL:1m;

    ssl_session_timeout  5m;

    ssl_ciphers  HIGH:!aNULL:!MD5;

    ssl_prefer_server_ciphers  on;

    location / {

    root  /data/empowerment/web/dist;

    try_files $uri $uri/ /index.html;

        index  index.html index.htm;

    }

    location ^~ /#/^~  {

      proxy_pass http://127.0.0.1:8081/;

      proxy_connect_timeout 300s;

      proxy_send_timeout 300s;

      proxy_read_timeout 300s;

    }

    location /system {

        proxy_pass http://127.0.0.1:8081/system;

    }

    location /list {

        proxy_pass http://127.0.0.1:8081/list;

    }

    error_page  405 =200 @405;

    location @405 {

        proxy_method GET;

        proxy_pass http://127.0.0.1:8081;

    }

   

}


3,信任CA机构证书

通过openssl创建的CA证书,只需要将CA证书的公钥(myCA.cer文件)导入到系统信任的根证书颁发机构里面就行了,即直接双击cer文件进行证书安装,最终不光是windows系统,任何操作系统都可以安装证书来进行对CA机构的进行信任操作。

第一步:双击证书,点击【安装证书】;

第二步:选择【本地计算机】,根据提示选择【是】;

第三步:选择【将所有的证书都放入下列存储】,并点击【浏览(R)】;

第四步:选择【受信任的根证书颁发机构】,并点击【确定】;

第五步:选择【下一页(N)】,紧接着点【完成】。

相关文章

  • iOS APP配置HTTPS流程

    一.准备工作 申请一个 SSL 证书 ,可在沃通WoSign网站申请所需SSL证书类型。SSL证书按验证的类别可分...

  • iOS APP配置HTTPS流程

    一.准备工作 申请一个 SSL 证书 ,可在沃通WoSign网站申请所需SSL证书类型。SSL证书按验证的类别可分...

  • 服务器配置HTTPS

    一、Linux下Nginx环境配置https 1、申请SSL证书 以阿里云为例:SSL证书申请地址 2、在服务器/...

  • 在Nginx中配置ssl证书

    网站配置ssl证书之后,就可以使用https的方式进行访问。 1.申请ssl证书 可以申请免费的ssl证书,也可以...

  • ssl

    ssl证书宝塔系统设置 条件: 已备案域名与服务器 步骤一:申请域名ssl证书(百度云ssl域名证书申请为例) 选...

  • Lnmp Swoole使用wss协议

    准备工作 申请 SSL 证书,传送 阿里云免费SSL证书申请[https://www.jianshu.com/p/...

  • Nginx下配置SSL证书

    一、申请证书 需要免费申请SSL证书的同学可以参考这篇文章《八大免费SSL证书:给你的网站免费添加HTTPS加密》...

  • HTTPS服务器配置

    HTTPS服务器配置 一、SSL证书申请 1、确认需要申请证书的域名 2、生成私钥和csr文件 在linux机器上...

  • 腾讯云 - 小程序服务端搭建

    一、购买服务器与域名镜像部署二、申请SSL证书1.下载证书2.上传证书至服务器 (路径:/etc/nginx/s...

  • Apache配置ssl

    阿里云申请的ssl证书配置(Apache) 下载证书 下载证书 上传到服务器 解压到 /etc/apache2/s...

网友评论

      本文标题:(2)服务器网站准备1.0申请ssl证书10year

      本文链接:https://www.haomeiwen.com/subject/dtqgedtx.html