OSDN Git Service

Merge change 9368
[android-x86/dalvik.git] / libcore / x-net / src / test / java / tests / api / javax / net / ssl / SSLServerSocketTest.java
1 /*
2  * Copyright (C) 2007 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 tests.api.javax.net.ssl;
18
19 import dalvik.annotation.KnownFailure;
20 import dalvik.annotation.TestLevel;
21 import dalvik.annotation.TestTargetClass;
22 import dalvik.annotation.TestTargetNew;
23 import dalvik.annotation.TestTargets;
24
25 import junit.framework.TestCase;
26
27 import org.apache.harmony.luni.util.Base64;
28
29 import tests.support.Support_PortManager;
30
31 import java.io.ByteArrayInputStream;
32 import java.io.IOException;
33 import java.io.InputStream;
34 import java.net.InetAddress;
35 import java.security.KeyStore;
36
37 import javax.net.ssl.KeyManager;
38 import javax.net.ssl.KeyManagerFactory;
39 import javax.net.ssl.SSLContext;
40 import javax.net.ssl.SSLServerSocket;
41
42 @TestTargetClass(SSLServerSocket.class) 
43 public class SSLServerSocketTest extends TestCase {
44
45     // set to true if on Android, false if on RI
46     boolean useBKS = true;
47
48     /**
49      * Additional class for SSLServerSocket constructor verification
50      */
51     class mySSLServerSocket extends SSLServerSocket {
52
53         public mySSLServerSocket() throws IOException{
54             super();
55         }
56
57         public mySSLServerSocket(int port) throws IOException{
58             super(port);
59         }
60
61         public mySSLServerSocket(int port, int backlog) throws IOException{
62             super(port, backlog);
63         }
64
65         public mySSLServerSocket(int port, int backlog, InetAddress address) throws IOException{
66             super(port, backlog, address);
67         }
68
69         public String[] getSupportedCipherSuites() {
70             return null;
71         }
72
73         public void setEnabledCipherSuites(String[] suites) {
74
75         }
76
77         public String[] getEnabledCipherSuites() {
78             return null;
79         }
80
81         public String[] getSupportedProtocols() {
82             return null;
83         }
84
85         public String[] getEnabledProtocols() {
86             return null;
87         }
88
89         public void setEnabledProtocols(String[] protocols) {
90
91         }
92         
93         public void setEnableSessionCreation(boolean flag) {
94
95         }
96
97         public boolean getEnableSessionCreation() {
98             return false;
99         }
100         
101         public void setNeedClientAuth(boolean need) {
102
103         }
104
105         public boolean getNeedClientAuth() {
106             return false;
107         }
108         
109         public boolean getUseClientMode() {
110             return false;
111         }
112
113         public void setUseClientMode(boolean mode) {
114
115         }
116         
117         public boolean getWantClientAuth() {
118             return false;
119         }
120         public void setWantClientAuth(boolean mode) {
121
122         }
123     }
124     
125     /**
126      * @tests javax.net.ssl.SSLServerSocket#SSLServerSocket() 
127      */
128     @TestTargetNew(
129         level = TestLevel.SUFFICIENT,
130         notes = "IOException wasn't implemented",
131         method = "SSLServerSocket",
132         args = {}
133     )
134     public void testConstructor_01() {
135         try {
136             SSLServerSocket ssl = new mySSLServerSocket();
137         } catch (Exception ex) {
138             fail("Unexpected exception was thrown " + ex);
139         }
140     }
141     
142     /**
143      * @tests javax.net.ssl.SSLServerSocket#SSLServerSocket(int port) 
144      */
145     @TestTargetNew(
146         level = TestLevel.COMPLETE,
147         notes = "",
148         method = "SSLServerSocket",
149         args = {int.class}
150     )
151     public void testConstructor_02() {
152         SSLServerSocket ssl;
153         int portNumber = Support_PortManager.getNextPort();
154         int[] port_invalid = {-1, 65536, Integer.MIN_VALUE, Integer.MAX_VALUE};
155         
156         try {
157             ssl = new mySSLServerSocket(portNumber);
158             assertEquals(portNumber, ssl.getLocalPort());
159         } catch (Exception ex) {
160             fail("Unexpected exception was thrown " + ex);
161         }
162         
163         for (int i = 0; i < port_invalid.length; i++) {
164             try {
165                 ssl = new mySSLServerSocket(port_invalid[i]);
166                 fail("IllegalArgumentException should be thrown");
167             } catch (IllegalArgumentException iae) {
168                 //expected
169             } catch (Exception e) {
170                 fail(e + " was thrown instead of IllegalArgumentException");
171             }
172         }
173         
174         try {
175             ssl = new mySSLServerSocket(portNumber);
176             new mySSLServerSocket(portNumber);
177             fail("IOException Expected when opening an already opened port");
178         } catch (IOException ioe) {
179             // expected
180         } catch (Exception ex) {
181             fail("Unexpected exception was thrown " + ex);
182         }
183     }
184     
185     /**
186      * @tests javax.net.ssl.SSLServerSocket#SSLServerSocket(int port, int backlog) 
187      */
188     @TestTargetNew(
189         level = TestLevel.SUFFICIENT,
190         notes = "Invalid values for backlog weren't checked",
191         method = "SSLServerSocket",
192         args = {int.class, int.class}
193     )
194     public void testConstructor_03() {
195         mySSLServerSocket ssl;
196         int portNumber = Support_PortManager.getNextPort();
197         int[] port_invalid = {-1, Integer.MIN_VALUE, Integer.MAX_VALUE};
198         
199         try {
200             ssl = new mySSLServerSocket(portNumber, 1);
201             assertEquals(portNumber, ssl.getLocalPort());
202         } catch (Exception ex) {
203             fail("Unexpected exception was thrown");
204         }
205         
206         for (int i = 0; i < port_invalid.length; i++) {
207             try {
208                 ssl = new mySSLServerSocket(port_invalid[i], 1);
209                 fail("IllegalArgumentException should be thrown");
210             } catch (IllegalArgumentException iae) {
211                 // expected
212             } catch (Exception e) {
213                 fail(e + " was thrown instead of IllegalArgumentException");
214             }
215         }
216         
217         portNumber = Support_PortManager.getNextPort();
218         try {
219             ssl = new mySSLServerSocket(portNumber, 1);
220             new mySSLServerSocket(portNumber, 1);
221             fail("IOException should be thrown");
222         } catch (IOException ioe) {
223         }
224     }
225     
226     /**
227      * @tests javax.net.ssl.SSLServerSocket#SSLServerSocket(int port, int backlog, InetAddress address) 
228      */
229     @TestTargetNew(
230         level = TestLevel.SUFFICIENT,
231         notes = "Invalid values for backlog weren\'t checked",
232         method = "SSLServerSocket",
233         args = {int.class, int.class, InetAddress.class}
234     )
235     public void testConstructor_04() {
236         mySSLServerSocket ssl;
237         InetAddress ia = null;
238         int portNumber = Support_PortManager.getNextPort();
239         int[] port_invalid = {-1, 65536, Integer.MIN_VALUE, Integer.MAX_VALUE};
240         
241         try {
242             ssl = new mySSLServerSocket(portNumber, 0, ia);
243             assertEquals(portNumber, ssl.getLocalPort());
244         } catch (Exception ex) {
245             fail("Unexpected exception was thrown");
246         }
247         
248         portNumber = Support_PortManager.getNextPort();
249         try {
250             ssl = new mySSLServerSocket(portNumber, 0, InetAddress.getLocalHost());
251             assertEquals(portNumber, ssl.getLocalPort());
252         } catch (Exception ex) {
253             fail("Unexpected exception was thrown");
254         }
255        
256         for (int i = 0; i < port_invalid.length; i++) {
257             try {
258                 ssl = new mySSLServerSocket(port_invalid[i], 1, InetAddress.getLocalHost());
259                 fail("IllegalArgumentException should be thrown");
260             } catch (IllegalArgumentException iae) {
261                 // expected
262             } catch (Exception e) {
263                 fail(e + " was thrown instead of IllegalArgumentException");
264             }
265         }
266         
267         portNumber = Support_PortManager.getNextPort();
268         try {
269            ssl = new mySSLServerSocket(portNumber, 0, InetAddress.getLocalHost());
270            new mySSLServerSocket(portNumber, 0, InetAddress.getLocalHost());
271            fail("IOException should be thrown for");
272         } catch (IOException ioe) {
273         }
274     } 
275     
276     /**
277      * @throws Exception 
278      * @tests javax.net.ssl.SSLServerSocket#getSupportedCipherSuites()
279      */
280     @TestTargetNew(
281         level = TestLevel.COMPLETE,
282         notes = "",
283         method = "getSupportedCipherSuites",
284         args = {}
285     )
286     public void test_getSupportedCipherSuites() throws Exception {
287         SSLServerSocket sss = getSSLServerSocket();
288         String[] res = sss.getSupportedCipherSuites();
289         assertNotNull("NULL result", res);
290         assertTrue("no supported cipher suites available.", res.length > 0);
291     }
292
293     /**
294      * @throws IOException 
295      * @tests javax.net.ssl.SSLServerSocket#getEnabledCipherSuites()
296      * @tests javax.net.ssl.SSLServerSocket#setEnabledCipherSuites(String[] suites)
297      */
298     @TestTargets({
299         @TestTargetNew(
300             level = TestLevel.COMPLETE,
301             notes = "",
302             method = "getEnabledCipherSuites",
303             args = {}
304         ),
305         @TestTargetNew(
306             level = TestLevel.COMPLETE,
307             notes = "",
308             method = "setEnabledCipherSuites",
309             args = {String[].class}
310         )
311     }) 
312     public void test_EnabledCipherSuites() throws Exception {
313         SSLServerSocket sss = getSSLServerSocket();
314         try {
315             sss.setEnabledCipherSuites(null);
316         } catch (IllegalArgumentException iae) {
317             //expected
318         }
319         String[] unsupportedCipherSuites = {"unsupported"};
320         try {
321             sss.setEnabledCipherSuites(unsupportedCipherSuites);
322         } catch (IllegalArgumentException iae) {
323             //expected
324         }
325         int count = sss.getSupportedCipherSuites().length;
326         assertTrue("No supported cipher suites", count > 0);
327         sss.setEnabledCipherSuites(sss.getSupportedCipherSuites());
328         String[] res = sss.getEnabledCipherSuites();
329         assertNotNull("NULL result", res);
330         assertTrue("No enabled cipher suites.", res.length == count);
331     }
332     
333     /**
334      * @throws IOException 
335      * @tests javax.net.ssl.SSLServerSocket#getSupportedProtocols()
336      */
337     @TestTargetNew(
338         level = TestLevel.COMPLETE,
339         notes = "",
340         method = "getSupportedProtocols",
341         args = {}
342     )
343     public void test_getSupportedProtocols() throws Exception {
344         SSLServerSocket sss = getSSLServerSocket();
345         String[] res = sss.getSupportedCipherSuites();
346         assertNotNull("NULL result", res);
347         assertTrue("no supported protocols available.", res.length > 0);
348     }
349     
350     /**
351      * @throws IOException 
352      * @tests javax.net.ssl.SSLServerSocket#getEnabledProtocols()
353      * @tests javax.net.ssl.SSLServerSocket#setEnabledProtocols(String[] protocols)
354      */
355     @TestTargets({
356         @TestTargetNew(
357             level = TestLevel.COMPLETE,
358             notes = "",
359             method = "setEnabledProtocols",
360             args = {String[].class}
361         ),
362         @TestTargetNew(
363             level = TestLevel.COMPLETE,
364             notes = "",
365             method = "getEnabledProtocols",
366             args = {}
367         )
368     })
369     @KnownFailure("Wrong check in SSLServerSocket. Has been fixed in Donutburger")
370     public void test_EnabledProtocols() throws Exception {
371         SSLServerSocket sss = getSSLServerSocket();
372         try {
373             sss.setEnabledProtocols(null);
374         } catch (IllegalArgumentException iae) {
375             //expected
376         }
377         String[] unsupportedProtocols = {"unsupported"};
378         try {
379             sss.setEnabledProtocols(unsupportedProtocols);
380         } catch (IllegalArgumentException iae) {
381             //expected
382         }
383         int count = sss.getSupportedProtocols().length;
384         assertTrue("No supported protocols", count > 0);
385         sss.setEnabledProtocols(sss.getSupportedProtocols());
386         String[] res = sss.getEnabledProtocols();
387         assertNotNull("NULL result", res);
388         assertTrue("no enabled protocols.", res.length == count);
389     }
390     
391     /**
392      * @throws IOException 
393      * @tests javax.net.ssl.SSLServerSocket#setEnableSessionCreation(boolean flag) 
394      * @tests javax.net.ssl.SSLServerSocket#getEnableSessionCreation()
395      */
396     @TestTargets({
397         @TestTargetNew(
398             level = TestLevel.COMPLETE,
399             notes = "",
400             method = "getEnableSessionCreation",
401             args = {}
402         ),
403         @TestTargetNew(
404             level = TestLevel.COMPLETE,
405             notes = "",
406             method = "setEnableSessionCreation",
407             args = {boolean.class}
408         )
409     })
410     public void test_EnableSessionCreation() throws Exception {
411         SSLServerSocket sss = getSSLServerSocket();
412         assertTrue(sss.getEnableSessionCreation());
413         sss.setEnableSessionCreation(false);
414         assertFalse(sss.getEnableSessionCreation());
415         sss.setEnableSessionCreation(true);
416         assertTrue(sss.getEnableSessionCreation());
417     }
418     
419     /**
420      * @throws IOException 
421      * @tests javax.net.ssl.SSLServerSocket#setNeedClientAuth(boolean need) 
422      * @tests javax.net.ssl.SSLServerSocket#getNeedClientAuthCreation()
423      */
424     @TestTargets({
425         @TestTargetNew(
426             level = TestLevel.COMPLETE,
427             notes = "",
428             method = "setNeedClientAuth",
429             args = {boolean.class}
430         ),
431         @TestTargetNew(
432             level = TestLevel.COMPLETE,
433             notes = "",
434             method = "getNeedClientAuth",
435             args = {}
436         )
437     })
438     public void test_NeedClientAuth() throws Exception {
439         SSLServerSocket sss = getSSLServerSocket();
440         sss.setNeedClientAuth(true);
441         assertTrue(sss.getNeedClientAuth());
442         sss.setNeedClientAuth(false);
443         assertFalse(sss.getNeedClientAuth());
444     }
445     
446     /**
447      * @throws IOException 
448      * @tests javax.net.ssl.SSLServerSocket#getUseClientMode()
449      * @tests javax.net.ssl.SSLServerSocket#setUseClientMode(boolean mode)
450      */
451     @TestTargets({
452         @TestTargetNew(
453             level = TestLevel.COMPLETE,
454             notes = "",
455             method = "getUseClientMode",
456             args = {}
457         ),
458         @TestTargetNew(
459             level = TestLevel.COMPLETE,
460             notes = "",
461             method = "setUseClientMode",
462             args = {boolean.class}
463         )
464     })
465     public void test_UseClientMode() throws Exception {
466         SSLServerSocket sss = getSSLServerSocket();
467         sss.setUseClientMode(false);
468         assertFalse(sss.getUseClientMode());
469         sss.setUseClientMode(true);
470         assertTrue(sss.getUseClientMode());
471     }
472     
473     /**
474      * @throws IOException 
475      * @tests javax.net.ssl.SSLServerSocket#setWantClientAuth(boolean want) 
476      * @tests javax.net.ssl.SSLServerSocket#getWantClientAuthCreation()
477      */
478     @TestTargets({
479         @TestTargetNew(
480             level = TestLevel.COMPLETE,
481             notes = "",
482             method = "getWantClientAuth",
483             args = {}
484         ),
485         @TestTargetNew(
486             level = TestLevel.COMPLETE,
487             notes = "",
488             method = "setWantClientAuth",
489             args = {boolean.class}
490         )
491     })
492     public void test_WantClientAuth() throws Exception {
493         SSLServerSocket sss = getSSLServerSocket();
494         sss.setWantClientAuth(true);
495         assertTrue(sss.getWantClientAuth());
496         sss.setWantClientAuth(false);
497         assertFalse(sss.getWantClientAuth());
498     }
499
500
501     /** 
502      * Defines the keystore contents for the server, BKS version. Holds just a
503      * single self-generated key. The subject name is "Test Server".
504      */
505     private static final String SERVER_KEYS_BKS = 
506         "AAAAAQAAABQDkebzoP1XwqyWKRCJEpn/t8dqIQAABDkEAAVteWtleQAAARpYl20nAAAAAQAFWC41" +
507         "MDkAAAJNMIICSTCCAbKgAwIBAgIESEfU1jANBgkqhkiG9w0BAQUFADBpMQswCQYDVQQGEwJVUzET" +
508         "MBEGA1UECBMKQ2FsaWZvcm5pYTEMMAoGA1UEBxMDTVRWMQ8wDQYDVQQKEwZHb29nbGUxEDAOBgNV" +
509         "BAsTB0FuZHJvaWQxFDASBgNVBAMTC1Rlc3QgU2VydmVyMB4XDTA4MDYwNTExNTgxNFoXDTA4MDkw" +
510         "MzExNTgxNFowaTELMAkGA1UEBhMCVVMxEzARBgNVBAgTCkNhbGlmb3JuaWExDDAKBgNVBAcTA01U" +
511         "VjEPMA0GA1UEChMGR29vZ2xlMRAwDgYDVQQLEwdBbmRyb2lkMRQwEgYDVQQDEwtUZXN0IFNlcnZl" +
512         "cjCBnzANBgkqhkiG9w0BAQEFAAOBjQAwgYkCgYEA0LIdKaIr9/vsTq8BZlA3R+NFWRaH4lGsTAQy" +
513         "DPMF9ZqEDOaL6DJuu0colSBBBQ85hQTPa9m9nyJoN3pEi1hgamqOvQIWcXBk+SOpUGRZZFXwniJV" +
514         "zDKU5nE9MYgn2B9AoiH3CSuMz6HRqgVaqtppIe1jhukMc/kHVJvlKRNy9XMCAwEAATANBgkqhkiG" +
515         "9w0BAQUFAAOBgQC7yBmJ9O/eWDGtSH9BH0R3dh2NdST3W9hNZ8hIa8U8klhNHbUCSSktZmZkvbPU" +
516         "hse5LI3dh6RyNDuqDrbYwcqzKbFJaq/jX9kCoeb3vgbQElMRX8D2ID1vRjxwlALFISrtaN4VpWzV" +
517         "yeoHPW4xldeZmoVtjn8zXNzQhLuBqX2MmAAAAqwAAAAUvkUScfw9yCSmALruURNmtBai7kQAAAZx" +
518         "4Jmijxs/l8EBaleaUru6EOPioWkUAEVWCxjM/TxbGHOi2VMsQWqRr/DZ3wsDmtQgw3QTrUK666sR" +
519         "MBnbqdnyCyvM1J2V1xxLXPUeRBmR2CXorYGF9Dye7NkgVdfA+9g9L/0Au6Ugn+2Cj5leoIgkgApN" +
520         "vuEcZegFlNOUPVEs3SlBgUF1BY6OBM0UBHTPwGGxFBBcetcuMRbUnu65vyDG0pslT59qpaR0TMVs" +
521         "P+tcheEzhyjbfM32/vwhnL9dBEgM8qMt0sqF6itNOQU/F4WGkK2Cm2v4CYEyKYw325fEhzTXosck" +
522         "MhbqmcyLab8EPceWF3dweoUT76+jEZx8lV2dapR+CmczQI43tV9btsd1xiBbBHAKvymm9Ep9bPzM" +
523         "J0MQi+OtURL9Lxke/70/MRueqbPeUlOaGvANTmXQD2OnW7PISwJ9lpeLfTG0LcqkoqkbtLKQLYHI" +
524         "rQfV5j0j+wmvmpMxzjN3uvNajLa4zQ8l0Eok9SFaRr2RL0gN8Q2JegfOL4pUiHPsh64WWya2NB7f" +
525         "V+1s65eA5ospXYsShRjo046QhGTmymwXXzdzuxu8IlnTEont6P4+J+GsWk6cldGbl20hctuUKzyx" +
526         "OptjEPOKejV60iDCYGmHbCWAzQ8h5MILV82IclzNViZmzAapeeCnexhpXhWTs+xDEYSKEiG/camt" +
527         "bhmZc3BcyVJrW23PktSfpBQ6D8ZxoMfF0L7V2GQMaUg+3r7ucrx82kpqotjv0xHghNIm95aBr1Qw" +
528         "1gaEjsC/0wGmmBDg1dTDH+F1p9TInzr3EFuYD0YiQ7YlAHq3cPuyGoLXJ5dXYuSBfhDXJSeddUkl" +
529         "k1ufZyOOcskeInQge7jzaRfmKg3U94r+spMEvb0AzDQVOKvjjo1ivxMSgFRZaDb/4qw=";
530
531     /** 
532      * Defines the keystore contents for the server, JKS version. Holds just a
533      * single self-generated key. The subject name is "Test Server".
534      */
535     private static final String SERVER_KEYS_JKS = 
536         "/u3+7QAAAAIAAAABAAAAAQAFbXlrZXkAAAEaWFfBeAAAArowggK2MA4GCisGAQQBKgIRAQEFAASC" +
537         "AqI2kp5XjnF8YZkhcF92YsJNQkvsmH7zqMM87j23zSoV4DwyE3XeC/gZWq1ToScIhoqZkzlbWcu4" +
538         "T/Zfc/DrfGk/rKbBL1uWKGZ8fMtlZk8KoAhxZk1JSyJvdkyKxqmzUbxk1OFMlN2VJNu97FPVH+du" +
539         "dvjTvmpdoM81INWBW/1fZJeQeDvn4mMbbe0IxgpiLnI9WSevlaDP/sm1X3iO9yEyzHLL+M5Erspo" +
540         "Cwa558fOu5DdsICMXhvDQxjWFKFhPHnKtGe+VvwkG9/bAaDgx3kfhk0w5zvdnkKb+8Ed9ylNRzdk" +
541         "ocAa/mxlMTOsTvDKXjjsBupNPIIj7OP4GNnZaxkJjSs98pEO67op1GX2qhy6FSOPNuq8k/65HzUc" +
542         "PYn6voEeh6vm02U/sjEnzRevQ2+2wXoAdp0EwtQ/DlMe+NvcwPGWKuMgX4A4L93DZGb04N2VmAU3" +
543         "YLOtZwTO0LbuWrcCM/q99G/7LcczkxIVrO2I/rh8RXVczlf9QzcrFObFv4ATuspWJ8xG7DhsMbnk" +
544         "rT94Pq6TogYeoz8o8ZMykesAqN6mt/9+ToIemmXv+e+KU1hI5oLwWMnUG6dXM6hIvrULY6o+QCPH" +
545         "172YQJMa+68HAeS+itBTAF4Clm/bLn6reHCGGU6vNdwU0lYldpiOj9cB3t+u2UuLo6tiFWjLf5Zs" +
546         "EQJETd4g/EK9nHxJn0GAKrWnTw7pEHQJ08elzUuy04C/jEEG+4QXU1InzS4o/kR0Sqz2WTGDoSoq" +
547         "ewuPRU5bzQs/b9daq3mXrnPtRBL6HfSDAdpTK76iHqLCGdqx3avHjVSBm4zFvEuYBCev+3iKOBmg" +
548         "yh7eQRTjz4UOWfy85omMBr7lK8PtfVBDzOXpasxS0uBgdUyBDX4tO6k9jZ8a1kmQRQAAAAEABVgu" +
549         "NTA5AAACSDCCAkQwggGtAgRIR8SKMA0GCSqGSIb3DQEBBAUAMGkxCzAJBgNVBAYTAlVTMRMwEQYD" +
550         "VQQIEwpDYWxpZm9ybmlhMQwwCgYDVQQHEwNNVFYxDzANBgNVBAoTBkdvb2dsZTEQMA4GA1UECxMH" +
551         "QW5kcm9pZDEUMBIGA1UEAxMLVGVzdCBTZXJ2ZXIwHhcNMDgwNjA1MTA0ODQyWhcNMDgwOTAzMTA0" +
552         "ODQyWjBpMQswCQYDVQQGEwJVUzETMBEGA1UECBMKQ2FsaWZvcm5pYTEMMAoGA1UEBxMDTVRWMQ8w" +
553         "DQYDVQQKEwZHb29nbGUxEDAOBgNVBAsTB0FuZHJvaWQxFDASBgNVBAMTC1Rlc3QgU2VydmVyMIGf" +
554         "MA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCwoC6chqCI84rj1PrXuJgbiit4EV909zR6N0jNlYfg" +
555         "itwB39bP39wH03rFm8T59b3mbSptnGmCIpLZn25KPPFsYD3JJ+wFlmiUdEP9H05flfwtFQJnw9uT" +
556         "3rRIdYVMPcQ3RoZzwAMliGr882I2thIDbA6xjGU/1nRIdvk0LtxH3QIDAQABMA0GCSqGSIb3DQEB" +
557         "BAUAA4GBAJn+6YgUlY18Ie+0+Vt8oEi81DNi/bfPrAUAh63fhhBikx/3R9dl3wh09Z6p7cIdNxjW" +
558         "n2ll+cRW9eqF7z75F0Omm0C7/KAEPjukVbszmzeU5VqzkpSt0j84YWi+TfcHRrfvhLbrlmGITVpY" +
559         "ol5pHLDyqGmDs53pgwipWqsn/nEXEBgj3EoqPeqHbDf7YaP8h/5BSt0=";
560
561     private String PASSWORD = "android";
562
563     /**
564      * Loads a keystore from a base64-encoded String. Returns the KeyManager[]
565      * for the result.
566      */
567     private KeyManager[] getKeyManagers() throws Exception {
568         String keys = (useBKS ? SERVER_KEYS_BKS : SERVER_KEYS_JKS);
569         byte[] bytes = new Base64().decode(keys.getBytes());                    
570         InputStream inputStream = new ByteArrayInputStream(bytes);
571         
572         KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
573         keyStore.load(inputStream, PASSWORD.toCharArray());
574         inputStream.close();
575         
576         String algorithm = KeyManagerFactory.getDefaultAlgorithm();
577         KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(algorithm);
578         keyManagerFactory.init(keyStore, PASSWORD.toCharArray());
579         
580         return keyManagerFactory.getKeyManagers();
581     }
582
583     private SSLServerSocket getSSLServerSocket() throws Exception {
584         SSLContext context = SSLContext.getInstance("TLS");
585         context.init(getKeyManagers(), null, null);
586         SSLServerSocket sss = (SSLServerSocket) context.getServerSocketFactory()
587                 .createServerSocket();
588         return sss;
589     }
590 }