summary refs log tree commit diff
diff options
context:
space:
mode:
authorDavid Hook <dgh@bouncycastle.org>2015-10-12 17:41:50 +1100
committerDavid Hook <dgh@bouncycastle.org>2015-10-12 17:41:50 +1100
commit98df52476e4c19695c5cb6c29437adf31c349952 (patch)
tree26319947c0e8ac9d20acea7240bb28e3d9c32ef0
parentIntroduced Utilities.IO.FilterStream (diff)
downloadBouncyCastle.NET-ed25519-98df52476e4c19695c5cb6c29437adf31c349952.tar.xz
removed bogus imports (old FilterStream)
added ISignatureGenerator to Pkcs10.
-rw-r--r--crypto/src/asn1/Asn1InputStream.cs1
-rw-r--r--crypto/src/cms/CMSTypedStream.cs1
-rw-r--r--crypto/src/pkcs/Pkcs10CertificationRequest.cs169
3 files changed, 87 insertions, 84 deletions
diff --git a/crypto/src/asn1/Asn1InputStream.cs b/crypto/src/asn1/Asn1InputStream.cs
index 18d13c32d..501e788a0 100644
--- a/crypto/src/asn1/Asn1InputStream.cs
+++ b/crypto/src/asn1/Asn1InputStream.cs
@@ -2,7 +2,6 @@ using System;
 using System.Diagnostics;
 using System.IO;
 
-using Org.BouncyCastle.Asn1.Utilities;
 using Org.BouncyCastle.Utilities.IO;
 
 namespace Org.BouncyCastle.Asn1
diff --git a/crypto/src/cms/CMSTypedStream.cs b/crypto/src/cms/CMSTypedStream.cs
index 9cb314211..d04846ee1 100644
--- a/crypto/src/cms/CMSTypedStream.cs
+++ b/crypto/src/cms/CMSTypedStream.cs
@@ -2,7 +2,6 @@ using System;
 using System.IO;
 
 using Org.BouncyCastle.Asn1.Pkcs;
-using Org.BouncyCastle.Asn1.Utilities;
 using Org.BouncyCastle.Utilities.IO;
 
 namespace Org.BouncyCastle.Cms
diff --git a/crypto/src/pkcs/Pkcs10CertificationRequest.cs b/crypto/src/pkcs/Pkcs10CertificationRequest.cs
index 9f24eb18a..878cb61d3 100644
--- a/crypto/src/pkcs/Pkcs10CertificationRequest.cs
+++ b/crypto/src/pkcs/Pkcs10CertificationRequest.cs
@@ -15,6 +15,7 @@ using Org.BouncyCastle.Security;
 using Org.BouncyCastle.Utilities;
 using Org.BouncyCastle.Utilities.Collections;
 using Org.BouncyCastle.X509;
+using Org.BouncyCastle.Crypto.Operators;
 
 namespace Org.BouncyCastle.Pkcs
 {
@@ -198,17 +199,18 @@ namespace Org.BouncyCastle.Pkcs
 			Stream input)
 			: base((Asn1Sequence) Asn1Object.FromStream(input))
 		{
-		}
-
-		/// <summary>
-		/// Instantiate a Pkcs10CertificationRequest object with the necessary credentials.
-		/// </summary>
-		///<param name="signatureAlgorithm">Name of Sig Alg.</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>
-		public Pkcs10CertificationRequest(
+        }
+
+        /// <summary>
+        /// Instantiate a Pkcs10CertificationRequest object with the necessary credentials.
+        /// </summary>
+        ///<param name="signatureAlgorithm">Name of Sig Alg.</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>
+        [Obsolete("Use constructor with an ISignatureCalculator")]
+        public Pkcs10CertificationRequest(
 			string					signatureAlgorithm,
 			X509Name				subject,
 			AsymmetricKeyParameter	publicKey,
@@ -226,79 +228,82 @@ namespace Org.BouncyCastle.Pkcs
 			if (!signingKey.IsPrivate)
 				throw new ArgumentException("key for signing must be private", "signingKey");
 
-//			DerObjectIdentifier sigOid = SignerUtilities.GetObjectIdentifier(signatureAlgorithm);
-			string algorithmName = Platform.ToUpperInvariant(signatureAlgorithm);
-			DerObjectIdentifier sigOid = (DerObjectIdentifier) algorithms[algorithmName];
-
-			if (sigOid == null)
-			{
-				try
-				{
-					sigOid = new DerObjectIdentifier(algorithmName);
-				}
-				catch (Exception e)
-				{
-					throw new ArgumentException("Unknown signature type requested", e);
-				}
-			}
-
-			if (noParams.Contains(sigOid))
-			{
-				this.sigAlgId = new AlgorithmIdentifier(sigOid);
-			}
-			else if (exParams.Contains(algorithmName))
-			{
-				this.sigAlgId = new AlgorithmIdentifier(sigOid, (Asn1Encodable) exParams[algorithmName]);
-			}
-			else
-			{
-				this.sigAlgId = new AlgorithmIdentifier(sigOid, DerNull.Instance);
-			}
-
-			SubjectPublicKeyInfo pubInfo = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(publicKey);
-
-			this.reqInfo = new CertificationRequestInfo(subject, pubInfo, attributes);
-
-			ISigner sig = SignerUtilities.GetSigner(signatureAlgorithm);
-
-			sig.Init(true, signingKey);
-
-			try
-			{
-				// Encode.
-				byte[] b = reqInfo.GetDerEncoded();
-				sig.BlockUpdate(b, 0, b.Length);
-			}
-			catch (Exception e)
-			{
-				throw new ArgumentException("exception encoding TBS cert request", e);
-			}
-
-			// Generate Signature.
-			sigBits = new DerBitString(sig.GenerateSignature());
+            init(new Asn1SignatureCalculator(signatureAlgorithm, signingKey), subject, publicKey, attributes, signingKey);
 		}
 
