123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320 |
- #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
- #pragma warning disable
- using System;
- using System.IO;
- using System.Text;
- using Best.HTTP.SecureProtocol.Org.BouncyCastle.Math;
- using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities;
- namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1
- {
- public class Asn1RelativeOid
- : Asn1Object
- {
- internal class Meta : Asn1UniversalType
- {
- internal static readonly Asn1UniversalType Instance = new Meta();
- private Meta() : base(typeof(Asn1RelativeOid), Asn1Tags.RelativeOid) {}
- internal override Asn1Object FromImplicitPrimitive(DerOctetString octetString)
- {
- return CreatePrimitive(octetString.GetOctets(), false);
- }
- }
- public static Asn1RelativeOid FromContents(byte[] contents)
- {
- return CreatePrimitive(contents, true);
- }
- public static Asn1RelativeOid GetInstance(object obj)
- {
- if (obj == null)
- return null;
- if (obj is Asn1RelativeOid asn1RelativeOid)
- return asn1RelativeOid;
- if (obj is IAsn1Convertible asn1Convertible)
- {
- Asn1Object asn1Object = asn1Convertible.ToAsn1Object();
- if (asn1Object is Asn1RelativeOid converted)
- return converted;
- }
- else if (obj is byte[] bytes)
- {
- try
- {
- return (Asn1RelativeOid)FromByteArray(bytes);
- }
- catch (IOException e)
- {
- throw new ArgumentException("failed to construct relative OID from byte[]: " + e.Message);
- }
- }
- throw new ArgumentException("illegal object in GetInstance: " + Org.BouncyCastle.Utilities.Platform.GetTypeName(obj), "obj");
- }
- public static Asn1RelativeOid GetInstance(Asn1TaggedObject taggedObject, bool declaredExplicit)
- {
- return (Asn1RelativeOid)Meta.Instance.GetContextInstance(taggedObject, declaredExplicit);
- }
- private const long LongLimit = (long.MaxValue >> 7) - 0x7F;
- private readonly string identifier;
- private byte[] contents;
- public Asn1RelativeOid(string identifier)
- {
- if (identifier == null)
- throw new ArgumentNullException("identifier");
- if (!IsValidIdentifier(identifier, 0))
- throw new FormatException("string " + identifier + " not a relative OID");
- this.identifier = identifier;
- }
- private Asn1RelativeOid(Asn1RelativeOid oid, string branchID)
- {
- if (!IsValidIdentifier(branchID, 0))
- throw new FormatException("string " + branchID + " not a valid relative OID branch");
- this.identifier = oid.Id + "." + branchID;
- }
- private Asn1RelativeOid(byte[] contents, bool clone)
- {
- this.identifier = ParseContents(contents);
- this.contents = clone ? Arrays.Clone(contents) : contents;
- }
- public virtual Asn1RelativeOid Branch(string branchID)
- {
- return new Asn1RelativeOid(this, branchID);
- }
- public string Id
- {
- get { return identifier; }
- }
- public override string ToString()
- {
- return identifier;
- }
- protected override bool Asn1Equals(Asn1Object asn1Object)
- {
- Asn1RelativeOid that = asn1Object as Asn1RelativeOid;
- return null != that
- && this.identifier == that.identifier;
- }
- protected override int Asn1GetHashCode()
- {
- return identifier.GetHashCode();
- }
- internal override IAsn1Encoding GetEncoding(int encoding)
- {
- return new PrimitiveEncoding(Asn1Tags.Universal, Asn1Tags.RelativeOid, GetContents());
- }
- internal override IAsn1Encoding GetEncodingImplicit(int encoding, int tagClass, int tagNo)
- {
- return new PrimitiveEncoding(tagClass, tagNo, GetContents());
- }
- private void DoOutput(MemoryStream bOut)
- {
- OidTokenizer tok = new OidTokenizer(identifier);
- while (tok.HasMoreTokens)
- {
- string token = tok.NextToken();
- if (token.Length <= 18)
- {
- WriteField(bOut, long.Parse(token));
- }
- else
- {
- WriteField(bOut, new BigInteger(token));
- }
- }
- }
- private byte[] GetContents()
- {
- lock (this)
- {
- if (contents == null)
- {
- MemoryStream bOut = new MemoryStream();
- DoOutput(bOut);
- contents = bOut.ToArray();
- }
- return contents;
- }
- }
- internal static Asn1RelativeOid CreatePrimitive(byte[] contents, bool clone)
- {
- return new Asn1RelativeOid(contents, clone);
- }
- internal static bool IsValidIdentifier(string identifier, int from)
- {
- int digitCount = 0;
- int pos = identifier.Length;
- while (--pos >= from)
- {
- char ch = identifier[pos];
- if (ch == '.')
- {
- if (0 == digitCount || (digitCount > 1 && identifier[pos + 1] == '0'))
- return false;
- digitCount = 0;
- }
- else if ('0' <= ch && ch <= '9')
- {
- ++digitCount;
- }
- else
- {
- return false;
- }
- }
- if (0 == digitCount || (digitCount > 1 && identifier[pos + 1] == '0'))
- return false;
- return true;
- }
- internal static void WriteField(Stream outputStream, long fieldValue)
- {
- #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
- Span<byte> result = stackalloc byte[9];
- #else
- byte[] result = new byte[9];
- #endif
- int pos = 8;
- result[pos] = (byte)((int)fieldValue & 0x7F);
- while (fieldValue >= (1L << 7))
- {
- fieldValue >>= 7;
- result[--pos] = (byte)((int)fieldValue | 0x80);
- }
- #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
- outputStream.Write(result[pos..]);
- #else
- outputStream.Write(result, pos, 9 - pos);
- #endif
- }
- internal static void WriteField(Stream outputStream, BigInteger fieldValue)
- {
- int byteCount = (fieldValue.BitLength + 6) / 7;
- if (byteCount == 0)
- {
- outputStream.WriteByte(0);
- }
- else
- {
- BigInteger tmpValue = fieldValue;
- #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
- Span<byte> tmp = byteCount <= 16
- ? stackalloc byte[byteCount]
- : new byte[byteCount];
- #else
- byte[] tmp = new byte[byteCount];
- #endif
- for (int i = byteCount - 1; i >= 0; i--)
- {
- tmp[i] = (byte)(tmpValue.IntValue | 0x80);
- tmpValue = tmpValue.ShiftRight(7);
- }
- tmp[byteCount - 1] &= 0x7F;
- #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
- outputStream.Write(tmp);
- #else
- outputStream.Write(tmp, 0, tmp.Length);
- #endif
- }
- }
- private static string ParseContents(byte[] contents)
- {
- StringBuilder objId = new StringBuilder();
- long value = 0;
- BigInteger bigValue = null;
- bool first = true;
- for (int i = 0; i != contents.Length; i++)
- {
- int b = contents[i];
- if (value <= LongLimit)
- {
- value += b & 0x7F;
- if ((b & 0x80) == 0)
- {
- if (first)
- {
- first = false;
- }
- else
- {
- objId.Append('.');
- }
- objId.Append(value);
- value = 0;
- }
- else
- {
- value <<= 7;
- }
- }
- else
- {
- if (bigValue == null)
- {
- bigValue = BigInteger.ValueOf(value);
- }
- bigValue = bigValue.Or(BigInteger.ValueOf(b & 0x7F));
- if ((b & 0x80) == 0)
- {
- if (first)
- {
- first = false;
- }
- else
- {
- objId.Append('.');
- }
- objId.Append(bigValue);
- bigValue = null;
- value = 0;
- }
- else
- {
- bigValue = bigValue.ShiftLeft(7);
- }
- }
- }
- return objId.ToString();
- }
- }
- }
- #pragma warning restore
- #endif
|