X509NameEntryConverter.cs 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. return Asn1Object.FromByteArray(Hex.DecodeStrict(hexString, offset, hexString.Length - offset));
  65. }
  66. /**
  67. * return true if the passed in string can be represented without
  68. * loss as a PrintableString, false otherwise.
  69. */
  70. protected bool CanBePrintable(
  71. string str)
  72. {
  73. return DerPrintableString.IsPrintableString(str);
  74. }
  75. /**
  76. * Convert the passed in string value into the appropriate ASN.1
  77. * encoded object.
  78. *
  79. * @param oid the oid associated with the value in the DN.
  80. * @param value the value of the particular DN component.
  81. * @return the ASN.1 equivalent for the value.
  82. */
  83. public abstract Asn1Object GetConvertedValue(DerObjectIdentifier oid, string value);
  84. }
  85. }
  86. #pragma warning restore
  87. #endif