summary refs log tree commit diff
path: root/crypto/src/cmp/CertificateConfirmationContentBuilder.cs
diff options
context:
space:
mode:
authorDavid Hook <dgh@bouncycastle.org>2019-01-14 18:10:49 +1100
committerDavid Hook <dgh@bouncycastle.org>2019-01-14 18:10:49 +1100
commita723aca1e07f57af70d7596a4fe3961045cdb0d9 (patch)
tree9c2c3b608e823c2705855a63069d3d8326fecaf5 /crypto/src/cmp/CertificateConfirmationContentBuilder.cs
parentNist algs and ProtectedMessageTests (diff)
downloadBouncyCastle.NET-ed25519-a723aca1e07f57af70d7596a4fe3961045cdb0d9.tar.xz
packaging fix
Diffstat (limited to 'crypto/src/cmp/CertificateConfirmationContentBuilder.cs')
-rw-r--r--crypto/src/cmp/CertificateConfirmationContentBuilder.cs73
1 files changed, 73 insertions, 0 deletions
diff --git a/crypto/src/cmp/CertificateConfirmationContentBuilder.cs b/crypto/src/cmp/CertificateConfirmationContentBuilder.cs
new file mode 100644
index 000000000..126484917
--- /dev/null
+++ b/crypto/src/cmp/CertificateConfirmationContentBuilder.cs
@@ -0,0 +1,73 @@
+using System;
+using System.Collections;
+using System.Collections.Generic;
+using System.Text;
+using Org.BouncyCastle.Asn1;
+using Org.BouncyCastle.Asn1.Cmp;
+using Org.BouncyCastle.Asn1.X509;
+using Org.BouncyCastle.Cms;
+using Org.BouncyCastle.Crypto.IO;
+using Org.BouncyCastle.Math;
+using Org.BouncyCastle.Security;
+using Org.BouncyCastle.X509;
+
+namespace Org.BouncyCastle.Cmp
+{
+    public class CertificateConfirmationContentBuilder
+    {
+        DefaultSignatureAlgorithmIdentifierFinder sigAlgFinder = new DefaultSignatureAlgorithmIdentifierFinder();
+        private DefaultDigestAlgorithmIdentifierFinder digestAlgFinder;
+        private ArrayList acceptedCerts = new ArrayList();
+        private ArrayList acceptedReqIds = new ArrayList();
+
+        public CertificateConfirmationContentBuilder() : this(new DefaultDigestAlgorithmIdentifierFinder())
+        {
+
+        }
+    
+        public CertificateConfirmationContentBuilder(DefaultDigestAlgorithmIdentifierFinder digestAlgFinder)
+        {
+            this.digestAlgFinder = digestAlgFinder;
+        }
+
+        public CertificateConfirmationContentBuilder AddAcceptedCertificate(X509Certificate certHolder,
+            BigInteger certReqId)
+        {
+            acceptedCerts.Add(certHolder);
+            acceptedReqIds.Add(certReqId);
+            return this;
+        }
+
+        public CertificateConfirmationContent Build()
+        {
+            Asn1EncodableVector v = new Asn1EncodableVector();
+            for (int i = 0; i != acceptedCerts.Count; i++)
+            {
+                X509Certificate cert = (X509Certificate) acceptedCerts[i];
+                BigInteger reqId = (BigInteger) acceptedReqIds[i];
+
+
+                
+                AlgorithmIdentifier algorithmIdentifier =  sigAlgFinder.Find(cert.SigAlgName);
+
+                AlgorithmIdentifier digAlg = digestAlgFinder.find(algorithmIdentifier);
+                if (digAlg == null)
+                {
+                    throw new CmpException("cannot find algorithm for digest from signature");
+                }
+
+                DigestSink sink = new DigestSink(DigestUtilities.GetDigest(digAlg.Algorithm));
+
+                sink.Write(cert.GetEncoded());
+
+                byte[] dig = new byte[sink.Digest.GetDigestSize()];
+                sink.Digest.DoFinal(dig, 0);
+
+                v.Add(new CertStatus(dig,reqId));
+            }
+
+            return new CertificateConfirmationContent(CertConfirmContent.GetInstance(new DerSequence(v)),
+                digestAlgFinder);
+        }
+    }
+}