mirror of
https://github.com/caddyserver/caddy.git
synced 2025-01-14 23:06:27 +03:00
caddytls: Encode big.Int as string with JSON
This commit is contained in:
parent
8ff330c555
commit
7ca15861dd
1 changed files with 25 additions and 4 deletions
|
@ -17,6 +17,7 @@ package caddytls
|
||||||
import (
|
import (
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"crypto/x509"
|
"crypto/x509"
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math/big"
|
"math/big"
|
||||||
|
|
||||||
|
@ -29,7 +30,7 @@ import (
|
||||||
// This was needed to solve https://github.com/caddyserver/caddy/issues/2588.
|
// This was needed to solve https://github.com/caddyserver/caddy/issues/2588.
|
||||||
type CustomCertSelectionPolicy struct {
|
type CustomCertSelectionPolicy struct {
|
||||||
// The certificate must have one of these serial numbers.
|
// 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.
|
// The certificate must have one of these organization names.
|
||||||
SubjectOrganization []string `json:"subject_organization,omitempty"`
|
SubjectOrganization []string `json:"subject_organization,omitempty"`
|
||||||
|
@ -57,7 +58,7 @@ nextChoice:
|
||||||
if len(p.SerialNumber) > 0 {
|
if len(p.SerialNumber) > 0 {
|
||||||
var found bool
|
var found bool
|
||||||
for _, sn := range p.SerialNumber {
|
for _, sn := range p.SerialNumber {
|
||||||
if cert.Leaf.SerialNumber.Cmp(sn) == 0 {
|
if cert.Leaf.SerialNumber.Cmp(&sn.Int) == 0 {
|
||||||
found = true
|
found = true
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
@ -120,5 +121,25 @@ nextChoice:
|
||||||
return certmagic.DefaultCertificateSelector(hello, viable)
|
return certmagic.DefaultCertificateSelector(hello, viable)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Interface guard
|
// bigInt is a big.Int type that interops with JSON encodings as a string.
|
||||||
var _ certmagic.CertificateSelector = (*CustomCertSelectionPolicy)(nil)
|
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
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue