|
|
@@ -1,7 +1,6 @@
|
|
|
package acme
|
|
|
|
|
|
import (
|
|
|
- "bytes"
|
|
|
"context"
|
|
|
"crypto"
|
|
|
"crypto/ecdsa"
|
|
|
@@ -21,13 +20,12 @@ import (
|
|
|
"io"
|
|
|
"net"
|
|
|
"net/url"
|
|
|
- "os"
|
|
|
- "os/exec"
|
|
|
"reflect"
|
|
|
"strconv"
|
|
|
"strings"
|
|
|
"time"
|
|
|
|
|
|
+ "github.com/coreos/go-oidc/v3/oidc"
|
|
|
"github.com/fxamacker/cbor/v2"
|
|
|
"github.com/google/go-tpm/legacy/tpm2"
|
|
|
"github.com/smallstep/go-attestation/attest"
|
|
|
@@ -39,6 +37,7 @@ import (
|
|
|
|
|
|
"github.com/smallstep/certificates/acme/wire"
|
|
|
"github.com/smallstep/certificates/authority/provisioner"
|
|
|
+ wireprovisioner "github.com/smallstep/certificates/authority/provisioner/wire"
|
|
|
)
|
|
|
|
|
|
type ChallengeType string
|
|
|
@@ -116,7 +115,7 @@ func (ch *Challenge) Validate(ctx context.Context, db DB, jwk *jose.JSONWebKey,
|
|
|
case WIREDPOP01:
|
|
|
return wireDPOP01Validate(ctx, ch, db, jwk, payload)
|
|
|
default:
|
|
|
- return NewErrorISE("unexpected challenge type '%s'", ch.Type)
|
|
|
+ return NewErrorISE("unexpected challenge type %q", ch.Type)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -353,63 +352,78 @@ func dns01Validate(ctx context.Context, ch *Challenge, db DB, jwk *jose.JSONWebK
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
-type WireChallengePayload struct {
|
|
|
- // IDToken
|
|
|
- IDToken string `json:"id_token,omitempty"`
|
|
|
- // KeyAuth ({challenge-token}.{jwk-thumbprint})
|
|
|
- KeyAuth string `json:"keyauth,omitempty"`
|
|
|
- // AccessToken is the token generated by wire-server
|
|
|
- AccessToken string `json:"access_token,omitempty"`
|
|
|
+type wireOidcPayload struct {
|
|
|
+ // IDToken contains the OIDC identity token
|
|
|
+ IDToken string `json:"id_token"`
|
|
|
}
|
|
|
|
|
|
func wireOIDC01Validate(ctx context.Context, ch *Challenge, db DB, jwk *jose.JSONWebKey, payload []byte) error {
|
|
|
prov, ok := ProvisionerFromContext(ctx)
|
|
|
if !ok {
|
|
|
- return NewErrorISE("no provisioner provided")
|
|
|
+ return NewErrorISE("missing provisioner")
|
|
|
+ }
|
|
|
+ linker, ok := LinkerFromContext(ctx)
|
|
|
+ if !ok {
|
|
|
+ return NewErrorISE("missing linker")
|
|
|
}
|
|
|
|
|
|
- var wireChallengePayload WireChallengePayload
|
|
|
- err := json.Unmarshal(payload, &wireChallengePayload)
|
|
|
+ var oidcPayload wireOidcPayload
|
|
|
+ err := json.Unmarshal(payload, &oidcPayload)
|
|
|
if err != nil {
|
|
|
- return storeError(ctx, db, ch, false, WrapError(ErrorRejectedIdentifierType, err,
|
|
|
- "error unmarshalling Wire challenge payload"))
|
|
|
+ return WrapError(ErrorMalformedType, err, "error unmarshalling Wire OIDC challenge payload")
|
|
|
}
|
|
|
|
|
|
- oidcOptions := prov.GetOptions().GetOIDCOptions()
|
|
|
- idToken, err := oidcOptions.GetProvider(ctx).Verifier(oidcOptions.GetConfig()).Verify(ctx, wireChallengePayload.IDToken)
|
|
|
+ wireID, err := wire.ParseID([]byte(ch.Value))
|
|
|
if err != nil {
|
|
|
- return storeError(ctx, db, ch, false, WrapError(ErrorRejectedIdentifierType, err,
|
|
|
- "error verifying ID token signature"))
|
|
|
+ return WrapErrorISE(err, "error unmarshalling challenge data")
|
|
|
}
|
|
|
|
|
|
- var claims struct {
|
|
|
- Name string `json:"preferred_username,omitempty"`
|
|
|
- Handle string `json:"name"`
|
|
|
- Issuer string `json:"iss,omitempty"`
|
|
|
- GivenName string `json:"given_name,omitempty"`
|
|
|
- KeyAuth string `json:"keyauth"` // TODO(hs): use this property instead of the one in the payload after https://github.com/wireapp/rusty-jwt-tools/tree/fix/keyauth is done
|
|
|
- }
|
|
|
- if err = idToken.Claims(&claims); err != nil {
|
|
|
- return storeError(ctx, db, ch, false, WrapError(ErrorRejectedIdentifierType, err,
|
|
|
- "error retrieving claims from ID token"))
|
|
|
+ wireOptions, err := prov.GetOptions().GetWireOptions()
|
|
|
+ if err != nil {
|
|
|
+ return WrapErrorISE(err, "failed getting Wire options")
|
|
|
}
|
|
|
|
|
|
- challengeValues, err := wire.ParseID([]byte(ch.Value))
|
|
|
+ oidcOptions := wireOptions.GetOIDCOptions()
|
|
|
+ verifier := oidcOptions.GetProvider(ctx).Verifier(oidcOptions.GetConfig())
|
|
|
+ idToken, err := verifier.Verify(ctx, oidcPayload.IDToken)
|
|
|
if err != nil {
|
|
|
- return WrapErrorISE(err, "error unmarshalling challenge data")
|
|
|
+ return storeError(ctx, db, ch, true, WrapError(ErrorRejectedIdentifierType, err,
|
|
|
+ "error verifying ID token signature"))
|
|
|
+ }
|
|
|
+
|
|
|
+ var claims struct {
|
|
|
+ Name string `json:"preferred_username,omitempty"`
|
|
|
+ Handle string `json:"name"`
|
|
|
+ Issuer string `json:"iss,omitempty"`
|
|
|
+ GivenName string `json:"given_name,omitempty"`
|
|
|
+ KeyAuth string `json:"keyauth"`
|
|
|
+ ACMEAudience string `json:"acme_aud,omitempty"`
|
|
|
+ }
|
|
|
+ if err := idToken.Claims(&claims); err != nil {
|
|
|
+ return storeError(ctx, db, ch, true, WrapError(ErrorRejectedIdentifierType, err,
|
|
|
+ "error retrieving claims from ID token"))
|
|
|
}
|
|
|
|
|
|
+ // TODO(hs): move this into validation below?
|
|
|
expectedKeyAuth, err := KeyAuthorization(ch.Token, jwk)
|
|
|
if err != nil {
|
|
|
- return err
|
|
|
+ return WrapErrorISE(err, "error determining key authorization")
|
|
|
}
|
|
|
- if expectedKeyAuth != wireChallengePayload.KeyAuth {
|
|
|
+ if expectedKeyAuth != claims.KeyAuth {
|
|
|
return storeError(ctx, db, ch, true, NewError(ErrorRejectedIdentifierType,
|
|
|
- "keyAuthorization does not match; expected %s, but got %s", expectedKeyAuth, wireChallengePayload.KeyAuth))
|
|
|
+ "keyAuthorization does not match; expected %q, but got %q", expectedKeyAuth, claims.KeyAuth))
|
|
|
}
|
|
|
|
|
|
- if challengeValues.Name != claims.Name || challengeValues.Handle != claims.Handle {
|
|
|
- return storeError(ctx, db, ch, false, NewError(ErrorRejectedIdentifierType, "OIDC claims don't match"))
|
|
|
+ // audience is the full URL to the challenge
|
|
|
+ acmeAudience := linker.GetLink(ctx, ChallengeLinkType, ch.AuthorizationID, ch.ID)
|
|
|
+ if claims.ACMEAudience != acmeAudience {
|
|
|
+ return storeError(ctx, db, ch, true, NewError(ErrorRejectedIdentifierType,
|
|
|
+ "invalid 'acme_aud' %q", claims.ACMEAudience))
|
|
|
+ }
|
|
|
+
|
|
|
+ transformedIDToken, err := validateWireOIDCClaims(oidcOptions, idToken, wireID)
|
|
|
+ if err != nil {
|
|
|
+ return storeError(ctx, db, ch, true, WrapError(ErrorRejectedIdentifierType, err, "claims in OIDC ID token don't match"))
|
|
|
}
|
|
|
|
|
|
// Update and store the challenge.
|
|
|
@@ -421,133 +435,110 @@ func wireOIDC01Validate(ctx context.Context, ch *Challenge, db DB, jwk *jose.JSO
|
|
|
return WrapErrorISE(err, "error updating challenge")
|
|
|
}
|
|
|
|
|
|
- parsedIDToken, err := jose.ParseSigned(wireChallengePayload.IDToken)
|
|
|
- if err != nil {
|
|
|
- return WrapErrorISE(err, "invalid OIDC ID token")
|
|
|
- }
|
|
|
- oidcToken := make(map[string]interface{})
|
|
|
- if err := parsedIDToken.UnsafeClaimsWithoutVerification(&oidcToken); err != nil {
|
|
|
- return WrapErrorISE(err, "failed parsing OIDC id token")
|
|
|
- }
|
|
|
-
|
|
|
orders, err := db.GetAllOrdersByAccountID(ctx, ch.AccountID)
|
|
|
if err != nil {
|
|
|
- return WrapErrorISE(err, "could not find current order by account id")
|
|
|
+ return WrapErrorISE(err, "could not retrieve current order by account id")
|
|
|
}
|
|
|
if len(orders) == 0 {
|
|
|
return NewErrorISE("there are not enough orders for this account for this custom OIDC challenge")
|
|
|
}
|
|
|
|
|
|
order := orders[len(orders)-1]
|
|
|
- if err := db.CreateOidcToken(ctx, order, oidcToken); err != nil {
|
|
|
+ if err := db.CreateOidcToken(ctx, order, transformedIDToken); err != nil {
|
|
|
return WrapErrorISE(err, "failed storing OIDC id token")
|
|
|
}
|
|
|
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
-func wireDPOP01Validate(ctx context.Context, ch *Challenge, db DB, jwk *jose.JSONWebKey, payload []byte) error {
|
|
|
- prov, ok := ProvisionerFromContext(ctx)
|
|
|
- if !ok {
|
|
|
- return NewErrorISE("missing provisioner")
|
|
|
+func validateWireOIDCClaims(o *wireprovisioner.OIDCOptions, token *oidc.IDToken, wireID wire.ID) (map[string]any, error) {
|
|
|
+ var m map[string]any
|
|
|
+ if err := token.Claims(&m); err != nil {
|
|
|
+ return nil, fmt.Errorf("failed extracting OIDC ID token claims: %w", err)
|
|
|
}
|
|
|
-
|
|
|
- rawKid, err := jwk.Thumbprint(crypto.SHA256)
|
|
|
+ transformed, err := o.Transform(m)
|
|
|
if err != nil {
|
|
|
- return storeError(ctx, db, ch, false, WrapError(ErrorServerInternalType, err, "failed to compute JWK thumbprint"))
|
|
|
+ return nil, fmt.Errorf("failed transforming OIDC ID token: %w", err)
|
|
|
}
|
|
|
- kid := base64.RawURLEncoding.EncodeToString(rawKid)
|
|
|
|
|
|
- dpopOptions := prov.GetOptions().GetDPOPOptions()
|
|
|
- key := dpopOptions.GetSigningKey()
|
|
|
-
|
|
|
- var wireChallengePayload WireChallengePayload
|
|
|
- if err := json.Unmarshal(payload, &wireChallengePayload); err != nil {
|
|
|
- return storeError(ctx, db, ch, false, WrapError(ErrorRejectedIdentifierType, err,
|
|
|
- "error unmarshalling Wire challenge payload"))
|
|
|
+ name, ok := transformed["name"]
|
|
|
+ if !ok {
|
|
|
+ return nil, fmt.Errorf("transformed OIDC ID token does not contain 'name'")
|
|
|
+ }
|
|
|
+ if wireID.Name != name {
|
|
|
+ return nil, fmt.Errorf("invalid 'name' %q after transformation", name)
|
|
|
}
|
|
|
|
|
|
- file, err := os.CreateTemp(os.TempDir(), "acme-validate-challenge-pubkey-")
|
|
|
- if err != nil {
|
|
|
- return WrapErrorISE(err, "temporary file could not be created")
|
|
|
+ preferredUsername, ok := transformed["preferred_username"]
|
|
|
+ if !ok {
|
|
|
+ return nil, fmt.Errorf("transformed OIDC ID token does not contain 'preferred_username'")
|
|
|
+ }
|
|
|
+ if wireID.Handle != preferredUsername {
|
|
|
+ return nil, fmt.Errorf("invalid 'preferred_username' %q after transformation", preferredUsername)
|
|
|
}
|
|
|
- defer file.Close()
|
|
|
- defer os.Remove(file.Name())
|
|
|
|
|
|
- buf := bytes.NewBuffer(nil)
|
|
|
- buf.WriteString(key)
|
|
|
+ return transformed, nil
|
|
|
+}
|
|
|
|
|
|
- n, err := file.Write(buf.Bytes())
|
|
|
- if err != nil {
|
|
|
- return WrapErrorISE(err, "failed writing signature key to temp file")
|
|
|
+type wireDpopPayload struct {
|
|
|
+ // AccessToken is the token generated by wire-server
|
|
|
+ AccessToken string `json:"access_token"`
|
|
|
+}
|
|
|
+
|
|
|
+func wireDPOP01Validate(ctx context.Context, ch *Challenge, db DB, accountJWK *jose.JSONWebKey, payload []byte) error {
|
|
|
+ prov, ok := ProvisionerFromContext(ctx)
|
|
|
+ if !ok {
|
|
|
+ return NewErrorISE("missing provisioner")
|
|
|
}
|
|
|
- if n != buf.Len() {
|
|
|
- return WrapErrorISE(err, "expected to write %d characters to the key file, got %d", buf.Len(), n)
|
|
|
+ linker, ok := LinkerFromContext(ctx)
|
|
|
+ if !ok {
|
|
|
+ return NewErrorISE("missing linker")
|
|
|
}
|
|
|
|
|
|
- challengeValues, err := wire.ParseID([]byte(ch.Value))
|
|
|
- if err != nil {
|
|
|
- return WrapErrorISE(err, "error unmarshalling challenge data")
|
|
|
+ var dpopPayload wireDpopPayload
|
|
|
+ if err := json.Unmarshal(payload, &dpopPayload); err != nil {
|
|
|
+ return WrapError(ErrorMalformedType, err, "error unmarshalling Wire DPoP challenge payload")
|
|
|
}
|
|
|
|
|
|
- clientID, err := wire.ParseClientID(challengeValues.ClientID)
|
|
|
+ wireID, err := wire.ParseID([]byte(ch.Value))
|
|
|
if err != nil {
|
|
|
- return WrapErrorISE(err, "error parsing device id")
|
|
|
+ return WrapErrorISE(err, "error unmarshalling challenge data")
|
|
|
}
|
|
|
|
|
|
- issuer, err := dpopOptions.GetTarget(clientID.DeviceID)
|
|
|
+ clientID, err := wire.ParseClientID(wireID.ClientID)
|
|
|
if err != nil {
|
|
|
- return WrapErrorISE(err, "invalid Go template registered for 'target'")
|
|
|
+ return WrapErrorISE(err, "error parsing device id")
|
|
|
}
|
|
|
|
|
|
- expiry := strconv.FormatInt(time.Now().Add(time.Hour*24*365).Unix(), 10)
|
|
|
- cmd := exec.CommandContext( //nolint:gosec // TODO(hs): replace this with Go implementation
|
|
|
- ctx,
|
|
|
- dpopOptions.GetValidationExecPath(),
|
|
|
- "verify-access",
|
|
|
- "--client-id",
|
|
|
- challengeValues.ClientID,
|
|
|
- "--handle",
|
|
|
- challengeValues.Handle,
|
|
|
- "--challenge",
|
|
|
- ch.Token,
|
|
|
- "--leeway",
|
|
|
- "360",
|
|
|
- "--max-expiry",
|
|
|
- expiry,
|
|
|
- "--issuer",
|
|
|
- issuer,
|
|
|
- "--hash-algorithm",
|
|
|
- `SHA-256`,
|
|
|
- "--kid",
|
|
|
- kid,
|
|
|
- "--key",
|
|
|
- file.Name(),
|
|
|
- )
|
|
|
-
|
|
|
- stdin, err := cmd.StdinPipe()
|
|
|
+ wireOptions, err := prov.GetOptions().GetWireOptions()
|
|
|
if err != nil {
|
|
|
- return WrapErrorISE(err, "error getting process stdin")
|
|
|
+ return WrapErrorISE(err, "failed getting Wire options")
|
|
|
}
|
|
|
|
|
|
- err = cmd.Start()
|
|
|
+ dpopOptions := wireOptions.GetDPOPOptions()
|
|
|
+ issuer, err := dpopOptions.EvaluateTarget(clientID.DeviceID)
|
|
|
if err != nil {
|
|
|
- return WrapErrorISE(err, "error starting validation process")
|
|
|
+ return WrapErrorISE(err, "invalid Go template registered for 'target'")
|
|
|
}
|
|
|
|
|
|
- _, err = stdin.Write([]byte(wireChallengePayload.AccessToken))
|
|
|
- if err != nil {
|
|
|
- return WrapErrorISE(err, "error writing to stdin")
|
|
|
- }
|
|
|
+ // audience is the full URL to the challenge
|
|
|
+ audience := linker.GetLink(ctx, ChallengeLinkType, ch.AuthorizationID, ch.ID)
|
|
|
|
|
|
- err = stdin.Close()
|
|
|
- if err != nil {
|
|
|
- return WrapErrorISE(err, "error closing stdin")
|
|
|
+ params := wireVerifyParams{
|
|
|
+ token: dpopPayload.AccessToken,
|
|
|
+ tokenKey: dpopOptions.GetSigningKey(),
|
|
|
+ dpopKey: accountJWK.Public(),
|
|
|
+ dpopKeyID: accountJWK.KeyID,
|
|
|
+ issuer: issuer,
|
|
|
+ audience: audience,
|
|
|
+ wireID: wireID,
|
|
|
+ chToken: ch.Token,
|
|
|
+ t: clock.Now().UTC(),
|
|
|
}
|
|
|
-
|
|
|
- err = cmd.Wait()
|
|
|
+ _, dpop, err := parseAndVerifyWireAccessToken(params)
|
|
|
if err != nil {
|
|
|
- return storeError(ctx, db, ch, true, NewError(ErrorRejectedIdentifierType, "error finishing validation: %s", err))
|
|
|
+ return storeError(ctx, db, ch, true, WrapError(ErrorRejectedIdentifierType, err,
|
|
|
+ "failed validating Wire access token"))
|
|
|
}
|
|
|
|
|
|
// Update and store the challenge.
|
|
|
@@ -559,45 +550,178 @@ func wireDPOP01Validate(ctx context.Context, ch *Challenge, db DB, jwk *jose.JSO
|
|
|
return WrapErrorISE(err, "error updating challenge")
|
|
|
}
|
|
|
|
|
|
- parsedAccessToken, err := jose.ParseSigned(wireChallengePayload.AccessToken)
|
|
|
+ orders, err := db.GetAllOrdersByAccountID(ctx, ch.AccountID)
|
|
|
if err != nil {
|
|
|
- return WrapErrorISE(err, "invalid access token")
|
|
|
+ return WrapErrorISE(err, "could not find current order by account id")
|
|
|
}
|
|
|
- access := make(map[string]interface{})
|
|
|
- if err := parsedAccessToken.UnsafeClaimsWithoutVerification(&access); err != nil {
|
|
|
- return WrapErrorISE(err, "failed parsing access token")
|
|
|
+ if len(orders) == 0 {
|
|
|
+ return NewErrorISE("there are not enough orders for this account for this custom OIDC challenge")
|
|
|
}
|
|
|
|
|
|
- rawDpop, ok := access["proof"].(string)
|
|
|
- if !ok {
|
|
|
- return WrapErrorISE(err, "invalid dpop proof format in access token")
|
|
|
+ order := orders[len(orders)-1]
|
|
|
+ if err := db.CreateDpopToken(ctx, order, map[string]any(*dpop)); err != nil {
|
|
|
+ return WrapErrorISE(err, "failed storing DPoP token")
|
|
|
}
|
|
|
|
|
|
- parsedDpopToken, err := jose.ParseSigned(rawDpop)
|
|
|
+ return nil
|
|
|
+}
|
|
|
+
|
|
|
+type wireCnf struct {
|
|
|
+ Kid string `json:"kid"`
|
|
|
+}
|
|
|
+
|
|
|
+type wireAccessToken struct {
|
|
|
+ jose.Claims
|
|
|
+ Challenge string `json:"chal"`
|
|
|
+ Nonce string `json:"nonce"`
|
|
|
+ Cnf wireCnf `json:"cnf"`
|
|
|
+ Proof string `json:"proof"`
|
|
|
+ ClientID string `json:"client_id"`
|
|
|
+ APIVersion int `json:"api_version"`
|
|
|
+ Scope string `json:"scope"`
|
|
|
+}
|
|
|
+
|
|
|
+type wireDpopJwt struct {
|
|
|
+ jose.Claims
|
|
|
+ ClientID string `json:"client_id"`
|
|
|
+ Challenge string `json:"chal"`
|
|
|
+ Nonce string `json:"nonce"`
|
|
|
+ HTU string `json:"htu"`
|
|
|
+}
|
|
|
+
|
|
|
+type wireDpopToken map[string]any
|
|
|
+
|
|
|
+type wireVerifyParams struct {
|
|
|
+ token string
|
|
|
+ tokenKey crypto.PublicKey
|
|
|
+ dpopKey crypto.PublicKey
|
|
|
+ dpopKeyID string
|
|
|
+ issuer string
|
|
|
+ audience string
|
|
|
+ wireID wire.ID
|
|
|
+ chToken string
|
|
|
+ t time.Time
|
|
|
+}
|
|
|
+
|
|
|
+func parseAndVerifyWireAccessToken(v wireVerifyParams) (*wireAccessToken, *wireDpopToken, error) {
|
|
|
+ jwt, err := jose.ParseSigned(v.token)
|
|
|
if err != nil {
|
|
|
- return WrapErrorISE(err, "invalid DPoP token")
|
|
|
+ return nil, nil, fmt.Errorf("failed parsing token: %w", err)
|
|
|
+ }
|
|
|
+
|
|
|
+ if len(jwt.Headers) != 1 {
|
|
|
+ return nil, nil, fmt.Errorf("token has wrong number of headers %d", len(jwt.Headers))
|
|
|
+ }
|
|
|
+ keyID, err := KeyToID(&jose.JSONWebKey{Key: v.tokenKey})
|
|
|
+ if err != nil {
|
|
|
+ return nil, nil, fmt.Errorf("failed calculating token key ID: %w", err)
|
|
|
+ }
|
|
|
+ jwtKeyID := jwt.Headers[0].KeyID
|
|
|
+ if jwtKeyID == "" {
|
|
|
+ if jwtKeyID, err = KeyToID(jwt.Headers[0].JSONWebKey); err != nil {
|
|
|
+ return nil, nil, fmt.Errorf("failed extracting token key ID: %w", err)
|
|
|
+ }
|
|
|
}
|
|
|
- dpop := make(map[string]interface{})
|
|
|
- if err := parsedDpopToken.UnsafeClaimsWithoutVerification(&dpop); err != nil {
|
|
|
- return WrapErrorISE(err, "failed parsing dpop token")
|
|
|
+ if jwtKeyID != keyID {
|
|
|
+ return nil, nil, fmt.Errorf("invalid token key ID %q", jwtKeyID)
|
|
|
}
|
|
|
|
|
|
- orders, err := db.GetAllOrdersByAccountID(ctx, ch.AccountID)
|
|
|
+ var accessToken wireAccessToken
|
|
|
+ if err = jwt.Claims(v.tokenKey, &accessToken); err != nil {
|
|
|
+ return nil, nil, fmt.Errorf("failed validating Wire DPoP token claims: %w", err)
|
|
|
+ }
|
|
|
+
|
|
|
+ if err := accessToken.ValidateWithLeeway(jose.Expected{
|
|
|
+ Time: v.t,
|
|
|
+ Issuer: v.issuer,
|
|
|
+ Audience: jose.Audience{v.audience},
|
|
|
+ }, 1*time.Minute); err != nil {
|
|
|
+ return nil, nil, fmt.Errorf("failed validation: %w", err)
|
|
|
+ }
|
|
|
+
|
|
|
+ if accessToken.Challenge == "" {
|
|
|
+ return nil, nil, errors.New("access token challenge must not be empty")
|
|
|
+ }
|
|
|
+ if accessToken.Cnf.Kid == "" || accessToken.Cnf.Kid != v.dpopKeyID {
|
|
|
+ return nil, nil, fmt.Errorf("expected kid %q; got %q", v.dpopKeyID, accessToken.Cnf.Kid)
|
|
|
+ }
|
|
|
+ if accessToken.ClientID != v.wireID.ClientID {
|
|
|
+ return nil, nil, fmt.Errorf("invalid Wire client ID %q", accessToken.ClientID)
|
|
|
+ }
|
|
|
+ if accessToken.Expiry.Time().After(v.t.Add(time.Hour)) {
|
|
|
+ return nil, nil, fmt.Errorf("'exp' %s is too far into the future", accessToken.Expiry.Time().String())
|
|
|
+ }
|
|
|
+ if accessToken.Scope != "wire_client_id" {
|
|
|
+ return nil, nil, fmt.Errorf("invalid Wire scope %q", accessToken.Scope)
|
|
|
+ }
|
|
|
+
|
|
|
+ dpopJWT, err := jose.ParseSigned(accessToken.Proof)
|
|
|
if err != nil {
|
|
|
- return WrapErrorISE(err, "could not find current order by account id")
|
|
|
+ return nil, nil, fmt.Errorf("invalid Wire DPoP token: %w", err)
|
|
|
+ }
|
|
|
+ if len(dpopJWT.Headers) != 1 {
|
|
|
+ return nil, nil, fmt.Errorf("DPoP token has wrong number of headers %d", len(jwt.Headers))
|
|
|
+ }
|
|
|
+ dpopJwtKeyID := dpopJWT.Headers[0].KeyID
|
|
|
+ if dpopJwtKeyID == "" {
|
|
|
+ if dpopJwtKeyID, err = KeyToID(dpopJWT.Headers[0].JSONWebKey); err != nil {
|
|
|
+ return nil, nil, fmt.Errorf("failed extracting DPoP token key ID: %w", err)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if dpopJwtKeyID != v.dpopKeyID {
|
|
|
+ return nil, nil, fmt.Errorf("invalid DPoP token key ID %q", dpopJWT.Headers[0].KeyID)
|
|
|
}
|
|
|
|
|
|
- if len(orders) == 0 {
|
|
|
- return WrapErrorISE(err, "there are not enough orders for this account for this custom OIDC challenge")
|
|
|
+ var wireDpop wireDpopJwt
|
|
|
+ if err := dpopJWT.Claims(v.dpopKey, &wireDpop); err != nil {
|
|
|
+ return nil, nil, fmt.Errorf("failed validating Wire DPoP token claims: %w", err)
|
|
|
}
|
|
|
|
|
|
- order := orders[len(orders)-1]
|
|
|
+ if err := wireDpop.ValidateWithLeeway(jose.Expected{
|
|
|
+ Time: v.t,
|
|
|
+ Audience: jose.Audience{v.audience},
|
|
|
+ }, 1*time.Minute); err != nil {
|
|
|
+ return nil, nil, fmt.Errorf("failed DPoP validation: %w", err)
|
|
|
+ }
|
|
|
+ if wireDpop.HTU == "" || wireDpop.HTU != v.issuer { // DPoP doesn't contains "iss" claim, but has it in the "htu" claim
|
|
|
+ return nil, nil, fmt.Errorf("DPoP contains invalid issuer (htu) %q", wireDpop.HTU)
|
|
|
+ }
|
|
|
+ if wireDpop.Expiry.Time().After(v.t.Add(time.Hour)) {
|
|
|
+ return nil, nil, fmt.Errorf("'exp' %s is too far into the future", wireDpop.Expiry.Time().String())
|
|
|
+ }
|
|
|
+ if wireDpop.Subject != v.wireID.ClientID {
|
|
|
+ return nil, nil, fmt.Errorf("DPoP contains invalid Wire client ID %q", wireDpop.ClientID)
|
|
|
+ }
|
|
|
+ if wireDpop.Nonce == "" || wireDpop.Nonce != accessToken.Nonce {
|
|
|
+ return nil, nil, fmt.Errorf("DPoP contains invalid nonce %q", wireDpop.Nonce)
|
|
|
+ }
|
|
|
+ if wireDpop.Challenge == "" || wireDpop.Challenge != accessToken.Challenge {
|
|
|
+ return nil, nil, fmt.Errorf("DPoP contains invalid challenge %q", wireDpop.Challenge)
|
|
|
+ }
|
|
|
|
|
|
- if err := db.CreateDpopToken(ctx, order, dpop); err != nil {
|
|
|
- return WrapErrorISE(err, "failed storing DPoP token")
|
|
|
+ // TODO(hs): can we use the wireDpopJwt and map that instead of doing Claims() twice?
|
|
|
+ var dpopToken wireDpopToken
|
|
|
+ if err := dpopJWT.Claims(v.dpopKey, &dpopToken); err != nil {
|
|
|
+ return nil, nil, fmt.Errorf("failed validating Wire DPoP token claims: %w", err)
|
|
|
}
|
|
|
|
|
|
- return nil
|
|
|
+ challenge, ok := dpopToken["chal"].(string)
|
|
|
+ if !ok {
|
|
|
+ return nil, nil, fmt.Errorf("invalid challenge in Wire DPoP token")
|
|
|
+ }
|
|
|
+ if challenge == "" || challenge != v.chToken {
|
|
|
+ return nil, nil, fmt.Errorf("invalid Wire DPoP challenge %q", challenge)
|
|
|
+ }
|
|
|
+
|
|
|
+ handle, ok := dpopToken["handle"].(string)
|
|
|
+ if !ok {
|
|
|
+ return nil, nil, fmt.Errorf("invalid handle in Wire DPoP token")
|
|
|
+ }
|
|
|
+ if handle == "" || handle != v.wireID.Handle {
|
|
|
+ return nil, nil, fmt.Errorf("invalid Wire client handle %q", handle)
|
|
|
+ }
|
|
|
+
|
|
|
+ return &accessToken, &dpopToken, nil
|
|
|
}
|
|
|
|
|
|
type payloadType struct {
|