Changeset 13463 in josm


Ignore:
Timestamp:
2018-02-25T22:41:49+01:00 (4 months ago)
Author:
Don-vip
Message:

see #15992 - make sure .NET framework 4.5 or later is installed, as previous versions do not support TLS 1.2

File:
1 edited

Legend:

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

    r13458 r13463  
    6666import java.util.Properties;
    6767import java.util.concurrent.ExecutionException;
     68import java.util.regex.Matcher;
     69import java.util.regex.Pattern;
    6870
    6971import javax.swing.JOptionPane;
     
    691693
    692694    /**
     695     * Determines if the .NET framework 4.5 (or later) is installed.
     696     * Windows 7 ships by default with an older version.
     697     * @return {@code true} if the .NET framework 4.5 (or later) is installed.
     698     * @since 13463
     699     */
     700    public static boolean isDotNet45Installed() {
     701        try {
     702            // https://docs.microsoft.com/en-us/dotnet/framework/migration-guide/how-to-determine-which-versions-are-installed#net_d
     703            // "The existence of the Release DWORD indicates that the .NET Framework 4.5 or later has been installed"
     704            // Great, but our WinRegistry only handles REG_SZ type, so we have to check the Version key
     705            String version = WinRegistry.readString(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\NET Framework Setup\\NDP\\v4\\Full", "Version");
     706            Matcher m = Pattern.compile("(\\d+)\\.(\\d+)(\\.\\d+.*)?").matcher(version);
     707            if (m.matches()) {
     708                int maj = Integer.valueOf(m.group(1));
     709                int min = Integer.valueOf(m.group(2));
     710                return (maj == 4 && min >= 5) || maj > 4;
     711            }
     712        } catch (IllegalAccessException | InvocationTargetException | NumberFormatException e) {
     713            Logging.error(e);
     714        }
     715        return false;
     716    }
     717
     718    /**
    693719     * Performs a web request using Windows CryptoAPI (through PowerShell).
    694720     * This is useful to ensure Windows trust store will contain a specific root CA.
     
    701727        // With PS 6.0 (not yet released in Windows) we could simply use:
    702728        // Invoke-WebRequest -SSlProtocol Tsl12 $uri
    703         // With PS 3.0 (Windows 8+) we can use (https://stackoverflow.com/a/41618979/2257172):
    704         // [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 ; Invoke-WebRequest $uri
    705         // Unfortunately there are still a lot of users with Windows 7 (PS 2.0) and Invoke-WebRequest is not available:
    706         try {
    707             // https://stackoverflow.com/a/25121601/2257172
    708             return Utils.execOutput(Arrays.asList("powershell", "-Command",
    709                     "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12;"+
    710                     "[System.Net.WebRequest]::Create('"+uri+"').GetResponse()"
    711                     ));
    712         } catch (ExecutionException | InterruptedException e) {
    713             Logging.error(e);
    714             return null;
    715         }
     729        // .NET framework < 4.5 does not support TLS 1.2 (https://stackoverflow.com/a/43240673/2257172)
     730        if (isDotNet45Installed()) {
     731            try {
     732                // The following works with PS 3.0 (Windows 8+), https://stackoverflow.com/a/41618979/2257172
     733                return Utils.execOutput(Arrays.asList("powershell", "-Command",
     734                        "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12;"+
     735                        "[System.Net.WebRequest]::Create('"+uri+"').GetResponse()"
     736                        ));
     737            } catch (ExecutionException | InterruptedException e) {
     738                Logging.error(e);
     739            }
     740        }
     741        return null;
    716742    }
    717743}
Note: See TracChangeset for help on using the changeset viewer.