Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 10aa952

Browse filesBrowse files
committed
Factored function to read certificates
1 parent 9da4115 commit 10aa952
Copy full SHA for 10aa952

File tree

Expand file treeCollapse file tree

3 files changed

+56
-50
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+56
-50
lines changed

‎certificates/certutils.go

Copy file name to clipboardExpand all lines: certificates/certutils.go
+43Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@ package certificates
2121
import (
2222
"crypto/tls"
2323
"crypto/x509"
24+
"encoding/pem"
2425
"fmt"
2526

27+
"github.com/arduino/go-paths-helper"
2628
"github.com/sirupsen/logrus"
2729
)
2830

@@ -53,3 +55,44 @@ func ScrapeRootCertificatesFromURL(URL string) (*x509.Certificate, error) {
5355
rootCertificate := peerCertificates[len(peerCertificates)-1]
5456
return rootCertificate, nil
5557
}
58+
59+
// LoadCertificatesFromFile read certificates from the given file. PEM and CER formats
60+
// are supported.
61+
func LoadCertificatesFromFile(certificateFile *paths.Path) ([]*x509.Certificate, error) {
62+
data, err := certificateFile.ReadFile()
63+
if err != nil {
64+
logrus.Error(err)
65+
return nil, err
66+
}
67+
var res []*x509.Certificate
68+
switch certificateFile.Ext() {
69+
case ".cer":
70+
cert, err := x509.ParseCertificate(data)
71+
if err != nil {
72+
logrus.Error(err)
73+
}
74+
res = append(res, cert)
75+
return res, err
76+
77+
case ".pem":
78+
for {
79+
block, rest := pem.Decode(data)
80+
if block == nil && len(rest) > 0 {
81+
return nil, fmt.Errorf("invalid .pem data")
82+
}
83+
if block == nil {
84+
return res, nil
85+
}
86+
cert, err := x509.ParseCertificate(block.Bytes)
87+
if err != nil {
88+
return nil, fmt.Errorf("failed to parse certificate: %w", err)
89+
}
90+
res = append(res, cert)
91+
if len(rest) == 0 {
92+
return res, nil
93+
}
94+
}
95+
default:
96+
return nil, fmt.Errorf("cert format %s not supported, please use .pem or .cer", certificateFile.Ext())
97+
}
98+
}

‎flasher/nina.go

Copy file name to clipboardExpand all lines: flasher/nina.go
+5-26Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ package flasher
2121
import (
2222
"bytes"
2323
"crypto/md5"
24-
"crypto/x509"
2524
"encoding/binary"
2625
"encoding/pem"
2726
"fmt"
@@ -102,11 +101,14 @@ func (f *NinaFlasher) FlashCertificates(certificatePaths *paths.PathList, URLs [
102101
logrus.Infof("Converting and flashing certificate %s", certPath)
103102
flasherOut.Write([]byte(fmt.Sprintf("Converting and flashing certificate %s\n", certPath)))
104103

105-
data, err := f.certificateFromFile(certPath)
104+
certs, err := certificates.LoadCertificatesFromFile(certPath)
106105
if err != nil {
107106
return err
108107
}
109-
certificatesData = append(certificatesData, data...)
108+
for _, cert := range certs {
109+
data := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: cert.Raw})
110+
certificatesData = append(certificatesData, data...)
111+
}
110112
}
111113

112114
for _, URL := range URLs {
@@ -141,29 +143,6 @@ func (f *NinaFlasher) FlashCertificates(certificatePaths *paths.PathList, URLs [
141143
return nil
142144
}
143145

144-
func (f *NinaFlasher) certificateFromFile(certificateFile *paths.Path) ([]byte, error) {
145-
data, err := certificateFile.ReadFile()
146-
if err != nil {
147-
logrus.Error(err)
148-
return nil, err
149-
}
150-
switch certificateFile.Ext() {
151-
case ".cer":
152-
// the data needs to be formatted in PEM format
153-
cert, err := x509.ParseCertificate(data)
154-
if err != nil {
155-
logrus.Error(err)
156-
return nil, err
157-
}
158-
return pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: cert.Raw}), nil
159-
case ".pem":
160-
// the data is already encoded in pem format and we do not need to parse it.
161-
return data, nil
162-
default:
163-
return nil, fmt.Errorf("cert format %s not supported, please use .pem or .cer", certificateFile.Ext())
164-
}
165-
}
166-
167146
func (f *NinaFlasher) certificateFromURL(URL string) ([]byte, error) {
168147
rootCertificate, err := certificates.ScrapeRootCertificatesFromURL(URL)
169148
if err != nil {

‎flasher/winc.go

Copy file name to clipboardExpand all lines: flasher/winc.go
+8-24Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,17 @@ func (f *WincFlasher) FlashCertificates(certificatePaths *paths.PathList, URLs [
8383
logrus.Infof("Converting and flashing certificate %s", certPath)
8484
flasherOut.Write([]byte(fmt.Sprintf("Converting and flashing certificate %s\n", certPath)))
8585

86-
data, err := f.certificateFromFile(certPath)
86+
certs, err := certificates.LoadCertificatesFromFile(certPath)
8787
if err != nil {
8888
return err
8989
}
90-
certificatesData = append(certificatesData, data...)
90+
for _, cert := range certs {
91+
data, err := f.getCertificateData(cert)
92+
if err != nil {
93+
return err
94+
}
95+
certificatesData = append(certificatesData, data...)
96+
}
9197
}
9298

9399
for _, URL := range URLs {
@@ -110,28 +116,6 @@ func (f *WincFlasher) FlashCertificates(certificatePaths *paths.PathList, URLs [
110116
return nil
111117
}
112118

113-
func (f *WincFlasher) certificateFromFile(certificateFile *paths.Path) ([]byte, error) {
114-
data, err := certificateFile.ReadFile()
115-
if err != nil {
116-
logrus.Error(err)
117-
return nil, err
118-
}
119-
switch certificateFile.Ext() {
120-
case ".cer":
121-
cert, err := x509.ParseCertificate(data)
122-
if err != nil {
123-
logrus.Error(err)
124-
return nil, err
125-
}
126-
return f.getCertificateData(cert)
127-
case ".pem":
128-
// the data is already encoded in pem format and we do not need to parse it.
129-
return data, nil
130-
default:
131-
return nil, fmt.Errorf("cert format %s not supported, please use .pem or .cer", certificateFile.Ext())
132-
}
133-
}
134-
135119
func (f *WincFlasher) certificateFromURL(URL string) ([]byte, error) {
136120
rootCertificate, err := certificates.ScrapeRootCertificatesFromURL(URL)
137121
if err != nil {

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.