KeyUsage.cs 2.2 KB

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