123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
- using System;
- using System.Collections;
- using System.IO;
- using Org.BouncyCastle.Utilities;
- namespace Org.BouncyCastle.Asn1
- {
- public class BerOctetString
- : DerOctetString, IEnumerable
- {
- public static BerOctetString FromSequence(Asn1Sequence seq)
- {
- IList v = Org.BouncyCastle.Utilities.Platform.CreateArrayList();
- foreach (Asn1Encodable obj in seq)
- {
- v.Add(obj);
- }
- return new BerOctetString(v);
- }
- private const int MaxLength = 1000;
- /**
- * convert a vector of octet strings into a single byte string
- */
- private static byte[] ToBytes(
- IEnumerable octs)
- {
- MemoryStream bOut = new MemoryStream();
- foreach (DerOctetString o in octs)
- {
- byte[] octets = o.GetOctets();
- bOut.Write(octets, 0, octets.Length);
- }
- return bOut.ToArray();
- }
- private readonly IEnumerable octs;
- /// <param name="str">The octets making up the octet string.</param>
- public BerOctetString(
- byte[] str)
- : base(str)
- {
- }
- public BerOctetString(
- IEnumerable octets)
- : base(ToBytes(octets))
- {
- this.octs = octets;
- }
- public BerOctetString(
- Asn1Object obj)
- : base(obj)
- {
- }
- public BerOctetString(
- Asn1Encodable obj)
- : base(obj.ToAsn1Object())
- {
- }
- public override byte[] GetOctets()
- {
- return str;
- }
- /**
- * return the DER octets that make up this string.
- */
- public IEnumerator GetEnumerator()
- {
- if (octs == null)
- {
- return GenerateOcts().GetEnumerator();
- }
- return octs.GetEnumerator();
- }
- [Obsolete("Use GetEnumerator() instead")]
- public IEnumerator GetObjects()
- {
- return GetEnumerator();
- }
- private IList GenerateOcts()
- {
- IList vec = Org.BouncyCastle.Utilities.Platform.CreateArrayList();
- for (int i = 0; i < str.Length; i += MaxLength)
- {
- int end = System.Math.Min(str.Length, i + MaxLength);
- byte[] nStr = new byte[end - i];
- Array.Copy(str, i, nStr, 0, nStr.Length);
- vec.Add(new DerOctetString(nStr));
- }
- return vec;
- }
- internal override void Encode(
- DerOutputStream derOut)
- {
- if (derOut is Asn1OutputStream || derOut is BerOutputStream)
- {
- derOut.WriteByte(Asn1Tags.Constructed | Asn1Tags.OctetString);
- derOut.WriteByte(0x80);
- //
- // write out the octet array
- //
- foreach (DerOctetString oct in this)
- {
- derOut.WriteObject(oct);
- }
- derOut.WriteByte(0x00);
- derOut.WriteByte(0x00);
- }
- else
- {
- base.Encode(derOut);
- }
- }
- }
- }
- #endif
|