Skip to content

Commit 37cd1e7

Browse files
committed
Factored function to encode certs as PEM
1 parent dc68681 commit 37cd1e7

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

certificates/certutils.go

+9
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,12 @@ func LoadCertificatesFromFile(certificateFile *paths.Path) ([]*x509.Certificate,
9696
return nil, fmt.Errorf("cert format %s not supported, please use .pem or .cer", certificateFile.Ext())
9797
}
9898
}
99+
100+
// EncodeCertificateAsPEM returns the PEM encoding of the given certificate
101+
func EncodeCertificateAsPEM(cert *x509.Certificate) []byte {
102+
pemBlock := &pem.Block{
103+
Type: "CERTIFICATE",
104+
Bytes: cert.Raw,
105+
}
106+
return pem.EncodeToMemory(pemBlock)
107+
}

flasher/nina.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
"bytes"
2323
"crypto/md5"
2424
"encoding/binary"
25-
"encoding/pem"
2625
"fmt"
2726
"io"
2827
"time"
@@ -106,7 +105,7 @@ func (f *NinaFlasher) FlashCertificates(certificatePaths *paths.PathList, URLs [
106105
return err
107106
}
108107
for _, cert := range certs {
109-
data := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: cert.Raw})
108+
data := certificates.EncodeCertificateAsPEM(cert)
110109
certificatesData = append(certificatesData, data...)
111110
}
112111
}
@@ -118,7 +117,7 @@ func (f *NinaFlasher) FlashCertificates(certificatePaths *paths.PathList, URLs [
118117
if err != nil {
119118
return err
120119
}
121-
data := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: rootCertificate.Raw})
120+
data := certificates.EncodeCertificateAsPEM(rootCertificate)
122121
certificatesData = append(certificatesData, data...)
123122
}
124123

0 commit comments

Comments
 (0)