生成证书与密钥
shell脚本
复制代码
#!/bin/bash
PROJECT_NAME=“https Project”
cat > ca_cert.conf << EOF
[ req ]
distinguished_name = req_distinguished_name
prompt = no
[ req_distinguished_name ]
O = $PROJECT_NAME Certificate Authority
EOF
cat > server_cert.conf << EOF
[ req ]
distinguished_name = req_distinguished_name
prompt = no
[ req_distinguished_name ]
O = $PROJECT_NAME
CN =
EOF
cat > client_cert.conf << EOF
[ req ]
distinguished_name = req_distinguished_name
prompt = no
[ req_distinguished_name ]
O = $PROJECT_NAME Device Certificate
CN =
EOF
mkdir ca
mkdir server
mkdir client
openssl genrsa -out ca.key 1024
openssl genrsa -out server.key 1024
openssl genrsa -out client.key 1024
openssl req -out ca.req -key ca.key -new -config ./ca_cert.conf
openssl req -out server.req -key server.key -new -config ./server_cert.conf
openssl req -out client.req -key client.key -new -config ./client_cert.conf
openssl x509 -req -in ca.req -out ca.crt -sha1 -days 5000 -signkey ca.key
openssl x509 -req -in server.req -out server.crt -sha1 -CAcreateserial -days 5000 -CA ca.crt -CAkey ca.key
openssl x509 -req -in client.req -out client.crt -sha1 -CAcreateserial -days 5000 -CA ca.crt -CAkey ca.key
mv ca.crt ca.key ca/
mv server.crt server.key server/
mv client.crt client.key client/
rm *.conf
rm *.req
rm *.srl
复制代码
一些命令的解释
复制代码
openssl genrsa [-out filename] [-passout arg] [-des] [-des3] [-idea] [numbits]
选项说明:
-out filename:将生成的私钥保存至filename文件,若未指定输出文件,则为标准输出。
-numbits:指定要生成的私钥的长度,默认为1024。该项必须为命令行的最后一项参数。
-des|-des3|-idea:指定加密私钥文件用的算法,这样每次使用私钥文件都将输入密码,太麻烦所以很少使用。
-passout args:加密私钥文件时,传递密码的格式,如果要加密私钥文件时单未指定该项,则提示输入密码。传递密码的args的格式
openssl req -out ca.req -key ca.key -new -config ./ca_cert.conf
主要命令选项:
-new :说明生成证书请求文件
-key :指定已有的秘钥文件生成秘钥请求,只与生成证书请求选项-new 配合。
-out :指定生成的证书请求或者自签名证书名称
openssl x509 -req -in ca.req -out ca.crt -sha1 -days 5000 -signkey ca.key
openssl x509命令具以下的一些功能,例如输出证书信息,签署证书请求文件、生成自签名证书、转换证书格式等。
-in filename:指定证书输入文件,若同时指定了"-req"选项,则表示输入文件为证书请求文件,再使用"-signkey"提供自签署时使用的私钥。
-out filename:指定输出文件
-days: 指定证书的有效时间长短。缺省为30天
复制代码
四、安装flask
需要安装python 的 openssl 的类库,使用pip 安装
pip install pyOpenSSL
五、https单向认证握手流程
python实现
server端:
复制代码
from flask import Flask
app = Flask(name)
@app.route(’/login’)
def hello_world():
return ‘Hello World!’
if name == ‘main’:
app.run(host=“0.0.0.0”, port=8091, ssl_context=(‘server.crt’, ‘server.key’))
复制代码
客户端:
复制代码
import urllib.request
import ssl
if name == ‘main’:
CA_FILE = “ca.crt”
context = ssl.SSLContext(ssl.PROTOCOL_TLS)
context.check_hostname = False
context.load_verify_locations(CA_FILE)
context.verify_mode = ssl.CERT_REQUIRED
try:
request = urllib.request.Request(‘https://127.0.0.1:8091/login’)
res = urllib.request.urlopen(request, context=context)
print(res.code)
print(res.read().decode(“utf-8”))
except Exception as ex:
print(“Found Error in auth phase:%s” % str(ex))
复制代码
六、https双向认证握手流程
python实现
客户端:
复制代码
from flask import Flask, request, Response
import json
app = Flask(name)
@app.route("/login")
def hello():
return “Hello World!”
@app.route(’/login1’, methods=[‘POST’])
def login():
username = request.form.get(“username”)
password = request.form.get(“password”)
login_config = {
“name”: “pwd1”
}
if username in login_config.keys():
if password == login_config[username]:
return Response(json.dumps(True), status=200, mimetype=‘application/json’)
return Response(json.dumps(False), status=200, mimetype='application/json')
if name == “main”:
app.run(host=“0.0.0.0”, port=8091, ssl_context=(‘server/server.crt’, ‘server/server.key’))
复制代码
客户端:
复制代码
import urllib.request
import ssl
if name == ‘main’:
CA_FILE = “ca.crt”
context = ssl.SSLContext(ssl.PROTOCOL_TLS) context.check_hostname = False context.load_verify_locations(CA_FILE) context.verify_mode = ssl.CERT_REQUIRED dict = { "username": "name", "password": "pwd1", } data = urllib.parse.urlencode(dict).encode('utf-8') try: request = urllib.request.Request('https://127.0.0.1:8091/login') res = urllib.request.urlopen(request, context=context) print(res.code) print(res.read().decode("utf-8")) except Exception as ex: print("Found Error in auth phase:%s" % str(ex)) try: request = urllib.request.Request('https://127.0.0.1:8091/login1', data=data, method='POST') res = urllib.request.urlopen(request, context=context) print(res.code) print(res.read().decode("utf-8")) except Exception as ex: print("Found Error in auth phase:%s" % str(ex)) USB Microphone https://www.soft-voice.com/
Wooden Speakers https://www.zeshuiplatform.com/
亚马逊测评 www.yisuping.cn
深圳网站建设www.sz886.com