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

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

see #14649 - fix @since

  • Property svn:eol-style set to native
File size: 7.4 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.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 */
43public 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 sha256;
52
53 CertAmend(String path, String sha256) {
54 this.id = path;
55 this.sha256 = sha256;
56 }
57
58 /**
59 * Returns the certificate identifier.
60 * @return path for JOSM embedded certificate, alias for platform certificate
61 */
62 public final String getId() {
63 return id;
64 }
65
66 /**
67 * Returns the SHA-256 hash.
68 * @return the SHA-256 hash, in hexadecimal
69 */
70 public final String getSha256() {
71 return sha256;
72 }
73 }
74
75 /**
76 * Certificates embedded in JOSM
77 */
78 private static final CertAmend[] CERT_AMEND = {
79 new CertAmend("resource://data/security/DST_Root_CA_X3.pem",
80 "0687260331a72403d909f105e69bcf0d32e1bd2493ffc6d9206d11bcd6770739")
81 };
82
83 /**
84 * Certificates looked into platform native keystore and not embedded in JOSM.
85 * Identifiers must match Windows keystore aliases for efficient search.
86 */
87 private static final CertAmend[] PLATFORM_CERT_AMEND = {
88 new CertAmend("Staat der Nederlanden Root CA - G2",
89 "668c83947da63b724bece1743c31a0e6aed0db8ec5b31be377bb784f91b6716f"),
90 new CertAmend("Government of Netherlands G3",
91 "3c4fb0b95ab8b30032f432b86f535fe172c185d0fd39865837cf36187fa6f428")
92 };
93
94 private CertificateAmendment() {
95 // Hide default constructor for utility classes
96 }
97
98 /**
99 * Add missing root certificates to the list of trusted certificates for TLS connections.
100 * @throws IOException if an I/O error occurs
101 * @throws GeneralSecurityException if a security error occurs
102 */
103 public static void addMissingCertificates() throws IOException, GeneralSecurityException {
104 if (!Main.pref.getBoolean("tls.add-missing-certificates", true))
105 return;
106 KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
107 Path cacertsPath = Paths.get(System.getProperty("java.home"), "lib", "security", "cacerts");
108 try (InputStream is = Files.newInputStream(cacertsPath)) {
109 keyStore.load(is, "changeit".toCharArray());
110 }
111
112 MessageDigest md = MessageDigest.getInstance("SHA-256");
113 CertificateFactory cf = CertificateFactory.getInstance("X.509");
114 boolean certificateAdded = false;
115 // Add embedded certificates. Exit in case of error
116 for (CertAmend certAmend : CERT_AMEND) {
117 try (CachedFile certCF = new CachedFile(certAmend.id)) {
118 X509Certificate cert = (X509Certificate) cf.generateCertificate(
119 new ByteArrayInputStream(certCF.getByteContent()));
120 if (checkAndAddCertificate(md, cert, certAmend, keyStore)) {
121 certificateAdded = true;
122 }
123 }
124 }
125
126 try {
127 // Try to add platform certificates. Do not exit in case of error (embedded certificates may be OK)
128 for (CertAmend certAmend : PLATFORM_CERT_AMEND) {
129 X509Certificate cert = Main.platform.getX509Certificate(certAmend);
130 if (checkAndAddCertificate(md, cert, certAmend, keyStore)) {
131 certificateAdded = true;
132 }
133 }
134 } catch (KeyStoreException | NoSuchAlgorithmException | CertificateException | IOException | IllegalStateException e) {
135 Main.error(e);
136 }
137
138 if (certificateAdded) {
139 TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
140 tmf.init(keyStore);
141 SSLContext sslContext = SSLContext.getInstance("TLS");
142 sslContext.init(null, tmf.getTrustManagers(), null);
143 SSLContext.setDefault(sslContext);
144 }
145 }
146
147 private static boolean checkAndAddCertificate(MessageDigest md, X509Certificate cert, CertAmend certAmend, KeyStore keyStore)
148 throws CertificateEncodingException, KeyStoreException, InvalidAlgorithmParameterException {
149 if (cert != null) {
150 String sha256 = Utils.toHexString(md.digest(cert.getEncoded()));
151 if (!certAmend.sha256.equals(sha256)) {
152 throw new IllegalStateException(
153 tr("Error adding certificate {0} - certificate fingerprint mismatch. Expected {1}, was {2}",
154 certAmend.id, certAmend.sha256, sha256));
155 }
156 if (certificateIsMissing(keyStore, cert)) {
157 if (Main.isDebugEnabled()) {
158 Main.debug(tr("Adding certificate for TLS connections: {0}", cert.getSubjectX500Principal().getName()));
159 }
160 String alias = "josm:" + new File(certAmend.id).getName();
161 keyStore.setCertificateEntry(alias, cert);
162 return true;
163 }
164 }
165 return false;
166 }
167
168 /**
169 * Check if the certificate is missing and needs to be added to the keystore.
170 * @param keyStore the keystore
171 * @param crt the certificate
172 * @return true, if the certificate is not contained in the keystore
173 * @throws InvalidAlgorithmParameterException if the keystore does not contain at least one trusted certificate entry
174 * @throws KeyStoreException if the keystore has not been initialized
175 */
176 private static boolean certificateIsMissing(KeyStore keyStore, X509Certificate crt)
177 throws KeyStoreException, InvalidAlgorithmParameterException {
178 PKIXParameters params = new PKIXParameters(keyStore);
179 String id = crt.getSubjectX500Principal().getName();
180 for (TrustAnchor ta : params.getTrustAnchors()) {
181 X509Certificate cert = ta.getTrustedCert();
182 if (Objects.equals(id, cert.getSubjectX500Principal().getName()))
183 return false;
184 }
185 return true;
186 }
187}
Note: See TracBrowser for help on using the repository browser.