ubuntu下openssl签名证书制作流程及验证demo

news/2024/11/16 17:50:29/

1.创建根CA证书

        生成 rootCA.key 以及 rootCA.crt. 用你的域名或者ip地址替换demo.mlopshub.com,比如192.168.10.11

openssl req -x509 \-sha256 -days 356 \-nodes \-newkey rsa:2048 \-subj "/CN=demo.mlopshub.com/C=CN/L=Beijing" \-keyout rootCA.key -out rootCA.crt 
或者
openssl req -x509 \-sha256 -days 356 \-nodes \-newkey rsa:2048 \-subj "/CN=192.168.10.11/C=CN/L=Beijing" \-keyout rootCA.key -out rootCA.crt 

如果上述命令提示Can’t load /home/username/.rnd into RNG,则需要你手动创建这个文件

2. 生成自签名证书

2.1生成server的私有key

openssl genrsa -out server.key 2048

2.2.创建证书签名请求配置

根据实际情况替换域名以及IP

cat > csr.conf <<EOF
[ req ]
default_bits = 2048
prompt = no
default_md = sha256
req_extensions = req_ext
distinguished_name = dn[ dn ]
C = US
ST = Beijing
L = Beijing
O = MLopsHub
OU = MlopsHub Dev
CN =test.com[ req_ext ]
subjectAltName = @alt_names[ alt_names ]
DNS.1 = test.com
DNS.2 = test2.com
IP.1 = 192.168.10.11
IP.2 = 192.168.1.22EOF

2.3.使用服务器私钥生成证书签名请求(CSR)

openssl req -new -key server.key -out server.csr -config csr.conf

2.4.创建一个外部文件


根据实际情况替换域名以及IP

cat > cert.conf <<EOFauthorityKeyIdentifier=keyid,issuer
basicConstraints=CA:FALSE
keyUsage = digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment
subjectAltName = @alt_names[alt_names]
DNS.1 = test.com
IP.1 = 192.168.10.62
EOF

2.5.使用自签名CA生成SSL证书

openssl x509 -req \-in server.csr \-CA rootCA.crt -CAkey rootCA.key \-CAcreateserial -out server.crt \-days 365 \-sha256 -extfile cert.conf

 将server.crt  server.key放到服务器目录下

将rootCA.crt放到客户端进行验签

3.python验证

3.1客户端单向/双向/不认证

(1)服务器端的代码

import socket
import ssl# 服务器配置
SERVER_HOST = '192.168.10.62'
SERVER_PORT = 12345def ssl_auth_double():# 创建一个普通的 TCP 套接字server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)print("Server socket created")# 绑定到本地地址和端口server_address = (SERVER_HOST, SERVER_PORT)server_socket.bind(server_address)print("Bound to address and port")# 开始监听连接server_socket.listen(1)print("Listening for connections")# 创建 SSL 上下文ssl_context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)ssl_context.load_cert_chain(certfile="server.crt", keyfile="server.key")ssl_context.load_verify_locations(cafile="root_cli.crt")ssl_context.verify_mode = ssl.CERT_REQUIREDtry:while True:# 接受客户端连接client_socket, client_address = server_socket.accept()print(f"Accepted connection from {client_address}")# 将客户端 socket 包装成 SSL socketssl_socket = ssl_context.wrap_socket(client_socket, server_side=True)try:while True:# 接收客户端数据data = ssl_socket.recv(1024)if not data:break# 将接收到的数据原样返回ssl_socket.sendall(data)except socket.error as e:print(f"Socket error: {e}")finally:# 关闭 SSL socketssl_socket.close()except KeyboardInterrupt:print("Server stopped by user")finally:# 关闭服务器 socketserver_socket.close()def ssl_auth_single():# 创建一个socket对象server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)print("Socket created")# 绑定到本地地址和端口server_address = (SERVER_HOST, SERVER_PORT)server_socket.bind(server_address)print("Bound to address and port")# 开始监听连接server_socket.listen(1)print("Listening for connections")# 创建SSL上下文ssl_context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)ssl_context.load_cert_chain(certfile='server.crt', keyfile='server.key')print('Server is listening on {}:{}'.format(*server_address))while True:# 接受客户端连接client_socket, client_address = server_socket.accept()print('Accepted connection from {}:{}'.format(*client_address))# 将客户端socket包装成SSL socketssl_socket = ssl_context.wrap_socket(client_socket, server_side=True)try:while True:# 接收客户端数据data = ssl_socket.recv(1024)if not data:break# 将接收到的数据原样返回ssl_socket.sendall(data)except socket.error as e:print('Socket error:', e)finally:# 关闭SSL socketssl_socket.close()def ssl_only():# 创建一个普通的 TCP/IP 套接字server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)server_socket.bind((SERVER_HOST, SERVER_PORT))server_socket.listen(5)# 创建 SSL 上下文,禁用客户端证书验证context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)context.load_cert_chain(certfile='server.crt', keyfile='server.key')context.verify_mode = ssl.CERT_NONE  # 不验证客户端证书# 将普通套接字封装为 SSL 套接字,开启 SSL 加密server_socket_ssl = context.wrap_socket(server_socket, server_side=True)print(f"Server is listening on {SERVER_HOST}:{SERVER_PORT}...")while True:# 等待客户端连接client_socket, addr = server_socket_ssl.accept()print(f"Connection from {addr} established!")try:# 接收数据data = client_socket.recv(1024)if data:print(f"Received: {data.decode('utf-8')}")# 将接收到的数据原样返回client_socket.sendall(data)else:print("No data received")except Exception as e:print(f"Error: {e}")finally:# 关闭客户端连接client_socket.close()
if __name__ == '__main__':ssl_only()

(2)客户端代码

import socket
import ssl
import os
import time
# 客户端配置
SERVER_HOST = '192.168.10.62'
SERVER_PORT = 12345
CA_CERT_FILE = 'rootCA.crt'  # CA 证书(客户端用于验证服务端)
current_directory = os.getcwd()
print(f"当前目录是: {current_directory}")def ssl_auth_double():client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)print("Client socket created")# 创建 SSL 上下文ssl_context = ssl.create_default_context(ssl.Purpose.SERVER_AUTH)ssl_context.load_cert_chain(certfile="client.crt", keyfile="client.key")ssl_context.load_verify_locations(cafile="rootCA.crt")ssl_context.verify_mode = ssl.CERT_REQUIRED# 将套接字包装为 SSL 套接字client_socket_ssl = ssl_context.wrap_socket(client_socket, server_hostname=SERVER_HOST)print("Client SSL socket wrapped")try:client_socket_ssl.connect((SERVER_HOST, SERVER_PORT))print(f"Connected to {SERVER_HOST}:{SERVER_PORT} over SSL.")while True:# 发送数据client_socket_ssl.sendall(b"Hello from SSL client!")print("Data sent to server")# 接收响应data = client_socket_ssl.recv(1024)print(f"Received from server: {data.decode('utf-8')}")except Exception as e:print(f"Error: {e}")finally:# 关闭连接client_socket_ssl.close()
def ssl_auth_single():# 创建一个普通的 TCP 套接字client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)print("Client socket created")# 将套接字包装为 SSL 套接字client_socket_ssl = ssl.wrap_socket(client_socket, certfile=None, keyfile=None, server_side=False, ssl_version=ssl.PROTOCOL_SSLv23, ca_certs=CA_CERT_FILE, cert_reqs=ssl.CERT_REQUIRED)print("Client SSL socket wrapped")try:client_socket_ssl.connect((SERVER_HOST, SERVER_PORT))print(f"Connected to {SERVER_HOST}:{SERVER_PORT} over SSL.")while True:# 发送数据client_socket_ssl.sendall(b"Hello from SSL client!")print("Data sent to server")# 接收响应data = client_socket_ssl.recv(1024)print(f"Received from server: {data.decode('utf-8')}")time.sleep(1)except Exception as e:print(f"Error: {e}")finally:# 关闭连接client_socket_ssl.close()print("Connection closed")
def ssl_only():# 创建一个普通的 TCP 套接字client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)print("Client socket created")# 创建 SSL 上下文ssl_context = ssl.create_default_context(ssl.Purpose.SERVER_AUTH)ssl_context.check_hostname = Falsessl_context.verify_mode = ssl.CERT_NONE# 设置支持的加密套件ssl_context.set_ciphers('ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:AES:CAMELLIA:DES-CBC3-SHA:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!aECDH:!EDH-DSS-DES-CBC3-SHA:!EDH-RSA-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA')# 将套接字包装为 SSL 套接字client_socket_ssl = ssl_context.wrap_socket(client_socket, server_hostname=SERVER_HOST)print("Client SSL socket wrapped")try:client_socket_ssl.connect((SERVER_HOST, SERVER_PORT))print(f"Connected to {SERVER_HOST}:{SERVER_PORT} over SSL.")while True:# 发送数据client_socket_ssl.sendall(b"Hello from SSL client!")print("Data sent to server")# 接收响应data = client_socket_ssl.recv(1024)print(f"Received from server: {data.decode('utf-8')}")time.sleep(1)except Exception as e:print(f"Error: {e}")finally:# 关闭连接client_socket_ssl.close()print("Connection closed")
if __name__ == '__main__':ssl_only()


