Ignore:
Timestamp:
2016-05-17T02:02:30+02:00 (8 years ago)
Author:
Don-vip
Message:

sonar - squid:S00112 - Generic exceptions should never be thrown

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  
    1111import java.nio.file.Path;
    1212import java.nio.file.Paths;
     13import java.security.GeneralSecurityException;
    1314import java.security.InvalidAlgorithmParameterException;
    14 import java.security.KeyManagementException;
    1515import java.security.KeyStore;
    1616import java.security.KeyStoreException;
    1717import java.security.MessageDigest;
    18 import java.security.NoSuchAlgorithmException;
    19 import java.security.cert.CertificateException;
    2018import java.security.cert.CertificateFactory;
    2119import java.security.cert.PKIXParameters;
     
    5957     * Add missing root certificates to the list of trusted certificates for TLS connections.
    6058     * @throws IOException if an I/O error occurs
     59     * @throws GeneralSecurityException if a security error occurs
    6160     */
    62     public static void addMissingCertificates() throws IOException {
     61    public static void addMissingCertificates() throws IOException, GeneralSecurityException {
    6362        if (!Main.pref.getBoolean("tls.add-missing-certificates", true))
    6463            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());
    7165        Path cacertsPath = Paths.get(System.getProperty("java.home"), "lib", "security", "cacerts");
    7266        try (InputStream is = Files.newInputStream(cacertsPath)) {
    7367            keyStore.load(is, "changeit".toCharArray());
    74         } catch (NoSuchAlgorithmException ex) {
    75             throw new RuntimeException(ex);
    76         } catch (CertificateException ex) {
    77             throw new IOException(ex);
    7868        }
    7969
    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");
    8671        boolean certificateAdded = false;
    8772        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);
    9577                MessageDigest md = MessageDigest.getInstance("SHA-256");
    9678                String sha1 = Utils.toHexString(md.digest(cert.getEncoded()));
    9779                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}",
    9982                            CERT_AMEND[i],
    10083                            SHA_HASHES[i],
     
    10285                            ));
    10386                }
    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;
    11294                }
    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;
    12095            }
    12196        }
    12297
    12398        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);
    133104        }
    134105    }
     
    139110     * @param crt the certificate
    140111     * @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
    141114     */
    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);
    151118        String id = crt.getSubjectX500Principal().getName();
    152119        for (TrustAnchor ta : params.getTrustAnchors()) {
  • trunk/src/org/openstreetmap/josm/io/OsmReader.java

    r10223 r10235  
    9595
    9696    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);
    9898    }
    9999
    100100    protected void throwException(String msg) throws XMLStreamException {
    101         throw new OsmParsingException(msg, parser.getLocation());
     101        throw new XmlStreamParsingException(msg, parser.getLocation());
    102102    }
    103103
     
    560560    }
    561561
    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         @Override
    575         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 
    591562    /**
    592563     * Exception thrown after user cancelation.
    593564     */
    594     private static final class OsmParsingCanceledException extends OsmParsingException implements ImportCancelException {
     565    private static final class OsmParsingCanceledException extends XmlStreamParsingException implements ImportCancelException {
    595566        /**
    596567         * Constructs a new {@code OsmParsingCanceledException}.
     
    639610        } catch (IllegalDataException e) {
    640611            throw e;
    641         } catch (OsmParsingException e) {
     612        } catch (XmlStreamParsingException e) {
    642613            throw new IllegalDataException(e.getMessage(), e);
    643614        } catch (XMLStreamException e) {
Note: See TracChangeset for help on using the changeset viewer.