X509NameEntryConverter.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Globalization;
  5. using System.IO;
  6. using System.Text;
  7. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Encoders;
  8. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509
  9. {
  10. /**
  11. * It turns out that the number of standard ways the fields in a DN should be
  12. * encoded into their ASN.1 counterparts is rapidly approaching the
  13. * number of machines on the internet. By default the X509Name class
  14. * will produce UTF8Strings in line with the current recommendations (RFC 3280).
  15. * <p>
  16. * An example of an encoder look like below:
  17. * <pre>
  18. * public class X509DirEntryConverter
  19. * : X509NameEntryConverter
  20. * {
  21. * public Asn1Object GetConvertedValue(
  22. * DerObjectIdentifier oid,
  23. * string value)
  24. * {
  25. * if (str.Length() != 0 &amp;&amp; str.charAt(0) == '#')
  26. * {
  27. * return ConvertHexEncoded(str, 1);
  28. * }
  29. * if (oid.Equals(EmailAddress))
  30. * {
  31. * return new DerIA5String(str);
  32. * }
  33. * else if (CanBePrintable(str))
  34. * {
  35. * return new DerPrintableString(str);
  36. * }
  37. * else if (CanBeUTF8(str))
  38. * {
  39. * return new DerUtf8String(str);
  40. * }
  41. * else
  42. * {
  43. * return new DerBmpString(str);
  44. * }
  45. * }
  46. * }
  47. * </pre>
  48. * </p>
  49. */
  50. public abstract class X509NameEntryConverter
  51. {
  52. /**
  53. * Convert an inline encoded hex string rendition of an ASN.1
  54. * object back into its corresponding ASN.1 object.
  55. *
  56. * @param str the hex encoded object
  57. * @param off the index at which the encoding starts
  58. * @return the decoded object
  59. */
  60. protected Asn1Object ConvertHexEncoded(
  61. string hexString,
  62. int offset)
  63. {
  64. string str = hexString.Substring(offset);
  65. return Asn1Object.FromByteArray(Hex.Decode(str));
  66. }
  67. /**
  68. * return true if the passed in string can be represented without
  69. * loss as a PrintableString, false otherwise.
  70. */
  71. protected bool CanBePrintable(
  72. string str)
  73. {
  74. return DerPrintableString.IsPrintableString(str);
  75. }
  76. /**
  77. * Convert the passed in string value into the appropriate ASN.1
  78. * encoded object.
  79. *
  80. * @param oid the oid associated with the value in the DN.
  81. * @param value the value of the particular DN component.
  82. * @return the ASN.1 equivalent for the value.
  83. */
  84. public abstract Asn1Object GetConvertedValue(DerObjectIdentifier oid, string value);
  85. }
  86. }
  87. #pragma warning restore
  88. #endif