Changeset 14431 in josm


Ignore:
Timestamp:
2018-11-19T23:13:30+01:00 (6 years ago)
Author:
Don-vip
Message:

fix #17006 - improve startup time on Windows by reworking certificate fetching (patch by GerdP, modified)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/tools/PlatformHookWindows.java

    r14389 r14431  
    9494 */
    9595public class PlatformHookWindows implements PlatformHook {
     96
     97    /**
     98     * Pattern of Microsoft .NET and Powershell version numbers in registry.
     99     */
     100    private static final Pattern MS_VERSION_PATTERN = Pattern.compile("(\\d+)\\.(\\d+)(\\.\\d+.*)?");
    96101
    97102    /**
     
    468473    public X509Certificate getX509Certificate(NativeCertAmend certAmend)
    469474            throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException {
    470         // Make a web request to target site to force Windows to update if needed its trust root store from its certificate trust list
    471         // A better, but a lot more complex method might be to get certificate list from Windows Registry with PowerShell
    472         // using (Get-ItemProperty -Path 'HKLM:\\SOFTWARE\\Microsoft\\SystemCertificates\\AuthRoot\\AutoUpdate').EncodedCtl)
    473         // then decode it using CertUtil -dump or calling CertCreateCTLContext API using JNI, and finally find and decode the certificate
    474         Logging.trace(webRequest(certAmend.getWebSite()));
    475475        // Get Windows Trust Root Store
    476476        KeyStore ks = getRootKeystore();
    477477        // Search by alias (fast)
    478478        Certificate result = ks.getCertificate(certAmend.getWinAlias());
     479        if (result == null) {
     480            // Make a web request to target site to force Windows to update if needed its trust root store from its certificate trust list
     481            // A better, but a lot more complex method might be to get certificate list from Windows Registry with PowerShell
     482            // using (Get-ItemProperty -Path 'HKLM:\\SOFTWARE\\Microsoft\\SystemCertificates\\AuthRoot\\AutoUpdate').EncodedCtl)
     483            // then decode it using CertUtil -dump or calling CertCreateCTLContext API using JNI, and finally find and decode the certificate
     484            Logging.trace(webRequest(certAmend.getWebSite()));
     485            // Reload Windows Trust Root Store and search again by alias (fast)
     486            ks = getRootKeystore();
     487            result = ks.getCertificate(certAmend.getWinAlias());
     488        }
    479489        if (result instanceof X509Certificate) {
    480490            return (X509Certificate) result;
     
    730740            String version = WinRegistry.readString(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\NET Framework Setup\\NDP\\v4\\Full", "Version");
    731741            if (version != null) {
    732                 Matcher m = Pattern.compile("(\\d+)\\.(\\d+)(\\.\\d+.*)?").matcher(version);
     742                Matcher m = MS_VERSION_PATTERN.matcher(version);
    733743                if (m.matches()) {
    734744                    int maj = Integer.parseInt(m.group(1));
     
    750760    public static int getPowerShellVersion() {
    751761        try {
    752             return Integer.parseInt(Utils.execOutput(Arrays.asList(
    753                     "powershell", "-Command", "$PSVersionTable.PSVersion.Major"), 2, TimeUnit.SECONDS));
    754         } catch (ExecutionException e) {
    755             // PowerShell 2.0 (included in Windows 7) does not even support this
    756             Logging.debug(e);
    757             return -1;
    758         } catch (NumberFormatException | IOException | InterruptedException e) {
     762            String version = WinRegistry.readString(
     763                    HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Powershell\\3\\PowershellEngine", "PowershellVersion");
     764            if (version != null) {
     765                Matcher m = MS_VERSION_PATTERN.matcher(version);
     766                if (m.matches()) {
     767                    return Integer.parseInt(m.group(1));
     768                }
     769            }
     770        } catch (NumberFormatException | IllegalAccessException | InvocationTargetException e) {
    759771            Logging.error(e);
    760             return -1;
    761         }
     772        }
     773        return -1;
    762774    }
    763775
Note: See TracChangeset for help on using the changeset viewer.