File size: 17,968 Bytes
8c763fb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 | # Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingConvertToSecureStringWithPlainText', '')]
param()
Import-Module (Join-Path -Path $PSScriptRoot 'certificateCommon.psm1') -Force
Describe "CmsMessage cmdlets and Get-PfxCertificate basic tests" -Tags "CI" {
BeforeAll {
$certLocation = New-GoodCertificate
$certLocation | Should -Not -BeNullOrEmpty | Out-Null
$protectedCertLocation = New-ProtectedCertificate
$protectedCertLocation | Should -Not -BeNullOrEmpty | Out-Null
}
It "Verify Get-PfxCertificate -FilePath" {
$cert = Get-PfxCertificate -FilePath $certLocation
$cert.Subject | Should -Be "CN=MyDataEnciphermentCert"
}
It "Verify Get-PfxCertificate -LiteralPath" {
$cert = Get-PfxCertificate -LiteralPath $certLocation
$cert.Subject | Should -Be "CN=MyDataEnciphermentCert"
}
It "Verify Get-PfxCertificate positional argument" {
$cert = Get-PfxCertificate $certLocation
$cert.Subject | Should -Be "CN=MyDataEnciphermentCert"
}
It "Verify Get-PfxCertificate right password" {
$password = Get-CertificatePassword
$cert = Get-PfxCertificate $protectedCertLocation -Password $password
$cert.Subject | Should -Be "CN=localhost"
}
It "Verify Get-PfxCertificate wrong password" {
#[SuppressMessage("Microsoft.Security", "CS002:SecretInNextLine", Justification="Test secret.")]
$pass = ConvertTo-SecureString "wrongpass" -AsPlainText -Force
{ Get-PfxCertificate $protectedCertLocation -Password $pass -ErrorAction Stop } |
Should -Throw -ErrorId "GetPfxCertificateUnknownCryptoError,Microsoft.PowerShell.Commands.GetPfxCertificateCommand"
}
It "Verify CMS message recipient resolution by path" -Skip:(!$IsWindows) {
$errors = $null
$recipient = [System.Management.Automation.CmsMessageRecipient] $certLocation
$recipient.Resolve($ExecutionContext.SessionState, "Encryption", [ref] $errors)
$recipient.Certificates.Count | Should -Be 1
$recipient.Certificates[0].Subject | Should -Match 'CN=MyDataEnciphermentCert'
}
It "Verify CMS message recipient resolution by cert" -Skip:(!$IsWindows) {
$errors = $null
$cert = Get-PfxCertificate $certLocation
$recipient = [System.Management.Automation.CmsMessageRecipient] $cert
$recipient.Resolve($ExecutionContext.SessionState, "Encryption", [ref] $errors)
$recipient.Certificates.Count | Should -Be 1
$recipient.Certificates[0].Subject | Should -Match 'CN=MyDataEnciphermentCert'
}
It "Verify a CMS message can be protected / unprotected" -Skip:(!$IsWindows) {
$protected = "Hello World","How are you?" | Protect-CmsMessage -To $certLocation
$protected.IndexOf("-----BEGIN CMS-----") | Should -Be 0
$message = $protected | Get-CmsMessage
$message.Recipients.Count | Should -Be 1
$message.Recipients[0].IssuerName | Should -Be "CN=MyDataEnciphermentCert"
$expected = "Hello World" + [System.Environment]::NewLine + "How are you?"
$decrypted = $message | Unprotect-CmsMessage -To $certLocation
$decrypted | Should -Be $expected
$decrypted = $protected | Unprotect-CmsMessage -To $certLocation
$decrypted | Should -Be $expected
}
}
Describe "CmsMessage cmdlets thorough tests" -Tags "Feature" {
BeforeAll{
if($IsWindows)
{
if (-not (Install-TestCertificates) ) {
$SetupFailure = $true
} else {
Push-Location Cert:\
$SetupFailure = $false
}
}
else
{
# Skip for non-Windows platforms
$defaultParamValues = $PSDefaultParameterValues.Clone()
$PSDefaultParameterValues = @{ "it:skip" = $true }
}
}
AfterAll {
if($IsWindows -and -not $SetupFailure)
{
Remove-TestCertificates
}
else
{
if ($defaultParamValues -ne $null) {
$global:PSDefaultParameterValues = $defaultParamValues
}
}
}
It "Verify message recipient resolution by Base64Cert" {
$certContent = "
-----BEGIN CERTIFICATE-----
MIIDXTCCAkWgAwIBAgIQRTsRwsx0LZBHrx9z5Dag2zANBgkqhkiG9w0BAQUFADAh
MR8wHQYDVQQDDBZNeURhdGFFbmNpcGhlcm1lbnRDZXJ0MCAXDTE0MDcyNTIyMjkz
OVoYDzMwMTQwNzI1MjIzOTM5WjAhMR8wHQYDVQQDDBZNeURhdGFFbmNpcGhlcm1l
bnRDZXJ0MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAx3SuShUvnRqn
tYOIouJdP3wPZ5rtDi2KYPurpngGNZjM0EGDTrnhmEAI8DL4Kp6n/zz1mYVoX73+
6uCpZX/13VDXg1neebJ261XpBX6FzxtclIQr8ywdUtrEgCnUAhgqgvO1Wwm4ogNR
tWGCGkmlnqyaoV1j/V4KSn4WvKqSUIOZm0umGCTtNAJ6VtdpYO+uxxnRAapPUCY+
qQ7DFzTUECIo1lMlBcuMiXj6NSFr4/D7ltkZ27jCdsZmzI7ZvRnDlfSYTPQnAO/E
0uYn9uyKY/xfngWkUX/pe+j+10Lm1ypbASrj2Ezgf0KeZRXBwqKUOLhKheEmBJ18
rLV27qwHeQIDAQABo4GOMIGLMA4GA1UdDwEB/wQEAwIEMDAUBgNVHSUEDTALBgkr
BgEEAYI3UAEwRAYJKoZIhvcNAQkPBDcwNTAOBggqhkiG9w0DAgICAIAwDgYIKoZI
hvcNAwQCAgCAMAcGBSsOAwIHMAoGCCqGSIb3DQMHMB0GA1UdDgQWBBRIyIzwInLJ
3B+FajVUFMACf1hrxjANBgkqhkiG9w0BAQUFAAOCAQEAfFt4rmUmWfCbbwi2mCrZ
Osq0lfVNUiZ+iLlEKga4VAI3sJZRtErnVM70eXUt7XpRaOdIfxjuXFpsgc37KyLi
ByCORLuRC0itZVs3aba48opfMDXivxBy0ngqCPPLQsyaN9K7WnpvYV1QxiudYwwU
8U5rFmzlwNLvc3XiyoGWaVZluk2DIJawQ5QYAU9/NMBBCbPHjTG7k0l4cpcEC+Ex
od3RlO6/MOYuK2WB4VTxKsV80EdA3ljlu7Td8P4movnrbB4rG4wpCpk05eREkg/5
Y54Ilo9m5OSAWtdx4yfS779eebLgUs3P+dk6EKwovXMokVveZA8cenIp3QkqSpeT
cQ==
-----END CERTIFICATE-----
"
$errors = $null
$recipient = [System.Management.Automation.CmsMessageRecipient] $certContent
$recipient.Resolve($ExecutionContext.SessionState, "Encryption", [ref] $errors)
$recipient.Certificates.Count | Should -Be 1
$recipient.Certificates[0].Subject | Should -Match 'CN=MyDataEnciphermentCert'
}
It "Verify wildcarded recipient resolution by path [Decryption]" {
if ($SetupFailure) { Set-ItResult -Inconclusive -Because "Test certificates are not installed"}
$errors = $null
$recipient = [System.Management.Automation.CmsMessageRecipient] ((Get-GoodCertificateLocation) + "*")
$recipient.Resolve($ExecutionContext.SessionState, "Decryption", [ref] $errors)
# Should have resolved single cert
$recipient.Certificates.Count | Should -Be 1
}
It "Verify wildcarded recipient resolution by path [Encryption]" {
if ($SetupFailure) { Set-ItResult -Inconclusive -Because "Test certificates are not installed"}
$errors = $null
$recipient = [System.Management.Automation.CmsMessageRecipient] ((Get-GoodCertificateLocation) + "*")
$recipient.Resolve($ExecutionContext.SessionState, "Encryption", [ref] $errors)
$recipient.Certificates.Count | Should -Be 1
}
It "Verify resolution by directory" {
if ($SetupFailure) { Set-ItResult -Inconclusive -Because "Test certificates are not installed"}
$protectedEventLoggingCertPath = Join-Path $TestDrive ProtectedEventLoggingDir
$null = New-Item -ItemType Directory $protectedEventLoggingCertPath -Force
Copy-Item (Get-GoodCertificateLocation) $protectedEventLoggingCertPath
Copy-Item (Get-GoodCertificateLocation) (Join-Path $protectedEventLoggingCertPath "SecondCert.pfx")
Copy-Item (Get-GoodCertificateLocation) (Join-Path $protectedEventLoggingCertPath "ThirdCert.pfx")
$errors = $null
$recipient = [System.Management.Automation.CmsMessageRecipient] $protectedEventLoggingCertPath
$recipient.Resolve($ExecutionContext.SessionState, "Decryption", [ref] $errors)
$recipient.Certificates.Count | Should -Be 1
}
It "Verify resolution by thumbprint" {
if ($SetupFailure) { Set-ItResult -Inconclusive -Because "Test certificates are not installed"}
$errors = $null
$recipient = [System.Management.Automation.CmsMessageRecipient] (Get-GoodCertificateObject).Thumbprint
$recipient.Resolve($ExecutionContext.SessionState, "Decryption", [ref] $errors)
# "Should have certs from thumbprint in 'My' store"
$recipient.Certificates.Count | Should -Be 1
$recipient.Certificates[0].Thumbprint | Should -Be (Get-GoodCertificateObject).Thumbprint
}
It "Verify resolution by subject name" {
if ($SetupFailure) { Set-ItResult -Inconclusive -Because "Test certificates are not installed"}
$errors = $null
$recipient = [System.Management.Automation.CmsMessageRecipient] (Get-GoodCertificateObject).Subject
$recipient.Resolve($ExecutionContext.SessionState, "Decryption", [ref] $errors)
$recipient.Certificates.Count | Should -Be 1
$recipient.Certificates[0].Thumbprint | Should -Be (Get-GoodCertificateObject).Thumbprint
}
It "Verify error when no cert found in encryption for encryption" {
if ($SetupFailure) { Set-ItResult -Inconclusive -Because "Test certificates are not installed"}
$errors = $null
$recipient = [System.Management.Automation.CmsMessageRecipient] "SomeCertificateThatDoesNotExist*"
$recipient.Resolve($ExecutionContext.SessionState, "Encryption", [ref] $errors)
$errors.Count | Should -Be 1
$errors[0].FullyQualifiedErrorId | Should -Be "NoCertificateFound"
}
It "Verify error when encrypting to non-wildcarded identifier for decryption" {
if ($SetupFailure) { Set-ItResult -Inconclusive -Because "Test certificates are not installed"}
$errors = $null
$recipient = [System.Management.Automation.CmsMessageRecipient] "SomeCertificateThatDoesNotExist"
$recipient.Resolve($ExecutionContext.SessionState, "Decryption", [ref] $errors)
$errors.Count | Should -Be 1
$errors[0].FullyQualifiedErrorId | Should -Be "NoCertificateFound"
}
It "Verify error when encrypting to wrong cert" {
if ($SetupFailure) { Set-ItResult -Inconclusive -Because "Test certificates are not installed"}
$errors = $null
$recipient = [System.Management.Automation.CmsMessageRecipient] (Get-BadCertificateObject).Thumbprint
$recipient.Resolve($ExecutionContext.SessionState, "Encryption", [ref] $errors)
$errors.Count | Should -Be 1
$errors[0].FullyQualifiedErrorId | Should -Be "CertificateCannotBeUsedForEncryption"
}
It "Verify no error when encrypting to wildcarded identifier for decryption" {
if ($SetupFailure) { Set-ItResult -Inconclusive -Because "Test certificates are not installed"}
$errors = $null
$recipient = [System.Management.Automation.CmsMessageRecipient] "SomeCertificateThatDoesNotExist*"
$recipient.Resolve($ExecutionContext.SessionState, "Decryption", [ref] $errors)
$errors | Should -Be $null
$recipient.Certificates.Count | Should -Be 0
}
It "Verify Protect-CmsMessage emits recipient errors" {
if ($SetupFailure) { Set-ItResult -Inconclusive -Because "Test certificates are not installed"}
{ "Hello World" | Protect-CmsMessage -To "SomeThumbprintThatDoesNotExist" -ErrorAction Stop } |
Should -Throw -ErrorId "NoCertificateFound,Microsoft.PowerShell.Commands.ProtectCmsMessageCommand"
}
It "Verify CmsMessage cmdlets works with paths" {
if ($SetupFailure) { Set-ItResult -Inconclusive -Because "Test certificates are not installed"}
try {
$randomNum = Get-Random -Minimum 1000 -Maximum 9999
$tempPath = Join-Path $TestDrive "$randomNum-Path-Test-File"
$encryptedPath = $tempPath + ".encrypted.txt"
"Hello World","How are you?" | Set-Content $tempPath
Protect-CmsMessage -Path $tempPath -To (Get-GoodCertificateLocation) -OutFile $encryptedPath
$message = Get-CmsMessage -LiteralPath $encryptedPath
$message.Recipients.Count | Should -Be 1
$message.Recipients[0].IssuerName | Should -Be "CN=MyDataEnciphermentCert"
$expected = "Hello World" + [System.Environment]::NewLine + "How are you?" + [System.Environment]::NewLine
$decrypted = $message | Unprotect-CmsMessage -To (Get-GoodCertificateLocation)
$decrypted | Should -Be $expected
$decrypted = Unprotect-CmsMessage -Path $encryptedPath -To (Get-GoodCertificateLocation)
$decrypted | Should -Be $expected
} finally {
Remove-Item $tempPath, $encryptedPath -Force -ErrorAction SilentlyContinue
}
}
It "Verify Unprotect-CmsMessage works with local store" {
if ($SetupFailure) { Set-ItResult -Inconclusive -Because "Test certificates are not installed"}
try {
$randomNum = Get-Random -Minimum 1000 -Maximum 9999
$tempPath = Join-Path $TestDrive "$randomNum-Path-Test-File"
"Hello World" | Protect-CmsMessage -To (Get-GoodCertificateLocation) -OutFile $tempPath
# Decrypt using $importedCert in the Cert store
$decrypted = Unprotect-CmsMessage -Path $tempPath
$decrypted | Should -Be "Hello World"
} finally {
Remove-Item $tempPath -Force -ErrorAction SilentlyContinue
}
}
It "Verify Unprotect-CmsMessage emits recipient errors" {
if ($SetupFailure) { Set-ItResult -Inconclusive -Because "Test certificates are not installed"}
{ "" | Unprotect-CmsMessage -To "SomeThumbprintThatDoesNotExist" -IncludeContext -ErrorAction Stop } |
Should -Throw -ErrorId "NoCertificateFound,Microsoft.PowerShell.Commands.UnprotectCmsMessageCommand"
}
It "Verify failure to extract Ascii armor generates an error [Unprotect-CmsMessage]" {
if ($SetupFailure) { Set-ItResult -Inconclusive -Because "Test certificates are not installed"}
{ "Hello World" | Unprotect-CmsMessage -ErrorAction Stop } |
Should -Throw -ErrorId "InputContainedNoEncryptedContentIncludeContext,Microsoft.PowerShell.Commands.UnprotectCmsMessageCommand"
}
It "Verify failure to extract Ascii armor generates an error [Get-CmsMessage]" {
if ($SetupFailure) { Set-ItResult -Inconclusive -Because "Test certificates are not installed"}
{ "Hello World" | Get-CmsMessage -ErrorAction Stop } |
Should -Throw -ErrorId "InputContainedNoEncryptedContent,Microsoft.PowerShell.Commands.GetCmsMessageCommand"
}
It "Verify 'Unprotect-CmsMessage -IncludeContext' with no encrypted input" {
if ($SetupFailure) { Set-ItResult -Inconclusive -Because "Test certificates are not installed"}
# Should have round-tripped content
$result = "Hello World" | Unprotect-CmsMessage -IncludeContext
$result | Should -Be "Hello World"
}
It "Verify Unprotect-CmsMessage lets you include context" {
if ($SetupFailure) { Set-ItResult -Inconclusive -Because "Test certificates are not installed"}
$protected = "Hello World" | Protect-CmsMessage -To (Get-GoodCertificateLocation)
$adjustedProtected = "Pre content" + [System.Environment]::NewLine + $protected + [System.Environment]::NewLine + "Post content"
$decryptedNoContext = $adjustedProtected | Unprotect-CmsMessage -To (Get-GoodCertificateLocation)
$decryptedWithContext = $adjustedProtected | Unprotect-CmsMessage -To (Get-GoodCertificateLocation) -IncludeContext
$decryptedNoContext | Should -Be "Hello World"
$expected = "Pre content" + [System.Environment]::NewLine + "Hello World" + [System.Environment]::NewLine + "Post content"
$decryptedWithContext | Should -Be $expected
}
It "Verify Unprotect-CmsMessage treats event logs as a first class citizen" {
if ($SetupFailure) { Set-ItResult -Inconclusive -Because "Test certificates are not installed"}
$protected = "Encrypted Message1","Encrypted Message2" | Protect-CmsMessage -To (Get-GoodCertificateLocation)
$virtualEventLog = Get-WinEvent Microsoft-Windows-PowerShell/Operational -MaxEvents 1
$savedId = $virtualEventLog.Id
$virtualEventLog.Message = $protected
$expected = "Encrypted Message1" + [System.Environment]::NewLine + "Encrypted Message2"
$decrypted = $virtualEventLog | Unprotect-CmsMessage -To (Get-GoodCertificateLocation)
$decrypted | Should -Be $expected
$processed = $virtualEventLog | Unprotect-CmsMessage -To (Get-GoodCertificateLocation) -IncludeContext
$processed.Id | Should -Be $savedId
$processed.Message | Should -Be $expected
}
# Pending due to #3847
It "Verify -DocumentEncryptionCert parameter works" -Pending {
$foundCerts = Get-ChildItem Cert:\CurrentUser -Recurse -DocumentEncryptionCert
# Validate they all match the EKU
$correctMatching = $foundCerts | Where-Object {
($_.EnhancedKeyUsageList.Count -gt 0) -and
($_.EnhancedKeyUsageList[0].ObjectId -eq '1.3.6.1.4.1.311.80.1')
}
# "All Document Encryption Cert should have had correct EKU"
@($foundCerts).Count | Should -Be @($correctMatching).Count
}
It "Verify protect message using OutString" {
if ($SetupFailure) { Set-ItResult -Inconclusive -Because "Test certificates are not installed"}
$protected = Get-Process -Id $PID | Protect-CmsMessage -To (Get-GoodCertificateLocation)
$decrypted = $protected | Unprotect-CmsMessage -To (Get-GoodCertificateLocation)
# Should have had PID in output
$decrypted | Should -Match $PID
}
}
|