utils.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package acme
  2. import (
  3. "crypto/x509"
  4. "encoding/pem"
  5. "fmt"
  6. "io/ioutil"
  7. "time"
  8. )
  9. // Get the issuer name from pem file
  10. func ExtractIssuerNameFromPEM(pemFilePath string) (string, error) {
  11. // Read the PEM file
  12. pemData, err := ioutil.ReadFile(pemFilePath)
  13. if err != nil {
  14. return "", err
  15. }
  16. // Parse the PEM block
  17. block, _ := pem.Decode(pemData)
  18. if block == nil || block.Type != "CERTIFICATE" {
  19. return "", fmt.Errorf("failed to decode PEM block containing certificate")
  20. }
  21. // Parse the certificate
  22. cert, err := x509.ParseCertificate(block.Bytes)
  23. if err != nil {
  24. return "", fmt.Errorf("failed to parse certificate: %v", err)
  25. }
  26. // Extract the issuer name
  27. issuer := cert.Issuer.Organization[0]
  28. return issuer, nil
  29. }
  30. // Check if a cert is expired
  31. func CertIsExpired(certBtyes []byte) bool {
  32. block, _ := pem.Decode(certBtyes)
  33. if block != nil {
  34. cert, err := x509.ParseCertificate(block.Bytes)
  35. if err == nil {
  36. elapsed := time.Since(cert.NotAfter)
  37. if elapsed > 0 {
  38. // if it is expired then add it in
  39. // make sure it's uniqueless
  40. return true
  41. }
  42. }
  43. }
  44. return false
  45. }