X509NameEntryConverter.cs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. using System;
  3. using System.Globalization;
  4. using System.IO;
  5. using System.Text;
  6. using Org.BouncyCastle.Utilities.Encoders;
  7. namespace Org.BouncyCastle.Asn1.X509
  8. {
  9. /**
  10. * It turns out that the number of standard ways the fields in a DN should be
  11. * encoded into their ASN.1 counterparts is rapidly approaching the
  12. * number of machines on the internet. By default the X509Name class
  13. * will produce UTF8Strings in line with the current recommendations (RFC 3280).
  14. * <p>
  15. * An example of an encoder look like below:
  16. * <pre>
  17. * public class X509DirEntryConverter
  18. * : X509NameEntryConverter
  19. * {
  20. * public Asn1Object GetConvertedValue(
  21. * DerObjectIdentifier oid,
  22. * string value)
  23. * {
  24. * if (str.Length() != 0 &amp;&amp; str.charAt(0) == '#')
  25. * {
  26. * return ConvertHexEncoded(str, 1);
  27. * }
  28. * if (oid.Equals(EmailAddress))
  29. * {
  30. * return new DerIA5String(str);
  31. * }
  32. * else if (CanBePrintable(str))
  33. * {
  34. * return new DerPrintableString(str);
  35. * }
  36. * else if (CanBeUTF8(str))
  37. * {
  38. * return new DerUtf8String(str);
  39. * }
  40. * else
  41. * {
  42. * return new DerBmpString(str);
  43. * }
  44. * }
  45. * }
  46. * </pre>
  47. * </p>
  48. */
  49. public abstract class X509NameEntryConverter
  50. {
  51. /**
  52. * Convert an inline encoded hex string rendition of an ASN.1
  53. * object back into its corresponding ASN.1 object.
  54. *
  55. * @param str the hex encoded object
  56. * @param off the index at which the encoding starts
  57. * @return the decoded object
  58. */
  59. protected Asn1Object ConvertHexEncoded(
  60. string hexString,
  61. int offset)
  62. {
  63. string str = hexString.Substring(offset);
  64. return Asn1Object.FromByteArray(Hex.Decode(str));
  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. #endif