|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch B.V. under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch B.V. licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package co.elastic.clients.transport; |
| 21 | + |
| 22 | +import javax.net.ssl.SSLContext; |
| 23 | +import javax.net.ssl.TrustManagerFactory; |
| 24 | +import javax.net.ssl.X509TrustManager; |
| 25 | +import java.io.File; |
| 26 | +import java.io.FileInputStream; |
| 27 | +import java.io.IOException; |
| 28 | +import java.io.InputStream; |
| 29 | +import java.security.KeyManagementException; |
| 30 | +import java.security.KeyStore; |
| 31 | +import java.security.KeyStoreException; |
| 32 | +import java.security.MessageDigest; |
| 33 | +import java.security.NoSuchAlgorithmException; |
| 34 | +import java.security.cert.Certificate; |
| 35 | +import java.security.cert.CertificateException; |
| 36 | +import java.security.cert.CertificateFactory; |
| 37 | +import java.security.cert.X509Certificate; |
| 38 | +import java.util.Arrays; |
| 39 | + |
| 40 | +public class TransportUtils { |
| 41 | + |
| 42 | + /** |
| 43 | + * Creates an <code>SSLContext</code> from the self-signed <code>http_ca.crt</code> certificate created by Elasticsearch during |
| 44 | + * its first start. |
| 45 | + * |
| 46 | + * @see <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/configuring-stack-security.html">Elasticsearch |
| 47 | + * documentation</a> |
| 48 | + */ |
| 49 | + public static SSLContext sslContextFromHttpCaCrt(File file) throws IOException { |
| 50 | + try(InputStream in = new FileInputStream(file)) { |
| 51 | + return sslContextFromHttpCaCrt(in); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Creates an <code>SSLContext</code> from the self-signed <code>http_ca.crt</code> certificate created by Elasticsearch during |
| 57 | + * its first start. |
| 58 | + * |
| 59 | + * @see <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/configuring-stack-security.html">Elasticsearch |
| 60 | + * documentation</a> |
| 61 | + */ |
| 62 | + public static SSLContext sslContextFromHttpCaCrt(InputStream in) { |
| 63 | + try { |
| 64 | + CertificateFactory cf = CertificateFactory.getInstance("X.509"); |
| 65 | + Certificate certificate = cf.generateCertificate(in); |
| 66 | + |
| 67 | + final KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType()); |
| 68 | + keyStore.load(null, null); |
| 69 | + keyStore.setCertificateEntry("elasticsearch-ca", certificate); |
| 70 | + |
| 71 | + TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); |
| 72 | + tmf.init(keyStore); |
| 73 | + |
| 74 | + SSLContext sslContext = SSLContext.getInstance("TLS"); |
| 75 | + sslContext.init(null, tmf.getTrustManagers(), null); |
| 76 | + return sslContext; |
| 77 | + |
| 78 | + } catch (CertificateException | NoSuchAlgorithmException | KeyManagementException | KeyStoreException | IOException e) { |
| 79 | + throw new RuntimeException(e); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Creates an <code>SSLContext</code> from the SHA-256 fingerprint of self-signed <code>http_ca.crt</code> certificate output by |
| 85 | + * Elasticsearch at startup time. |
| 86 | + * |
| 87 | + * @param fingerPrint the SHA-256 fingerprint. Can be uppercase or lowercase, with or without colons separating bytes |
| 88 | + * @see <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/configuring-stack-security.html">Elasticsearch |
| 89 | + * documentation</a> |
| 90 | + */ |
| 91 | + public static SSLContext sslContextFromCaFingerprint(String fingerPrint) { |
| 92 | + |
| 93 | + fingerPrint = fingerPrint.replace(":", ""); |
| 94 | + int len = fingerPrint.length(); |
| 95 | + byte[] fpBytes = new byte[len / 2]; |
| 96 | + for (int i = 0; i < len; i += 2) { |
| 97 | + fpBytes[i / 2] = (byte) ( |
| 98 | + (Character.digit(fingerPrint.charAt(i), 16) << 4) + |
| 99 | + Character.digit(fingerPrint.charAt(i+1), 16) |
| 100 | + ); |
| 101 | + } |
| 102 | + |
| 103 | + try { |
| 104 | + X509TrustManager tm = new X509TrustManager() { |
| 105 | + @Override |
| 106 | + public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { |
| 107 | + throw new CertificateException("This is a client-side only trust manager"); |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { |
| 112 | + |
| 113 | + // The CA root is the last element of the chain |
| 114 | + X509Certificate anchor = chain[chain.length - 1]; |
| 115 | + |
| 116 | + byte[] bytes; |
| 117 | + try { |
| 118 | + MessageDigest md = MessageDigest.getInstance("SHA-256"); |
| 119 | + md.update(anchor.getEncoded()); |
| 120 | + bytes = md.digest(); |
| 121 | + } catch (NoSuchAlgorithmException e) { |
| 122 | + throw new RuntimeException(e); |
| 123 | + } |
| 124 | + |
| 125 | + if (Arrays.equals(fpBytes, bytes)) { |
| 126 | + return; |
| 127 | + } |
| 128 | + |
| 129 | + throw new CertificateException("Untrusted certificate: " + anchor.getSubjectX500Principal()); |
| 130 | + } |
| 131 | + |
| 132 | + @Override |
| 133 | + public X509Certificate[] getAcceptedIssuers() { |
| 134 | + return new X509Certificate[0]; |
| 135 | + } |
| 136 | + }; |
| 137 | + |
| 138 | + SSLContext sslContext = SSLContext.getInstance("TLS"); |
| 139 | + sslContext.init(null, new X509TrustManager[] { tm }, null); |
| 140 | + return sslContext; |
| 141 | + |
| 142 | + } catch (NoSuchAlgorithmException | KeyManagementException e) { |
| 143 | + // Exceptions that should normally not occur |
| 144 | + throw new RuntimeException(e); |
| 145 | + } |
| 146 | + } |
| 147 | +} |
0 commit comments