certificates/acme/challenge.go

1162 lines
39 KiB
Go
Raw Normal View History

2021-02-28 18:09:06 +00:00
package acme
2021-02-28 01:05:37 +00:00
import (
"context"
"crypto"
"crypto/ecdsa"
"crypto/ed25519"
"crypto/elliptic"
"crypto/rsa"
2021-02-28 01:05:37 +00:00
"crypto/sha256"
"crypto/subtle"
"crypto/tls"
"crypto/x509"
"crypto/x509/pkix"
2021-02-28 01:05:37 +00:00
"encoding/asn1"
"encoding/base64"
"encoding/hex"
"encoding/json"
"errors"
2021-02-28 01:05:37 +00:00
"fmt"
"io"
2021-02-28 01:05:37 +00:00
"net"
"net/url"
"reflect"
2022-08-30 02:37:30 +00:00
"strconv"
2021-02-28 01:05:37 +00:00
"strings"
"time"
"github.com/fxamacker/cbor/v2"
"github.com/google/go-attestation/attest"
2022-09-21 12:58:03 +00:00
"github.com/google/go-tpm/tpm2"
"golang.org/x/exp/slices"
2022-09-21 12:58:03 +00:00
2021-02-28 01:05:37 +00:00
"go.step.sm/crypto/jose"
"go.step.sm/crypto/keyutil"
"go.step.sm/crypto/pemutil"
"go.step.sm/crypto/x509util"
2022-09-21 12:58:03 +00:00
"github.com/smallstep/certificates/authority/provisioner"
2021-02-28 01:05:37 +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
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"
)
var (
// InsecurePortHTTP01 is the port used to verify http-01 challenges. If not set it
// defaults to 80.
InsecurePortHTTP01 int
// InsecurePortTLSALPN01 is the port used to verify tls-alpn-01 challenges. If not
// set it defaults to 443.
//
// This variable can be used for testing purposes.
InsecurePortTLSALPN01 int
)
2021-02-28 01:05:37 +00:00
// Challenge represents an ACME response Challenge type.
type Challenge struct {
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
}
2023-01-27 14:36:48 +00:00
// Validate attempts to validate the Challenge. Stores changes to the Challenge
// type using the DB interface. If the Challenge is 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.
if ch.Status != StatusPending {
2021-02-28 01:05:37 +00:00
return nil
}
switch ch.Type {
case HTTP01:
2022-04-29 02:15:18 +00:00
return http01Validate(ctx, ch, db, jwk)
case DNS01:
2022-04-29 02:15:18 +00:00
return dns01Validate(ctx, ch, db, jwk)
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 {
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
// Append insecure port if set.
// Only used for testing purposes.
if InsecurePortHTTP01 != 0 {
u.Host += ":" + strconv.Itoa(InsecurePortHTTP01)
}
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 {
return storeError(ctx, db, ch, false, WrapError(ErrorConnectionType, err,
"error doing http GET for url %s", u))
2021-02-28 01:05:37 +00:00
}
defer resp.Body.Close()
2021-02-28 01:05:37 +00:00
if resp.StatusCode >= 400 {
return storeError(ctx, db, ch, false, NewError(ErrorConnectionType,
"error doing http GET for url %s with status code %d", u, resp.StatusCode))
2021-02-28 01:05:37 +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 "+
"response body for url %s", u)
2021-02-28 01:05:37 +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 {
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
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
}
// 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
}
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{
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,
ServerName: serverName(ch),
2022-09-21 04:01:55 +00:00
InsecureSkipVerify: true, //nolint:gosec // we expect a self-signed challenge certificate
2021-02-28 01:05:37 +00:00
}
var hostPort string
// Allow to change TLS port for testing purposes.
if port := InsecurePortTLSALPN01; port == 0 {
hostPort = net.JoinHostPort(ch.Value, "443")
} else {
hostPort = net.JoinHostPort(ch.Value, strconv.Itoa(port))
}
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 {
// With Go 1.17+ tls.Dial fails if there's no overlap between configured
// 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 {
return storeError(ctx, db, ch, true, NewError(ErrorRejectedIdentifierType,
"cannot negotiate ALPN acme-tls/1 protocol for tls-alpn-01 challenge"))
}
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 {
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
}
if cs.NegotiatedProtocol != "acme-tls/1" {
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]
// if no DNS names present, look for IP address and verify that exactly one exists
if len(leafCert.DNSNames) == 0 {
if len(leafCert.IPAddresses) != 1 || !leafCert.IPAddresses[0].Equal(net.ParseIP(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))
}
} 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-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 {
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) {
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 {
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
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 {
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
}
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 {
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 {
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
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
}
2023-01-27 14:36:48 +00:00
type payloadType struct {
AttObj string `json:"attObj"`
Error string `json:"error"`
}
2023-01-27 14:36:48 +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.
2022-04-02 02:56:05 +00:00
func deviceAttest01Validate(ctx context.Context, ch *Challenge, db DB, jwk *jose.JSONWebKey, payload []byte) error {
// Load authorization to store the key fingerprint.
az, err := db.GetAuthorization(ctx, ch.AuthorizationID)
if err != nil {
return WrapErrorISE(err, "error loading authorization")
}
// Parse payload.
2023-01-27 14:36:48 +00:00
var p payloadType
if err := json.Unmarshal(payload, &p); err != nil {
return WrapErrorISE(err, "error unmarshalling JSON")
}
if p.Error != "" {
return storeError(ctx, db, ch, true, NewError(ErrorRejectedIdentifierType,
"payload contained error: %v", p.Error))
}
attObj, err := base64.RawURLEncoding.DecodeString(p.AttObj)
if err != nil {
return WrapErrorISE(err, "error base64 decoding attObj")
}
2023-01-27 14:36:48 +00:00
att := attestationObject{}
if err := cbor.Unmarshal(attObj, &att); err != nil {
return WrapErrorISE(err, "error unmarshalling CBOR")
}
2022-09-09 00:38:05 +00:00
prov := MustProvisionerFromContext(ctx)
if !prov.IsAttestationFormatEnabled(ctx, provisioner.ACMEAttestationFormat(att.Format)) {
return storeError(ctx, db, ch, true,
NewError(ErrorBadAttestationStatementType, "attestation format %q is not enabled", att.Format))
}
switch att.Format {
case "apple":
data, err := doAppleAttestationFormat(ctx, prov, ch, &att)
if err != nil {
var acmeError *Error
if errors.As(err, &acmeError) {
if acmeError.Status == 500 {
return acmeError
}
return storeError(ctx, db, ch, true, acmeError)
}
2022-09-01 23:18:13 +00:00
return WrapErrorISE(err, "error validating attestation")
}
// Validate nonce with SHA-256 of the token.
if len(data.Nonce) != 0 {
sum := sha256.Sum256([]byte(ch.Token))
if subtle.ConstantTimeCompare(data.Nonce, sum[:]) != 1 {
2022-09-08 17:46:58 +00:00
return storeError(ctx, db, ch, true, NewError(ErrorBadAttestationStatementType, "challenge token does not match"))
}
}
// 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 {
2022-09-08 17:46:58 +00:00
return storeError(ctx, db, ch, true, NewError(ErrorBadAttestationStatementType, "permanent identifier does not match"))
}
// Update attestation key fingerprint to compare against the CSR
az.Fingerprint = data.Fingerprint
2022-08-30 02:37:30 +00:00
case "step":
data, err := doStepAttestationFormat(ctx, prov, ch, jwk, &att)
2022-08-30 02:37:30 +00:00
if err != nil {
var acmeError *Error
if errors.As(err, &acmeError) {
if acmeError.Status == 500 {
return acmeError
}
return storeError(ctx, db, ch, true, acmeError)
}
2022-09-01 23:18:13 +00:00
return WrapErrorISE(err, "error validating attestation")
2022-08-30 02:37:30 +00:00
}
// Validate the YubiKey serial number from the attestation
// certificate with the challenged Order value.
2022-08-30 02:37:30 +00:00
//
// Note: We might want to use an external service for this.
if data.SerialNumber != ch.Value {
subproblem := NewSubproblemWithIdentifier(
ErrorMalformedType,
Identifier{Type: "permanent-identifier", Value: ch.Value},
"challenge identifier %q doesn't match the attested hardware identifier %q", ch.Value, data.SerialNumber,
)
2023-01-26 14:36:15 +00:00
return storeError(ctx, db, ch, true, NewError(ErrorBadAttestationStatementType, "permanent identifier does not match").AddSubproblems(subproblem))
2022-08-30 02:37:30 +00:00
}
// Update attestation key fingerprint to compare against the CSR
az.Fingerprint = data.Fingerprint
case "tpm":
2023-03-31 12:57:25 +00:00
data, err := doTPMAttestationFormat(ctx, prov, ch, jwk, &att)
2022-09-21 12:58:03 +00:00
if err != nil {
// TODO(hs): we should provide more details in the error reported to the client;
// "Attestation statement cannot be verified" is VERY generic. Also holds true for the other formats.
var acmeError *Error
if errors.As(err, &acmeError) {
if acmeError.Status == 500 {
return acmeError
}
return storeError(ctx, db, ch, true, acmeError)
}
return WrapErrorISE(err, "error validating attestation")
2022-09-21 12:58:03 +00:00
}
// TODO(hs): currently this will allow a request for which no PermanentIdentifiers have been
// extracted from the AK certificate. This is currently the case for AK certs from the CLI, as we
// haven't implemented a way for AK certs requested by the CLI to always contain the requested
// PermanentIdentifier. Omitting the check below doesn't allow just any request, as the Order can
// still fail if the challenge value isn't equal to the CSR subject.
if len(data.PermanentIdentifiers) > 0 && !slices.Contains(data.PermanentIdentifiers, ch.Value) { // TODO(hs): add support for HardwareModuleName
subproblem := NewSubproblemWithIdentifier(
ErrorMalformedType,
Identifier{Type: "permanent-identifier", Value: ch.Value},
"challenge identifier %q doesn't match any of the attested hardware identifiers %q", ch.Value, data.PermanentIdentifiers,
)
return storeError(ctx, db, ch, true, NewError(ErrorRejectedIdentifierType, "permanent identifier does not match").AddSubproblems(subproblem))
}
2022-09-21 12:58:03 +00:00
// Update attestation key fingerprint to compare against the CSR
az.Fingerprint = data.Fingerprint
default:
2022-09-08 17:46:58 +00:00
return storeError(ctx, db, ch, true, NewError(ErrorBadAttestationStatementType, "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)
// Store the fingerprint in the authorization.
//
// TODO: add method to update authorization and challenge atomically.
if az.Fingerprint != "" {
if err := db.UpdateAuthorization(ctx, az); err != nil {
return WrapErrorISE(err, "error updating authorization")
}
}
2022-04-02 02:56:05 +00:00
if err := db.UpdateChallenge(ctx, ch); err != nil {
return WrapErrorISE(err, "error updating challenge")
}
return nil
}
var (
oidSubjectAlternativeName = asn1.ObjectIdentifier{2, 5, 29, 17}
)
2022-09-21 12:58:03 +00:00
type tpmAttestationData struct {
Certificate *x509.Certificate
VerifiedChains [][]*x509.Certificate
PermanentIdentifiers []string
Fingerprint string
2022-09-21 12:58:03 +00:00
}
2023-03-31 12:57:25 +00:00
func doTPMAttestationFormat(ctx context.Context, prov Provisioner, ch *Challenge, jwk *jose.JSONWebKey, att *attestationObject) (*tpmAttestationData, error) {
ver, ok := att.AttStatement["ver"].(string)
if !ok {
return nil, NewError(ErrorBadAttestationStatementType, "ver not present")
}
if ver != "2.0" {
return nil, NewError(ErrorBadAttestationStatementType, "version %q is not supported", ver)
}
2022-09-21 12:58:03 +00:00
x5c, ok := att.AttStatement["x5c"].([]interface{})
if !ok {
return nil, NewError(ErrorBadAttestationStatementType, "x5c not present")
2022-09-21 12:58:03 +00:00
}
if len(x5c) == 0 {
return nil, NewError(ErrorBadAttestationStatementType, "x5c is empty")
2022-09-21 12:58:03 +00:00
}
akCertBytes, ok := x5c[0].([]byte)
2022-09-21 12:58:03 +00:00
if !ok {
return nil, NewError(ErrorBadAttestationStatementType, "x5c is malformed")
2022-09-21 12:58:03 +00:00
}
akCert, err := x509.ParseCertificate(akCertBytes)
2022-09-21 12:58:03 +00:00
if err != nil {
return nil, WrapError(ErrorBadAttestationStatementType, err, "x5c is malformed")
2022-09-21 12:58:03 +00:00
}
intermediates := x509.NewCertPool()
if len(x5c[1:]) > 0 {
for _, v := range x5c[1:] {
intCertBytes, vok := v.([]byte)
if !vok {
return nil, NewError(ErrorBadAttestationStatementType, "x5c is malformed")
2022-09-21 12:58:03 +00:00
}
intCert, err := x509.ParseCertificate(intCertBytes)
2022-09-21 12:58:03 +00:00
if err != nil {
return nil, WrapError(ErrorBadAttestationStatementType, err, "x5c is malformed")
2022-09-21 12:58:03 +00:00
}
intermediates.AddCert(intCert)
2022-09-21 12:58:03 +00:00
}
}
// TODO(hs): this can be removed when permanent-identifier/hardware-module-name are handled correctly in
// the stdlib in https://cs.opensource.google/go/go/+/refs/tags/go1.19:src/crypto/x509/parser.go;drc=b5b2cf519fe332891c165077f3723ee74932a647;l=362,
// but I doubt that will happen.
if len(akCert.UnhandledCriticalExtensions) > 0 {
unhandledCriticalExtensions := akCert.UnhandledCriticalExtensions[:0]
for _, extOID := range akCert.UnhandledCriticalExtensions {
2022-09-21 12:58:03 +00:00
switch {
case extOID.Equal(oidSubjectAlternativeName):
// allow Subject Alternative Names, including PermanentIdentifier, HardwareModuleName, TPM attributes, etc
2022-09-21 12:58:03 +00:00
default:
// OIDs that are not in the switch with explicitly allowed OIDs remain unhandled
2022-09-21 12:58:03 +00:00
unhandledCriticalExtensions = append(unhandledCriticalExtensions, extOID)
}
}
akCert.UnhandledCriticalExtensions = unhandledCriticalExtensions
2022-09-21 12:58:03 +00:00
}
roots, ok := prov.GetAttestationRoots()
if !ok {
2023-03-31 12:57:25 +00:00
return nil, NewErrorISE("failed getting TPM attestation root CAs")
2022-09-21 12:58:03 +00:00
}
verifiedChains, err := akCert.Verify(x509.VerifyOptions{
2022-09-21 12:58:03 +00:00
Roots: roots,
Intermediates: intermediates,
CurrentTime: time.Now().Truncate(time.Second),
KeyUsages: []x509.ExtKeyUsage{x509.ExtKeyUsageAny},
})
if err != nil {
return nil, WrapError(ErrorBadAttestationStatementType, err, "x5c is not valid")
2022-09-21 12:58:03 +00:00
}
// TODO(hs): implement revocation check; Verify() doesn't perform CRL check nor OCSP lookup.
// extract and validate Subject Alternative Name extension to contain at least one PermanentIdentifier
var sanExtension pkix.Extension
for _, ext := range akCert.Extensions {
if ext.Id.Equal(oidSubjectAlternativeName) {
sanExtension = ext
break
}
}
if sanExtension.Value == nil {
return nil, NewError(ErrorBadAttestationStatementType, "AK certificate is missing Subject Alternative Name extension")
}
sans, err := parseSANs(sanExtension)
if err != nil {
return nil, WrapError(ErrorBadAttestationStatementType, err, "failed parsing AK certificate SAN extension")
}
var permanentIdentifiers []string
for _, san := range sans {
if san.Type == x509util.PermanentIdentifierType {
permanentIdentifiers = append(permanentIdentifiers, san.Value)
}
}
// extract and validate pubArea, sig, certInfo and alg properties from the request body
pubArea, ok := att.AttStatement["pubArea"].([]byte)
if !ok {
return nil, NewError(ErrorBadAttestationStatementType, "invalid pubArea in attestation statement")
}
if len(pubArea) == 0 {
return nil, NewError(ErrorBadAttestationStatementType, "pubArea is empty")
}
sig, ok := att.AttStatement["sig"].([]byte)
if !ok {
return nil, NewError(ErrorBadAttestationStatementType, "invalid sig in attestation statement")
}
if len(sig) == 0 {
return nil, NewError(ErrorBadAttestationStatementType, "sig is empty")
}
certInfo, ok := att.AttStatement["certInfo"].([]byte)
if !ok {
return nil, NewError(ErrorBadAttestationStatementType, "invalid certInfo in attestation statement")
}
if len(certInfo) == 0 {
return nil, NewError(ErrorBadAttestationStatementType, "certInfo is empty")
}
alg, ok := att.AttStatement["alg"].(int64)
if !ok {
return nil, NewError(ErrorBadAttestationStatementType, "invalid alg in attestation statement")
}
var hash crypto.Hash
switch alg {
case -257: // RS256
hash = crypto.SHA256
case -8: // EdDSA
hash = crypto.Hash(0)
case -7: // ES256
hash = crypto.SHA256
default:
return nil, NewError(ErrorBadAttestationStatementType, "invalid alg %d in attestation statement", alg)
}
2022-09-21 12:58:03 +00:00
// recreate the generated key certification parameter values and verify
// the attested key using the public key of the AK.
certificationParameters := &attest.CertificationParameters{
Public: pubArea, // the public key that was attested
CreateAttestation: certInfo, // the attested properties of the key
CreateSignature: sig, // signature over the attested properties
}
verifyOpts := attest.VerifyOpts{
Public: akCert.PublicKey, // public key of the AK that attested the key
Hash: hash,
}
if err = certificationParameters.Verify(verifyOpts); err != nil {
return nil, WrapError(ErrorBadAttestationStatementType, err, "invalid certification parameters")
2022-09-21 12:58:03 +00:00
}
2023-03-31 12:57:25 +00:00
// decode the "certInfo" data. This won't fail, as it's also done as part of Verify().
tpmCertInfo, err := tpm2.DecodeAttestationData(certInfo)
2022-09-21 12:58:03 +00:00
if err != nil {
return nil, WrapError(ErrorBadAttestationStatementType, err, "failed decoding attestation data")
2022-09-21 12:58:03 +00:00
}
keyAuth, err := KeyAuthorization(ch.Token, jwk)
if err != nil {
return nil, WrapError(ErrorBadAttestationStatementType, err, "failed creating key auth digest")
}
hashedKeyAuth := sha256.Sum256([]byte(keyAuth))
// verify the WebAuthn object contains the expect key authorization digest, which is carried
// within the encoded `certInfo` property of the attestation statement.
if subtle.ConstantTimeCompare(hashedKeyAuth[:], []byte(tpmCertInfo.ExtraData)) == 0 {
return nil, NewError(ErrorBadAttestationStatementType, "key authorization does not match")
}
2023-03-31 12:57:25 +00:00
// decode the (attested) public key and determine its fingerprint. This won't fail, as it's also done as part of Verify().
pub, err := tpm2.DecodePublic(pubArea)
if err != nil {
return nil, WrapError(ErrorBadAttestationStatementType, err, "failed decoding pubArea")
}
publicKey, err := pub.Key()
if err != nil {
return nil, WrapError(ErrorBadAttestationStatementType, err, "failed getting public key")
}
data := &tpmAttestationData{
Certificate: akCert,
VerifiedChains: verifiedChains,
PermanentIdentifiers: permanentIdentifiers,
}
if data.Fingerprint, err = keyutil.Fingerprint(publicKey); err != nil {
return nil, WrapErrorISE(err, "error calculating key fingerprint")
}
// TODO(hs): pass more attestation data, so that that can be used/recorded too?
return data, nil
2022-09-21 12:58:03 +00:00
}
// RFC 4043
//
// https://tools.ietf.org/html/rfc4043
var (
oidPermanentIdentifier = []int{1, 3, 6, 1, 5, 5, 7, 8, 3}
)
// PermanentIdentifier represents an ASN.1 encoded "permanent identifier" as
// defined by RFC4043.
//
// PermanentIdentifier ::= SEQUENCE {
// identifierValue UTF8String OPTIONAL,
// assigner OBJECT IDENTIFIER OPTIONAL
// }
//
// https://datatracker.ietf.org/doc/html/rfc4043
type permanentIdentifier struct {
IdentifierValue string `asn1:"utf8,optional"`
Assigner asn1.ObjectIdentifier `asn1:"optional"`
}
func parsePermanentIdentifier(der []byte) (permanentIdentifier, error) {
var permID permanentIdentifier
if _, err := asn1.UnmarshalWithParams(der, &permID, "explicit,tag:0"); err != nil {
return permanentIdentifier{}, err
}
return permID, nil
}
func parseSANs(ext pkix.Extension) (sans []x509util.SubjectAlternativeName, err error) {
_, otherNames, err := parseSubjectAltName(ext)
if err != nil {
return nil, fmt.Errorf("parseSubjectAltName: %w", err)
}
for _, otherName := range otherNames {
if otherName.TypeID.Equal(oidPermanentIdentifier) {
permID, err := parsePermanentIdentifier(otherName.Value.FullBytes)
if err != nil {
return nil, fmt.Errorf("parsePermanentIdentifier: %w", err)
}
permanentIdentifier := x509util.SubjectAlternativeName{
Type: x509util.PermanentIdentifierType,
Value: permID.IdentifierValue, // TODO(hs): change how these are returned
}
sans = append(sans, permanentIdentifier)
}
}
return
}
// OtherName ::= SEQUENCE {
// type-id OBJECT IDENTIFIER,
// value [0] EXPLICIT ANY DEFINED BY type-id }
type otherName struct {
TypeID asn1.ObjectIdentifier
Value asn1.RawValue
}
// https://datatracker.ietf.org/doc/html/rfc5280#page-35
func parseSubjectAltName(ext pkix.Extension) (dirNames []pkix.Name, otherNames []otherName, err error) {
err = forEachSAN(ext.Value, func(generalName asn1.RawValue) error {
switch generalName.Tag {
case 0: // otherName
2023-03-31 15:41:43 +00:00
var on otherName
if _, err := asn1.UnmarshalWithParams(generalName.FullBytes, &on, "tag:0"); err != nil {
return fmt.Errorf("OtherName: asn1.UnmarshalWithParams: %w", err)
}
2023-03-31 15:41:43 +00:00
otherNames = append(otherNames, on)
case 4: // directoryName
var rdns pkix.RDNSequence
if _, err := asn1.Unmarshal(generalName.Bytes, &rdns); err != nil {
2023-03-31 15:41:43 +00:00
return fmt.Errorf("DirectoryName: asn1.Unmarshal: %w", err)
}
var dirName pkix.Name
dirName.FillFromRDNSequence(&rdns)
dirNames = append(dirNames, dirName)
default:
//return fmt.Errorf("expected tag %d", generalName.Tag)
// TODO(hs): implement the others ... skipping for now
}
return nil
})
return
}
// Borrowed from the x509 package.
func forEachSAN(extension []byte, callback func(ext asn1.RawValue) error) error {
var seq asn1.RawValue
rest, err := asn1.Unmarshal(extension, &seq)
if err != nil {
return err
} else if len(rest) != 0 {
return errors.New("x509: trailing data after X.509 extension")
}
if !seq.IsCompound || seq.Tag != 16 || seq.Class != 0 {
return asn1.StructuralError{Msg: "bad SAN sequence"}
}
rest = seq.Bytes
for len(rest) > 0 {
var v asn1.RawValue
rest, err = asn1.Unmarshal(rest, &v)
if err != nil {
return err
}
if err := callback(v); err != nil {
return err
}
}
return nil
}
// 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
type appleAttestationData struct {
Nonce []byte
SerialNumber string
UDID string
SEPVersion string
Certificate *x509.Certificate
Fingerprint string
}
2022-06-07 04:09:31 +00:00
2023-01-27 14:36:48 +00:00
func doAppleAttestationFormat(ctx context.Context, prov Provisioner, ch *Challenge, att *attestationObject) (*appleAttestationData, error) {
// Use configured or default attestation roots if none is configured.
roots, ok := prov.GetAttestationRoots()
if !ok {
root, err := pemutil.ParseCertificate([]byte(appleEnterpriseAttestationRootCA))
if err != nil {
return nil, WrapErrorISE(err, "error parsing apple enterprise ca")
}
roots = x509.NewCertPool()
roots.AddCert(root)
2022-06-07 04:09:31 +00:00
}
x5c, ok := att.AttStatement["x5c"].([]interface{})
if !ok {
2022-09-08 17:46:58 +00:00
return nil, NewError(ErrorBadAttestationStatementType, "x5c not present")
2022-06-07 04:09:31 +00:00
}
2022-06-22 19:43:24 +00:00
if len(x5c) == 0 {
return nil, NewError(ErrorRejectedIdentifierType, "x5c is empty")
2022-06-22 19:43:24 +00:00
}
der, ok := x5c[0].([]byte)
if !ok {
2022-09-08 17:46:58 +00:00
return nil, NewError(ErrorBadAttestationStatementType, "x5c is malformed")
2022-06-07 04:09:31 +00:00
}
leaf, err := x509.ParseCertificate(der)
2022-06-07 04:09:31 +00:00
if err != nil {
2022-09-08 17:46:58 +00:00
return nil, WrapError(ErrorBadAttestationStatementType, err, "x5c is malformed")
2022-06-07 04:09:31 +00:00
}
intermediates := x509.NewCertPool()
for _, v := range x5c[1:] {
der, ok = v.([]byte)
if !ok {
2022-09-08 17:46:58 +00:00
return nil, NewError(ErrorBadAttestationStatementType, "x5c is malformed")
}
cert, err := x509.ParseCertificate(der)
if err != nil {
2022-09-08 17:46:58 +00:00
return nil, WrapError(ErrorBadAttestationStatementType, 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 {
2022-09-08 17:46:58 +00:00
return nil, WrapError(ErrorBadAttestationStatementType, err, "x5c is not valid")
}
data := &appleAttestationData{
Certificate: leaf,
}
if data.Fingerprint, err = keyutil.Fingerprint(leaf.PublicKey); err != nil {
return nil, WrapErrorISE(err, "error calculating key fingerprint")
}
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):
data.Nonce = ext.Value
}
2022-06-07 04:09:31 +00:00
}
return data, nil
2022-06-07 04:09:31 +00:00
}
2022-08-30 02:37:30 +00:00
// Yubico PIV Root CA Serial 263751
// https://developers.yubico.com/PIV/Introduction/piv-attestation-ca.pem
const yubicoPIVRootCA = `-----BEGIN CERTIFICATE-----
MIIDFzCCAf+gAwIBAgIDBAZHMA0GCSqGSIb3DQEBCwUAMCsxKTAnBgNVBAMMIFl1
YmljbyBQSVYgUm9vdCBDQSBTZXJpYWwgMjYzNzUxMCAXDTE2MDMxNDAwMDAwMFoY
DzIwNTIwNDE3MDAwMDAwWjArMSkwJwYDVQQDDCBZdWJpY28gUElWIFJvb3QgQ0Eg
U2VyaWFsIDI2Mzc1MTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAMN2
cMTNR6YCdcTFRxuPy31PabRn5m6pJ+nSE0HRWpoaM8fc8wHC+Tmb98jmNvhWNE2E
ilU85uYKfEFP9d6Q2GmytqBnxZsAa3KqZiCCx2LwQ4iYEOb1llgotVr/whEpdVOq
joU0P5e1j1y7OfwOvky/+AXIN/9Xp0VFlYRk2tQ9GcdYKDmqU+db9iKwpAzid4oH
BVLIhmD3pvkWaRA2H3DA9t7H/HNq5v3OiO1jyLZeKqZoMbPObrxqDg+9fOdShzgf
wCqgT3XVmTeiwvBSTctyi9mHQfYd2DwkaqxRnLbNVyK9zl+DzjSGp9IhVPiVtGet
X02dxhQnGS7K6BO0Qe8CAwEAAaNCMEAwHQYDVR0OBBYEFMpfyvLEojGc6SJf8ez0
1d8Cv4O/MA8GA1UdEwQIMAYBAf8CAQEwDgYDVR0PAQH/BAQDAgEGMA0GCSqGSIb3
DQEBCwUAA4IBAQBc7Ih8Bc1fkC+FyN1fhjWioBCMr3vjneh7MLbA6kSoyWF70N3s
XhbXvT4eRh0hvxqvMZNjPU/VlRn6gLVtoEikDLrYFXN6Hh6Wmyy1GTnspnOvMvz2
lLKuym9KYdYLDgnj3BeAvzIhVzzYSeU77/Cupofj093OuAswW0jYvXsGTyix6B3d
bW5yWvyS9zNXaqGaUmP3U9/b6DlHdDogMLu3VLpBB9bm5bjaKWWJYgWltCVgUbFq
Fqyi4+JE014cSgR57Jcu3dZiehB6UtAPgad9L5cNvua/IWRmm+ANy3O2LH++Pyl8
SREzU8onbBsjMg9QDiSf5oJLKvd/Ren+zGY7
-----END CERTIFICATE-----`
// Serial number of the YubiKey, encoded as an integer.
// https://developers.yubico.com/PIV/Introduction/PIV_attestation.html
var oidYubicoSerialNumber = asn1.ObjectIdentifier{1, 3, 6, 1, 4, 1, 41482, 3, 7}
type stepAttestationData struct {
Certificate *x509.Certificate
SerialNumber string
Fingerprint string
2022-08-30 02:37:30 +00:00
}
2023-01-27 14:36:48 +00:00
func doStepAttestationFormat(ctx context.Context, prov Provisioner, ch *Challenge, jwk *jose.JSONWebKey, att *attestationObject) (*stepAttestationData, error) {
// Use configured or default attestation roots if none is configured.
roots, ok := prov.GetAttestationRoots()
if !ok {
root, err := pemutil.ParseCertificate([]byte(yubicoPIVRootCA))
if err != nil {
return nil, WrapErrorISE(err, "error parsing root ca")
}
roots = x509.NewCertPool()
roots.AddCert(root)
2022-08-30 02:37:30 +00:00
}
// Extract x5c and verify certificate
2022-08-30 02:37:30 +00:00
x5c, ok := att.AttStatement["x5c"].([]interface{})
if !ok {
2022-09-08 17:46:58 +00:00
return nil, NewError(ErrorBadAttestationStatementType, "x5c not present")
2022-08-30 02:37:30 +00:00
}
if len(x5c) == 0 {
return nil, NewError(ErrorRejectedIdentifierType, "x5c is empty")
2022-08-30 02:37:30 +00:00
}
der, ok := x5c[0].([]byte)
if !ok {
2022-09-08 17:46:58 +00:00
return nil, NewError(ErrorBadAttestationStatementType, "x5c is malformed")
2022-08-30 02:37:30 +00:00
}
leaf, err := x509.ParseCertificate(der)
if err != nil {
2022-09-08 17:46:58 +00:00
return nil, WrapError(ErrorBadAttestationStatementType, err, "x5c is malformed")
2022-08-30 02:37:30 +00:00
}
intermediates := x509.NewCertPool()
for _, v := range x5c[1:] {
der, ok = v.([]byte)
if !ok {
2022-09-08 17:46:58 +00:00
return nil, NewError(ErrorBadAttestationStatementType, "x5c is malformed")
2022-08-30 02:37:30 +00:00
}
cert, err := x509.ParseCertificate(der)
if err != nil {
2022-09-08 17:46:58 +00:00
return nil, WrapError(ErrorBadAttestationStatementType, err, "x5c is malformed")
2022-08-30 02:37:30 +00:00
}
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 {
2022-09-08 17:46:58 +00:00
return nil, WrapError(ErrorBadAttestationStatementType, err, "x5c is not valid")
2022-08-30 02:37:30 +00:00
}
// Verify proof of possession of private key validating the key
// authorization. Per recommendation at
// https://w3c.github.io/webauthn/#sctn-signature-attestation-types the
// signature is CBOR-encoded.
var sig []byte
csig, ok := att.AttStatement["sig"].([]byte)
if !ok {
2022-09-08 17:46:58 +00:00
return nil, NewError(ErrorBadAttestationStatementType, "sig not present")
}
if err := cbor.Unmarshal(csig, &sig); err != nil {
2022-09-08 17:46:58 +00:00
return nil, NewError(ErrorBadAttestationStatementType, "sig is malformed")
}
keyAuth, err := KeyAuthorization(ch.Token, jwk)
if err != nil {
return nil, err
}
switch pub := leaf.PublicKey.(type) {
case *ecdsa.PublicKey:
if pub.Curve != elliptic.P256() {
2022-09-08 17:46:58 +00:00
return nil, WrapError(ErrorBadAttestationStatementType, err, "unsupported elliptic curve %s", pub.Curve)
}
sum := sha256.Sum256([]byte(keyAuth))
if !ecdsa.VerifyASN1(pub, sum[:], sig) {
2022-09-08 17:46:58 +00:00
return nil, NewError(ErrorBadAttestationStatementType, "failed to validate signature")
}
case *rsa.PublicKey:
sum := sha256.Sum256([]byte(keyAuth))
if err := rsa.VerifyPKCS1v15(pub, crypto.SHA256, sum[:], sig); err != nil {
2022-09-08 17:46:58 +00:00
return nil, NewError(ErrorBadAttestationStatementType, "failed to validate signature")
}
case ed25519.PublicKey:
if !ed25519.Verify(pub, []byte(keyAuth), sig) {
2022-09-08 17:46:58 +00:00
return nil, NewError(ErrorBadAttestationStatementType, "failed to validate signature")
}
default:
2022-09-08 17:46:58 +00:00
return nil, NewError(ErrorBadAttestationStatementType, "unsupported public key type %T", pub)
}
2022-09-01 23:18:13 +00:00
// Parse attestation data:
// TODO(mariano): add support for other extensions.
2022-08-30 02:37:30 +00:00
data := &stepAttestationData{
Certificate: leaf,
}
if data.Fingerprint, err = keyutil.Fingerprint(leaf.PublicKey); err != nil {
return nil, WrapErrorISE(err, "error calculating key fingerprint")
}
2022-08-30 02:37:30 +00:00
for _, ext := range leaf.Extensions {
2022-09-01 23:18:13 +00:00
if !ext.Id.Equal(oidYubicoSerialNumber) {
continue
}
var serialNumber int
rest, err := asn1.Unmarshal(ext.Value, &serialNumber)
if err != nil || len(rest) > 0 {
2022-09-08 17:46:58 +00:00
return nil, WrapError(ErrorBadAttestationStatementType, err, "error parsing serial number")
2022-08-30 02:37:30 +00:00
}
2022-09-01 23:18:13 +00:00
data.SerialNumber = strconv.Itoa(serialNumber)
break
2022-08-30 02:37:30 +00:00
}
return data, nil
}
// 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
// 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 {
2022-09-21 12:58:03 +00:00
v := val / 10
buf[i] = byte('0' + val - v*10)
2021-06-03 22:01:43 +00:00
i--
2022-09-21 12:58:03 +00:00
val = v
2021-06-03 22:01:43 +00:00
}
// 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.
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
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
}