Changeset 10235 in josm for trunk/src/org/openstreetmap/josm/io
- Timestamp:
- 2016-05-17T02:02:30+02:00 (9 years ago)
- Location:
- trunk/src/org/openstreetmap/josm/io
- Files:
-
- 1 added
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/io/CertificateAmendment.java
r10088 r10235 11 11 import java.nio.file.Path; 12 12 import java.nio.file.Paths; 13 import java.security.GeneralSecurityException; 13 14 import java.security.InvalidAlgorithmParameterException; 14 import java.security.KeyManagementException;15 15 import java.security.KeyStore; 16 16 import java.security.KeyStoreException; 17 17 import java.security.MessageDigest; 18 import java.security.NoSuchAlgorithmException;19 import java.security.cert.CertificateException;20 18 import java.security.cert.CertificateFactory; 21 19 import java.security.cert.PKIXParameters; … … 59 57 * Add missing root certificates to the list of trusted certificates for TLS connections. 60 58 * @throws IOException if an I/O error occurs 59 * @throws GeneralSecurityException if a security error occurs 61 60 */ 62 public static void addMissingCertificates() throws IOException { 61 public static void addMissingCertificates() throws IOException, GeneralSecurityException { 63 62 if (!Main.pref.getBoolean("tls.add-missing-certificates", true)) 64 63 return; 65 KeyStore keyStore; 66 try { 67 keyStore = KeyStore.getInstance(KeyStore.getDefaultType()); 68 } catch (KeyStoreException ex) { 69 throw new IOException(ex); 70 } 64 KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType()); 71 65 Path cacertsPath = Paths.get(System.getProperty("java.home"), "lib", "security", "cacerts"); 72 66 try (InputStream is = Files.newInputStream(cacertsPath)) { 73 67 keyStore.load(is, "changeit".toCharArray()); 74 } catch (NoSuchAlgorithmException ex) {75 throw new RuntimeException(ex);76 } catch (CertificateException ex) {77 throw new IOException(ex);78 68 } 79 69 80 CertificateFactory cf; 81 try { 82 cf = CertificateFactory.getInstance("X.509"); 83 } catch (CertificateException ex) { 84 throw new RuntimeException(ex); 85 } 70 CertificateFactory cf = CertificateFactory.getInstance("X.509"); 86 71 boolean certificateAdded = false; 87 72 for (int i = 0; i < CERT_AMEND.length; i++) { 88 CachedFile certCF = new CachedFile(CERT_AMEND[i]); 89 byte[] certBytes = certCF.getByteContent(); 90 ByteArrayInputStream certIS = new ByteArrayInputStream(certBytes); 91 X509Certificate cert; 92 93 try { 94 cert = (X509Certificate) cf.generateCertificate(certIS); 73 try (CachedFile certCF = new CachedFile(CERT_AMEND[i])) { 74 byte[] certBytes = certCF.getByteContent(); 75 ByteArrayInputStream certIS = new ByteArrayInputStream(certBytes); 76 X509Certificate cert = (X509Certificate) cf.generateCertificate(certIS); 95 77 MessageDigest md = MessageDigest.getInstance("SHA-256"); 96 78 String sha1 = Utils.toHexString(md.digest(cert.getEncoded())); 97 79 if (!SHA_HASHES[i].equals(sha1)) { 98 throw new RuntimeException(tr("Error adding certificate {0} - certificate fingerprint mismatch. Expected {1}, was {2}", 80 throw new IllegalStateException( 81 tr("Error adding certificate {0} - certificate fingerprint mismatch. Expected {1}, was {2}", 99 82 CERT_AMEND[i], 100 83 SHA_HASHES[i], … … 102 85 )); 103 86 } 104 } catch (CertificateException ex) { 105 throw new IOException(ex); 106 } catch (NoSuchAlgorithmException ex) { 107 throw new RuntimeException(ex); 108 } 109 if (certificateIsMissing(keyStore, cert)) { 110 if (Main.isDebugEnabled()) { 111 Main.debug(tr("Adding certificate for TLS connections: {0}", cert.getSubjectX500Principal().getName())); 87 if (certificateIsMissing(keyStore, cert)) { 88 if (Main.isDebugEnabled()) { 89 Main.debug(tr("Adding certificate for TLS connections: {0}", cert.getSubjectX500Principal().getName())); 90 } 91 String alias = "josm:" + new File(CERT_AMEND[i]).getName(); 92 keyStore.setCertificateEntry(alias, cert); 93 certificateAdded = true; 112 94 } 113 String alias = "josm:" + new File(CERT_AMEND[i]).getName();114 try {115 keyStore.setCertificateEntry(alias, cert);116 } catch (KeyStoreException ex) {117 throw new AssertionError(ex);118 }119 certificateAdded = true;120 95 } 121 96 } 122 97 123 98 if (certificateAdded) { 124 try { 125 TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); 126 tmf.init(keyStore); 127 SSLContext sslContext = SSLContext.getInstance("TLS"); 128 sslContext.init(null, tmf.getTrustManagers(), null); 129 SSLContext.setDefault(sslContext); 130 } catch (NoSuchAlgorithmException | KeyStoreException | KeyManagementException ex) { 131 throw new RuntimeException(ex); 132 } 99 TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); 100 tmf.init(keyStore); 101 SSLContext sslContext = SSLContext.getInstance("TLS"); 102 sslContext.init(null, tmf.getTrustManagers(), null); 103 SSLContext.setDefault(sslContext); 133 104 } 134 105 } … … 139 110 * @param crt the certificate 140 111 * @return true, if the certificate is not contained in the keystore 112 * @throws InvalidAlgorithmParameterException if the keystore does not contain at least one trusted certificate entry 113 * @throws KeyStoreException if the keystore has not been initialized 141 114 */ 142 private static boolean certificateIsMissing(KeyStore keyStore, X509Certificate crt) { 143 PKIXParameters params; 144 try { 145 params = new PKIXParameters(keyStore); 146 } catch (KeyStoreException ex) { 147 throw new AssertionError(ex); 148 } catch (InvalidAlgorithmParameterException ex) { 149 throw new RuntimeException(ex); 150 } 115 private static boolean certificateIsMissing(KeyStore keyStore, X509Certificate crt) 116 throws KeyStoreException, InvalidAlgorithmParameterException { 117 PKIXParameters params = new PKIXParameters(keyStore); 151 118 String id = crt.getSubjectX500Principal().getName(); 152 119 for (TrustAnchor ta : params.getTrustAnchors()) { -
trunk/src/org/openstreetmap/josm/io/OsmReader.java
r10223 r10235 95 95 96 96 protected void throwException(String msg, Throwable th) throws XMLStreamException { 97 throw new OsmParsingException(msg, parser.getLocation(), th);97 throw new XmlStreamParsingException(msg, parser.getLocation(), th); 98 98 } 99 99 100 100 protected void throwException(String msg) throws XMLStreamException { 101 throw new OsmParsingException(msg, parser.getLocation());101 throw new XmlStreamParsingException(msg, parser.getLocation()); 102 102 } 103 103 … … 560 560 } 561 561 562 private static class OsmParsingException extends XMLStreamException {563 564 OsmParsingException(String msg, Location location) {565 super(msg); /* cannot use super(msg, location) because it messes with the message preventing localization */566 this.location = location;567 }568 569 OsmParsingException(String msg, Location location, Throwable th) {570 super(msg, th);571 this.location = location;572 }573 574 @Override575 public String getMessage() {576 String msg = super.getMessage();577 if (msg == null) {578 msg = getClass().getName();579 }580 if (getLocation() == null)581 return msg;582 msg += ' ' + tr("(at line {0}, column {1})", getLocation().getLineNumber(), getLocation().getColumnNumber());583 int offset = getLocation().getCharacterOffset();584 if (offset > -1) {585 msg += ". "+ tr("{0} bytes have been read", offset);586 }587 return msg;588 }589 }590 591 562 /** 592 563 * Exception thrown after user cancelation. 593 564 */ 594 private static final class OsmParsingCanceledException extends OsmParsingException implements ImportCancelException {565 private static final class OsmParsingCanceledException extends XmlStreamParsingException implements ImportCancelException { 595 566 /** 596 567 * Constructs a new {@code OsmParsingCanceledException}. … … 639 610 } catch (IllegalDataException e) { 640 611 throw e; 641 } catch ( OsmParsingException e) {612 } catch (XmlStreamParsingException e) { 642 613 throw new IllegalDataException(e.getMessage(), e); 643 614 } catch (XMLStreamException e) {
Note:
See TracChangeset
for help on using the changeset viewer.