CHUNHANHAN 2021-12-08 22:03 采纳率: 25%
浏览 10
已结题

openssl server client发送消息,连接自动关闭

问题遇到的现象和发生背景

在python使用subprocess搭建了openssl s_server和s_client端,建立连接后,通过tkinter文本框发送通信内容。没有报错,但是通信内容不能成功发送,并且发送后通信连接关闭。

问题相关代码,请勿粘贴截图

import tkinter as tk
import tkinter.messagebox
import subprocess
import threading
import base64

myWindow=tk.Tk()
myWindow.title('post-quantumn + https')
myWindow.geometry('1050x800')
myWindow.resizable(width=True, height=True)
canvas = tk.Canvas(myWindow, bg='white',width=1050, height=800)
tk.Label(myWindow, text='CA',bg='red',width=10,height=2).place(x=100,y=20)
tk.Label(myWindow, text='server',bg='red',width=10,height=2).place(x=500,y=20)
tk.Label(myWindow, text='client',bg='red',width=10,height=2).place(x=850,y=20)
line = canvas.create_line(350, 0, 350, 800,width=1,fill='black')
line = canvas.create_line(700, 0, 700, 800,width=1,fill='black')
tk.Label(myWindow, text='operation',bg='white',width=10,height=1).place(x=100,y=70)
tk.Label(myWindow, text='operation',bg='white',width=10,height=1).place(x=500,y=70)
tk.Label(myWindow, text='operation',bg='white',width=10,height=1).place(x=850,y=70)
tk.Label(myWindow, text='file',bg='white',width=10,height=1).place(x=100,y=250)
tk.Label(myWindow, text='file',bg='white',width=10,height=1).place(x=500,y=250)
tk.Label(myWindow, text='file',bg='white',width=10,height=1).place(x=850,y=250)
tk.Label(myWindow, text='message',bg='white',width=10,height=1).place(x=100,y=390)
tk.Label(myWindow, text='massage',bg='white',width=10,height=1).place(x=500,y=390)
tk.Label(myWindow, text='message',bg='white',width=10,height=1).place(x=850,y=390)
global child1

class MyThread(threading.Thread):
    def __init__(self, func, *args):
        super().__init__()
        
        self.func = func
        self.args = args
        
        self.setDaemon(True)
        self.start()   
        
    def run(self):
        self.func(*self.args)

def genCAkey():
    tk.Label(myWindow, text='CA key',bg='red',width=12,height=1).place(x=80,y=270)
    tk.Label(myWindow, text='1. genetarte CA key using algorithm.',bg='white',width=40,height=1).place(x=50,y=410)
CAkey=tk.Button(myWindow, text="generate CA key",command=genCAkey).place(x=80,y=90)

def genCAroot():
    tk.Label(myWindow, text='CA root cert',bg='red',width=12,height=1).place(x=80,y=300)
    tk.Label(myWindow, text='2. generate CA root cert using dilithium2.',bg='white',width=40,height=1).place(x=50,y=440)
    subprocess.run(["/home/hannah/apps/openssl", "req", "-x509", "-new", "-newkey", "dilithium2", "-keyout", "dilithium2_CA.key", "-out", "dilithium2_CA.crt", "-nodes", "-subj", "/CN=oqtest", "-days", "365" ,"-config", "/home/hannah/apps/openssl.cnf"])
CAkey=tk.Button(myWindow, text="generate CA  root cert",command=genCAroot).place(x=80,y=130)

def sign():
    tk.Label(myWindow, text='server cert signed by CA',bg='red',width=20,height=1).place(x=80,y=360)
    tk.Label(myWindow, text='5. sign server cert using dilithium2', bg='white',width=40,height=1).place(x=50,y=530)
    subprocess.run(["/home/hannah/apps/openssl", "x509", "-req", "-in", "dilithium2_srv.csr", "-out", "dilithium2_srv.crt", "-CA", "dilithium2_CA.crt", "-CAkey", "dilithium2_CA.key", "-CAcreateserial", "-days", "365"])
CAkey=tk.Button(myWindow, text="sign server's cert",command=sign).place(x=80,y=170)            #CA's operation

def genServerKey():
    tk.Label(myWindow, text='server key',bg='red',width=12,height=1).place(x=460,y=270)
    tk.Label(myWindow, text='3. generate server key using algorithm',bg='white',width=40,height=1).place(x=400,y=470)
    subprocess.run(["/home/hannah/apps/openssl", "genpkey", "-algorithm", "dilithium2", "-out", "dilithium2_srv.key"])
CAkey=tk.Button(myWindow, text="generate server key",command=genServerKey).place(x=460,y=90)

