|
| 1 | +package no.nav.security.mock.oauth2.http |
| 2 | + |
| 3 | +import org.bouncycastle.asn1.oiw.OIWObjectIdentifiers |
| 4 | +import org.bouncycastle.asn1.x500.X500Name |
| 5 | +import org.bouncycastle.asn1.x509.AlgorithmIdentifier |
| 6 | +import org.bouncycastle.asn1.x509.AuthorityKeyIdentifier |
| 7 | +import org.bouncycastle.asn1.x509.BasicConstraints |
| 8 | +import org.bouncycastle.asn1.x509.ExtendedKeyUsage |
| 9 | +import org.bouncycastle.asn1.x509.Extension |
| 10 | +import org.bouncycastle.asn1.x509.GeneralName |
| 11 | +import org.bouncycastle.asn1.x509.GeneralNames |
| 12 | +import org.bouncycastle.asn1.x509.KeyPurposeId |
| 13 | +import org.bouncycastle.asn1.x509.KeyUsage |
| 14 | +import org.bouncycastle.asn1.x509.SubjectKeyIdentifier |
| 15 | +import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo |
| 16 | +import org.bouncycastle.cert.X509ExtensionUtils |
| 17 | +import org.bouncycastle.cert.X509v3CertificateBuilder |
| 18 | +import org.bouncycastle.cert.jcajce.JcaX509CertificateConverter |
| 19 | +import org.bouncycastle.cert.jcajce.JcaX509v3CertificateBuilder |
| 20 | +import org.bouncycastle.jce.provider.BouncyCastleProvider |
| 21 | +import org.bouncycastle.operator.ContentSigner |
| 22 | +import org.bouncycastle.operator.bc.BcDigestCalculatorProvider |
| 23 | +import org.bouncycastle.operator.jcajce.JcaContentSignerBuilder |
| 24 | +import java.io.File |
| 25 | +import java.math.BigInteger |
| 26 | +import java.security.KeyPair |
| 27 | +import java.security.KeyPairGenerator |
| 28 | +import java.security.KeyStore |
| 29 | +import java.security.PublicKey |
| 30 | +import java.security.cert.X509Certificate |
| 31 | +import java.time.Duration |
| 32 | +import java.time.Instant |
| 33 | +import java.util.Date |
| 34 | +import javax.net.ssl.KeyManagerFactory |
| 35 | +import javax.net.ssl.SSLContext |
| 36 | +import javax.net.ssl.SSLEngine |
| 37 | + |
| 38 | +class Ssl @JvmOverloads constructor( |
| 39 | + val sslKeystore: SslKeystore = SslKeystore() |
| 40 | +) { |
| 41 | + fun sslEngine(): SSLEngine = sslContext().createSSLEngine().apply { |
| 42 | + useClientMode = false |
| 43 | + needClientAuth = false |
| 44 | + } |
| 45 | + |
| 46 | + fun sslContext(): SSLContext { |
| 47 | + val keyManager = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()).apply { |
| 48 | + init(sslKeystore.keyStore, sslKeystore.keyPassword.toCharArray()) |
| 49 | + } |
| 50 | + return SSLContext.getInstance("TLS").apply { |
| 51 | + init(keyManager.keyManagers, null, null) |
| 52 | + } |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +class SslKeystore @JvmOverloads constructor( |
| 57 | + val keyPassword: String = "", |
| 58 | + val keyStore: KeyStore = generate("localhost", keyPassword) |
| 59 | +) { |
| 60 | + @JvmOverloads constructor( |
| 61 | + keyPassword: String, |
| 62 | + keystoreFile: File, |
| 63 | + keystoreType: KeyStoreType = KeyStoreType.PKCS12, |
| 64 | + keystorePassword: String = "", |
| 65 | + ) : this(keyPassword, keyStore(keystoreFile, keystoreType, keystorePassword)) |
| 66 | + |
| 67 | + enum class KeyStoreType { |
| 68 | + PKCS12, |
| 69 | + JKS |
| 70 | + } |
| 71 | + |
| 72 | + companion object { |
| 73 | + private const val CERT_SIGNATURE_ALG = "SHA256withRSA" |
| 74 | + private const val KEY_ALG = "RSA" |
| 75 | + private const val KEY_SIZE = 2048 |
| 76 | + |
| 77 | + fun generate(hostname: String, keyPassword: String): KeyStore { |
| 78 | + val keyPair = KeyPairGenerator.getInstance(KEY_ALG).apply { initialize(KEY_SIZE) }.generateKeyPair() |
| 79 | + val cert = keyPair.toX509Certificate(hostname) |
| 80 | + return KeyStore.getInstance(KeyStoreType.PKCS12.name).apply { |
| 81 | + this.load(null) |
| 82 | + this.setKeyEntry(hostname, keyPair.private, keyPassword.toCharArray(), arrayOf(cert)) |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + private fun keyStore( |
| 87 | + keystoreFile: File, |
| 88 | + keystoreType: KeyStoreType = KeyStoreType.PKCS12, |
| 89 | + keystorePassword: String = "", |
| 90 | + ) = KeyStore.getInstance(keystoreType.name).apply { |
| 91 | + keystoreFile.inputStream().use { |
| 92 | + load(it, keystorePassword.toCharArray()) |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + private fun KeyPair.toX509Certificate(cn: String, expiry: Duration = Duration.ofDays(365)): X509Certificate { |
| 97 | + val now = Instant.now() |
| 98 | + val x500Name = X500Name("CN=$cn") |
| 99 | + val contentSigner: ContentSigner = JcaContentSignerBuilder(CERT_SIGNATURE_ALG).build(this.private) |
| 100 | + val certificateHolder = JcaX509v3CertificateBuilder( |
| 101 | + x500Name, |
| 102 | + BigInteger.valueOf(now.toEpochMilli()), |
| 103 | + Date.from(now), |
| 104 | + Date.from(now.plus(expiry)), |
| 105 | + x500Name, |
| 106 | + this.public |
| 107 | + ).addExtensions(cn, this.public).build(contentSigner) |
| 108 | + return JcaX509CertificateConverter().setProvider(BouncyCastleProvider()).getCertificate(certificateHolder) |
| 109 | + } |
| 110 | + |
| 111 | + private fun X509v3CertificateBuilder.addExtensions(cn: String, publicKey: PublicKey) = apply { |
| 112 | + addExtension(Extension.subjectKeyIdentifier, false, publicKey.createSubjectKeyId()) |
| 113 | + .addExtension(Extension.authorityKeyIdentifier, false, publicKey.createAuthorityKeyId()) |
| 114 | + .addExtension(Extension.basicConstraints, true, BasicConstraints(true)) |
| 115 | + .addExtension(Extension.subjectAlternativeName, false, GeneralNames(GeneralName(GeneralName.dNSName, cn))) |
| 116 | + .addExtension(Extension.keyUsage, false, KeyUsage(KeyUsage.digitalSignature)) |
| 117 | + .addExtension(Extension.extendedKeyUsage, false, ExtendedKeyUsage(KeyPurposeId.id_kp_serverAuth)) |
| 118 | + } |
| 119 | + |
| 120 | + private fun PublicKey.createSubjectKeyId(): SubjectKeyIdentifier = |
| 121 | + X509ExtensionUtils(digestCalculator()).createSubjectKeyIdentifier(SubjectPublicKeyInfo.getInstance(encoded)) |
| 122 | + |
| 123 | + private fun PublicKey.createAuthorityKeyId(): AuthorityKeyIdentifier = |
| 124 | + X509ExtensionUtils(digestCalculator()).createAuthorityKeyIdentifier(SubjectPublicKeyInfo.getInstance(encoded)) |
| 125 | + |
| 126 | + private fun digestCalculator() = BcDigestCalculatorProvider().get(AlgorithmIdentifier(OIWObjectIdentifiers.idSHA1)) |
| 127 | + } |
| 128 | +} |
0 commit comments