Skip to content

Instantly share code, notes, and snippets.

@jaloplo
Created March 3, 2024 16:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaloplo/724f28db17fab829457528fe8d5ad5ac to your computer and use it in GitHub Desktop.
Save jaloplo/724f28db17fab829457528fe8d5ad5ac to your computer and use it in GitHub Desktop.
PowerShell script for generating the script to create public and private keys using the openssl tool
param(
[Parameter(Mandatory=$true)]
[string] $Path,
[Parameter(Mandatory=$true)]
[string] $Name,
[Parameter(Mandatory=$true)]
[string] $OutFile
)
# Set the paths for the certificate, private key, and PEM files
$certificatePath = "$Path\$Name.crt"
$certificateCsrPath = "$Path\$Name.csr"
$certificateKeyPath = "$Path\$Name.key"
$certificatePemPath = "$Path\$Name.pem"
$null = New-Item -Path $Path -Name $OutFile -ItemType File -Force
"# Generate an RSA private key (PKCS#8 format) and a certificate signing request (CSR)" | Out-File -Append -FilePath "$Path\$OutFile"
"openssl genpkey -algorithm RSA -out ""$certificateKeyPath"" -pkeyopt rsa_keygen_bits:2048" | Out-File -Append -FilePath "$Path\$OutFile"
"" | Out-File -Append -FilePath "$Path\$OutFile"
"openssl req -new -key ""$certificateKeyPath"" -out ""$certificateCsrPath"" -subj ""/CN=$Name""" | Out-File -Append -FilePath "$Path\$OutFile"
"" | Out-File -Append -FilePath "$Path\$OutFile"
"" | Out-File -Append -FilePath "$Path\$OutFile"
"# Generate a self-signed certificate using the CSR" | Out-File -Append -FilePath "$Path\$OutFile"
"openssl x509 -signkey ""$certificateKeyPath"" -in ""$certificateCsrPath"" -req -days 365 -out ""$certificatePath""" | Out-File -Append -FilePath "$Path\$OutFile"
"" | Out-File -Append -FilePath "$Path\$OutFile"
"" | Out-File -Append -FilePath "$Path\$OutFile"
"# Convert the private key to PEM format" | Out-File -Append -FilePath "$Path\$OutFile"
"openssl pkey -in ""$certificateKeyPath"" -out ""$certificatePemPath""" | Out-File -Append -FilePath "$Path\$OutFile"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment