DERExternal.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.IO;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  6. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1
  7. {
  8. /**
  9. * Class representing the DER-type External
  10. */
  11. public class DerExternal
  12. : Asn1Object
  13. {
  14. private DerObjectIdentifier directReference;
  15. private DerInteger indirectReference;
  16. private Asn1Object dataValueDescriptor;
  17. private int encoding;
  18. private Asn1Object externalContent;
  19. public DerExternal(
  20. Asn1EncodableVector vector)
  21. {
  22. int offset = 0;
  23. Asn1Object enc = GetObjFromVector(vector, offset);
  24. if (enc is DerObjectIdentifier)
  25. {
  26. directReference = (DerObjectIdentifier)enc;
  27. offset++;
  28. enc = GetObjFromVector(vector, offset);
  29. }
  30. if (enc is DerInteger)
  31. {
  32. indirectReference = (DerInteger) enc;
  33. offset++;
  34. enc = GetObjFromVector(vector, offset);
  35. }
  36. if (!(enc is Asn1TaggedObject))
  37. {
  38. dataValueDescriptor = enc;
  39. offset++;
  40. enc = GetObjFromVector(vector, offset);
  41. }
  42. if (vector.Count != offset + 1)
  43. throw new ArgumentException("input vector too large", "vector");
  44. if (!(enc is Asn1TaggedObject))
  45. throw new ArgumentException("No tagged object found in vector. Structure doesn't seem to be of type External", "vector");
  46. Asn1TaggedObject obj = (Asn1TaggedObject)enc;
  47. // Use property accessor to include check on value
  48. Encoding = obj.TagNo;
  49. if (encoding < 0 || encoding > 2)
  50. throw new InvalidOperationException("invalid encoding value");
  51. externalContent = obj.GetObject();
  52. }
  53. /**
  54. * Creates a new instance of DerExternal
  55. * See X.690 for more informations about the meaning of these parameters
  56. * @param directReference The direct reference or <code>null</code> if not set.
  57. * @param indirectReference The indirect reference or <code>null</code> if not set.
  58. * @param dataValueDescriptor The data value descriptor or <code>null</code> if not set.
  59. * @param externalData The external data in its encoded form.
  60. */
  61. public DerExternal(DerObjectIdentifier directReference, DerInteger indirectReference, Asn1Object dataValueDescriptor, DerTaggedObject externalData)
  62. : this(directReference, indirectReference, dataValueDescriptor, externalData.TagNo, externalData.ToAsn1Object())
  63. {
  64. }
  65. /**
  66. * Creates a new instance of DerExternal.
  67. * See X.690 for more informations about the meaning of these parameters
  68. * @param directReference The direct reference or <code>null</code> if not set.
  69. * @param indirectReference The indirect reference or <code>null</code> if not set.
  70. * @param dataValueDescriptor The data value descriptor or <code>null</code> if not set.
  71. * @param encoding The encoding to be used for the external data
  72. * @param externalData The external data
  73. */
  74. public DerExternal(DerObjectIdentifier directReference, DerInteger indirectReference, Asn1Object dataValueDescriptor, int encoding, Asn1Object externalData)
  75. {
  76. DirectReference = directReference;
  77. IndirectReference = indirectReference;
  78. DataValueDescriptor = dataValueDescriptor;
  79. Encoding = encoding;
  80. ExternalContent = externalData.ToAsn1Object();
  81. }
  82. internal override int EncodedLength(bool withID)
  83. {
  84. int contentsLength = 0;
  85. if (directReference != null)
  86. {
  87. contentsLength += directReference.EncodedLength(true);
  88. }
  89. if (indirectReference != null)
  90. {
  91. contentsLength += indirectReference.EncodedLength(true);
  92. }
  93. if (dataValueDescriptor != null)
  94. {
  95. // TODO[asn1]
  96. //contentsLength += dataValueDescriptor.ToDerObject().EncodedLength(true);
  97. contentsLength += dataValueDescriptor.GetDerEncoded().Length;
  98. }
  99. // TODO[asn1]
  100. //contentsLength += new DerTaggedObject(true, encoding, externalContent).EncodedLength(true);
  101. contentsLength += new DerTaggedObject(Asn1Tags.External, externalContent).EncodedLength(true);
  102. return Asn1OutputStream.GetLengthOfEncodingDL(withID, contentsLength);
  103. }
  104. internal override void Encode(Asn1OutputStream asn1Out, bool withID)
  105. {
  106. MemoryStream ms = new MemoryStream();
  107. WriteEncodable(ms, directReference);
  108. WriteEncodable(ms, indirectReference);
  109. WriteEncodable(ms, dataValueDescriptor);
  110. WriteEncodable(ms, new DerTaggedObject(Asn1Tags.External, externalContent));
  111. asn1Out.WriteEncodingDL(withID, Asn1Tags.Constructed | Asn1Tags.External, ms.ToArray());
  112. }
  113. protected override int Asn1GetHashCode()
  114. {
  115. int ret = externalContent.GetHashCode();
  116. if (directReference != null)
  117. {
  118. ret ^= directReference.GetHashCode();
  119. }
  120. if (indirectReference != null)
  121. {
  122. ret ^= indirectReference.GetHashCode();
  123. }
  124. if (dataValueDescriptor != null)
  125. {
  126. ret ^= dataValueDescriptor.GetHashCode();
  127. }
  128. return ret;
  129. }
  130. protected override bool Asn1Equals(
  131. Asn1Object asn1Object)
  132. {
  133. if (this == asn1Object)
  134. return true;
  135. DerExternal other = asn1Object as DerExternal;
  136. if (other == null)
  137. return false;
  138. return BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.Equals(directReference, other.directReference)
  139. && BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.Equals(indirectReference, other.indirectReference)
  140. && BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.Equals(dataValueDescriptor, other.dataValueDescriptor)
  141. && externalContent.Equals(other.externalContent);
  142. }
  143. public Asn1Object DataValueDescriptor
  144. {
  145. get { return dataValueDescriptor; }
  146. set { this.dataValueDescriptor = value; }
  147. }
  148. public DerObjectIdentifier DirectReference
  149. {
  150. get { return directReference; }
  151. set { this.directReference = value; }
  152. }
  153. /**
  154. * The encoding of the content. Valid values are
  155. * <ul>
  156. * <li><code>0</code> single-ASN1-type</li>
  157. * <li><code>1</code> OCTET STRING</li>
  158. * <li><code>2</code> BIT STRING</li>
  159. * </ul>
  160. */
  161. public int Encoding
  162. {
  163. get
  164. {
  165. return encoding;
  166. }
  167. set
  168. {
  169. if (encoding < 0 || encoding > 2)
  170. throw new InvalidOperationException("invalid encoding value: " + encoding);
  171. this.encoding = value;
  172. }
  173. }
  174. public Asn1Object ExternalContent
  175. {
  176. get { return externalContent; }
  177. set { this.externalContent = value; }
  178. }
  179. public DerInteger IndirectReference
  180. {
  181. get { return indirectReference; }
  182. set { this.indirectReference = value; }
  183. }
  184. private static Asn1Object GetObjFromVector(Asn1EncodableVector v, int index)
  185. {
  186. if (v.Count <= index)
  187. throw new ArgumentException("too few objects in input vector", "v");
  188. return v[index].ToAsn1Object();
  189. }
  190. private static void WriteEncodable(MemoryStream ms, Asn1Encodable e)
  191. {
  192. if (e != null)
  193. {
  194. byte[] bs = e.GetDerEncoded();
  195. ms.Write(bs, 0, bs.Length);
  196. }
  197. }
  198. }
  199. }
  200. #pragma warning restore
  201. #endif