2012-05-15 42 views
2

我正在編寫一個應用程序,它將SSLEngine與NIO一起使用,我同時編寫客戶端和服務器。 客戶端能夠連接到服務器,他連接後,我希望他能夠執行會話恢復/重新協商,但目前沒有運氣..Java JSSE SSLEngine無法恢復SSL會話

由於使用SSLEngine的代碼是相當大的(SSLEngine用法是很複雜),我會寫一個簡單的僞代碼演示情況:

Server: 
    global sslcontext initialized once 
    await new client 
    client.sslEngine = create new server ssl engine using the global sslcontext 
    client.handleHandshake and wait for it to be done 
    handle client. 

Client: 
    global sslcontext initialized once 
    sslEngine = create new client ssl engine using the global sslcontext 
    performHandshake and wait for it to be done 
    disconnect (close gracefully the connection) 
    sslEngine = create new client ssl engine using the global sslcontext 
    configure engine to not allow session creation 
    performHandshake and wait for it to be done 

**我更願意再發布的代碼,可以幫助(甚至是完整的代碼,雖然作爲任何部分我說它是巨大的..)

當我執行我的程序第一次連接成功,但第二次導致異常:

javax.net.ssl.SSLHandshakeException: No existing session to resume 

我是否錯過了一些ssl會話恢復所需的配料?

回答

5

如果您使用SSLContext.createEngine(host, port)創建它,SSLEngine將僅恢復會話。否則它無法知道它在與誰通話,所以沒有辦法知道SSLSession要加入。

+0

謝謝你非常非常 - 你剛剛完成 – bennyl

+0

@bennyl的無盡googeling :) 3天,不是記錄,並且它不完全明顯,但是當你去想它一定是這樣...... – EJP

+0

我不要認爲它必須是這樣的 - 因爲SSLEngine與傳輸層分離,我認爲通過使用相同的SSLContext創建2個SSLEngines,它們將共享相同的SSLSession緩存...... – bennyl

0

SSLContext應該是singleton。 您可以使用netty 4.0.44.Final SslContextBuilder。通過sessionId工作恢復會話。

private SslContext sslContext; 
... 

if (serverSSLContext == null) { 
    serverSSLContext = SslContextBuilder.forServer(new File("cert.crt"), new File("cert.key")).build(); 
} 
channelPipeLine.addLast(serverSSLContext.newHandler(channelPipeLine.channel().alloc()));