forked from ebhomengo/niki
1
0
Fork 0
niki/vendor/golang.org/x/crypto/acme/acme.go

1459 lines
27 KiB
Go
Raw Normal View History

2024-02-18 10:42:21 +00:00
// Copyright 2015 The Go Authors. All rights reserved.
2024-02-18 10:42:21 +00:00
// Use of this source code is governed by a BSD-style
2024-02-18 10:42:21 +00:00
// license that can be found in the LICENSE file.
// Package acme provides an implementation of the
2024-02-18 10:42:21 +00:00
// Automatic Certificate Management Environment (ACME) spec,
2024-02-18 10:42:21 +00:00
// most famously used by Let's Encrypt.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// The initial implementation of this package was based on an early version
2024-02-18 10:42:21 +00:00
// of the spec. The current implementation supports only the modern
2024-02-18 10:42:21 +00:00
// RFC 8555 but some of the old API surface remains for compatibility.
2024-02-18 10:42:21 +00:00
// While code using the old API will still compile, it will return an error.
2024-02-18 10:42:21 +00:00
// Note the deprecation comments to update your code.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// See https://tools.ietf.org/html/rfc8555 for the spec.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// Most common scenarios will want to use autocert subdirectory instead,
2024-02-18 10:42:21 +00:00
// which provides automatic access to certificates from Let's Encrypt
2024-02-18 10:42:21 +00:00
// and any other ACME-based CA.
2024-02-18 10:42:21 +00:00
package acme
import (
"context"
"crypto"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/sha256"
"crypto/tls"
"crypto/x509"
"crypto/x509/pkix"
"encoding/asn1"
"encoding/base64"
"encoding/hex"
"encoding/json"
"encoding/pem"
"errors"
"fmt"
"math/big"
"net/http"
"strings"
"sync"
"time"
)
const (
2024-02-18 10:42:21 +00:00
// LetsEncryptURL is the Directory endpoint of Let's Encrypt CA.
2024-02-18 10:42:21 +00:00
LetsEncryptURL = "https://acme-v02.api.letsencrypt.org/directory"
// ALPNProto is the ALPN protocol name used by a CA server when validating
2024-02-18 10:42:21 +00:00
// tls-alpn-01 challenges.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// Package users must ensure their servers can negotiate the ACME ALPN in
2024-02-18 10:42:21 +00:00
// order for tls-alpn-01 challenge verifications to succeed.
2024-02-18 10:42:21 +00:00
// See the crypto/tls package's Config.NextProtos field.
2024-02-18 10:42:21 +00:00
ALPNProto = "acme-tls/1"
)
// idPeACMEIdentifier is the OID for the ACME extension for the TLS-ALPN challenge.
2024-02-18 10:42:21 +00:00
// https://tools.ietf.org/html/draft-ietf-acme-tls-alpn-05#section-5.1
2024-02-18 10:42:21 +00:00
var idPeACMEIdentifier = asn1.ObjectIdentifier{1, 3, 6, 1, 5, 5, 7, 1, 31}
const (
maxChainLen = 5 // max depth and breadth of a certificate chain
2024-02-18 10:42:21 +00:00
maxCertSize = 1 << 20 // max size of a certificate, in DER bytes
2024-02-18 10:42:21 +00:00
// Used for decoding certs from application/pem-certificate-chain response,
2024-02-18 10:42:21 +00:00
// the default when in RFC mode.
2024-02-18 10:42:21 +00:00
maxCertChainSize = maxCertSize * maxChainLen
// Max number of collected nonces kept in memory.
2024-02-18 10:42:21 +00:00
// Expect usual peak of 1 or 2.
2024-02-18 10:42:21 +00:00
maxNonces = 100
)
// Client is an ACME client.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// The only required field is Key. An example of creating a client with a new key
2024-02-18 10:42:21 +00:00
// is as follows:
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// key, err := rsa.GenerateKey(rand.Reader, 2048)
2024-02-18 10:42:21 +00:00
// if err != nil {
2024-02-18 10:42:21 +00:00
// log.Fatal(err)
2024-02-18 10:42:21 +00:00
// }
2024-02-18 10:42:21 +00:00
// client := &Client{Key: key}
2024-02-18 10:42:21 +00:00
type Client struct {
2024-02-18 10:42:21 +00:00
// Key is the account key used to register with a CA and sign requests.
2024-02-18 10:42:21 +00:00
// Key.Public() must return a *rsa.PublicKey or *ecdsa.PublicKey.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// The following algorithms are supported:
2024-02-18 10:42:21 +00:00
// RS256, ES256, ES384 and ES512.
2024-02-18 10:42:21 +00:00
// See RFC 7518 for more details about the algorithms.
2024-02-18 10:42:21 +00:00
Key crypto.Signer
// HTTPClient optionally specifies an HTTP client to use
2024-02-18 10:42:21 +00:00
// instead of http.DefaultClient.
2024-02-18 10:42:21 +00:00
HTTPClient *http.Client
// DirectoryURL points to the CA directory endpoint.
2024-02-18 10:42:21 +00:00
// If empty, LetsEncryptURL is used.
2024-02-18 10:42:21 +00:00
// Mutating this value after a successful call of Client's Discover method
2024-02-18 10:42:21 +00:00
// will have no effect.
2024-02-18 10:42:21 +00:00
DirectoryURL string
// RetryBackoff computes the duration after which the nth retry of a failed request
2024-02-18 10:42:21 +00:00
// should occur. The value of n for the first call on failure is 1.
2024-02-18 10:42:21 +00:00
// The values of r and resp are the request and response of the last failed attempt.
2024-02-18 10:42:21 +00:00
// If the returned value is negative or zero, no more retries are done and an error
2024-02-18 10:42:21 +00:00
// is returned to the caller of the original method.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// Requests which result in a 4xx client error are not retried,
2024-02-18 10:42:21 +00:00
// except for 400 Bad Request due to "bad nonce" errors and 429 Too Many Requests.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// If RetryBackoff is nil, a truncated exponential backoff algorithm
2024-02-18 10:42:21 +00:00
// with the ceiling of 10 seconds is used, where each subsequent retry n
2024-02-18 10:42:21 +00:00
// is done after either ("Retry-After" + jitter) or (2^n seconds + jitter),
2024-02-18 10:42:21 +00:00
// preferring the former if "Retry-After" header is found in the resp.
2024-02-18 10:42:21 +00:00
// The jitter is a random value up to 1 second.
2024-02-18 10:42:21 +00:00
RetryBackoff func(n int, r *http.Request, resp *http.Response) time.Duration
// UserAgent is prepended to the User-Agent header sent to the ACME server,
2024-02-18 10:42:21 +00:00
// which by default is this package's name and version.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// Reusable libraries and tools in particular should set this value to be
2024-02-18 10:42:21 +00:00
// identifiable by the server, in case they are causing issues.
2024-02-18 10:42:21 +00:00
UserAgent string
cacheMu sync.Mutex
dir *Directory // cached result of Client's Discover method
2024-02-18 10:42:21 +00:00
// KID is the key identifier provided by the CA. If not provided it will be
2024-02-18 10:42:21 +00:00
// retrieved from the CA by making a call to the registration endpoint.
2024-02-18 10:42:21 +00:00
KID KeyID
noncesMu sync.Mutex
nonces map[string]struct{} // nonces collected from previous responses
2024-02-18 10:42:21 +00:00
}
// accountKID returns a key ID associated with c.Key, the account identity
2024-02-18 10:42:21 +00:00
// provided by the CA during RFC based registration.
2024-02-18 10:42:21 +00:00
// It assumes c.Discover has already been called.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// accountKID requires at most one network roundtrip.
2024-02-18 10:42:21 +00:00
// It caches only successful result.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// When in pre-RFC mode or when c.getRegRFC responds with an error, accountKID
2024-02-18 10:42:21 +00:00
// returns noKeyID.
2024-02-18 10:42:21 +00:00
func (c *Client) accountKID(ctx context.Context) KeyID {
2024-02-18 10:42:21 +00:00
c.cacheMu.Lock()
2024-02-18 10:42:21 +00:00
defer c.cacheMu.Unlock()
2024-02-18 10:42:21 +00:00
if c.KID != noKeyID {
2024-02-18 10:42:21 +00:00
return c.KID
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
a, err := c.getRegRFC(ctx)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return noKeyID
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
c.KID = KeyID(a.URI)
2024-02-18 10:42:21 +00:00
return c.KID
2024-02-18 10:42:21 +00:00
}
var errPreRFC = errors.New("acme: server does not support the RFC 8555 version of ACME")
// Discover performs ACME server discovery using c.DirectoryURL.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// It caches successful result. So, subsequent calls will not result in
2024-02-18 10:42:21 +00:00
// a network round-trip. This also means mutating c.DirectoryURL after successful call
2024-02-18 10:42:21 +00:00
// of this method will have no effect.
2024-02-18 10:42:21 +00:00
func (c *Client) Discover(ctx context.Context) (Directory, error) {
2024-02-18 10:42:21 +00:00
c.cacheMu.Lock()
2024-02-18 10:42:21 +00:00
defer c.cacheMu.Unlock()
2024-02-18 10:42:21 +00:00
if c.dir != nil {
2024-02-18 10:42:21 +00:00
return *c.dir, nil
2024-02-18 10:42:21 +00:00
}
res, err := c.get(ctx, c.directoryURL(), wantStatus(http.StatusOK))
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return Directory{}, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
defer res.Body.Close()
2024-02-18 10:42:21 +00:00
c.addNonce(res.Header)
var v struct {
Reg string `json:"newAccount"`
Authz string `json:"newAuthz"`
Order string `json:"newOrder"`
Revoke string `json:"revokeCert"`
Nonce string `json:"newNonce"`
2024-02-18 10:42:21 +00:00
KeyChange string `json:"keyChange"`
Meta struct {
Terms string `json:"termsOfService"`
Website string `json:"website"`
CAA []string `json:"caaIdentities"`
ExternalAcct bool `json:"externalAccountRequired"`
2024-02-18 10:42:21 +00:00
}
}
2024-02-18 10:42:21 +00:00
if err := json.NewDecoder(res.Body).Decode(&v); err != nil {
2024-02-18 10:42:21 +00:00
return Directory{}, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if v.Order == "" {
2024-02-18 10:42:21 +00:00
return Directory{}, errPreRFC
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
c.dir = &Directory{
RegURL: v.Reg,
AuthzURL: v.Authz,
OrderURL: v.Order,
RevokeURL: v.Revoke,
NonceURL: v.Nonce,
KeyChangeURL: v.KeyChange,
Terms: v.Meta.Terms,
Website: v.Meta.Website,
CAA: v.Meta.CAA,
2024-02-18 10:42:21 +00:00
ExternalAccountRequired: v.Meta.ExternalAcct,
}
2024-02-18 10:42:21 +00:00
return *c.dir, nil
2024-02-18 10:42:21 +00:00
}
func (c *Client) directoryURL() string {
2024-02-18 10:42:21 +00:00
if c.DirectoryURL != "" {
2024-02-18 10:42:21 +00:00
return c.DirectoryURL
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return LetsEncryptURL
2024-02-18 10:42:21 +00:00
}
// CreateCert was part of the old version of ACME. It is incompatible with RFC 8555.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// Deprecated: this was for the pre-RFC 8555 version of ACME. Callers should use CreateOrderCert.
2024-02-18 10:42:21 +00:00
func (c *Client) CreateCert(ctx context.Context, csr []byte, exp time.Duration, bundle bool) (der [][]byte, certURL string, err error) {
2024-02-18 10:42:21 +00:00
return nil, "", errPreRFC
2024-02-18 10:42:21 +00:00
}
// FetchCert retrieves already issued certificate from the given url, in DER format.
2024-02-18 10:42:21 +00:00
// It retries the request until the certificate is successfully retrieved,
2024-02-18 10:42:21 +00:00
// context is cancelled by the caller or an error response is received.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// If the bundle argument is true, the returned value also contains the CA (issuer)
2024-02-18 10:42:21 +00:00
// certificate chain.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// FetchCert returns an error if the CA's response or chain was unreasonably large.
2024-02-18 10:42:21 +00:00
// Callers are encouraged to parse the returned value to ensure the certificate is valid
2024-02-18 10:42:21 +00:00
// and has expected features.
2024-02-18 10:42:21 +00:00
func (c *Client) FetchCert(ctx context.Context, url string, bundle bool) ([][]byte, error) {
2024-02-18 10:42:21 +00:00
if _, err := c.Discover(ctx); err != nil {
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return c.fetchCertRFC(ctx, url, bundle)
2024-02-18 10:42:21 +00:00
}
// RevokeCert revokes a previously issued certificate cert, provided in DER format.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// The key argument, used to sign the request, must be authorized
2024-02-18 10:42:21 +00:00
// to revoke the certificate. It's up to the CA to decide which keys are authorized.
2024-02-18 10:42:21 +00:00
// For instance, the key pair of the certificate may be authorized.
2024-02-18 10:42:21 +00:00
// If the key is nil, c.Key is used instead.
2024-02-18 10:42:21 +00:00
func (c *Client) RevokeCert(ctx context.Context, key crypto.Signer, cert []byte, reason CRLReasonCode) error {
2024-02-18 10:42:21 +00:00
if _, err := c.Discover(ctx); err != nil {
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return c.revokeCertRFC(ctx, key, cert, reason)
2024-02-18 10:42:21 +00:00
}
// AcceptTOS always returns true to indicate the acceptance of a CA's Terms of Service
2024-02-18 10:42:21 +00:00
// during account registration. See Register method of Client for more details.
2024-02-18 10:42:21 +00:00
func AcceptTOS(tosURL string) bool { return true }
// Register creates a new account with the CA using c.Key.
2024-02-18 10:42:21 +00:00
// It returns the registered account. The account acct is not modified.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// The registration may require the caller to agree to the CA's Terms of Service (TOS).
2024-02-18 10:42:21 +00:00
// If so, and the account has not indicated the acceptance of the terms (see Account for details),
2024-02-18 10:42:21 +00:00
// Register calls prompt with a TOS URL provided by the CA. Prompt should report
2024-02-18 10:42:21 +00:00
// whether the caller agrees to the terms. To always accept the terms, the caller can use AcceptTOS.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// When interfacing with an RFC-compliant CA, non-RFC 8555 fields of acct are ignored
2024-02-18 10:42:21 +00:00
// and prompt is called if Directory's Terms field is non-zero.
2024-02-18 10:42:21 +00:00
// Also see Error's Instance field for when a CA requires already registered accounts to agree
2024-02-18 10:42:21 +00:00
// to an updated Terms of Service.
2024-02-18 10:42:21 +00:00
func (c *Client) Register(ctx context.Context, acct *Account, prompt func(tosURL string) bool) (*Account, error) {
2024-02-18 10:42:21 +00:00
if c.Key == nil {
2024-02-18 10:42:21 +00:00
return nil, errors.New("acme: client.Key must be set to Register")
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if _, err := c.Discover(ctx); err != nil {
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return c.registerRFC(ctx, acct, prompt)
2024-02-18 10:42:21 +00:00
}
// GetReg retrieves an existing account associated with c.Key.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// The url argument is a legacy artifact of the pre-RFC 8555 API
2024-02-18 10:42:21 +00:00
// and is ignored.
2024-02-18 10:42:21 +00:00
func (c *Client) GetReg(ctx context.Context, url string) (*Account, error) {
2024-02-18 10:42:21 +00:00
if _, err := c.Discover(ctx); err != nil {
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return c.getRegRFC(ctx)
2024-02-18 10:42:21 +00:00
}
// UpdateReg updates an existing registration.
2024-02-18 10:42:21 +00:00
// It returns an updated account copy. The provided account is not modified.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// The account's URI is ignored and the account URL associated with
2024-02-18 10:42:21 +00:00
// c.Key is used instead.
2024-02-18 10:42:21 +00:00
func (c *Client) UpdateReg(ctx context.Context, acct *Account) (*Account, error) {
2024-02-18 10:42:21 +00:00
if _, err := c.Discover(ctx); err != nil {
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return c.updateRegRFC(ctx, acct)
2024-02-18 10:42:21 +00:00
}
// AccountKeyRollover attempts to transition a client's account key to a new key.
2024-02-18 10:42:21 +00:00
// On success client's Key is updated which is not concurrency safe.
2024-02-18 10:42:21 +00:00
// On failure an error will be returned.
2024-02-18 10:42:21 +00:00
// The new key is already registered with the ACME provider if the following is true:
2024-02-18 10:42:21 +00:00
// - error is of type acme.Error
2024-02-18 10:42:21 +00:00
// - StatusCode should be 409 (Conflict)
2024-02-18 10:42:21 +00:00
// - Location header will have the KID of the associated account
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// More about account key rollover can be found at
2024-02-18 10:42:21 +00:00
// https://tools.ietf.org/html/rfc8555#section-7.3.5.
2024-02-18 10:42:21 +00:00
func (c *Client) AccountKeyRollover(ctx context.Context, newKey crypto.Signer) error {
2024-02-18 10:42:21 +00:00
return c.accountKeyRollover(ctx, newKey)
2024-02-18 10:42:21 +00:00
}
// Authorize performs the initial step in the pre-authorization flow,
2024-02-18 10:42:21 +00:00
// as opposed to order-based flow.
2024-02-18 10:42:21 +00:00
// The caller will then need to choose from and perform a set of returned
2024-02-18 10:42:21 +00:00
// challenges using c.Accept in order to successfully complete authorization.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// Once complete, the caller can use AuthorizeOrder which the CA
2024-02-18 10:42:21 +00:00
// should provision with the already satisfied authorization.
2024-02-18 10:42:21 +00:00
// For pre-RFC CAs, the caller can proceed directly to requesting a certificate
2024-02-18 10:42:21 +00:00
// using CreateCert method.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// If an authorization has been previously granted, the CA may return
2024-02-18 10:42:21 +00:00
// a valid authorization which has its Status field set to StatusValid.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// More about pre-authorization can be found at
2024-02-18 10:42:21 +00:00
// https://tools.ietf.org/html/rfc8555#section-7.4.1.
2024-02-18 10:42:21 +00:00
func (c *Client) Authorize(ctx context.Context, domain string) (*Authorization, error) {
2024-02-18 10:42:21 +00:00
return c.authorize(ctx, "dns", domain)
2024-02-18 10:42:21 +00:00
}
// AuthorizeIP is the same as Authorize but requests IP address authorization.
2024-02-18 10:42:21 +00:00
// Clients which successfully obtain such authorization may request to issue
2024-02-18 10:42:21 +00:00
// a certificate for IP addresses.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// See the ACME spec extension for more details about IP address identifiers:
2024-02-18 10:42:21 +00:00
// https://tools.ietf.org/html/draft-ietf-acme-ip.
2024-02-18 10:42:21 +00:00
func (c *Client) AuthorizeIP(ctx context.Context, ipaddr string) (*Authorization, error) {
2024-02-18 10:42:21 +00:00
return c.authorize(ctx, "ip", ipaddr)
2024-02-18 10:42:21 +00:00
}
func (c *Client) authorize(ctx context.Context, typ, val string) (*Authorization, error) {
2024-02-18 10:42:21 +00:00
if _, err := c.Discover(ctx); err != nil {
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
type authzID struct {
Type string `json:"type"`
2024-02-18 10:42:21 +00:00
Value string `json:"value"`
}
2024-02-18 10:42:21 +00:00
req := struct {
Resource string `json:"resource"`
2024-02-18 10:42:21 +00:00
Identifier authzID `json:"identifier"`
}{
Resource: "new-authz",
2024-02-18 10:42:21 +00:00
Identifier: authzID{Type: typ, Value: val},
}
2024-02-18 10:42:21 +00:00
res, err := c.post(ctx, nil, c.dir.AuthzURL, req, wantStatus(http.StatusCreated))
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
defer res.Body.Close()
var v wireAuthz
2024-02-18 10:42:21 +00:00
if err := json.NewDecoder(res.Body).Decode(&v); err != nil {
2024-02-18 10:42:21 +00:00
return nil, fmt.Errorf("acme: invalid response: %v", err)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if v.Status != StatusPending && v.Status != StatusValid {
2024-02-18 10:42:21 +00:00
return nil, fmt.Errorf("acme: unexpected status: %s", v.Status)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return v.authorization(res.Header.Get("Location")), nil
2024-02-18 10:42:21 +00:00
}
// GetAuthorization retrieves an authorization identified by the given URL.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// If a caller needs to poll an authorization until its status is final,
2024-02-18 10:42:21 +00:00
// see the WaitAuthorization method.
2024-02-18 10:42:21 +00:00
func (c *Client) GetAuthorization(ctx context.Context, url string) (*Authorization, error) {
2024-02-18 10:42:21 +00:00
if _, err := c.Discover(ctx); err != nil {
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
res, err := c.postAsGet(ctx, url, wantStatus(http.StatusOK))
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
defer res.Body.Close()
2024-02-18 10:42:21 +00:00
var v wireAuthz
2024-02-18 10:42:21 +00:00
if err := json.NewDecoder(res.Body).Decode(&v); err != nil {
2024-02-18 10:42:21 +00:00
return nil, fmt.Errorf("acme: invalid response: %v", err)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return v.authorization(url), nil
2024-02-18 10:42:21 +00:00
}
// RevokeAuthorization relinquishes an existing authorization identified
2024-02-18 10:42:21 +00:00
// by the given URL.
2024-02-18 10:42:21 +00:00
// The url argument is an Authorization.URI value.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// If successful, the caller will be required to obtain a new authorization
2024-02-18 10:42:21 +00:00
// using the Authorize or AuthorizeOrder methods before being able to request
2024-02-18 10:42:21 +00:00
// a new certificate for the domain associated with the authorization.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// It does not revoke existing certificates.
2024-02-18 10:42:21 +00:00
func (c *Client) RevokeAuthorization(ctx context.Context, url string) error {
2024-02-18 10:42:21 +00:00
if _, err := c.Discover(ctx); err != nil {
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
req := struct {
Resource string `json:"resource"`
Status string `json:"status"`
Delete bool `json:"delete"`
2024-02-18 10:42:21 +00:00
}{
2024-02-18 10:42:21 +00:00
Resource: "authz",
Status: "deactivated",
Delete: true,
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
res, err := c.post(ctx, nil, url, req, wantStatus(http.StatusOK))
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
defer res.Body.Close()
2024-02-18 10:42:21 +00:00
return nil
2024-02-18 10:42:21 +00:00
}
// WaitAuthorization polls an authorization at the given URL
2024-02-18 10:42:21 +00:00
// until it is in one of the final states, StatusValid or StatusInvalid,
2024-02-18 10:42:21 +00:00
// the ACME CA responded with a 4xx error code, or the context is done.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// It returns a non-nil Authorization only if its Status is StatusValid.
2024-02-18 10:42:21 +00:00
// In all other cases WaitAuthorization returns an error.
2024-02-18 10:42:21 +00:00
// If the Status is StatusInvalid, the returned error is of type *AuthorizationError.
2024-02-18 10:42:21 +00:00
func (c *Client) WaitAuthorization(ctx context.Context, url string) (*Authorization, error) {
2024-02-18 10:42:21 +00:00
if _, err := c.Discover(ctx); err != nil {
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
for {
2024-02-18 10:42:21 +00:00
res, err := c.postAsGet(ctx, url, wantStatus(http.StatusOK, http.StatusAccepted))
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
var raw wireAuthz
2024-02-18 10:42:21 +00:00
err = json.NewDecoder(res.Body).Decode(&raw)
2024-02-18 10:42:21 +00:00
res.Body.Close()
2024-02-18 10:42:21 +00:00
switch {
2024-02-18 10:42:21 +00:00
case err != nil:
2024-02-18 10:42:21 +00:00
// Skip and retry.
2024-02-18 10:42:21 +00:00
case raw.Status == StatusValid:
2024-02-18 10:42:21 +00:00
return raw.authorization(url), nil
2024-02-18 10:42:21 +00:00
case raw.Status == StatusInvalid:
2024-02-18 10:42:21 +00:00
return nil, raw.error(url)
2024-02-18 10:42:21 +00:00
}
// Exponential backoff is implemented in c.get above.
2024-02-18 10:42:21 +00:00
// This is just to prevent continuously hitting the CA
2024-02-18 10:42:21 +00:00
// while waiting for a final authorization status.
2024-02-18 10:42:21 +00:00
d := retryAfter(res.Header.Get("Retry-After"))
2024-02-18 10:42:21 +00:00
if d == 0 {
2024-02-18 10:42:21 +00:00
// Given that the fastest challenges TLS-SNI and HTTP-01
2024-02-18 10:42:21 +00:00
// require a CA to make at least 1 network round trip
2024-02-18 10:42:21 +00:00
// and most likely persist a challenge state,
2024-02-18 10:42:21 +00:00
// this default delay seems reasonable.
2024-02-18 10:42:21 +00:00
d = time.Second
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
t := time.NewTimer(d)
2024-02-18 10:42:21 +00:00
select {
2024-02-18 10:42:21 +00:00
case <-ctx.Done():
2024-02-18 10:42:21 +00:00
t.Stop()
2024-02-18 10:42:21 +00:00
return nil, ctx.Err()
2024-02-18 10:42:21 +00:00
case <-t.C:
2024-02-18 10:42:21 +00:00
// Retry.
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
// GetChallenge retrieves the current status of an challenge.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// A client typically polls a challenge status using this method.
2024-02-18 10:42:21 +00:00
func (c *Client) GetChallenge(ctx context.Context, url string) (*Challenge, error) {
2024-02-18 10:42:21 +00:00
if _, err := c.Discover(ctx); err != nil {
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
res, err := c.postAsGet(ctx, url, wantStatus(http.StatusOK, http.StatusAccepted))
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
defer res.Body.Close()
2024-02-18 10:42:21 +00:00
v := wireChallenge{URI: url}
2024-02-18 10:42:21 +00:00
if err := json.NewDecoder(res.Body).Decode(&v); err != nil {
2024-02-18 10:42:21 +00:00
return nil, fmt.Errorf("acme: invalid response: %v", err)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return v.challenge(), nil
2024-02-18 10:42:21 +00:00
}
// Accept informs the server that the client accepts one of its challenges
2024-02-18 10:42:21 +00:00
// previously obtained with c.Authorize.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// The server will then perform the validation asynchronously.
2024-02-18 10:42:21 +00:00
func (c *Client) Accept(ctx context.Context, chal *Challenge) (*Challenge, error) {
2024-02-18 10:42:21 +00:00
if _, err := c.Discover(ctx); err != nil {
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
res, err := c.post(ctx, nil, chal.URI, json.RawMessage("{}"), wantStatus(
http.StatusOK, // according to the spec
2024-02-18 10:42:21 +00:00
http.StatusAccepted, // Let's Encrypt: see https://goo.gl/WsJ7VT (acme-divergences.md)
2024-02-18 10:42:21 +00:00
))
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
defer res.Body.Close()
var v wireChallenge
2024-02-18 10:42:21 +00:00
if err := json.NewDecoder(res.Body).Decode(&v); err != nil {
2024-02-18 10:42:21 +00:00
return nil, fmt.Errorf("acme: invalid response: %v", err)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return v.challenge(), nil
2024-02-18 10:42:21 +00:00
}
// DNS01ChallengeRecord returns a DNS record value for a dns-01 challenge response.
2024-02-18 10:42:21 +00:00
// A TXT record containing the returned value must be provisioned under
2024-02-18 10:42:21 +00:00
// "_acme-challenge" name of the domain being validated.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// The token argument is a Challenge.Token value.
2024-02-18 10:42:21 +00:00
func (c *Client) DNS01ChallengeRecord(token string) (string, error) {
2024-02-18 10:42:21 +00:00
ka, err := keyAuth(c.Key.Public(), token)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return "", err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
b := sha256.Sum256([]byte(ka))
2024-02-18 10:42:21 +00:00
return base64.RawURLEncoding.EncodeToString(b[:]), nil
2024-02-18 10:42:21 +00:00
}
// HTTP01ChallengeResponse returns the response for an http-01 challenge.
2024-02-18 10:42:21 +00:00
// Servers should respond with the value to HTTP requests at the URL path
2024-02-18 10:42:21 +00:00
// provided by HTTP01ChallengePath to validate the challenge and prove control
2024-02-18 10:42:21 +00:00
// over a domain name.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// The token argument is a Challenge.Token value.
2024-02-18 10:42:21 +00:00
func (c *Client) HTTP01ChallengeResponse(token string) (string, error) {
2024-02-18 10:42:21 +00:00
return keyAuth(c.Key.Public(), token)
2024-02-18 10:42:21 +00:00
}
// HTTP01ChallengePath returns the URL path at which the response for an http-01 challenge
2024-02-18 10:42:21 +00:00
// should be provided by the servers.
2024-02-18 10:42:21 +00:00
// The response value can be obtained with HTTP01ChallengeResponse.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// The token argument is a Challenge.Token value.
2024-02-18 10:42:21 +00:00
func (c *Client) HTTP01ChallengePath(token string) string {
2024-02-18 10:42:21 +00:00
return "/.well-known/acme-challenge/" + token
2024-02-18 10:42:21 +00:00
}
// TLSSNI01ChallengeCert creates a certificate for TLS-SNI-01 challenge response.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// Deprecated: This challenge type is unused in both draft-02 and RFC versions of the ACME spec.
2024-02-18 10:42:21 +00:00
func (c *Client) TLSSNI01ChallengeCert(token string, opt ...CertOption) (cert tls.Certificate, name string, err error) {
2024-02-18 10:42:21 +00:00
ka, err := keyAuth(c.Key.Public(), token)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return tls.Certificate{}, "", err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
b := sha256.Sum256([]byte(ka))
2024-02-18 10:42:21 +00:00
h := hex.EncodeToString(b[:])
2024-02-18 10:42:21 +00:00
name = fmt.Sprintf("%s.%s.acme.invalid", h[:32], h[32:])
2024-02-18 10:42:21 +00:00
cert, err = tlsChallengeCert([]string{name}, opt)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return tls.Certificate{}, "", err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return cert, name, nil
2024-02-18 10:42:21 +00:00
}
// TLSSNI02ChallengeCert creates a certificate for TLS-SNI-02 challenge response.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// Deprecated: This challenge type is unused in both draft-02 and RFC versions of the ACME spec.
2024-02-18 10:42:21 +00:00
func (c *Client) TLSSNI02ChallengeCert(token string, opt ...CertOption) (cert tls.Certificate, name string, err error) {
2024-02-18 10:42:21 +00:00
b := sha256.Sum256([]byte(token))
2024-02-18 10:42:21 +00:00
h := hex.EncodeToString(b[:])
2024-02-18 10:42:21 +00:00
sanA := fmt.Sprintf("%s.%s.token.acme.invalid", h[:32], h[32:])
ka, err := keyAuth(c.Key.Public(), token)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return tls.Certificate{}, "", err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
b = sha256.Sum256([]byte(ka))
2024-02-18 10:42:21 +00:00
h = hex.EncodeToString(b[:])
2024-02-18 10:42:21 +00:00
sanB := fmt.Sprintf("%s.%s.ka.acme.invalid", h[:32], h[32:])
cert, err = tlsChallengeCert([]string{sanA, sanB}, opt)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return tls.Certificate{}, "", err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return cert, sanA, nil
2024-02-18 10:42:21 +00:00
}
// TLSALPN01ChallengeCert creates a certificate for TLS-ALPN-01 challenge response.
2024-02-18 10:42:21 +00:00
// Servers can present the certificate to validate the challenge and prove control
2024-02-18 10:42:21 +00:00
// over a domain name. For more details on TLS-ALPN-01 see
2024-02-18 10:42:21 +00:00
// https://tools.ietf.org/html/draft-shoemaker-acme-tls-alpn-00#section-3
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// The token argument is a Challenge.Token value.
2024-02-18 10:42:21 +00:00
// If a WithKey option is provided, its private part signs the returned cert,
2024-02-18 10:42:21 +00:00
// and the public part is used to specify the signee.
2024-02-18 10:42:21 +00:00
// If no WithKey option is provided, a new ECDSA key is generated using P-256 curve.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// The returned certificate is valid for the next 24 hours and must be presented only when
2024-02-18 10:42:21 +00:00
// the server name in the TLS ClientHello matches the domain, and the special acme-tls/1 ALPN protocol
2024-02-18 10:42:21 +00:00
// has been specified.
2024-02-18 10:42:21 +00:00
func (c *Client) TLSALPN01ChallengeCert(token, domain string, opt ...CertOption) (cert tls.Certificate, err error) {
2024-02-18 10:42:21 +00:00
ka, err := keyAuth(c.Key.Public(), token)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return tls.Certificate{}, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
shasum := sha256.Sum256([]byte(ka))
2024-02-18 10:42:21 +00:00
extValue, err := asn1.Marshal(shasum[:])
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return tls.Certificate{}, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
acmeExtension := pkix.Extension{
Id: idPeACMEIdentifier,
2024-02-18 10:42:21 +00:00
Critical: true,
Value: extValue,
2024-02-18 10:42:21 +00:00
}
tmpl := defaultTLSChallengeCertTemplate()
var newOpt []CertOption
2024-02-18 10:42:21 +00:00
for _, o := range opt {
2024-02-18 10:42:21 +00:00
switch o := o.(type) {
2024-02-18 10:42:21 +00:00
case *certOptTemplate:
2024-02-18 10:42:21 +00:00
t := *(*x509.Certificate)(o) // shallow copy is ok
2024-02-18 10:42:21 +00:00
tmpl = &t
2024-02-18 10:42:21 +00:00
default:
2024-02-18 10:42:21 +00:00
newOpt = append(newOpt, o)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
tmpl.ExtraExtensions = append(tmpl.ExtraExtensions, acmeExtension)
2024-02-18 10:42:21 +00:00
newOpt = append(newOpt, WithTemplate(tmpl))
2024-02-18 10:42:21 +00:00
return tlsChallengeCert([]string{domain}, newOpt)
2024-02-18 10:42:21 +00:00
}
// popNonce returns a nonce value previously stored with c.addNonce
2024-02-18 10:42:21 +00:00
// or fetches a fresh one from c.dir.NonceURL.
2024-02-18 10:42:21 +00:00
// If NonceURL is empty, it first tries c.directoryURL() and, failing that,
2024-02-18 10:42:21 +00:00
// the provided url.
2024-02-18 10:42:21 +00:00
func (c *Client) popNonce(ctx context.Context, url string) (string, error) {
2024-02-18 10:42:21 +00:00
c.noncesMu.Lock()
2024-02-18 10:42:21 +00:00
defer c.noncesMu.Unlock()
2024-02-18 10:42:21 +00:00
if len(c.nonces) == 0 {
2024-02-18 10:42:21 +00:00
if c.dir != nil && c.dir.NonceURL != "" {
2024-02-18 10:42:21 +00:00
return c.fetchNonce(ctx, c.dir.NonceURL)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
dirURL := c.directoryURL()
2024-02-18 10:42:21 +00:00
v, err := c.fetchNonce(ctx, dirURL)
2024-02-18 10:42:21 +00:00
if err != nil && url != dirURL {
2024-02-18 10:42:21 +00:00
v, err = c.fetchNonce(ctx, url)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return v, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
var nonce string
2024-02-18 10:42:21 +00:00
for nonce = range c.nonces {
2024-02-18 10:42:21 +00:00
delete(c.nonces, nonce)
2024-02-18 10:42:21 +00:00
break
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return nonce, nil
2024-02-18 10:42:21 +00:00
}
// clearNonces clears any stored nonces
2024-02-18 10:42:21 +00:00
func (c *Client) clearNonces() {
2024-02-18 10:42:21 +00:00
c.noncesMu.Lock()
2024-02-18 10:42:21 +00:00
defer c.noncesMu.Unlock()
2024-02-18 10:42:21 +00:00
c.nonces = make(map[string]struct{})
2024-02-18 10:42:21 +00:00
}
// addNonce stores a nonce value found in h (if any) for future use.
2024-02-18 10:42:21 +00:00
func (c *Client) addNonce(h http.Header) {
2024-02-18 10:42:21 +00:00
v := nonceFromHeader(h)
2024-02-18 10:42:21 +00:00
if v == "" {
2024-02-18 10:42:21 +00:00
return
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
c.noncesMu.Lock()
2024-02-18 10:42:21 +00:00
defer c.noncesMu.Unlock()
2024-02-18 10:42:21 +00:00
if len(c.nonces) >= maxNonces {
2024-02-18 10:42:21 +00:00
return
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if c.nonces == nil {
2024-02-18 10:42:21 +00:00
c.nonces = make(map[string]struct{})
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
c.nonces[v] = struct{}{}
2024-02-18 10:42:21 +00:00
}
func (c *Client) fetchNonce(ctx context.Context, url string) (string, error) {
2024-02-18 10:42:21 +00:00
r, err := http.NewRequest("HEAD", url, nil)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return "", err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
resp, err := c.doNoRetry(ctx, r)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return "", err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
defer resp.Body.Close()
2024-02-18 10:42:21 +00:00
nonce := nonceFromHeader(resp.Header)
2024-02-18 10:42:21 +00:00
if nonce == "" {
2024-02-18 10:42:21 +00:00
if resp.StatusCode > 299 {
2024-02-18 10:42:21 +00:00
return "", responseError(resp)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return "", errors.New("acme: nonce not found")
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return nonce, nil
2024-02-18 10:42:21 +00:00
}
func nonceFromHeader(h http.Header) string {
2024-02-18 10:42:21 +00:00
return h.Get("Replay-Nonce")
2024-02-18 10:42:21 +00:00
}
// linkHeader returns URI-Reference values of all Link headers
2024-02-18 10:42:21 +00:00
// with relation-type rel.
2024-02-18 10:42:21 +00:00
// See https://tools.ietf.org/html/rfc5988#section-5 for details.
2024-02-18 10:42:21 +00:00
func linkHeader(h http.Header, rel string) []string {
2024-02-18 10:42:21 +00:00
var links []string
2024-02-18 10:42:21 +00:00
for _, v := range h["Link"] {
2024-02-18 10:42:21 +00:00
parts := strings.Split(v, ";")
2024-02-18 10:42:21 +00:00
for _, p := range parts {
2024-02-18 10:42:21 +00:00
p = strings.TrimSpace(p)
2024-02-18 10:42:21 +00:00
if !strings.HasPrefix(p, "rel=") {
2024-02-18 10:42:21 +00:00
continue
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if v := strings.Trim(p[4:], `"`); v == rel {
2024-02-18 10:42:21 +00:00
links = append(links, strings.Trim(parts[0], "<>"))
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return links
2024-02-18 10:42:21 +00:00
}
// keyAuth generates a key authorization string for a given token.
2024-02-18 10:42:21 +00:00
func keyAuth(pub crypto.PublicKey, token string) (string, error) {
2024-02-18 10:42:21 +00:00
th, err := JWKThumbprint(pub)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return "", err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return fmt.Sprintf("%s.%s", token, th), nil
2024-02-18 10:42:21 +00:00
}
// defaultTLSChallengeCertTemplate is a template used to create challenge certs for TLS challenges.
2024-02-18 10:42:21 +00:00
func defaultTLSChallengeCertTemplate() *x509.Certificate {
2024-02-18 10:42:21 +00:00
return &x509.Certificate{
SerialNumber: big.NewInt(1),
NotBefore: time.Now(),
NotAfter: time.Now().Add(24 * time.Hour),
2024-02-18 10:42:21 +00:00
BasicConstraintsValid: true,
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
// tlsChallengeCert creates a temporary certificate for TLS-SNI challenges
2024-02-18 10:42:21 +00:00
// with the given SANs and auto-generated public/private key pair.
2024-02-18 10:42:21 +00:00
// The Subject Common Name is set to the first SAN to aid debugging.
2024-02-18 10:42:21 +00:00
// To create a cert with a custom key pair, specify WithKey option.
2024-02-18 10:42:21 +00:00
func tlsChallengeCert(san []string, opt []CertOption) (tls.Certificate, error) {
2024-02-18 10:42:21 +00:00
var key crypto.Signer
2024-02-18 10:42:21 +00:00
tmpl := defaultTLSChallengeCertTemplate()
2024-02-18 10:42:21 +00:00
for _, o := range opt {
2024-02-18 10:42:21 +00:00
switch o := o.(type) {
2024-02-18 10:42:21 +00:00
case *certOptKey:
2024-02-18 10:42:21 +00:00
if key != nil {
2024-02-18 10:42:21 +00:00
return tls.Certificate{}, errors.New("acme: duplicate key option")
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
key = o.key
2024-02-18 10:42:21 +00:00
case *certOptTemplate:
2024-02-18 10:42:21 +00:00
t := *(*x509.Certificate)(o) // shallow copy is ok
2024-02-18 10:42:21 +00:00
tmpl = &t
2024-02-18 10:42:21 +00:00
default:
2024-02-18 10:42:21 +00:00
// package's fault, if we let this happen:
2024-02-18 10:42:21 +00:00
panic(fmt.Sprintf("unsupported option type %T", o))
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if key == nil {
2024-02-18 10:42:21 +00:00
var err error
2024-02-18 10:42:21 +00:00
if key, err = ecdsa.GenerateKey(elliptic.P256(), rand.Reader); err != nil {
2024-02-18 10:42:21 +00:00
return tls.Certificate{}, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
tmpl.DNSNames = san
2024-02-18 10:42:21 +00:00
if len(san) > 0 {
2024-02-18 10:42:21 +00:00
tmpl.Subject.CommonName = san[0]
2024-02-18 10:42:21 +00:00
}
der, err := x509.CreateCertificate(rand.Reader, tmpl, tmpl, key.Public(), key)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return tls.Certificate{}, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return tls.Certificate{
2024-02-18 10:42:21 +00:00
Certificate: [][]byte{der},
PrivateKey: key,
2024-02-18 10:42:21 +00:00
}, nil
2024-02-18 10:42:21 +00:00
}
// encodePEM returns b encoded as PEM with block of type typ.
2024-02-18 10:42:21 +00:00
func encodePEM(typ string, b []byte) []byte {
2024-02-18 10:42:21 +00:00
pb := &pem.Block{Type: typ, Bytes: b}
2024-02-18 10:42:21 +00:00
return pem.EncodeToMemory(pb)
2024-02-18 10:42:21 +00:00
}
// timeNow is time.Now, except in tests which can mess with it.
2024-02-18 10:42:21 +00:00
var timeNow = time.Now