一起源码网

  • www.171739.xyz
  • 全球最大的互联网技术和资源下载平台
搜索
一起源码网 门户 高级进阶 查看主题

使用linux自建证书(ios强制https 微信小程序强制https 本地开发环境) ... ...

发布者: atui | 发布时间: 2018-1-4 00:55| 查看数: 2539| 评论数: 1|帖子模式

本文作者:ranfengzheng,来自授权地址

前言: 作为一个运维人员不背锅,谁背呢!

正文: 现在都要https了,很多童鞋也痛苦本地该如何搭建https环境,网上一搜 一搜一大把,但是使用就。。。。。。

今天在这里笔者就教大家一步一步使用linux搭建本地的https环境。。。因我司使用的nginx 所以接口相关的服务端配置 也只列出nginx 的配置接下来看笔者如何一步一步弄好https。(可以调用openssl命令雷同!)

假设我司有alp环境,bet环境 我司线上域名为test.com 对应的则为 test.alp test.bet相关域名
则我本地应该生成通配型的.test.alp .test.bet (ps:注意一点很重要三级域名需要单独配置 如:m.a.test.alp 需要单独生成 *.a.test.alp 的通配型 或者 m.a.test.alp 购买证书的时候也是如此! 一个点多1W多块钱!! 很重要!!!本篇文章不涉及三级域名只争对二级的本地配置)

一、生成根CA的keylinux下使用如下命令:

openssl genrsa -des3 -out RootCA.key 2048

过程中会出现如下提示则为需要给根证书的key设置一个密码(我直接输入了123456):

Enter pass phrase for RootCA.key:

接下来如下提示为确认输入根证书的key密码(依然123456):

Verifying - Enter pass phrase for RootCA.key:

则生成了根证书的key:

[root@localhost key]# ll
total 4
-rw-r--r-- 1 root root 1751 Jan  5 10:27 RootCA.key
[root@localhost key]# 

二、生成CA的证书crt(Common Name填RootCA 其他的都不填写 这个证书是需要安卓手机 浏览器 苹果手机 mac 都需要去安装的 ps:苹果手机需要使用url的形式或者邮件的形式才能安装 很重要!!电脑版安装的时候需要将证书添加到受信任的根证书中 手机 ios 不需要 注意!!)

linux下使用如下命令:

openssl req -new -x509 -sha256 -days 3650 -key RootCA.key -out RootCA.crt

然后提示输入密码(刚刚设置的123456):

Enter pass phrase for RootCA.key:

如下提示直接回车:

Country Name (2 letter code) [XX]:

如下提示直接回车:

State or Province Name (full name) []:

如下提示直接回车:

Locality Name (eg, city) [Default City]:

如下提示直接回车:

Organization Name (eg, company) [Default Company Ltd]:

如下提示直接回车:

Organizational Unit Name (eg, section) []:

如下提示填RootCA即可:

