X509ExtensionUtil.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. using System;
  3. using System.Collections;
  4. using System.IO;
  5. using Org.BouncyCastle.Asn1;
  6. using Org.BouncyCastle.Asn1.X509;
  7. using Org.BouncyCastle.Security.Certificates;
  8. using Org.BouncyCastle.Utilities;
  9. namespace Org.BouncyCastle.X509.Extension
  10. {
  11. public class X509ExtensionUtilities
  12. {
  13. public static Asn1Object FromExtensionValue(
  14. Asn1OctetString extensionValue)
  15. {
  16. return Asn1Object.FromByteArray(extensionValue.GetOctets());
  17. }
  18. public static ICollection GetIssuerAlternativeNames(
  19. X509Certificate cert)
  20. {
  21. Asn1OctetString extVal = cert.GetExtensionValue(X509Extensions.IssuerAlternativeName);
  22. return GetAlternativeName(extVal);
  23. }
  24. public static ICollection GetSubjectAlternativeNames(
  25. X509Certificate cert)
  26. {
  27. Asn1OctetString extVal = cert.GetExtensionValue(X509Extensions.SubjectAlternativeName);
  28. return GetAlternativeName(extVal);
  29. }
  30. private static ICollection GetAlternativeName(
  31. Asn1OctetString extVal)
  32. {
  33. IList temp = Org.BouncyCastle.Utilities.Platform.CreateArrayList();
  34. if (extVal != null)
  35. {
  36. try
  37. {
  38. Asn1Sequence seq = DerSequence.GetInstance(FromExtensionValue(extVal));
  39. foreach (GeneralName genName in seq)
  40. {
  41. IList list = Org.BouncyCastle.Utilities.Platform.CreateArrayList();
  42. list.Add(genName.TagNo);
  43. switch (genName.TagNo)
  44. {
  45. case GeneralName.EdiPartyName:
  46. case GeneralName.X400Address:
  47. case GeneralName.OtherName:
  48. list.Add(genName.Name.ToAsn1Object());
  49. break;
  50. case GeneralName.DirectoryName:
  51. list.Add(X509Name.GetInstance(genName.Name).ToString());
  52. break;
  53. case GeneralName.DnsName:
  54. case GeneralName.Rfc822Name:
  55. case GeneralName.UniformResourceIdentifier:
  56. list.Add(((IAsn1String)genName.Name).GetString());
  57. break;
  58. case GeneralName.RegisteredID:
  59. list.Add(DerObjectIdentifier.GetInstance(genName.Name).Id);
  60. break;
  61. case GeneralName.IPAddress:
  62. list.Add(DerOctetString.GetInstance(genName.Name).GetOctets());
  63. break;
  64. default:
  65. throw new IOException("Bad tag number: " + genName.TagNo);
  66. }
  67. temp.Add(list);
  68. }
  69. }
  70. catch (Exception e)
  71. {
  72. throw new CertificateParsingException(e.Message);
  73. }
  74. }
  75. return temp;
  76. }
  77. }
  78. }
  79. #endif