gpt4 book ai didi

android - 使用 SSLEngine 时出现异常

转载 作者:太空宇宙 更新时间:2023-11-03 14:57:42 25 4
gpt4 key购买 nike

我正在为 Android 手机编写自定义服务器来处理 https 请求。它首先监听特定端口,一旦建立连接,它就会与客户端进行握手。这是代码 fragment :

ServerSocket sock = new ServerSocket(8080);
Socketclient client = sock.accept();
doHandshake(client);

...

void doHandshake(Socket socket) throws Exception {
try {

SSLContext context = SSLContext.getDefault();
SSLEngine engine = context.createSSLEngine();
engine.setUseClientMode(false);

SSLSession session = engine.getSession();
// Create byte buffers to use for holding application data
ByteBuffer myAppData = ByteBuffer.allocate(session.getApplicationBufferSize());
ByteBuffer peerAppData = ByteBuffer.allocate(session.getApplicationBufferSize());

ByteBuffer myNetData = ByteBuffer.allocate(session.getPacketBufferSize());
byte[] peerNetData = new byte[session.getPacketBufferSize()];

// Begin handshake
engine.beginHandshake();
SSLEngineResult.HandshakeStatus hs = engine.getHandshakeStatus();
InputStream inputStream = socket.getInputStream();
OutputStream outputStream = socket.getOutputStream();

int bytesRead;
// Process handshaking message
while (hs != SSLEngineResult.HandshakeStatus.FINISHED &&
hs != SSLEngineResult.HandshakeStatus.NOT_HANDSHAKING) {

switch (hs) {

case NEED_UNWRAP:
// Receive handshaking data from peer
bytesRead = inputStream.read(peerNetData);
if (bytesRead < 0) {
// The channel has reached end-of-stream
}

ByteBuffer peerData = ByteBuffer.wrap(peerNetData, 0, bytesRead);

SSLEngineResult res = engine.unwrap(peerData, peerAppData);

当我将浏览器指向这个服务器 (url = https://localhost:8080) 时,我在“engine.unwrap(peerData, peerAppData)”上遇到了异常

javax.net.ssl.SSLProtocolException: SSL handshake terminated: ssl=0xb8347db8: Failure in SSL library, 通常是协议(protocol)错误错误:1408A0C1:SSL 例程:SSL3_GET_CLIENT_HELLO:没有共享密码(外部/openssl/ssl/s3_srvr.c:1394 0xace00e61:0x00000000)

我错过了什么?

最佳答案

你应该扔掉这段代码。如果您要使用流,请摆脱 SSLEngine 并只使用 SSLSocketSSLEngine 用于SocketChannels 的非阻塞模式,如果不将流添加到混合中就很难做到这一点。

关于android - 使用 SSLEngine 时出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27218899/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com