Skip to content

Commit 1748494

Browse files
committed
Refactoring around Pkcs10CertificationRequest
- see #123
1 parent cc5062b commit 1748494

File tree

2 files changed

+60
-45
lines changed

2 files changed

+60
-45
lines changed

crypto/src/crypto/operators/Asn1Signature.cs

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,8 @@ public override void SetLength(
325325
/// Calculator factory class for signature generation in ASN.1 based profiles that use an AlgorithmIdentifier to preserve
326326
/// signature algorithm details.
327327
/// </summary>
328-
public class Asn1SignatureFactory: ISignatureFactory
328+
public class Asn1SignatureFactory
329+
: ISignatureFactory
329330
{
330331
private readonly AlgorithmIdentifier algID;
331332
private readonly string algorithm;
@@ -337,7 +338,8 @@ public class Asn1SignatureFactory: ISignatureFactory
337338
/// </summary>
338339
/// <param name="algorithm">The name of the signature algorithm to use.</param>
339340
/// <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)
341343
{
342344
}
343345

@@ -347,14 +349,21 @@ public Asn1SignatureFactory (string algorithm, AsymmetricKeyParameter privateKey
347349
/// <param name="algorithm">The name of the signature algorithm to use.</param>
348350
/// <param name="privateKey">The private key to be used in the signing operation.</param>
349351
/// <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)
351353
{
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);
353362

354363
this.algorithm = algorithm;
355364
this.privateKey = privateKey;
356365
this.random = random;
357-
this.algID = X509Utilities.GetSigAlgID (sigOid, algorithm);
366+
this.algID = X509Utilities.GetSigAlgID(sigOid, algorithm);
358367
}
359368

360369
public Object AlgorithmDetails
@@ -365,16 +374,12 @@ public Object AlgorithmDetails
365374
public IStreamCalculator CreateCalculator()
366375
{
367376
ISigner sig = SignerUtilities.GetSigner(algorithm);
368-
377+
ICipherParameters cp = privateKey;
369378
if (random != null)
370379
{
371-
sig.Init(true, new ParametersWithRandom(privateKey, random));
380+
cp = new ParametersWithRandom(cp, random);
372381
}
373-
else
374-
{
375-
sig.Init(true, privateKey);
376-
}
377-
382+
sig.Init(true, cp);
378383
return new SigCalculator(sig);
379384
}
380385

@@ -437,7 +442,8 @@ public int Collect(byte[] destination, int offset)
437442
/// Verifier class for signature verification in ASN.1 based profiles that use an AlgorithmIdentifier to preserve
438443
/// signature algorithm details.
439444
/// </summary>
440-
public class Asn1VerifierFactory: IVerifierFactory
445+
public class Asn1VerifierFactory
446+
: IVerifierFactory
441447
{
442448
private readonly AlgorithmIdentifier algID;
443449
private readonly AsymmetricKeyParameter publicKey;
@@ -447,15 +453,22 @@ public class Asn1VerifierFactory: IVerifierFactory
447453
/// </summary>
448454
/// <param name="algorithm">The name of the signature algorithm to use.</param>
449455
/// <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)
451457
{
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);
453466

454467
this.publicKey = publicKey;
455-
this.algID = X509Utilities.GetSigAlgID (sigOid, algorithm);
468+
this.algID = X509Utilities.GetSigAlgID(sigOid, algorithm);
456469
}
457470

458-
public Asn1VerifierFactory (AlgorithmIdentifier algorithm, AsymmetricKeyParameter publicKey)
471+
public Asn1VerifierFactory(AlgorithmIdentifier algorithm, AsymmetricKeyParameter publicKey)
459472
{
460473
this.publicKey = publicKey;
461474
this.algID = algorithm;
@@ -540,7 +553,7 @@ public Asn1VerifierFactoryProvider(AsymmetricKeyParameter publicKey)
540553

541554
public IVerifierFactory CreateVerifierFactory(Object algorithmDetails)
542555
{
543-
return new Asn1VerifierFactory ((AlgorithmIdentifier)algorithmDetails, publicKey);
556+
return new Asn1VerifierFactory((AlgorithmIdentifier)algorithmDetails, publicKey);
544557
}
545558

546559
/// <summary>

crypto/src/pkcs/Pkcs10CertificationRequest.cs

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -210,71 +210,73 @@ public Pkcs10CertificationRequest(
210210
/// <param name="publicKey">Public Key to be included in cert reqest.</param>
211211
/// <param name="attributes">ASN1Set of Attributes.</param>
212212
/// <param name="signingKey">Matching Private key for nominated (above) public key to be used to sign the request.</param>
213-
[Obsolete("Use constructor with an ISignatureFactory")]
214213
public Pkcs10CertificationRequest(
215214
string signatureAlgorithm,
216215
X509Name subject,
217216
AsymmetricKeyParameter publicKey,
218217
Asn1Set attributes,
219218
AsymmetricKeyParameter signingKey)
219+
: this(new Asn1SignatureFactory(signatureAlgorithm, signingKey), subject, publicKey, attributes)
220220
{
221-
if (signatureAlgorithm == null)
222-
throw new ArgumentNullException("signatureAlgorithm");
223-
if (subject == null)
224-
throw new ArgumentNullException("subject");
225-
if (publicKey == null)
226-
throw new ArgumentNullException("publicKey");
227-
if (publicKey.IsPrivate)
228-
throw new ArgumentException("expected public key", "publicKey");
229-
if (!signingKey.IsPrivate)
230-
throw new ArgumentException("key for signing must be private", "signingKey");
231-
232-
init(new Asn1SignatureFactory(signatureAlgorithm, signingKey), subject, publicKey, attributes, signingKey);
233221
}
234222

235223
/// <summary>
236224
/// Instantiate a Pkcs10CertificationRequest object with the necessary credentials.
237225
/// </summary>
238-
///<param name="signatureCalculatorFactory">The factory for signature calculators to sign the PKCS#10 request with.</param>
226+
///<param name="signatureFactory">The factory for signature calculators to sign the PKCS#10 request with.</param>
239227
/// <param name="subject">X509Name of subject eg OU="My unit." O="My Organisatioin" C="au" </param>
240228
/// <param name="publicKey">Public Key to be included in cert reqest.</param>
241229
/// <param name="attributes">ASN1Set of Attributes.</param>
242-
/// <param name="signingKey">Matching Private key for nominated (above) public key to be used to sign the request.</param>
230+
/// <param name="signingKey">Ignored.</param>
231+
[Obsolete("Use constructor without 'signingKey' parameter (ignored here)")]
243232
public Pkcs10CertificationRequest(
244-
ISignatureFactory signatureCalculatorFactory,
233+
ISignatureFactory signatureFactory,
245234
X509Name subject,
246235
AsymmetricKeyParameter publicKey,
247236
Asn1Set attributes,
248237
AsymmetricKeyParameter signingKey)
238+
: this(signatureFactory, subject, publicKey, attributes)
249239
{
250-
if (signatureCalculatorFactory == null)
251-
throw new ArgumentNullException("signatureCalculator");
240+
}
241+
242+
/// <summary>
243+
/// Instantiate a Pkcs10CertificationRequest object with the necessary credentials.
244+
/// </summary>
245+
///<param name="signatureFactory">The factory for signature calculators to sign the PKCS#10 request with.</param>
246+
/// <param name="subject">X509Name of subject eg OU="My unit." O="My Organisatioin" C="au" </param>
247+
/// <param name="publicKey">Public Key to be included in cert reqest.</param>
248+
/// <param name="attributes">ASN1Set of Attributes.</param>
249+
public Pkcs10CertificationRequest(
250+
ISignatureFactory signatureFactory,
251+
X509Name subject,
252+
AsymmetricKeyParameter publicKey,
253+
Asn1Set attributes)
254+
{
255+
if (signatureFactory == null)
256+
throw new ArgumentNullException("signatureFactory");
252257
if (subject == null)
253258
throw new ArgumentNullException("subject");
254259
if (publicKey == null)
255260
throw new ArgumentNullException("publicKey");
256261
if (publicKey.IsPrivate)
257262
throw new ArgumentException("expected public key", "publicKey");
258-
if (!signingKey.IsPrivate)
259-
throw new ArgumentException("key for signing must be private", "signingKey");
260263

261-
init(signatureCalculatorFactory, subject, publicKey, attributes, signingKey);
264+
Init(signatureFactory, subject, publicKey, attributes);
262265
}
263266

264-
private void init(
265-
ISignatureFactory signatureCalculator,
267+
private void Init(
268+
ISignatureFactory signatureFactory,
266269
X509Name subject,
267270
AsymmetricKeyParameter publicKey,
268-
Asn1Set attributes,
269-
AsymmetricKeyParameter signingKey)
271+
Asn1Set attributes)
270272
{
271-
this.sigAlgId = (AlgorithmIdentifier)signatureCalculator.AlgorithmDetails;
273+
this.sigAlgId = (AlgorithmIdentifier)signatureFactory.AlgorithmDetails;
272274

273275
SubjectPublicKeyInfo pubInfo = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(publicKey);
274276

275277
this.reqInfo = new CertificationRequestInfo(subject, pubInfo, attributes);
276278

277-
IStreamCalculator streamCalculator = signatureCalculator.CreateCalculator();
279+
IStreamCalculator streamCalculator = signatureFactory.CreateCalculator();
278280

279281
byte[] reqInfoData = reqInfo.GetDerEncoded();
280282

0 commit comments

Comments
 (0)