forked from mtusi2026/mtusi2026lab7
41 lines
1.7 KiB
PowerShell
41 lines
1.7 KiB
PowerShell
[CmdletBinding()]
|
|
param(
|
|
[string]$OutputDirectory = (Join-Path $PSScriptRoot '..\secrets\tls'),
|
|
[int]$ValidityDays = 30
|
|
)
|
|
|
|
$ErrorActionPreference = 'Stop'
|
|
|
|
if (-not (Get-Command openssl -ErrorAction SilentlyContinue)) {
|
|
throw 'openssl is required and was not found in PATH'
|
|
}
|
|
|
|
New-Item -ItemType Directory -Path $OutputDirectory -Force | Out-Null
|
|
$OutputDirectory = (Resolve-Path -LiteralPath $OutputDirectory).Path
|
|
|
|
$caKey = Join-Path $OutputDirectory 'ca.key'
|
|
$caCert = Join-Path $OutputDirectory 'ca.crt'
|
|
$serverKey = Join-Path $OutputDirectory 'server.key'
|
|
$serverCsr = Join-Path $OutputDirectory 'server.csr'
|
|
$serverCert = Join-Path $OutputDirectory 'server.crt'
|
|
$extensions = Join-Path $OutputDirectory 'server.ext'
|
|
|
|
@'
|
|
basicConstraints=critical,CA:FALSE
|
|
keyUsage=critical,digitalSignature,keyEncipherment
|
|
extendedKeyUsage=serverAuth
|
|
subjectAltName=DNS:localhost,IP:127.0.0.1
|
|
'@ | Set-Content -LiteralPath $extensions -Encoding ascii
|
|
|
|
& openssl genpkey -algorithm RSA -pkeyopt rsa_keygen_bits:3072 -out $caKey
|
|
& openssl req -x509 -new -sha256 -key $caKey -days $ValidityDays `
|
|
-subj '/CN=HLA Lab 7 Local CA' -out $caCert
|
|
& openssl genpkey -algorithm RSA -pkeyopt rsa_keygen_bits:3072 -out $serverKey
|
|
& openssl req -new -sha256 -key $serverKey -subj '/CN=localhost' -out $serverCsr
|
|
& openssl x509 -req -sha256 -in $serverCsr -CA $caCert -CAkey $caKey `
|
|
-CAcreateserial -days $ValidityDays -extfile $extensions -out $serverCert
|
|
|
|
Remove-Item -LiteralPath $serverCsr, $extensions, (Join-Path $OutputDirectory 'ca.srl') -Force -ErrorAction SilentlyContinue
|
|
Write-Host "Created local TLS material in $OutputDirectory"
|
|
Write-Host "Trust this CA only for the lab: $caCert"
|