forked from darkoperator/Posh-OpenPGP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathposh-openpgp.psm1
2659 lines (2347 loc) · 98.7 KB
/
posh-openpgp.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
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<#
.Synopsis
Get a specified or all private keys from a OpenPGP key ring file.
.DESCRIPTION
Get a specified or all private keys from a OpenPGP key ring file.
.EXAMPLE
Get-PGPSecretKey -KeyRing $env:APPDATA\gnupg\secring.gpg -Id DCC9422A3F0DB692
Id : DCC9422A3F0DB692
PreferedSymmetric : {AES256, AES192, AES128, CAST5...}
PreferedHash : {Sha256, SHA1, Sha384, Sha512...}
PreferedCompression : {ZLib, Bzip2, Zip}
ExpirationDate : 10/3/2015 6:14:55 PM
IsSigningKey : True
IsMasterKey : True
KeyEncryptionAlgorithm : Cast5
KeyId : -2537424165832640878
PublicKey : Org.BouncyCastle.Bcpg.OpenPgp.PgpPublicKey
UserIds : {Carlos Perez <[email protected]>}
UserAttributes : {}
.EXAMPLE
Get-PGPSecretKey -KeyRing $env:APPDATA\gnupg\secring.gpg
Id : DCC9422A3F0DB692
PreferedSymmetric : {AES256, AES192, AES128, CAST5...}
PreferedHash : {Sha256, SHA1, Sha384, Sha512...}
PreferedCompression : {ZLib, Bzip2, Zip}
ExpirationDate : 10/3/2015 6:14:55 PM
IsSigningKey : True
IsMasterKey : True
KeyEncryptionAlgorithm : Cast5
KeyId : -2537424165832640878
PublicKey : Org.BouncyCastle.Bcpg.OpenPgp.PgpPublicKey
UserIds : {Carlos Perez <[email protected]>}
UserAttributes : {}
Id : 48E6AA1C3ED92AC3
PreferedSymmetric : {AES256, AES192, AES128, CAST5...}
PreferedHash : {Sha256, SHA1, Sha384, Sha512...}
PreferedCompression : {ZLib, Bzip2, Zip}
ExpirationDate : 7/1/2016 2:49:15 AM
IsSigningKey : True
IsMasterKey : True
KeyEncryptionAlgorithm : Cast5
KeyId : 5253073053664488131
PublicKey : Org.BouncyCastle.Bcpg.OpenPgp.PgpPublicKey
UserIds : {Carlos Perez <[email protected]>}
UserAttributes : {}
Id : 1F09E81ACCFF0A6A
PreferedSymmetric : {AES256, AES192, AES128, CAST5...}
PreferedHash : {Sha256, SHA1, Sha384, Sha512...}
PreferedCompression : {ZLib, Bzip2, Zip}
ExpirationDate : 0
IsSigningKey : True
IsMasterKey : True
KeyEncryptionAlgorithm : Cast5
KeyId : 2236573891772222058
PublicKey : Org.BouncyCastle.Bcpg.OpenPgp.PgpPublicKey
UserIds : {Marta Perez <[email protected]>}
UserAttributes : {}
Id : 52FB7527672C924D
PreferedSymmetric : {AES256, AES192, AES128, CAST5...}
PreferedHash : {Sha256, SHA1, Sha384, Sha512...}
PreferedCompression : {ZLib, Bzip2, Zip}
ExpirationDate : 0
IsSigningKey : True
IsMasterKey : True
KeyEncryptionAlgorithm : Cast5
KeyId : 5979501742359614029
PublicKey : Org.BouncyCastle.Bcpg.OpenPgp.PgpPublicKey
UserIds : {Marely Del Valle <[email protected]>}
UserAttributes : {}
.EXAMPLE
Get-PGPSecretKey -KeyRing $env:APPDATA\gnupg\secring.gpg -UserId "Carlos"
Id : DCC9422A3F0DB692
PreferedSymmetric : {AES256, AES192, AES128, CAST5...}
PreferedHash : {Sha256, SHA1, Sha384, Sha512...}
PreferedCompression : {ZLib, Bzip2, Zip}
ExpirationDate : 10/3/2015 6:14:55 PM
IsSigningKey : True
IsMasterKey : True
KeyEncryptionAlgorithm : Cast5
KeyId : -2537424165832640878
PublicKey : Org.BouncyCastle.Bcpg.OpenPgp.PgpPublicKey
UserIds : {Carlos Perez <[email protected]>}
UserAttributes : {}
#>
function Get-PGPSecretKey
{
[CmdletBinding(DefaultParameterSetName = 'All')]
[OutputType([Org.BouncyCastle.Bcpg.OpenPgp.PgpSecretKey])]
Param
(
# Param1 help description
[Parameter(Mandatory=$true,
ValueFromPipelineByPropertyName=$true,
Position=0)]
[ValidateScript({Test-Path $_})]
[string]$SecretKeyBundle,
[Parameter(Mandatory=$false,
ValueFromPipelineByPropertyName=$true,
Position=2)]
[Parameter(ParameterSetName='Id')]
[string]$Id,
[Parameter(Mandatory=$false,
ValueFromPipelineByPropertyName=$true,
Position=2)]
[Parameter(ParameterSetName='UserId')]
$UserId,
[Parameter(Mandatory=$false,
ValueFromPipelineByPropertyName=$true,
Position=2)]
[Parameter(ParameterSetName='All')]
[switch]$All = $true
)
Begin
{
$compressionalgos = @{
1 = 'Zip'
2 = 'ZLib'
3 = 'Bzip2'
}
$symetricalgos = @{
10 = 'Towfish'
9 = 'AES256'
8 = 'AES192'
7 = 'AES128'
6 = 'DES'
5 = 'SAFER'
4 = 'Blowfish'
3 = 'CAST5'
2 = '3DES'
1 = 'IDEA'
}
$hashalgos = @{
1 = 'MD5'
2 = 'SHA1'
3 = 'RipeMD160'
4 = 'DoubleSha'
5 = 'MD2'
6 = 'Tiger192'
7 = 'Haval5pass160'
8 = 'Sha256'
9 = 'Sha384'
10 = 'Sha512'
11 = 'Sha224'
}
}
Process
{
[system.io.stream]$stream = [system.io.File]::OpenRead((Resolve-Path $SecretKeyBundle).Path)
# Decode key ring
$instream = [Org.BouncyCastle.Bcpg.OpenPgp.PgpUtilities]::GetDecoderStream($stream)
try
{
$PrivKeyBundle = New-Object -TypeName Org.BouncyCastle.Bcpg.OpenPgp.PgpSecretKeyRingBundle -ArgumentList $instream
if (!($PrivKeyBundle))
{
throw "$($SecretKeyBundle) is not a valid key ring."
}
switch ($PsCmdlet.ParameterSetName)
{
'Id' {
$idlongformat = ($Id | foreach {[Convert]::ToInt64($_,16)}) -join ''
$kp = $PrivKeyBundle.GetSecretKey($idlongformat)
$secpubsigs = $kp.PublicKey.GetSignatures()
$PreferedHashAlgos = @()
$PreferedSymAlgos = @()
$PreferedCompressionAlgos = @()
# RFC 4880 5.2.3.10. Signature Expiration Time
if ($kp.PublicKey.ValidDays -ne 0)
{
$ValidTime = $kp.PublicKey.CreationTime.AddDays($kp.PublicKey.ValidDays)
}
else
{
$ValidTime = 0
}
foreach($sig in $secpubsigs)
{
# Make sure we look at the subpackets for the key and not signers
if ($sig.KeyId -eq $kp.KeyId)
{
foreach($Subpckt in $sig.GetHashedSubPackets())
{
if ([datetime]::UtcNow -lt $ValidTime -or $ValidTime -eq 0)
{
$compalgos = $Subpckt.GetPreferredCompressionAlgorithms()
foreach ($calgo in $compalgos)
{
$PreferedCompressionAlgos += $compressionalgos[$calgo]
}
$symalgost = $Subpckt.GetPreferredSymmetricAlgorithms()
foreach ($salgo in $symalgost)
{
$PreferedSymAlgos += $symetricalgos[$salgo]
}
$hashgost = $Subpckt.GetPreferredHashAlgorithms()
foreach ($halgo in $hashgost)
{
$PreferedHashAlgos += $hashalgos[$halgo]
}
}
else
{
Write-Warning "Subkey $(($sig.KeyId | foreach { $_.ToString('X2') }) -join '') has expired"
}
}
}
}
if ($kp)
{
# Add some additional properties to the object
Add-Member -InputObject $kp -MemberType NoteProperty -Name 'Id' -Value (($kp.KeyId | foreach { $_.ToString('X2') }) -join '')
Add-Member -InputObject $kp -MemberType NoteProperty -Name 'PreferedSymmetric' -Value $PreferedSymAlgos
Add-Member -InputObject $kp -MemberType NoteProperty -Name 'PreferedHash' -Value $PreferedHashAlgos
Add-Member -InputObject $kp -MemberType NoteProperty -Name 'PreferedCompression' -Value $PreferedCompressionAlgos
Add-Member -InputObject $kp -MemberType NoteProperty -Name 'ExpirationDate' -Value $ValidTime
$kp
}
}
'UserId' {
$keyring = $PrivKeyBundle.GetKeyRings($UserId,$true,$true)
foreach($ring in $KeyRing)
{
$kp = $ring.GetSecretKey()
$secpubsigs = $kp.PublicKey.GetSignatures()
$PreferedHashAlgos = @()
$PreferedSymAlgos = @()
$PreferedCompressionAlgos = @()
# RFC 4880 5.2.3.10. Signature Expiration Time
if ($kp.PublicKey.ValidDays -ne 0)
{
$ValidTime = $kp.PublicKey.CreationTime.AddDays($kp.PublicKey.ValidDays)
}
else
{
$ValidTime = 0
}
foreach($sig in $secpubsigs)
{
# Make sure we look at the subpackets for the key and not signers
if ($sig.KeyId -eq $kp.KeyId)
{
foreach($Subpckt in $sig.GetHashedSubPackets())
{
if ([datetime]::UtcNow -lt $ValidTime -or $ValidTime -eq 0)
{
$compalgos = $Subpckt.GetPreferredCompressionAlgorithms()
foreach ($calgo in $compalgos)
{
$PreferedCompressionAlgos += $compressionalgos[$calgo]
}
$symalgost = $Subpckt.GetPreferredSymmetricAlgorithms()
foreach ($salgo in $symalgost)
{
$PreferedSymAlgos += $symetricalgos[$salgo]
}
$hashgost = $Subpckt.GetPreferredHashAlgorithms()
foreach ($halgo in $hashgost)
{
$PreferedHashAlgos += $hashalgos[$halgo]
}
}
else
{
Write-Warning "Subkey $(($sig.KeyId | foreach { $_.ToString('X2') }) -join '') has expired"
}
}
}
}
if ($kp)
{
# Add some additional properties to the object
Add-Member -InputObject $kp -MemberType NoteProperty -Name 'Id' -Value (($kp.KeyId | foreach { $_.ToString('X2') }) -join '')
Add-Member -InputObject $kp -MemberType NoteProperty -Name 'PreferedSymmetric' -Value $PreferedSymAlgos
Add-Member -InputObject $kp -MemberType NoteProperty -Name 'PreferedHash' -Value $PreferedHashAlgos
Add-Member -InputObject $kp -MemberType NoteProperty -Name 'PreferedCompression' -Value $PreferedCompressionAlgos
Add-Member -InputObject $kp -MemberType NoteProperty -Name 'ExpirationDate' -Value $ValidTime
$kp
}
}
}
'All' {
# Get all keyrings from the file
foreach ($keyring in $PrivKeyBundle.GetKeyRings())
{
# Get only the public keys from the key ring
$kp = $keyring.GetSecretKey()
if ($kp)
{
if ($kp.IsSigningKey)
{
$secpubsigs = $kp.PublicKey.GetSignatures()
$PreferedHashAlgos = @()
$PreferedSymAlgos = @()
$PreferedCompressionAlgos = @()
# RFC 4880 5.2.3.10. Signature Expiration Time
if ($kp.PublicKey.ValidDays -ne 0)
{
$ValidTime = $kp.PublicKey.CreationTime.AddDays($kp.PublicKey.ValidDays)
}
else
{
$ValidTime = 0
}
foreach($sig in $secpubsigs)
{
# Make sure we look at the subpackets for the key and not signers
if ($sig.KeyId -eq $kp.KeyId)
{
foreach($Subpckt in $sig.GetHashedSubPackets())
{
if ([datetime]::UtcNow -lt $ValidTime -or $ValidTime -eq 0)
{
$compalgos = $Subpckt.GetPreferredCompressionAlgorithms()
foreach ($calgo in $compalgos)
{
$PreferedCompressionAlgos += $compressionalgos[$calgo]
}
$symalgost = $Subpckt.GetPreferredSymmetricAlgorithms()
foreach ($salgo in $symalgost)
{
$PreferedSymAlgos += $symetricalgos[$salgo]
}
$hashgost = $Subpckt.GetPreferredHashAlgorithms()
foreach ($halgo in $hashgost)
{
$PreferedHashAlgos += $hashalgos[$halgo]
}
}
else
{
Write-Warning "Subkey $(($sig.KeyId | foreach { $_.ToString('X2') }) -join '') has expired"
}
}
}
}
#Add some additional properties to the object
Add-Member -InputObject $kp -MemberType NoteProperty -Name 'Id' -Value (($kp.KeyId | foreach { $_.ToString('X2') }) -join '')
Add-Member -InputObject $kp -MemberType NoteProperty -Name 'PreferedSymmetric' -Value $PreferedSymAlgos
Add-Member -InputObject $kp -MemberType NoteProperty -Name 'PreferedHash' -Value $PreferedHashAlgos
Add-Member -InputObject $kp -MemberType NoteProperty -Name 'PreferedCompression' -Value $PreferedCompressionAlgos
Add-Member -InputObject $kp -MemberType NoteProperty -Name 'ExpirationDate' -Value $ValidTime
$kp
}
else
{
$kp
}
}
}
}
}
}
catch
{
$error_message = $_.Exception.Message
if ($error_message -like '*PgpPublicKeyRing expected*')
{
throw 'Key specified is not a public key.'
}
elseif ($error_message -like '*unsupported version*')
{
throw 'File specified is not a OpenPGP Key file.'
}
else
{
Throw "$($error_message)"
}
}
}
End
{
}
}
<#
.Synopsis
Get a specified or all publick keys from a OpenPGP key ring file.
.DESCRIPTION
Get a specified or all publick keys from a OpenPGP key ring file.
.EXAMPLE
Get-PGPPublicKey -KeyRing C:\Users\Carlos\AppData\Roaming\gnupg\pubring.gpg -id 52FB7527672C924D
Id : 52FB7527672C924D
UserIds : {Marely Del Valle <[email protected]>}
Fingerprint : 96C6AA166721D0561F08CB0152FB7527672C924D
PreferedSymmetric : {AES256, AES192, AES128, CAST5...}
PreferedHash : {Sha256, SHA1, Sha384, Sha512...}
PreferedCompression : {ZLib, Bzip2, Zip}
ExpirationDate : 0
Version : 4
CreationTime : 10/3/2013 7:39:43 PM
ValidDays : 0
KeyId : 5979501742359614029
IsEncryptionKey : True
IsMasterKey : True
Algorithm : RsaGeneral
BitStrength : 2048
.EXAMPLE
Get-PGPPublicKey -KeyRing C:\Users\Carlos\AppData\Roaming\gnupg\pubring.gpg -UserId marta,mare
Id : 1F09E81ACCFF0A6A
UserIds : {Marta Perez <[email protected]>}
Fingerprint : 4D3EF95CFA1CF2B9846E87601F09E81ACCFF0A6A
PreferedSymmetric : {AES256, AES192, AES128, CAST5...}
PreferedHash : {Sha256, SHA1, Sha384, Sha512...}
PreferedCompression : {ZLib, Bzip2, Zip}
ExpirationDate : 0
Version : 4
CreationTime : 10/3/2013 7:40:48 PM
ValidDays : 0
KeyId : 2236573891772222058
IsEncryptionKey : True
IsMasterKey : True
Algorithm : RsaGeneral
BitStrength : 2048
Id : 52FB7527672C924D
UserIds : {Marely Del Valle <[email protected]>}
Fingerprint : 96C6AA166721D0561F08CB0152FB7527672C924D
PreferedSymmetric : {AES256, AES192, AES128, CAST5...}
PreferedHash : {Sha256, SHA1, Sha384, Sha512...}
PreferedCompression : {ZLib, Bzip2, Zip}
ExpirationDate : 0
Version : 4
CreationTime : 10/3/2013 7:39:43 PM
ValidDays : 0
KeyId : 5979501742359614029
IsEncryptionKey : True
IsMasterKey : True
Algorithm : RsaGeneral
BitStrength : 2048
#>
function Get-PGPPublicKey
{
[CmdletBinding(DefaultParameterSetName = 'All')]
[OutputType([Org.BouncyCastle.Bcpg.OpenPgp.PgpPublicKey])]
Param
(
[Parameter(Mandatory=$true,
ValueFromPipelineByPropertyName=$true,
Position=0)]
[ValidateScript({Test-Path $_})]
[string]$PublicKeyBundle,
[Parameter(Mandatory=$false,
ValueFromPipelineByPropertyName=$true,
Position=2)]
[Parameter(ParameterSetName='Id')]
[string]$Id,
[Parameter(Mandatory=$false,
ValueFromPipelineByPropertyName=$true,
Position=2)]
[Parameter(ParameterSetName='UserId')]
[string[]]$UserId,
[Parameter(Mandatory=$false,
ValueFromPipelineByPropertyName=$true,
Position=2)]
[Parameter(ParameterSetName='All')]
[switch]$All,
[Parameter(Mandatory=$false,
ValueFromPipelineByPropertyName=$true)]
[switch]$EncryptionOnly,
[Parameter(Mandatory=$false,
ValueFromPipelineByPropertyName=$true)]
[switch]$IncludeRevoked,
[Parameter(Mandatory=$false,
ValueFromPipelineByPropertyName=$true)]
[switch]$IncludeExpired
)
Begin
{
$compressionalgos = @{
1 = 'Zip'
2 = 'ZLib'
3 = 'Bzip2'
}
$symetricalgos = @{
13 = 'Camellia256' # Not supported by BC implementation
12 = 'Camellia192' # Not supported by BC implementation
11 = 'Camellia128' # Not supported by BC implementation
10 = 'Towfish'
9 = 'AES256'
8 = 'AES192'
7 = 'AES128'
6 = 'DES'
5 = 'SAFER'
4 = 'Blowfish'
3 = 'CAST5'
2 = '3DES'
1 = 'IDEA'
}
$hashalgos = @{
1 = 'MD5'
2 = 'SHA1'
3 = 'RipeMD160'
4 = 'DoubleSha'
5 = 'MD2'
6 = 'Tiger192'
7 = 'Haval5pass160'
8 = 'Sha256'
9 = 'Sha384'
10 = 'Sha512'
11 = 'Sha224'
}
}
Process
{
[system.io.stream]$stream = [system.io.File]::OpenRead($PublicKeyBundle)
# Decode key ring
$instream = [Org.BouncyCastle.Bcpg.OpenPgp.PgpUtilities]::GetDecoderStream($stream)
try
{
$PubKeyBundle = New-Object -TypeName Org.BouncyCastle.Bcpg.OpenPgp.PgpPublicKeyRingBundle -ArgumentList $instream
if (!($PubKeyBundle))
{
throw "$($PublicKeyBundle) is not a valid key ring."
}
switch ($PsCmdlet.ParameterSetName)
{
'Id'
{
$idlongformat = ($Id | foreach {[Convert]::ToInt64($_,16)}) -join ''
$keyrings = $PubKeyBundle.GetPublicKeyRing($idlongformat)
if (!($keyrings))
{
Write-Verbose "No key was found for $($Id)."
return
}
}
'UserId'
{
$keyrings = @()
foreach($uid in $UserId)
{
$keyrings += $PubKeyBundle.GetKeyRings($uid,$true,$true)
# check if a keyring was found for the given user id
if ($keyrings -eq $null)
{
Write-host "No keyrings where found for the UserId $($uid)"
continue
}
# Check if we got more than one keyring
if ($keyring.length -gt 1)
{
Write-host "More than one keyring was found for the given UserId $($uid) use Key Id or more precise UserId"
continue
}
}
}
'All'
{
# Get all keyrings from the file
$keyrings = $PubKeyBundle.GetKeyRings()
}
}
foreach($keyring in $keyrings)
{
# Check if the key is revoked
$PublicKeys = $keyring.GetPublicKeys()
$MasterPreferedHashAlgos = @()
$MasterPreferedSymAlgos = @()
$MasterPreferedCompressionAlgos = @()
$MasterUserIDs = @()
# for checking subsignaturs
$MasterKeyID = 0
foreach($PublicKey in $PublicKeys)
{
# save master key id
if ($PublicKey.IsMasterKey)
{
$MasterKeyID = $PublicKey.KeyId
$MasterUserIDs = $PublicKey.GetUserIds()
}
$KeyID = (($PublicKey.KeyId | foreach { $_.ToString('X2') }) -join '')
if($PublicKey.IsRevoked() -and !($IncludeRevoked))
{
Write-Warning "Key with Id $((($PublicKey.KeyId | foreach { $_.ToString('X2') }) -join '')) is revoked, skipping key."
Continue
}
# Check if the key is expired
if (IsExpired($PublicKey) -and !($IncludeExpired))
{
Write-host "Key with Id $($KeyID) is expired, skipping key."
Continue
}
# Select keys depending on their usage
if (($EncryptionOnly) -and !(IsEnCryptionKey($PublicKey)))
{
Continue
}
# We sort by creation time so the prefered algorithms are those from the most
# recent subpacket as specified by the RFC 4880 5.2.3.3
$PubKeySigs = $PublicKey.GetSignatures() | sort-object -Property CreationTime
if (!($PubKeySigs))
{
Write-Verbose "No signatures where founf for $($KeyID)"
}
$PreferedHashAlgos = @()
$PreferedSymAlgos = @()
$PreferedCompressionAlgos = @()
foreach($signature in $PubKeySigs)
{
# Check that it is a self-signed signature either 0x18 or 0x10-0x13
if ($signature.SignatureType -notin @(24,16, 17, 18, 19))
{
Continue
}
# if the signature does not have subpackets we skip it since it will
# not contain the info we want
if (!($signature.HasSubpackets))
{
Continue
}
# Get the signature hashed subpackets and unhashed subpackets
$Hashed = $signature.GetHashedSubPackets()
$Unhashed = $signature.GetUnhashedSubPackets()
# Check if signed by the master key or has not being signed
if ("$($Hashed.GetIssuerKeyId())" -notin @("$($MasterKeyID)", '0'))
{
Continue
}
# if the signature is not signed by the key we skip it
# Get the preffered Symmetric Algorithm
$prefsymm = $Hashed.GetPreferredSymmetricAlgorithms()
if ($prefsymm -ne $null)
{
foreach ($salgo in $prefsymm)
{
$PreferedSymAlgos += $symetricalgos[$salgo]
if ($PublicKey.IsMasterKey)
{
$MasterPreferedSymAlgos += $symetricalgos[$salgo]
}
}
}
# Get the prefered Hashing Algorithms
$prefhash = $Hashed.GetPreferredHashAlgorithms()
if ($prefhash -ne $null)
{
foreach ($halgo in $prefhash)
{
$PreferedHashAlgos += $hashalgos[$halgo]
if ($PublicKey.IsMasterKey)
{
$MasterPreferedHashAlgos += $hashalgos[$halgo]
}
}
}
# Get the prefered Compression Algorithms
$compalgos = $Hashed.GetPreferredCompressionAlgorithms()
if ($compalgos -ne $null)
{
foreach ($calgo in $compalgos)
{
$PreferedCompressionAlgos += $compressionalgos[$calgo]
if ($PublicKey.IsMasterKey)
{
$MasterPreferedCompressionAlgos += $compressionalgos[$calgo]
}
}
}
# Get prefered Compression Algorithms
if (($PreferedHashAlgos.Length -eq 0) -or ($PreferedHashAlgos.Length -eq 0) -or ($PreferedCompressionAlgos.Length -eq 0))
{
Write-Verbose "Did not found prefered algorithms in hashed subpacket for $($KeyID)."
Write-Verbose "Checking unhashed subpackets for prefered algorithms $($KeyID)."
# Get the preffered Symmetric Algorithm
$prefsymm = $Unhashed.GetPreferredSymmetricAlgorithms()
if ($prefsymm -ne $null)
{
foreach ($salgo in $prefsymm)
{
$PreferedSymAlgos += $symetricalgos[$salgo]
if ($PublicKey.IsMasterKey)
{
$MasterPreferedSymAlgos += $symetricalgos[$salgo]
}
}
}
# Get the prefered Hashing Algorithms
$prefhash = $Unhashed.GetPreferredHashAlgorithms()
if ($prefhash -ne $null)
{
foreach ($halgo in $prefhash)
{
$PreferedHashAlgos += $hashalgos[$halgo]
if ($PublicKey.IsMasterKey)
{
$MasterPreferedHashAlgos += $hashalgos[$halgo]
}
}
}
# Get the prefered Compression Algorithms
$compalgos = $Unhashed.GetPreferredCompressionAlgorithms()
if ($compalgos -ne $null)
{
foreach ($calgo in $compalgos)
{
$PreferedCompressionAlgos += $compressionalgos[$calgo]
if ($PublicKey.IsMasterKey)
{
$MasterPreferedCompressionAlgos += $compressionalgos[$calgo]
}
}
}
# If the the key does not have prefered algorithm we will use the master key algorithms
# most implementations set their preferences on the master key at the very least and the
# master key is the first in the list of keys.
if ($PreferedCompressionAlgos.Length -eq 0)
{
Write-Verbose 'Could not find Compression Algorithms using Master Key values'
$PreferedCompressionAlgos = $MasterPreferedCompressionAlgos
}
if ($PreferedSymAlgos.Length -eq 0)
{
Write-Verbose 'Could not find Symmetric Algorithms using Master Key values'
$PreferedSymAlgos = $MasterPreferedSymAlgos
}
if ($PreferedHashAlgos.Length -eq 0)
{
Write-Verbose 'Could not find Hashing Algorithms using Master Key values'
$PreferedHashAlgos = $MasterPreferedHashAlgos
}
}
}
# RFC 4880 5.2.3.10. Signature Expiration Time
if ($PublicKey.ValidDays -ne 0)
{
$ValidTime = $PublicKey.CreationTime.AddDays($PublicKey.ValidDays)
}
else
{
$ValidTime = 0
}
$Usage = @()
if (IsEnCryptionKey($PublicKey))
{
$Usage += 'Encryption'
}
if (IsSigningKey($PublicKey))
{
$Usage += 'Signing'
}
if (IsAuthentication($PublicKey))
{
$Usage += 'Authentication'
}
if (IsCertificationKey($PublicKey))
{
$Usage += 'Certification'
}
if (!($PublicKey.IsMasterKey))
{
$PublickkeyUserIds = $PublicKey.GetUserIds()
if ($PublicKey.GetUserIds())
{
$PublickkeyUserIds = $MasterUserIDs
}
}
else
{
$PublickkeyUserIds = $MasterUserIDs
}
# Add some additional properties to the object
Add-Member -InputObject $PublicKey -MemberType NoteProperty -Name 'Id' -Value (($PublicKey.KeyId | foreach { $_.ToString('X2') }) -join '')
Add-Member -InputObject $PublicKey -MemberType NoteProperty -Name 'UserIds' -Value $PublickkeyUserIds
Add-Member -InputObject $PublicKey -MemberType NoteProperty -Name 'Fingerprint' -Value (($PublicKey.GetFingerprint() | foreach { $_.ToString('X2') }) -join '')
Add-Member -InputObject $PublicKey -MemberType NoteProperty -Name 'PreferedSymmetric' -Value $PreferedSymAlgos
Add-Member -InputObject $PublicKey -MemberType NoteProperty -Name 'PreferedHash' -Value $PreferedHashAlgos
Add-Member -InputObject $PublicKey -MemberType NoteProperty -Name 'PreferedCompression' -Value $PreferedCompressionAlgos
Add-Member -InputObject $PublicKey -MemberType NoteProperty -Name 'ExpirationDate' -Value $ValidTime
Add-Member -InputObject $PublicKey -MemberType NoteProperty -Name 'Usage' -Value $Usage
$PublicKey
}
}
}
catch
{
$error_message = $_.Exception.Message
if ($error_message -like '*PgpPublicKeyRing expected*')
{
throw 'Key specified is not a public key.'
}
elseif ($error_message -like '*unsupported version*')
{
throw 'File specified is not a OpenPGP Key file.'
}
else
{
Throw "$($error_message)"
}
}
}
End
{
}
}
<#
.Synopsis
Generates a new RSA OpenPGP Key pair.
.DESCRIPTION
Generates a new RSA OpenPGP Key pair. The keys default Size is of 2048 bits encrypted with CAST5.
The key has a Symmetric Algorithm preference of AES 256, AES 192, AES 128, TowFish, CAST5 and 3DES.
The key has al Hashing Algorithum preference of SHA 256, SHA 384, SHA 512 and RipeMD160. It supports
compression for ZLib, Zip and BZip2.
.EXAMPLE
New-PGPRSAKeyPair -Path c:\ -Identity "Carlos Perez" -Email "[email protected]" -PassPhrase (Read-Host -AsSecureString) -Verbose
VERBOSE: Generating key pair.
VERBOSE: Saving secret key to C:\\95b4851b599cb231_sec.pgp
VERBOSE: Saving public key to C:\\95b4851b599cb231_pub.pgp
VERBOSE: Generating public key
#>
function New-PGPRSAKeyPair
{
[CmdletBinding()]
[OutputType([int])]
Param
(
[Parameter(Mandatory=$true,
ValueFromPipelineByPropertyName=$true,
Position=0,
HelpMessage = 'Path to where to save the key pair.')]
[string]$Path,
[Parameter(Mandatory=$true,
ValueFromPipelineByPropertyName=$true,
Position=1,
HelpMessage = 'Identity of user of the key. (Example name of the user)')]
[string]$Identity,
[Parameter(Mandatory=$true,
ValueFromPipelineByPropertyName=$true,
Position=2,
HelpMessage = 'Email address to associate key to.')]
[string]$Email,
[Parameter(Mandatory=$true,
ValueFromPipelineByPropertyName=$true,
Position=3,
HelpMessage = 'Secure String representing the passphase for the key.')]
[securestring]$PassPhrase,
[Parameter(Mandatory=$false,
ValueFromPipelineByPropertyName=$true)]
[switch]$Armor,
[Parameter(Mandatory=$false,
ValueFromPipelineByPropertyName=$true)]
[ValidateSet('IDEA',
'3DES',
'CAST5',
'BlowFish',
'TowFish',
'DES',
'AES128',
'AES196',
'AES256')]
[string]$SymmetricAlgorithm = 'CAST5',
[Parameter(Mandatory=$false,
ValueFromPipelineByPropertyName=$true)]
[ValidateSet(1024,2048, 3072, 4096)]
[int]$KeySize = 2048,
[Parameter(Mandatory=$false,
ValueFromPipelineByPropertyName=$true)]
[datetime]$ExpirationDate
)
Begin
{
switch ($SymmetricAlgorithm)
{
'3DES' {$SymAlgo = [Org.BouncyCastle.Bcpg.SymmetricKeyAlgorithmTag]::TripleDes}
'CAST5' {$SymAlgo = [Org.BouncyCastle.Bcpg.SymmetricKeyAlgorithmTag]::Cast5}
'BlowFish' {$SymAlgo = [Org.BouncyCastle.Bcpg.SymmetricKeyAlgorithmTag]::Blowfish}
'TowFish' {$SymAlgo = [Org.BouncyCastle.Bcpg.SymmetricKeyAlgorithmTag]::Twofish}
'DES' {$SymAlgo = [Org.BouncyCastle.Bcpg.SymmetricKeyAlgorithmTag]::Des}
'AES128' {$SymAlgo = [Org.BouncyCastle.Bcpg.SymmetricKeyAlgorithmTag]::Aes128}
'AES192' {$SymAlgo = [Org.BouncyCastle.Bcpg.SymmetricKeyAlgorithmTag]::Aes192}
'AES256' {$SymAlgo = [Org.BouncyCastle.Bcpg.SymmetricKeyAlgorithmTag]::Aes256}
Default {$SymAlgo = [Org.BouncyCastle.Bcpg.SymmetricKeyAlgorithmTag]::Cast5}
}
if (Test-Path $Path)
{
$keypath = (Resolve-Path $Path).Path
}
else
{
throw 'The path specified does not exist!'
}
}
Process
{
$Generator = [Org.BouncyCastle.Security.GeneratorUtilities]::GetKeyPairGenerator('RSA')
# Public exponent of 65537
$BI = New-Object Org.BouncyCastle.Math.BigInteger -ArgumentList '10001',16
$SecureRand = New-Object Org.BouncyCastle.Security.SecureRandom
$RSAOps = New-Object Org.BouncyCastle.Crypto.Parameters.RsaKeyGenerationParameters -ArgumentList $BI,$SecureRand,$KeySize,25
$Generator.Init($RSAOps)
$keyparams = $Generator.GenerateKeyPair()
#hashparam
$HashPacket = new-object Org.BouncyCastle.Bcpg.OpenPgp.PGPSignatureSubpacketGenerator
# Prefered Symetric Algorithms in order AES 256, AES 192, AES 128, TowFish, CAST5, 3DES