|
| 1 | +package dev.suresh.tls |
| 2 | + |
| 3 | +import java.net.JarURLConnection |
| 4 | +import java.security.Security |
| 5 | +import java.util.jar.Manifest |
| 6 | +import kotlin.reflect.KClass |
| 7 | + |
| 8 | +/** |
| 9 | + * Enumeration of JSSE (Java Secure Socket Extension) system and security properties used for |
| 10 | + * configuring TLS/SSL connections. |
| 11 | + */ |
| 12 | +enum class TLSProp(val prop: String, val desc: String, val system: Boolean = true) { |
| 13 | + Debug("javax.net.debug", "Debugging SSL/TLS Connections."), |
| 14 | + KeyStore("javax.net.ssl.keyStore", "Default keystore"), |
| 15 | + KeyStoreType("javax.net.ssl.keyStoreType", "Default keystore type"), |
| 16 | + KeyStorePassword("javax.net.ssl.keyStorePassword", "Default keystore password"), |
| 17 | + KeyStoreProvider("javax.net.ssl.keyStoreProvider", "Default keystore provider"), |
| 18 | + TrustStore("javax.net.ssl.trustStore", "Default truststore"), |
| 19 | + TrustStoreType("javax.net.ssl.trustStoreType", "Default truststore type"), |
| 20 | + TrustStorePassword("javax.net.ssl.trustStorePassword", "Default truststore password"), |
| 21 | + TrustStoreProvider("javax.net.ssl.trustStoreProvider", "Default truststore provider"), |
| 22 | + ProxyHost("https.proxyHost", "Default HTTPS proxy host"), |
| 23 | + ProxyPort("https.proxyPort", "Default HTTPS proxy port"), |
| 24 | + HttpsCipherSuites("https.cipherSuites", "Default cipher suites"), |
| 25 | + HttpsProtocols("https.protocols", "Default HTTPS handshaking protocols"), |
| 26 | + TLSProtocols("jdk.tls.client.protocols", "Default Enabled TLS Protocols"), |
| 27 | + CertPathDisabledAlgos( |
| 28 | + "jdk.certpath.disabledAlgorithms", |
| 29 | + "Disabled certificate verification cryptographic algorithms", |
| 30 | + false), |
| 31 | + TLSDisabledAlgos("jdk.tls.disabledAlgorithms", "Disabled/Restricted Algorithms", false); |
| 32 | + |
| 33 | + /** Sets the JSSE system/security property to the given value. */ |
| 34 | + fun set(value: String) { |
| 35 | + when (system) { |
| 36 | + true -> System.setProperty(prop, value) |
| 37 | + else -> Security.setProperty(prop, value) |
| 38 | + } |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +/** |
| 43 | + * Returns the jar [Manifest] of the class. Returns `null` if the class is not bundled in a jar |
| 44 | + * (Classes in an unpacked class hierarchy). |
| 45 | + */ |
| 46 | +val <T : Any> KClass<T>.jarManifest: Manifest? |
| 47 | + get() { |
| 48 | + val res = java.getResource("${java.simpleName}.class") |
| 49 | + val conn = res?.openConnection() |
| 50 | + return if (conn is JarURLConnection) conn.manifest else null |
| 51 | + } |
0 commit comments