Skip to content

Commit eac7cf0

Browse files
mateusz834gopherbot
authored andcommitted
x509roots/fallback: move parsing code to a non-generated file
For golang/go#73691 Change-Id: I3e2b09055c39286d863fe70ca3bd72a839e25d0a Reviewed-on: https://go-review.googlesource.com/c/crypto/+/676215 Reviewed-by: Michael Knyszek <mknyszek@google.com> Reviewed-by: Roland Shoemaker <roland@golang.org> Reviewed-by: Sean Liao <sean@liao.dev> Auto-Submit: Sean Liao <sean@liao.dev> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
1 parent 18228cd commit eac7cf0

File tree

3 files changed

+59
-124
lines changed

3 files changed

+59
-124
lines changed

x509roots/fallback/bundle.go

Lines changed: 0 additions & 61 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

x509roots/fallback/fallback.go

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,16 @@
1818
// available.
1919
package fallback
2020

21-
import "crypto/x509"
21+
import (
22+
"crypto/x509"
23+
"encoding/pem"
24+
"fmt"
25+
"time"
26+
)
2227

2328
func init() {
2429
p := x509.NewCertPool()
25-
for _, c := range parsedCertificates {
30+
for _, c := range mustParse(unparsedCertificates) {
2631
if len(c.constraints) == 0 {
2732
p.AddCert(c.cert)
2833
} else {
@@ -38,3 +43,55 @@ func init() {
3843
}
3944
x509.SetFallbackRoots(p)
4045
}
46+
47+
type unparsedCertificate struct {
48+
cn string
49+
sha256Hash string
50+
pem string
51+
52+
// possible constraints
53+
distrustAfter string
54+
}
55+
56+
type parsedCertificate struct {
57+
cert *x509.Certificate
58+
constraints []func([]*x509.Certificate) error
59+
}
60+
61+
func mustParse(unparsedCerts []unparsedCertificate) []parsedCertificate {
62+
var b []parsedCertificate
63+
for _, unparsed := range unparsedCerts {
64+
block, rest := pem.Decode([]byte(unparsed.pem))
65+
if block == nil {
66+
panic(fmt.Sprintf("unexpected nil PEM block for %q", unparsed.cn))
67+
}
68+
if len(rest) != 0 {
69+
panic(fmt.Sprintf("unexpected trailing data in PEM for %q", unparsed.cn))
70+
}
71+
if block.Type != "CERTIFICATE" {
72+
panic(fmt.Sprintf("unexpected PEM block type for %q: %s", unparsed.cn, block.Type))
73+
}
74+
cert, err := x509.ParseCertificate(block.Bytes)
75+
if err != nil {
76+
panic(err)
77+
}
78+
parsed := parsedCertificate{cert: cert}
79+
// parse possible constraints, this should check all fields of unparsedCertificate.
80+
if unparsed.distrustAfter != "" {
81+
distrustAfter, err := time.Parse(time.RFC3339, unparsed.distrustAfter)
82+
if err != nil {
83+
panic(fmt.Sprintf("failed to parse distrustAfter %q: %s", unparsed.distrustAfter, err))
84+
}
85+
parsed.constraints = append(parsed.constraints, func(chain []*x509.Certificate) error {
86+
for _, c := range chain {
87+
if c.NotBefore.After(distrustAfter) {
88+
return fmt.Errorf("certificate issued after distrust-after date %q", distrustAfter)
89+
}
90+
}
91+
return nil
92+
})
93+
}
94+
b = append(b, parsed)
95+
}
96+
return b
97+
}

x509roots/gen_fallback_bundle.go

Lines changed: 0 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -30,67 +30,6 @@ const tmpl = `// Code generated by gen_fallback_bundle.go; DO NOT EDIT.
3030
3131
package fallback
3232
33-
import (
34-
"crypto/x509"
35-
"encoding/pem"
36-
"fmt"
37-
"time"
38-
)
39-
40-
type unparsedCertificate struct {
41-
cn string
42-
sha256Hash string
43-
pem string
44-
45-
// possible constraints
46-
distrustAfter string
47-
}
48-
49-
type parsedCertificate struct {
50-
cert *x509.Certificate
51-
constraints []func([]*x509.Certificate) error
52-
}
53-
54-
func mustParse(unparsedCerts []unparsedCertificate) []parsedCertificate {
55-
var b []parsedCertificate
56-
for _, unparsed := range unparsedCerts {
57-
block, rest := pem.Decode([]byte(unparsed.pem))
58-
if block == nil {
59-
panic(fmt.Sprintf("unexpected nil PEM block for %q", unparsed.cn))
60-
}
61-
if len(rest) != 0 {
62-
panic(fmt.Sprintf("unexpected trailing data in PEM for %q", unparsed.cn))
63-
}
64-
if block.Type != "CERTIFICATE" {
65-
panic(fmt.Sprintf("unexpected PEM block type for %q: %s", unparsed.cn, block.Type))
66-
}
67-
cert, err := x509.ParseCertificate(block.Bytes)
68-
if err != nil {
69-
panic(err)
70-
}
71-
parsed := parsedCertificate{cert: cert}
72-
// parse possible constraints, this should check all fields of unparsedCertificate.
73-
if unparsed.distrustAfter != "" {
74-
distrustAfter, err := time.Parse(time.RFC3339, unparsed.distrustAfter)
75-
if err != nil {
76-
panic(fmt.Sprintf("failed to parse distrustAfter %q: %s", unparsed.distrustAfter, err))
77-
}
78-
parsed.constraints = append(parsed.constraints, func(chain []*x509.Certificate) error {
79-
for _, c := range chain {
80-
if c.NotBefore.After(distrustAfter) {
81-
return fmt.Errorf("certificate issued after distrust-after date %q", distrustAfter)
82-
}
83-
}
84-
return nil
85-
})
86-
}
87-
b = append(b, parsed)
88-
}
89-
return b
90-
}
91-
92-
var parsedCertificates = mustParse(unparsedCertificates)
93-
9433
var unparsedCertificates = []unparsedCertificate{
9534
`
9635

0 commit comments

Comments
 (0)