Skip to content

Commit

Permalink
Merge branch '58570-Add_Cert_With_No_PW' into MacysPOC
Browse files Browse the repository at this point in the history
  • Loading branch information
rcpokorny authored May 29, 2024
2 parents 3ce27b4 + 1a5353d commit c50a384
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 44 deletions.
114 changes: 73 additions & 41 deletions IISU/ClientPSCertStoreManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using Keyfactor.Orchestrators.Common.Enums;
using Keyfactor.Orchestrators.Extensions;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.FlowAnalysis;
using Microsoft.Extensions.Logging;
using System;
using System.IO;
Expand Down Expand Up @@ -115,7 +116,7 @@ public void DeletePFXFile(string filePath, string fileName)
}
}

public JobResult ImportPFXFile(string filePath, string privateKeyPassword, string cryptoProviderName)
public JobResult ImportPFXFile(string filePath, string privateKeyPassword, string cryptoProviderName, string storePath)
{
try
{
Expand All @@ -127,61 +128,92 @@ public JobResult ImportPFXFile(string filePath, string privateKeyPassword, strin

if (cryptoProviderName == null)
{
//string script = @"
//param($pfxFilePath, $privateKeyPassword)
//$output = certutil -importpfx -p $privateKeyPassword $pfxFilePath 2>&1
//$c = $LASTEXITCODE 2>&1
//$output
//";

string script = @"
param($pfxFilePath, $privateKeyPassword)
$output = certutil -importpfx -p $privateKeyPassword $pfxFilePath 2>&1
$exit_message = ""LASTEXITCODE:$($LASTEXITCODE)""
$stuff = certutil -dump
if ($stuff.GetType().Name -eq ""String"")
if (privateKeyPassword == null)
{
$stuff = @($stuff, $exit_message)
// If no private key password is provided, import the pfx file directory to the store using addstore argument
string script = @"
param($pfxFilePath, $storePath)
$output = certutil -addstore Cert:\LocalMachine\$storePath $pfxFilePath 2>&1
$c = $LASTEXITCODE
$output
";

ps.AddScript(script);
ps.AddParameter("pfxFilePath", filePath);
ps.AddParameter("storePath", storePath);
}
else
{
$stuff += $exit_message
}
// Use ImportPFX to import the pfx file with private key password to the appropriate cert store

$output
$stuff
";
string script = @"
param($pfxFilePath, $privateKeyPassword)
$output = certutil -importpfx -p $privateKeyPassword $pfxFilePath 2>&1
$exit_message = ""LASTEXITCODE:$($LASTEXITCODE)""
$stuff = certutil -dump
if ($stuff.GetType().Name -eq ""String"")
{
$stuff = @($stuff, $exit_message)
}
else
{
$stuff += $exit_message
}
ps.AddScript(script);
ps.AddParameter("pfxFilePath", filePath);
ps.AddParameter("privateKeyPassword", privateKeyPassword);
$output
$stuff
";

ps.AddScript(script);
ps.AddParameter("pfxFilePath", filePath);
ps.AddParameter("privateKeyPassword", privateKeyPassword);
ps.AddParameter("storePath", storePath);
}
}
else
{
string script = @"
param($pfxFilePath, $privateKeyPassword, $cspName)
$output = certutil -importpfx -csp $cspName -p $privateKeyPassword $pfxFilePath 2>&1
$exit_message = ""LASTEXITCODE:$($LASTEXITCODE)""
$stuff = certutil -dump
if ($stuff.GetType().Name -eq ""String"")
if (privateKeyPassword == null)
{
$stuff = @($stuff, $exit_message)
string script = @"
param($pfxFilePath, $cspName, $storePath)
$output = certutil -csp $cspName -addstore LocalMachine\$storePath $pfxFilePath 2>&1
$c = $LASTEXITCODE
$output
";

ps.AddScript(script);
ps.AddParameter("pfxFilePath", filePath);
ps.AddParameter("cspName", cryptoProviderName);
ps.AddParameter("storePath", storePath);
}
else
{
$stuff += $exit_message
}
string script = @"
param($pfxFilePath, $privateKeyPassword, $cspName)
$output = certutil -importpfx -csp $cspName -p $privateKeyPassword LocalMachine\$storePath $pfxFilePath 2>&1
$exit_message = ""LASTEXITCODE:$($LASTEXITCODE)""
$stuff = certutil -dump
$output
$stuff
";
if ($stuff.GetType().Name -eq ""String"")
{
$stuff = @($stuff, $exit_message)
}
else
{
$stuff += $exit_message
}
ps.AddScript(script);
ps.AddParameter("pfxFilePath", filePath);
ps.AddParameter("privateKeyPassword", privateKeyPassword);
ps.AddParameter("cspName", cryptoProviderName);
$output
$stuff
";

ps.AddScript(script);
ps.AddParameter("pfxFilePath", filePath);
ps.AddParameter("privateKeyPassword", privateKeyPassword);
ps.AddParameter("cspName", cryptoProviderName);
ps.AddParameter("storePath", storePath);
}
}

// Invoke the script
Expand Down
2 changes: 1 addition & 1 deletion IISU/ImplementedStoreTypes/Win/Management.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ private JobResult performAddition(ManagementJobConfiguration config)

// Using certutil on the remote computer, import the pfx file using a supplied csp if any.
_logger.LogTrace($"Importing temporary PFX File: {filePath}.");
JobResult result = manager.ImportPFXFile(filePath, privateKeyPassword, cryptoProvider);
JobResult result = manager.ImportPFXFile(filePath, privateKeyPassword, cryptoProvider, storePath);

// Delete the temporary file
_logger.LogTrace($"Deleting temporary PFX File: {filePath}.");
Expand Down
2 changes: 1 addition & 1 deletion IISU/ImplementedStoreTypes/WinIIS/Management.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ private JobResult PerformAddCertificate(ManagementJobConfiguration config, strin
_logger.LogTrace($"{filePath} was created.");

// Using certutil on the remote computer, import the pfx file using a supplied csp if any.
JobResult result = manager.ImportPFXFile(filePath, privateKeyPassword, cryptoProvider);
JobResult result = manager.ImportPFXFile(filePath, privateKeyPassword, cryptoProvider, storePath);

// Delete the temporary file
manager.DeletePFXFile(Path.GetDirectoryName(filePath), Path.GetFileNameWithoutExtension(filePath));
Expand Down
2 changes: 1 addition & 1 deletion IISU/ImplementedStoreTypes/WinSQL/Management.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ private JobResult PerformAddCertificate(ManagementJobConfiguration config, strin
_logger.LogTrace($"{filePath} was created.");

// Using certutil on the remote computer, import the pfx file using a supplied csp if any.
JobResult result = manager.ImportPFXFile(filePath, privateKeyPassword, cryptoProvider);
JobResult result = manager.ImportPFXFile(filePath, privateKeyPassword, cryptoProvider, storePath);

// Delete the temporary file
manager.DeletePFXFile(Path.GetDirectoryName(filePath), Path.GetFileNameWithoutExtension(filePath));
Expand Down

0 comments on commit c50a384

Please sign in to comment.