summaryrefslogtreecommitdiff
path: root/modules/caddytls/certselection.go
diff options
context:
space:
mode:
authorMatthew Holt <mholt@users.noreply.github.com>2020-04-02 09:43:33 -0600
committerMatthew Holt <mholt@users.noreply.github.com>2020-04-02 09:43:33 -0600
commit7ca15861dd70e72f242b2c0077b5678bae83ffad (patch)
tree4936d0ee85ebc9d4ae7dfdea0b1d7e63edf7d7f8 /modules/caddytls/certselection.go
parent8ff330c555d86f20f44a5579869be591a7f8cc7c (diff)
caddytls: Encode big.Int as string with JSON
Diffstat (limited to 'modules/caddytls/certselection.go')
-rw-r--r--modules/caddytls/certselection.go29
1 files changed, 25 insertions, 4 deletions
diff --git a/modules/caddytls/certselection.go b/modules/caddytls/certselection.go
index 8a24034..0311f11 100644
--- a/modules/caddytls/certselection.go
+++ b/modules/caddytls/certselection.go
@@ -17,6 +17,7 @@ package caddytls
import (
"crypto/tls"
"crypto/x509"
+ "encoding/json"
"fmt"
"math/big"
@@ -29,7 +30,7 @@ import (
// This was needed to solve https://github.com/caddyserver/caddy/issues/2588.
type CustomCertSelectionPolicy struct {
// The certificate must have one of these serial numbers.
- SerialNumber []*big.Int `json:"serial_number,omitempty"`
+ SerialNumber []bigInt `json:"serial_number,omitempty"`
// The certificate must have one of these organization names.
SubjectOrganization []string `json:"subject_organization,omitempty"`
@@ -57,7 +58,7 @@ nextChoice:
if len(p.SerialNumber) > 0 {
var found bool
for _, sn := range p.SerialNumber {
- if cert.Leaf.SerialNumber.Cmp(sn) == 0 {
+ if cert.Leaf.SerialNumber.Cmp(&sn.Int) == 0 {
found = true
break
}
@@ -120,5 +121,25 @@ nextChoice:
return certmagic.DefaultCertificateSelector(hello, viable)
}
-// Interface guard
-var _ certmagic.CertificateSelector = (*CustomCertSelectionPolicy)(nil)
+// bigInt is a big.Int type that interops with JSON encodings as a string.
+type bigInt struct{ big.Int }
+
+func (bi bigInt) MarshalJSON() ([]byte, error) {
+ return json.Marshal(bi.String())
+}
+
+func (bi *bigInt) UnmarshalJSON(p []byte) error {
+ if string(p) == "null" {
+ return nil
+ }
+ var stringRep string
+ err := json.Unmarshal(p, &stringRep)
+ if err != nil {
+ return err
+ }
+ _, ok := bi.SetString(stringRep, 10)
+ if !ok {
+ return fmt.Errorf("not a valid big integer: %s", p)
+ }
+ return nil
+}