PageRenderTime 43ms CodeModel.GetById 16ms app.highlight 21ms RepoModel.GetById 1ms app.codeStats 1ms

/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 */
 31package com.notnoop.apns.utils;
 32
 33import com.notnoop.apns.internal.SSLContextBuilder;
 34
 35import javax.net.ssl.SSLContext;
 36import javax.net.ssl.X509TrustManager;
 37import java.io.InputStream;
 38
 39public class FixedCertificates {
 40
 41    public static final String CLIENT_STORE = "clientStore.p12";
 42    public static final String CLIENT_PASSWORD = "123456";
 43
 44    public static final String CLIENT_MULTI_KEY_STORE = "clientStore.jks";
 45    public static final String CLIENT_MULTI_KEY_PASSWORD = "123456";
 46
 47    public static final String SERVER_STORE = "serverStore.p12";
 48    public static final String SERVER_PASSWORD = "123456";
 49
 50    public static final String SERVER_TRUST_STORE = "serverTrustStore.p12";
 51    public static final String SERVER_TRUST_PASSWORD = "123456";
 52
 53    public static final String LOCALHOST = "localhost";
 54
 55    public static SSLContext serverContext() {
 56        try {
 57            InputStream stream = FixedCertificates.class.getResourceAsStream("/" + SERVER_STORE);
 58            InputStream trustStream = FixedCertificates.class.getResourceAsStream("/" + SERVER_TRUST_STORE);
 59            assert stream != null;
 60            return new SSLContextBuilder()
 61                    .withAlgorithm("sunx509")
 62                    .withCertificateKeyStore(stream, SERVER_PASSWORD, "PKCS12")
 63                    .withTrustKeyStore(trustStream, SERVER_TRUST_PASSWORD, "PKCS12")
 64                    .build();
 65        } catch (Exception e) {
 66            throw new RuntimeException(e);
 67        }
 68    }
 69
 70    public static SSLContext clientContext() {
 71        try {
 72            InputStream stream = FixedCertificates.class.getResourceAsStream("/" + CLIENT_STORE);
 73            assert stream != null;
 74            return new SSLContextBuilder()
 75                    .withAlgorithm("sunx509")
 76                    .withCertificateKeyStore(stream, CLIENT_PASSWORD, "PKCS12")
 77                    .withTrustManager(new X509TrustManagerTrustAll())
 78                    .build();
 79        } catch (Exception e) {
 80            throw new RuntimeException(e);
 81        }
 82    }
 83
 84    public static SSLContext clientMultiKeyContext(String keyAlias) {
 85        try {
 86            InputStream stream = FixedCertificates.class.getResourceAsStream("/" + CLIENT_MULTI_KEY_STORE);
 87            assert stream != null;
 88            return new SSLContextBuilder()
 89                    .withAlgorithm("sunx509")
 90                    .withCertificateKeyStore(stream, CLIENT_MULTI_KEY_PASSWORD, "JKS", keyAlias)
 91                    .withTrustManager(new X509TrustManagerTrustAll())
 92                    .build();
 93        } catch (Exception e) {
 94            throw new RuntimeException(e);
 95        }
 96    }
 97
 98    public static String clientCertPath() {
 99        return ClassLoader.getSystemResource(CLIENT_STORE).getPath();
100    }
101
102    static class X509TrustManagerTrustAll implements X509TrustManager {
103        public boolean checkClientTrusted(java.security.cert.X509Certificate[] chain){
104            return true;
105        }
106
107        public boolean isServerTrusted(java.security.cert.X509Certificate[] chain){
108            return true;
109        }
110
111        public boolean isClientTrusted(java.security.cert.X509Certificate[] chain){
112            return true;
113        }
114
115        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
116            return null;
117        }
118
119        public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) {}
120
121        public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) {}
122    }
123
124}