pem.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // Copyright 2021 Tencent Inc. All rights reserved.
  2. package utils
  3. import (
  4. "crypto/rsa"
  5. "crypto/x509"
  6. "encoding/pem"
  7. "errors"
  8. "fmt"
  9. "io/ioutil"
  10. "time"
  11. )
  12. // LoadCertificate 通过证书的文本内容加载证书
  13. func LoadCertificate(certificateStr string) (certificate *x509.Certificate, err error) {
  14. block, _ := pem.Decode([]byte(certificateStr))
  15. if block == nil {
  16. return nil, fmt.Errorf("decode certificate err")
  17. }
  18. if block.Type != "CERTIFICATE" {
  19. return nil, fmt.Errorf("the kind of PEM should be CERTIFICATE")
  20. }
  21. certificate, err = x509.ParseCertificate(block.Bytes)
  22. if err != nil {
  23. return nil, fmt.Errorf("parse certificate err:%s", err.Error())
  24. }
  25. return certificate, nil
  26. }
  27. // LoadPrivateKey 通过私钥的文本内容加载私钥
  28. func LoadPrivateKey(privateKeyStr string) (privateKey *rsa.PrivateKey, err error) {
  29. block, _ := pem.Decode([]byte(privateKeyStr))
  30. if block == nil {
  31. return nil, fmt.Errorf("decode private key err")
  32. }
  33. if block.Type != "PRIVATE KEY" {
  34. return nil, fmt.Errorf("the kind of PEM should be PRVATE KEY")
  35. }
  36. key, err := x509.ParsePKCS8PrivateKey(block.Bytes)
  37. if err != nil {
  38. return nil, fmt.Errorf("parse private key err:%s", err.Error())
  39. }
  40. privateKey, ok := key.(*rsa.PrivateKey)
  41. if !ok {
  42. return nil, fmt.Errorf("not a RSA private key")
  43. }
  44. return privateKey, nil
  45. }
  46. // LoadPublicKey 通过公钥的文本内容加载公钥
  47. func LoadPublicKey(publicKeyStr string) (publicKey *rsa.PublicKey, err error) {
  48. block, _ := pem.Decode([]byte(publicKeyStr))
  49. if block == nil {
  50. return nil, errors.New("decode public key error")
  51. }
  52. if block.Type != "PUBLIC KEY" {
  53. return nil, fmt.Errorf("the kind of PEM should be PUBLIC KEY")
  54. }
  55. key, err := x509.ParsePKIXPublicKey(block.Bytes)
  56. if err != nil {
  57. return nil, fmt.Errorf("parse public key err:%s", err.Error())
  58. }
  59. publicKey, ok := key.(*rsa.PublicKey)
  60. if !ok {
  61. return nil, fmt.Errorf("%s is not rsa public key", publicKeyStr)
  62. }
  63. return publicKey, nil
  64. }
  65. // LoadCertificateWithPath 通过证书的文件路径加载证书
  66. func LoadCertificateWithPath(path string) (certificate *x509.Certificate, err error) {
  67. certificateBytes, err := ioutil.ReadFile(path)
  68. if err != nil {
  69. return nil, fmt.Errorf("read certificate pem file err:%s", err.Error())
  70. }
  71. return LoadCertificate(string(certificateBytes))
  72. }
  73. // LoadPrivateKeyWithPath 通过私钥的文件路径内容加载私钥
  74. func LoadPrivateKeyWithPath(path string) (privateKey *rsa.PrivateKey, err error) {
  75. privateKeyBytes, err := ioutil.ReadFile(path)
  76. if err != nil {
  77. return nil, fmt.Errorf("read private pem file err:%s", err.Error())
  78. }
  79. return LoadPrivateKey(string(privateKeyBytes))
  80. }
  81. // LoadPublicKeyWithPath 通过公钥的文件路径加载公钥
  82. func LoadPublicKeyWithPath(path string) (publicKey *rsa.PublicKey, err error) {
  83. publicKeyBytes, err := ioutil.ReadFile(path)
  84. if err != nil {
  85. return nil, fmt.Errorf("read certificate pem file err:%s", err.Error())
  86. }
  87. return LoadPublicKey(string(publicKeyBytes))
  88. }
  89. // GetCertificateSerialNumber 从证书中获取证书序列号
  90. func GetCertificateSerialNumber(certificate x509.Certificate) string {
  91. return fmt.Sprintf("%X", certificate.SerialNumber.Bytes())
  92. }
  93. // IsCertExpired 判定证书在特定时间是否过期
  94. //
  95. // Deprecated: 请使用 IsCertificateExpired
  96. func IsCertExpired(certificate x509.Certificate, now time.Time) bool {
  97. return now.After(certificate.NotAfter)
  98. }
  99. // IsCertificateExpired 判定证书在特定时间是否过期
  100. func IsCertificateExpired(certificate x509.Certificate, now time.Time) bool {
  101. return now.After(certificate.NotAfter)
  102. }
  103. // IsCertValid 判定证书在特定时间是否有效
  104. //
  105. // Deprecated: 请使用 IsCertificateValid
  106. func IsCertValid(certificate x509.Certificate, now time.Time) bool {
  107. return now.After(certificate.NotBefore) && now.Before(certificate.NotAfter)
  108. }
  109. // IsCertificateValid 判定证书在特定时间是否有效
  110. func IsCertificateValid(certificate x509.Certificate, now time.Time) bool {
  111. return now.After(certificate.NotBefore) && now.Before(certificate.NotAfter)
  112. }