| 1 |
/* |
| 2 |
* Copyright 2015-2020 the original author or authors |
| 3 |
* |
| 4 |
* This software is licensed under the Apache License, Version 2.0, |
| 5 |
* the GNU Lesser General Public License version 2 or later ("LGPL") |
| 6 |
* and the WTFPL. |
| 7 |
* You may choose either license to govern your use of this software only |
| 8 |
* upon the condition that you accept all of the terms of either |
| 9 |
* the Apache License 2.0, the LGPL 2.1+ or the WTFPL. |
| 10 |
*/ |
| 11 |
package org.minidns.dane.java7; |
| 12 |
|
| 13 |
import org.minidns.dane.DaneVerifier; |
| 14 |
import org.minidns.dane.X509TrustManagerUtil; |
| 15 |
import org.minidns.dnssec.DnssecClient; |
| 16 |
|
| 17 |
import javax.net.ssl.SSLContext; |
| 18 |
import javax.net.ssl.SSLEngine; |
| 19 |
import javax.net.ssl.TrustManager; |
| 20 |
import javax.net.ssl.X509ExtendedTrustManager; |
| 21 |
import javax.net.ssl.X509TrustManager; |
| 22 |
|
| 23 |
import java.net.Socket; |
| 24 |
import java.security.KeyManagementException; |
| 25 |
import java.security.NoSuchAlgorithmException; |
| 26 |
import java.security.cert.CertificateException; |
| 27 |
import java.security.cert.X509Certificate; |
| 28 |
import java.util.logging.Logger; |
| 29 |
|
| 30 |
public class DaneExtendedTrustManager extends X509ExtendedTrustManager { |
| 31 |
private static final Logger LOGGER = Logger.getLogger(DaneExtendedTrustManager.class.getName()); |
| 32 |
|
| 33 |
private final X509TrustManager base; |
| 34 |
private final DaneVerifier verifier; |
| 35 |
|
| 36 |
public static void inject() { |
| 37 |
inject(new DaneExtendedTrustManager()); |
| 38 |
} |
| 39 |
|
| 40 |
public static void inject(DaneExtendedTrustManager trustManager) { |
| 41 |
try { |
| 42 |
SSLContext sslContext = SSLContext.getInstance("TLS"); |
| 43 |
sslContext.init(null, new TrustManager[] {trustManager}, null); |
| 44 |
SSLContext.setDefault(sslContext); |
| 45 |
} catch (NoSuchAlgorithmException | KeyManagementException e) { |
| 46 |
throw new RuntimeException(e); |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
public DaneExtendedTrustManager() { |
| 51 |
this(X509TrustManagerUtil.getDefault()); |
| 52 |
} |
| 53 |
|
| 54 |
public DaneExtendedTrustManager(DnssecClient client) { |
| 55 |
this(client, X509TrustManagerUtil.getDefault()); |
| 56 |
} |
| 57 |
|
| 58 |
public DaneExtendedTrustManager(X509TrustManager base) { |
| 59 |
this(new DaneVerifier(), base); |
| 60 |
} |
| 61 |
|
| 62 |
public DaneExtendedTrustManager(DnssecClient client, X509TrustManager base) { |
| 63 |
this(new DaneVerifier(client), base); |
| 64 |
} |
| 65 |
|
| 66 |
public DaneExtendedTrustManager(DaneVerifier verifier, X509TrustManager base) { |
| 67 |
this.verifier = verifier; |
| 68 |
this.base = base; |
| 69 |
} |
| 70 |
|
| 71 |
@Override |
| 72 |
public void checkClientTrusted(X509Certificate[] chain, String authType, Socket socket) throws CertificateException { |
| 73 |
if (base == null) { |
| 74 |
LOGGER.warning("DaneExtendedTrustManager invalidly used for client certificate check and no fallback X509TrustManager specified"); |
| 75 |
} else { |
| 76 |
LOGGER.info("DaneExtendedTrustManager invalidly used for client certificate check forwarding request to fallback X509TrustManage"); |
| 77 |
if (base instanceof X509ExtendedTrustManager) { |
| 78 |
((X509ExtendedTrustManager) base).checkClientTrusted(chain, authType, socket); |
| 79 |
} else { |
| 80 |
base.checkClientTrusted(chain, authType); |
| 81 |
} |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
@Override |
| 86 |
public void checkServerTrusted(X509Certificate[] chain, String authType, Socket socket) throws CertificateException { |
| 87 |
if (!verifier.verifyCertificateChain(chain, socket.getInetAddress().getHostName(), socket.getPort())) { |
| 88 |
if (base instanceof X509ExtendedTrustManager) { |
| 89 |
((X509ExtendedTrustManager) base).checkServerTrusted(chain, authType, socket); |
| 90 |
} else { |
| 91 |
base.checkClientTrusted(chain, authType); |
| 92 |
} |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
@Override |
| 97 |
public void checkClientTrusted(X509Certificate[] chain, String authType, SSLEngine engine) throws CertificateException { |
| 98 |
if (base == null) { |
| 99 |
LOGGER.warning("DaneExtendedTrustManager invalidly used for client certificate check and no fallback X509TrustManager specified"); |
| 100 |
} else { |
| 101 |
LOGGER.info("DaneExtendedTrustManager invalidly used for client certificate check, forwarding request to fallback X509TrustManage"); |
| 102 |
if (base instanceof X509ExtendedTrustManager) { |
| 103 |
((X509ExtendedTrustManager) base).checkClientTrusted(chain, authType, engine); |
| 104 |
} else { |
| 105 |
base.checkClientTrusted(chain, authType); |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
@Override |
| 111 |
public void checkServerTrusted(X509Certificate[] chain, String authType, SSLEngine engine) throws CertificateException { |
| 112 |
if (!verifier.verifyCertificateChain(chain, engine.getPeerHost(), engine.getPeerPort())) { |
| 113 |
if (base instanceof X509ExtendedTrustManager) { |
| 114 |
((X509ExtendedTrustManager) base).checkServerTrusted(chain, authType, engine); |
| 115 |
} else { |
| 116 |
base.checkClientTrusted(chain, authType); |
| 117 |
} |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
@Override |
| 122 |
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { |
| 123 |
if (base == null) { |
| 124 |
LOGGER.warning("DaneExtendedTrustManager invalidly used for client certificate check and no fallback X509TrustManager specified"); |
| 125 |
} else { |
| 126 |
LOGGER.info("DaneExtendedTrustManager invalidly used for client certificate check, forwarding request to fallback X509TrustManage"); |
| 127 |
base.checkClientTrusted(chain, authType); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
@Override |
| 132 |
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { |
| 133 |
LOGGER.info("DaneExtendedTrustManager cannot be used without hostname information, forwarding request to fallback X509TrustManage"); |
| 134 |
base.checkServerTrusted(chain, authType); |
| 135 |
} |
| 136 |
|
| 137 |
@Override |
| 138 |
public X509Certificate[] getAcceptedIssuers() { |
| 139 |
return base.getAcceptedIssuers(); |
| 140 |
} |
| 141 |
} |