2020-03-24 06:13:01 -04:00
function ConvertTo-IcingaX509Certificate ( )
{
param (
[ string ] $CertFile = $null ,
[ string ] $OutFile = $null ,
[ switch ] $Force = $FALSE
) ;
2020-03-26 02:33:28 -04:00
if ( [ string ] :: IsNullOrEmpty ( $CertFile ) ) {
throw 'Please specify a valid path to an existing certificate (.cer, .pem, .cert)' ;
}
if ( ( Test-Path $CertFile ) -eq $FALSE ) {
throw 'The provided path to your certificate was not valid' ;
}
2020-03-24 06:13:01 -04:00
# Use an empty password for converted certificates
$Password = $null ;
# Use a target file to specify if we use temp files or not
2020-03-26 02:32:57 -04:00
$TargetFile = $OutFile ;
2020-03-24 06:13:01 -04:00
# Temp Cert
[ bool ] $TempFile = $FALSE ;
# Create a temp file to store the certificate in
if ( [ string ] :: IsNullOrEmpty ( $OutFile ) ) {
# Create a temporary file for full path and name
$TargetFile = New-IcingaTemporaryFile ;
# Get the actual path to work with
$TargetFile = $TargetFile . FullName ;
# Set internally that we are using a temp file
$TempFile = $TRUE ;
# Delete the file again
Remove-Item $TargetFile -Force -ErrorAction SilentlyContinue ;
}
# Convert our certificate if our target file does not exist
# it is a temp file or we force its creation
if ( -Not ( Test-Path $TargetFile ) -Or $TempFile -Or $Force ) {
Write-Output " $Password
2024-04-12 13:22:55 -04:00
$Password " | & 'C:\Windows\system32\certutil.exe' -mergepfx " $CertFile " " $TargetFile " | Set-Variable -Name 'CertUtilOutput';
2020-03-24 06:13:01 -04:00
}
2020-03-26 02:34:10 -04:00
Write-IcingaDebugMessage -Message (
[ string ] :: Format (
'Certutil merge request has been completed. Certutil message:{0}{0}{1}' ,
( New-IcingaNewLine ) ,
2024-02-28 09:41:05 -05:00
( $CertUtilOutput | Out-String )
2020-03-26 02:34:10 -04:00
)
) ;
2020-03-24 06:13:01 -04:00
# If no target file exists afterwards (a valid PFX certificate)
# then throw an exception
if ( -Not ( Test-Path $TargetFile ) ) {
2024-02-28 09:41:05 -05:00
[ string ] $ErrMessage = [ string ] :: Format ( 'Unable to create the Icinga for Windows certificate file "icingaforwindows.pfx". Certutil output:{0}{1}' , ( New-IcingaNewLine ) , ( $CertUtilOutput | Out-String ) ) ;
Write-IcingaConsoleError $ErrMessage ;
throw $ErrMessage ;
2020-03-24 06:13:01 -04:00
}
# Now load the actual certificate from the path
$Certificate = New-Object Security . Cryptography . X509Certificates . X509Certificate2 $TargetFile ;
# Delete the PFX-Certificate which will be present after certutil merge
2020-03-26 02:32:20 -04:00
if ( $TempFile ) {
Remove-Item $TargetFile -Force -ErrorAction SilentlyContinue ;
}
2020-03-24 06:13:01 -04:00
# Return the certificate
return $Certificate
}