/src/test/java/com/notnoop/apns/utils/FixedCertificates.java

http://github.com/notnoop/java-apns · Java · 124 lines · 75 code · 19 blank · 30 comment · 3 complexity · 7b18d457e54b6e593fae8ed2f42457f0 MD5 · raw file

  1. /*
  2. * Copyright 2009, Mahmood Ali.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are
  7. * met:
  8. *
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above
  12. * copyright notice, this list of conditions and the following disclaimer
  13. * in the documentation and/or other materials provided with the
  14. * distribution.
  15. * * Neither the name of Mahmood Ali. nor the names of its
  16. * contributors may be used to endorse or promote products derived from
  17. * this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. package com.notnoop.apns.utils;
  32. import com.notnoop.apns.internal.SSLContextBuilder;
  33. import javax.net.ssl.SSLContext;
  34. import javax.net.ssl.X509TrustManager;
  35. import java.io.InputStream;
  36. public class FixedCertificates {
  37. public static final String CLIENT_STORE = "clientStore.p12";
  38. public static final String CLIENT_PASSWORD = "123456";
  39. public static final String CLIENT_MULTI_KEY_STORE = "clientStore.jks";
  40. public static final String CLIENT_MULTI_KEY_PASSWORD = "123456";
  41. public static final String SERVER_STORE = "serverStore.p12";
  42. public static final String SERVER_PASSWORD = "123456";
  43. public static final String SERVER_TRUST_STORE = "serverTrustStore.p12";
  44. public static final String SERVER_TRUST_PASSWORD = "123456";
  45. public static final String LOCALHOST = "localhost";
  46. public static SSLContext serverContext() {
  47. try {
  48. InputStream stream = FixedCertificates.class.getResourceAsStream("/" + SERVER_STORE);
  49. InputStream trustStream = FixedCertificates.class.getResourceAsStream("/" + SERVER_TRUST_STORE);
  50. assert stream != null;
  51. return new SSLContextBuilder()
  52. .withAlgorithm("sunx509")
  53. .withCertificateKeyStore(stream, SERVER_PASSWORD, "PKCS12")
  54. .withTrustKeyStore(trustStream, SERVER_TRUST_PASSWORD, "PKCS12")
  55. .build();
  56. } catch (Exception e) {
  57. throw new RuntimeException(e);
  58. }
  59. }
  60. public static SSLContext clientContext() {
  61. try {
  62. InputStream stream = FixedCertificates.class.getResourceAsStream("/" + CLIENT_STORE);
  63. assert stream != null;
  64. return new SSLContextBuilder()
  65. .withAlgorithm("sunx509")
  66. .withCertificateKeyStore(stream, CLIENT_PASSWORD, "PKCS12")
  67. .withTrustManager(new X509TrustManagerTrustAll())
  68. .build();
  69. } catch (Exception e) {
  70. throw new RuntimeException(e);
  71. }
  72. }
  73. public static SSLContext clientMultiKeyContext(String keyAlias) {
  74. try {
  75. InputStream stream = FixedCertificates.class.getResourceAsStream("/" + CLIENT_MULTI_KEY_STORE);
  76. assert stream != null;
  77. return new SSLContextBuilder()
  78. .withAlgorithm("sunx509")
  79. .withCertificateKeyStore(stream, CLIENT_MULTI_KEY_PASSWORD, "JKS", keyAlias)
  80. .withTrustManager(new X509TrustManagerTrustAll())
  81. .build();
  82. } catch (Exception e) {
  83. throw new RuntimeException(e);
  84. }
  85. }
  86. public static String clientCertPath() {
  87. return ClassLoader.getSystemResource(CLIENT_STORE).getPath();
  88. }
  89. static class X509TrustManagerTrustAll implements X509TrustManager {
  90. public boolean checkClientTrusted(java.security.cert.X509Certificate[] chain){
  91. return true;
  92. }
  93. public boolean isServerTrusted(java.security.cert.X509Certificate[] chain){
  94. return true;
  95. }
  96. public boolean isClientTrusted(java.security.cert.X509Certificate[] chain){
  97. return true;
  98. }
  99. public java.security.cert.X509Certificate[] getAcceptedIssuers() {
  100. return null;
  101. }
  102. public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) {}
  103. public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) {}
  104. }
  105. }