PrincipalUtil.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.IO;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509;
  7. using BestHTTP.SecureProtocol.Org.BouncyCastle.Security.Certificates;
  8. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.X509
  9. {
  10. /// <remarks>
  11. /// A utility class that will extract X509Principal objects from X.509 certificates.
  12. /// <p>
  13. /// Use this in preference to trying to recreate a principal from a string, not all
  14. /// DNs are what they should be, so it's best to leave them encoded where they
  15. /// can be.</p>
  16. /// </remarks>
  17. public class PrincipalUtilities
  18. {
  19. /// <summary>Return the issuer of the given cert as an X509Principal.</summary>
  20. public static X509Name GetIssuerX509Principal(
  21. X509Certificate cert)
  22. {
  23. try
  24. {
  25. TbsCertificateStructure tbsCert = TbsCertificateStructure.GetInstance(
  26. Asn1Object.FromByteArray(cert.GetTbsCertificate()));
  27. return tbsCert.Issuer;
  28. }
  29. catch (Exception e)
  30. {
  31. throw new CertificateEncodingException("Could not extract issuer", e);
  32. }
  33. }
  34. /// <summary>Return the subject of the given cert as an X509Principal.</summary>
  35. public static X509Name GetSubjectX509Principal(
  36. X509Certificate cert)
  37. {
  38. try
  39. {
  40. TbsCertificateStructure tbsCert = TbsCertificateStructure.GetInstance(
  41. Asn1Object.FromByteArray(cert.GetTbsCertificate()));
  42. return tbsCert.Subject;
  43. }
  44. catch (Exception e)
  45. {
  46. throw new CertificateEncodingException("Could not extract subject", e);
  47. }
  48. }
  49. /// <summary>Return the issuer of the given CRL as an X509Principal.</summary>
  50. public static X509Name GetIssuerX509Principal(
  51. X509Crl crl)
  52. {
  53. try
  54. {
  55. TbsCertificateList tbsCertList = TbsCertificateList.GetInstance(
  56. Asn1Object.FromByteArray(crl.GetTbsCertList()));
  57. return tbsCertList.Issuer;
  58. }
  59. catch (Exception e)
  60. {
  61. throw new CrlException("Could not extract issuer", e);
  62. }
  63. }
  64. }
  65. }
  66. #pragma warning restore
  67. #endif