| | 1 | // License: GPL. For details, see LICENSE file. |
| | 2 | package org.openstreetmap.josm.io; |
| | 3 | |
| | 4 | import static org.openstreetmap.josm.tools.I18n.tr; |
| | 5 | |
| | 6 | import java.io.ByteArrayInputStream; |
| | 7 | import java.io.File; |
| | 8 | import java.io.IOException; |
| | 9 | import java.io.InputStream; |
| | 10 | import java.nio.file.Files; |
| | 11 | import java.nio.file.Path; |
| | 12 | import java.nio.file.Paths; |
| | 13 | import java.security.InvalidAlgorithmParameterException; |
| | 14 | import java.security.KeyManagementException; |
| | 15 | import java.security.KeyStore; |
| | 16 | import java.security.KeyStoreException; |
| | 17 | import java.security.MessageDigest; |
| | 18 | import java.security.NoSuchAlgorithmException; |
| | 19 | import java.security.cert.CertificateException; |
| | 20 | import java.security.cert.CertificateFactory; |
| | 21 | import java.security.cert.PKIXParameters; |
| | 22 | import java.security.cert.TrustAnchor; |
| | 23 | import java.security.cert.X509Certificate; |
| | 24 | import java.util.Objects; |
| | 25 | |
| | 26 | import javax.net.ssl.SSLContext; |
| | 27 | import javax.net.ssl.TrustManagerFactory; |
| | 28 | |
| | 29 | import org.openstreetmap.josm.Main; |
| | 30 | import org.openstreetmap.josm.tools.Utils; |
| | 31 | |
| | 32 | /** |
| | 33 | * Class to add missing root certificates to the list of trusted certificates |
| | 34 | * for TLS connections. |
| | 35 | * |
| | 36 | * The added certificates are deemed trustworthy by the main web browsers and |
| | 37 | * operating systems, but not included in some distributions of Java. |
| | 38 | * |
| | 39 | * The certificates are added in-memory at each start, nothing is written to disk. |
| | 40 | */ |
| | 41 | public class CertificateAmendment { |
| | 42 | |
| | 43 | public static final String[] CERT_AMEND = { "resource://data/security/DST_Root_CA_X3.pem" }; |
| | 44 | public static final String[] SHA_HASHES = { "139a5e4a4e0fa505378c72c5f700934ce8333f4e6b1b508886c4b0eb14f4be99" }; |
| | 45 | |
| | 46 | /** |
| | 47 | * Add missing root certificates to the list of trusted certificates for TLS connections. |
| | 48 | * @throws IOException if an I/O error occurs |
| | 49 | */ |
| | 50 | public static void addMissingCertificates() throws IOException { |
| | 51 | KeyStore keyStore; |
| | 52 | try { |
| | 53 | keyStore = KeyStore.getInstance(KeyStore.getDefaultType()); |
| | 54 | } catch (KeyStoreException ex) { |
| | 55 | throw new IOException(ex); |
| | 56 | } |
| | 57 | Path cacertsPath = Paths.get(System.getProperty("java.home"), "lib", "security", "cacerts"); |
| | 58 | try (InputStream is = Files.newInputStream(cacertsPath)) { |
| | 59 | keyStore.load(is, "changeit".toCharArray()); |
| | 60 | } catch (NoSuchAlgorithmException ex) { |
| | 61 | throw new RuntimeException(ex); |
| | 62 | } catch (CertificateException ex) { |
| | 63 | throw new IOException(ex); |
| | 64 | } |
| | 65 | |
| | 66 | CertificateFactory cf; |
| | 67 | try { |
| | 68 | cf = CertificateFactory.getInstance("X.509"); |
| | 69 | } catch (CertificateException ex) { |
| | 70 | throw new RuntimeException(ex); |
| | 71 | } |
| | 72 | boolean certificateAdded = false; |
| | 73 | for (int i = 0; i < CERT_AMEND.length; i++) { |
| | 74 | CachedFile certCF = new CachedFile(CERT_AMEND[i]); |
| | 75 | byte[] certBytes = certCF.getByteContent(); |
| | 76 | MessageDigest md; |
| | 77 | try { |
| | 78 | md = MessageDigest.getInstance("SHA-256"); |
| | 79 | } catch (NoSuchAlgorithmException ex) { |
| | 80 | throw new RuntimeException(ex); |
| | 81 | } |
| | 82 | byte[] sha = md.digest(certBytes); |
| | 83 | if (!SHA_HASHES[i].equals(Utils.toHexString(sha))) |
| | 84 | throw new RuntimeException(tr("certificate hash mismatch")); |
| | 85 | |
| | 86 | ByteArrayInputStream certIS = new ByteArrayInputStream(certBytes); |
| | 87 | X509Certificate cert; |
| | 88 | try { |
| | 89 | cert = (X509Certificate) cf.generateCertificate(certIS); |
| | 90 | } catch (CertificateException ex) { |
| | 91 | throw new IOException(ex); |
| | 92 | } |
| | 93 | if (certificateIsMissing(keyStore, cert)) { |
| | 94 | Main.debug(tr("Adding certificate for TLS connections: {0}", cert.getSubjectX500Principal().getName())); |
| | 95 | String alias = "josm:" + new File(CERT_AMEND[i]).getName(); |
| | 96 | try { |
| | 97 | keyStore.setCertificateEntry(alias, cert); |
| | 98 | } catch (KeyStoreException ex) { |
| | 99 | throw new AssertionError(ex); |
| | 100 | } |
| | 101 | certificateAdded = true; |
| | 102 | } |
| | 103 | } |
| | 104 | |
| | 105 | if (certificateAdded) { |
| | 106 | try { |
| | 107 | TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); |
| | 108 | tmf.init(keyStore); |
| | 109 | SSLContext sslContext = SSLContext.getInstance("TLS"); |
| | 110 | sslContext.init(null, tmf.getTrustManagers(), null); |
| | 111 | SSLContext.setDefault(sslContext); |
| | 112 | } catch (NoSuchAlgorithmException | KeyStoreException | KeyManagementException ex) { |
| | 113 | throw new RuntimeException(ex); |
| | 114 | } |
| | 115 | } |
| | 116 | } |
| | 117 | |
| | 118 | /** |
| | 119 | * Check if the certificate is missing and needs to be added to the keystore. |
| | 120 | * @param keyStore the keystore |
| | 121 | * @param crt the certificate |
| | 122 | * @return true, if the certificate is not contained in the keystore |
| | 123 | */ |
| | 124 | private static boolean certificateIsMissing(KeyStore keyStore, X509Certificate crt) { |
| | 125 | String id = crt.getSubjectX500Principal().getName(); |
| | 126 | PKIXParameters params; |
| | 127 | try { |
| | 128 | params = new PKIXParameters(keyStore); |
| | 129 | } catch (KeyStoreException ex) { |
| | 130 | throw new AssertionError(ex); |
| | 131 | } catch (InvalidAlgorithmParameterException ex) { |
| | 132 | throw new RuntimeException(ex); |
| | 133 | } |
| | 134 | for (TrustAnchor ta : params.getTrustAnchors()) { |
| | 135 | X509Certificate cert = ta.getTrustedCert(); |
| | 136 | if (Objects.equals(id, cert.getSubjectX500Principal().getName())) |
| | 137 | return false; |
| | 138 | } |
| | 139 | return true; |
| | 140 | } |
| | 141 | } |