|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2024 Red Hat, Inc. |
| 3 | + * Distributed under license by Red Hat, Inc. All rights reserved. |
| 4 | + * This program is made available under the terms of the |
| 5 | + * Eclipse Public License v2.0 which accompanies this distribution, |
| 6 | + * and is available at http://www.eclipse.org/legal/epl-v20.html |
| 7 | + * |
| 8 | + * Contributors: |
| 9 | + * Based on nl.altindag.ssl.trustmanager.CompositeX509ExtendedTrustManager at https://github.com/Hakky54/sslcontext-kickstart |
| 10 | + * Red Hat, Inc. - initial API and implementation |
| 11 | + ******************************************************************************/ |
| 12 | +package com.redhat.devtools.intellij.common.ssl; |
| 13 | + |
| 14 | +import javax.net.ssl.SSLEngine; |
| 15 | +import javax.net.ssl.X509ExtendedTrustManager; |
| 16 | +import javax.net.ssl.X509TrustManager; |
| 17 | +import java.net.Socket; |
| 18 | +import java.security.InvalidAlgorithmParameterException; |
| 19 | +import java.security.cert.CertificateException; |
| 20 | +import java.security.cert.X509Certificate; |
| 21 | +import java.util.ArrayList; |
| 22 | +import java.util.Arrays; |
| 23 | +import java.util.Collections; |
| 24 | +import java.util.List; |
| 25 | +import java.util.Objects; |
| 26 | + |
| 27 | +public class CompositeX509ExtendedTrustManager extends X509ExtendedTrustManager { |
| 28 | + |
| 29 | + private static final String CERTIFICATE_EXCEPTION_MESSAGE = "None of the TrustManagers trust this certificate chain"; |
| 30 | + |
| 31 | + private final List<X509ExtendedTrustManager> innerTrustManagers; |
| 32 | + private final X509Certificate[] acceptedIssuers; |
| 33 | + |
| 34 | + public CompositeX509ExtendedTrustManager(List<X509ExtendedTrustManager> trustManagers) { |
| 35 | + this.innerTrustManagers = Collections.unmodifiableList(trustManagers); |
| 36 | + this.acceptedIssuers = trustManagers.stream() |
| 37 | + .map((manager) -> |
| 38 | + Objects.requireNonNullElseGet(manager.getAcceptedIssuers(), () -> new X509Certificate[]{}) |
| 39 | + ) |
| 40 | + .flatMap(Arrays::stream) |
| 41 | + .toArray(X509Certificate[]::new); |
| 42 | + } |
| 43 | + |
| 44 | + @Override |
| 45 | + public X509Certificate[] getAcceptedIssuers() { |
| 46 | + return Arrays.copyOf(acceptedIssuers, acceptedIssuers.length); |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { |
| 51 | + checkTrusted((trustManager) -> trustManager.checkClientTrusted(chain, authType)); |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { |
| 56 | + checkTrusted((trustManager) -> trustManager.checkServerTrusted(chain, authType)); |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public void checkClientTrusted(X509Certificate[] chain, String authType, Socket socket) throws CertificateException { |
| 61 | + checkTrusted((trustManager) -> trustManager.checkClientTrusted(chain, authType, socket)); |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public void checkServerTrusted(X509Certificate[] chain, String authType, Socket socket) throws CertificateException { |
| 66 | + checkTrusted((trustManager) -> trustManager.checkServerTrusted(chain, authType, socket)); |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public void checkClientTrusted(X509Certificate[] chain, String authType, SSLEngine engine) throws CertificateException { |
| 71 | + checkTrusted((trustManager) -> trustManager.checkClientTrusted(chain, authType, engine)); |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public void checkServerTrusted(X509Certificate[] chain, String authType, SSLEngine engine) throws CertificateException { |
| 76 | + checkTrusted((trustManager) -> trustManager.checkServerTrusted(chain, authType, engine)); |
| 77 | + } |
| 78 | + |
| 79 | + public List<X509ExtendedTrustManager> getInnerTrustManagers() { |
| 80 | + return innerTrustManagers; |
| 81 | + } |
| 82 | + |
| 83 | + private void checkTrusted(TrustManagerConsumer consumer) throws CertificateException { |
| 84 | + List<CertificateException> certificateExceptions = new ArrayList<>(); |
| 85 | + for (X509ExtendedTrustManager trustManager : innerTrustManagers) { |
| 86 | + try { |
| 87 | + consumer.checkTrusted(trustManager); |
| 88 | + return; |
| 89 | + } catch (CertificateException e) { |
| 90 | + certificateExceptions.add(e); |
| 91 | + } catch (RuntimeException e) { |
| 92 | + Throwable cause = e.getCause(); |
| 93 | + if (!(cause instanceof InvalidAlgorithmParameterException)) { |
| 94 | + throw e; |
| 95 | + } |
| 96 | + |
| 97 | + certificateExceptions.add(new CertificateException(cause)); |
| 98 | + } |
| 99 | + } |
| 100 | + CertificateException certificateException = new CertificateException(CERTIFICATE_EXCEPTION_MESSAGE); |
| 101 | + certificateExceptions.forEach(certificateException::addSuppressed); |
| 102 | + throw certificateException; |
| 103 | + } |
| 104 | + |
| 105 | + interface TrustManagerConsumer { |
| 106 | + void checkTrusted(X509ExtendedTrustManager var1) throws CertificateException; |
| 107 | + } |
| 108 | +} |
0 commit comments