2023-03-29 21:08:31 +00:00
|
|
|
package pki
|
2023-03-28 17:29:54 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rand"
|
|
|
|
"encoding/base64"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2023-04-03 20:08:25 +00:00
|
|
|
"strings"
|
2023-03-28 17:29:54 +00:00
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hashicorp/vault/sdk/framework"
|
2023-04-03 20:08:25 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/logical"
|
2023-03-28 17:29:54 +00:00
|
|
|
)
|
|
|
|
|
2023-04-03 20:08:25 +00:00
|
|
|
const (
|
|
|
|
// How long nonces are considered valid.
|
|
|
|
nonceExpiry = 15 * time.Minute
|
|
|
|
|
|
|
|
// Path Prefixes
|
|
|
|
acmePathPrefix = "acme/"
|
|
|
|
acmeAccountPrefix = acmePathPrefix + "accounts/"
|
|
|
|
)
|
2023-03-28 17:29:54 +00:00
|
|
|
|
2023-03-29 21:08:31 +00:00
|
|
|
type acmeState struct {
|
2023-03-28 17:29:54 +00:00
|
|
|
nextExpiry *atomic.Int64
|
|
|
|
nonces *sync.Map // map[string]time.Time
|
|
|
|
}
|
|
|
|
|
2023-03-29 21:08:31 +00:00
|
|
|
func NewACMEState() *acmeState {
|
|
|
|
return &acmeState{
|
2023-03-28 17:29:54 +00:00
|
|
|
nextExpiry: new(atomic.Int64),
|
|
|
|
nonces: new(sync.Map),
|
2023-03-29 18:22:48 +00:00
|
|
|
}
|
2023-03-28 17:29:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func generateNonce() (string, error) {
|
|
|
|
data := make([]byte, 21)
|
|
|
|
if _, err := io.ReadFull(rand.Reader, data); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return base64.RawURLEncoding.EncodeToString(data), nil
|
|
|
|
}
|
|
|
|
|
2023-03-29 21:08:31 +00:00
|
|
|
func (a *acmeState) GetNonce() (string, time.Time, error) {
|
2023-03-28 17:29:54 +00:00
|
|
|
now := time.Now()
|
|
|
|
nonce, err := generateNonce()
|
|
|
|
if err != nil {
|
|
|
|
return "", now, err
|
|
|
|
}
|
|
|
|
|
|
|
|
then := now.Add(nonceExpiry)
|
|
|
|
a.nonces.Store(nonce, then)
|
|
|
|
|
|
|
|
nextExpiry := a.nextExpiry.Load()
|
|
|
|
next := time.Unix(nextExpiry, 0)
|
|
|
|
if now.After(next) || then.Before(next) {
|
|
|
|
a.nextExpiry.Store(then.Unix())
|
|
|
|
}
|
|
|
|
|
|
|
|
return nonce, then, nil
|
|
|
|
}
|
|
|
|
|
2023-03-29 21:08:31 +00:00
|
|
|
func (a *acmeState) RedeemNonce(nonce string) bool {
|
2023-03-28 17:29:54 +00:00
|
|
|
rawTimeout, present := a.nonces.LoadAndDelete(nonce)
|
|
|
|
if !present {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
timeout := rawTimeout.(time.Time)
|
|
|
|
if time.Now().After(timeout) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2023-03-29 21:08:31 +00:00
|
|
|
func (a *acmeState) DoTidyNonces() {
|
2023-03-28 17:29:54 +00:00
|
|
|
now := time.Now()
|
|
|
|
expiry := a.nextExpiry.Load()
|
|
|
|
then := time.Unix(expiry, 0)
|
|
|
|
|
|
|
|
if expiry == 0 || now.After(then) {
|
|
|
|
a.TidyNonces()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-29 21:08:31 +00:00
|
|
|
func (a *acmeState) TidyNonces() {
|
2023-03-28 17:29:54 +00:00
|
|
|
now := time.Now()
|
|
|
|
nextRun := now.Add(nonceExpiry)
|
|
|
|
|
|
|
|
a.nonces.Range(func(key, value any) bool {
|
|
|
|
timeout := value.(time.Time)
|
|
|
|
if now.After(timeout) {
|
|
|
|
a.nonces.Delete(key)
|
|
|
|
}
|
|
|
|
|
|
|
|
if timeout.Before(nextRun) {
|
|
|
|
nextRun = timeout
|
|
|
|
}
|
|
|
|
|
|
|
|
return false /* don't quit looping */
|
|
|
|
})
|
|
|
|
|
|
|
|
a.nextExpiry.Store(nextRun.Unix())
|
|
|
|
}
|
|
|
|
|
2023-04-03 20:08:25 +00:00
|
|
|
type ACMEStates string
|
|
|
|
|
|
|
|
const (
|
|
|
|
StatusValid = "valid"
|
|
|
|
StatusDeactivated = "deactivated"
|
|
|
|
StatusRevoked = "revoked"
|
|
|
|
)
|
|
|
|
|
|
|
|
type acmeAccount struct {
|
|
|
|
KeyId string `json:"-"`
|
|
|
|
Status ACMEStates `json:"state"`
|
|
|
|
Contact []string `json:"contact"`
|
|
|
|
TermsOfServiceAgreed bool `json:"termsOfServiceAgreed"`
|
|
|
|
Jwk []byte `json:"jwk"`
|
2023-03-28 17:29:54 +00:00
|
|
|
}
|
|
|
|
|
2023-04-03 20:08:25 +00:00
|
|
|
func (a *acmeState) CreateAccount(ac *acmeContext, c *jwsCtx, contact []string, termsOfServiceAgreed bool) (*acmeAccount, error) {
|
|
|
|
acct := &acmeAccount{
|
|
|
|
KeyId: c.Kid,
|
|
|
|
Contact: contact,
|
|
|
|
TermsOfServiceAgreed: termsOfServiceAgreed,
|
|
|
|
Jwk: c.Jwk,
|
|
|
|
}
|
|
|
|
|
|
|
|
json, err := logical.StorageEntryJSON(acmeAccountPrefix+c.Kid, acct)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error creating account entry: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := ac.sc.Storage.Put(ac.sc.Context, json); err != nil {
|
|
|
|
return nil, fmt.Errorf("error writing account entry: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return acct, nil
|
2023-03-29 19:06:09 +00:00
|
|
|
}
|
|
|
|
|
2023-04-03 20:08:25 +00:00
|
|
|
func cleanKid(keyID string) string {
|
|
|
|
pieces := strings.Split(keyID, "/")
|
|
|
|
return pieces[len(pieces)-1]
|
2023-03-29 19:06:09 +00:00
|
|
|
}
|
|
|
|
|
2023-04-03 20:08:25 +00:00
|
|
|
func (a *acmeState) LoadAccount(ac *acmeContext, keyID string) (*acmeAccount, error) {
|
|
|
|
kid := cleanKid(keyID)
|
|
|
|
|
|
|
|
entry, err := ac.sc.Storage.Get(ac.sc.Context, acmeAccountPrefix+kid)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error loading account: %w", err)
|
|
|
|
}
|
|
|
|
if entry == nil {
|
|
|
|
return nil, fmt.Errorf("account not found: %w", ErrMalformed)
|
|
|
|
}
|
|
|
|
|
|
|
|
var acct acmeAccount
|
|
|
|
err = entry.DecodeJSON(&acct)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error loading account: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &acct, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *acmeState) DoesAccountExist(ac *acmeContext, keyId string) bool {
|
|
|
|
account, err := a.LoadAccount(ac, keyId)
|
|
|
|
return err == nil && account != nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *acmeState) LoadJWK(ac *acmeContext, keyID string) ([]byte, error) {
|
|
|
|
key, err := a.LoadAccount(ac, keyID)
|
2023-03-28 17:29:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-04-03 20:08:25 +00:00
|
|
|
if len(key.Jwk) == 0 {
|
2023-03-28 17:29:54 +00:00
|
|
|
return nil, fmt.Errorf("malformed key entry lacks JWK")
|
|
|
|
}
|
|
|
|
|
2023-04-03 20:08:25 +00:00
|
|
|
return key.Jwk, nil
|
2023-03-28 17:29:54 +00:00
|
|
|
}
|
|
|
|
|
2023-04-03 20:08:25 +00:00
|
|
|
func (a *acmeState) ParseRequestParams(ac *acmeContext, data *framework.FieldData) (*jwsCtx, map[string]interface{}, error) {
|
2023-03-29 21:08:31 +00:00
|
|
|
var c jwsCtx
|
2023-03-28 17:29:54 +00:00
|
|
|
var m map[string]interface{}
|
|
|
|
|
|
|
|
// Parse the key out.
|
2023-03-29 21:08:31 +00:00
|
|
|
rawJWKBase64, ok := data.GetOk("protected")
|
|
|
|
if !ok {
|
|
|
|
return nil, nil, fmt.Errorf("missing required field 'protected': %w", ErrMalformed)
|
|
|
|
}
|
|
|
|
jwkBase64 := rawJWKBase64.(string)
|
|
|
|
|
2023-03-28 17:29:54 +00:00
|
|
|
jwkBytes, err := base64.RawURLEncoding.DecodeString(jwkBase64)
|
|
|
|
if err != nil {
|
2023-03-29 21:08:31 +00:00
|
|
|
return nil, nil, fmt.Errorf("failed to base64 parse 'protected': %s: %w", err, ErrMalformed)
|
2023-03-28 17:29:54 +00:00
|
|
|
}
|
2023-04-03 20:08:25 +00:00
|
|
|
if err = c.UnmarshalJSON(a, ac, jwkBytes); err != nil {
|
2023-03-28 17:29:54 +00:00
|
|
|
return nil, nil, fmt.Errorf("failed to json unmarshal 'protected': %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Since we already parsed the header to verify the JWS context, we
|
|
|
|
// should read and redeem the nonce here too, to avoid doing any extra
|
|
|
|
// work if it is invalid.
|
|
|
|
if !a.RedeemNonce(c.Nonce) {
|
2023-03-29 21:08:31 +00:00
|
|
|
return nil, nil, fmt.Errorf("invalid or reused nonce: %w", ErrBadNonce)
|
2023-03-28 17:29:54 +00:00
|
|
|
}
|
|
|
|
|
2023-03-29 21:08:31 +00:00
|
|
|
rawPayloadBase64, ok := data.GetOk("payload")
|
|
|
|
if !ok {
|
|
|
|
return nil, nil, fmt.Errorf("missing required field 'payload': %w", ErrMalformed)
|
|
|
|
}
|
|
|
|
payloadBase64 := rawPayloadBase64.(string)
|
|
|
|
|
|
|
|
rawSignatureBase64, ok := data.GetOk("signature")
|
|
|
|
if !ok {
|
|
|
|
return nil, nil, fmt.Errorf("missing required field 'signature': %w", ErrMalformed)
|
|
|
|
}
|
|
|
|
signatureBase64 := rawSignatureBase64.(string)
|
2023-03-28 17:29:54 +00:00
|
|
|
|
|
|
|
// go-jose only seems to support compact signature encodings.
|
|
|
|
compactSig := fmt.Sprintf("%v.%v.%v", jwkBase64, payloadBase64, signatureBase64)
|
|
|
|
m, err = c.VerifyJWS(compactSig)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, fmt.Errorf("failed to verify signature: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &c, m, nil
|
|
|
|
}
|