SignerInfoGenerator.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Cms;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509;
  7. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto;
  8. using BestHTTP.SecureProtocol.Org.BouncyCastle.X509;
  9. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Cms
  10. {
  11. internal interface ISignerInfoGenerator
  12. {
  13. SignerInfo Generate(DerObjectIdentifier contentType, AlgorithmIdentifier digestAlgorithm,
  14. byte[] calculatedDigest);
  15. }
  16. public class SignerInfoGenerator
  17. {
  18. internal X509Certificate certificate;
  19. internal ISignatureFactory contentSigner;
  20. internal SignerIdentifier sigId;
  21. internal CmsAttributeTableGenerator signedGen;
  22. internal CmsAttributeTableGenerator unsignedGen;
  23. private bool isDirectSignature;
  24. internal SignerInfoGenerator(SignerIdentifier sigId, ISignatureFactory signerFactory): this(sigId, signerFactory, false)
  25. {
  26. }
  27. internal SignerInfoGenerator(SignerIdentifier sigId, ISignatureFactory signerFactory, bool isDirectSignature)
  28. {
  29. this.sigId = sigId;
  30. this.contentSigner = signerFactory;
  31. this.isDirectSignature = isDirectSignature;
  32. if (this.isDirectSignature)
  33. {
  34. this.signedGen = null;
  35. this.unsignedGen = null;
  36. }
  37. else
  38. {
  39. this.signedGen = new DefaultSignedAttributeTableGenerator();
  40. this.unsignedGen = null;
  41. }
  42. }
  43. internal SignerInfoGenerator(SignerIdentifier sigId, ISignatureFactory contentSigner, CmsAttributeTableGenerator signedGen, CmsAttributeTableGenerator unsignedGen)
  44. {
  45. this.sigId = sigId;
  46. this.contentSigner = contentSigner;
  47. this.signedGen = signedGen;
  48. this.unsignedGen = unsignedGen;
  49. this.isDirectSignature = false;
  50. }
  51. internal void setAssociatedCertificate(X509Certificate certificate)
  52. {
  53. this.certificate = certificate;
  54. }
  55. }
  56. public class SignerInfoGeneratorBuilder
  57. {
  58. private bool directSignature;
  59. private CmsAttributeTableGenerator signedGen;
  60. private CmsAttributeTableGenerator unsignedGen;
  61. public SignerInfoGeneratorBuilder()
  62. {
  63. }
  64. /**
  65. * If the passed in flag is true, the signer signature will be based on the data, not
  66. * a collection of signed attributes, and no signed attributes will be included.
  67. *
  68. * @return the builder object
  69. */
  70. public SignerInfoGeneratorBuilder SetDirectSignature(bool hasNoSignedAttributes)
  71. {
  72. this.directSignature = hasNoSignedAttributes;
  73. return this;
  74. }
  75. /**
  76. * Provide a custom signed attribute generator.
  77. *
  78. * @param signedGen a generator of signed attributes.
  79. * @return the builder object
  80. */
  81. public SignerInfoGeneratorBuilder WithSignedAttributeGenerator(CmsAttributeTableGenerator signedGen)
  82. {
  83. this.signedGen = signedGen;
  84. return this;
  85. }
  86. /**
  87. * Provide a generator of unsigned attributes.
  88. *
  89. * @param unsignedGen a generator for signed attributes.
  90. * @return the builder object
  91. */
  92. public SignerInfoGeneratorBuilder WithUnsignedAttributeGenerator(CmsAttributeTableGenerator unsignedGen)
  93. {
  94. this.unsignedGen = unsignedGen;
  95. return this;
  96. }
  97. /**
  98. * Build a generator with the passed in X.509 certificate issuer and serial number as the signerIdentifier.
  99. *
  100. * @param contentSigner operator for generating the final signature in the SignerInfo with.
  101. * @param certificate X.509 certificate related to the contentSigner.
  102. * @return a SignerInfoGenerator
  103. * @throws OperatorCreationException if the generator cannot be built.
  104. */
  105. public SignerInfoGenerator Build(ISignatureFactory contentSigner, X509Certificate certificate)
  106. {
  107. SignerIdentifier sigId = new SignerIdentifier(new IssuerAndSerialNumber(certificate.IssuerDN, new DerInteger(certificate.SerialNumber)));
  108. SignerInfoGenerator sigInfoGen = CreateGenerator(contentSigner, sigId);
  109. sigInfoGen.setAssociatedCertificate(certificate);
  110. return sigInfoGen;
  111. }
  112. /**
  113. * Build a generator with the passed in subjectKeyIdentifier as the signerIdentifier. If used you should
  114. * try to follow the calculation described in RFC 5280 section 4.2.1.2.
  115. *
  116. * @param signerFactory operator factory for generating the final signature in the SignerInfo with.
  117. * @param subjectKeyIdentifier key identifier to identify the public key for verifying the signature.
  118. * @return a SignerInfoGenerator
  119. */
  120. public SignerInfoGenerator Build(ISignatureFactory signerFactory, byte[] subjectKeyIdentifier)
  121. {
  122. SignerIdentifier sigId = new SignerIdentifier(new DerOctetString(subjectKeyIdentifier));
  123. return CreateGenerator(signerFactory, sigId);
  124. }
  125. private SignerInfoGenerator CreateGenerator(ISignatureFactory contentSigner, SignerIdentifier sigId)
  126. {
  127. if (directSignature)
  128. {
  129. return new SignerInfoGenerator(sigId, contentSigner, true);
  130. }
  131. if (signedGen != null || unsignedGen != null)
  132. {
  133. if (signedGen == null)
  134. {
  135. signedGen = new DefaultSignedAttributeTableGenerator();
  136. }
  137. return new SignerInfoGenerator(sigId, contentSigner, signedGen, unsignedGen);
  138. }
  139. return new SignerInfoGenerator(sigId, contentSigner);
  140. }
  141. }
  142. }
  143. #pragma warning restore
  144. #endif