Files
homelab-scripts/csr.ps1
2020-08-04 18:21:41 -04:00

65 lines
1.8 KiB
PowerShell

## still need to get parser and ask if none
$domain = "home.johnhgaunt.com"
$hostnames = "gauntgitea"
foreach ($hostname in $hostnames) {
# create a working directory in the temp folder
$workingDirectory = "$env:TEMP\csr"
$outDirectory = "C:\Users\jgaunt\Temp"
$configFile = "$workingDirectory\csr.conf"
$ecParamsFile = "$workingDirectory\ec.params"
$keyFile = "$outDirectory\$hostname.$domain.key"
$csrFile = "$outDirectory\$hostname.$domain.csr"
$crtFile = "$outDirectory\$hostname.$domain.crt"
$config = "[ req ]
prompt = no
default_md = sha512
req_extensions = req_ext
distinguished_name = dn
[ dn ]
C=US
ST=PA
L=Pittsburgh
O=Gaunt
OU=Gaunt
emailAddress=admin@johnhgaunt.com
CN=$hostname.$domain
[ req_ext ]
subjectAltName = @alt_names
[ alt_names ]
DNS.1 = $hostname.$domain
DNS.2 = $hostname"
# create the temp directory
if (-not (test-path $dir)) {
New-Item -ItemType directory -Path $dir | Out-Null
}
# convert to unix file
# https://stackoverflow.com/questions/5102115/unix-format-files-with-powershell
sc $configFile ([byte[]][char[]] "$config") -Encoding Byte
# create the ec params
openssl ecparam -name secp384r1 -out $ecParamsFile
# create the ecc private key
openssl ecparam -in $ecParamsFile -genkey -noout -out $keyFile
# generate csr file
openssl req -new -key $keyFile -nodes -out $csrFile -config $configFile
# submit the created CSR
certreq -submit -config "GauntDC01.home.johnhgaunt.com\GAUNTDC01-CA" -attrib "CertificateTemplate:ServerandClient" $csrFile $crtFile
# remove temp files and dir
Remove-Item -Path $workingDirectory -Force -Recurse
Remove-Item -Path $outDirectory\$hostname.$domain.rsp
Remove-Item -Path $csrFile
}