def genCSR():
    tk.Label(myWindow, text='CSR from server',bg='red',width=15,height=1).place(x=80,y=330)
    tk.Label(myWindow, text='4. generate cert sign request, sending to CA',bg='white',width=40,height=1).place(x=400,y=500)
    subprocess.run(["/home/hannah/apps/openssl", "req", "-new", "-newkey", "dilithium2", "-keyout", "dilithium2_srv.key", "-out", "dilithium2_srv.csr", "-nodes", "-subj", "/CN=oqtest server","-config", "/home/hannah/apps/openssl.cnf" ])
CAkey=tk.Button(myWindow, text="generate cert sign request",command=genCSR).place(x=460,y=130)


def importcert():
    global child1
    tk.Label(myWindow, text='server cert signed by CA',bg='red',width=20,height=1).place(x=460,y=300)
    tk.Label(myWindow, text='6. import serber cert signed by CA',bg='white',width=40,height=1).place(x=400,y=560)
    child1=subprocess.Popen(["/home/hannah/apps/openssl", "s_server", "-accept", "10001", "-key", "dilithium2_srv.key", "-cert", "dilithium2_srv.crt"], stdin=subprocess.PIPE)
CAkey=tk.Button(myWindow, text="import server's cert",command=lambda :MyThread(importcert)).place(x=460,y=170)


def sendmessageS():   ######
    window_server_msg = tk.Toplevel(myWindow)
    window_server_msg.geometry('300x200')
    window_server_msg.title('Server\'s message')
    text1 = tk.Text(window_server_msg, height=10)  
    text1.place(x=75, y=50)
    
    def closeServer():
        window_server_msg.destroy()
    def sendserver():
        smsg=text1.get("1.0" , "end")
        smsg2=smsg.encode()
        smsg3=base64.b64encode(smsg2)
        child1.communicate(smsg3)
       #print(smsg)
    tk.Button(window_server_msg, text='send', command=lambda :MyThread(sendserver)).place(x=50, y=150)
    tk.Button(window_server_msg, text='close', command=closeServer).place(x=175, y=150)
CAkey=tk.Button(myWindow, text="send message",command=sendmessageS).place(x=460,y=210)        #server's operation

def setKEX():
    tk.Label(myWindow, text='CA private key',bg='red',width=12,height=1).place(x=150,y=20)
    tk.Label(myWindow, text='7. negotiate KEX: kyber512 and inform server',bg='white',width=40,height=1).place(x=750,y=590)
CAkey=tk.Button(myWindow, text="negotiate KEX",command=setKEX).place(x=830,y=90)

def getCAroot():
    tk.Label(myWindow, text='CA root cert',bg='red',width=12,height=1).place(x=830,y=270)
    tk.Label(myWindow, text='8. import CA root cert to client',bg='white',width=40,height=1).place(x=750,y=620)
    subprocess.run(["/home/hannah/apps/openssl", "s_client", "-groups", "kyber512", "-CAfile", "dilithium2_CA.crt", "-connect", "localhost:10001"])
CAkey=tk.Button(myWindow, text="import CA's root cert",command=lambda :MyThread(getCAroot)).place(x=830,y=130)

def sendmessageC():           #####
    window_client_msg = tk.Toplevel(myWindow)
    window_client_msg.geometry('300x200')
    window_client_msg.title('Client\'s message')
CAkey=tk.Button(myWindow, text="send message",command=sendmessageC).place(x=830,y=170)         #client's operation

canvas.pack() 
myWindow.mainloop()
运行结果及报错内容

https连接断开后,显示下图内容

img

我的解答思路和尝试过的方法
我想要达到的结果

在text1文本框输入server发给client的消息,能正确显示

  • 写回答

0条回答 默认 最新

    报告相同问题?

    问题事件

    • 系统已结题 12月16日
    • 创建了问题 12月8日

    悬赏问题

    • ¥15 晶体塑性有限元——Damask求解
    • ¥15 写出这个有没有人能写一下今天中午就要
    • ¥30 设计一个图形用户界面来控制你机械臂的运动
    • ¥30 3d打印机无法识别到SD卡,如何解决?(相关搜索:格式化)
    • ¥15 RPG游戏架构设计和开发方法
    • ¥15 前端返回pdf时不显示内容
    • ¥50 如何在不能联网影子模式下的电脑解决usb锁
    • ¥20 服务器redhat5.8网络问题
    • ¥15 如何利用c++ MFC绘制复杂网络多层图
    • ¥20 要做柴油机燃烧室优化 需要保持压缩比不变 请问怎么用AVL fire ESE软件里面的 compensation volume 来使用补偿体积来保持压缩比不变