123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218 |
- #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
- #pragma warning disable
- using System;
- using System.Collections;
- using System.IO;
- using System.Text;
- using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
- using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Encoders;
- namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.X500.Style
- {
- public abstract class IetfUtilities
- {
- public static string ValueToString(Asn1Encodable value)
- {
- StringBuilder vBuf = new StringBuilder();
- if (value is IAsn1String && !(value is DerUniversalString))
- {
- string v = ((IAsn1String)value).GetString();
- if (v.Length > 0 && v[0] == '#')
- {
- vBuf.Append('\\');
- }
- vBuf.Append(v);
- }
- else
- {
- try
- {
- vBuf.Append('#');
- vBuf.Append(Hex.ToHexString(value.ToAsn1Object().GetEncoded(Asn1Encodable.Der)));
- }
- catch (IOException e)
- {
- throw new ArgumentException("Other value has no encoded form", e);
- }
- }
- int end = vBuf.Length;
- int index = 0;
- if (vBuf.Length >= 2 && vBuf[0] == '\\' && vBuf[1] == '#')
- {
- index += 2;
- }
- while (index != end)
- {
- switch (vBuf[index])
- {
- case ',':
- case '"':
- case '\\':
- case '+':
- case '=':
- case '<':
- case '>':
- case ';':
- {
- vBuf.Insert(index, "\\");
- index += 2;
- ++end;
- break;
- }
- default:
- {
- ++index;
- break;
- }
- }
- }
- int start = 0;
- if (vBuf.Length > 0)
- {
- while (vBuf.Length > start && vBuf[start] == ' ')
- {
- vBuf.Insert(start, "\\");
- start += 2;
- }
- }
- int endBuf = vBuf.Length - 1;
- while (endBuf >= 0 && vBuf[endBuf] == ' ')
- {
- vBuf.Insert(endBuf, "\\");
- endBuf--;
- }
- return vBuf.ToString();
- }
- public static string Canonicalize(string s)
- {
- string value = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.ToLowerInvariant(s);
- if (value.Length > 0 && value[0] == '#')
- {
- Asn1Object obj = DecodeObject(value);
- if (obj is IAsn1String)
- {
- value = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.ToLowerInvariant(((IAsn1String)obj).GetString());
- }
- }
- if (value.Length > 1)
- {
- int start = 0;
- while (start + 1 < value.Length && value[start] == '\\' && value[start + 1] == ' ')
- {
- start += 2;
- }
- int end = value.Length - 1;
- while (end - 1 > 0 && value[end - 1] == '\\' && value[end] == ' ')
- {
- end -= 2;
- }
- if (start > 0 || end < value.Length - 1)
- {
- value = value.Substring(start, end + 1 - start);
- }
- }
- return StripInternalSpaces(value);
- }
- public static string CanonicalString(Asn1Encodable value)
- {
- return Canonicalize(ValueToString(value));
- }
- private static Asn1Object DecodeObject(string oValue)
- {
- try
- {
- return Asn1Object.FromByteArray(Hex.DecodeStrict(oValue, 1, oValue.Length - 1));
- }
- catch (IOException e)
- {
- throw new InvalidOperationException("unknown encoding in name: " + e);
- }
- }
- public static string StripInternalSpaces(string str)
- {
- if (str.IndexOf(" ") < 0)
- return str;
- StringBuilder res = new StringBuilder();
- char c1 = str[0];
- res.Append(c1);
- for (int k = 1; k < str.Length; k++)
- {
- char c2 = str[k];
- if (!(' ' == c1 && ' ' == c2))
- {
- res.Append(c2);
- c1 = c2;
- }
- }
- return res.ToString();
- }
- public static bool RdnAreEqual(Rdn rdn1, Rdn rdn2)
- {
- if (rdn1.Count != rdn2.Count)
- return false;
- AttributeTypeAndValue[] atvs1 = rdn1.GetTypesAndValues();
- AttributeTypeAndValue[] atvs2 = rdn2.GetTypesAndValues();
- if (atvs1.Length != atvs2.Length)
- return false;
- for (int i = 0; i != atvs1.Length; i++)
- {
- if (!AtvAreEqual(atvs1[i], atvs2[i]))
- return false;
- }
- return true;
- }
- private static bool AtvAreEqual(AttributeTypeAndValue atv1, AttributeTypeAndValue atv2)
- {
- if (atv1 == atv2)
- return true;
- if (null == atv1 || null == atv2)
- return false;
- DerObjectIdentifier o1 = atv1.Type;
- DerObjectIdentifier o2 = atv2.Type;
- if (!o1.Equals(o2))
- return false;
- string v1 = CanonicalString(atv1.Value);
- string v2 = CanonicalString(atv2.Value);
- if (!v1.Equals(v2))
- return false;
- return true;
- }
- }
- }
- #pragma warning restore
- #endif
|