# Makes a private certificate authority and a server certificate signed by it, # in .\certs next to this script: # # certs\ca.crt give this to clients (sslrootcert=certs/ca.crt) # certs\ca.key keep it private; it signs new server certificates # certs\server.crt the server's certificate (--pg-cert, --rdb-cert) # certs\server.key the server's private key (--pg-key, --rdb-key) # # usage: .\make-certs.ps1 [extra host name or IP ...] # The certificate always names localhost, 127.0.0.1 and rasterizeddb (the # Compose service name). Add the names clients use to reach the server: # .\make-certs.ps1 db.example.com 10.0.0.5 # # Needs openssl on PATH (Git for Windows ships one in its usr\bin folder). # Refuses to overwrite an existing certs folder. param( [Parameter(ValueFromRemainingArguments = $true)] [string[]] $Names, [int] $Days = 825 ) $ErrorActionPreference = 'Stop' Set-Location $PSScriptRoot $dir = 'certs' if (Test-Path $dir) { throw "$dir already exists; move it away to make new certificates." } if (-not (Get-Command openssl -ErrorAction SilentlyContinue)) { throw 'openssl is not on PATH.' } $san = 'DNS:localhost,IP:127.0.0.1,DNS:rasterizeddb' foreach ($name in $Names) { if ($name -match '^[0-9.]+$' -or $name -like '*:*') { $san += ",IP:$name" } else { $san += ",DNS:$name" } } New-Item -ItemType Directory -Path $dir | Out-Null function Invoke-OpenSsl { & openssl @args if ($LASTEXITCODE -ne 0) { throw "openssl $($args[0]) failed" } } # Own configuration files, so the result does not depend on the system's # openssl.cnf. @' [req] prompt = no distinguished_name = dn x509_extensions = v3_ca [dn] CN = RasterizedDB local CA [v3_ca] basicConstraints = critical, CA:TRUE keyUsage = critical, keyCertSign, cRLSign subjectKeyIdentifier = hash '@ | Set-Content -Encoding ascii "$dir\ca.cnf" @' [req] prompt = no distinguished_name = dn [dn] CN = rasterizeddb '@ | Set-Content -Encoding ascii "$dir\server.cnf" @" basicConstraints = critical, CA:FALSE keyUsage = critical, digitalSignature, keyEncipherment extendedKeyUsage = serverAuth subjectAltName = $san "@ | Set-Content -Encoding ascii "$dir\server.ext" Invoke-OpenSsl req -x509 -new -newkey rsa:3072 -nodes -sha256 -days 3650 ` -config "$dir/ca.cnf" -keyout "$dir/ca.key" -out "$dir/ca.crt" Invoke-OpenSsl req -new -newkey rsa:3072 -nodes -sha256 ` -config "$dir/server.cnf" -keyout "$dir/server.key" -out "$dir/server.csr" Invoke-OpenSsl x509 -req -sha256 -days $Days ` -in "$dir/server.csr" -CA "$dir/ca.crt" -CAkey "$dir/ca.key" -CAcreateserial ` -extfile "$dir/server.ext" -out "$dir/server.crt" Remove-Item -Force -ErrorAction SilentlyContinue "$dir\ca.cnf", "$dir\server.cnf", "$dir\server.ext", "$dir\server.csr", "$dir\ca.srl" Write-Host '' Write-Host "Wrote $dir\ca.crt and $dir\server.crt (with their keys) for: $san" Write-Host "Keep $dir\ca.key and $dir\server.key private."