|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +package com.microsoft.aad.msal4j; |
| 5 | + |
| 6 | +import javax.net.ssl.HostnameVerifier; |
| 7 | +import javax.net.ssl.HttpsURLConnection; |
| 8 | +import javax.net.ssl.SSLSocketFactory; |
| 9 | +import java.io.IOException; |
| 10 | +import java.net.HttpURLConnection; |
| 11 | +import java.net.Proxy; |
| 12 | +import java.net.URL; |
| 13 | +import java.net.URLConnection; |
| 14 | +import javax.net.ssl.SSLContext; |
| 15 | +import javax.net.ssl.SSLSession; |
| 16 | +import javax.net.ssl.TrustManager; |
| 17 | +import javax.net.ssl.X509TrustManager; |
| 18 | +import java.security.KeyManagementException; |
| 19 | +import java.security.MessageDigest; |
| 20 | +import java.security.NoSuchAlgorithmException; |
| 21 | +import java.security.cert.Certificate; |
| 22 | +import java.security.cert.CertificateEncodingException; |
| 23 | +import java.security.cert.CertificateException; |
| 24 | +import java.security.cert.X509Certificate; |
| 25 | + |
| 26 | +/** An extension for the default HttpClient which is meant to perform any extra HTTP behavior needed for a managed identity flow. |
| 27 | + * <p> |
| 28 | + * Currently the only extra behavior is the Service Fabric flow, where we must add a certificate thumbprint to the HTTP connection. |
| 29 | + */ |
| 30 | +class DefaultHttpClientManagedIdentity extends DefaultHttpClient { |
| 31 | + |
| 32 | + public static final HostnameVerifier ALL_HOSTS_ACCEPT_HOSTNAME_VERIFIER; |
| 33 | + |
| 34 | + static { |
| 35 | + ALL_HOSTS_ACCEPT_HOSTNAME_VERIFIER = new HostnameVerifier() { |
| 36 | + @SuppressWarnings("BadHostnameVerifier") |
| 37 | + @Override |
| 38 | + public boolean verify(String hostname, SSLSession session) { |
| 39 | + return true; |
| 40 | + } |
| 41 | + }; |
| 42 | + } |
| 43 | + |
| 44 | + DefaultHttpClientManagedIdentity(Proxy proxy, SSLSocketFactory sslSocketFactory, Integer connectTimeout, Integer readTimeout) { |
| 45 | + super(proxy, sslSocketFactory, connectTimeout, readTimeout); |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + HttpURLConnection openConnection(final URL finalURL) |
| 50 | + throws IOException { |
| 51 | + URLConnection connection; |
| 52 | + |
| 53 | + if (proxy != null) { |
| 54 | + connection = finalURL.openConnection(proxy); |
| 55 | + } else { |
| 56 | + connection = finalURL.openConnection(); |
| 57 | + } |
| 58 | + |
| 59 | + connection.setConnectTimeout(connectTimeout); |
| 60 | + connection.setReadTimeout(readTimeout); |
| 61 | + |
| 62 | + if (connection instanceof HttpURLConnection) { |
| 63 | + return (HttpURLConnection) connection; |
| 64 | + } else { |
| 65 | + HttpsURLConnection httpsConnection = (HttpsURLConnection) connection; |
| 66 | + |
| 67 | + if (sslSocketFactory != null) { |
| 68 | + httpsConnection.setSSLSocketFactory(sslSocketFactory); |
| 69 | + } |
| 70 | + |
| 71 | + if (System.getenv(Constants.IDENTITY_SERVER_THUMBPRINT) != null) { |
| 72 | + addTrustedCertificateThumbprint(httpsConnection, System.getenv(Constants.IDENTITY_SERVER_THUMBPRINT)); |
| 73 | + } |
| 74 | + |
| 75 | + return httpsConnection; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * |
| 81 | + * Pins the specified HTTPS URL Connection to work against a specific server-side certificate with |
| 82 | + * the specified thumbprint only. |
| 83 | + * |
| 84 | + * @param httpsUrlConnection The https url connection to configure |
| 85 | + * @param certificateThumbprint The thumbprint of the certificate |
| 86 | + */ |
| 87 | + public static void addTrustedCertificateThumbprint(HttpsURLConnection httpsUrlConnection, |
| 88 | + String certificateThumbprint) { |
| 89 | + //We expect the connection to work against a specific server side certificate only, so it's safe to disable the |
| 90 | + // host name verification. |
| 91 | + if (httpsUrlConnection.getHostnameVerifier() != ALL_HOSTS_ACCEPT_HOSTNAME_VERIFIER) { |
| 92 | + httpsUrlConnection.setHostnameVerifier(ALL_HOSTS_ACCEPT_HOSTNAME_VERIFIER); |
| 93 | + } |
| 94 | + |
| 95 | + // Create a Trust manager that trusts only certificate with specified thumbprint. |
| 96 | + TrustManager[] certificateTrust = new TrustManager[]{new X509TrustManager() { |
| 97 | + public X509Certificate[] getAcceptedIssuers() { |
| 98 | + return new X509Certificate[]{}; |
| 99 | + } |
| 100 | + |
| 101 | + public void checkClientTrusted(X509Certificate[] certificates, String authenticationType) |
| 102 | + throws CertificateException { |
| 103 | + throw new CertificateException("No client side certificate configured."); |
| 104 | + } |
| 105 | + |
| 106 | + public void checkServerTrusted(X509Certificate[] certificates, String authenticationType) |
| 107 | + throws CertificateException { |
| 108 | + if (certificates == null || certificates.length == 0) { |
| 109 | + throw new CertificateException("Did not receive any certificate from the server."); |
| 110 | + } |
| 111 | + |
| 112 | + for (X509Certificate x509Certificate : certificates) { |
| 113 | + String sslCertificateThumbprint = extractCertificateThumbprint(x509Certificate); |
| 114 | + if (certificateThumbprint.equalsIgnoreCase(sslCertificateThumbprint)) { |
| 115 | + return; |
| 116 | + } |
| 117 | + } |
| 118 | + throw new RuntimeException("Thumbprint of certificates received did not match the expected thumbprint."); |
| 119 | + } |
| 120 | + } |
| 121 | + }; |
| 122 | + |
| 123 | + SSLSocketFactory sslSocketFactory; |
| 124 | + try { |
| 125 | + SSLContext sslContext = SSLContext.getInstance("TLS"); |
| 126 | + sslContext.init(null, certificateTrust, null); |
| 127 | + sslSocketFactory = sslContext.getSocketFactory(); |
| 128 | + } catch (NoSuchAlgorithmException | KeyManagementException e) { |
| 129 | + throw new RuntimeException("Error Creating SSL Context", e); |
| 130 | + } |
| 131 | + |
| 132 | + // Pin the connection to a specific certificate with specified thumbprint. |
| 133 | + if (httpsUrlConnection.getSSLSocketFactory() != sslSocketFactory) { |
| 134 | + httpsUrlConnection.setSSLSocketFactory(sslSocketFactory); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + private static String extractCertificateThumbprint(Certificate certificate) { |
| 139 | + try { |
| 140 | + StringBuilder thumbprint = new StringBuilder(); |
| 141 | + MessageDigest messageDigest = MessageDigest.getInstance("SHA-1"); |
| 142 | + |
| 143 | + byte[] encodedCertificate; |
| 144 | + |
| 145 | + try { |
| 146 | + encodedCertificate = certificate.getEncoded(); |
| 147 | + } catch (CertificateEncodingException e) { |
| 148 | + throw new RuntimeException(e); |
| 149 | + } |
| 150 | + |
| 151 | + byte[] updatedDigest = messageDigest.digest(encodedCertificate); |
| 152 | + |
| 153 | + for (byte b : updatedDigest) { |
| 154 | + int unsignedByte = b & 0xff; |
| 155 | + |
| 156 | + if (unsignedByte < 16) { |
| 157 | + thumbprint.append("0"); |
| 158 | + } |
| 159 | + thumbprint.append(Integer.toHexString(unsignedByte)); |
| 160 | + } |
| 161 | + return thumbprint.toString(); |
| 162 | + } catch (NoSuchAlgorithmException e) { |
| 163 | + throw new MsalClientException("NoSuchAlgorithmException when extracting certificate thumbprint: ", e.getMessage()); |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | +} |
0 commit comments