-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathClearSign.psm1
209 lines (187 loc) · 6.36 KB
/
ClearSign.psm1
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
<#
.Synopsis
Clear OpenPGP Signs a text file or string.
.DESCRIPTION
Clear OpenPGP Signs a text file or string. In multi string a carrige return must be added to to the end
or an empty line must be present for proper formating.
.EXAMPLE
New-PGPClearSignature -SecretKey $seckey -File C:\evidence.txt -PassPhrase (Read-Host -AsSecureString) -OutFile C:\evidence.asc
.EXAMPLE
$seckey = Get-PGPSecretKey -KeyRing $env:APPDATA\gnupg\secring.gpg -UserId "darkoperator"
PS C:\> $message = New-PGPClearSignature -SecretKey $seckey -Text "This is my ubber secret message`n" -PassPhrase (Read-Host -AsSecureString)
PS C:\> $creds = Get-Credential [email protected]
PS C:\> $param = @{
SmtpServer = 'smtp.gmail.com'
Port = 587
UseSsl = $true
Credential = $creds
From = '[email protected]'
To = '[email protected]'
Subject = 'My Secret Message'
Body = "$($message -join "`r`n")"
}
PS C:\> Send-MailMessage @param -Verbose
Sends a Signed OpenPGP Email
#>
function New-PGPClearSignature
{
[CmdletBinding(DefaultParameterSetName = 'File')]
Param
(
# Param1 help description
[Parameter(Mandatory=$true,
ValueFromPipelineByPropertyName=$true)]
[Org.BouncyCastle.Bcpg.OpenPgp.PgpSecretKey]$SecretKey,
[Parameter(Mandatory=$true,
ValueFromPipelineByPropertyName=$true,
ParameterSetName='File')]
[ValidateScript({Test-Path -Path $_})]
[string]$File,
[Parameter(Mandatory=$true,
ValueFromPipelineByPropertyName=$true,
ParameterSetName='Text')]
[string]$Text,
[Parameter(Mandatory=$true,
ValueFromPipelineByPropertyName=$true,
HelpMessage = "Secure String representing the passphase for the key.")]
[securestring]$PassPhrase,
[Parameter(Mandatory=$false,
ValueFromPipelineByPropertyName=$true)]
[string]$OutFile,
[Parameter(Mandatory=$false,
ValueFromPipelineByPropertyName=$true)]
[ValidateSet('SHA1', "SHA256",'SHA384','SHA512','RIPEMD160')]
[string]$HashAlgorithm
)
Begin
{
}
Process
{
if ($OutFile)
{
$outstream = [System.IO.File]::Create(($ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($OutFile)))
}
else
{
$outstream = New-Object -TypeName System.IO.MemoryStream
}
switch ($PsCmdlet.ParameterSetName)
{
'File'{
Write-Verbose "Signing file $($File)"
$filecontent = Get-Content $file -Raw
$enc = [system.Text.Encoding]::ASCII
$data1 = $enc.GetBytes($filecontent)
$instream = New-Object System.IO.MemoryStream
$instream.Write($data1,0,$data1.Length)
$instream.Position = 0
}
'Text' {
Write-Verbose "Signing provided string"
$enc = [system.Text.Encoding]::ASCII
$data1 = $enc.GetBytes($Text)
$instream = New-Object System.IO.MemoryStream
$instream.Write($data1,0,$data1.Length)
$instream.Position = 0
}
}
if ($HashAlgorithm)
{
Write-Verbose "Using hash algorithm $($HashAlgorithm)"
}
else
{
Write-Verbose "Using preferred hash algorithm $($SecretKey.PreferedHash[0])"
$HashAlgorithm = $SecretKey.PreferedHash[0]
}
[PGPHelper.ClearSignedFileProcessor]::SignFile($instream,
$SecretKey,
$outstream,
([Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($PassPhrase))),
$HashAlgorithm)
if ($OutFile)
{
Write-Verbose "Signed content created at $($OutFile)"
$outstream.Close()
}
else
{
Write-Verbose "Output of signed content sent to Console"
[void]$outstream.Seek(0,"Begin")
$readStream = New-Object System.IO.StreamReader $outstream
while ($readStream.Peek() -ne -1)
{
$readStream.ReadLine()
}
}
}
End
{
}
}
<#
.Synopsis
Verifies an OpenPGP clearsigned file or string.
.DESCRIPTION
Verifies an OpenPGP clearsigned file or string.
.EXAMPLE
Confirm-PGPClearSignature -File C:\evidence.asc -KeyRing $env:APPDATA\gnupg\pubring.gpg
Valid : True
Created : 10/4/2013 1:10:16 AM
KeyID : 48E6AA1C3ED92AC3
HashAlgorithm : Sha512
Version : 4
Signature : Org.BouncyCastle.Bcpg.OpenPgp.PgpSignature
#>
function Confirm-PGPClearSignature
{
[CmdletBinding(DefaultParameterSetName='File')]
Param
(
[Parameter(Mandatory=$true,
ValueFromPipelineByPropertyName=$true,
ParameterSetName='File')]
[ValidateScript({Test-Path $_})]
[string]$File,
[Parameter(Mandatory=$true,
ValueFromPipelineByPropertyName=$true,
ParameterSetName='Text')]
[string]$Text,
[Parameter(Mandatory=$true,
ValueFromPipelineByPropertyName=$true,
Position=0)]
[ValidateScript({Test-Path $_})]
$KeyRing
)
Begin
{
}
Process
{
switch ($PsCmdlet.ParameterSetName)
{
'File'{
$infile = [System.IO.File]::OpenRead(($ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($File)))
$keyfile = [System.IO.File]::OpenRead(($ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($KeyRing)))
[PGPHelper.ClearSignedFileProcessor]::VerifyFile($infile,$keyfile)
$infile.Close()
$keyfile.Close()
}
'Text' {
$enc = [system.Text.Encoding]::ASCII
$data1 = $enc.GetBytes($Text)
$instream = New-Object System.IO.MemoryStream
$instream.Write($data1,0,$data1.Length)
$instream.Position = 0
$keyfile = [System.IO.File]::OpenRead(($ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($KeyRing)))
[PGPHelper.ClearSignedFileProcessor]::VerifyFile($instream,$keyfile)
$keyfile.Close()
}
}
}
End
{
}
}