2021-02-28 18:09:06 +00:00
|
|
|
package acme
|
2021-02-28 01:05:37 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto"
|
|
|
|
"crypto/sha256"
|
|
|
|
"crypto/subtle"
|
|
|
|
"crypto/tls"
|
2022-04-08 20:49:11 +00:00
|
|
|
"crypto/x509"
|
2021-02-28 01:05:37 +00:00
|
|
|
"encoding/asn1"
|
|
|
|
"encoding/base64"
|
|
|
|
"encoding/hex"
|
|
|
|
"encoding/json"
|
2022-06-07 04:09:31 +00:00
|
|
|
"encoding/pem"
|
2021-08-18 00:06:25 +00:00
|
|
|
"errors"
|
2021-02-28 01:05:37 +00:00
|
|
|
"fmt"
|
2021-11-12 23:46:34 +00:00
|
|
|
"io"
|
2021-02-28 01:05:37 +00:00
|
|
|
"net"
|
2021-03-30 05:58:26 +00:00
|
|
|
"net/url"
|
2022-06-07 04:09:31 +00:00
|
|
|
"os"
|
2021-08-18 00:06:25 +00:00
|
|
|
"reflect"
|
2021-02-28 01:05:37 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2022-04-08 20:49:11 +00:00
|
|
|
"github.com/fxamacker/cbor/v2"
|
2021-02-28 01:05:37 +00:00
|
|
|
"go.step.sm/crypto/jose"
|
2022-07-15 00:10:03 +00:00
|
|
|
"go.step.sm/crypto/pemutil"
|
2021-02-28 01:05:37 +00:00
|
|
|
)
|
|
|
|
|
2021-06-18 10:39:36 +00:00
|
|
|
type ChallengeType string
|
|
|
|
|
|
|
|
const (
|
2021-11-12 16:13:10 +00:00
|
|
|
// HTTP01 is the http-01 ACME challenge type
|
|
|
|
HTTP01 ChallengeType = "http-01"
|
|
|
|
// DNS01 is the dns-01 ACME challenge type
|
|
|
|
DNS01 ChallengeType = "dns-01"
|
|
|
|
// TLSALPN01 is the tls-alpn-01 ACME challenge type
|
2021-06-18 10:39:36 +00:00
|
|
|
TLSALPN01 ChallengeType = "tls-alpn-01"
|
2022-04-02 02:56:05 +00:00
|
|
|
// DEVICEATTEST01 is the device-attest-01 ACME challenge type
|
|
|
|
DEVICEATTEST01 ChallengeType = "device-attest-01"
|
2021-06-18 10:39:36 +00:00
|
|
|
)
|
|
|
|
|
2021-02-28 01:05:37 +00:00
|
|
|
// Challenge represents an ACME response Challenge type.
|
|
|
|
type Challenge struct {
|
2021-06-18 10:39:36 +00:00
|
|
|
ID string `json:"-"`
|
|
|
|
AccountID string `json:"-"`
|
|
|
|
AuthorizationID string `json:"-"`
|
|
|
|
Value string `json:"-"`
|
|
|
|
Type ChallengeType `json:"type"`
|
|
|
|
Status Status `json:"status"`
|
|
|
|
Token string `json:"token"`
|
|
|
|
ValidatedAt string `json:"validated,omitempty"`
|
|
|
|
URL string `json:"url"`
|
|
|
|
Error *Error `json:"error,omitempty"`
|
2021-02-28 01:05:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ToLog enables response logging.
|
|
|
|
func (ch *Challenge) ToLog() (interface{}, error) {
|
|
|
|
b, err := json.Marshal(ch)
|
|
|
|
if err != nil {
|
2021-03-05 07:10:46 +00:00
|
|
|
return nil, WrapErrorISE(err, "error marshaling challenge for logging")
|
2021-02-28 01:05:37 +00:00
|
|
|
}
|
|
|
|
return string(b), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate attempts to validate the challenge. Stores changes to the Challenge
|
|
|
|
// type using the DB interface.
|
|
|
|
// satisfactorily validated, the 'status' and 'validated' attributes are
|
|
|
|
// updated.
|
2022-04-02 02:56:05 +00:00
|
|
|
func (ch *Challenge) Validate(ctx context.Context, db DB, jwk *jose.JSONWebKey, payload []byte) error {
|
2021-02-28 01:05:37 +00:00
|
|
|
// If already valid or invalid then return without performing validation.
|
2021-03-30 05:58:26 +00:00
|
|
|
if ch.Status != StatusPending {
|
2021-02-28 01:05:37 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
switch ch.Type {
|
2021-06-18 10:39:36 +00:00
|
|
|
case HTTP01:
|
2022-04-29 02:15:18 +00:00
|
|
|
return http01Validate(ctx, ch, db, jwk)
|
2021-06-18 10:39:36 +00:00
|
|
|
case DNS01:
|
2022-04-29 02:15:18 +00:00
|
|
|
return dns01Validate(ctx, ch, db, jwk)
|
2021-06-18 10:39:36 +00:00
|
|
|
case TLSALPN01:
|
2022-04-29 02:15:18 +00:00
|
|
|
return tlsalpn01Validate(ctx, ch, db, jwk)
|
2022-04-02 02:56:05 +00:00
|
|
|
case DEVICEATTEST01:
|
|
|
|
return deviceAttest01Validate(ctx, ch, db, jwk, payload)
|
2021-02-28 01:05:37 +00:00
|
|
|
default:
|
2021-03-05 07:10:46 +00:00
|
|
|
return NewErrorISE("unexpected challenge type '%s'", ch.Type)
|
2021-02-28 01:05:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-29 02:15:18 +00:00
|
|
|
func http01Validate(ctx context.Context, ch *Challenge, db DB, jwk *jose.JSONWebKey) error {
|
2022-04-07 10:37:34 +00:00
|
|
|
u := &url.URL{Scheme: "http", Host: http01ChallengeHost(ch.Value), Path: fmt.Sprintf("/.well-known/acme-challenge/%s", ch.Token)}
|
2021-02-28 01:05:37 +00:00
|
|
|
|
2022-04-29 02:15:18 +00:00
|
|
|
vc := MustClientFromContext(ctx)
|
|
|
|
resp, err := vc.Get(u.String())
|
2021-02-28 01:05:37 +00:00
|
|
|
if err != nil {
|
2021-03-30 05:58:26 +00:00
|
|
|
return storeError(ctx, db, ch, false, WrapError(ErrorConnectionType, err,
|
2021-10-08 18:59:57 +00:00
|
|
|
"error doing http GET for url %s", u))
|
2021-02-28 01:05:37 +00:00
|
|
|
}
|
2021-03-30 05:58:26 +00:00
|
|
|
defer resp.Body.Close()
|
2021-02-28 01:05:37 +00:00
|
|
|
if resp.StatusCode >= 400 {
|
2021-03-30 05:58:26 +00:00
|
|
|
return storeError(ctx, db, ch, false, NewError(ErrorConnectionType,
|
2021-10-08 18:59:57 +00:00
|
|
|
"error doing http GET for url %s with status code %d", u, resp.StatusCode))
|
2021-02-28 01:05:37 +00:00
|
|
|
}
|
|
|
|
|
2021-11-12 23:46:34 +00:00
|
|
|
body, err := io.ReadAll(resp.Body)
|
2021-02-28 01:05:37 +00:00
|
|
|
if err != nil {
|
2021-03-05 07:10:46 +00:00
|
|
|
return WrapErrorISE(err, "error reading "+
|
2021-10-08 18:59:57 +00:00
|
|
|
"response body for url %s", u)
|
2021-02-28 01:05:37 +00:00
|
|
|
}
|
2021-03-30 05:58:26 +00:00
|
|
|
keyAuth := strings.TrimSpace(string(body))
|
2021-02-28 01:05:37 +00:00
|
|
|
|
|
|
|
expected, err := KeyAuthorization(ch.Token, jwk)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if keyAuth != expected {
|
2021-03-30 05:58:26 +00:00
|
|
|
return storeError(ctx, db, ch, true, NewError(ErrorRejectedIdentifierType,
|
2021-03-01 07:33:18 +00:00
|
|
|
"keyAuthorization does not match; expected %s, but got %s", expected, keyAuth))
|
2021-02-28 01:05:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update and store the challenge.
|
|
|
|
ch.Status = StatusValid
|
|
|
|
ch.Error = nil
|
2021-03-19 06:08:13 +00:00
|
|
|
ch.ValidatedAt = clock.Now().Format(time.RFC3339)
|
2021-02-28 01:05:37 +00:00
|
|
|
|
2021-03-01 07:33:18 +00:00
|
|
|
if err = db.UpdateChallenge(ctx, ch); err != nil {
|
2021-03-05 07:10:46 +00:00
|
|
|
return WrapErrorISE(err, "error updating challenge")
|
2021-03-01 07:33:18 +00:00
|
|
|
}
|
|
|
|
return nil
|
2021-02-28 01:05:37 +00:00
|
|
|
}
|
|
|
|
|
2022-04-07 10:37:34 +00:00
|
|
|
// http01ChallengeHost checks if a Challenge value is an IPv6 address
|
|
|
|
// and adds square brackets if that's the case, so that it can be used
|
|
|
|
// as a hostname. Returns the original Challenge value as the host to
|
|
|
|
// use in other cases.
|
|
|
|
func http01ChallengeHost(value string) string {
|
|
|
|
if ip := net.ParseIP(value); ip != nil && ip.To4() == nil {
|
|
|
|
value = "[" + value + "]"
|
|
|
|
}
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
|
2021-08-18 00:06:25 +00:00
|
|
|
func tlsAlert(err error) uint8 {
|
|
|
|
var opErr *net.OpError
|
|
|
|
if errors.As(err, &opErr) {
|
|
|
|
v := reflect.ValueOf(opErr.Err)
|
|
|
|
if v.Kind() == reflect.Uint8 {
|
|
|
|
return uint8(v.Uint())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2022-04-29 02:15:18 +00:00
|
|
|
func tlsalpn01Validate(ctx context.Context, ch *Challenge, db DB, jwk *jose.JSONWebKey) error {
|
2021-02-28 01:05:37 +00:00
|
|
|
config := &tls.Config{
|
2021-03-30 05:58:26 +00:00
|
|
|
NextProtos: []string{"acme-tls/1"},
|
|
|
|
// https://tools.ietf.org/html/rfc8737#section-4
|
|
|
|
// ACME servers that implement "acme-tls/1" MUST only negotiate TLS 1.2
|
|
|
|
// [RFC5246] or higher when connecting to clients for validation.
|
|
|
|
MinVersion: tls.VersionTLS12,
|
2021-06-18 15:27:35 +00:00
|
|
|
ServerName: serverName(ch),
|
2022-08-19 00:46:20 +00:00
|
|
|
InsecureSkipVerify: true, // nolint:gosec // we expect a self-signed challenge certificate
|
2021-02-28 01:05:37 +00:00
|
|
|
}
|
|
|
|
|
2021-03-01 06:49:20 +00:00
|
|
|
hostPort := net.JoinHostPort(ch.Value, "443")
|
2021-02-28 01:05:37 +00:00
|
|
|
|
2022-04-29 02:15:18 +00:00
|
|
|
vc := MustClientFromContext(ctx)
|
|
|
|
conn, err := vc.TLSDial("tcp", hostPort, config)
|
2021-02-28 01:05:37 +00:00
|
|
|
if err != nil {
|
2021-08-17 23:31:53 +00:00
|
|
|
// With Go 1.17+ tls.Dial fails if there's no overlap between configured
|
2021-08-18 00:06:25 +00:00
|
|
|
// client and server protocols. When this happens the connection is
|
|
|
|
// closed with the error no_application_protocol(120) as required by
|
|
|
|
// RFC7301. See https://golang.org/doc/go1.17#ALPN
|
|
|
|
if tlsAlert(err) == 120 {
|
2021-08-17 23:31:53 +00:00
|
|
|
return storeError(ctx, db, ch, true, NewError(ErrorRejectedIdentifierType,
|
|
|
|
"cannot negotiate ALPN acme-tls/1 protocol for tls-alpn-01 challenge"))
|
|
|
|
}
|
2021-03-30 05:58:26 +00:00
|
|
|
return storeError(ctx, db, ch, false, WrapError(ErrorConnectionType, err,
|
2021-03-01 07:33:18 +00:00
|
|
|
"error doing TLS dial for %s", hostPort))
|
2021-02-28 01:05:37 +00:00
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
cs := conn.ConnectionState()
|
|
|
|
certs := cs.PeerCertificates
|
|
|
|
|
|
|
|
if len(certs) == 0 {
|
2021-03-30 05:58:26 +00:00
|
|
|
return storeError(ctx, db, ch, true, NewError(ErrorRejectedIdentifierType,
|
2021-03-01 07:33:18 +00:00
|
|
|
"%s challenge for %s resulted in no certificates", ch.Type, ch.Value))
|
2021-02-28 01:05:37 +00:00
|
|
|
}
|
|
|
|
|
2021-04-13 21:53:05 +00:00
|
|
|
if cs.NegotiatedProtocol != "acme-tls/1" {
|
2021-03-30 05:58:26 +00:00
|
|
|
return storeError(ctx, db, ch, true, NewError(ErrorRejectedIdentifierType,
|
2021-03-01 07:33:18 +00:00
|
|
|
"cannot negotiate ALPN acme-tls/1 protocol for tls-alpn-01 challenge"))
|
2021-02-28 01:05:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
leafCert := certs[0]
|
2021-05-28 22:19:14 +00:00
|
|
|
|
|
|
|
// if no DNS names present, look for IP address and verify that exactly one exists
|
|
|
|
if len(leafCert.DNSNames) == 0 {
|
2021-06-18 15:27:35 +00:00
|
|
|
if len(leafCert.IPAddresses) != 1 || !leafCert.IPAddresses[0].Equal(net.ParseIP(ch.Value)) {
|
2021-05-28 22:19:14 +00:00
|
|
|
return storeError(ctx, db, ch, true, NewError(ErrorRejectedIdentifierType,
|
2021-06-04 06:42:24 +00:00
|
|
|
"incorrect certificate for tls-alpn-01 challenge: leaf certificate must contain a single IP address or DNS name, %v", ch.Value))
|
2021-05-28 22:19:14 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if len(leafCert.DNSNames) != 1 || !strings.EqualFold(leafCert.DNSNames[0], ch.Value) {
|
|
|
|
return storeError(ctx, db, ch, true, NewError(ErrorRejectedIdentifierType,
|
2021-06-04 06:42:24 +00:00
|
|
|
"incorrect certificate for tls-alpn-01 challenge: leaf certificate must contain a single IP address or DNS name, %v", ch.Value))
|
2021-05-28 22:19:14 +00:00
|
|
|
}
|
2021-02-28 01:05:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
idPeAcmeIdentifier := asn1.ObjectIdentifier{1, 3, 6, 1, 5, 5, 7, 1, 31}
|
|
|
|
idPeAcmeIdentifierV1Obsolete := asn1.ObjectIdentifier{1, 3, 6, 1, 5, 5, 7, 1, 30, 1}
|
|
|
|
foundIDPeAcmeIdentifierV1Obsolete := false
|
|
|
|
|
2021-03-01 06:49:20 +00:00
|
|
|
keyAuth, err := KeyAuthorization(ch.Token, jwk)
|
2021-02-28 01:05:37 +00:00
|
|
|
if err != nil {
|
2021-03-01 06:49:20 +00:00
|
|
|
return err
|
2021-02-28 01:05:37 +00:00
|
|
|
}
|
|
|
|
hashedKeyAuth := sha256.Sum256([]byte(keyAuth))
|
|
|
|
|
|
|
|
for _, ext := range leafCert.Extensions {
|
|
|
|
if idPeAcmeIdentifier.Equal(ext.Id) {
|
|
|
|
if !ext.Critical {
|
2021-03-30 05:58:26 +00:00
|
|
|
return storeError(ctx, db, ch, true, NewError(ErrorRejectedIdentifierType,
|
2021-03-01 07:33:18 +00:00
|
|
|
"incorrect certificate for tls-alpn-01 challenge: acmeValidationV1 extension not critical"))
|
2021-02-28 01:05:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var extValue []byte
|
|
|
|
rest, err := asn1.Unmarshal(ext.Value, &extValue)
|
|
|
|
|
|
|
|
if err != nil || len(rest) > 0 || len(hashedKeyAuth) != len(extValue) {
|
2021-03-30 05:58:26 +00:00
|
|
|
return storeError(ctx, db, ch, true, NewError(ErrorRejectedIdentifierType,
|
2021-03-01 07:33:18 +00:00
|
|
|
"incorrect certificate for tls-alpn-01 challenge: malformed acmeValidationV1 extension value"))
|
2021-02-28 01:05:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if subtle.ConstantTimeCompare(hashedKeyAuth[:], extValue) != 1 {
|
2021-03-30 05:58:26 +00:00
|
|
|
return storeError(ctx, db, ch, true, NewError(ErrorRejectedIdentifierType,
|
2021-03-01 07:33:18 +00:00
|
|
|
"incorrect certificate for tls-alpn-01 challenge: "+
|
|
|
|
"expected acmeValidationV1 extension value %s for this challenge but got %s",
|
|
|
|
hex.EncodeToString(hashedKeyAuth[:]), hex.EncodeToString(extValue)))
|
2021-02-28 01:05:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ch.Status = StatusValid
|
|
|
|
ch.Error = nil
|
2021-03-19 06:08:13 +00:00
|
|
|
ch.ValidatedAt = clock.Now().Format(time.RFC3339)
|
2021-02-28 01:05:37 +00:00
|
|
|
|
2021-03-01 06:49:20 +00:00
|
|
|
if err = db.UpdateChallenge(ctx, ch); err != nil {
|
2021-03-05 07:10:46 +00:00
|
|
|
return WrapErrorISE(err, "tlsalpn01ValidateChallenge - error updating challenge")
|
2021-03-01 06:49:20 +00:00
|
|
|
}
|
|
|
|
return nil
|
2021-02-28 01:05:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if idPeAcmeIdentifierV1Obsolete.Equal(ext.Id) {
|
|
|
|
foundIDPeAcmeIdentifierV1Obsolete = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if foundIDPeAcmeIdentifierV1Obsolete {
|
2021-03-30 05:58:26 +00:00
|
|
|
return storeError(ctx, db, ch, true, NewError(ErrorRejectedIdentifierType,
|
2021-03-01 07:33:18 +00:00
|
|
|
"incorrect certificate for tls-alpn-01 challenge: obsolete id-pe-acmeIdentifier in acmeValidationV1 extension"))
|
2021-02-28 01:05:37 +00:00
|
|
|
}
|
|
|
|
|
2021-03-30 05:58:26 +00:00
|
|
|
return storeError(ctx, db, ch, true, NewError(ErrorRejectedIdentifierType,
|
2021-03-01 07:33:18 +00:00
|
|
|
"incorrect certificate for tls-alpn-01 challenge: missing acmeValidationV1 extension"))
|
2021-02-28 01:05:37 +00:00
|
|
|
}
|
|
|
|
|
2022-04-29 02:15:18 +00:00
|
|
|
func dns01Validate(ctx context.Context, ch *Challenge, db DB, jwk *jose.JSONWebKey) error {
|
2021-02-28 01:05:37 +00:00
|
|
|
// Normalize domain for wildcard DNS names
|
|
|
|
// This is done to avoid making TXT lookups for domains like
|
|
|
|
// _acme-challenge.*.example.com
|
|
|
|
// Instead perform txt lookup for _acme-challenge.example.com
|
2021-03-01 06:49:20 +00:00
|
|
|
domain := strings.TrimPrefix(ch.Value, "*.")
|
2021-02-28 01:05:37 +00:00
|
|
|
|
2022-04-29 02:15:18 +00:00
|
|
|
vc := MustClientFromContext(ctx)
|
|
|
|
txtRecords, err := vc.LookupTxt("_acme-challenge." + domain)
|
2021-02-28 01:05:37 +00:00
|
|
|
if err != nil {
|
2021-03-30 05:58:26 +00:00
|
|
|
return storeError(ctx, db, ch, false, WrapError(ErrorDNSType, err,
|
2021-03-01 07:33:18 +00:00
|
|
|
"error looking up TXT records for domain %s", domain))
|
2021-02-28 01:05:37 +00:00
|
|
|
}
|
|
|
|
|
2021-03-01 06:49:20 +00:00
|
|
|
expectedKeyAuth, err := KeyAuthorization(ch.Token, jwk)
|
2021-02-28 01:05:37 +00:00
|
|
|
if err != nil {
|
2021-03-01 06:49:20 +00:00
|
|
|
return err
|
2021-02-28 01:05:37 +00:00
|
|
|
}
|
|
|
|
h := sha256.Sum256([]byte(expectedKeyAuth))
|
|
|
|
expected := base64.RawURLEncoding.EncodeToString(h[:])
|
|
|
|
var found bool
|
|
|
|
for _, r := range txtRecords {
|
|
|
|
if r == expected {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
2021-03-30 05:58:26 +00:00
|
|
|
return storeError(ctx, db, ch, false, NewError(ErrorRejectedIdentifierType,
|
2021-03-01 07:33:18 +00:00
|
|
|
"keyAuthorization does not match; expected %s, but got %s", expectedKeyAuth, txtRecords))
|
2021-02-28 01:05:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update and store the challenge.
|
|
|
|
ch.Status = StatusValid
|
|
|
|
ch.Error = nil
|
2021-03-19 06:08:13 +00:00
|
|
|
ch.ValidatedAt = clock.Now().Format(time.RFC3339)
|
2021-02-28 01:05:37 +00:00
|
|
|
|
2021-03-01 07:33:18 +00:00
|
|
|
if err = db.UpdateChallenge(ctx, ch); err != nil {
|
2021-03-05 07:10:46 +00:00
|
|
|
return WrapErrorISE(err, "error updating challenge")
|
2021-03-01 07:33:18 +00:00
|
|
|
}
|
|
|
|
return nil
|
2021-02-28 01:05:37 +00:00
|
|
|
}
|
|
|
|
|
2022-04-08 20:49:11 +00:00
|
|
|
type Payload struct {
|
2022-07-15 00:10:03 +00:00
|
|
|
AttObj string `json:"attObj"`
|
|
|
|
Error string `json:"error"`
|
2022-04-08 20:49:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type AttestationObject struct {
|
|
|
|
Format string `json:"fmt"`
|
|
|
|
AttStatement map[string]interface{} `json:"attStmt,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(bweeks): move attestation verification to a shared package.
|
|
|
|
// TODO(bweeks): define new error type for failed attestation validation.
|
2022-04-02 02:56:05 +00:00
|
|
|
func deviceAttest01Validate(ctx context.Context, ch *Challenge, db DB, jwk *jose.JSONWebKey, payload []byte) error {
|
2022-04-08 20:49:11 +00:00
|
|
|
var p Payload
|
|
|
|
if err := json.Unmarshal(payload, &p); err != nil {
|
|
|
|
return WrapErrorISE(err, "error unmarshalling JSON")
|
|
|
|
}
|
|
|
|
|
2022-07-15 00:10:03 +00:00
|
|
|
if p.Error != "" {
|
2022-04-08 20:49:11 +00:00
|
|
|
return storeError(ctx, db, ch, true, NewError(ErrorRejectedIdentifierType,
|
2022-07-15 00:10:03 +00:00
|
|
|
"payload contained error: %v", p.Error))
|
2022-06-01 01:51:17 +00:00
|
|
|
}
|
|
|
|
|
2022-07-15 00:10:03 +00:00
|
|
|
attObj, err := base64.RawURLEncoding.DecodeString(p.AttObj)
|
2022-06-01 01:51:17 +00:00
|
|
|
if err != nil {
|
2022-07-15 00:10:03 +00:00
|
|
|
return WrapErrorISE(err, "error base64 decoding attObj")
|
2022-06-01 01:51:17 +00:00
|
|
|
}
|
|
|
|
|
2022-07-15 00:10:03 +00:00
|
|
|
att := AttestationObject{}
|
|
|
|
if err := cbor.Unmarshal(attObj, &att); err != nil {
|
|
|
|
return WrapErrorISE(err, "error unmarshalling CBOR")
|
2022-06-01 01:51:17 +00:00
|
|
|
}
|
|
|
|
|
2022-07-15 00:10:03 +00:00
|
|
|
switch att.Format {
|
|
|
|
case "apple":
|
|
|
|
data, err := doAppleAttestationFormat(ctx, ch, db, &att)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2022-04-08 20:49:11 +00:00
|
|
|
}
|
|
|
|
|
2022-08-09 22:06:52 +00:00
|
|
|
// Validate nonce with SHA-256 of the token.
|
|
|
|
if len(data.Nonce) != 0 {
|
2022-07-15 00:10:03 +00:00
|
|
|
sum := sha256.Sum256([]byte(ch.Token))
|
2022-08-09 22:06:52 +00:00
|
|
|
if subtle.ConstantTimeCompare(data.Nonce, sum[:]) != 1 {
|
2022-07-15 00:10:03 +00:00
|
|
|
return storeError(ctx, db, ch, true, NewError(ErrorBadAttestationStatement, "challenge token does not match"))
|
|
|
|
}
|
|
|
|
}
|
2022-04-08 20:49:11 +00:00
|
|
|
|
2022-07-15 00:10:03 +00:00
|
|
|
// Validate Apple's ClientIdentifier (Identifier.Value) with device
|
|
|
|
// identifiers.
|
|
|
|
//
|
|
|
|
// Note: We might want to use an external service for this.
|
|
|
|
if data.UDID != ch.Value && data.SerialNumber != ch.Value {
|
|
|
|
return storeError(ctx, db, ch, true, NewError(ErrorBadAttestationStatement, "permanent identifier does not match"))
|
|
|
|
}
|
2022-04-08 20:49:11 +00:00
|
|
|
|
2022-07-15 00:10:03 +00:00
|
|
|
// TODO(mariano): debug - remove me
|
|
|
|
pem.Encode(os.Stderr, &pem.Block{
|
|
|
|
Type: "CERTIFICATE", Bytes: data.Certificate.Raw,
|
|
|
|
})
|
|
|
|
default:
|
|
|
|
return storeError(ctx, db, ch, true, NewError(ErrorBadAttestationStatement, "unexpected attestation object format"))
|
2022-04-02 02:56:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update and store the challenge.
|
|
|
|
ch.Status = StatusValid
|
|
|
|
ch.Error = nil
|
|
|
|
ch.ValidatedAt = clock.Now().Format(time.RFC3339)
|
|
|
|
|
|
|
|
if err := db.UpdateChallenge(ctx, ch); err != nil {
|
|
|
|
return WrapErrorISE(err, "error updating challenge")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-07-15 00:10:03 +00:00
|
|
|
// Apple Enterprise Attestation Root CA from
|
|
|
|
// https://www.apple.com/certificateauthority/private/
|
|
|
|
const appleEnterpriseAttestationRootCA = `-----BEGIN CERTIFICATE-----
|
|
|
|
MIICJDCCAamgAwIBAgIUQsDCuyxyfFxeq/bxpm8frF15hzcwCgYIKoZIzj0EAwMw
|
|
|
|
UTEtMCsGA1UEAwwkQXBwbGUgRW50ZXJwcmlzZSBBdHRlc3RhdGlvbiBSb290IENB
|
|
|
|
MRMwEQYDVQQKDApBcHBsZSBJbmMuMQswCQYDVQQGEwJVUzAeFw0yMjAyMTYxOTAx
|
|
|
|
MjRaFw00NzAyMjAwMDAwMDBaMFExLTArBgNVBAMMJEFwcGxlIEVudGVycHJpc2Ug
|
|
|
|
QXR0ZXN0YXRpb24gUm9vdCBDQTETMBEGA1UECgwKQXBwbGUgSW5jLjELMAkGA1UE
|
|
|
|
BhMCVVMwdjAQBgcqhkjOPQIBBgUrgQQAIgNiAAT6Jigq+Ps9Q4CoT8t8q+UnOe2p
|
|
|
|
oT9nRaUfGhBTbgvqSGXPjVkbYlIWYO+1zPk2Sz9hQ5ozzmLrPmTBgEWRcHjA2/y7
|
|
|
|
7GEicps9wn2tj+G89l3INNDKETdxSPPIZpPj8VmjQjBAMA8GA1UdEwEB/wQFMAMB
|
|
|
|
Af8wHQYDVR0OBBYEFPNqTQGd8muBpV5du+UIbVbi+d66MA4GA1UdDwEB/wQEAwIB
|
|
|
|
BjAKBggqhkjOPQQDAwNpADBmAjEA1xpWmTLSpr1VH4f8Ypk8f3jMUKYz4QPG8mL5
|
|
|
|
8m9sX/b2+eXpTv2pH4RZgJjucnbcAjEA4ZSB6S45FlPuS/u4pTnzoz632rA+xW/T
|
|
|
|
ZwFEh9bhKjJ+5VQ9/Do1os0u3LEkgN/r
|
|
|
|
-----END CERTIFICATE-----`
|
|
|
|
|
|
|
|
var (
|
|
|
|
oidAppleSerialNumber = asn1.ObjectIdentifier{1, 2, 840, 113635, 100, 8, 9, 1}
|
|
|
|
oidAppleUniqueDeviceIdentifier = asn1.ObjectIdentifier{1, 2, 840, 113635, 100, 8, 9, 2}
|
|
|
|
oidAppleSecureEnclaveProcessorOSVersion = asn1.ObjectIdentifier{1, 2, 840, 113635, 100, 8, 10, 2}
|
|
|
|
oidAppleNonce = asn1.ObjectIdentifier{1, 2, 840, 113635, 100, 8, 11, 1}
|
|
|
|
)
|
2022-06-07 04:09:31 +00:00
|
|
|
|
2022-07-15 00:10:03 +00:00
|
|
|
type appleAttestationData struct {
|
2022-08-09 22:06:52 +00:00
|
|
|
Nonce []byte
|
2022-07-15 00:10:03 +00:00
|
|
|
SerialNumber string
|
|
|
|
UDID string
|
|
|
|
SEPVersion string
|
|
|
|
Certificate *x509.Certificate
|
|
|
|
}
|
2022-06-07 04:09:31 +00:00
|
|
|
|
2022-07-15 00:10:03 +00:00
|
|
|
func doAppleAttestationFormat(ctx context.Context, ch *Challenge, db DB, att *AttestationObject) (*appleAttestationData, error) {
|
|
|
|
root, err := pemutil.ParseCertificate([]byte(appleEnterpriseAttestationRootCA))
|
2022-06-07 04:09:31 +00:00
|
|
|
if err != nil {
|
2022-07-15 00:10:03 +00:00
|
|
|
return nil, WrapErrorISE(err, "error parsing apple enterprise ca")
|
2022-06-07 04:09:31 +00:00
|
|
|
}
|
2022-07-15 00:10:03 +00:00
|
|
|
roots := x509.NewCertPool()
|
|
|
|
roots.AddCert(root)
|
2022-06-07 04:09:31 +00:00
|
|
|
|
2022-07-15 00:10:03 +00:00
|
|
|
x5c, ok := att.AttStatement["x5c"].([]interface{})
|
|
|
|
if !ok {
|
|
|
|
return nil, storeError(ctx, db, ch, true, NewError(ErrorBadAttestationStatement, "x5c not present"))
|
2022-06-07 04:09:31 +00:00
|
|
|
}
|
2022-06-22 19:43:24 +00:00
|
|
|
if len(x5c) == 0 {
|
2022-07-15 00:10:03 +00:00
|
|
|
return nil, storeError(ctx, db, ch, true, NewError(ErrorRejectedIdentifierType, "x5c is empty"))
|
2022-06-22 19:43:24 +00:00
|
|
|
}
|
|
|
|
|
2022-07-15 00:10:03 +00:00
|
|
|
der, ok := x5c[0].([]byte)
|
|
|
|
if !ok {
|
|
|
|
return nil, storeError(ctx, db, ch, true, NewError(ErrorBadAttestationStatement, "x5c is malformed"))
|
2022-06-07 04:09:31 +00:00
|
|
|
}
|
2022-07-15 00:10:03 +00:00
|
|
|
leaf, err := x509.ParseCertificate(der)
|
2022-06-07 04:09:31 +00:00
|
|
|
if err != nil {
|
2022-07-15 00:10:03 +00:00
|
|
|
return nil, storeError(ctx, db, ch, true, WrapError(ErrorBadAttestationStatement, err, "x5c is malformed"))
|
2022-06-07 04:09:31 +00:00
|
|
|
}
|
|
|
|
|
2022-07-15 00:10:03 +00:00
|
|
|
intermediates := x509.NewCertPool()
|
|
|
|
for _, v := range x5c[1:] {
|
|
|
|
der, ok = v.([]byte)
|
|
|
|
if !ok {
|
|
|
|
return nil, storeError(ctx, db, ch, true, NewError(ErrorBadAttestationStatement, "x5c is malformed"))
|
|
|
|
}
|
|
|
|
cert, err := x509.ParseCertificate(der)
|
|
|
|
if err != nil {
|
|
|
|
return nil, storeError(ctx, db, ch, true, WrapError(ErrorBadAttestationStatement, err, "x5c is malformed"))
|
|
|
|
}
|
|
|
|
intermediates.AddCert(cert)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := leaf.Verify(x509.VerifyOptions{
|
|
|
|
Intermediates: intermediates,
|
|
|
|
Roots: roots,
|
|
|
|
CurrentTime: time.Now().Truncate(time.Second),
|
|
|
|
KeyUsages: []x509.ExtKeyUsage{x509.ExtKeyUsageAny},
|
|
|
|
}); err != nil {
|
|
|
|
return nil, storeError(ctx, db, ch, true, WrapError(ErrorBadAttestationStatement, err, "x5c is not valid"))
|
|
|
|
}
|
|
|
|
|
|
|
|
data := &appleAttestationData{
|
|
|
|
Certificate: leaf,
|
|
|
|
}
|
|
|
|
for _, ext := range leaf.Extensions {
|
|
|
|
switch {
|
|
|
|
case ext.Id.Equal(oidAppleSerialNumber):
|
|
|
|
data.SerialNumber = string(ext.Value)
|
|
|
|
case ext.Id.Equal(oidAppleUniqueDeviceIdentifier):
|
|
|
|
data.UDID = string(ext.Value)
|
|
|
|
case ext.Id.Equal(oidAppleSecureEnclaveProcessorOSVersion):
|
|
|
|
data.SEPVersion = string(ext.Value)
|
|
|
|
case ext.Id.Equal(oidAppleNonce):
|
2022-08-09 22:06:52 +00:00
|
|
|
data.Nonce = ext.Value
|
2022-07-15 00:10:03 +00:00
|
|
|
}
|
2022-06-07 04:09:31 +00:00
|
|
|
}
|
|
|
|
|
2022-07-15 00:10:03 +00:00
|
|
|
return data, nil
|
2022-06-07 04:09:31 +00:00
|
|
|
}
|
|
|
|
|
2021-06-18 15:27:35 +00:00
|
|
|
// serverName determines the SNI HostName to set based on an acme.Challenge
|
2021-06-25 12:07:40 +00:00
|
|
|
// for TLS-ALPN-01 challenges RFC8738 states that, if HostName is an IP, it
|
2021-06-18 15:27:35 +00:00
|
|
|
// should be the ARPA address https://datatracker.ietf.org/doc/html/rfc8738#section-6.
|
|
|
|
// It also references TLS Extensions [RFC6066].
|
|
|
|
func serverName(ch *Challenge) string {
|
|
|
|
var serverName string
|
|
|
|
ip := net.ParseIP(ch.Value)
|
|
|
|
if ip != nil {
|
|
|
|
serverName = reverseAddr(ip)
|
|
|
|
} else {
|
|
|
|
serverName = ch.Value
|
|
|
|
}
|
|
|
|
return serverName
|
|
|
|
}
|
|
|
|
|
2021-06-03 22:01:43 +00:00
|
|
|
// reverseaddr returns the in-addr.arpa. or ip6.arpa. hostname of the IP
|
|
|
|
// address addr suitable for rDNS (PTR) record lookup or an error if it fails
|
|
|
|
// to parse the IP address.
|
|
|
|
// Implementation taken and adapted from https://golang.org/src/net/dnsclient.go?s=780:834#L20
|
|
|
|
func reverseAddr(ip net.IP) (arpa string) {
|
|
|
|
if ip.To4() != nil {
|
|
|
|
return uitoa(uint(ip[15])) + "." + uitoa(uint(ip[14])) + "." + uitoa(uint(ip[13])) + "." + uitoa(uint(ip[12])) + ".in-addr.arpa."
|
|
|
|
}
|
|
|
|
// Must be IPv6
|
|
|
|
buf := make([]byte, 0, len(ip)*4+len("ip6.arpa."))
|
|
|
|
// Add it, in reverse, to the buffer
|
|
|
|
for i := len(ip) - 1; i >= 0; i-- {
|
|
|
|
v := ip[i]
|
|
|
|
buf = append(buf, hexit[v&0xF],
|
|
|
|
'.',
|
|
|
|
hexit[v>>4],
|
|
|
|
'.')
|
|
|
|
}
|
|
|
|
// Append "ip6.arpa." and return (buf already has the final .)
|
|
|
|
buf = append(buf, "ip6.arpa."...)
|
|
|
|
return string(buf)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert unsigned integer to decimal string.
|
|
|
|
// Implementation taken from https://golang.org/src/net/parse.go
|
|
|
|
func uitoa(val uint) string {
|
|
|
|
if val == 0 { // avoid string allocation
|
|
|
|
return "0"
|
|
|
|
}
|
|
|
|
var buf [20]byte // big enough for 64bit value base 10
|
|
|
|
i := len(buf) - 1
|
|
|
|
for val >= 10 {
|
|
|
|
q := val / 10
|
|
|
|
buf[i] = byte('0' + val - q*10)
|
|
|
|
i--
|
|
|
|
val = q
|
|
|
|
}
|
|
|
|
// val < 10
|
|
|
|
buf[i] = byte('0' + val)
|
|
|
|
return string(buf[i:])
|
|
|
|
}
|
|
|
|
|
|
|
|
const hexit = "0123456789abcdef"
|
|
|
|
|
2021-02-28 01:05:37 +00:00
|
|
|
// KeyAuthorization creates the ACME key authorization value from a token
|
|
|
|
// and a jwk.
|
|
|
|
func KeyAuthorization(token string, jwk *jose.JSONWebKey) (string, error) {
|
|
|
|
thumbprint, err := jwk.Thumbprint(crypto.SHA256)
|
|
|
|
if err != nil {
|
2021-03-05 07:10:46 +00:00
|
|
|
return "", WrapErrorISE(err, "error generating JWK thumbprint")
|
2021-02-28 01:05:37 +00:00
|
|
|
}
|
|
|
|
encPrint := base64.RawURLEncoding.EncodeToString(thumbprint)
|
|
|
|
return fmt.Sprintf("%s.%s", token, encPrint), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// storeError the given error to an ACME error and saves using the DB interface.
|
2021-03-30 05:58:26 +00:00
|
|
|
func storeError(ctx context.Context, db DB, ch *Challenge, markInvalid bool, err *Error) error {
|
2021-03-01 07:33:18 +00:00
|
|
|
ch.Error = err
|
2021-03-30 05:58:26 +00:00
|
|
|
if markInvalid {
|
|
|
|
ch.Status = StatusInvalid
|
|
|
|
}
|
2021-02-28 01:05:37 +00:00
|
|
|
if err := db.UpdateChallenge(ctx, ch); err != nil {
|
2021-03-05 07:10:46 +00:00
|
|
|
return WrapErrorISE(err, "failure saving error to acme challenge")
|
2021-02-28 01:05:37 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|