KeyUsage.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509
  4. {
  5. /**
  6. * The KeyUsage object.
  7. * <pre>
  8. * id-ce-keyUsage OBJECT IDENTIFIER ::= { id-ce 15 }
  9. *
  10. * KeyUsage ::= BIT STRING {
  11. * digitalSignature (0),
  12. * nonRepudiation (1),
  13. * keyEncipherment (2),
  14. * dataEncipherment (3),
  15. * keyAgreement (4),
  16. * keyCertSign (5),
  17. * cRLSign (6),
  18. * encipherOnly (7),
  19. * decipherOnly (8) }
  20. * </pre>
  21. */
  22. public class KeyUsage
  23. : DerBitString
  24. {
  25. public const int DigitalSignature = (1 << 7);
  26. public const int NonRepudiation = (1 << 6);
  27. public const int KeyEncipherment = (1 << 5);
  28. public const int DataEncipherment = (1 << 4);
  29. public const int KeyAgreement = (1 << 3);
  30. public const int KeyCertSign = (1 << 2);
  31. public const int CrlSign = (1 << 1);
  32. public const int EncipherOnly = (1 << 0);
  33. public const int DecipherOnly = (1 << 15);
  34. public static new KeyUsage GetInstance(object obj)
  35. {
  36. if (obj is KeyUsage)
  37. return (KeyUsage)obj;
  38. if (obj is X509Extension)
  39. return GetInstance(X509Extension.ConvertValueToObject((X509Extension)obj));
  40. if (obj == null)
  41. return null;
  42. return new KeyUsage(DerBitString.GetInstance(obj));
  43. }
  44. public static KeyUsage FromExtensions(X509Extensions extensions)
  45. {
  46. return GetInstance(X509Extensions.GetExtensionParsedValue(extensions, X509Extensions.KeyUsage));
  47. }
  48. /**
  49. * Basic constructor.
  50. *
  51. * @param usage - the bitwise OR of the Key Usage flags giving the
  52. * allowed uses for the key.
  53. * e.g. (KeyUsage.keyEncipherment | KeyUsage.dataEncipherment)
  54. */
  55. public KeyUsage(int usage)
  56. : base(usage)
  57. {
  58. }
  59. private KeyUsage(
  60. DerBitString usage)
  61. : base(usage.GetBytes(), usage.PadBits)
  62. {
  63. }
  64. public override string ToString()
  65. {
  66. byte[] data = GetBytes();
  67. if (data.Length == 1)
  68. {
  69. return "KeyUsage: 0x" + (data[0] & 0xff).ToString("X");
  70. }
  71. return "KeyUsage: 0x" + ((data[1] & 0xff) << 8 | (data[0] & 0xff)).ToString("X");
  72. }
  73. }
  74. }
  75. #pragma warning restore
  76. #endif