DefaultSignedAttributeTableGenerator.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Collections.Generic;
  5. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1;
  6. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.Cms;
  7. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Cms
  8. {
  9. /**
  10. * Default signed attributes generator.
  11. */
  12. public class DefaultSignedAttributeTableGenerator
  13. : CmsAttributeTableGenerator
  14. {
  15. private readonly IDictionary<DerObjectIdentifier, object> m_table;
  16. /**
  17. * Initialise to use all defaults
  18. */
  19. public DefaultSignedAttributeTableGenerator()
  20. {
  21. m_table = new Dictionary<DerObjectIdentifier, object>();
  22. }
  23. /**
  24. * Initialise with some extra attributes or overrides.
  25. *
  26. * @param attributeTable initial attribute table to use.
  27. */
  28. public DefaultSignedAttributeTableGenerator(AttributeTable attributeTable)
  29. {
  30. if (attributeTable != null)
  31. {
  32. m_table = attributeTable.ToDictionary();
  33. }
  34. else
  35. {
  36. m_table = new Dictionary<DerObjectIdentifier, object>();
  37. }
  38. }
  39. /**
  40. * Create a standard attribute table from the passed in parameters - this will
  41. * normally include contentType, signingTime, and messageDigest. If the constructor
  42. * using an AttributeTable was used, entries in it for contentType, signingTime, and
  43. * messageDigest will override the generated ones.
  44. *
  45. * @param parameters source parameters for table generation.
  46. *
  47. * @return a filled in Dictionary of attributes.
  48. */
  49. protected virtual IDictionary<DerObjectIdentifier, object> CreateStandardAttributeTable(
  50. IDictionary<CmsAttributeTableParameter, object> parameters)
  51. {
  52. var std = new Dictionary<DerObjectIdentifier, object>(m_table);
  53. DoCreateStandardAttributeTable(parameters, std);
  54. return std;
  55. }
  56. private void DoCreateStandardAttributeTable(IDictionary<CmsAttributeTableParameter, object> parameters,
  57. IDictionary<DerObjectIdentifier, object> std)
  58. {
  59. // contentType will be absent if we're trying to generate a counter signature.
  60. if (parameters.ContainsKey(CmsAttributeTableParameter.ContentType))
  61. {
  62. if (!std.ContainsKey(CmsAttributes.ContentType))
  63. {
  64. DerObjectIdentifier contentType = (DerObjectIdentifier)
  65. parameters[CmsAttributeTableParameter.ContentType];
  66. Asn1.Cms.Attribute attr = new Asn1.Cms.Attribute(CmsAttributes.ContentType,
  67. new DerSet(contentType));
  68. std[attr.AttrType] = attr;
  69. }
  70. }
  71. if (!std.ContainsKey(CmsAttributes.SigningTime))
  72. {
  73. Asn1.Cms.Attribute attr = new Asn1.Cms.Attribute(CmsAttributes.SigningTime,
  74. new DerSet(new Time(DateTime.UtcNow)));
  75. std[attr.AttrType] = attr;
  76. }
  77. if (!std.ContainsKey(CmsAttributes.MessageDigest))
  78. {
  79. byte[] messageDigest = (byte[])parameters[CmsAttributeTableParameter.Digest];
  80. Asn1.Cms.Attribute attr = new Asn1.Cms.Attribute(CmsAttributes.MessageDigest,
  81. new DerSet(new DerOctetString(messageDigest)));
  82. std[attr.AttrType] = attr;
  83. }
  84. }
  85. /**
  86. * @param parameters source parameters
  87. * @return the populated attribute table
  88. */
  89. public virtual AttributeTable GetAttributes(IDictionary<CmsAttributeTableParameter, object> parameters)
  90. {
  91. var table = CreateStandardAttributeTable(parameters);
  92. return new AttributeTable(table);
  93. }
  94. }
  95. }
  96. #pragma warning restore
  97. #endif