source: josm/trunk/src/org/openstreetmap/josm/io/CertificateAmendment.java@ 12620

Last change on this file since 12620 was 12620, checked in by Don-vip, 7 years ago

see #15182 - deprecate all Main logging methods and introduce suitable replacements in Logging for most of them

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