@@ -210,7 +210,14 @@ function Add-KFCertificateToStore{
210210 Write-Verbose " Add-KFCertificateToStore - Received: StoreName: '$StoreName ', CryptoServiceProvider: '$CryptoServiceProvider ', Base64Cert: '$Base64Cert '"
211211
212212 # Get the thumbprint of the passed in certificate
213- $thumbprint = Get-PfxThumbprint - Base64Cert $Base64Cert - Password $PrivateKeyPassword
213+ # Convert password to secure string if provided, otherwise use $null
214+ $bytes = [System.Convert ]::FromBase64String($Base64Cert )
215+ $securePassword = if ($PrivateKeyPassword ) { ConvertTo-SecureString - String $PrivateKeyPassword - AsPlainText - Force } else { $null }
216+
217+ #
218+ $cert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2($bytes , $securePassword , [System.Security.Cryptography.X509Certificates.X509KeyStorageFlags ]::EphemeralKeySet)
219+ $thumbprint = $cert.Thumbprint
220+
214221 if (-not $thumbprint ) { throw " Failed to get the certificate thumbprint. The PFX may be invalid or the password is incorrect." }
215222
216223 if ($CryptoServiceProvider )
@@ -289,19 +296,15 @@ function Add-KFCertificateToStore{
289296 }
290297
291298 } else {
292- $bytes = [System.Convert ]::FromBase64String($Base64Cert )
293299 $certStore = New-Object - TypeName System.Security.Cryptography.X509Certificates.X509Store - ArgumentList $storeName , " LocalMachine"
294300 Write-Information " Store '$StoreName ' is open."
295301 $certStore.Open (5 )
296-
297- $cert = New-Object - TypeName System.Security.Cryptography.X509Certificates.X509Certificate2 - ArgumentList $bytes , $PrivateKeyPassword , 18 <# Persist, Machine #>
298302 $certStore.Add ($cert )
299303 $certStore.Close ();
300304 Write-Information " Store '$StoreName ' is closed."
301-
302305 }
303306
304- Write-Information " The thumbprint '$thumbprint ' was created in store $StoreName ."
307+ Write-Information " The thumbprint '$thumbprint ' was added to store $StoreName ."
305308 return $thumbprint
306309 } catch {
307310 Write-Error " An error occurred: $_ "
0 commit comments