KeyUsage.cs 2.3 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(
  35. object obj)
  36. {
  37. if (obj is KeyUsage)
  38. {
  39. return (KeyUsage)obj;
  40. }
  41. if (obj is X509Extension)
  42. {
  43. return GetInstance(X509Extension.ConvertValueToObject((X509Extension) obj));
  44. }
  45. return new KeyUsage(DerBitString.GetInstance(obj));
  46. }
  47. /**
  48. * Basic constructor.
  49. *
  50. * @param usage - the bitwise OR of the Key Usage flags giving the
  51. * allowed uses for the key.
  52. * e.g. (KeyUsage.keyEncipherment | KeyUsage.dataEncipherment)
  53. */
  54. public KeyUsage(int usage)
  55. : base(usage)
  56. {
  57. }
  58. private KeyUsage(
  59. DerBitString usage)
  60. : base(usage.GetBytes(), usage.PadBits)
  61. {
  62. }
  63. public override string ToString()
  64. {
  65. byte[] data = GetBytes();
  66. if (data.Length == 1)
  67. {
  68. return "KeyUsage: 0x" + (data[0] & 0xff).ToString("X");
  69. }
  70. return "KeyUsage: 0x" + ((data[1] & 0xff) << 8 | (data[0] & 0xff)).ToString("X");
  71. }
  72. }
  73. }
  74. #pragma warning restore
  75. #endif