Changeset 7206 in josm


Ignore:
Timestamp:
2014-06-01T17:55:24+02:00 (10 years ago)
Author:
Don-vip
Message:

see #10033 - allow remote control to work from osm.org in https on Windows systems by adding updated JOSM localhost certificate to Windows Root Certificates keystore

Location:
trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/io/remotecontrol/RemoteControlHttpsServer.java

    r7037 r7206  
    1212import java.net.Socket;
    1313import java.net.SocketException;
     14import java.security.Key;
    1415import java.security.KeyManagementException;
    1516import java.security.KeyStore;
    1617import java.security.KeyStoreException;
    1718import java.security.NoSuchAlgorithmException;
    18 import java.security.UnrecoverableKeyException;
     19import java.security.PrivateKey;
     20import java.security.UnrecoverableEntryException;
     21import java.security.cert.Certificate;
    1922import java.security.cert.CertificateException;
    2023import java.util.Arrays;
     
    3235/**
    3336 * Simple HTTPS server that spawns a {@link RequestProcessor} for every secure connection.
    34  * 
     37 *
    3538 * @since 6941
    3639 */
     
    4245    private static RemoteControlHttpsServer instance;
    4346    private boolean initOK = false;
    44     private SSLContext sslContext; 
     47    private SSLContext sslContext;
    4548
    4649    private static final String KEYSTORE_PATH = "/data/josm.keystore";
     
    5356                KeyStore ks = KeyStore.getInstance("JKS");
    5457                char[] password = KEYSTORE_PASSWORD.toCharArray();
    55                
    56                 // Load keystore
     58
     59                // Load keystore generated with Java 7 keytool as follows:
     60                // keytool -genkeypair -storepass josm_ssl -keypass josm_ssl -alias josm_localhost -dname "CN=localhost, OU=JOSM, O=OpenStreetMap"
     61                // -ext san=ip:127.0.0.1 -keyalg RSA -validity 1825
    5762                try (InputStream in = RemoteControlHttpsServer.class.getResourceAsStream(KEYSTORE_PATH)) {
    5863                    if (in == null) {
     
    6065                    } else {
    6166                        ks.load(in, password);
    62                        
     67
    6368                        if (Main.isDebugEnabled()) {
    6469                            for (Enumeration<String> aliases = ks.aliases(); aliases.hasMoreElements();) {
     
    6671                            }
    6772                        }
    68    
     73
    6974                        KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
    7075                        kmf.init(ks, password);
    71                        
     76
    7277                        TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
    7378                        tmf.init(ks);
    74                        
     79
    7580                        sslContext = SSLContext.getInstance("TLS");
    7681                        sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
    77                        
     82
    7883                        if (Main.isDebugEnabled()) {
    7984                            Main.debug("SSL Context protocol: " + sslContext.getProtocol());
    8085                            Main.debug("SSL Context provider: " + sslContext.getProvider());
    8186                        }
    82                        
     87
     88                        Enumeration<String> aliases = ks.aliases();
     89                        if (aliases.hasMoreElements()) {
     90                            String aliasKey = aliases.nextElement();
     91                            Key key = ks.getKey(aliasKey, password);
     92                            Certificate[] chain = ks.getCertificateChain(aliasKey);
     93                            Main.platform.setupHttpsCertificate(new KeyStore.PrivateKeyEntry((PrivateKey) key, chain));
     94                        }
     95
    8396                        initOK = true;
    8497                    }
    8598                }
    86             } catch (KeyStoreException | NoSuchAlgorithmException | CertificateException | 
    87                     IOException | UnrecoverableKeyException | KeyManagementException e) {
     99            } catch (KeyStoreException | NoSuchAlgorithmException | CertificateException |
     100                    IOException | KeyManagementException | UnrecoverableEntryException e) {
    88101                Main.error(e);
    89102            }
     
    136149        super("RemoteControl HTTPS Server");
    137150        this.setDaemon(true);
    138        
     151
    139152        initialize();
    140        
     153
    141154        // Create SSL Server factory
    142155        SSLServerSocketFactory factory = sslContext.getServerSocketFactory();
     
    144157            Main.debug("SSL factory - Supported Cipher suites: "+Arrays.toString(factory.getSupportedCipherSuites()));
    145158        }
    146        
     159
    147160        // Start the server socket with only 1 connection.
    148161        // Also make sure we only listen
     
    151164        this.server = factory.createServerSocket(port, 1,
    152165            InetAddress.getByName(Main.pref.get("remote.control.host", "localhost")));
    153        
     166
    154167        if (Main.isDebugEnabled() && server instanceof SSLServerSocket) {
    155168            SSLServerSocket sslServer = (SSLServerSocket) server;
  • trunk/src/org/openstreetmap/josm/tools/PlatformHook.java

    r6830 r7206  
    44import java.io.File;
    55import java.io.IOException;
     6import java.security.KeyStore;
     7import java.security.KeyStoreException;
     8import java.security.NoSuchAlgorithmException;
     9import java.security.cert.CertificateException;
    610
    711/**
     
    102106     */
    103107    public String getOSDescription();
     108
     109    /**
     110     * Setup system keystore to add JOSM HTTPS certificate (for remote control).
     111     * @param privateKeyEntry the JOSM certificate for localhost and associated private key
     112     * @throws KeyStoreException in case of error
     113     * @throws IOException in case of error
     114     * @throws CertificateException in case of error
     115     * @throws NoSuchAlgorithmException in case of error
     116     * @since 7206
     117     */
     118    public void setupHttpsCertificate(KeyStore.PrivateKeyEntry privateKeyEntry)
     119            throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException;
    104120}
  • trunk/src/org/openstreetmap/josm/tools/PlatformHookUnixoid.java

    r7082 r7206  
    1616import java.net.URISyntaxException;
    1717import java.nio.charset.StandardCharsets;
     18import java.security.KeyStore;
     19import java.security.KeyStoreException;
     20import java.security.NoSuchAlgorithmException;
     21import java.security.cert.CertificateException;
    1822import java.util.Arrays;
    1923
     
    2529
    2630/**
    27  * see PlatformHook.java
    28  *
    29  * BTW: THIS IS A STUB. See comments below for details.
     31 * {@code PlatformHook} base implementation.
    3032 *
    3133 * Don't write (Main.platform instanceof PlatformHookUnixoid) because other platform
     
    364366        });
    365367    }
     368
     369    @Override
     370    public void setupHttpsCertificate(KeyStore.PrivateKeyEntry privateKeyEntry)
     371            throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException {
     372        // TODO setup HTTPS certificate on Unix systems
     373    }
    366374}
  • trunk/src/org/openstreetmap/josm/tools/PlatformHookWindows.java

    r7001 r7206  
    3030import java.io.File;
    3131import java.io.IOException;
     32import java.security.KeyStore;
     33import java.security.KeyStoreException;
     34import java.security.NoSuchAlgorithmException;
     35import java.security.cert.Certificate;
     36import java.security.cert.CertificateException;
     37import java.util.Enumeration;
     38
     39import org.openstreetmap.josm.Main;
    3240
    3341/**
     
    129137                ((System.getenv("ProgramFiles(x86)") == null) ? "32" : "64") + "-Bit";
    130138    }
     139
     140    @Override
     141    public void setupHttpsCertificate(KeyStore.PrivateKeyEntry privateKeyEntry)
     142            throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException {
     143        KeyStore ks = KeyStore.getInstance("Windows-ROOT");
     144        ks.load(null, null);
     145        Enumeration<String> en = ks.aliases();
     146        while (en.hasMoreElements()) {
     147            String alias = en.nextElement();
     148            Certificate c = ks.getCertificate(alias);
     149            if (ks.isKeyEntry(alias) && c.equals(privateKeyEntry.getCertificate())) {
     150                // JOSM certificate found, return
     151                return;
     152            }
     153        }
     154        // JOSM certificate not found, install it
     155        Main.info("Adding JOSM localhost certificate to Windows-ROOT keystore");
     156        ks.setEntry("josm_localhost", privateKeyEntry, new KeyStore.PasswordProtection("josm_ssl".toCharArray()));
     157    }
    131158}
Note: See TracChangeset for help on using the changeset viewer.