|
20 | 20 | import java.security.KeyStoreException; |
21 | 21 | import java.security.NoSuchAlgorithmException; |
22 | 22 | import java.security.UnrecoverableKeyException; |
| 23 | +import java.security.cert.X509Certificate; |
| 24 | +import java.util.ArrayList; |
| 25 | +import java.util.Arrays; |
| 26 | +import java.util.Enumeration; |
| 27 | +import java.util.List; |
23 | 28 | import java.util.Objects; |
24 | 29 | import javax.net.ssl.KeyManager; |
25 | 30 | import javax.net.ssl.KeyManagerFactory; |
@@ -59,6 +64,41 @@ private SslConfiguration( |
59 | 64 | this.verifyHostName = verifyHostName; |
60 | 65 | } |
61 | 66 |
|
| 67 | + /** |
| 68 | + * Creates an identifier based on the contents of the SSL configuration. |
| 69 | + * |
| 70 | + * @return an ID based on the contents of the configuration |
| 71 | + */ |
| 72 | + public String getId() { |
| 73 | + ArrayList<String> hashElements = new ArrayList<>(); |
| 74 | + addElementsForHashingFromKeyStore(keyStoreConfig, hashElements); |
| 75 | + addElementsForHashingFromKeyStore(trustStoreConfig, hashElements); |
| 76 | + return String.valueOf(Arrays.hashCode(hashElements.toArray())); |
| 77 | + } |
| 78 | + |
| 79 | + private void addElementsForHashingFromKeyStore( |
| 80 | + AbstractKeyStoreConfiguration keyStoreConfiguration, ArrayList<String> hashElements) { |
| 81 | + final List<String> aliases = new ArrayList<>(); |
| 82 | + try { |
| 83 | + Enumeration<String> aliasEnumeration = |
| 84 | + keyStoreConfiguration.getKeyStore().aliases(); |
| 85 | + while (aliasEnumeration.hasMoreElements()) { |
| 86 | + aliases.add(aliasEnumeration.nextElement()); |
| 87 | + } |
| 88 | + aliases.sort(null); |
| 89 | + for (final String alias : aliases) { |
| 90 | + final X509Certificate certificate = |
| 91 | + (X509Certificate) keyStoreConfiguration.getKeyStore().getCertificate(alias); |
| 92 | + final String issuer = certificate.getIssuerX500Principal().getName(); |
| 93 | + final String serialNumber = certificate.getSerialNumber().toString(); |
| 94 | + hashElements.add(issuer); |
| 95 | + hashElements.add(serialNumber); |
| 96 | + } |
| 97 | + } catch (KeyStoreException e) { |
| 98 | + LOGGER.debug("Error encountered reading " + keyStoreConfiguration.getLocation(), e); |
| 99 | + } |
| 100 | + } |
| 101 | + |
62 | 102 | /** |
63 | 103 | * Clears the secret fields in this object but still allow it to operate normally. |
64 | 104 | */ |
|
0 commit comments