DefaultAuthenticatedAttributeTableGenerator.cs 2.8 KB

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