123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
- using System;
- using System.Text;
- using Org.BouncyCastle.Utilities;
- namespace Org.BouncyCastle.Asn1
- {
-
- public class DerPrintableString
- : DerStringBase
- {
- private readonly string str;
-
- public static DerPrintableString GetInstance(
- object obj)
- {
- if (obj == null || obj is DerPrintableString)
- {
- return (DerPrintableString)obj;
- }
- throw new ArgumentException("illegal object in GetInstance: " + Org.BouncyCastle.Utilities.Platform.GetTypeName(obj));
- }
-
- public static DerPrintableString GetInstance(
- Asn1TaggedObject obj,
- bool isExplicit)
- {
- Asn1Object o = obj.GetObject();
- if (isExplicit || o is DerPrintableString)
- {
- return GetInstance(o);
- }
- return new DerPrintableString(Asn1OctetString.GetInstance(o).GetOctets());
- }
-
- public DerPrintableString(
- byte[] str)
- : this(Strings.FromAsciiByteArray(str), false)
- {
- }
-
- public DerPrintableString(
- string str)
- : this(str, false)
- {
- }
-
- public DerPrintableString(
- string str,
- bool validate)
- {
- if (str == null)
- throw new ArgumentNullException("str");
- if (validate && !IsPrintableString(str))
- throw new ArgumentException("string contains illegal characters", "str");
- this.str = str;
- }
- public override string GetString()
- {
- return str;
- }
- public byte[] GetOctets()
- {
- return Strings.ToAsciiByteArray(str);
- }
- internal override void Encode(
- DerOutputStream derOut)
- {
- derOut.WriteEncoded(Asn1Tags.PrintableString, GetOctets());
- }
- protected override bool Asn1Equals(
- Asn1Object asn1Object)
- {
- DerPrintableString other = asn1Object as DerPrintableString;
- if (other == null)
- return false;
- return this.str.Equals(other.str);
- }
-
- public static bool IsPrintableString(
- string str)
- {
- foreach (char ch in str)
- {
- if (ch > 0x007f)
- return false;
- if (char.IsLetterOrDigit(ch))
- continue;
- switch (ch)
- {
- case ' ':
- case '\'':
- case '(':
- case ')':
- case '+':
- case '-':
- case '.':
- case ':':
- case '=':
- case '?':
- case '/':
- case ',':
- continue;
- }
- return false;
- }
- return true;
- }
- }
- }
- #endif
|