summary refs log tree commit diff
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2018-08-07 20:17:18 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2018-08-07 20:17:18 +0700
commit17484948ae42b7d5fd75a4e96b31a8bb0cb47825 (patch)
tree83f64608d50c999f21b608cc0662fadacd551fd3
parentAdd entry explaining DH changes (diff)
downloadBouncyCastle.NET-ed25519-17484948ae42b7d5fd75a4e96b31a8bb0cb47825.tar.xz
Refactoring around Pkcs10CertificationRequest
- see https://github.com/bcgit/bc-csharp/pull/123
-rw-r--r--crypto/src/crypto/operators/Asn1Signature.cs49
-rw-r--r--crypto/src/pkcs/Pkcs10CertificationRequest.cs56
2 files changed, 60 insertions, 45 deletions
diff --git a/crypto/src/crypto/operators/Asn1Signature.cs b/crypto/src/crypto/operators/Asn1Signature.cs
index e023c1d18..373ba0cee 100644
--- a/crypto/src/crypto/operators/Asn1Signature.cs
+++ b/crypto/src/crypto/operators/Asn1Signature.cs
@@ -325,7 +325,8 @@ namespace Org.BouncyCastle.Crypto.Operators
     /// Calculator factory class for signature generation in ASN.1 based profiles that use an AlgorithmIdentifier to preserve
     /// signature algorithm details.
     /// </summary>
