123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243 |
- #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
- #pragma warning disable
- using System;
- using System.Collections;
- using System.Diagnostics;
- using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
- namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1
- {
- public class BerOctetString
- : DerOctetString, IEnumerable
- {
- private const int DefaultSegmentLimit = 1000;
- public static BerOctetString FromSequence(Asn1Sequence seq)
- {
- int count = seq.Count;
- Asn1OctetString[] v = new Asn1OctetString[count];
- for (int i = 0; i < count; ++i)
- {
- v[i] = GetInstance(seq[i]);
- }
- return new BerOctetString(v);
- }
- internal static byte[] FlattenOctetStrings(Asn1OctetString[] octetStrings)
- {
- int count = octetStrings.Length;
- switch (count)
- {
- case 0:
- return EmptyOctets;
- case 1:
- return octetStrings[0].str;
- default:
- {
- int totalOctets = 0;
- for (int i = 0; i < count; ++i)
- {
- totalOctets += octetStrings[i].str.Length;
- }
- byte[] str = new byte[totalOctets];
- int pos = 0;
- for (int i = 0; i < count; ++i)
- {
- byte[] octets = octetStrings[i].str;
- Array.Copy(octets, 0, str, pos, octets.Length);
- pos += octets.Length;
- }
- Debug.Assert(pos == totalOctets);
- return str;
- }
- }
- }
- private static Asn1OctetString[] ToOctetStringArray(IEnumerable e)
- {
- IList list = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateArrayList(e);
- int count = list.Count;
- Asn1OctetString[] v = new Asn1OctetString[count];
- for (int i = 0; i < count; ++i)
- {
- v[i] = GetInstance(list[i]);
- }
- return v;
- }
- private readonly int segmentLimit;
- private readonly Asn1OctetString[] elements;
- public BerOctetString(IEnumerable e)
- : this(ToOctetStringArray(e))
- {
- }
- public BerOctetString(byte[] str)
- : this(str, DefaultSegmentLimit)
- {
- }
- public BerOctetString(Asn1OctetString[] elements)
- : this(elements, DefaultSegmentLimit)
- {
- }
- public BerOctetString(byte[] str, int segmentLimit)
- : this(str, null, segmentLimit)
- {
- }
- public BerOctetString(Asn1OctetString[] elements, int segmentLimit)
- : this(FlattenOctetStrings(elements), elements, segmentLimit)
- {
- }
- private BerOctetString(byte[] octets, Asn1OctetString[] elements, int segmentLimit)
- : base(octets)
- {
- this.elements = elements;
- this.segmentLimit = segmentLimit;
- }
- /**
- * return the DER octets that make up this string.
- */
- public IEnumerator GetEnumerator()
- {
- if (elements == null)
- return new ChunkEnumerator(str, segmentLimit);
- return elements.GetEnumerator();
- }
- public IEnumerator GetObjects()
- {
- return GetEnumerator();
- }
- private bool IsConstructed
- {
- get { return null != elements || str.Length > segmentLimit; }
- }
- internal override int EncodedLength(bool withID)
- {
- throw BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateNotImplementedException("BerOctetString.EncodedLength");
- // TODO This depends on knowing it's not DER
- //if (!IsConstructed)
- // return EncodedLength(withID, str.Length);
- //int totalLength = withID ? 4 : 3;
- //if (null != elements)
- //{
- // for (int i = 0; i < elements.Length; ++i)
- // {
- // totalLength += elements[i].EncodedLength(true);
- // }
- //}
- //else
- //{
- // int fullSegments = str.Length / segmentLimit;
- // totalLength += fullSegments * EncodedLength(true, segmentLimit);
- // int lastSegmentLength = str.Length - (fullSegments * segmentLimit);
- // if (lastSegmentLength > 0)
- // {
- // totalLength += EncodedLength(true, lastSegmentLength);
- // }
- //}
- //return totalLength;
- }
- internal override void Encode(Asn1OutputStream asn1Out, bool withID)
- {
- if (!asn1Out.IsBer || !IsConstructed)
- {
- base.Encode(asn1Out, withID);
- return;
- }
- asn1Out.WriteIdentifier(withID, Asn1Tags.Constructed | Asn1Tags.OctetString);
- asn1Out.WriteByte(0x80);
- if (null != elements)
- {
- asn1Out.WritePrimitives(elements);
- }
- else
- {
- int pos = 0;
- while (pos < str.Length)
- {
- int segmentLength = System.Math.Min(str.Length - pos, segmentLimit);
- Encode(asn1Out, true, str, pos, segmentLength);
- pos += segmentLength;
- }
- }
- asn1Out.WriteByte(0x00);
- asn1Out.WriteByte(0x00);
- }
- private class ChunkEnumerator
- : IEnumerator
- {
- private readonly byte[] octets;
- private readonly int segmentLimit;
- private DerOctetString currentSegment = null;
- private int nextSegmentPos = 0;
- internal ChunkEnumerator(byte[] octets, int segmentLimit)
- {
- this.octets = octets;
- this.segmentLimit = segmentLimit;
- }
- public object Current
- {
- get
- {
- if (null == currentSegment)
- throw new InvalidOperationException();
- return currentSegment;
- }
- }
- public bool MoveNext()
- {
- if (nextSegmentPos >= octets.Length)
- {
- this.currentSegment = null;
- return false;
- }
- int length = System.Math.Min(octets.Length - nextSegmentPos, segmentLimit);
- byte[] segment = new byte[length];
- Array.Copy(octets, nextSegmentPos, segment, 0, length);
- this.currentSegment = new DerOctetString(segment);
- this.nextSegmentPos += length;
- return true;
- }
- public void Reset()
- {
- this.currentSegment = null;
- this.nextSegmentPos = 0;
- }
- }
- }
- }
- #pragma warning restore
- #endif
|