|
| 1 | +/* |
| 2 | + * Copyright 2024-2024 the original author or authors. |
| 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 | + * https://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.springframework.grpc.internal; |
| 17 | + |
| 18 | +import java.net.Socket; |
| 19 | +import java.security.InvalidAlgorithmParameterException; |
| 20 | +import java.security.KeyStore; |
| 21 | +import java.security.KeyStoreException; |
| 22 | +import java.security.Provider; |
| 23 | +import java.security.cert.CertificateException; |
| 24 | +import java.security.cert.X509Certificate; |
| 25 | + |
| 26 | +import javax.net.ssl.ManagerFactoryParameters; |
| 27 | +import javax.net.ssl.SSLEngine; |
| 28 | +import javax.net.ssl.TrustManager; |
| 29 | +import javax.net.ssl.TrustManagerFactory; |
| 30 | +import javax.net.ssl.TrustManagerFactorySpi; |
| 31 | +import javax.net.ssl.X509ExtendedTrustManager; |
| 32 | +import javax.net.ssl.X509TrustManager; |
| 33 | + |
| 34 | +/** |
| 35 | + * A custom implementation of the TrustManagerFactory class that provides an insecure |
| 36 | + * trust manager. This trust manager does not perform any certificate validation and |
| 37 | + * accepts all certificates. It is intended for testing or development purposes only and |
| 38 | + * should not be used in production environments. |
| 39 | + */ |
| 40 | +public class InsecureTrustManagerFactory extends TrustManagerFactory { |
| 41 | + |
| 42 | + public static final TrustManagerFactory INSTANCE = new InsecureTrustManagerFactory(); |
| 43 | + |
| 44 | + private static final Provider provider = new Provider("", "0.0", "") { |
| 45 | + private static final long serialVersionUID = -2680540247105807895L; |
| 46 | + |
| 47 | + }; |
| 48 | + |
| 49 | + protected InsecureTrustManagerFactory() { |
| 50 | + super(new SimpleTrustManagerFactorySpi(), provider, ""); |
| 51 | + } |
| 52 | + |
| 53 | + private final static class InsecureTrustManager extends X509ExtendedTrustManager { |
| 54 | + |
| 55 | + static final InsecureTrustManager INSTANCE = new InsecureTrustManager(); |
| 56 | + |
| 57 | + static final X509Certificate[] EMPTY_CERTS = new X509Certificate[] {}; |
| 58 | + |
| 59 | + @Override |
| 60 | + public void checkClientTrusted(X509Certificate[] chain, String authType) { |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public void checkServerTrusted(X509Certificate[] chain, String authType) { |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public X509Certificate[] getAcceptedIssuers() { |
| 69 | + return EMPTY_CERTS; |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public void checkClientTrusted(X509Certificate[] chain, String authType, Socket socket) |
| 74 | + throws CertificateException { |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public void checkServerTrusted(X509Certificate[] chain, String authType, Socket socket) |
| 79 | + throws CertificateException { |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public void checkClientTrusted(X509Certificate[] chain, String authType, SSLEngine engine) |
| 84 | + throws CertificateException { |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public void checkServerTrusted(X509Certificate[] chain, String authType, SSLEngine engine) |
| 89 | + throws CertificateException { |
| 90 | + } |
| 91 | + |
| 92 | + } |
| 93 | + |
| 94 | + private final static class SimpleTrustManagerFactorySpi extends TrustManagerFactorySpi { |
| 95 | + |
| 96 | + static final TrustManager[] TRUST_ALL = new X509TrustManager[] { InsecureTrustManager.INSTANCE }; |
| 97 | + |
| 98 | + @Override |
| 99 | + protected void engineInit(KeyStore keyStore) throws KeyStoreException { |
| 100 | + } |
| 101 | + |
| 102 | + @Override |
| 103 | + protected void engineInit(ManagerFactoryParameters managerFactoryParameters) |
| 104 | + throws InvalidAlgorithmParameterException { |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + protected TrustManager[] engineGetTrustManagers() { |
| 109 | + return TRUST_ALL; |
| 110 | + } |
| 111 | + |
| 112 | + } |
| 113 | + |
| 114 | +} |
0 commit comments