• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2017 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 package org.conscrypt;
17 
18 import java.security.Security;
19 import javax.net.ssl.SSLContext;
20 import javax.net.ssl.SSLEngine;
21 
22 /**
23  * Enumeration of various types of engines for use with engine-based benchmarks.
24  */
25 @SuppressWarnings({"ImmutableEnumChecker", "unused"})
26 public enum AndroidEngineFactory implements EngineFactory {
27     CONSCRYPT_UNPOOLED {
28         private final SSLContext clientContext = newConscryptClientContext();
29         private final SSLContext serverContext = newConscryptServerContext();
30 
31         @Override
newClientEngine(String cipher, boolean useAlpn)32         public SSLEngine newClientEngine(String cipher, boolean useAlpn) {
33             SSLEngine engine = initEngine(clientContext.createSSLEngine(), cipher, true);
34             if (useAlpn) {
35                 Conscrypt.setApplicationProtocols(engine, new String[] {"h2"});
36             }
37             return engine;
38         }
39 
40         @Override
newServerEngine(String cipher, boolean useAlpn)41         public SSLEngine newServerEngine(String cipher, boolean useAlpn) {
42             SSLEngine engine = initEngine(serverContext.createSSLEngine(), cipher, false);
43             if (useAlpn) {
44                 Conscrypt.setApplicationProtocols(engine, new String[] {"h2"});
45             }
46             return engine;
47         }
48 
49         @Override
dispose(SSLEngine engine)50         public void dispose(SSLEngine engine) {
51             engine.closeOutbound();
52         }
53     },
54     PLATFORM {
55         private final SSLContext clientContext = TestUtils.newClientSslContext(
56             Security.getProvider("AndroidOpenSSL"));
57         private final SSLContext serverContext = TestUtils.newServerSslContext(
58             Security.getProvider("AndroidOpenSSL"));
59 
60         @Override
newClientEngine(String cipher, boolean useAlpn)61         public SSLEngine newClientEngine(String cipher, boolean useAlpn) {
62             SSLEngine engine = initEngine(clientContext.createSSLEngine(), cipher, true);
63             if (useAlpn) {
64                 Conscrypt.setApplicationProtocols(engine, new String[] {"h2"});
65             }
66             return engine;
67         }
68 
69         @Override
newServerEngine(String cipher, boolean useAlpn)70         public SSLEngine newServerEngine(String cipher, boolean useAlpn) {
71             SSLEngine engine = initEngine(serverContext.createSSLEngine(), cipher, false);
72             if (useAlpn) {
73                 Conscrypt.setApplicationProtocols(engine, new String[] {"h2"});
74             }
75             return engine;
76         }
77 
78         @Override
dispose(SSLEngine engine)79         public void dispose(SSLEngine engine) {
80             engine.closeOutbound();
81         }
82     };
83 
84     @Override
dispose(SSLEngine engine)85     public void dispose(SSLEngine engine) {}
86 
newConscryptClientContext()87     private static SSLContext newConscryptClientContext() {
88         return TestUtils.newClientSslContext(TestUtils.getConscryptProvider());
89     }
90 
newConscryptServerContext()91     private static SSLContext newConscryptServerContext() {
92         return TestUtils.newServerSslContext(TestUtils.getConscryptProvider());
93     }
94 
initEngine(SSLEngine engine, String cipher, boolean client)95     static SSLEngine initEngine(SSLEngine engine, String cipher, boolean client) {
96         engine.setEnabledProtocols(new String[]{"TLSv1.2"});
97         engine.setEnabledCipherSuites(new String[] {cipher});
98         engine.setUseClientMode(client);
99         return engine;
100     }
101 }
102