1
1
using System ;
2
2
using System . Collections ;
3
-
4
3
using Org . BouncyCastle . Security ;
5
4
using Org . BouncyCastle . Utilities ;
6
5
@@ -280,42 +279,70 @@ public void AddSubKey(PgpKeyPair keyPair, HashAlgorithmTag hashAlgorithm)
280
279
this . AddSubKey ( keyPair , this . hashedPacketVector , this . unhashedPacketVector , hashAlgorithm ) ;
281
280
}
282
281
283
- /// <summary>
284
- /// Add a subkey with specific hashed and unhashed packets associated with it and
285
- /// default certification.
286
- /// </summary>
287
- /// <param name="keyPair">Public/private key pair.</param>
288
- /// <param name="hashedPackets">Hashed packet values to be included in certification.</param>
289
- /// <param name="unhashedPackets">Unhashed packets values to be included in certification.</param>
290
- /// <exception cref="PgpException"></exception>
291
- public void AddSubKey (
282
+ /// <summary>
283
+ /// Add a signing subkey to the key ring to be generated with default certification and a primary key binding signature.
284
+ /// </summary>
285
+ /// <param name="keyPair">The key pair.</param>
286
+ /// <param name="hashAlgorithm">The hash algorithm.</param>
287
+ /// <param name="primaryKeyBindingHashAlgorithm">The primary-key binding hash algorithm.</param>
288
+ public void AddSubKey ( PgpKeyPair keyPair , HashAlgorithmTag hashAlgorithm , HashAlgorithmTag primaryKeyBindingHashAlgorithm )
289
+ {
290
+ this . AddSubKey ( keyPair , this . hashedPacketVector , this . unhashedPacketVector , hashAlgorithm , primaryKeyBindingHashAlgorithm ) ;
291
+ }
292
+
293
+ /// <summary>
294
+ /// Add a subkey with specific hashed and unhashed packets associated with it and
295
+ /// default certification using SHA-1.
296
+ /// </summary>
297
+ /// <param name="keyPair">Public/private key pair.</param>
298
+ /// <param name="hashedPackets">Hashed packet values to be included in certification.</param>
299
+ /// <param name="unhashedPackets">Unhashed packets values to be included in certification.</param>
300
+ /// <exception cref="PgpException"></exception>
301
+ public void AddSubKey (
292
302
PgpKeyPair keyPair ,
293
303
PgpSignatureSubpacketVector hashedPackets ,
294
304
PgpSignatureSubpacketVector unhashedPackets )
295
305
{
296
- try
306
+ AddSubKey ( keyPair , hashedPackets , unhashedPackets , HashAlgorithmTag . Sha1 ) ;
307
+ }
308
+
309
+ /// <summary>
310
+ /// Add a subkey with specific hashed and unhashed packets associated with it and
311
+ /// default certification.
312
+ /// </summary>
313
+ /// <param name="keyPair">Public/private key pair.</param>
314
+ /// <param name="hashedPackets">Hashed packet values to be included in certification.</param>
315
+ /// <param name="unhashedPackets">Unhashed packets values to be included in certification.</param>
316
+ /// <param name="hashAlgorithm">The hash algorithm.</param>
317
+ /// <exception cref="Org.BouncyCastle.Bcpg.OpenPgp.PgpException">exception adding subkey: </exception>
318
+ /// <exception cref="PgpException"></exception>
319
+ public void AddSubKey (
320
+ PgpKeyPair keyPair ,
321
+ PgpSignatureSubpacketVector hashedPackets ,
322
+ PgpSignatureSubpacketVector unhashedPackets ,
323
+ HashAlgorithmTag hashAlgorithm )
324
+ {
325
+ try
297
326
{
298
- PgpSignatureGenerator sGen = new PgpSignatureGenerator (
299
- masterKey . PublicKey . Algorithm , HashAlgorithmTag . Sha1 ) ;
327
+ PgpSignatureGenerator sGen = new PgpSignatureGenerator ( masterKey . PublicKey . Algorithm , hashAlgorithm ) ;
300
328
301
- //
329
+ //
302
330
// Generate the certification
303
331
//
304
332
sGen . InitSign ( PgpSignature . SubkeyBinding , masterKey . PrivateKey ) ;
305
333
306
- sGen . SetHashedSubpackets ( hashedPackets ) ;
334
+ sGen . SetHashedSubpackets ( hashedPackets ) ;
307
335
sGen . SetUnhashedSubpackets ( unhashedPackets ) ;
308
336
309
- IList subSigs = Platform . CreateArrayList ( ) ;
310
-
311
- subSigs . Add ( sGen . GenerateCertification ( masterKey . PublicKey , keyPair . PublicKey ) ) ;
337
+ IList subSigs = Platform . CreateArrayList ( ) ;
338
+ subSigs . Add ( sGen . GenerateCertification ( masterKey . PublicKey , keyPair . PublicKey ) ) ;
312
339
313
- keys . Add ( new PgpSecretKey ( keyPair . PrivateKey , new PgpPublicKey ( keyPair . PublicKey , null , subSigs ) , encAlgorithm ,
340
+ keys . Add ( new PgpSecretKey ( keyPair . PrivateKey , new PgpPublicKey ( keyPair . PublicKey , null , subSigs ) , encAlgorithm ,
314
341
rawPassPhrase , false , useSha1 , rand , false ) ) ;
315
- }
316
- catch ( PgpException e )
342
+ }
343
+ catch ( PgpException )
317
344
{
318
- throw e ;
345
+ throw ;
319
346
}
320
347
catch ( Exception e )
321
348
{
@@ -324,20 +351,22 @@ public void AddSubKey(
324
351
}
325
352
326
353
/// <summary>
327
- /// Add a subkey with specific hashed and unhashed packets associated with it and
328
- /// default certification .
354
+ /// Add a signing subkey with specific hashed and unhashed packets associated with it and
355
+ /// default certifications, including the primary-key binding signature .
329
356
/// </summary>
330
357
/// <param name="keyPair">Public/private key pair.</param>
331
358
/// <param name="hashedPackets">Hashed packet values to be included in certification.</param>
332
359
/// <param name="unhashedPackets">Unhashed packets values to be included in certification.</param>
333
360
/// <param name="hashAlgorithm">The hash algorithm.</param>
361
+ /// <param name="primaryKeyBindingHashAlgorithm">The primary-key binding hash algorithm.</param>
334
362
/// <exception cref="Org.BouncyCastle.Bcpg.OpenPgp.PgpException">exception adding subkey: </exception>
335
363
/// <exception cref="PgpException"></exception>
336
364
public void AddSubKey (
337
365
PgpKeyPair keyPair ,
338
366
PgpSignatureSubpacketVector hashedPackets ,
339
367
PgpSignatureSubpacketVector unhashedPackets ,
340
- HashAlgorithmTag hashAlgorithm )
368
+ HashAlgorithmTag hashAlgorithm ,
369
+ HashAlgorithmTag primaryKeyBindingHashAlgorithm )
341
370
{
342
371
try
343
372
{
@@ -348,7 +377,17 @@ public void AddSubKey(
348
377
//
349
378
sGen . InitSign ( PgpSignature . SubkeyBinding , masterKey . PrivateKey ) ;
350
379
351
- sGen . SetHashedSubpackets ( hashedPackets ) ;
380
+ // add primary key binding sub packet
381
+ PgpSignatureGenerator pGen = new PgpSignatureGenerator ( keyPair . PublicKey . Algorithm , primaryKeyBindingHashAlgorithm ) ;
382
+
383
+ pGen . InitSign ( PgpSignature . PrimaryKeyBinding , keyPair . PrivateKey ) ;
384
+
385
+ PgpSignatureSubpacketGenerator spGen = new PgpSignatureSubpacketGenerator ( hashedPackets ) ;
386
+
387
+ spGen . SetEmbeddedSignature ( false ,
388
+ pGen . GenerateCertification ( masterKey . PublicKey , keyPair . PublicKey ) ) ;
389
+
390
+ sGen . SetHashedSubpackets ( spGen . Generate ( ) ) ;
352
391
sGen . SetUnhashedSubpackets ( unhashedPackets ) ;
353
392
354
393
IList subSigs = Platform . CreateArrayList ( ) ;
@@ -367,8 +406,7 @@ public void AddSubKey(
367
406
}
368
407
}
369
408
370
-
371
- /// <summary>Return the secret key ring.</summary>
409
+ /// <summary>Return the secret key ring.</summary>
372
410
public PgpSecretKeyRing GenerateSecretKeyRing ( )
373
411
{
374
412
return new PgpSecretKeyRing ( keys ) ;
0 commit comments