Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 14, 05:15 UTC
caddytls: Encode big.Int as string with JSON
Matthew Holt committed
Apr 2, 2020 at 09:43 UTC
7ca15861dd70e72f242b2c0077b5678bae83ffad
1 file changed
+25
-4
modules/caddytls/certselection.go
+25
-4
@@ -17,6 +17,7 @@ package caddytls
17
import (
18
"crypto/tls"
19
"crypto/x509"
20
+ "encoding/json"
21
"fmt"
22
"math/big"
23
@@ -29,7 +30,7 @@ import (
30
// This was needed to solve https://github.com/caddyserver/caddy/issues/2588.
31
type CustomCertSelectionPolicy struct {
32
// The certificate must have one of these serial numbers.
32
- SerialNumber []*big.Int `json:"serial_number,omitempty"`
33
+ SerialNumber []bigInt `json:"serial_number,omitempty"`
34
35
// The certificate must have one of these organization names.
36
SubjectOrganization []string `json:"subject_organization,omitempty"`
@@ -57,7 +58,7 @@ nextChoice:
58
if len(p.SerialNumber) > 0 {
59
var found bool
60
for _, sn := range p.SerialNumber {
60
- if cert.Leaf.SerialNumber.Cmp(sn) == 0 {
61
+ if cert.Leaf.SerialNumber.Cmp(&sn.Int) == 0 {
62
found = true
63
break
64
}
@@ -120,5 +121,25 @@ nextChoice:
121
return certmagic.DefaultCertificateSelector(hello, viable)
122
}
123
123
-// Interface guard
124
-var _ certmagic.CertificateSelector = (*CustomCertSelectionPolicy)(nil)
124
+// bigInt is a big.Int type that interops with JSON encodings as a string.
125
+type bigInt struct{ big.Int }
126
+
127
+func (bi bigInt) MarshalJSON() ([]byte, error) {
128
+ return json.Marshal(bi.String())
129
+}
130
+
131
+func (bi *bigInt) UnmarshalJSON(p []byte) error {
132
+ if string(p) == "null" {
133
+ return nil
134
+ }
135
+ var stringRep string
136
+ err := json.Unmarshal(p, &stringRep)
137
+ if err != nil {
138
+ return err
139
+ }
140
+ _, ok := bi.SetString(stringRep, 10)
141
+ if !ok {
142
+ return fmt.Errorf("not a valid big integer: %s", p)
143
+ }
144
+ return nil
145
+}