This repository has been archived by the owner on Jul 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* adds certificate store location * add additional certificate store tests * add cert store tests for New-CredentialStoreItem * fix test * add error handling for credential store path * add Import-CSCertificate helper function * Import new certificate if param is given * fix extension filter * add linux error message * fix pester test for linux * update cert helper functions * export helper functions * fix cs cert import * simplify cs cret lookup * remove obsolete functions * fix pester test for linux * fix error type for linux * fix var name * fix pester test * disable travis artifact upload * update cert lookup for item functions * debug build error * use cert instance constructor for linux * disable debug output * remove obsolete exports
- Loading branch information
Showing
12 changed files
with
422 additions
and
166 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
function Get-CSCertificate { | ||
<# | ||
.SYNOPSIS | ||
Returns the certificate object given by thumbprint. | ||
.DESCRIPTION | ||
You can use this function to get a stored certificate. Search for the object by its unique thumbprint. | ||
.PARAMETER Thumbprint | ||
Provide one or more thumprints. | ||
.PARAMETER StoreName | ||
Select the store name in which you want to search the certificates. | ||
.PARAMETER StoreLocation | ||
Select between the both available locations CurrentUser odr LocalMachine. | ||
.INPUTS | ||
[string] | ||
.OUTPUTS | ||
[System.Security.Cryptography.X509Certificates.X509Certificate2[]] | ||
.EXAMPLE | ||
Get-CSCertificate -Thumbprint '12345678' -StoreName 'My' -StoreLocation 'CurrentUser' | ||
.NOTES | ||
File Name : Get-CSCertificate.ps1 | ||
Author : Marco Blessing - [email protected] | ||
Requires : | ||
.LINK | ||
https://github.com/OCram85/PSCredentialStore | ||
#> | ||
[CmdletBinding()] | ||
[OutputType([System.Security.Cryptography.X509Certificates.X509Certificate2])] | ||
param( | ||
[Parameter(Mandatory = $true, ValueFromPipeline = $true)] | ||
[ValidateNotNullOrEmpty()] | ||
[string[]]$Thumbprint, | ||
|
||
[Parameter(Mandatory = $false)] | ||
[ValidateSet( | ||
'AddressBook', | ||
'AuthRoot', | ||
'CertificateAuthority', | ||
'Disallowed', | ||
'My', | ||
'Root', | ||
'TrustedPeople', | ||
'TrustedPublisher' | ||
)] | ||
[string]$StoreName = 'My', | ||
|
||
[Parameter(Mandatory = $false)] | ||
[ValidateSet( | ||
'CurrentUser', | ||
'LocalMachine' | ||
)] | ||
[string]$StoreLocation = 'CurrentUser' | ||
) | ||
|
||
begin { | ||
$Store = [System.Security.Cryptography.X509Certificates.X509Store]::New($StoreName, $StoreLocation) | ||
try { | ||
$Store.Open('ReadOnly') | ||
} | ||
catch { | ||
$_.Exception.Message | Write-Error -ErrorAction Stop | ||
} | ||
} | ||
|
||
process { | ||
foreach ($Thumb in $Thumbprint) { | ||
Write-Output $Store.Certificates | Where-Object { $_.Thumbprint -eq $Thumb } | ||
} | ||
} | ||
end { | ||
$Store.Close() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
function Import-CSCertificate { | ||
<# | ||
.SYNOPSIS | ||
adds a given pfx certificate file to current uerers personal certificate store. | ||
.DESCRIPTION | ||
This function is used to import existing pfx certificate files. The Import-PFXCertificate cmdle from the | ||
PKI module imports the certficate into a deprecated store. Thus you can't read the private key afterwards or | ||
using it for decrypting data. | ||
.PARAMETER Path | ||
Path to an existing *.pfx certificate file. | ||
.PARAMETER StoreName | ||
Additionally you change change the store where you want the certificate into. | ||
.INPUTS | ||
[None] | ||
.OUTPUTS | ||
[None] | ||
.EXAMPLE | ||
Import-CSCertificate -Path (Join-Path -Path $Env:APPDATA -ChildPath '/PSCredentialStore.pfx') | ||
.NOTES | ||
File Name : Import-CSCertificate.ps1 | ||
Author : Marco Blessing - [email protected] | ||
Requires : | ||
.LINK | ||
https://github.com/OCram85/PSCredentialStore | ||
#> | ||
[CmdletBinding()] | ||
[OutputType()] | ||
param( | ||
[Parameter(Mandatory = $true)] | ||
[ValidateNotNullOrEmpty()] | ||
[string]$Path, | ||
|
||
[Parameter(Mandatory = $false)] | ||
[ValidateSet( | ||
'AddressBook', | ||
'AuthRoot', | ||
'CertificateAuthority', | ||
'Disallowed', | ||
'My', | ||
'Root', | ||
'TrustedPeople', | ||
'TrustedPublisher' | ||
)] | ||
[string]$StoreName = 'My', | ||
|
||
[Parameter(Mandatory = $false)] | ||
[ValidateSet( | ||
'CurrentUser', | ||
'LocalMachine' | ||
)] | ||
[string]$StoreLocation = 'CurrentUser', | ||
|
||
[Parameter(Mandatory = $false)] | ||
[ValidateSet( | ||
'ReadOnly', | ||
'ReadWrite', | ||
'MaxAllowed', | ||
'OpenExistingOnly', | ||
'InclueArchived' | ||
)] | ||
[string]$OpenFlags = 'ReadWrite' | ||
) | ||
begin { | ||
$Store = [System.Security.Cryptography.X509Certificates.X509Store]::new($StoreName, $StoreLocation) | ||
try { | ||
$Store.Open($OpenFlags) | ||
} | ||
catch { | ||
$_.Exception.Message | Write-Error -ErrorAction Stop | ||
} | ||
} | ||
process { | ||
try { | ||
$cert = [System.Security.Cryptography.X509Certificates.X509Certificate2]::new( | ||
$Path, | ||
$null, | ||
( | ||
[System.Security.Cryptography.X509Certificates.X509KeyStorageFlags]::Exportable -bor | ||
[System.Security.Cryptography.X509Certificates.X509KeyStorageFlags]::PersistKeySet | ||
) | ||
) | ||
|
||
if (Test-CSCertificate -Thumbprint $cert.Thumbprint) { | ||
Write-Warning -Message ('The certificate with thumbprint {0} is already present!' -f $cert.Thumbprint) | ||
} | ||
else { | ||
$Store.Add($cert) | ||
} | ||
} | ||
catch { | ||
$_.Exception.Message | Write-Error -ErrorAction Stop | ||
$ErrorParams = @{ | ||
ErrorAction = 'Stop' | ||
Exception = [System.Exception]::new( | ||
'Could not read or add the pfx certificate!' | ||
) | ||
} | ||
Write-Error @ErrorParams | ||
} | ||
} | ||
end { | ||
$Store.Close() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
function Test-CSCertificate { | ||
<# | ||
.SYNOPSIS | ||
Tests if the given certificate exists in a store. | ||
.DESCRIPTION | ||
Use this function to ensure if a certificate is already imported into a given store. | ||
.PARAMETER Thumbprint | ||
Provide one or more thumprints. | ||
.PARAMETER StoreName | ||
Select the store name in which you want to search the certificates. | ||
.PARAMETER StoreLocation | ||
Select between the both available locations CurrentUser odr LocalMachine. | ||
.INPUTS | ||
[None] | ||
.OUTPUTS | ||
[bool] | ||
.EXAMPLE | ||
Test-CSCertificate -Thumbprint '12345678' -StoreName 'My' -StoreLocation 'CurrentUser' | ||
.NOTES | ||
File Name : Test-CSCertificate.ps1 | ||
Author : Marco Blessing - [email protected] | ||
Requires : | ||
.LINK | ||
https://github.com/OCram85/PSCredentialStore | ||
#> | ||
[CmdletBinding()] | ||
[OutputType([bool])] | ||
param( | ||
[Parameter(Mandatory = $true, ValueFromPipeline = $true)] | ||
[ValidateNotNullOrEmpty()] | ||
[string]$Thumbprint, | ||
|
||
[Parameter(Mandatory = $false)] | ||
[ValidateSet( | ||
'AddressBook', | ||
'AuthRoot', | ||
'CertificateAuthority', | ||
'Disallowed', | ||
'My', | ||
'Root', | ||
'TrustedPeople', | ||
'TrustedPublisher' | ||
)] | ||
[string]$StoreName = 'My', | ||
|
||
[Parameter(Mandatory = $false)] | ||
[ValidateSet( | ||
'CurrentUser', | ||
'LocalMachine' | ||
)] | ||
[string]$StoreLocation = 'CurrentUser' | ||
) | ||
|
||
begin { | ||
$Store = [System.Security.Cryptography.X509Certificates.X509Store]::New($StoreName, $StoreLocation) | ||
try { | ||
$Store.Open('ReadOnly') | ||
} | ||
catch { | ||
$_.Exception.Message | Write-Error -ErrorAction Stop | ||
} | ||
} | ||
|
||
process { | ||
$Cert = $Store.Certificates | Where-Object { $_.Thumbprint -eq $Thumbprint } | ||
|
||
if ($null -eq $Cert) { | ||
return $false | ||
} | ||
else { | ||
return $true | ||
} | ||
} | ||
end { | ||
$Store.Close() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.