-//        internal Pkcs10CertificationRequest(
-//        	Asn1InputStream seqStream)
-//        {
-//			Asn1Sequence seq = (Asn1Sequence) seqStream.ReadObject();
-//            try
-//            {
-//                this.reqInfo = CertificationRequestInfo.GetInstance(seq[0]);
-//                this.sigAlgId = AlgorithmIdentifier.GetInstance(seq[1]);
-//                this.sigBits = (DerBitString) seq[2];
-//            }
-//            catch (Exception ex)
-//            {
-//                throw new ArgumentException("Create From Asn1Sequence: " + ex.Message);
-//            }
-//        }
-
-		/// <summary>
-		/// Get the public key.
-		/// </summary>
-		/// <returns>The public key.</returns>
-		public AsymmetricKeyParameter GetPublicKey()
+        /// <summary>
+        /// Instantiate a Pkcs10CertificationRequest object with the necessary credentials.
+        /// </summary>
+        ///<param name="signatureCalculator">The signature calculator 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>
+        public Pkcs10CertificationRequest(
+            ISignatureCalculator signatureCalculator,
+            X509Name subject,
+            AsymmetricKeyParameter publicKey,
+            Asn1Set attributes,
+            AsymmetricKeyParameter signingKey)
+        {
+            if (signatureCalculator == null)
+                throw new ArgumentNullException("signatureCalculator");
+            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(signatureCalculator, subject, publicKey, attributes, signingKey);
+        }
+
+        private void init(
+            ISignatureCalculator signatureCalculator, 
+            X509Name subject,
+            AsymmetricKeyParameter publicKey,
+            Asn1Set attributes,
+            AsymmetricKeyParameter signingKey)
+        {
+            this.sigAlgId = (AlgorithmIdentifier)signatureCalculator.AlgorithmDetails;
+
+            SubjectPublicKeyInfo pubInfo = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(publicKey);
+
+            this.reqInfo = new CertificationRequestInfo(subject, pubInfo, attributes);
+
+            Stream sigStream = signatureCalculator.GetSignatureUpdater();
+
+            byte[] reqInfoData = reqInfo.GetDerEncoded();
+
+            sigStream.Write(reqInfoData, 0, reqInfoData.Length);
+
+            // Generate Signature.
+            sigBits = new DerBitString(signatureCalculator.Signature());
+        }
+
+        //        internal Pkcs10CertificationRequest(
+        //        	Asn1InputStream seqStream)
+        //        {
+        //			Asn1Sequence seq = (Asn1Sequence) seqStream.ReadObject();
+        //            try
+        //            {
+        //                this.reqInfo = CertificationRequestInfo.GetInstance(seq[0]);
+        //                this.sigAlgId = AlgorithmIdentifier.GetInstance(seq[1]);
+        //                this.sigBits = (DerBitString) seq[2];
+        //            }
+        //            catch (Exception ex)
+        //            {
+        //                throw new ArgumentException("Create From Asn1Sequence: " + ex.Message);
+        //            }
+        //        }
+
+        /// <summary>
+        /// Get the public key.
+        /// </summary>
+        /// <returns>The public key.</returns>
+        public AsymmetricKeyParameter GetPublicKey()
 		{
 			return PublicKeyFactory.CreateKey(reqInfo.SubjectPublicKeyInfo);
 		}