本站源代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

127 lines
2.7KB

  1. // +build go1.4
  2. package jwt
  3. import (
  4. "crypto"
  5. "crypto/rand"
  6. "crypto/rsa"
  7. )
  8. // Implements the RSAPSS family of signing methods signing methods
  9. type SigningMethodRSAPSS struct {
  10. *SigningMethodRSA
  11. Options *rsa.PSSOptions
  12. }
  13. // Specific instances for RS/PS and company
  14. var (
  15. SigningMethodPS256 *SigningMethodRSAPSS
  16. SigningMethodPS384 *SigningMethodRSAPSS
  17. SigningMethodPS512 *SigningMethodRSAPSS
  18. )
  19. func init() {
  20. // PS256
  21. SigningMethodPS256 = &SigningMethodRSAPSS{
  22. &SigningMethodRSA{
  23. Name: "PS256",
  24. Hash: crypto.SHA256,
  25. },
  26. &rsa.PSSOptions{
  27. SaltLength: rsa.PSSSaltLengthAuto,
  28. Hash: crypto.SHA256,
  29. },
  30. }
  31. RegisterSigningMethod(SigningMethodPS256.Alg(), func() SigningMethod {
  32. return SigningMethodPS256
  33. })
  34. // PS384
  35. SigningMethodPS384 = &SigningMethodRSAPSS{
  36. &SigningMethodRSA{
  37. Name: "PS384",
  38. Hash: crypto.SHA384,
  39. },
  40. &rsa.PSSOptions{
  41. SaltLength: rsa.PSSSaltLengthAuto,
  42. Hash: crypto.SHA384,
  43. },
  44. }
  45. RegisterSigningMethod(SigningMethodPS384.Alg(), func() SigningMethod {
  46. return SigningMethodPS384
  47. })
  48. // PS512
  49. SigningMethodPS512 = &SigningMethodRSAPSS{
  50. &SigningMethodRSA{
  51. Name: "PS512",
  52. Hash: crypto.SHA512,
  53. },
  54. &rsa.PSSOptions{
  55. SaltLength: rsa.PSSSaltLengthAuto,
  56. Hash: crypto.SHA512,
  57. },
  58. }
  59. RegisterSigningMethod(SigningMethodPS512.Alg(), func() SigningMethod {
  60. return SigningMethodPS512
  61. })
  62. }
  63. // Implements the Verify method from SigningMethod
  64. // For this verify method, key must be an rsa.PublicKey struct
  65. func (m *SigningMethodRSAPSS) Verify(signingString, signature string, key interface{}) error {
  66. var err error
  67. // Decode the signature
  68. var sig []byte
  69. if sig, err = DecodeSegment(signature); err != nil {
  70. return err
  71. }
  72. var rsaKey *rsa.PublicKey
  73. switch k := key.(type) {
  74. case *rsa.PublicKey:
  75. rsaKey = k
  76. default:
  77. return ErrInvalidKey
  78. }
  79. // Create hasher
  80. if !m.Hash.Available() {
  81. return ErrHashUnavailable
  82. }
  83. hasher := m.Hash.New()
  84. hasher.Write([]byte(signingString))
  85. return rsa.VerifyPSS(rsaKey, m.Hash, hasher.Sum(nil), sig, m.Options)
  86. }
  87. // Implements the Sign method from SigningMethod
  88. // For this signing method, key must be an rsa.PrivateKey struct
  89. func (m *SigningMethodRSAPSS) Sign(signingString string, key interface{}) (string, error) {
  90. var rsaKey *rsa.PrivateKey
  91. switch k := key.(type) {
  92. case *rsa.PrivateKey:
  93. rsaKey = k
  94. default:
  95. return "", ErrInvalidKeyType
  96. }
  97. // Create the hasher
  98. if !m.Hash.Available() {
  99. return "", ErrHashUnavailable
  100. }
  101. hasher := m.Hash.New()
  102. hasher.Write([]byte(signingString))
  103. // Sign the string and return the encoded bytes
  104. if sigBytes, err := rsa.SignPSS(rand.Reader, rsaKey, m.Hash, hasher.Sum(nil), m.Options); err == nil {
  105. return EncodeSegment(sigBytes), nil
  106. } else {
  107. return "", err
  108. }
  109. }
上海开阖软件有限公司 沪ICP备12045867号-1