12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- package acme
- import (
- "crypto/x509"
- "encoding/pem"
- "fmt"
- "io/ioutil"
- "time"
- )
- // Get the issuer name from pem file
- func ExtractIssuerNameFromPEM(pemFilePath string) (string, error) {
- // Read the PEM file
- pemData, err := ioutil.ReadFile(pemFilePath)
- if err != nil {
- return "", err
- }
- // Parse the PEM block
- block, _ := pem.Decode(pemData)
- if block == nil || block.Type != "CERTIFICATE" {
- return "", fmt.Errorf("failed to decode PEM block containing certificate")
- }
- // Parse the certificate
- cert, err := x509.ParseCertificate(block.Bytes)
- if err != nil {
- return "", fmt.Errorf("failed to parse certificate: %v", err)
- }
- // Extract the issuer name
- issuer := cert.Issuer.Organization[0]
- return issuer, nil
- }
- // Check if a cert is expired
- func CertIsExpired(certBtyes []byte) bool {
- block, _ := pem.Decode(certBtyes)
- if block != nil {
- cert, err := x509.ParseCertificate(block.Bytes)
- if err == nil {
- elapsed := time.Since(cert.NotAfter)
- if elapsed > 0 {
- // if it is expired then add it in
- // make sure it's uniqueless
- return true
- }
- }
- }
- return false
- }
|