IetfAttrSyntax.cs 3.7 KB

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