DefaultSignedAttributeTableGenerator.cs 4.3 KB

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