Skip to content

Update Get-AzKeyVaultCertificate.md - Get cert and save it as pfx section to work with PowerShell Core #13557

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

Merged
merged 2 commits into from
Nov 24, 2020

Conversation

dawsonar802
Copy link
Contributor

@dawsonar802 dawsonar802 commented Nov 20, 2020

Description

The existing syntax does not seem to work in PowerShell Core. Using the proposed syntax works with both PowerShell Desktop and Core. When Calling the 'import' in this line:
$x509Cert.Import($secretByte, "", "Exportable,PersistKeySet") in PowerShell core, an error is received:
"Import" with "3" argument(s): "X509Certificate is immutable on this platform. Use the equivalent constructor instead."

Declaring the cert object like this works fine:
$x509Cert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2($secretByte, "", "Exportable,PersistKeySet")

The requested change is a combination of the existing MS Documentation and the above which was pulled from: https://gist.github.com/holgerjay/0db1f759f93cba268d671341b787472a

Requested Change to the example code:

$secretValueText = '';
$ssPtr = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($secret.SecretValue)
try {
$secretValueText = [System.Runtime.InteropServices.Marshal]::PtrToStringBSTR($ssPtr)
} finally {
[System.Runtime.InteropServices.Marshal]::ZeroFreeBSTR($ssPtr)
}
$secretByte = [Convert]::FromBase64String($secretValueText)
$x509Cert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2($secretByte, "", "Exportable,PersistKeySet")
$type = [System.Security.Cryptography.X509Certificates.X509ContentType]::Pfx
$pfxFileByte = $x509Cert.Export($type, $password)

Checklist

  • [x ] I have read the Submitting Changes section of CONTRIBUTING.md
  • [x ] The title of the PR is clear and informative
  • The appropriate ChangeLog.md file(s) has been updated:
    • For any service, the ChangeLog.md file can be found at src/{{SERVICE}}/{{SERVICE}}/ChangeLog.md
    • A snippet outlining the change(s) made in the PR should be written under the ## Upcoming Release header -- no new version header should be added
  • [x ] The PR does not introduce breaking changes
  • [ x] If applicable, the changes made in the PR have proper test coverage
  • For public API changes to cmdlets:
    • a cmdlet design review was approved for the changes in this repository (Microsoft internal only)
      • {Please put the link here}
    • the markdown help files have been regenerated using the commands listed here

The existing syntax does not seem to work in PowerShell Core.  Using the proposed syntax works with both PowerShell Desktop and Core.  When Calling the 'import' in this line:
$x509Cert.Import($secretByte, "", "Exportable,PersistKeySet") in PowerShell core, an error is received "Import" with "3" argument(s): "X509Certificate is immutable on this platform. Use the equivalent constructor instead."  
Declaring the cert object like this works fine:
$x509Cert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2($secretByte, "", "Exportable,PersistKeySet")

The recommended change is a combination of the existing MS Documentation and the above which was pulled from: https://gist.github.com/holgerjay/0db1f759f93cba268d671341b787472a
@ghost ghost added the customer-reported label Nov 20, 2020
@ghost
Copy link

ghost commented Nov 20, 2020

Thank you for your contribution dawsonar802! We will review the pull request and get back to you soon.

@msJinLei
Copy link
Contributor

/azp run

@azure-pipelines
Copy link
Contributor

Azure Pipelines successfully started running 3 pipeline(s).

@isra-fel
Copy link
Member

Thank you @dawsonar802 for your contribution! I verifed and the solution works on both Windows PowerShell and PowerShell 7.1

@isra-fel
Copy link
Member

/azp run

@azure-pipelines
Copy link
Contributor

Azure Pipelines successfully started running 3 pipeline(s).

@msJinLei msJinLei merged commit 3a197cb into Azure:master Nov 24, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants