/platform/libcore/x-net/src/test/java/tests/api/javax/net/ssl/SSLServerSocketTest.java

https://github.com/lems111/Intercept-CM6-Kernel · Java · 588 lines · 435 code · 64 blank · 89 comment · 5 complexity · 9c5cb642181c2cf6184b50c8e586f040 MD5 · raw file

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