summary refs log tree commit diff
path: root/crypto/src/x509/X509V2AttributeCertificateGenerator.cs
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2022-06-29 20:55:57 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2022-06-29 20:55:57 +0700
commite049cab36e9e68adff298acc24acdaa2551f72ef (patch)
treee772d623791461cba50b1eba3bbd4f833f893ae9 /crypto/src/x509/X509V2AttributeCertificateGenerator.cs
parentAdd Objects class (diff)
downloadBouncyCastle.NET-ed25519-e049cab36e9e68adff298acc24acdaa2551f72ef.tar.xz
Refactoring around Stream signers
Diffstat (limited to 'crypto/src/x509/X509V2AttributeCertificateGenerator.cs')
-rw-r--r--crypto/src/x509/X509V2AttributeCertificateGenerator.cs50
1 files changed, 21 insertions, 29 deletions
diff --git a/crypto/src/x509/X509V2AttributeCertificateGenerator.cs b/crypto/src/x509/X509V2AttributeCertificateGenerator.cs
index 2e5c9c863..3e1a58e49 100644
--- a/crypto/src/x509/X509V2AttributeCertificateGenerator.cs
+++ b/crypto/src/x509/X509V2AttributeCertificateGenerator.cs
@@ -1,5 +1,6 @@
 using System;
 using System.Collections.Generic;
+using System.IO;
 
 using Org.BouncyCastle.Asn1;
 using Org.BouncyCastle.Asn1.X509;
@@ -99,44 +100,35 @@ namespace Org.BouncyCastle.X509
 			extGenerator.AddExtension(new DerObjectIdentifier(oid), critical, extensionValue);
 		}
 
-        /// <summary>
-        /// Generate a new X.509 Attribute Certificate using the passed in SignatureCalculator.
-        /// </summary>
-        /// <param name="signatureCalculatorFactory">A signature calculator factory with the necessary algorithm details.</param>
-        /// <returns>An IX509AttributeCertificate.</returns>
-        public X509V2AttributeCertificate Generate(ISignatureFactory signatureCalculatorFactory)
+		/// <summary>
+		/// Generate a new <see cref="X509V2AttributeCertificate"/> using the provided <see cref="ISignatureFactory"/>.
+		/// </summary>
+		/// <param name="signatureFactory">A <see cref="ISignatureFactory">signature factory</see> with the necessary
+		/// algorithm details.</param>
+		/// <returns>An <see cref="X509V2AttributeCertificate"/>.</returns>
+		public X509V2AttributeCertificate Generate(ISignatureFactory signatureFactory)
         {
-            if (!extGenerator.IsEmpty)
+			var sigAlgID = (AlgorithmIdentifier)signatureFactory.AlgorithmDetails;
+
+			acInfoGen.SetSignature(sigAlgID);
+
+			if (!extGenerator.IsEmpty)
 			{
 				acInfoGen.SetExtensions(extGenerator.Generate());
 			}
 
-            AlgorithmIdentifier sigAlgID = (AlgorithmIdentifier)signatureCalculatorFactory.AlgorithmDetails;
-
-            acInfoGen.SetSignature(sigAlgID);
-
             AttributeCertificateInfo acInfo = acInfoGen.GenerateAttributeCertificateInfo();
 
-            byte[] encoded = acInfo.GetDerEncoded();
-
-            IStreamCalculator streamCalculator = signatureCalculatorFactory.CreateCalculator();
-
-            streamCalculator.Stream.Write(encoded, 0, encoded.Length);
-
-            Platform.Dispose(streamCalculator.Stream);
-
-            try
+			IStreamCalculator streamCalculator = signatureFactory.CreateCalculator();
+			using (Stream sigStream = streamCalculator.Stream)
 			{
-                DerBitString signatureValue = new DerBitString(((IBlockResult)streamCalculator.GetResult()).Collect());
-
-                return new X509V2AttributeCertificate(new AttributeCertificate(acInfo, sigAlgID, signatureValue));
-			}
-			catch (Exception e)
-			{
-				// TODO
-//				throw new ExtCertificateEncodingException("constructed invalid certificate", e);
-				throw new CertificateEncodingException("constructed invalid certificate", e);
+				acInfo.EncodeTo(sigStream, Asn1Encodable.Der);
 			}
+
+			var signature = ((IBlockResult)streamCalculator.GetResult()).Collect();
+
+			return new X509V2AttributeCertificate(
+				new AttributeCertificate(acInfo, sigAlgID, new DerBitString(signature)));
 		}
 
 		/// <summary>