Skip to content

Commit

Permalink
Release 1.5.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Glatzer committed Apr 24, 2021
1 parent 3289edc commit f802754
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 97 deletions.
2 changes: 1 addition & 1 deletion ACME-PS/ACME-PS.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
ReleaseNotes = 'Please see the release notes from the release distribution page: https://github.com/PKISharp/ACME-PS/releases'

# Prerelase
Prerelease = 'beta'
# Prerelease = 'beta'
} # End of PSData hashtable

} # End of PrivateData hashtable
Expand Down
2 changes: 1 addition & 1 deletion dist/ACME-PS/ACME-PS.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
ReleaseNotes = 'Please see the release notes from the release distribution page: https://github.com/PKISharp/ACME-PS/releases'

# Prerelase
Prerelease = 'beta'
# Prerelease = 'beta'
} # End of PSData hashtable

} # End of PrivateData hashtable
Expand Down
190 changes: 95 additions & 95 deletions dist/ACME-PS/ACME-PS.psm1
Original file line number Diff line number Diff line change
@@ -1,98 +1,3 @@
class Certificate {
static [Security.Cryptography.X509Certificates.X509Certificate2] CreateX509WithKey([byte[]] $acmeCertificate, [Security.Cryptography.AsymmetricAlgorithm] $algorithm) {
$certificate = [Security.Cryptography.X509Certificates.X509Certificate2]::new($acmeCertificate);

if($algorithm -is [Security.Cryptography.RSA]) {
$certificate = [Security.Cryptography.X509Certificates.RSACertificateExtensions]::CopyWithPrivateKey($certificate, $algorithm);
}
elseif($algorithm -is [Security.Cryptography.ECDsa]) {
$certificate = [Security.Cryptography.X509Certificates.ECDsaCertificateExtensions]::CopyWithPrivateKey($certificate, $algorithm);
}
else {
throw [InvalidOperationException]::new("Cannot use $($algorithm.GetType().Name) to export pfx.");
}

return $certificate
}

static [byte[]] ExportPfxCertificate([byte[]] $acmeCertificate, [AcmePSKey] $key, [SecureString] $password) {
return [Certificate]::ExportPfxCertificate($acmeCertificate, $key.GetAlgorithm(), $password);
}

static [byte[]] ExportPfxCertificate([byte[]] $acmeCertificate, [Security.Cryptography.AsymmetricAlgorithm] $algorithm, [securestring] $password) {
$certificate = [Certificate]::CreateX509WithKey($acmeCertificate, $algorithm);

if($password) {
return $certificate.Export([Security.Cryptography.X509Certificates.X509ContentType]::Pfx, $password);
} else {
return $certificate.Export([Security.Cryptography.X509Certificates.X509ContentType]::Pfx);
}
}

static [byte[]] ExportPfxCertificateChain([byte[][]] $acmeCertificates, [AcmePSKey] $key, [SecureString] $password) {
return [Certificate]::ExportPfxCertificateChain($acmeCertificates, $key.GetAlgorithm(), $password);
}

static [byte[]] ExportPfxCertificateChain([byte[][]] $acmeCertificates, [Security.Cryptography.AsymmetricAlgorithm] $algorithm, [securestring] $password) {
$leafCertificate = [Certificate]::CreateX509WithKey($acmeCertificates[0], $algorithm);
$certificateCollection = [Security.Cryptography.X509Certificates.X509Certificate2Collection]::new($leafCertificate);

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

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

return $certificateCollection.Export([Security.Cryptography.X509Certificates.X509ContentType]::Pfx, $unprotectedPassword);
} else {
return $certificateCollection.Export([Security.Cryptography.X509Certificates.X509ContentType]::Pfx);
}
}

static [byte[]] GenerateCsr([string[]] $dnsNames, [string]$distinguishedName, [AcmePSKey] $key) {
return [Certificate]::GenerateCsr($dnsNames, $distinguishedName, $key.GetAlgorithm(), $key.GetHashName());
}

static [byte[]] GenerateCsr([string[]] $dnsNames, [string]$distinguishedName,
[Security.Cryptography.AsymmetricAlgorithm] $algorithm,
[Security.Cryptography.HashAlgorithmName] $hashName)
{
if(-not $dnsNames) {
throw [ArgumentException]::new("You need to provide at least one DNSName", "dnsNames");
}
if(-not $distinguishedName) {
thtow [ArgumentException]::new("Provide a distinguishedName for the Certificate")
}

$sanBuilder = [Security.Cryptography.X509Certificates.SubjectAlternativeNameBuilder]::new();
foreach ($dnsName in $dnsNames) {
$sanBuilder.AddDnsName($dnsName);
}

$certDN = [X500DistinguishedName]::new($distinguishedName);

[Security.Cryptography.X509Certificates.CertificateRequest]$certRequest = $null;

if($algorithm -is [Security.Cryptography.RSA]) {
$certRequest = [Security.Cryptography.X509Certificates.CertificateRequest]::new(
$certDN, $algorithm, $hashName, [Security.Cryptography.RSASignaturePadding]::Pkcs1);
}
elseif($algorithm -is [Security.Cryptography.ECDsa]) {
$certRequest = [Security.Cryptography.X509Certificates.CertificateRequest]::new(
$certDN, $algorithm, $hashName);

}
else {
throw [InvalidOperationException]::new("Cannot use $($algorithm.GetType().Name) to create CSR.");
}

$certRequest.CertificateExtensions.Add($sanBuilder.Build());
return $certRequest.CreateSigningRequest();
}
}

