DefaultAuthenticatedAttributeTableGenerator.cs 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 authenticated attributes generator.
  12. */
  13. public class DefaultAuthenticatedAttributeTableGenerator
  14. : CmsAttributeTableGenerator
  15. {
  16. private readonly IDictionary table;
  17. /**
  18. * Initialise to use all defaults
  19. */
  20. public DefaultAuthenticatedAttributeTableGenerator()
  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 DefaultAuthenticatedAttributeTableGenerator(
  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. /**
  42. * Create a standard attribute table from the passed in parameters - this will
  43. * normally include contentType and messageDigest. If the constructor
  44. * using an AttributeTable was used, entries in it for contentType and
  45. * messageDigest will override the generated ones.
  46. *
  47. * @param parameters source parameters for table generation.
  48. *
  49. * @return a filled in IDictionary of attributes.
  50. */
  51. protected virtual IDictionary CreateStandardAttributeTable(
  52. IDictionary parameters)
  53. {
  54. IDictionary std = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateHashtable(table);
  55. if (!std.Contains(CmsAttributes.ContentType))
  56. {
  57. DerObjectIdentifier contentType = (DerObjectIdentifier)
  58. parameters[CmsAttributeTableParameter.ContentType];
  59. Asn1.Cms.Attribute attr = new Asn1.Cms.Attribute(CmsAttributes.ContentType,
  60. new DerSet(contentType));
  61. std[attr.AttrType] = attr;
  62. }
  63. if (!std.Contains(CmsAttributes.MessageDigest))
  64. {
  65. byte[] messageDigest = (byte[])parameters[CmsAttributeTableParameter.Digest];
  66. Asn1.Cms.Attribute attr = new Asn1.Cms.Attribute(CmsAttributes.MessageDigest,
  67. new DerSet(new DerOctetString(messageDigest)));
  68. std[attr.AttrType] = attr;
  69. }
  70. return std;
  71. }
  72. /**
  73. * @param parameters source parameters
  74. * @return the populated attribute table
  75. */
  76. public virtual AttributeTable GetAttributes(
  77. IDictionary parameters)
  78. {
  79. IDictionary table = CreateStandardAttributeTable(parameters);
  80. return new AttributeTable(table);
  81. }
  82. }
  83. }
  84. #pragma warning restore
  85. #endif