@@ -325,7 +325,8 @@ public override void SetLength(
325
325
/// Calculator factory class for signature generation in ASN.1 based profiles that use an AlgorithmIdentifier to preserve
326
326
/// signature algorithm details.
327
327
/// </summary>
328
- public class Asn1SignatureFactory : ISignatureFactory
328
+ public class Asn1SignatureFactory
329
+ : ISignatureFactory
329
330
{
330
331
private readonly AlgorithmIdentifier algID ;
331
332
private readonly string algorithm ;
@@ -337,7 +338,8 @@ public class Asn1SignatureFactory: ISignatureFactory
337
338
/// </summary>
338
339
/// <param name="algorithm">The name of the signature algorithm to use.</param>
339
340
/// <param name="privateKey">The private key to be used in the signing operation.</param>
340
- public Asn1SignatureFactory ( string algorithm , AsymmetricKeyParameter privateKey ) : this ( algorithm , privateKey , null )
341
+ public Asn1SignatureFactory ( string algorithm , AsymmetricKeyParameter privateKey )
342
+ : this ( algorithm , privateKey , null )
341
343
{
342
344
}
343
345
@@ -347,14 +349,21 @@ public Asn1SignatureFactory (string algorithm, AsymmetricKeyParameter privateKey
347
349
/// <param name="algorithm">The name of the signature algorithm to use.</param>
348
350
/// <param name="privateKey">The private key to be used in the signing operation.</param>
349
351
/// <param name="random">The source of randomness to be used in signature calculation.</param>
350
- public Asn1SignatureFactory ( string algorithm , AsymmetricKeyParameter privateKey , SecureRandom random )
352
+ public Asn1SignatureFactory ( string algorithm , AsymmetricKeyParameter privateKey , SecureRandom random )
351
353
{
352
- DerObjectIdentifier sigOid = X509Utilities . GetAlgorithmOid ( algorithm ) ;
354
+ if ( algorithm == null )
355
+ throw new ArgumentNullException ( "algorithm" ) ;
356
+ if ( privateKey == null )
357
+ throw new ArgumentNullException ( "privateKey" ) ;
358
+ if ( ! privateKey . IsPrivate )
359
+ throw new ArgumentException ( "Key for signing must be private" , "privateKey" ) ;
360
+
361
+ DerObjectIdentifier sigOid = X509Utilities . GetAlgorithmOid ( algorithm ) ;
353
362
354
363
this . algorithm = algorithm ;
355
364
this . privateKey = privateKey ;
356
365
this . random = random ;
357
- this . algID = X509Utilities . GetSigAlgID ( sigOid , algorithm ) ;
366
+ this . algID = X509Utilities . GetSigAlgID ( sigOid , algorithm ) ;
358
367
}
359
368
360
369
public Object AlgorithmDetails
@@ -365,16 +374,12 @@ public Object AlgorithmDetails
365
374
public IStreamCalculator CreateCalculator ( )
366
375
{
367
376
ISigner sig = SignerUtilities . GetSigner ( algorithm ) ;
368
-
377
+ ICipherParameters cp = privateKey ;
369
378
if ( random != null )
370
379
{
371
- sig . Init ( true , new ParametersWithRandom ( privateKey , random ) ) ;
380
+ cp = new ParametersWithRandom ( cp , random ) ;
372
381
}
373
- else
374
- {
375
- sig . Init ( true , privateKey ) ;
376
- }
377
-
382
+ sig . Init ( true , cp ) ;
378
383
return new SigCalculator ( sig ) ;
379
384
}
380
385
@@ -437,7 +442,8 @@ public int Collect(byte[] destination, int offset)
437
442
/// Verifier class for signature verification in ASN.1 based profiles that use an AlgorithmIdentifier to preserve
438
443
/// signature algorithm details.
439
444
/// </summary>
440
- public class Asn1VerifierFactory : IVerifierFactory
445
+ public class Asn1VerifierFactory
446
+ : IVerifierFactory
441
447
{
442
448
private readonly AlgorithmIdentifier algID ;
443
449
private readonly AsymmetricKeyParameter publicKey ;
@@ -447,15 +453,22 @@ public class Asn1VerifierFactory: IVerifierFactory
447
453
/// </summary>
448
454
/// <param name="algorithm">The name of the signature algorithm to use.</param>
449
455
/// <param name="publicKey">The public key to be used in the verification operation.</param>
450
- public Asn1VerifierFactory ( String algorithm , AsymmetricKeyParameter publicKey )
456
+ public Asn1VerifierFactory ( string algorithm , AsymmetricKeyParameter publicKey )
451
457
{
452
- DerObjectIdentifier sigOid = X509Utilities . GetAlgorithmOid ( algorithm ) ;
458
+ if ( algorithm == null )
459
+ throw new ArgumentNullException ( "algorithm" ) ;
460
+ if ( publicKey == null )
461
+ throw new ArgumentNullException ( "publicKey" ) ;
462
+ if ( publicKey . IsPrivate )
463
+ throw new ArgumentException ( "Key for verifying must be public" , "publicKey" ) ;
464
+
465
+ DerObjectIdentifier sigOid = X509Utilities . GetAlgorithmOid ( algorithm ) ;
453
466
454
467
this . publicKey = publicKey ;
455
- this . algID = X509Utilities . GetSigAlgID ( sigOid , algorithm ) ;
468
+ this . algID = X509Utilities . GetSigAlgID ( sigOid , algorithm ) ;
456
469
}
457
470
458
- public Asn1VerifierFactory ( AlgorithmIdentifier algorithm , AsymmetricKeyParameter publicKey )
471
+ public Asn1VerifierFactory ( AlgorithmIdentifier algorithm , AsymmetricKeyParameter publicKey )
459
472
{
460
473
this . publicKey = publicKey ;
461
474
this . algID = algorithm ;
@@ -540,7 +553,7 @@ public Asn1VerifierFactoryProvider(AsymmetricKeyParameter publicKey)
540
553
541
554
public IVerifierFactory CreateVerifierFactory ( Object algorithmDetails )
542
555
{
543
- return new Asn1VerifierFactory ( ( AlgorithmIdentifier ) algorithmDetails , publicKey ) ;
556
+ return new Asn1VerifierFactory ( ( AlgorithmIdentifier ) algorithmDetails , publicKey ) ;
544
557
}
545
558
546
559
/// <summary>
0 commit comments