Common Name (eg, your name or your server's hostname) []:RootCA

如下提示直接回车:

Email Address []:

将生成RootCA.key 现在可以将这个key发给测试的同事 开发的同事 安装此证书(ps:苹果手机需要使用url的形式或者邮件的形式才能安装 很重要!!)

[root@localhost key]# ll
total 8
-rw-r--r-- 1 root root 1265 Jan  5 10:42 RootCA.crt
-rw-r--r-- 1 root root 1751 Jan  5 10:33 RootCA.key
[root@localhost key]# 

三、生成*.test.alp的证书(包含key 和 crt)

1.生成*.test.alp证书的key

openssl genrsa -des3 -out testalp.key 2048

如下提示输入key的密码(我输入的依然为123456):

Enter pass phrase for testalp.key:

如下提示再次确认密码(输入123456):

Verifying - Enter pass phrase for testalp.key:

及生产一个含有密码的*.test.alp证书的key

[root@localhost key]# ll
total 12
-rw-r--r-- 1 root root 1265 Jan  5 10:42 RootCA.crt
-rw-r--r-- 1 root root 1751 Jan  5 10:33 RootCA.key
-rw-r--r-- 1 root root 1743 Jan  5 10:48 testalp.key
[root@localhost key]# 

2.刚刚生成的是带密码的 现在导出一个无密码的.test.alp证书的key(nginx使用的就是现在生产的这个无密码的.test.alp证书的key)

1.如下命令通过key生成一个无密码的key:

openssl rsa -in testalp.key -out testalp_nopass.key

如下提示输入密码(输入123456):

Enter pass phrase for testalp.key:

即生产了testalp_nopass.key:

[root@localhost key]# ll
total 16
-rw-r--r-- 1 root root 1265 Jan  5 10:42 RootCA.crt
-rw-r--r-- 1 root root 1751 Jan  5 10:33 RootCA.key
-rw-r--r-- 1 root root 1743 Jan  5 10:48 testalp.key
-rw-r--r-- 1 root root 1675 Jan  5 11:00 testalp_nopass.key
[root@localhost key]# 

四、根据.test.alp的key生产证书签名的请求文件(这个请求文件等会需要 使用RootCA.key签名 这里使用 testalp_nopass.key去生成 ps:这里一定要注意一个问题 Common Name 一定要写:.test.alp)

如下命令生产请求文件testalp_nopass.csr:

openssl req -new -sha256 -days 3650 -key testalp_nopass.key -out testalp_nopass.csr

如下提示直接回车:

Country Name (2 letter code) [XX]:

如下提示直接回车:

State or Province Name (full name) []:

如下提示直接回车:

Locality Name (eg, city) [Default City]:

如下提示直接回车:

Organization Name (eg, company) [Default Company Ltd]:

如下提示直接回车:

Organizational Unit Name (eg, section) []:

如下输入*.test.alp 很重要!!:

Common Name (eg, your name or your server's hostname) []:*.test.alp

如下提示直接回车:

Email Address []:

如下提示直接回车:

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:

如下提示直接回车:

An optional company name []:

则生成了名字为*.test.alp的请求文件testalp_nopass.csr:

[root@localhost key]# ll
total 20
-rw-r--r-- 1 root root 1265 Jan  5 10:42 RootCA.crt
-rw-r--r-- 1 root root 1751 Jan  5 10:33 RootCA.key
-rw-r--r-- 1 root root 1743 Jan  5 10:48 testalp.key
-rw-r--r-- 1 root root  980 Jan  5 11:06 testalp_nopass.csr
-rw-r--r-- 1 root root 1675 Jan  5 11:00 testalp_nopass.key
[root@localhost key]# 

五、最后一步通过之前生成的RootCA.key 和RootCA.crt签名testalp_nopass.csr并生成testalp_nopass.crt

如下命令:

openssl ca -md sha256 -in testalp_nopass.csr -config /etc/pki/tls/openssl.cnf -days 3650 -out testalp_nopass.crt -cert RootCA.crt -keyfile RootCA.key

如下提示需要输入RootCa.key的密码(我的为之前配置的123456):

Enter pass phrase for RootCA.key:

很高兴的报错了:

/etc/pki/CA/index.txt: No such file or directory
unable to open '/etc/pki/CA/index.txt'
139938927691592:error:02001002:system library:fopen:No such file or directory:bss_file.c:398:fopen('/etc/pki/CA/index.txt','r')
139938927691592:error:20074002:BIO routines:FILE_CTRL:system lib:bss_file.c:400:
[root@localhost key]# 

这也是我刻意用新机器发生的错误 该错误的解决办法:如下命令:

touch /etc/pki/CA/index.txt

然后继续执行:

openssl ca -in testalp_nopass.csr -config /etc/pki/tls/openssl.cnf -days 3650 -out testalp_nopass.crt -cert RootCA.crt -keyfile RootCA.key

如下提示需要输入RootCa.key的密码(我的为之前配置的123456):

Enter pass phrase for RootCA.key:

很高兴又报错了:

Using configuration from /etc/pki/tls/openssl.cnf
Enter pass phrase for RootCA.key:
/etc/pki/CA/serial: No such file or directory
error while loading serial number
139989781997384:error:02001002:system library:fopen:No such file or directory:bss_file.c:398:fopen('/etc/pki/CA/serial','r')
139989781997384:error:20074002:BIO routines:FILE_CTRL:system lib:bss_file.c:400:
[root@localhost key]# 

该错误的解决办法:如下命令:

touch /etc/pki/CA/serial
echo "00" > /etc/pki/CA/serial

然后继续执行:

openssl ca -in testalp_nopass.csr -config /etc/pki/tls/openssl.cnf -days 3650 -out testalp_nopass.crt -cert RootCA.crt -keyfile RootCA.key

如下提示需要输入RootCa.key的密码(我的为之前配置的123456):

Enter pass phrase for RootCA.key:

恭喜恭喜三连错:

Check that the request matches the signature
Signature ok
The mandatory stateOrProvinceName field was missing
[root@localhost key]# 

该错误的解决办法:把/etc/pki/tls/openssl.cnf中大约86 87的行

请使用vi把如下内容

stateOrProvinceName= match
organizationName= match

修改成

stateOrProvinceName= optional 
organizationName= optional 

在保存就可以了。

然后继续执行:

openssl ca -in testalp_nopass.csr -config /etc/pki/tls/openssl.cnf -days 3650 -out testalp_nopass.crt -cert RootCA.crt -keyfile RootCA.key

如下提示需要输入RootCa.key的密码(我的为之前配置的123456):

Enter pass phrase for RootCA.key:

如下提示输入y:

Sign the certificate? [y/n]:y

如下提示输入y:

1 out of 1 certificate requests certified, commit? [y/n]y

则签名成功:

[root@localhost key]# ll
total 28
-rw-r--r-- 1 root root 1265 Jan  5 10:42 RootCA.crt
-rw-r--r-- 1 root root 1751 Jan  5 10:33 RootCA.key
-rw-r--r-- 1 root root 1743 Jan  5 10:48 testalp.key
-rw-r--r-- 1 root root 4373 Jan  5 11:26 testalp_nopass.crt
-rw-r--r-- 1 root root  980 Jan  5 11:06 testalp_nopass.csr
-rw-r--r-- 1 root root 1675 Jan  5 11:00 testalp_nopass.key
[root@localhost key]# 

到这里对如下文件进行说明:

nginx只需要testalp_nopass.crt 以及testalp_nopass.key即可

浏览器 手机端只需要安装 RootCA.crt 即可 且这个RootCA.crt是不需要到最后弄完了才可以使用,这个crt安装了之后后续该电脑 该手机设备 不需要再次更新安装!!一次安装永久有效 也不会影响后续对*.test.bet的签发(ps:苹果手机需要使用url的形式或者邮件的形式才能安装 很重要!! 电脑版安装的时候需要将证书添加到受信任的根证书中 手机 ios 不需要 注意!!)

.test.bet的签发和生产也如上.test.alp一样即可

这里我给大家演示下效果.

nginx下的配置:

我在nginx 的conf目录下创建了一个key的文件夹存放 testalp_nopass.crt 和testalp_nopass.key:

[root@localhost key]# ll
total 12
-rw-r--r-- 1 root root 4373 Jan  5 11:26 testalp_nopass.crt
-rw-r--r-- 1 root root 1675 Jan  5 11:00 testalp_nopass.key
[root@localhost key]# pwd
/usr/local/etc/nginx/conf/key
[root@localhost key]# 

配置nginx.conf增加如下配置(因只做测试 直接在nginx.conf直接配置 且项目为写的html的测试页):

    server
        {
           listen 443;
            ssl on;
            ssl_certificate key/testalp_nopass.crt;
            ssl_certificate_key key/testalp_nopass.key;
           index index.html;
           root  /data/test/;
        }

主要配置为:

           listen 443;
            ssl on;
            ssl_certificate key/testalp_nopass.crt;
            ssl_certificate_key key/testalp_nopass.key;

访问一下试试会发现https了 但是貌似不对:

wKioL1htwEmjo8YdAAGfZ0qmdJg324.png-wh_50


wKiom1htwITALPU5AABFvzzzncY420.png-wh_50

上说说找不到颁发者需要这里我需要先安装生产Root.crt(电脑版安装的时候需要将证书添加到受信任的根证书中 手机 ios 不需要 注意!!):

wKioL1htwXniwv5gAAEhALm-HkA208.jpg-wh_50

若没有添加到信任机构中则会是这样的:

wKiom1htwcDQsKrQAABRz2_GMR4692.png-wh_50

所以切记电脑端需要添加到受信任的根证书颁发机构!

但是就算你配置了你会发现 还是不行 :

wKioL1htwijTAeZ8AAHtsZZfIc8234.jpg-wh_50

尽管说证书没有问题。原因在于域名!没错!是域名 这个也就是为什么这里提出来 是因为要反证之前为什么说要配置*.test.alp的重要:

nginx增加配置和电脑host解析test.test.alp之后的结果:

nginx增加配置:

    server
        {
           listen 443;
            server_name test.test.alp;
            ssl on;
            ssl_certificate key/testalp_nopass.crt;
            ssl_certificate_key key/testalp_nopass.key;
           index index.html;
           root  /data/test/;
        }

主要增加:

server_name test.test.alp;

配置hosts之后访问一切正常!

到最后说一点就是如果你需要重新配置需要清空/etc/pki/CA/index.txt这个文件如下命令:

echo > /etc/pki/CA/index.txt 

即可

笔者只写了具体怎么做,但是使用到的openssl的参数还需要自行百度一下!

最新评论

q3626024 发表于 2022-5-7 19:46
源代码怎么听vip歌曲

浏览过的版块

一起源码让程序更轻更快

www.171739.xyz

工作时间 周一至周六 8:00-17:30

侵权处理

客服QQ点击咨询

关注抖音号

定期抽VIP

Copyright © 2016-2021 https://www.171739.xyz/ 滇ICP备13200218号

快速回复 返回顶部 返回列表