IetfAttrSyntax.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509
  7. {
  8. /**
  9. * Implementation of <code>IetfAttrSyntax</code> as specified by RFC3281.
  10. */
  11. public class IetfAttrSyntax
  12. : Asn1Encodable
  13. {
  14. public const int ValueOctets = 1;
  15. public const int ValueOid = 2;
  16. public const int ValueUtf8 = 3;
  17. internal readonly GeneralNames policyAuthority;
  18. internal readonly Asn1EncodableVector values = new Asn1EncodableVector();
  19. internal int valueChoice = -1;
  20. /**
  21. *
  22. */
  23. public IetfAttrSyntax(
  24. Asn1Sequence seq)
  25. {
  26. int i = 0;
  27. if (seq[0] is Asn1TaggedObject)
  28. {
  29. policyAuthority = GeneralNames.GetInstance(((Asn1TaggedObject)seq[0]), false);
  30. i++;
  31. }
  32. else if (seq.Count == 2)
  33. { // VOMS fix
  34. policyAuthority = GeneralNames.GetInstance(seq[0]);
  35. i++;
  36. }
  37. if (!(seq[i] is Asn1Sequence))
  38. {
  39. throw new ArgumentException("Non-IetfAttrSyntax encoding");
  40. }
  41. seq = (Asn1Sequence) seq[i];
  42. foreach (Asn1Object obj in seq)
  43. {
  44. int type;
  45. if (obj is DerObjectIdentifier)
  46. {
  47. type = ValueOid;
  48. }
  49. else if (obj is DerUtf8String)
  50. {
  51. type = ValueUtf8;
  52. }
  53. else if (obj is DerOctetString)
  54. {
  55. type = ValueOctets;
  56. }
  57. else
  58. {
  59. throw new ArgumentException("Bad value type encoding IetfAttrSyntax");
  60. }
  61. if (valueChoice < 0)
  62. {
  63. valueChoice = type;
  64. }
  65. if (type != valueChoice)
  66. {
  67. throw new ArgumentException("Mix of value types in IetfAttrSyntax");
  68. }
  69. values.Add(obj);
  70. }
  71. }
  72. public GeneralNames PolicyAuthority
  73. {
  74. get { return policyAuthority; }
  75. }
  76. public int ValueType
  77. {
  78. get { return valueChoice; }
  79. }
  80. public object[] GetValues()
  81. {
  82. if (this.ValueType == ValueOctets)
  83. {
  84. Asn1OctetString[] tmp = new Asn1OctetString[values.Count];
  85. for (int i = 0; i != tmp.Length; i++)
  86. {
  87. tmp[i] = (Asn1OctetString) values[i];
  88. }
  89. return tmp;
  90. }
  91. if (this.ValueType == ValueOid)
  92. {
  93. DerObjectIdentifier[] tmp = new DerObjectIdentifier[values.Count];
  94. for (int i = 0; i != tmp.Length; i++)
  95. {
  96. tmp[i] = (DerObjectIdentifier) values[i];
  97. }
  98. return tmp;
  99. }
  100. {
  101. DerUtf8String[] tmp = new DerUtf8String[values.Count];
  102. for (int i = 0; i != tmp.Length; i++)
  103. {
  104. tmp[i] = (DerUtf8String) values[i];
  105. }
  106. return tmp;
  107. }
  108. }
  109. /**
  110. *
  111. * <pre>
  112. *
  113. * IetfAttrSyntax ::= Sequence {
  114. * policyAuthority [0] GeneralNames OPTIONAL,
  115. * values Sequence OF CHOICE {
  116. * octets OCTET STRING,
  117. * oid OBJECT IDENTIFIER,
  118. * string UTF8String
  119. * }
  120. * }
  121. *
  122. * </pre>
  123. */
  124. public override Asn1Object ToAsn1Object()
  125. {
  126. Asn1EncodableVector v = new Asn1EncodableVector();
  127. v.AddOptionalTagged(true, 0, policyAuthority);
  128. v.Add(new DerSequence(values));
  129. return new DerSequence(v);
  130. }
  131. }
  132. }
  133. #pragma warning restore
  134. #endif