Contenidos
Git https://github.com/jesusninoc/SSLsinError
Crear un certificado en PowerShell
1 2 3 |
$cert = New-SelfSignedCertificate -DnsName "localhost" -CertStoreLocation "cert:\LocalMachine\My" $password = ConvertTo-SecureString -String "123456789" -Force -AsPlainText Export-PfxCertificate -Cert "cert:\LocalMachine\My\$($cert.Thumbprint)" -FilePath "keystore.pfx" -Password $password |
Servidor en Kotlin (es necesario instalar dependencias KTOR)
1 2 3 4 |
dependencies { implementation("io.ktor:ktor-server-netty:1.6.5") implementation("io.ktor:ktor-server-core:1.6.5") } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
import io.ktor.application.* import io.ktor.features.* import io.ktor.http.* import io.ktor.response.* import io.ktor.routing.* import io.ktor.server.engine.* import io.ktor.server.netty.* import java.io.File import java.security.KeyStore import javax.net.ssl.KeyManagerFactory import javax.net.ssl.SSLContext import javax.net.ssl.TrustManagerFactory fun main() { val keystoreFile = File("keystore.pfx") val keystorePassword = "123456789" // Load the keystore val keyStore = KeyStore.getInstance("PKCS12").apply { load(keystoreFile.inputStream(), keystorePassword.toCharArray()) } // Create an SSL context val sslContext = SSLContext.getInstance("TLS").apply { val keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()).apply { init(keyStore, keystorePassword.toCharArray()) } val trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()).apply { init(keyStore) } init(keyManagerFactory.keyManagers, trustManagerFactory.trustManagers, null) } val environment = applicationEngineEnvironment { sslConnector( keyStore = keyStore, keyAlias = keyStore.aliases().nextElement(), // Use the first alias keyStorePassword = { keystorePassword.toCharArray() }, privateKeyPassword = { keystorePassword.toCharArray() } ) { port = 8443 keyStorePath = keystoreFile.absoluteFile } module(Application::module) } embeddedServer(Netty, environment).start(wait = true) } fun Application.module() { install(CallLogging) routing { get("/") { call.respondText("<html><body><h1>Hello, world!</h1></body></html>", ContentType.Text.Html) } } } |
Petición desde Chrome al puerto configurado como seguro
