Skip to content

Commit

Permalink
Minor changes to condense code
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Glatzer committed Apr 19, 2022
1 parent ada2940 commit 38c5d54
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions ACME-PS/internal/classes/crypto/Certificate.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,16 @@ class Certificate {
# note: this returns issuers before the certs they've issued. This is the opposite order to what's desired; but is the order that produces the correct output when combined with the X509Certificate2Collection's Export command
hidden static [Security.Cryptography.X509Certificates.X509Certificate2Collection] ConvertToCertificateCollection([Collections.ArrayList] $acmeCertificates) {
$result = [Security.Cryptography.X509Certificates.X509Certificate2Collection]::new();

# process any quick wins (i.e. where it's clear there's no dependency
$todoCount = $acmeCertificates.Count - 1;
for ($i = $todoCount; $i -ge 0; $i--) {
if ([string]::IsNullOrWhitespace($acmeCertificates[$i].Issuer) -or ($acmeCertificates[$i].Subject -eq $acmeCertificates[$i].Issuer)) {
if (!($acmeCertificates[$i].Issuer) -or ($acmeCertificates[$i].Subject -eq $acmeCertificates[$i].Issuer)) {
$result.Add($acmeCertificates[$i]); #| out-null
$acmeCertificates.RemoveAt($i);
}
}

# then work through the chains, returning all certificates whose issuers aren't in the unprocessed collection
$todoSubjects = [Collections.ArrayList]::new( ($acmeCertificates | Select-Object -ExpandProperty 'Subject') );
while ($todoCount = $acmeCertificates.Count) {
Expand All @@ -56,15 +58,19 @@ class Certificate {
throw [System.ArgumentException]::new("There appears to be a circular loop in the given certificate's dependency chain"); # I don't think this would ever occur; but maybe it's a risk for some self signed cert scenarios?
}
}

return $result;
}

hidden static [Security.Cryptography.X509Certificates.X509Certificate2Collection] ConvertToCertificateCollection([byte[][]] $acmeCertificates, [Security.Cryptography.AsymmetricAlgorithm] $algorithm) {
hidden static [Security.Cryptography.X509Certificates.X509Certificate2Collection] CreateCertificateCollection([byte[][]] $acmeCertificates, [Security.Cryptography.AsymmetricAlgorithm] $algorithm) {
$certs = [Collections.ArrayList]::new();

$certs.Add([Certificate]::CreateX509WithKey($acmeCertificates[0], $algorithm));

for($i = 1; $i -lt $acmeCertificates.Length; $i++) {
$certs.Add([Security.Cryptography.X509Certificates.X509Certificate2]::new($acmeCertificates[$i]));
}

return [Certificate]::ConvertToCertificateCollection($certs);
}

Expand All @@ -74,7 +80,7 @@ class Certificate {

static [byte[]] ExportPfxCertificateChain([byte[][]] $acmeCertificates, [Security.Cryptography.AsymmetricAlgorithm] $algorithm, [securestring] $password) {

$certificateCollection = [Certificate]::ConvertToCertificateCollection($acmeCertificates, $algorithm);
$certificateCollection = [Certificate]::CreateCertificateCollection($acmeCertificates, $algorithm);

if($password) {
$unprotectedPassword = [PSCredential]::new("ACME-PS", $password).GetNetworkCredential().Password;
Expand Down

0 comments on commit 38c5d54

Please sign in to comment.