X509Extension.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. using System;
  3. namespace Org.BouncyCastle.Asn1.X509
  4. {
  5. /**
  6. * an object for the elements in the X.509 V3 extension block.
  7. */
  8. public class X509Extension
  9. {
  10. internal bool critical;
  11. internal Asn1OctetString value;
  12. public X509Extension(
  13. DerBoolean critical,
  14. Asn1OctetString value)
  15. {
  16. if (critical == null)
  17. {
  18. throw new ArgumentNullException("critical");
  19. }
  20. this.critical = critical.IsTrue;
  21. this.value = value;
  22. }
  23. public X509Extension(
  24. bool critical,
  25. Asn1OctetString value)
  26. {
  27. this.critical = critical;
  28. this.value = value;
  29. }
  30. public bool IsCritical { get { return critical; } }
  31. public Asn1OctetString Value { get { return value; } }
  32. public Asn1Encodable GetParsedValue()
  33. {
  34. return ConvertValueToObject(this);
  35. }
  36. public override int GetHashCode()
  37. {
  38. int vh = this.Value.GetHashCode();
  39. return IsCritical ? vh : ~vh;
  40. }
  41. public override bool Equals(
  42. object obj)
  43. {
  44. X509Extension other = obj as X509Extension;
  45. if (other == null)
  46. {
  47. return false;
  48. }
  49. return Value.Equals(other.Value) && IsCritical == other.IsCritical;
  50. }
  51. /// <sumary>Convert the value of the passed in extension to an object.</sumary>
  52. /// <param name="ext">The extension to parse.</param>
  53. /// <returns>The object the value string contains.</returns>
  54. /// <exception cref="ArgumentException">If conversion is not possible.</exception>
  55. public static Asn1Object ConvertValueToObject(
  56. X509Extension ext)
  57. {
  58. try
  59. {
  60. return Asn1Object.FromByteArray(ext.Value.GetOctets());
  61. }
  62. catch (Exception e)
  63. {
  64. throw new ArgumentException("can't convert extension", e);
  65. }
  66. }
  67. }
  68. }
  69. #endif