使用SSLEngine时例外

时间:2014-11-30 22:15:02

标签: android ssl jsse

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

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)时,我在&#34; engine.unwrap(peerData,peerAppData)&#34;

上遇到异常

javax.net.ssl.SSLProtocolException:SSL握手终止:ssl = 0xb8347db8:SSL库失败,通常是协议错误 错误:1408A0C1:SSL例程:SSL3_GET_CLIENT_HELLO:无共享密码(外部/ openssl / ssl / s3_srvr.c:1394 0xace00e61:0x00000000)

我错过了什么?

2 个答案:

答案 0 :(得分:0)

你应该扔掉这段代码。如果要使用流,请删除SSLEngine并使用SSLSocketSSLEngine用于SocketChannels的非阻止模式,并且在没有添加流的情况下很难做到这一点。

答案 1 :(得分:0)

  

没有共享密码

正确设置SSL引擎时出现问题。一个原因可能是您没有设置任何证书,因此只能使用匿名密码套件。但浏览器不支持这些不安全的密码套件,浏览器和服务器之间没有共享的密码套件。