You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

KeyStoreLocator.java 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc.tls;
  18. import java.io.File;
  19. import java.io.FileInputStream;
  20. import java.io.IOException;
  21. import java.security.GeneralSecurityException;
  22. import java.security.KeyStore;
  23. import java.security.KeyStoreException;
  24. import java.security.NoSuchAlgorithmException;
  25. import java.security.NoSuchProviderException;
  26. import java.security.cert.CertificateException;
  27. import javax.annotation.Nullable;
  28. /**
  29. * Locates a platform-appropriate {@link KeyStore} to read trusted root certificates from.
  30. */
  31. public class KeyStoreLocator {
  32. private static final String WINDOWS_PROVIDER = "SunMSCAPI";
  33. private static final String WINDOWS_TYPE = "Windows-ROOT";
  34. private static final String MAC_PROVIDER = "Apple";
  35. private static final String MAC_TYPE = "KeychainStore";
  36. private static final String LINUX_CACERTS_PATH = "/etc/ssl/certs/java/cacerts";
  37. /**
  38. * Attempts to locate an appropriate system-wide source of trusted certificates. If a system
  39. * store cannot be located or read, the bundled Java cacerts list will be use. If that also
  40. * fails, {@code null} will be returned.
  41. *
  42. * @return The best available KeyStore to use, or {@code null} if no working store was found.
  43. */
  44. @Nullable
  45. public KeyStore getKeyStore() {
  46. try {
  47. final String osName = System.getProperty("os.name");
  48. if (osName.startsWith("Mac OS")) {
  49. return getMacKeyStore();
  50. } else if (osName.startsWith("Windows")) {
  51. return getWindowsKeyStore();
  52. } else {
  53. return getLinuxKeyStore();
  54. }
  55. } catch (IOException | GeneralSecurityException ex1) {
  56. // Couldn't load a system KeyStore, try to load the Java one.
  57. try {
  58. return getJavaKeyStore();
  59. } catch (IOException | GeneralSecurityException ex2) {
  60. // Couldn't even load the Java one...
  61. return null;
  62. }
  63. }
  64. }
  65. /**
  66. * Returns the root (system-wide) keystore on Windows operating systems.
  67. */
  68. private KeyStore getWindowsKeyStore() throws NoSuchProviderException, KeyStoreException,
  69. CertificateException, NoSuchAlgorithmException, IOException {
  70. final KeyStore keyStore = KeyStore.getInstance(WINDOWS_TYPE, WINDOWS_PROVIDER);
  71. keyStore.load(null, null);
  72. return keyStore;
  73. }
  74. /**
  75. * Returns the root (system-wide) keystore on Mac operating systems.
  76. */
  77. private KeyStore getMacKeyStore() throws NoSuchProviderException, KeyStoreException,
  78. CertificateException, NoSuchAlgorithmException, IOException {
  79. final KeyStore keyStore = KeyStore.getInstance(MAC_TYPE, MAC_PROVIDER);
  80. keyStore.load(null, null);
  81. return keyStore;
  82. }
  83. /**
  84. * Returns a KeyStore that reads system-wide trusted certificates on Linux. These are found in
  85. * {@code /etc/ssl/certs}, and the Java version is provided by the {@code ca-certificates-java}
  86. * package.
  87. */
  88. private KeyStore getLinuxKeyStore() throws IOException, KeyStoreException,
  89. CertificateException, NoSuchAlgorithmException {
  90. try (FileInputStream is = new FileInputStream(LINUX_CACERTS_PATH)) {
  91. final KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
  92. keystore.load(is, null);
  93. return keystore;
  94. }
  95. }
  96. /**
  97. * Returns a KeyStore that reads from the {@code cacerts} file bundled with the in-use
  98. * Java Runtime Environment. This is less preferable to using a system-provided KeyStore,
  99. * as it will not include any user/admin-defined certificates.
  100. */
  101. private KeyStore getJavaKeyStore() throws IOException, KeyStoreException,
  102. CertificateException, NoSuchAlgorithmException {
  103. final String filename = System.getProperty("java.home")
  104. + File.separatorChar + "lib"
  105. + File.separatorChar + "security"
  106. + File .separatorChar + "cacerts";
  107. try (FileInputStream is = new FileInputStream(filename)) {
  108. final KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
  109. keystore.load(is, null);
  110. return keystore;
  111. }
  112. }
  113. }