rsa_utils.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package jwt
  2. import (
  3. "crypto/rsa"
  4. "crypto/x509"
  5. "encoding/pem"
  6. "errors"
  7. )
  8. var (
  9. ErrKeyMustBePEMEncoded = errors.New("Invalid Key: Key must be PEM encoded PKCS1 or PKCS8 private key")
  10. ErrNotRSAPrivateKey = errors.New("Key is not a valid RSA private key")
  11. ErrNotRSAPublicKey = errors.New("Key is not a valid RSA public key")
  12. )
  13. // Parse PEM encoded PKCS1 or PKCS8 private key
  14. func ParseRSAPrivateKeyFromPEM(key []byte) (*rsa.PrivateKey, error) {
  15. var err error
  16. // Parse PEM block
  17. var block *pem.Block
  18. if block, _ = pem.Decode(key); block == nil {
  19. return nil, ErrKeyMustBePEMEncoded
  20. }
  21. var parsedKey interface{}
  22. if parsedKey, err = x509.ParsePKCS1PrivateKey(block.Bytes); err != nil {
  23. if parsedKey, err = x509.ParsePKCS8PrivateKey(block.Bytes); err != nil {
  24. return nil, err
  25. }
  26. }
  27. var pkey *rsa.PrivateKey
  28. var ok bool
  29. if pkey, ok = parsedKey.(*rsa.PrivateKey); !ok {
  30. return nil, ErrNotRSAPrivateKey
  31. }
  32. return pkey, nil
  33. }
  34. // Parse PEM encoded PKCS1 or PKCS8 private key protected with password
  35. func ParseRSAPrivateKeyFromPEMWithPassword(key []byte, password string) (*rsa.PrivateKey, error) {
  36. var err error
  37. // Parse PEM block
  38. var block *pem.Block
  39. if block, _ = pem.Decode(key); block == nil {
  40. return nil, ErrKeyMustBePEMEncoded
  41. }
  42. var parsedKey interface{}
  43. var blockDecrypted []byte
  44. if blockDecrypted, err = x509.DecryptPEMBlock(block, []byte(password)); err != nil {
  45. return nil, err
  46. }
  47. if parsedKey, err = x509.ParsePKCS1PrivateKey(blockDecrypted); err != nil {
  48. if parsedKey, err = x509.ParsePKCS8PrivateKey(blockDecrypted); err != nil {
  49. return nil, err
  50. }
  51. }
  52. var pkey *rsa.PrivateKey
  53. var ok bool
  54. if pkey, ok = parsedKey.(*rsa.PrivateKey); !ok {
  55. return nil, ErrNotRSAPrivateKey
  56. }
  57. return pkey, nil
  58. }
  59. // Parse PEM encoded PKCS1 or PKCS8 public key
  60. func ParseRSAPublicKeyFromPEM(key []byte) (*rsa.PublicKey, error) {
  61. var err error
  62. // Parse PEM block
  63. var block *pem.Block
  64. if block, _ = pem.Decode(key); block == nil {
  65. return nil, ErrKeyMustBePEMEncoded
  66. }
  67. // Parse the key
  68. var parsedKey interface{}
  69. if parsedKey, err = x509.ParsePKIXPublicKey(block.Bytes); err != nil {
  70. if cert, err := x509.ParseCertificate(block.Bytes); err == nil {
  71. parsedKey = cert.PublicKey
  72. } else {
  73. return nil, err
  74. }
  75. }
  76. var pkey *rsa.PublicKey
  77. var ok bool
  78. if pkey, ok = parsedKey.(*rsa.PublicKey); !ok {
  79. return nil, ErrNotRSAPublicKey
  80. }
  81. return pkey, nil
  82. }