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.GeneralSecurityException;
|
---|
14 | import java.security.InvalidAlgorithmParameterException;
|
---|
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.CertificateEncodingException;
|
---|
20 | import java.security.cert.CertificateException;
|
---|
21 | import java.security.cert.CertificateFactory;
|
---|
22 | import java.security.cert.PKIXParameters;
|
---|
23 | import java.security.cert.TrustAnchor;
|
---|
24 | import java.security.cert.X509Certificate;
|
---|
25 | import java.util.Objects;
|
---|
26 |
|
---|
27 | import javax.net.ssl.SSLContext;
|
---|
28 | import javax.net.ssl.TrustManagerFactory;
|
---|
29 |
|
---|
30 | import org.openstreetmap.josm.Main;
|
---|
31 | import org.openstreetmap.josm.tools.Utils;
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * Class to add missing root certificates to the list of trusted certificates
|
---|
35 | * for TLS connections.
|
---|
36 | *
|
---|
37 | * The added certificates are deemed trustworthy by the main web browsers and
|
---|
38 | * operating systems, but not included in some distributions of Java.
|
---|
39 | *
|
---|
40 | * The certificates are added in-memory at each start, nothing is written to disk.
|
---|
41 | * @since 9995
|
---|
42 | */
|
---|
43 | public final class CertificateAmendment {
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * A certificate amendment.
|
---|
47 | * @since 11943
|
---|
48 | */
|
---|
49 | public static class CertAmend {
|
---|
50 | private final String id;
|
---|
51 | private final String filename;
|
---|
52 | private final String sha256;
|
---|
53 |
|
---|
54 | CertAmend(String id, String filename, String sha256) {
|
---|
55 | this.id = id;
|
---|
56 | this.filename = filename;
|
---|
57 | this.sha256 = sha256;
|
---|
58 | }
|
---|
59 |
|
---|
60 | /**
|
---|
61 | * Returns the certificate identifier.
|
---|
62 | * @return path for JOSM embedded certificate, alias for platform certificate
|
---|
63 | */
|
---|
64 | public final String getId() {
|
---|
65 | return id;
|
---|
66 | }
|
---|
67 |
|
---|
68 | /**
|
---|
69 | * Returns the certificate filename.
|
---|
70 | * @return filename for both JOSM embedded certificate and platform certificate
|
---|
71 | * @since 12241
|
---|
72 | */
|
---|
73 | public final String getFilename() {
|
---|
74 | return filename;
|
---|
75 | }
|
---|
76 |
|
---|
77 | /**
|
---|
78 | * Returns the SHA-256 hash.
|
---|
79 | * @return the SHA-256 hash, in hexadecimal
|
---|
80 | */
|
---|
81 | public final String getSha256() {
|
---|
82 | return sha256;
|
---|
83 | }
|
---|
84 | }
|
---|
85 |
|
---|
86 | /**
|
---|
87 | * Certificates embedded in JOSM
|
---|
88 | */
|
---|
89 | private static final CertAmend[] CERT_AMEND = {
|
---|
90 | new CertAmend("resource://data/security/DST_Root_CA_X3.pem", "DST_Root_CA_X3.pem",
|
---|
91 | "0687260331a72403d909f105e69bcf0d32e1bd2493ffc6d9206d11bcd6770739")
|
---|
92 | };
|
---|
93 |
|
---|
94 | /**
|
---|
95 | * Certificates looked into platform native keystore and not embedded in JOSM.
|
---|
96 | * Identifiers must match Windows keystore aliases for efficient search.
|
---|
97 | */
|
---|
98 | private static final CertAmend[] PLATFORM_CERT_AMEND = {
|
---|
99 | new CertAmend("Staat der Nederlanden Root CA - G2", "Staat_der_Nederlanden_Root_CA_-_G2.crt",
|
---|
100 | "668c83947da63b724bece1743c31a0e6aed0db8ec5b31be377bb784f91b6716f"),
|
---|
101 | new CertAmend("Government of Netherlands G3", "Staat_der_Nederlanden_Root_CA_-_G3.crt",
|
---|
102 | "3c4fb0b95ab8b30032f432b86f535fe172c185d0fd39865837cf36187fa6f428")
|
---|
103 | };
|
---|
104 |
|
---|
105 | private CertificateAmendment() {
|
---|
106 | // Hide default constructor for utility classes
|
---|
107 | }
|
---|
108 |
|
---|
109 | /**
|
---|
110 | * Add missing root certificates to the list of trusted certificates for TLS connections.
|
---|
111 | * @throws IOException if an I/O error occurs
|
---|
112 | * @throws GeneralSecurityException if a security error occurs
|
---|
113 | */
|
---|
114 | public static void addMissingCertificates() throws IOException, GeneralSecurityException {
|
---|
115 | if (!Main.pref.getBoolean("tls.add-missing-certificates", true))
|
---|
116 | return;
|
---|
117 | KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
|
---|
118 | Path cacertsPath = Paths.get(System.getProperty("java.home"), "lib", "security", "cacerts");
|
---|
119 | try (InputStream is = Files.newInputStream(cacertsPath)) {
|
---|
120 | keyStore.load(is, "changeit".toCharArray());
|
---|
121 | }
|
---|
122 |
|
---|
123 | MessageDigest md = MessageDigest.getInstance("SHA-256");
|
---|
124 | CertificateFactory cf = CertificateFactory.getInstance("X.509");
|
---|
125 | boolean certificateAdded = false;
|
---|
126 | // Add embedded certificates. Exit in case of error
|
---|
127 | for (CertAmend certAmend : CERT_AMEND) {
|
---|
128 | try (CachedFile certCF = new CachedFile(certAmend.id)) {
|
---|
129 | X509Certificate cert = (X509Certificate) cf.generateCertificate(
|
---|
130 | new ByteArrayInputStream(certCF.getByteContent()));
|
---|
131 | if (checkAndAddCertificate(md, cert, certAmend, keyStore)) {
|
---|
132 | certificateAdded = true;
|
---|
133 | }
|
---|
134 | }
|
---|
135 | }
|
---|
136 |
|
---|
137 | try {
|
---|
138 | // Try to add platform certificates. Do not exit in case of error (embedded certificates may be OK)
|
---|
139 | for (CertAmend certAmend : PLATFORM_CERT_AMEND) {
|
---|
140 | X509Certificate cert = Main.platform.getX509Certificate(certAmend);
|
---|
141 | if (checkAndAddCertificate(md, cert, certAmend, keyStore)) {
|
---|
142 | certificateAdded = true;
|
---|
143 | }
|
---|
144 | }
|
---|
145 | } catch (KeyStoreException | NoSuchAlgorithmException | CertificateException | IOException | IllegalStateException e) {
|
---|
146 | Main.error(e);
|
---|
147 | }
|
---|
148 |
|
---|
149 | if (certificateAdded) {
|
---|
150 | TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
|
---|
151 | tmf.init(keyStore);
|
---|
152 | SSLContext sslContext = SSLContext.getInstance("TLS");
|
---|
153 | sslContext.init(null, tmf.getTrustManagers(), null);
|
---|
154 | SSLContext.setDefault(sslContext);
|
---|
155 | }
|
---|
156 | }
|
---|
157 |
|
---|
158 | private static boolean checkAndAddCertificate(MessageDigest md, X509Certificate cert, CertAmend certAmend, KeyStore keyStore)
|
---|
159 | throws CertificateEncodingException, KeyStoreException, InvalidAlgorithmParameterException {
|
---|
160 | if (cert != null) {
|
---|
161 | String sha256 = Utils.toHexString(md.digest(cert.getEncoded()));
|
---|
162 | if (!certAmend.sha256.equals(sha256)) {
|
---|
163 | throw new IllegalStateException(
|
---|
164 | tr("Error adding certificate {0} - certificate fingerprint mismatch. Expected {1}, was {2}",
|
---|
165 | certAmend.id, certAmend.sha256, sha256));
|
---|
166 | }
|
---|
167 | if (certificateIsMissing(keyStore, cert)) {
|
---|
168 | if (Main.isDebugEnabled()) {
|
---|
169 | Main.debug(tr("Adding certificate for TLS connections: {0}", cert.getSubjectX500Principal().getName()));
|
---|
170 | }
|
---|
171 | String alias = "josm:" + new File(certAmend.id).getName();
|
---|
172 | keyStore.setCertificateEntry(alias, cert);
|
---|
173 | return true;
|
---|
174 | }
|
---|
175 | }
|
---|
176 | return false;
|
---|
177 | }
|
---|
178 |
|
---|
179 | /**
|
---|
180 | * Check if the certificate is missing and needs to be added to the keystore.
|
---|
181 | * @param keyStore the keystore
|
---|
182 | * @param crt the certificate
|
---|
183 | * @return true, if the certificate is not contained in the keystore
|
---|
184 | * @throws InvalidAlgorithmParameterException if the keystore does not contain at least one trusted certificate entry
|
---|
185 | * @throws KeyStoreException if the keystore has not been initialized
|
---|
186 | */
|
---|
187 | private static boolean certificateIsMissing(KeyStore keyStore, X509Certificate crt)
|
---|
188 | throws KeyStoreException, InvalidAlgorithmParameterException {
|
---|
189 | PKIXParameters params = new PKIXParameters(keyStore);
|
---|
190 | String id = crt.getSubjectX500Principal().getName();
|
---|
191 | for (TrustAnchor ta : params.getTrustAnchors()) {
|
---|
192 | X509Certificate cert = ta.getTrustedCert();
|
---|
193 | if (Objects.equals(id, cert.getSubjectX500Principal().getName()))
|
---|
194 | return false;
|
---|
195 | }
|
---|
196 | return true;
|
---|
197 | }
|
---|
198 | }
|
---|