interface.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package jwk
  2. import (
  3. "crypto"
  4. "crypto/ecdsa"
  5. "crypto/rsa"
  6. "crypto/x509"
  7. "errors"
  8. )
  9. // KeyUsageType is used to denote what this key should be used for
  10. type KeyUsageType string
  11. const (
  12. // ForSignature is the value used in the headers to indicate that
  13. // this key should be used for signatures
  14. ForSignature KeyUsageType = "sig"
  15. // ForEncryption is the value used in the headers to indicate that
  16. // this key should be used for encryptiong
  17. ForEncryption KeyUsageType = "enc"
  18. )
  19. type CertificateChain struct {
  20. certs []*x509.Certificate
  21. }
  22. // Errors related to JWK
  23. var (
  24. ErrInvalidHeaderName = errors.New("invalid header name")
  25. ErrInvalidHeaderValue = errors.New("invalid value for header key")
  26. ErrUnsupportedKty = errors.New("unsupported kty")
  27. ErrUnsupportedCurve = errors.New("unsupported curve")
  28. )
  29. type KeyOperation string
  30. const (
  31. KeyOpSign KeyOperation = "sign" // (compute digital signature or MAC)
  32. KeyOpVerify = "verify" // (verify digital signature or MAC)
  33. KeyOpEncrypt = "encrypt" // (encrypt content)
  34. KeyOpDecrypt = "decrypt" // (decrypt content and validate decryption, if applicable)
  35. KeyOpWrapKey = "wrapKey" // (encrypt key)
  36. KeyOpUnwrapKey = "unwrapKey" // (decrypt key and validate decryption, if applicable)
  37. KeyOpDeriveKey = "deriveKey" // (derive key)
  38. KeyOpDeriveBits = "deriveBits" // (derive bits not to be used as a key)
  39. )
  40. // Set is a convenience struct to allow generating and parsing
  41. // JWK sets as opposed to single JWKs
  42. type Set struct {
  43. Keys []Key `json:"keys"`
  44. }
  45. // Key defines the minimal interface for each of the
  46. // key types. Their use and implementation differ significantly
  47. // between each key types, so you should use type assertions
  48. // to perform more specific tasks with each key
  49. type Key interface {
  50. Headers
  51. // Materialize creates the corresponding key. For example,
  52. // RSA types would create *rsa.PublicKey or *rsa.PrivateKey,
  53. // EC types would create *ecdsa.PublicKey or *ecdsa.PrivateKey,
  54. // and OctetSeq types create a []byte key.
  55. Materialize() (interface{}, error)
  56. // Thumbprint returns the JWK thumbprint using the indicated
  57. // hashing algorithm, according to RFC 7638
  58. Thumbprint(crypto.Hash) ([]byte, error)
  59. }
  60. type headers interface {
  61. Headers
  62. }
  63. // RSAPublicKey is a type of JWK generated from RSA public keys
  64. type RSAPublicKey struct {
  65. headers
  66. key *rsa.PublicKey
  67. }
  68. // RSAPrivateKey is a type of JWK generated from RSA private keys
  69. type RSAPrivateKey struct {
  70. headers
  71. key *rsa.PrivateKey
  72. }
  73. // SymmetricKey is a type of JWK generated from symmetric keys
  74. type SymmetricKey struct {
  75. headers
  76. key []byte
  77. }
  78. // ECDSAPublicKey is a type of JWK generated from ECDSA public keys
  79. type ECDSAPublicKey struct {
  80. headers
  81. key *ecdsa.PublicKey
  82. }
  83. // ECDSAPrivateKey is a type of JWK generated from ECDH-ES private keys
  84. type ECDSAPrivateKey struct {
  85. headers
  86. key *ecdsa.PrivateKey
  87. }