class AcmePSKey {
hidden [string] $_AlgorithmType;
hidden [Security.Cryptography.AsymmetricAlgorithm] $_Algorithm;
Expand Down Expand Up @@ -332,6 +237,101 @@ class AcmePSKey {
}
}

class Certificate {
static [Security.Cryptography.X509Certificates.X509Certificate2] CreateX509WithKey([byte[]] $acmeCertificate, [Security.Cryptography.AsymmetricAlgorithm] $algorithm) {
$certificate = [Security.Cryptography.X509Certificates.X509Certificate2]::new($acmeCertificate);

if($algorithm -is [Security.Cryptography.RSA]) {
$certificate = [Security.Cryptography.X509Certificates.RSACertificateExtensions]::CopyWithPrivateKey($certificate, $algorithm);
}
elseif($algorithm -is [Security.Cryptography.ECDsa]) {
$certificate = [Security.Cryptography.X509Certificates.ECDsaCertificateExtensions]::CopyWithPrivateKey($certificate, $algorithm);
}
else {
throw [InvalidOperationException]::new("Cannot use $($algorithm.GetType().Name) to export pfx.");
}

return $certificate
}

static [byte[]] ExportPfxCertificate([byte[]] $acmeCertificate, [AcmePSKey] $key, [SecureString] $password) {
return [Certificate]::ExportPfxCertificate($acmeCertificate, $key.GetAlgorithm(), $password);
}

static [byte[]] ExportPfxCertificate([byte[]] $acmeCertificate, [Security.Cryptography.AsymmetricAlgorithm] $algorithm, [securestring] $password) {
$certificate = [Certificate]::CreateX509WithKey($acmeCertificate, $algorithm);

if($password) {
return $certificate.Export([Security.Cryptography.X509Certificates.X509ContentType]::Pfx, $password);
} else {
return $certificate.Export([Security.Cryptography.X509Certificates.X509ContentType]::Pfx);
}
}

static [byte[]] ExportPfxCertificateChain([byte[][]] $acmeCertificates, [AcmePSKey] $key, [SecureString] $password) {
return [Certificate]::ExportPfxCertificateChain($acmeCertificates, $key.GetAlgorithm(), $password);
}

static [byte[]] ExportPfxCertificateChain([byte[][]] $acmeCertificates, [Security.Cryptography.AsymmetricAlgorithm] $algorithm, [securestring] $password) {
$leafCertificate = [Certificate]::CreateX509WithKey($acmeCertificates[0], $algorithm);
$certificateCollection = [Security.Cryptography.X509Certificates.X509Certificate2Collection]::new($leafCertificate);

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

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

return $certificateCollection.Export([Security.Cryptography.X509Certificates.X509ContentType]::Pfx, $unprotectedPassword);
} else {
return $certificateCollection.Export([Security.Cryptography.X509Certificates.X509ContentType]::Pfx);
}
}

static [byte[]] GenerateCsr([string[]] $dnsNames, [string]$distinguishedName, [AcmePSKey] $key) {
return [Certificate]::GenerateCsr($dnsNames, $distinguishedName, $key.GetAlgorithm(), $key.GetHashName());
}

static [byte[]] GenerateCsr([string[]] $dnsNames, [string]$distinguishedName,
[Security.Cryptography.AsymmetricAlgorithm] $algorithm,
[Security.Cryptography.HashAlgorithmName] $hashName)
{
if(-not $dnsNames) {
throw [ArgumentException]::new("You need to provide at least one DNSName", "dnsNames");
}
if(-not $distinguishedName) {
thtow [ArgumentException]::new("Provide a distinguishedName for the Certificate")
}

$sanBuilder = [Security.Cryptography.X509Certificates.SubjectAlternativeNameBuilder]::new();
foreach ($dnsName in $dnsNames) {
$sanBuilder.AddDnsName($dnsName);
}

$certDN = [X500DistinguishedName]::new($distinguishedName);

[Security.Cryptography.X509Certificates.CertificateRequest]$certRequest = $null;

if($algorithm -is [Security.Cryptography.RSA]) {
$certRequest = [Security.Cryptography.X509Certificates.CertificateRequest]::new(
$certDN, $algorithm, $hashName, [Security.Cryptography.RSASignaturePadding]::Pkcs1);
}
elseif($algorithm -is [Security.Cryptography.ECDsa]) {
$certRequest = [Security.Cryptography.X509Certificates.CertificateRequest]::new(
$certDN, $algorithm, $hashName);

}
else {
throw [InvalidOperationException]::new("Cannot use $($algorithm.GetType().Name) to create CSR.");
}

$certRequest.CertificateExtensions.Add($sanBuilder.Build());
return $certRequest.CreateSigningRequest();
}
}

class AcmeHttpResponse {
AcmeHttpResponse() {}

Expand Down

0 comments on commit f802754

Please sign in to comment.