Last active
March 16, 2024 02:11
-
-
Save mrhockeymonkey/628facf8e0c0f3c0ec8d19de5f638ec8 to your computer and use it in GitHub Desktop.
OpenSSL Example Usages
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# example creating certs for use with docker swarm | |
$openssl = 'C:\Program Files (x86)\OpenSSL\1.0.1L\bin\openssl.exe' | |
$Utf8NoBomEncoding = New-Object System.Text.UTF8Encoding $False | |
$CN = "some-cert-name" | |
$Fqdn = "computer1.com" | |
# create a CA | |
& $openssl genrsa -aes256 -out ca-key.pem 4096 | |
& $openssl req -new -x509 -days 365 -key ca-key.pem -sha256 -out ca.pem -subj "/CN=$CN" | |
# create a server cert | |
# create key and csr | |
& $openssl genrsa -out server-key.pem 4096 | |
& $openssl req -new -sha256 -key server-key.pem -out server.csr -subj "/CN=$Fqdn" | |
# sign server cert with the ca | |
$cnf = @" | |
subjectAltName = DNS:$Fqdn | |
extendedKeyUsage = serverAuth | |
"@ | |
[System.IO.File]::WriteAllLines("$PSScriptRoot\server-extfile.cnf", $cnf, $Utf8NoBomEncoding) | |
& $openssl x509 -req -days 365 -sha256 -in server.csr -CA ca.pem -CAkey ca-key.pem -CAcreateserial -out server-cert.pem -extfile server-extfile.cnf | |
Remove-Item *.srl,*.csr,*.cnf | |
# create a client cert | |
# generate new certificate key | |
& $openssl genrsa -out key.pem 4096 | |
& $openssl req -subj "/CN=$CN" -new -key key.pem -out client.csr | |
# sign this with the CA | |
$cnf = 'extendedKeyUsage = clientAuth' | |
[System.IO.File]::WriteAllLines("$PSScriptRoot\client-extfile.cnf", $cnf, $Utf8NoBomEncoding) | |
& $openssl x509 -req -days 365 -sha256 -in client.csr -CA ca.pem -CAkey ca-key.pem -CAcreateserial -out cert.pem -extfile client-extfile.cnf | |
Remove-Item *.srl,*.csr,*.cnf |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment