I have generated self-signed certificate via next command:
/bin/bash -c 'openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -days 5 -nodes
And check the certificate, it's valid for the next 5 days.
I need to write the script which will just check the expiration date of this certificate, but unfortunately it's cannot validate it. Could you please just maybe put on correct flow?
My program:
package main
import (
"crypto/x509"
"encoding/pem"
"fmt"
)
func main() {
const certPEM = `
-----BEGIN CERTIFICATE-----
...
-----END CERTIFICATE-----`
block, _ := pem.Decode([]byte(certPEM))
if block == nil {
panic("failed to parse certificate PEM")
}
cert, err := x509.ParseCertificate(block.Bytes)
if err != nil {
panic("failed to parse certificate: " + err.Error())
}
opts := x509.VerifyOptions{
DNSName: "test.com",
}
if _, err := cert.Verify(opts); err != nil {
panic("failed to verify certificate: " + err.Error())
}
fmt.Println("correct")
}
The next error I have:
panic: failed to verify certificate: x509: certificate signed by unknown authority