certificates/acme/order.go

221 lines
6.7 KiB
Go
Raw Normal View History

2019-05-27 00:41:10 +00:00
import (
"context"
"crypto/x509"
"encoding/json"
"sort"
"strings"
2019-05-27 00:41:10 +00:00
"time"
"github.com/pkg/errors"
"github.com/smallstep/certificates/authority/provisioner"
"go.step.sm/crypto/x509util"
2019-05-27 00:41:10 +00:00
)
// Order contains order metadata for the ACME protocol order type.
type Order struct {
2021-02-28 01:05:37 +00:00
Status string `json:"status"`
Expires string `json:"expires,omitempty"`
Identifiers []Identifier `json:"identifiers"`
NotBefore string `json:"notBefore,omitempty"`
NotAfter string `json:"notAfter,omitempty"`
Error interface{} `json:"error,omitempty"`
Authorizations []string `json:"authorizations"`
Finalize string `json:"finalize"`
Certificate string `json:"certificate,omitempty"`
ID string `json:"-"`
ProvisionerID string `json:"-"`
DefaultDuration time.Duration `json:"-"`
Backdate time.Duration `json:"-"`
2019-05-27 00:41:10 +00:00
}
// ToLog enables response logging.
func (o *Order) ToLog() (interface{}, error) {
b, err := json.Marshal(o)
if err != nil {
return nil, ServerInternalErr(errors.Wrap(err, "error marshaling order for logging"))
}
return string(b), nil
}
2021-02-28 01:05:37 +00:00
// UpdateStatus updates the ACME Order Status if necessary.
// Changes to the order are saved using the database interface.
func (o *Order) UpdateStatus(ctx context.Context, db DB) error {
now := time.Now().UTC()
expiry, err := time.Parse(time.RFC3339, o.Expires)
2019-05-27 00:41:10 +00:00
if err != nil {
2021-02-28 01:05:37 +00:00
return ServerInternalErr(errors.Wrap("error converting expiry string to time"))
2019-05-27 00:41:10 +00:00
}
switch o.Status {
case StatusInvalid:
2021-02-28 01:05:37 +00:00
return nil
2019-05-27 00:41:10 +00:00
case StatusValid:
2021-02-28 01:05:37 +00:00
return nil
2019-05-27 00:41:10 +00:00
case StatusReady:
2021-02-28 01:05:37 +00:00
// Check expiry
if now.After(expiry) {
o.Status = StatusInvalid
o.Error = MalformedErr(errors.New("order has expired"))
2019-05-27 00:41:10 +00:00
break
}
2021-02-28 01:05:37 +00:00
return nil
2019-05-27 00:41:10 +00:00
case StatusPending:
2021-02-28 01:05:37 +00:00
// Check expiry
if now.After(expiry) {
o.Status = StatusInvalid
o.Error = MalformedErr(errors.New("order has expired"))
2019-05-27 00:41:10 +00:00
break
}
var count = map[string]int{
StatusValid: 0,
StatusInvalid: 0,
StatusPending: 0,
}
for _, azID := range o.Authorizations {
2021-02-28 01:05:37 +00:00
az, err := db.GetAuthorization(ctx, azID)
2019-05-27 00:41:10 +00:00
if err != nil {
2021-02-28 01:05:37 +00:00
return false, err
2019-05-27 00:41:10 +00:00
}
2021-02-28 01:05:37 +00:00
if az, err = az.UpdateStatus(db); err != nil {
return false, err
2019-05-27 00:41:10 +00:00
}
2021-02-28 01:05:37 +00:00
st := az.Status
2019-05-27 00:41:10 +00:00
count[st]++
}
switch {
case count[StatusInvalid] > 0:
2021-02-28 01:05:37 +00:00
o.Status = StatusInvalid
// No change in the order status, so just return the order as is -
// without writing any changes.
2019-05-27 00:41:10 +00:00
case count[StatusPending] > 0:
2021-02-28 01:05:37 +00:00
return nil
2019-05-27 00:41:10 +00:00
case count[StatusValid] == len(o.Authorizations):
2021-02-28 01:05:37 +00:00
o.Status = StatusReady
2019-05-27 00:41:10 +00:00
default:
return nil, ServerInternalErr(errors.New("unexpected authz status"))
}
default:
return nil, ServerInternalErr(errors.Errorf("unrecognized order status: %s", o.Status))
}
2021-02-28 01:05:37 +00:00
return db.UpdateOrder(ctx, o)
2019-05-27 00:41:10 +00:00
}
// finalize signs a certificate if the necessary conditions for Order completion
// have been met.
2021-02-28 01:05:37 +00:00
func (o *order) Finalize(ctx, db DB, csr *x509.CertificateRequest, auth SignAuthority, p Provisioner) error {
2019-05-27 00:41:10 +00:00
var err error
2021-02-28 01:05:37 +00:00
if o, err = o.UpdateStatus(db); err != nil {
2019-05-27 00:41:10 +00:00
return nil, err
}
2019-05-27 00:41:10 +00:00
switch o.Status {
case StatusInvalid:
2021-02-28 01:05:37 +00:00
return OrderNotReadyErr(errors.Errorf("order %s has been abandoned", o.ID))
2019-05-27 00:41:10 +00:00
case StatusValid:
2021-02-28 01:05:37 +00:00
return nil
2019-05-27 00:41:10 +00:00
case StatusPending:
2021-02-28 01:05:37 +00:00
return OrderNotReadyErr(errors.Errorf("order %s is not ready", o.ID))
2019-05-27 00:41:10 +00:00
case StatusReady:
break
default:
return nil, ServerInternalErr(errors.Errorf("unexpected status %s for order %s", o.Status, o.ID))
}
// RFC8555: The CSR MUST indicate the exact same set of requested
// identifiers as the initial newOrder request. Identifiers of type "dns"
// MUST appear either in the commonName portion of the requested subject
// name or in an extensionRequest attribute [RFC2985] requesting a
// subjectAltName extension, or both.
if csr.Subject.CommonName != "" {
csr.DNSNames = append(csr.DNSNames, csr.Subject.CommonName)
2019-05-27 00:41:10 +00:00
}
csr.DNSNames = uniqueLowerNames(csr.DNSNames)
orderNames := make([]string, len(o.Identifiers))
for i, n := range o.Identifiers {
orderNames[i] = n.Value
2019-05-27 00:41:10 +00:00
}
2021-02-28 01:05:37 +00:00
orderNames = uniqueSortedLowerNames(orderNames)
// Validate identifier names against CSR alternative names.
//
2020-07-31 17:45:59 +00:00
// Note that with certificate templates we are not going to check for the
// absence of other SANs as they will only be set if the templates allows
// them.
if len(csr.DNSNames) != len(orderNames) {
return nil, BadCSRErr(errors.Errorf("CSR names do not match identifiers exactly: CSR names = %v, Order names = %v", csr.DNSNames, orderNames))
}
sans := make([]x509util.SubjectAlternativeName, len(csr.DNSNames))
for i := range csr.DNSNames {
if csr.DNSNames[i] != orderNames[i] {
return nil, BadCSRErr(errors.Errorf("CSR names do not match identifiers exactly: CSR names = %v, Order names = %v", csr.DNSNames, orderNames))
}
sans[i] = x509util.SubjectAlternativeName{
Type: x509util.DNSType,
Value: csr.DNSNames[i],
}
2020-06-23 18:10:45 +00:00
}
2019-05-27 00:41:10 +00:00
// Get authorizations from the ACME provisioner.
ctx := provisioner.NewContextWithMethod(context.Background(), provisioner.SignMethod)
signOps, err := p.AuthorizeSign(ctx, "")
if err != nil {
return nil, ServerInternalErr(errors.Wrapf(err, "error retrieving authorization options from ACME provisioner"))
}
// Template data
data := x509util.NewTemplateData()
data.SetCommonName(csr.Subject.CommonName)
data.Set(x509util.SANsKey, sans)
templateOptions, err := provisioner.TemplateOptions(p.GetOptions(), data)
if err != nil {
return nil, ServerInternalErr(errors.Wrapf(err, "error creating template options from ACME provisioner"))
}
signOps = append(signOps, templateOptions)
2019-05-27 00:41:10 +00:00
// Create and store a new certificate.
certChain, err := auth.Sign(csr, provisioner.SignOptions{
2019-05-27 00:41:10 +00:00
NotBefore: provisioner.NewTimeDuration(o.NotBefore),
NotAfter: provisioner.NewTimeDuration(o.NotAfter),
}, signOps...)
if err != nil {
return nil, ServerInternalErr(errors.Wrapf(err, "error generating certificate for order %s", o.ID))
}
cert, err := newCert(db, CertOptions{
AccountID: o.AccountID,
OrderID: o.ID,
Leaf: certChain[0],
Intermediates: certChain[1:],
2019-05-27 00:41:10 +00:00
})
if err != nil {
return nil, err
}
2021-02-28 01:05:37 +00:00
o.Certificate = cert.ID
o.Status = StatusValid
return db.UpdateOrder(ctx, o)
2019-05-27 00:41:10 +00:00
}
2021-02-28 01:05:37 +00:00
// uniqueSortedLowerNames returns the set of all unique names in the input after all
// of them are lowercased. The returned names will be in their lowercased form
// and sorted alphabetically.
2021-02-28 01:05:37 +00:00
func uniqueSortedLowerNames(names []string) (unique []string) {
nameMap := make(map[string]int, len(names))
for _, name := range names {
nameMap[strings.ToLower(name)] = 1
}
unique = make([]string, 0, len(nameMap))
for name := range nameMap {
unique = append(unique, name)
}
sort.Strings(unique)
return
}