Skip to content

Allow exporting only key/cert #84

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 27 additions & 4 deletions ssl-windows/Convert-PfxToPem.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
Private key passphrase, if applicable.
.PARAMETER Overwrite
Clobber any existing file when writing the PEM key file.
.PARAMETER CertOnly
Export only the certificate part of PFX file.
.PARAMETER KeyOnly
Export only the key part of PFX file.
#>

#
Expand Down Expand Up @@ -55,7 +59,11 @@ Param(
[Parameter(Mandatory=$false, Position=2)]
[string] $PEMFile,

[switch] $Overwrite = $false
[switch] $Overwrite = $false,

[switch] $CertOnly = $false,

[switch] $KeyOnly = $false
)

Add-Type @'
Expand Down Expand Up @@ -193,10 +201,25 @@ if (-not $cert.PrivateKey.CspKeyContainerInfo.Exportable)
Exit
}

$result = [MongoDB_Utils]::PfxCertificateToPem($cert)
if ($CertOnly -and $KeyOnly)
{
Write-Warning "CertOnly and KeyOnly parameters are mutually exclusive"
}

$parameters = ([Security.Cryptography.RSACryptoServiceProvider] $cert.PrivateKey).ExportParameters($true)
$result += "`r`n" + [MongoDB_Utils]::RsaPrivateKeyToPem($parameters);
if (-not $KeyOnly)
{
$result = [MongoDB_Utils]::PfxCertificateToPem($cert)
}

if (-not $CertOnly)
{
$parameters = ([Security.Cryptography.RSACryptoServiceProvider] $cert.PrivateKey).ExportParameters($true)
if ($result)
{
$result += "`r`n"
}
$result += [MongoDB_Utils]::RsaPrivateKeyToPem($parameters);
}

if (-not $PEMFile)
{
Expand Down