@@ -209,7 +209,9 @@ function Add-KFCertificateToStore{
209209 Write-Information " Entering PowerShell Script Add-KFCertificate"
210210 Write-Verbose " Add-KFCertificateToStore - Received: StoreName: '$StoreName ', CryptoServiceProvider: '$CryptoServiceProvider ', Base64Cert: '$Base64Cert '"
211211
212- $thumbprint = $null
212+ # Get the thumbprint of the passed in certificate
213+ $thumbprint = Get-PfxThumbprint - Base64Cert $Base64Cert - Password $PrivateKeyPassword
214+ if (-not $thumbprint ) { throw " Failed to get the certificate thumbprint. The PFX may be invalid or the password is incorrect." }
213215
214216 if ($CryptoServiceProvider )
215217 {
@@ -278,29 +280,11 @@ function Add-KFCertificateToStore{
278280 if ($process.ExitCode -ne 0 ) {
279281 throw " certutil failed with code $ ( $process.ExitCode ) . Output:`n $stdOut `n Error:`n $stdErr "
280282 }
281-
282- # Retrieve thumbprint of the newly imported cert
283- try {
284- $cert = Get-ChildItem - Path " Cert:\LocalMachine\$StoreName " |
285- Sort-Object NotAfter - Descending |
286- Select-Object - First 1
287- if ($cert ) {
288- Write-Information " Imported certificate thumbprint: $ ( $cert.Thumbprint ) "
289- return $cert.Thumbprint
290- } else {
291- Write-Warning " Could not retrieve the imported certificate."
292- return $null
293- }
294- }
295- catch {
296- Write-Warning " Failed to retrieve thumbprint: $_ "
297- return $null
298- }
299283 } catch {
300284 Write-Error " ERROR: $_ "
301285 } finally {
302286 if (Test-Path $tempPfx ) {
303- # Remove-Item $tempPfx -Force
287+ Remove-Item $tempPfx - Force
304288 }
305289 }
306290
@@ -314,13 +298,10 @@ function Add-KFCertificateToStore{
314298 $certStore.Add ($cert )
315299 $certStore.Close ();
316300 Write-Information " Store '$StoreName ' is closed."
317-
318- # Get the thumbprint so it can be returned to the calling function
319- $thumbprint = $cert.Thumbprint
320- Write-Information " The thumbprint '$thumbprint ' was created."
301+
321302 }
322303
323- Write-Host " Certificate added successfully to $StoreName ."
304+ Write-Information " The thumbprint ' $thumbprint ' was created in store $StoreName ."
324305 return $thumbprint
325306 } catch {
326307 Write-Error " An error occurred: $_ "
@@ -469,12 +450,19 @@ function New-KFIISSiteBinding {
469450 $_.bindingInformation -eq $searchBindings
470451 }
471452
472- if ($binding ) {
473- Write-Verbose " Binding thumbprint $thumbprint to $binding .bindingInformation in store: $StoreName "
474- $null = $binding.AddSslCertificate ($Thumbprint , $StoreName )
475- $result = New-ResultObject - Status Success - Code 0 - Step BindSSL
476- } else {
477- $result = New-ResultObject - Status Error - Code 202 - Step BindSSL - Message " No binding found for: $searchBindings "
453+ try
454+ {
455+ if ($binding ) {
456+ Write-Verbose " Binding thumbprint $thumbprint to $binding .bindingInformation in store: $StoreName "
457+ $null = $binding.AddSslCertificate ($Thumbprint , $StoreName )
458+ $result = New-ResultObject - Status Success - Code 0 - Step BindSSL
459+ } else {
460+ $result = New-ResultObject - Status Error - Code 202 - Step BindSSL - Message " No binding found for: $searchBindings "
461+ }
462+ }
463+ catch
464+ {
465+ $result = New-ResultObject - Status Error - Code 202 - Step BindSSL - Message $_
478466 }
479467 }
480468 } else {
@@ -1264,6 +1252,37 @@ function Import-SignedCertificate {
12641252# ####
12651253
12661254# Shared Functions
1255+ # Function to return the certificate's thumbprint
1256+ function Get-PfxThumbprint {
1257+ [CmdletBinding ()]
1258+ param (
1259+ [Parameter (Mandatory = $true )]
1260+ [string ]$Base64Cert ,
1261+
1262+ [Parameter (Mandatory = $false )]
1263+ [string ]$Password
1264+ )
1265+
1266+ try {
1267+ # Convert Base64 to byte array
1268+ $pfxBytes = [Convert ]::FromBase64String($Base64Cert )
1269+
1270+ # Convert password to secure string if provided, otherwise use $null
1271+ $securePassword = if ($Password ) { ConvertTo-SecureString - String $Password - AsPlainText - Force } else { $null }
1272+
1273+ # Import certificate
1274+ $cert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2
1275+ $cert.Import ($pfxBytes , $securePassword , [System.Security.Cryptography.X509Certificates.X509KeyStorageFlags ]::Exportable)
1276+
1277+ # Return thumbprint (formatted)
1278+ return $cert.Thumbprint.Replace (" " , " " ).ToUpper()
1279+ }
1280+ catch {
1281+ Write-Error " Failed to load PFX: $_ "
1282+ return $null
1283+ }
1284+ }
1285+
12671286# Function to get SAN (Subject Alternative Names) from a certificate
12681287function Get-KFSAN ($cert ) {
12691288 $san = $cert.Extensions | Where-Object { $_.Oid.FriendlyName -eq " Subject Alternative Name" }
0 commit comments