HTTPS-自己生成数字证书
一、获取证书的途径
- 自签名证书,适用于开发者测试HTTPS,最快速的途径就是生成自签名证书,非常方便。
- Let’s Encrypt证书,可以使用免费CA机构签发的证书。
- 使用收费CA机构签发的证书,如果对证书安全性、兼容性、功能有特殊需求,可以向CA机构申请证书。
二、自签名证书
自签名证书是我们自己签发的,浏览器不会集成私有的CA机构的根证书,所以打开页面的时候会进行提示,用户选择信任证书之后,后续的通信就会进行加密保护的。
自签名证书的用途还是很广泛的,对于一些企业内部系统,由于购买证书需要成本,可以生成自签名证书,企业内部系统的用户一般运行在同一个局域网下,由防火墙保护,风险相对可控,当浏览器提示用户自签名证书存在风险时,用户可以选择信任自签名证书,等同于访问了一个HTTPS网站。
生成自签名证书的步骤如下
1.生成私钥对和CSR
我们设置密钥的长度为2048bit;
我们最终会得到flask_self_csr.pem和flask_self_key.pem两个文件;
CSR(Certificate Signing Request)表示证书签名请求,里面包含了服务器的密钥对,CA机构接收到请求会验证CSR请求的签名;
flask_self_csr.pem包含了我们的密钥对;
执行命令之后,我们可以在交互式提示中,设置证书包含的一些信息;
mango@mango-ubuntu:~/文档/blogs/web/证书$ openssl req -newkey rsa:2048 -nodes -keyout flask_self_key.pem -out flask_self_csr.pem
Generating a RSA private key
.........+++++
......+++++
writing new private key to 'flask_self_key.pem'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [AU]:CN
State or Province Name (full name) [Some-State]:Beijing
Locality Name (eg, city) []:Beijing
Organization Name (eg, company) [Internet Widgits Pty Ltd]:mango
Organizational Unit Name (eg, section) []:mango
Common Name (e.g. server FQDN or YOUR name) []:cee1-110-251-30-176.ngrok.io
Email Address []:mango@163.comPlease enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
- 生成自签名证书
接下来通过CSR生成证书,对于自签名证书,我们可以认为自己就是一个CA机构,输入如下命令生成证书:
mango@mango-ubuntu:~/文档/blogs/web/证书$ openssl x509 -signkey flask_self_key.pem -in flask_self_csr.pem -req -days 365 -out flask_self_cert.pem
Signature ok
subject=C = CN, ST = Beijing, L = Beijing, O = mango, OU = mango, CN = cee1-110-251-30-176.ngrok.io, emailAddress = mango@163.com
Getting Private key
- 验证证书
将生成的flask_self_cert.pem和flask_self_key.pem拷贝到站点根目录下,并设置启用ssl
from flask import Flaskapp = Flask(__name__)@app.route("/", methods=["GET"])
def hello():return 'hello python'if __name__ == "__main__":app.run('0.0.0.0', ssl_context=('flask_self_cert.pem', 'flask_self_key.pem'))# app.run('0.0.0.0', debug=True, ssl_context='adhoc')
三、使用Let’s Encrypt证书
Let’s Encrypt首先是一个CA机构,得到了很多大公司的支持,兼容性非常不错,同时它定义了ACME协议,将管理证书的流程进行了标准化、自动化,不用人工管理。可以使用基于ACME协议的客户端在Let’s Encrypt管理证书,官方推荐Certbot客户端,使用非常方便。
1.安装Certbot客户端
mango@mango-ubuntu:~/文档/blogs/web/证书/certbot$ sudo snap install --classic certbot
certbot 1.21.0 from Certbot Project (certbot-eff✓) installed
2.手动生成证书和密钥文件
mango@mango-ubuntu:~/文档/blogs/web/证书$ sudo certbot certonly --manual -d 565c-110-251-30-176.ngrok.io
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Requesting a certificate for 565c-110-251-30-176.ngrok.io- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Create a file containing just this data:csM3J5YGt3V-PQDeRpcDhjlpy7Hdf9tjh-NsIqqoA6A.eRfiNKPaGpDq-g1FefRl52GbfFeSDV_Qg8Gwe1KQP5MAnd make it available on your web server at this URL:http://565c-110-251-30-176.ngrok.io/.well-known/acme-challenge/csM3J5YGt3V-PQDeRpcDhjlpy7Hdf9tjh-NsIqqoA6A- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Press Enter to ContinueSuccessfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/565c-110-251-30-176.ngrok.io/fullchain.pem
Key is saved at: /etc/letsencrypt/live/565c-110-251-30-176.ngrok.io/privkey.pem
This certificate expires on 2022-02-10.
These files will be updated when the certificate renews.NEXT STEPS:
- This certificate will not be renewed automatically. Autorenewal of --manual certificates requires the use of an authentication hook script (--manual-auth-hook) but one was not provided. To renew this certificate, repeat this same certbot command before the certificate's expiry date.- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
If you like Certbot, please consider supporting our work by:* Donating to ISRG / Let's Encrypt: https://letsencrypt.org/donate* Donating to EFF: https://eff.org/donate-le
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
为了通过CA对站点的验证,我们需要新增对应的action来响应对应的请求
@app.route("/.well-known/acme-challenge/csM3J5YGt3V-PQDeRpcDhjlpy7Hdf9tjh-NsIqqoA6A")
def challenge():return 'csM3J5YGt3V-PQDeRpcDhjlpy7Hdf9tjh-NsIqqoA6A.eRfiNKPaGpDq-g1FefRl52GbfFeSDV_Qg8Gwe1KQP5M'
- 验证证书
将生成的证书和密钥文件拷贝到站点根目录,并修改文件权限
mango@mango-ubuntu:~/文档/blogs/webhook$ sudo cp /etc/letsencrypt/live/565c-110-251-30-176.ngrok.io/fullchain.pem fullchain.pem
mango@mango-ubuntu:~/文档/blogs/webhook$ sudo cp /etc/letsencrypt/live/565c-110-251-30-176.ngrok.io/privkey.pem
mango@mango-ubuntu:~/文档/blogs/webhook$ sudo chown mango fullchain.pem
mango@mango-ubuntu:~/文档/blogs/webhook$ sudo chown mango privkey.pem
修改站点启用ssl
from flask import Flaskapp = Flask(__name__)@app.route("/", methods=["GET"])
def hello():return 'hello python'@app.route("/.well-known/acme-challenge/csM3J5YGt3V-PQDeRpcDhjlpy7Hdf9tjh-NsIqqoA6A")
def challenge():return 'csM3J5YGt3V-PQDeRpcDhjlpy7Hdf9tjh-NsIqqoA6A.eRfiNKPaGpDq-g1FefRl52GbfFeSDV_Qg8Gwe1KQP5M'if __name__ == "__main__":app.run('0.0.0.0', ssl_context=('fullchain.pem', 'privkey.pem'))# app.run('0.0.0.0', ssl_context=('flask_self_cert.pem', 'flask_self_key.pem'))# app.run('0.0.0.0', debug=True, ssl_context='adhoc')
HTTPS-自己生成数字证书相关推荐
- https原理和数字证书
https原理和数字证书 为什么需要加密? 什么是对称加密? 什么是非对称加密? 非对称加密+对称加密? 中间人攻击 数字证书 数字签名 浏览器验证过程 怎么证明CA机构的公钥是可信的? HTTPS( ...
- 用 openssl 生成数字证书
以前用 windows 自带的 makecert 生成数字证书.这个命令有一些局限性,而且现在的 windows 也不支持了.网上查了一下,目前用的比较多的是开源工具 openssl.这个命令以前学习 ...
- Ubuntu使用OpenSSL生成数字证书详解
在安全通信编程中有时我们会用到数字证书进行通信加密,那么如何生成自己的数字证书进行测试呢?下面是使用OpenSSL在Ubuntu环境生成数字证书的步骤: 一.安装openssl a) 略 二.生 ...
- java生成数字证书_java生成数字证书方法
Java 中生成数字证书也很简单的. 1.创建证书 Java 中的 keytool.exe (位于 JDKBin 目录下)可以用来创建数字证书,所有的数字证书是以一条一条(采用别名区别)的形式存入证书 ...
- Keytool生成数字证书 + Tomcat https 配置
参考:http://www.cnblogs.com/JeffreySun/archive/2010/06/24/1627247.html https://baike.baidu.com/item/ht ...
- HTTPS生成数字证书
由于 HTTPS 具有良好的安全性,在开发中得到了越来越广泛 的应 像微信公众号 .小 程序 等的开发都要 使用 HTTPS 来完成. 在\j dk \ bin 录下 ,通过这个 工具可 以自己 生成 ...
- 利用keytool工具生成数字证书
一.制作数字证书 因测试微信小程序, 腾讯要求使用 https协议,所以需要使用证书.使用jdk工具制作数字证书流程如下: 1.查看JDK是否安装,使用命令java -version 2.切换目录至 ...
- Windows(32bit.64bit) OpenSSL生成数字证书pfx、cer。拒绝收费,(不需要编译openssl源代码,快速安装)
1. windows 安装 openssl 下载地址:Win32/Win64 OpenSSL Installer for Windows - Shining Light Productions 建议下 ...
- 聊一聊https中的数字证书
由于https中涉及的概念较多,因此简单说说其中较为麻烦的数字证书等相关内容. 由于http的传输服务是明文传输,所谓的明文传输就是在通信双方的中间通过抓包发现http的头部是明文,可以很容易获取co ...
- ubuntu使用OpenSSL生成数字证书常见错误修改
一.安装openssl a) 略 二.生成ca证书 a) 创建一个证书目录,mkdir /home/liuzhigong/SSL b) 将CA.sh拷贝到/h ...
最新文章
- 在实习面试的过程的当中,常见的面试题(自己面试一路走来,所出现的频率较高的面试题总结)
- docker的网络模式
- tomcat的日志文件权限与启动用户的权限不一致
- npoi 导入 winform excel_勤哲Excel服务器做影视制作企业管理系统 - 科技
- 潍职e校帮APP功能简介
- java 栈的变量_深入Java核心:JVM中的栈和局部变量
- UVA11888 Abnormal 89‘s【回文】
- 【Servlet】web.xml中url-pattern的用法
- 全国计算机等级考试二级C语言程序设计考点整理
- kali虚拟机分辨率设置
- Python提取PDF中的信息,写入Excel
- 计算机网络五层结构要点以及功能,试述五层协议的网络体系结构的要点,包括各层的主要功能...
- 费马大定理四分之一解决
- User does not have the ‘LOCK TABLES‘ privilege required to obtain a consistent snapshot by preventin
- 为什么要“推销自己”?
- 计算机属性cmd命令,怎么用命令提示符打开系统属性
- Android S 默认WIFi 热点名称
- 初学者这样玩 TypeScript,迟早进大厂系列!
- 都2022年了相册分类还如此不准?不如试试亲影
- Javascript 检测键盘按键