$ConfigContent = @"
; Request.inf
[Version]
Signature="`$Windows NT$"
[NewRequest]
Subject = "CN=$CN,C=ES,ST=Barcelona,L=Barcelona,O=$O"
KeySpec = 1
KeyLength = 2048
Exportable = TRUE
MachineKeySet = TRUE
SMIME = False
PrivateKeyArchive = FALSE
UserProtected = FALSE
UseExistingKeySet = FALSE
ProviderName = "Microsoft RSA SChannel Cryptographic Provider"
ProviderType = 12
RequestType = PKCS10
KeyUsage = 0xa0
[EnhancedKeyUsageExtension]
OID=1.3.6.1.5.5.7.3.1 ; Server Authentication
[Extensions]
2.5.29.17 = "{text}"
_continue_ = "DNS=$CN&"
_continue_ = "DNS=testing.$CN&"
"@
$ConfigContent | Out-File -FilePath "$CN.inf" -Encoding ASCII
# Create a certificate request
if (certreq -new -f "$CN.inf" "$CN.csr") {
# Submit the request to a Certificate Authority
# Define a regular expression pattern to match the ID
$pattern = 'Id\. de solicitud: (\d+)'
$commandOutput = certreq -submit -config "localhost\COMPANY-AD01-CA" "$CN.csr" "$CN.crt"
# Use the Select-String cmdlet to find the first match in the output
$match = $commandOutput | Select-String -Pattern $pattern | Select-Object -First 1
if ($match) {
$id = $match.Matches.Groups[1].Value
# Accept the issued certificate
certutil -config "localhost\COMPANY-AD01-CA" -resubmit $id
certreq -config "localhost\COMPANY-AD01-CA" -q -f -retrieve $id "$CN.crt"
Remove-Item -Path "$CN.inf", "$CN.csr", "$CN.rsp", "$CN.csr" -Force
}
else {
Write-Host "Failed to submit the certificate request."
}
}
else {
Write-Host "Failed to create the certificate request."
}
Using this I create a certificate, send it to the CA and accept it.
certreq -retrieve
only gets me the certificate, not the key, how do I get the key or how do I use that certificate in apache?