Blame vendor/golang.org/x/crypto/acme/rfc8555.go

Packit Service 509fd4
// Copyright 2019 The Go Authors. All rights reserved.
Packit Service 509fd4
// Use of this source code is governed by a BSD-style
Packit Service 509fd4
// license that can be found in the LICENSE file.
Packit Service 509fd4
Packit Service 509fd4
package acme
Packit Service 509fd4
Packit Service 509fd4
import (
Packit Service 509fd4
	"context"
Packit Service 509fd4
	"crypto"
Packit Service 509fd4
	"encoding/base64"
Packit Service 509fd4
	"encoding/json"
Packit Service 509fd4
	"encoding/pem"
Packit Service 509fd4
	"errors"
Packit Service 509fd4
	"fmt"
Packit Service 509fd4
	"io"
Packit Service 509fd4
	"io/ioutil"
Packit Service 509fd4
	"net/http"
Packit Service 509fd4
	"time"
Packit Service 509fd4
)
Packit Service 509fd4
Packit Service 509fd4
// DeactivateReg permanently disables an existing account associated with c.Key.
Packit Service 509fd4
// A deactivated account can no longer request certificate issuance or access
Packit Service 509fd4
// resources related to the account, such as orders or authorizations.
Packit Service 509fd4
//
Packit Service 509fd4
// It only works with CAs implementing RFC 8555.
Packit Service 509fd4
func (c *Client) DeactivateReg(ctx context.Context) error {
Packit Service 509fd4
	url := string(c.accountKID(ctx))
Packit Service 509fd4
	if url == "" {
Packit Service 509fd4
		return ErrNoAccount
Packit Service 509fd4
	}
Packit Service 509fd4
	req := json.RawMessage(`{"status": "deactivated"}`)
Packit Service 509fd4
	res, err := c.post(ctx, nil, url, req, wantStatus(http.StatusOK))
Packit Service 509fd4
	if err != nil {
Packit Service 509fd4
		return err
Packit Service 509fd4
	}
Packit Service 509fd4
	res.Body.Close()
Packit Service 509fd4
	return nil
Packit Service 509fd4
}
Packit Service 509fd4
Packit Service 509fd4
// registerRFC is quivalent to c.Register but for CAs implementing RFC 8555.
Packit Service 509fd4
// It expects c.Discover to have already been called.
Packit Service 509fd4
// TODO: Implement externalAccountBinding.
Packit Service 509fd4
func (c *Client) registerRFC(ctx context.Context, acct *Account, prompt func(tosURL string) bool) (*Account, error) {
Packit Service 509fd4
	c.cacheMu.Lock() // guard c.kid access
Packit Service 509fd4
	defer c.cacheMu.Unlock()
Packit Service 509fd4
Packit Service 509fd4
	req := struct {
Packit Service 509fd4
		TermsAgreed bool     `json:"termsOfServiceAgreed,omitempty"`
Packit Service 509fd4
		Contact     []string `json:"contact,omitempty"`
Packit Service 509fd4
	}{
Packit Service 509fd4
		Contact: acct.Contact,
Packit Service 509fd4
	}
Packit Service 509fd4
	if c.dir.Terms != "" {
Packit Service 509fd4
		req.TermsAgreed = prompt(c.dir.Terms)
Packit Service 509fd4
	}
Packit Service 509fd4
	res, err := c.post(ctx, c.Key, c.dir.RegURL, req, wantStatus(
Packit Service 509fd4
		http.StatusOK,      // account with this key already registered
Packit Service 509fd4
		http.StatusCreated, // new account created
Packit Service 509fd4
	))
Packit Service 509fd4
	if err != nil {
Packit Service 509fd4
		return nil, err
Packit Service 509fd4
	}
Packit Service 509fd4
Packit Service 509fd4
	defer res.Body.Close()
Packit Service 509fd4
	a, err := responseAccount(res)
Packit Service 509fd4
	if err != nil {
Packit Service 509fd4
		return nil, err
Packit Service 509fd4
	}
Packit Service 509fd4
	// Cache Account URL even if we return an error to the caller.
Packit Service 509fd4
	// It is by all means a valid and usable "kid" value for future requests.
Packit Service 509fd4
	c.kid = keyID(a.URI)
Packit Service 509fd4
	if res.StatusCode == http.StatusOK {
Packit Service 509fd4
		return nil, ErrAccountAlreadyExists
Packit Service 509fd4
	}
Packit Service 509fd4
	return a, nil
Packit Service 509fd4
}
Packit Service 509fd4
Packit Service 509fd4
// updateGegRFC is equivalent to c.UpdateReg but for CAs implementing RFC 8555.
Packit Service 509fd4
// It expects c.Discover to have already been called.
Packit Service 509fd4
func (c *Client) updateRegRFC(ctx context.Context, a *Account) (*Account, error) {
Packit Service 509fd4
	url := string(c.accountKID(ctx))
Packit Service 509fd4
	if url == "" {
Packit Service 509fd4
		return nil, ErrNoAccount
Packit Service 509fd4
	}
Packit Service 509fd4
	req := struct {
Packit Service 509fd4
		Contact []string `json:"contact,omitempty"`
Packit Service 509fd4
	}{
Packit Service 509fd4
		Contact: a.Contact,
Packit Service 509fd4
	}
Packit Service 509fd4
	res, err := c.post(ctx, nil, url, req, wantStatus(http.StatusOK))
Packit Service 509fd4
	if err != nil {
Packit Service 509fd4
		return nil, err
Packit Service 509fd4
	}
Packit Service 509fd4
	defer res.Body.Close()
Packit Service 509fd4
	return responseAccount(res)
Packit Service 509fd4
}
Packit Service 509fd4
Packit Service 509fd4
// getGegRFC is equivalent to c.GetReg but for CAs implementing RFC 8555.
Packit Service 509fd4
// It expects c.Discover to have already been called.
Packit Service 509fd4
func (c *Client) getRegRFC(ctx context.Context) (*Account, error) {
Packit Service 509fd4
	req := json.RawMessage(`{"onlyReturnExisting": true}`)
Packit Service 509fd4
	res, err := c.post(ctx, c.Key, c.dir.RegURL, req, wantStatus(http.StatusOK))
Packit Service 509fd4
	if e, ok := err.(*Error); ok && e.ProblemType == "urn:ietf:params:acme:error:accountDoesNotExist" {
Packit Service 509fd4
		return nil, ErrNoAccount
Packit Service 509fd4
	}
Packit Service 509fd4
	if err != nil {
Packit Service 509fd4
		return nil, err
Packit Service 509fd4
	}
Packit Service 509fd4
Packit Service 509fd4
	defer res.Body.Close()
Packit Service 509fd4
	return responseAccount(res)
Packit Service 509fd4
}
Packit Service 509fd4
Packit Service 509fd4
func responseAccount(res *http.Response) (*Account, error) {
Packit Service 509fd4
	var v struct {
Packit Service 509fd4
		Status  string
Packit Service 509fd4
		Contact []string
Packit Service 509fd4
		Orders  string
Packit Service 509fd4
	}
Packit Service 509fd4
	if err := json.NewDecoder(res.Body).Decode(&v); err != nil {
Packit Service 509fd4
		return nil, fmt.Errorf("acme: invalid account response: %v", err)
Packit Service 509fd4
	}
Packit Service 509fd4
	return &Account{
Packit Service 509fd4
		URI:       res.Header.Get("Location"),
Packit Service 509fd4
		Status:    v.Status,
Packit Service 509fd4
		Contact:   v.Contact,
Packit Service 509fd4
		OrdersURL: v.Orders,
Packit Service 509fd4
	}, nil
Packit Service 509fd4
}
Packit Service 509fd4
Packit Service 509fd4
// AuthorizeOrder initiates the order-based application for certificate issuance,
Packit Service 509fd4
// as opposed to pre-authorization in Authorize.
Packit Service 509fd4
// It is only supported by CAs implementing RFC 8555.
Packit Service 509fd4
//
Packit Service 509fd4
// The caller then needs to fetch each authorization with GetAuthorization,
Packit Service 509fd4
// identify those with StatusPending status and fulfill a challenge using Accept.
Packit Service 509fd4
// Once all authorizations are satisfied, the caller will typically want to poll
Packit Service 509fd4
// order status using WaitOrder until it's in StatusReady state.
Packit Service 509fd4
// To finalize the order and obtain a certificate, the caller submits a CSR with CreateOrderCert.
Packit Service 509fd4
func (c *Client) AuthorizeOrder(ctx context.Context, id []AuthzID, opt ...OrderOption) (*Order, error) {
Packit Service 509fd4
	dir, err := c.Discover(ctx)
Packit Service 509fd4
	if err != nil {
Packit Service 509fd4
		return nil, err
Packit Service 509fd4
	}
Packit Service 509fd4
Packit Service 509fd4
	req := struct {
Packit Service 509fd4
		Identifiers []wireAuthzID `json:"identifiers"`
Packit Service 509fd4
		NotBefore   string        `json:"notBefore,omitempty"`
Packit Service 509fd4
		NotAfter    string        `json:"notAfter,omitempty"`
Packit Service 509fd4
	}{}
Packit Service 509fd4
	for _, v := range id {
Packit Service 509fd4
		req.Identifiers = append(req.Identifiers, wireAuthzID{
Packit Service 509fd4
			Type:  v.Type,
Packit Service 509fd4
			Value: v.Value,
Packit Service 509fd4
		})
Packit Service 509fd4
	}
Packit Service 509fd4
	for _, o := range opt {
Packit Service 509fd4
		switch o := o.(type) {
Packit Service 509fd4
		case orderNotBeforeOpt:
Packit Service 509fd4
			req.NotBefore = time.Time(o).Format(time.RFC3339)
Packit Service 509fd4
		case orderNotAfterOpt:
Packit Service 509fd4
			req.NotAfter = time.Time(o).Format(time.RFC3339)
Packit Service 509fd4
		default:
Packit Service 509fd4
			// Package's fault if we let this happen.
Packit Service 509fd4
			panic(fmt.Sprintf("unsupported order option type %T", o))
Packit Service 509fd4
		}
Packit Service 509fd4
	}
Packit Service 509fd4
Packit Service 509fd4
	res, err := c.post(ctx, nil, dir.OrderURL, req, wantStatus(http.StatusCreated))
Packit Service 509fd4
	if err != nil {
Packit Service 509fd4
		return nil, err
Packit Service 509fd4
	}
Packit Service 509fd4
	defer res.Body.Close()
Packit Service 509fd4
	return responseOrder(res)
Packit Service 509fd4
}
Packit Service 509fd4
Packit Service 509fd4
// GetOrder retrives an order identified by the given URL.
Packit Service 509fd4
// For orders created with AuthorizeOrder, the url value is Order.URI.
Packit Service 509fd4
//
Packit Service 509fd4
// If a caller needs to poll an order until its status is final,
Packit Service 509fd4
// see the WaitOrder method.
Packit Service 509fd4
func (c *Client) GetOrder(ctx context.Context, url string) (*Order, error) {
Packit Service 509fd4
	if _, err := c.Discover(ctx); err != nil {
Packit Service 509fd4
		return nil, err
Packit Service 509fd4
	}
Packit Service 509fd4
Packit Service 509fd4
	res, err := c.postAsGet(ctx, url, wantStatus(http.StatusOK))
Packit Service 509fd4
	if err != nil {
Packit Service 509fd4
		return nil, err
Packit Service 509fd4
	}
Packit Service 509fd4
	defer res.Body.Close()
Packit Service 509fd4
	return responseOrder(res)
Packit Service 509fd4
}
Packit Service 509fd4
Packit Service 509fd4
// WaitOrder polls an order from the given URL until it is in one of the final states,
Packit Service 509fd4
// StatusReady, StatusValid or StatusInvalid, the CA responded with a non-retryable error
Packit Service 509fd4
// or the context is done.
Packit Service 509fd4
//
Packit Service 509fd4
// It returns a non-nil Order only if its Status is StatusReady or StatusValid.
Packit Service 509fd4
// In all other cases WaitOrder returns an error.
Packit Service 509fd4
// If the Status is StatusInvalid, the returned error is of type *OrderError.
Packit Service 509fd4
func (c *Client) WaitOrder(ctx context.Context, url string) (*Order, error) {
Packit Service 509fd4
	if _, err := c.Discover(ctx); err != nil {
Packit Service 509fd4
		return nil, err
Packit Service 509fd4
	}
Packit Service 509fd4
	for {
Packit Service 509fd4
		res, err := c.postAsGet(ctx, url, wantStatus(http.StatusOK))
Packit Service 509fd4
		if err != nil {
Packit Service 509fd4
			return nil, err
Packit Service 509fd4
		}
Packit Service 509fd4
		o, err := responseOrder(res)
Packit Service 509fd4
		res.Body.Close()
Packit Service 509fd4
		switch {
Packit Service 509fd4
		case err != nil:
Packit Service 509fd4
			// Skip and retry.
Packit Service 509fd4
		case o.Status == StatusInvalid:
Packit Service 509fd4
			return nil, &OrderError{OrderURL: o.URI, Status: o.Status}
Packit Service 509fd4
		case o.Status == StatusReady || o.Status == StatusValid:
Packit Service 509fd4
			return o, nil
Packit Service 509fd4
		}
Packit Service 509fd4
Packit Service 509fd4
		d := retryAfter(res.Header.Get("Retry-After"))
Packit Service 509fd4
		if d == 0 {
Packit Service 509fd4
			// Default retry-after.
Packit Service 509fd4
			// Same reasoning as in WaitAuthorization.
Packit Service 509fd4
			d = time.Second
Packit Service 509fd4
		}
Packit Service 509fd4
		t := time.NewTimer(d)
Packit Service 509fd4
		select {
Packit Service 509fd4
		case <-ctx.Done():
Packit Service 509fd4
			t.Stop()
Packit Service 509fd4
			return nil, ctx.Err()
Packit Service 509fd4
		case <-t.C:
Packit Service 509fd4
			// Retry.
Packit Service 509fd4
		}
Packit Service 509fd4
	}
Packit Service 509fd4
}
Packit Service 509fd4
Packit Service 509fd4
func responseOrder(res *http.Response) (*Order, error) {
Packit Service 509fd4
	var v struct {
Packit Service 509fd4
		Status         string
Packit Service 509fd4
		Expires        time.Time
Packit Service 509fd4
		Identifiers    []wireAuthzID
Packit Service 509fd4
		NotBefore      time.Time
Packit Service 509fd4
		NotAfter       time.Time
Packit Service 509fd4
		Error          *wireError
Packit Service 509fd4
		Authorizations []string
Packit Service 509fd4
		Finalize       string
Packit Service 509fd4
		Certificate    string
Packit Service 509fd4
	}
Packit Service 509fd4
	if err := json.NewDecoder(res.Body).Decode(&v); err != nil {
Packit Service 509fd4
		return nil, fmt.Errorf("acme: error reading order: %v", err)
Packit Service 509fd4
	}
Packit Service 509fd4
	o := &Order{
Packit Service 509fd4
		URI:         res.Header.Get("Location"),
Packit Service 509fd4
		Status:      v.Status,
Packit Service 509fd4
		Expires:     v.Expires,
Packit Service 509fd4
		NotBefore:   v.NotBefore,
Packit Service 509fd4
		NotAfter:    v.NotAfter,
Packit Service 509fd4
		AuthzURLs:   v.Authorizations,
Packit Service 509fd4
		FinalizeURL: v.Finalize,
Packit Service 509fd4
		CertURL:     v.Certificate,
Packit Service 509fd4
	}
Packit Service 509fd4
	for _, id := range v.Identifiers {
Packit Service 509fd4
		o.Identifiers = append(o.Identifiers, AuthzID{Type: id.Type, Value: id.Value})
Packit Service 509fd4
	}
Packit Service 509fd4
	if v.Error != nil {
Packit Service 509fd4
		o.Error = v.Error.error(nil /* headers */)
Packit Service 509fd4
	}
Packit Service 509fd4
	return o, nil
Packit Service 509fd4
}
Packit Service 509fd4
Packit Service 509fd4
// CreateOrderCert submits the CSR (Certificate Signing Request) to a CA at the specified URL.
Packit Service 509fd4
// The URL is the FinalizeURL field of an Order created with AuthorizeOrder.
Packit Service 509fd4
//
Packit Service 509fd4
// If the bundle argument is true, the returned value also contain the CA (issuer)
Packit Service 509fd4
// certificate chain. Otherwise, only a leaf certificate is returned.
Packit Service 509fd4
// The returned URL can be used to re-fetch the certificate using FetchCert.
Packit Service 509fd4
//
Packit Service 509fd4
// This method is only supported by CAs implementing RFC 8555. See CreateCert for pre-RFC CAs.
Packit Service 509fd4
//
Packit Service 509fd4
// CreateOrderCert returns an error if the CA's response is unreasonably large.
Packit Service 509fd4
// Callers are encouraged to parse the returned value to ensure the certificate is valid and has the expected features.
Packit Service 509fd4
func (c *Client) CreateOrderCert(ctx context.Context, url string, csr []byte, bundle bool) (der [][]byte, certURL string, err error) {
Packit Service 509fd4
	if _, err := c.Discover(ctx); err != nil { // required by c.accountKID
Packit Service 509fd4
		return nil, "", err
Packit Service 509fd4
	}
Packit Service 509fd4
Packit Service 509fd4
	// RFC describes this as "finalize order" request.
Packit Service 509fd4
	req := struct {
Packit Service 509fd4
		CSR string `json:"csr"`
Packit Service 509fd4
	}{
Packit Service 509fd4
		CSR: base64.RawURLEncoding.EncodeToString(csr),
Packit Service 509fd4
	}
Packit Service 509fd4
	res, err := c.post(ctx, nil, url, req, wantStatus(http.StatusOK))
Packit Service 509fd4
	if err != nil {
Packit Service 509fd4
		return nil, "", err
Packit Service 509fd4
	}
Packit Service 509fd4
	defer res.Body.Close()
Packit Service 509fd4
	o, err := responseOrder(res)
Packit Service 509fd4
	if err != nil {
Packit Service 509fd4
		return nil, "", err
Packit Service 509fd4
	}
Packit Service 509fd4
Packit Service 509fd4
	// Wait for CA to issue the cert if they haven't.
Packit Service 509fd4
	if o.Status != StatusValid {
Packit Service 509fd4
		o, err = c.WaitOrder(ctx, o.URI)
Packit Service 509fd4
	}
Packit Service 509fd4
	if err != nil {
Packit Service 509fd4
		return nil, "", err
Packit Service 509fd4
	}
Packit Service 509fd4
	// The only acceptable status post finalize and WaitOrder is "valid".
Packit Service 509fd4
	if o.Status != StatusValid {
Packit Service 509fd4
		return nil, "", &OrderError{OrderURL: o.URI, Status: o.Status}
Packit Service 509fd4
	}
Packit Service 509fd4
	crt, err := c.fetchCertRFC(ctx, o.CertURL, bundle)
Packit Service 509fd4
	return crt, o.CertURL, err
Packit Service 509fd4
}
Packit Service 509fd4
Packit Service 509fd4
// fetchCertRFC downloads issued certificate from the given URL.
Packit Service 509fd4
// It expects the CA to respond with PEM-encoded certificate chain.
Packit Service 509fd4
//
Packit Service 509fd4
// The URL argument is the CertURL field of Order.
Packit Service 509fd4
func (c *Client) fetchCertRFC(ctx context.Context, url string, bundle bool) ([][]byte, error) {
Packit Service 509fd4
	res, err := c.postAsGet(ctx, url, wantStatus(http.StatusOK))
Packit Service 509fd4
	if err != nil {
Packit Service 509fd4
		return nil, err
Packit Service 509fd4
	}
Packit Service 509fd4
	defer res.Body.Close()
Packit Service 509fd4
Packit Service 509fd4
	// Get all the bytes up to a sane maximum.
Packit Service 509fd4
	// Account very roughly for base64 overhead.
Packit Service 509fd4
	const max = maxCertChainSize + maxCertChainSize/33
Packit Service 509fd4
	b, err := ioutil.ReadAll(io.LimitReader(res.Body, max+1))
Packit Service 509fd4
	if err != nil {
Packit Service 509fd4
		return nil, fmt.Errorf("acme: fetch cert response stream: %v", err)
Packit Service 509fd4
	}
Packit Service 509fd4
	if len(b) > max {
Packit Service 509fd4
		return nil, errors.New("acme: certificate chain is too big")
Packit Service 509fd4
	}
Packit Service 509fd4
Packit Service 509fd4
	// Decode PEM chain.
Packit Service 509fd4
	var chain [][]byte
Packit Service 509fd4
	for {
Packit Service 509fd4
		var p *pem.Block
Packit Service 509fd4
		p, b = pem.Decode(b)
Packit Service 509fd4
		if p == nil {
Packit Service 509fd4
			break
Packit Service 509fd4
		}
Packit Service 509fd4
		if p.Type != "CERTIFICATE" {
Packit Service 509fd4
			return nil, fmt.Errorf("acme: invalid PEM cert type %q", p.Type)
Packit Service 509fd4
		}
Packit Service 509fd4
Packit Service 509fd4
		chain = append(chain, p.Bytes)
Packit Service 509fd4
		if !bundle {
Packit Service 509fd4
			return chain, nil
Packit Service 509fd4
		}
Packit Service 509fd4
		if len(chain) > maxChainLen {
Packit Service 509fd4
			return nil, errors.New("acme: certificate chain is too long")
Packit Service 509fd4
		}
Packit Service 509fd4
	}
Packit Service 509fd4
	if len(chain) == 0 {
Packit Service 509fd4
		return nil, errors.New("acme: certificate chain is empty")
Packit Service 509fd4
	}
Packit Service 509fd4
	return chain, nil
Packit Service 509fd4
}
Packit Service 509fd4
Packit Service 509fd4
// sends a cert revocation request in either JWK form when key is non-nil or KID form otherwise.
Packit Service 509fd4
func (c *Client) revokeCertRFC(ctx context.Context, key crypto.Signer, cert []byte, reason CRLReasonCode) error {
Packit Service 509fd4
	req := &struct {
Packit Service 509fd4
		Cert   string `json:"certificate"`
Packit Service 509fd4
		Reason int    `json:"reason"`
Packit Service 509fd4
	}{
Packit Service 509fd4
		Cert:   base64.RawURLEncoding.EncodeToString(cert),
Packit Service 509fd4
		Reason: int(reason),
Packit Service 509fd4
	}
Packit Service 509fd4
	res, err := c.post(ctx, key, c.dir.RevokeURL, req, wantStatus(http.StatusOK))
Packit Service 509fd4
	if err != nil {
Packit Service 509fd4
		if isAlreadyRevoked(err) {
Packit Service 509fd4
			// Assume it is not an error to revoke an already revoked cert.
Packit Service 509fd4
			return nil
Packit Service 509fd4
		}
Packit Service 509fd4
		return err
Packit Service 509fd4
	}
Packit Service 509fd4
	defer res.Body.Close()
Packit Service 509fd4
	return nil
Packit Service 509fd4
}
Packit Service 509fd4
Packit Service 509fd4
func isAlreadyRevoked(err error) bool {
Packit Service 509fd4
	e, ok := err.(*Error)
Packit Service 509fd4
	return ok && e.ProblemType == "urn:ietf:params:acme:error:alreadyRevoked"
Packit Service 509fd4
}