-
Notifications
You must be signed in to change notification settings - Fork 0
/
cert.go
40 lines (36 loc) · 1004 Bytes
/
cert.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package main
import (
"crypto/x509"
"encoding/pem"
"fmt"
"time"
)
// Cert captures a subset of ready-to-render fields from x509.Certificate
type Cert struct {
Version string
SerialNumber string
Subject string
Issuer string
NotBefore string
NotAfter string
PublicKeyAlgorithm string
PublicKeyPem string
}
func FromX509Certificate(c *x509.Certificate) *Cert {
pubkeyBytes, _ := x509.MarshalPKIXPublicKey(c.PublicKey)
p := &pem.Block{
Type: "PUBLIC KEY",
Bytes: pubkeyBytes,
}
res := &Cert{
Version: fmt.Sprintf("%v", c.Version),
SerialNumber: fmt.Sprintf("%v", c.SerialNumber),
Subject: c.Subject.String(),
Issuer: c.Issuer.String(),
NotBefore: c.NotBefore.Format(time.RFC1123),
NotAfter: c.NotAfter.Format(time.RFC1123),
PublicKeyAlgorithm: c.PublicKeyAlgorithm.String(),
PublicKeyPem: string(pem.EncodeToMemory(p)),
}
return res
}