CertificateConfirmationContentBuilder.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System.Collections.Generic;
  4. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1;
  5. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.Cmp;
  6. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509;
  7. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Cms;
  8. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Math;
  9. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Security;
  10. using Best.HTTP.SecureProtocol.Org.BouncyCastle.X509;
  11. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Cmp
  12. {
  13. public sealed class CertificateConfirmationContentBuilder
  14. {
  15. private static readonly DefaultSignatureAlgorithmIdentifierFinder SigAlgFinder =
  16. new DefaultSignatureAlgorithmIdentifierFinder();
  17. private readonly DefaultDigestAlgorithmIdentifierFinder m_digestAlgFinder;
  18. private readonly IList<X509Certificate> m_acceptedCerts = new List<X509Certificate>();
  19. private readonly IList<BigInteger> m_acceptedReqIDs = new List<BigInteger>();
  20. public CertificateConfirmationContentBuilder()
  21. : this(new DefaultDigestAlgorithmIdentifierFinder())
  22. {
  23. }
  24. public CertificateConfirmationContentBuilder(DefaultDigestAlgorithmIdentifierFinder digestAlgFinder)
  25. {
  26. this.m_digestAlgFinder = digestAlgFinder;
  27. }
  28. public CertificateConfirmationContentBuilder AddAcceptedCertificate(X509Certificate certHolder,
  29. BigInteger certReqId)
  30. {
  31. m_acceptedCerts.Add(certHolder);
  32. m_acceptedReqIDs.Add(certReqId);
  33. return this;
  34. }
  35. public CertificateConfirmationContent Build()
  36. {
  37. Asn1EncodableVector v = new Asn1EncodableVector();
  38. for (int i = 0; i != m_acceptedCerts.Count; i++)
  39. {
  40. X509Certificate cert = m_acceptedCerts[i];
  41. BigInteger reqID = m_acceptedReqIDs[i];
  42. AlgorithmIdentifier algorithmIdentifier = SigAlgFinder.Find(cert.SigAlgName);
  43. if (null == algorithmIdentifier)
  44. throw new CmpException("cannot find algorithm identifier for signature name");
  45. AlgorithmIdentifier digAlg = m_digestAlgFinder.Find(algorithmIdentifier);
  46. if (null == digAlg)
  47. throw new CmpException("cannot find algorithm for digest from signature");
  48. byte[] digest = DigestUtilities.CalculateDigest(digAlg.Algorithm, cert.GetEncoded());
  49. v.Add(new CertStatus(digest, reqID));
  50. }
  51. return new CertificateConfirmationContent(CertConfirmContent.GetInstance(new DerSequence(v)),
  52. m_digestAlgFinder);
  53. }
  54. }
  55. }
  56. #pragma warning restore
  57. #endif