http://www.ppmy.cn/news/1547502.html

相关文章

fpga 同步fifo

FIFO 基础知识 FIFO&#xff08;First In First Out&#xff0c;即先入先出&#xff09;&#xff0c;是一种数据缓存器&#xff0c;用来实现数据先入先出 的读写方式。在 FPGA 或者 ASIC 中使用到的 FIFO 一般指的是对数据的存储具有先入先出 特性的缓存器&#xff0c;常被用于…

远程控制步骤

当远在千里之外的朋友想求助你帮他找到他电脑上的文件、或者是给他安装软件时。但是你给他说了他又找不到&#xff0c;那么这时你就可以通过控制对方的电脑去做一系列的操作。 如何远程控制对方的电脑非常关键。 方法一&#xff08;Windows自带远程桌面功能&#xff09;&#…

利用正则表达式批量修改文件名

首先&#xff0c; 我们需要稍微学习一下正则表达式的使用方式&#xff0c;可以看这里&#xff1a;Notepad正则表达式使用方法_notepad正则匹配-CSDN博客 经过初步学习之后&#xff0c;比较重要的内容我做如下转载&#xff1a; 元字符是正则表达式的基本构成单位&#xff0c;它们…

离线语音识别自定义功能怎么用?

一、离线语音识别 随着人工智能的飞速发展&#xff0c;离线语音识别技术成为了一项备受瞩目的创新。离线语音识别技术能够将人的语音转化为可理解的文本&#xff0c;无需依赖网络连接&#xff0c;极大地提升了语音识别的便捷性和实用性。 与传统的云端语音识别相比&#xff0c;…

【go从零单排】File Paths文件路径

&#x1f308;Don’t worry , just coding! 内耗与overthinking只会削弱你的精力&#xff0c;虚度你的光阴&#xff0c;每天迈出一小步&#xff0c;回头时发现已经走了很远。 &#x1f4d7;概念 在 Go 中&#xff0c;处理文件路径通常使用 path/filepath 包。这个包提供了一系…

达梦数据库迁移j脚本

国产环境使用达梦数据库的越来越多&#xff0c;除了使用管理工具&#xff0c;还是可以使用脚本。 下面简单记录下&#xff0c;我在迁移中遇到的问题&#xff1a; 备份脚本 使用此脚本可以一次备份一个数据 backup_one_db.sh #!/bin/bashexport DB$1 export PASS<your_p…

杰控通过 OPCproxy 获取数据发送到服务器

把数据从 杰控 取出来发到服务器 前提你在杰控中已经有变量了&#xff08;wincc 也适用&#xff09; 打开你的opcproxy 软件包 opcvarFile 添加变量 写文件就写到 了 opcproxy.ini中 这个文件里就是会读取到的数据 然后 opcproxy.exe发送到桌面快捷方式再考回来 &#…

【Qt】Qt在窗口中加载Web界面的方法汇总

1、Qt WebEngine 1)Qt版本:Qt5.4以上; 2)平台要求(https://doc.qt.io/archives/qt-5.9/qtwebengine-platform-notes.html): 例如:Windows下只能使用 MSVC 编译器,不支持MinGW编译器,会报错(: error: Unknown module(s) in QT: webenginewidgets) 并且不能用在Qt编…