NotationData.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.IO;
  5. using System.Text;
  6. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Bcpg.Sig
  7. {
  8. /**
  9. * Class provided a NotationData object according to
  10. * RFC2440, Chapter 5.2.3.15. Notation Data
  11. */
  12. public class NotationData
  13. : SignatureSubpacket
  14. {
  15. public const int HeaderFlagLength = 4;
  16. public const int HeaderNameLength = 2;
  17. public const int HeaderValueLength = 2;
  18. public NotationData(
  19. bool critical,
  20. bool isLongLength,
  21. byte[] data)
  22. : base(SignatureSubpacketTag.NotationData, critical, isLongLength, data)
  23. {
  24. }
  25. public NotationData(
  26. bool critical,
  27. bool humanReadable,
  28. string notationName,
  29. string notationValue)
  30. : base(SignatureSubpacketTag.NotationData, critical, false,
  31. CreateData(humanReadable, notationName, notationValue))
  32. {
  33. }
  34. private static byte[] CreateData(
  35. bool humanReadable,
  36. string notationName,
  37. string notationValue)
  38. {
  39. MemoryStream os = new MemoryStream();
  40. // (4 octets of flags, 2 octets of name length (M),
  41. // 2 octets of value length (N),
  42. // M octets of name data,
  43. // N octets of value data)
  44. // flags
  45. os.WriteByte(humanReadable ? (byte)0x80 : (byte)0x00);
  46. os.WriteByte(0x0);
  47. os.WriteByte(0x0);
  48. os.WriteByte(0x0);
  49. byte[] nameData, valueData = null;
  50. int nameLength, valueLength;
  51. nameData = Encoding.UTF8.GetBytes(notationName);
  52. nameLength = System.Math.Min(nameData.Length, 0xFF);
  53. valueData = Encoding.UTF8.GetBytes(notationValue);
  54. valueLength = System.Math.Min(valueData.Length, 0xFF);
  55. // name length
  56. os.WriteByte((byte)(nameLength >> 8));
  57. os.WriteByte((byte)(nameLength >> 0));
  58. // value length
  59. os.WriteByte((byte)(valueLength >> 8));
  60. os.WriteByte((byte)(valueLength >> 0));
  61. // name
  62. os.Write(nameData, 0, nameLength);
  63. // value
  64. os.Write(valueData, 0, valueLength);
  65. return os.ToArray();
  66. }
  67. public bool IsHumanReadable
  68. {
  69. get { return data[0] == (byte)0x80; }
  70. }
  71. public string GetNotationName()
  72. {
  73. int nameLength = ((data[HeaderFlagLength] << 8) + (data[HeaderFlagLength + 1] << 0));
  74. int namePos = HeaderFlagLength + HeaderNameLength + HeaderValueLength;
  75. return Encoding.UTF8.GetString(data, namePos, nameLength);
  76. }
  77. public string GetNotationValue()
  78. {
  79. int nameLength = ((data[HeaderFlagLength] << 8) + (data[HeaderFlagLength + 1] << 0));
  80. int valueLength = ((data[HeaderFlagLength + HeaderNameLength] << 8) + (data[HeaderFlagLength + HeaderNameLength + 1] << 0));
  81. int valuePos = HeaderFlagLength + HeaderNameLength + HeaderValueLength + nameLength;
  82. return Encoding.UTF8.GetString(data, valuePos, valueLength);
  83. }
  84. public byte[] GetNotationValueBytes()
  85. {
  86. int nameLength = ((data[HeaderFlagLength] << 8) + (data[HeaderFlagLength + 1] << 0));
  87. int valueLength = ((data[HeaderFlagLength + HeaderNameLength] << 8) + (data[HeaderFlagLength + HeaderNameLength + 1] << 0));
  88. int valuePos = HeaderFlagLength + HeaderNameLength + HeaderValueLength + nameLength;
  89. byte[] bytes = new byte[valueLength];
  90. Array.Copy(data, valuePos, bytes, 0, valueLength);
  91. return bytes;
  92. }
  93. }
  94. }
  95. #pragma warning restore
  96. #endif