• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.net.ssl;
18 
19 import static org.junit.Assert.assertFalse;
20 import static org.junit.Assert.assertNotNull;
21 import static org.junit.Assert.assertNull;
22 import static org.junit.Assert.assertTrue;
23 import static org.junit.Assert.fail;
24 
25 import com.android.org.conscrypt.tlswire.TlsTester;
26 import com.android.org.conscrypt.tlswire.handshake.ClientHello;
27 import com.android.org.conscrypt.tlswire.handshake.HelloExtension;
28 import java.nio.ByteBuffer;
29 import javax.net.ssl.SSLContext;
30 import javax.net.ssl.SSLEngine;
31 import javax.net.ssl.SSLEngineResult;
32 import javax.net.ssl.SSLException;
33 import javax.net.ssl.SSLSession;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.junit.runners.JUnit4;
37 
38 @RunWith(JUnit4.class)
39 public class SSLEnginesTest {
40 
41     private static class BrokenSSLEngine extends SSLEngine {
wrap(ByteBuffer[] byteBuffers, int i, int i1, ByteBuffer byteBuffer)42         @Override public SSLEngineResult wrap(ByteBuffer[] byteBuffers, int i, int i1,
43                 ByteBuffer byteBuffer) throws SSLException { throw new AssertionError(); }
unwrap(ByteBuffer byteBuffer, ByteBuffer[] byteBuffers, int i, int i1)44         @Override public SSLEngineResult unwrap(ByteBuffer byteBuffer, ByteBuffer[] byteBuffers,
45                 int i, int i1) throws SSLException { throw new AssertionError(); }
getDelegatedTask()46         @Override public Runnable getDelegatedTask() { throw new AssertionError(); }
closeInbound()47         @Override public void closeInbound() throws SSLException { throw new AssertionError(); }
isInboundDone()48         @Override public boolean isInboundDone() { throw new AssertionError(); }
closeOutbound()49         @Override public void closeOutbound() { throw new AssertionError(); }
isOutboundDone()50         @Override public boolean isOutboundDone() { throw new AssertionError(); }
getSupportedCipherSuites()51         @Override public String[] getSupportedCipherSuites() { throw new AssertionError(); }
getEnabledCipherSuites()52         @Override public String[] getEnabledCipherSuites() { throw new AssertionError(); }
setEnabledCipherSuites(String[] strings)53         @Override public void setEnabledCipherSuites(String[] strings) { throw new AssertionError(); }
getSupportedProtocols()54         @Override public String[] getSupportedProtocols() { throw new AssertionError(); }
getEnabledProtocols()55         @Override public String[] getEnabledProtocols() { throw new AssertionError(); }
setEnabledProtocols(String[] strings)56         @Override public void setEnabledProtocols(String[] strings) { throw new AssertionError(); }
getSession()57         @Override public SSLSession getSession() { throw new AssertionError(); }
beginHandshake()58         @Override public void beginHandshake() throws SSLException { throw new AssertionError(); }
getHandshakeStatus()59         @Override public SSLEngineResult.HandshakeStatus getHandshakeStatus() { throw new AssertionError(); }
setUseClientMode(boolean b)60         @Override public void setUseClientMode(boolean b) { throw new AssertionError(); }
getUseClientMode()61         @Override public boolean getUseClientMode() { throw new AssertionError(); }
setNeedClientAuth(boolean b)62         @Override public void setNeedClientAuth(boolean b) { throw new AssertionError(); }
getNeedClientAuth()63         @Override public boolean getNeedClientAuth() { throw new AssertionError(); }
setWantClientAuth(boolean b)64         @Override public void setWantClientAuth(boolean b) { throw new AssertionError(); }
getWantClientAuth()65         @Override public boolean getWantClientAuth() { throw new AssertionError(); }
setEnableSessionCreation(boolean b)66         @Override public void setEnableSessionCreation(boolean b) { throw new AssertionError(); }
getEnableSessionCreation()67         @Override public boolean getEnableSessionCreation() { throw new AssertionError(); }
68     }
69 
70     private static final ByteBuffer EMPTY_BUFFER = ByteBuffer.allocate(0);
71 
72     @Test
testIsSupported()73     public void testIsSupported() throws Exception {
74         SSLEngine e = SSLContext.getDefault().createSSLEngine();
75         assertTrue(SSLEngines.isSupportedEngine(e));
76 
77         e = new BrokenSSLEngine();
78         assertFalse(SSLEngines.isSupportedEngine(e));
79     }
80 
81     @Test
testUseSessionTickets()82     public void testUseSessionTickets() throws Exception {
83         try {
84             SSLEngines.setUseSessionTickets(new BrokenSSLEngine(), true);
85             fail();
86         } catch (IllegalArgumentException expected) {
87         }
88 
89         SSLEngine e = SSLContext.getDefault().createSSLEngine();
90         e.setUseClientMode(true);
91         SSLEngines.setUseSessionTickets(e, true);
92 
93         ClientHello hello = getClientHello(e);
94         assertNotNull(hello.findExtensionByType(HelloExtension.TYPE_SESSION_TICKET));
95 
96         e = SSLContext.getDefault().createSSLEngine();
97         e.setUseClientMode(true);
98         SSLEngines.setUseSessionTickets(e, false);
99 
100         hello = getClientHello(e);
101         assertNull(hello.findExtensionByType(HelloExtension.TYPE_SESSION_TICKET));
102     }
103 
getClientHello(SSLEngine e)104     private static ClientHello getClientHello(SSLEngine e) throws Exception {
105         ByteBuffer out = ByteBuffer.allocate(64 * 1024);
106 
107         e.wrap(EMPTY_BUFFER, out);
108         out.flip();
109         byte[] data = new byte[out.limit()];
110         out.get(data);
111 
112         return TlsTester.parseClientHello(data);
113     }
114 }
115