-	public class Asn1SignatureFactory: ISignatureFactory
+	public class Asn1SignatureFactory
+        : ISignatureFactory
 	{
 		private readonly AlgorithmIdentifier algID;
         private readonly string algorithm;
@@ -337,7 +338,8 @@ namespace Org.BouncyCastle.Crypto.Operators
         /// </summary>
         /// <param name="algorithm">The name of the signature algorithm to use.</param>
         /// <param name="privateKey">The private key to be used in the signing operation.</param>
-		public Asn1SignatureFactory (string algorithm, AsymmetricKeyParameter privateKey): this(algorithm, privateKey, null)
+		public Asn1SignatureFactory (string algorithm, AsymmetricKeyParameter privateKey)
+            : this(algorithm, privateKey, null)
 		{
 		}
 
@@ -347,14 +349,21 @@ namespace Org.BouncyCastle.Crypto.Operators
         /// <param name="algorithm">The name of the signature algorithm to use.</param>
         /// <param name="privateKey">The private key to be used in the signing operation.</param>
         /// <param name="random">The source of randomness to be used in signature calculation.</param>
-		public Asn1SignatureFactory (string algorithm, AsymmetricKeyParameter privateKey, SecureRandom random)
+		public Asn1SignatureFactory(string algorithm, AsymmetricKeyParameter privateKey, SecureRandom random)
 		{
-			DerObjectIdentifier sigOid = X509Utilities.GetAlgorithmOid (algorithm);
+            if (algorithm == null)
+                throw new ArgumentNullException("algorithm");
+            if (privateKey == null)
+                throw new ArgumentNullException("privateKey");
+            if (!privateKey.IsPrivate)
+                throw new ArgumentException("Key for signing must be private", "privateKey");
+
+			DerObjectIdentifier sigOid = X509Utilities.GetAlgorithmOid(algorithm);
 
             this.algorithm = algorithm;
             this.privateKey = privateKey;
             this.random = random;
-			this.algID = X509Utilities.GetSigAlgID (sigOid, algorithm);
+			this.algID = X509Utilities.GetSigAlgID(sigOid, algorithm);
 		}
 
 		public Object AlgorithmDetails
@@ -365,16 +374,12 @@ namespace Org.BouncyCastle.Crypto.Operators
         public IStreamCalculator CreateCalculator()
         {
             ISigner sig = SignerUtilities.GetSigner(algorithm);
-
+            ICipherParameters cp = privateKey;
             if (random != null)
             {
-                sig.Init(true, new ParametersWithRandom(privateKey, random));
+                cp = new ParametersWithRandom(cp, random);
             }
-            else
-            {
-                sig.Init(true, privateKey);
-            }
-
+            sig.Init(true, cp);
             return new SigCalculator(sig);
         }
 
@@ -437,7 +442,8 @@ namespace Org.BouncyCastle.Crypto.Operators
     /// Verifier class for signature verification in ASN.1 based profiles that use an AlgorithmIdentifier to preserve
     /// signature algorithm details.
     /// </summary>
-    public class Asn1VerifierFactory: IVerifierFactory
+    public class Asn1VerifierFactory
+        : IVerifierFactory
 	{
 		private readonly AlgorithmIdentifier algID;
         private readonly AsymmetricKeyParameter publicKey;
@@ -447,15 +453,22 @@ namespace Org.BouncyCastle.Crypto.Operators
         /// </summary>
         /// <param name="algorithm">The name of the signature algorithm to use.</param>
         /// <param name="publicKey">The public key to be used in the verification operation.</param>
-        public Asn1VerifierFactory (String algorithm, AsymmetricKeyParameter publicKey)
+        public Asn1VerifierFactory(string algorithm, AsymmetricKeyParameter publicKey)
 		{
-			DerObjectIdentifier sigOid = X509Utilities.GetAlgorithmOid (algorithm);
+            if (algorithm == null)
+                throw new ArgumentNullException("algorithm");
+            if (publicKey == null)
+                throw new ArgumentNullException("publicKey");
+            if (publicKey.IsPrivate)
+                throw new ArgumentException("Key for verifying must be public", "publicKey");
+
+			DerObjectIdentifier sigOid = X509Utilities.GetAlgorithmOid(algorithm);
 
             this.publicKey = publicKey;
-			this.algID = X509Utilities.GetSigAlgID (sigOid, algorithm);
+			this.algID = X509Utilities.GetSigAlgID(sigOid, algorithm);
 		}
 
-		public Asn1VerifierFactory (AlgorithmIdentifier algorithm, AsymmetricKeyParameter publicKey)
+		public Asn1VerifierFactory(AlgorithmIdentifier algorithm, AsymmetricKeyParameter publicKey)
 		{
             this.publicKey = publicKey;
 			this.algID = algorithm;
@@ -540,7 +553,7 @@ namespace Org.BouncyCastle.Crypto.Operators
 
 		public IVerifierFactory CreateVerifierFactory(Object algorithmDetails)
 		{
-            return new Asn1VerifierFactory ((AlgorithmIdentifier)algorithmDetails, publicKey);
+            return new Asn1VerifierFactory((AlgorithmIdentifier)algorithmDetails, publicKey);
 		}
 
 		/// <summary>
diff --git a/crypto/src/pkcs/Pkcs10CertificationRequest.cs b/crypto/src/pkcs/Pkcs10CertificationRequest.cs
index 24dc9b1cc..34bda3815 100644
--- a/crypto/src/pkcs/Pkcs10CertificationRequest.cs
+++ b/crypto/src/pkcs/Pkcs10CertificationRequest.cs
@@ -210,71 +210,73 @@ namespace Org.BouncyCastle.Pkcs
         /// <param name="publicKey">Public Key to be included in cert reqest.</param>
         /// <param name="attributes">ASN1Set of Attributes.</param>
         /// <param name="signingKey">Matching Private key for nominated (above) public key to be used to sign the request.</param>
-        [Obsolete("Use constructor with an ISignatureFactory")]
         public Pkcs10CertificationRequest(
 			string					signatureAlgorithm,
 			X509Name				subject,
 			AsymmetricKeyParameter	publicKey,
 			Asn1Set					attributes,
 			AsymmetricKeyParameter	signingKey)
+            : this(new Asn1SignatureFactory(signatureAlgorithm, signingKey), subject, publicKey, attributes)
 		{
-			if (signatureAlgorithm == null)
-				throw new ArgumentNullException("signatureAlgorithm");
-			if (subject == null)
-				throw new ArgumentNullException("subject");
-			if (publicKey == null)
-				throw new ArgumentNullException("publicKey");
-			if (publicKey.IsPrivate)
-				throw new ArgumentException("expected public key", "publicKey");
-			if (!signingKey.IsPrivate)
-				throw new ArgumentException("key for signing must be private", "signingKey");
-
-            init(new Asn1SignatureFactory(signatureAlgorithm, signingKey), subject, publicKey, attributes, signingKey);
 		}
 
         /// <summary>
         /// Instantiate a Pkcs10CertificationRequest object with the necessary credentials.
         /// </summary>
-        ///<param name="signatureCalculatorFactory">The factory for signature calculators to sign the PKCS#10 request with.</param>
+        ///<param name="signatureFactory">The factory for signature calculators to sign the PKCS#10 request with.</param>
         /// <param name="subject">X509Name of subject eg OU="My unit." O="My Organisatioin" C="au" </param>
         /// <param name="publicKey">Public Key to be included in cert reqest.</param>
         /// <param name="attributes">ASN1Set of Attributes.</param>
-        /// <param name="signingKey">Matching Private key for nominated (above) public key to be used to sign the request.</param>
+        /// <param name="signingKey">Ignored.</param>
+        [Obsolete("Use constructor without 'signingKey' parameter (ignored here)")]
         public Pkcs10CertificationRequest(
-            ISignatureFactory signatureCalculatorFactory,
+            ISignatureFactory signatureFactory,
             X509Name subject,
             AsymmetricKeyParameter publicKey,
             Asn1Set attributes,
             AsymmetricKeyParameter signingKey)
+            : this(signatureFactory, subject, publicKey, attributes)
         {
-            if (signatureCalculatorFactory == null)
-                throw new ArgumentNullException("signatureCalculator");
+        }
+
+        /// <summary>
+        /// Instantiate a Pkcs10CertificationRequest object with the necessary credentials.
+        /// </summary>
+        ///<param name="signatureFactory">The factory for signature calculators to sign the PKCS#10 request with.</param>
+        /// <param name="subject">X509Name of subject eg OU="My unit." O="My Organisatioin" C="au" </param>
+        /// <param name="publicKey">Public Key to be included in cert reqest.</param>
+        /// <param name="attributes">ASN1Set of Attributes.</param>
+        public Pkcs10CertificationRequest(
+            ISignatureFactory signatureFactory,
+            X509Name subject,
+            AsymmetricKeyParameter publicKey,
+            Asn1Set attributes)
+        {
+            if (signatureFactory == null)
+                throw new ArgumentNullException("signatureFactory");
             if (subject == null)
                 throw new ArgumentNullException("subject");
             if (publicKey == null)
                 throw new ArgumentNullException("publicKey");
             if (publicKey.IsPrivate)
                 throw new ArgumentException("expected public key", "publicKey");
-            if (!signingKey.IsPrivate)
-                throw new ArgumentException("key for signing must be private", "signingKey");
 
-            init(signatureCalculatorFactory, subject, publicKey, attributes, signingKey);
+            Init(signatureFactory, subject, publicKey, attributes);
         }
 
-        private void init(
-            ISignatureFactory signatureCalculator, 
+        private void Init(
+            ISignatureFactory signatureFactory, 
             X509Name subject,
             AsymmetricKeyParameter publicKey,
-            Asn1Set attributes,
-            AsymmetricKeyParameter signingKey)
+            Asn1Set attributes)
         {
-            this.sigAlgId = (AlgorithmIdentifier)signatureCalculator.AlgorithmDetails;
+            this.sigAlgId = (AlgorithmIdentifier)signatureFactory.AlgorithmDetails;
 
             SubjectPublicKeyInfo pubInfo = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(publicKey);
 
             this.reqInfo = new CertificationRequestInfo(subject, pubInfo, attributes);
 
-            IStreamCalculator streamCalculator = signatureCalculator.CreateCalculator();
+            IStreamCalculator streamCalculator = signatureFactory.CreateCalculator();
 
             byte[] reqInfoData = reqInfo.GetDerEncoded();