Debian Codegraph @

test/integration/targets/win_app_control/library/wdac_setup.ps1

raw
1 #!powershell
2
3 #AnsibleRequires -CSharpUtil Ansible.Basic
4
5 $spec = @{
6 options = @{
7 cert_pw = @{
8 type = 'str'
9 required = $true
10 no_log = $true
11 }
12 remote_tmp_dir = @{
13 type = 'path'
14 required = $true
15 }
16 }
17 }
18
19 $module = [Ansible.Basic.AnsibleModule]::Create($args, $spec)
20
21 $testPrefix = 'Ansible-WDAC'
22 $certPassword = ConvertTo-SecureString -String $module.Params.cert_pw -Force -AsPlainText
23 $remoteTmpDir = $module.Params.remote_tmp_dir
24
25 $module.Result.changed = $true
26
27 $enhancedKeyUsage = [Security.Cryptography.OidCollection]::new()
28 $null = $enhancedKeyUsage.Add('1.3.6.1.5.5.7.3.3') # Code Signing
29 $caParams = @{
30 Extension = @(
31 [Security.Cryptography.X509Certificates.X509BasicConstraintsExtension]::new($true, $false, 0, $true),
32 [Security.Cryptography.X509Certificates.X509KeyUsageExtension]::new('KeyCertSign', $false),
33 [Security.Cryptography.X509Certificates.X509EnhancedKeyUsageExtension ]::new($enhancedKeyUsage, $false)
34 )
35 CertStoreLocation = 'Cert:\CurrentUser\My'
36 NotAfter = (Get-Date).AddDays(1)
37 Type = 'Custom'
38 }
39 $ca = New-SelfSignedCertificate @caParams -Subject "CN=$testPrefix-Root"
40
41 $certParams = @{
42 CertStoreLocation = 'Cert:\CurrentUser\My'
43 KeyUsage = 'DigitalSignature'
44 TextExtension = @("2.5.29.37={text}1.3.6.1.5.5.7.3.3", "2.5.29.19={text}")
45 Type = 'Custom'
46 }
47 $cert = New-SelfSignedCertificate @certParams -Subject "CN=$testPrefix-Signed" -Signer $ca
48 $null = $cert | Export-PfxCertificate -Password $certPassword -FilePath "$remoteTmpDir\signing.pfx"
49 $cert.Export('Cert') | Set-Content -LiteralPath "$remoteTmpDir\signing.cer" -Encoding Byte
50
51 $certUntrusted = New-SelfSignedCertificate @certParams -Subject "CN=$testPrefix-Untrusted"
52 $null = $certUntrusted | Export-PfxCertificate -Password $certPassword -FilePath "$remoteTmpDir\untrusted.pfx"
53
54 $caWithoutKey = [System.Security.Cryptography.X509Certificates.X509Certificate2]::new($ca.Export('Cert'))
55 $certWithoutKey = [System.Security.Cryptography.X509Certificates.X509Certificate2]::new($cert.Export('Cert'))
56
57 Remove-Item -LiteralPath "Cert:\CurrentUser\My\$($ca.Thumbprint)" -DeleteKey -Force
58 Remove-Item -LiteralPath "Cert:\CurrentUser\My\$($cert.Thumbprint)" -DeleteKey -Force
59 Remove-Item -LiteralPath "Cert:\CurrentUser\My\$($certUntrusted.Thumbprint)" -DeleteKey -Force
60
61 $root = Get-Item -LiteralPath Cert:\LocalMachine\Root
62 $root.Open('ReadWrite')
63 $root.Add($caWithoutKey)
64 $root.Dispose()
65
66 $trustedPublisher = Get-Item -LiteralPath Cert:\LocalMachine\TrustedPublisher
67 $trustedPublisher.Open('ReadWrite')
68 $trustedPublisher.Add($certWithoutKey)
69 $trustedPublisher.Dispose()
70
71 $module.Result.ca_thumbprint = $caWithoutKey.Thumbprint
72 $module.Result.thumbprint = $certWithoutKey.Thumbprint
73 $module.Result.untrusted_thumbprint = $certUntrusted.Thumbprint
74
75 $module.ExitJson()