Skip to content

Instantly share code, notes, and snippets.

@stevenroose
Last active June 4, 2021 06:25
Show Gist options
  • Save stevenroose/e6abde14258971eae982 to your computer and use it in GitHub Desktop.
Save stevenroose/e6abde14258971eae982 to your computer and use it in GitHub Desktop.
Setup CA-enabled SSL for Dart
void enableSSL() {
// the password used for the certutil db
var sslPassword = "";
// the certificate subject
// retrieved from certutil with command
// > certutil -d sql:. -L -n my_domain
// and look for the "Subject: " line under certificate data
var certificateName = "CN=mydomain.com,OU=...";
// init
SecureSocket.initialize(database: ".", password: sslPassword);
// bind
HttpServer.bindSecure(host, sslPort, certificateName: certificateName).then((server) {
// ...
});
}
# generate new private key
openssl req -out my_domain.csr -new -newkey rsa:2048 -nodes -keyout my_domain.key
# send the CSR to the SSL provider to issue a certificate
# files received from SSL provider:
# - AddTrustExternalCARoot.crt
# - COMODORSAAddTrustCA.crt
# - COMODORSADomainValidationSecureServerCA.crt
# - my_domain.crt
# create a new database
certutil -d sql:. -N
# add the root certificate (from SSL provider)
certutil -d sql:./ -A -t "C,," -n AddTrustExternalCARoot -i AddTrustExternalCARoot.crt
# add intermediate vertificates (from SSL provider)
certutil -d sql:./ -A -t ",," -n COMODORSAAddTrustCA -i COMODORSAAddTrustCA.crt
certutil -d sql:./ -A -t ",," -n COMODORSADomainValidationSecureServerCA -i COMODORSADomainValidationSecureServerCA.crt
# add my domain certificate (from SSL provider)
certutil -d sql:./ -A -t "P,," -n my_domain -i my_domain.crt
# with this config, the server (its a primitive Dart server) gives this error:
# > Cannot find private key for certificate
# convert the private key to a pkcs12 key (thanks to Eric Darchis, http://stackoverflow.com/a/27176982/749521)
openssl pkcs12 -export -out my_domain.p12 -inkey my_domain.key -in my_domain.crt -certfile COMODORSADomainValidationSecureServerCA.crt
# add the key to the database
pk12util -i my_domain.p12 -d sql:.
# put the 2 .db files into the bin/ folder of the Dart server project
# celebrate
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment