2023-04-14 18:12:31 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2023-03-29 21:08:31 +00:00
|
|
|
package pki
|
2023-03-28 17:29:54 +00:00
|
|
|
|
|
|
|
import (
|
2023-03-29 19:06:09 +00:00
|
|
|
"crypto"
|
|
|
|
"encoding/base64"
|
2023-03-28 17:29:54 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2023-04-12 15:29:54 +00:00
|
|
|
"strings"
|
2023-03-28 17:29:54 +00:00
|
|
|
|
|
|
|
jose "gopkg.in/square/go-jose.v2"
|
|
|
|
)
|
|
|
|
|
2023-03-28 20:49:07 +00:00
|
|
|
var AllowedOuterJWSTypes = map[string]interface{}{
|
|
|
|
"RS256": true,
|
|
|
|
"RS384": true,
|
|
|
|
"RS512": true,
|
|
|
|
"PS256": true,
|
|
|
|
"PS384": true,
|
|
|
|
"PS512": true,
|
|
|
|
"ES256": true,
|
|
|
|
"ES384": true,
|
|
|
|
"ES512": true,
|
|
|
|
"EdDSA2": true,
|
|
|
|
}
|
|
|
|
|
2023-03-28 17:29:54 +00:00
|
|
|
// This wraps a JWS message structure.
|
2023-03-29 21:08:31 +00:00
|
|
|
type jwsCtx struct {
|
2023-03-29 19:06:09 +00:00
|
|
|
Algo string `json:"alg"`
|
|
|
|
Kid string `json:"kid"`
|
2023-04-03 20:08:25 +00:00
|
|
|
Jwk json.RawMessage `json:"jwk"`
|
2023-03-29 19:06:09 +00:00
|
|
|
Nonce string `json:"nonce"`
|
|
|
|
Url string `json:"url"`
|
2023-04-03 20:08:25 +00:00
|
|
|
Key jose.JSONWebKey `json:"-"`
|
2023-03-29 19:06:09 +00:00
|
|
|
Existing bool `json:"-"`
|
2023-03-28 17:29:54 +00:00
|
|
|
}
|
|
|
|
|
2023-04-12 15:29:54 +00:00
|
|
|
func (c *jwsCtx) GetKeyThumbprint() (string, error) {
|
|
|
|
keyThumbprint, err := c.Key.Thumbprint(crypto.SHA256)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("failed creating thumbprint: %w", err)
|
|
|
|
}
|
|
|
|
return base64.URLEncoding.EncodeToString(keyThumbprint), nil
|
|
|
|
}
|
|
|
|
|
2023-04-03 20:08:25 +00:00
|
|
|
func (c *jwsCtx) UnmarshalJSON(a *acmeState, ac *acmeContext, jws []byte) error {
|
2023-03-28 17:29:54 +00:00
|
|
|
var err error
|
|
|
|
if err = json.Unmarshal(jws, c); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-04-03 20:08:25 +00:00
|
|
|
if c.Kid != "" && len(c.Jwk) > 0 {
|
2023-03-28 17:29:54 +00:00
|
|
|
// See RFC 8555 Section 6.2. Request Authentication:
|
|
|
|
//
|
|
|
|
// > The "jwk" and "kid" fields are mutually exclusive. Servers MUST
|
|
|
|
// > reject requests that contain both.
|
2023-03-29 21:08:31 +00:00
|
|
|
return fmt.Errorf("invalid header: got both account 'kid' and 'jwk' in the same message; expected only one: %w", ErrMalformed)
|
2023-03-28 17:29:54 +00:00
|
|
|
}
|
|
|
|
|
2023-04-03 20:08:25 +00:00
|
|
|
if c.Kid == "" && len(c.Jwk) == 0 {
|
2023-03-28 20:49:07 +00:00
|
|
|
// See RFC 8555 Section 6.2. Request Authentication:
|
2023-03-28 17:29:54 +00:00
|
|
|
//
|
|
|
|
// > Either "jwk" (JSON Web Key) or "kid" (Key ID) as specified
|
|
|
|
// > below
|
2023-03-29 21:08:31 +00:00
|
|
|
return fmt.Errorf("invalid header: got neither required fields of 'kid' nor 'jwk': %w", ErrMalformed)
|
2023-03-28 17:29:54 +00:00
|
|
|
}
|
|
|
|
|
2023-03-28 20:49:07 +00:00
|
|
|
if _, present := AllowedOuterJWSTypes[c.Algo]; !present {
|
|
|
|
// See RFC 8555 Section 6.2. Request Authentication:
|
|
|
|
//
|
|
|
|
// > The JWS Protected Header MUST include the following fields:
|
|
|
|
// >
|
|
|
|
// > - "alg" (Algorithm)
|
|
|
|
// >
|
|
|
|
// > * This field MUST NOT contain "none" or a Message
|
|
|
|
// > Authentication Code (MAC) algorithm (e.g. one in which the
|
|
|
|
// > algorithm registry description mentions MAC/HMAC).
|
2023-03-29 21:08:31 +00:00
|
|
|
return fmt.Errorf("invalid header: unexpected value for 'algo': %w", ErrMalformed)
|
2023-03-28 20:49:07 +00:00
|
|
|
}
|
|
|
|
|
2023-03-28 17:29:54 +00:00
|
|
|
if c.Kid != "" {
|
|
|
|
// Load KID from storage first.
|
2023-04-12 15:29:54 +00:00
|
|
|
kid := getKeyIdFromAccountUrl(c.Kid)
|
|
|
|
c.Jwk, err = a.LoadJWK(ac, kid)
|
2023-03-28 17:29:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-04-12 15:29:54 +00:00
|
|
|
c.Kid = kid // Use the uuid itself, not the full account url that was originally provided to us.
|
2023-03-29 19:06:09 +00:00
|
|
|
c.Existing = true
|
2023-03-28 17:29:54 +00:00
|
|
|
}
|
|
|
|
|
2023-04-03 20:08:25 +00:00
|
|
|
if err = c.Key.UnmarshalJSON(c.Jwk); err != nil {
|
2023-03-28 17:29:54 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-04-03 20:08:25 +00:00
|
|
|
if !c.Key.Valid() {
|
2023-03-29 21:08:31 +00:00
|
|
|
return fmt.Errorf("received invalid jwk: %w", ErrMalformed)
|
2023-03-28 17:29:54 +00:00
|
|
|
}
|
|
|
|
|
2023-04-03 20:08:25 +00:00
|
|
|
if c.Kid == "" {
|
2023-04-12 15:29:54 +00:00
|
|
|
c.Kid = genUuid()
|
2023-03-29 19:06:09 +00:00
|
|
|
c.Existing = false
|
|
|
|
}
|
|
|
|
|
2023-03-28 17:29:54 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-04-12 15:29:54 +00:00
|
|
|
func getKeyIdFromAccountUrl(accountUrl string) string {
|
|
|
|
pieces := strings.Split(accountUrl, "/")
|
|
|
|
return pieces[len(pieces)-1]
|
|
|
|
}
|
|
|
|
|
2023-03-28 17:29:54 +00:00
|
|
|
func hasValues(h jose.Header) bool {
|
|
|
|
return h.KeyID != "" || h.JSONWebKey != nil || h.Algorithm != "" || h.Nonce != "" || len(h.ExtraHeaders) > 0
|
|
|
|
}
|
|
|
|
|
2023-03-29 21:08:31 +00:00
|
|
|
func (c *jwsCtx) VerifyJWS(signature string) (map[string]interface{}, error) {
|
2023-03-28 20:49:07 +00:00
|
|
|
// See RFC 8555 Section 6.2. Request Authentication:
|
|
|
|
//
|
|
|
|
// > The JWS Unencoded Payload Option [RFC7797] MUST NOT be used
|
|
|
|
//
|
|
|
|
// This is validated by go-jose.
|
2023-03-28 17:29:54 +00:00
|
|
|
sig, err := jose.ParseSigned(signature)
|
|
|
|
if err != nil {
|
2023-03-29 21:08:31 +00:00
|
|
|
return nil, fmt.Errorf("error parsing signature: %s: %w", err, ErrMalformed)
|
2023-03-28 17:29:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(sig.Signatures) > 1 {
|
|
|
|
// See RFC 8555 Section 6.2. Request Authentication:
|
|
|
|
//
|
|
|
|
// > The JWS MUST NOT have multiple signatures
|
2023-03-29 21:08:31 +00:00
|
|
|
return nil, fmt.Errorf("request had multiple signatures: %w", ErrMalformed)
|
2023-03-28 17:29:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if hasValues(sig.Signatures[0].Unprotected) {
|
|
|
|
// See RFC 8555 Section 6.2. Request Authentication:
|
|
|
|
//
|
|
|
|
// > The JWS Unprotected Header [RFC7515] MUST NOT be used
|
2023-03-29 21:08:31 +00:00
|
|
|
return nil, fmt.Errorf("request had unprotected headers: %w", ErrMalformed)
|
2023-03-28 17:29:54 +00:00
|
|
|
}
|
|
|
|
|
2023-04-03 20:08:25 +00:00
|
|
|
payload, err := sig.Verify(c.Key)
|
2023-03-28 17:29:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-04-12 16:55:25 +00:00
|
|
|
if len(payload) == 0 {
|
|
|
|
// Distinguish POST-AS-GET from POST-with-an-empty-body.
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2023-03-28 17:29:54 +00:00
|
|
|
var m map[string]interface{}
|
|
|
|
if err := json.Unmarshal(payload, &m); err != nil {
|
2023-03-29 21:08:31 +00:00
|
|
|
return nil, fmt.Errorf("failed to json unmarshal 'payload': %s: %w", err, ErrMalformed)
|
2023-03-28 17:29:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return m, nil
|
|
|
|
}
|