2016-02-08 00:59:03 +00:00
|
|
|
// Package acme implements the ACME protocol for Let's Encrypt and other conforming providers.
|
2015-06-08 00:36:07 +00:00
|
|
|
package acme
|
|
|
|
|
|
|
|
import (
|
2016-01-08 09:14:41 +00:00
|
|
|
"crypto"
|
2015-10-18 22:42:04 +00:00
|
|
|
"crypto/x509"
|
2015-06-13 01:55:53 +00:00
|
|
|
"encoding/base64"
|
2018-07-01 00:35:39 +00:00
|
|
|
"encoding/pem"
|
2015-06-08 00:36:07 +00:00
|
|
|
"errors"
|
2015-06-08 21:54:15 +00:00
|
|
|
"fmt"
|
2015-06-08 00:36:07 +00:00
|
|
|
"io/ioutil"
|
2016-01-08 07:04:38 +00:00
|
|
|
"net"
|
2015-06-08 00:36:07 +00:00
|
|
|
"regexp"
|
2015-10-18 00:16:15 +00:00
|
|
|
"strconv"
|
2015-06-08 00:36:07 +00:00
|
|
|
"strings"
|
2015-10-18 00:16:15 +00:00
|
|
|
"time"
|
2015-06-08 00:36:07 +00:00
|
|
|
|
2018-05-30 17:53:04 +00:00
|
|
|
"github.com/xenolf/lego/log"
|
2015-12-05 21:01:08 +00:00
|
|
|
)
|
2015-06-08 00:36:07 +00:00
|
|
|
|
2017-03-13 21:41:19 +00:00
|
|
|
const (
|
|
|
|
// maxBodySize is the maximum size of body that we will read.
|
|
|
|
maxBodySize = 1024 * 1024
|
|
|
|
|
|
|
|
// overallRequestLimit is the overall number of request per second limited on the
|
|
|
|
// “new-reg”, “new-authz” and “new-cert” endpoints. From the documentation the
|
|
|
|
// limitation is 20 requests per second, but using 20 as value doesn't work but 18 do
|
|
|
|
overallRequestLimit = 18
|
|
|
|
)
|
2017-01-15 15:54:49 +00:00
|
|
|
|
2015-06-08 00:36:07 +00:00
|
|
|
// User interface is to be implemented by users of this library.
|
|
|
|
// It is used by the client type to get user specific information.
|
|
|
|
type User interface {
|
|
|
|
GetEmail() string
|
|
|
|
GetRegistration() *RegistrationResource
|
2016-01-27 01:01:39 +00:00
|
|
|
GetPrivateKey() crypto.PrivateKey
|
2015-06-08 00:36:07 +00:00
|
|
|
}
|
|
|
|
|
2015-06-13 02:50:36 +00:00
|
|
|
// Interface for all challenge solvers to implement.
|
2015-06-11 13:31:09 +00:00
|
|
|
type solver interface {
|
2015-06-13 01:55:53 +00:00
|
|
|
Solve(challenge challenge, domain string) error
|
2015-06-10 13:11:01 +00:00
|
|
|
}
|
|
|
|
|
2018-09-08 09:56:51 +00:00
|
|
|
// Interface for challenges like dns, where we can set a record in advance for ALL challenges.
|
|
|
|
// This saves quite a bit of time vs creating the records and solving them serially.
|
|
|
|
type presolver interface {
|
|
|
|
PreSolve(challenge challenge, domain string) error
|
|
|
|
}
|
|
|
|
|
|
|
|
// Interface for challenges like dns, where we can solve all the challenges before to delete them.
|
|
|
|
type cleanup interface {
|
|
|
|
CleanUp(challenge challenge, domain string) error
|
|
|
|
}
|
|
|
|
|
2015-12-27 18:08:17 +00:00
|
|
|
type validateFunc func(j *jws, domain, uri string, chlng challenge) error
|
|
|
|
|
2015-06-08 00:36:07 +00:00
|
|
|
// Client is the user-friendy way to ACME
|
|
|
|
type Client struct {
|
2016-12-13 23:22:48 +00:00
|
|
|
directory directory
|
|
|
|
user User
|
|
|
|
jws *jws
|
|
|
|
keyType KeyType
|
|
|
|
solvers map[Challenge]solver
|
2015-06-08 00:36:07 +00:00
|
|
|
}
|
|
|
|
|
2015-12-27 19:56:02 +00:00
|
|
|
// NewClient creates a new ACME client on behalf of the user. The client will depend on
|
2016-08-04 15:56:40 +00:00
|
|
|
// the ACME directory located at caDirURL for the rest of its actions. A private
|
|
|
|
// key of type keyType (see KeyType contants) will be generated when requesting a new
|
|
|
|
// certificate if one isn't provided.
|
2016-01-27 01:01:39 +00:00
|
|
|
func NewClient(caDirURL string, user User, keyType KeyType) (*Client, error) {
|
2015-11-20 19:01:06 +00:00
|
|
|
privKey := user.GetPrivateKey()
|
2015-10-27 23:00:42 +00:00
|
|
|
if privKey == nil {
|
|
|
|
return nil, errors.New("private key was nil")
|
2015-06-08 00:36:07 +00:00
|
|
|
}
|
2015-10-27 23:00:42 +00:00
|
|
|
|
2015-09-26 20:59:16 +00:00
|
|
|
var dir directory
|
2015-12-05 21:32:53 +00:00
|
|
|
if _, err := getJSON(caDirURL, &dir); err != nil {
|
2015-12-05 15:59:15 +00:00
|
|
|
return nil, fmt.Errorf("get directory at '%s': %v", caDirURL, err)
|
2015-10-27 23:00:42 +00:00
|
|
|
}
|
|
|
|
|
2018-05-30 17:53:04 +00:00
|
|
|
if dir.NewAccountURL == "" {
|
2015-10-27 23:00:42 +00:00
|
|
|
return nil, errors.New("directory missing new registration URL")
|
|
|
|
}
|
2018-05-30 17:53:04 +00:00
|
|
|
if dir.NewOrderURL == "" {
|
|
|
|
return nil, errors.New("directory missing new order URL")
|
2015-09-26 20:59:16 +00:00
|
|
|
}
|
|
|
|
|
2018-05-30 17:53:04 +00:00
|
|
|
jws := &jws{privKey: privKey, getNonceURL: dir.NewNonceURL}
|
|
|
|
if reg := user.GetRegistration(); reg != nil {
|
|
|
|
jws.kid = reg.URI
|
|
|
|
}
|
2015-11-12 01:55:28 +00:00
|
|
|
|
|
|
|
// REVIEW: best possibility?
|
|
|
|
// Add all available solvers with the right index as per ACME
|
|
|
|
// spec to this map. Otherwise they won`t be found.
|
2018-06-13 23:20:56 +00:00
|
|
|
solvers := map[Challenge]solver{
|
|
|
|
HTTP01: &httpChallenge{jws: jws, validate: validate, provider: &HTTPProviderServer{}},
|
|
|
|
TLSALPN01: &tlsALPNChallenge{jws: jws, validate: validate, provider: &TLSALPNProviderServer{}},
|
|
|
|
}
|
2015-11-12 01:55:28 +00:00
|
|
|
|
2016-01-27 01:01:39 +00:00
|
|
|
return &Client{directory: dir, user: user, jws: jws, keyType: keyType, solvers: solvers}, nil
|
2015-06-08 00:36:07 +00:00
|
|
|
}
|
|
|
|
|
2016-09-29 00:19:52 +00:00
|
|
|
// SetChallengeProvider specifies a custom provider p that can solve the given challenge type.
|
2016-01-15 04:06:25 +00:00
|
|
|
func (c *Client) SetChallengeProvider(challenge Challenge, p ChallengeProvider) error {
|
|
|
|
switch challenge {
|
|
|
|
case HTTP01:
|
|
|
|
c.solvers[challenge] = &httpChallenge{jws: c.jws, validate: validate, provider: p}
|
|
|
|
case DNS01:
|
2016-01-24 23:23:21 +00:00
|
|
|
c.solvers[challenge] = &dnsChallenge{jws: c.jws, validate: validate, provider: p}
|
2018-06-13 23:20:56 +00:00
|
|
|
case TLSALPN01:
|
|
|
|
c.solvers[challenge] = &tlsALPNChallenge{jws: c.jws, validate: validate, provider: p}
|
2016-01-15 04:06:25 +00:00
|
|
|
default:
|
2018-06-13 23:20:56 +00:00
|
|
|
return fmt.Errorf("unknown challenge %v", challenge)
|
2016-01-15 04:06:25 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-08 07:04:38 +00:00
|
|
|
// SetHTTPAddress specifies a custom interface:port to be used for HTTP based challenges.
|
|
|
|
// If this option is not used, the default port 80 and all interfaces will be used.
|
|
|
|
// To only specify a port and no interface use the ":port" notation.
|
2016-09-29 00:19:52 +00:00
|
|
|
//
|
|
|
|
// NOTE: This REPLACES any custom HTTP provider previously set by calling
|
|
|
|
// c.SetChallengeProvider with the default HTTP challenge provider.
|
2016-01-08 07:04:38 +00:00
|
|
|
func (c *Client) SetHTTPAddress(iface string) error {
|
|
|
|
host, port, err := net.SplitHostPort(iface)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-01-15 04:06:25 +00:00
|
|
|
if chlng, ok := c.solvers[HTTP01]; ok {
|
2016-02-14 15:56:14 +00:00
|
|
|
chlng.(*httpChallenge).provider = NewHTTPProviderServer(host, port)
|
2015-12-27 17:56:36 +00:00
|
|
|
}
|
2016-01-08 07:04:38 +00:00
|
|
|
|
|
|
|
return nil
|
2015-12-27 17:28:20 +00:00
|
|
|
}
|
|
|
|
|
2018-06-13 23:20:56 +00:00
|
|
|
// SetTLSAddress specifies a custom interface:port to be used for TLS based challenges.
|
|
|
|
// If this option is not used, the default port 443 and all interfaces will be used.
|
|
|
|
// To only specify a port and no interface use the ":port" notation.
|
|
|
|
//
|
|
|
|
// NOTE: This REPLACES any custom TLS-ALPN provider previously set by calling
|
|
|
|
// c.SetChallengeProvider with the default TLS-ALPN challenge provider.
|
|
|
|
func (c *Client) SetTLSAddress(iface string) error {
|
|
|
|
host, port, err := net.SplitHostPort(iface)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if chlng, ok := c.solvers[TLSALPN01]; ok {
|
|
|
|
chlng.(*tlsALPNChallenge).provider = NewTLSALPNProviderServer(host, port)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-12-27 17:28:20 +00:00
|
|
|
// ExcludeChallenges explicitly removes challenges from the pool for solving.
|
2016-01-15 04:06:25 +00:00
|
|
|
func (c *Client) ExcludeChallenges(challenges []Challenge) {
|
2015-12-27 17:28:20 +00:00
|
|
|
// Loop through all challenges and delete the requested one if found.
|
|
|
|
for _, challenge := range challenges {
|
2016-01-15 04:06:25 +00:00
|
|
|
delete(c.solvers, challenge)
|
2015-12-27 17:28:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-30 17:53:04 +00:00
|
|
|
// GetToSURL returns the current ToS URL from the Directory
|
|
|
|
func (c *Client) GetToSURL() string {
|
|
|
|
return c.directory.Meta.TermsOfService
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetExternalAccountRequired returns the External Account Binding requirement of the Directory
|
|
|
|
func (c *Client) GetExternalAccountRequired() bool {
|
|
|
|
return c.directory.Meta.ExternalAccountRequired
|
|
|
|
}
|
|
|
|
|
2015-06-08 00:36:07 +00:00
|
|
|
// Register the current account to the ACME server.
|
2018-05-30 17:53:04 +00:00
|
|
|
func (c *Client) Register(tosAgreed bool) (*RegistrationResource, error) {
|
2015-11-07 06:22:32 +00:00
|
|
|
if c == nil || c.user == nil {
|
|
|
|
return nil, errors.New("acme: cannot register a nil client or user")
|
|
|
|
}
|
2018-06-21 17:06:16 +00:00
|
|
|
log.Infof("acme: Registering account for %s", c.user.GetEmail())
|
2015-10-23 14:24:02 +00:00
|
|
|
|
2018-05-30 17:53:04 +00:00
|
|
|
accMsg := accountMessage{}
|
2015-10-30 23:11:33 +00:00
|
|
|
if c.user.GetEmail() != "" {
|
2018-05-30 17:53:04 +00:00
|
|
|
accMsg.Contact = []string{"mailto:" + c.user.GetEmail()}
|
2015-10-30 23:11:33 +00:00
|
|
|
} else {
|
2018-05-30 17:53:04 +00:00
|
|
|
accMsg.Contact = []string{}
|
2015-10-30 23:11:33 +00:00
|
|
|
}
|
2018-05-30 17:53:04 +00:00
|
|
|
accMsg.TermsOfServiceAgreed = tosAgreed
|
2015-10-30 23:11:33 +00:00
|
|
|
|
2018-05-30 17:53:04 +00:00
|
|
|
var serverReg accountMessage
|
|
|
|
hdr, err := postJSON(c.jws, c.directory.NewAccountURL, accMsg, &serverReg)
|
2015-06-08 00:36:07 +00:00
|
|
|
if err != nil {
|
2016-06-06 12:13:45 +00:00
|
|
|
remoteErr, ok := err.(RemoteError)
|
|
|
|
if ok && remoteErr.StatusCode == 409 {
|
|
|
|
} else {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-06-08 00:36:07 +00:00
|
|
|
}
|
|
|
|
|
2018-05-30 17:53:04 +00:00
|
|
|
reg := &RegistrationResource{
|
|
|
|
URI: hdr.Get("Location"),
|
|
|
|
Body: serverReg,
|
|
|
|
}
|
|
|
|
c.jws.kid = reg.URI
|
2015-06-08 00:36:07 +00:00
|
|
|
|
2018-05-30 17:53:04 +00:00
|
|
|
return reg, nil
|
|
|
|
}
|
2016-06-06 12:13:45 +00:00
|
|
|
|
2018-05-30 22:03:55 +00:00
|
|
|
// RegisterWithExternalAccountBinding Register the current account to the ACME server.
|
2018-05-30 17:53:04 +00:00
|
|
|
func (c *Client) RegisterWithExternalAccountBinding(tosAgreed bool, kid string, hmacEncoded string) (*RegistrationResource, error) {
|
|
|
|
if c == nil || c.user == nil {
|
|
|
|
return nil, errors.New("acme: cannot register a nil client or user")
|
2015-06-08 00:36:07 +00:00
|
|
|
}
|
2018-06-21 17:06:16 +00:00
|
|
|
log.Infof("acme: Registering account (EAB) for %s", c.user.GetEmail())
|
2015-06-08 00:36:07 +00:00
|
|
|
|
2018-05-30 17:53:04 +00:00
|
|
|
accMsg := accountMessage{}
|
|
|
|
if c.user.GetEmail() != "" {
|
|
|
|
accMsg.Contact = []string{"mailto:" + c.user.GetEmail()}
|
2015-06-08 00:36:07 +00:00
|
|
|
} else {
|
2018-05-30 17:53:04 +00:00
|
|
|
accMsg.Contact = []string{}
|
|
|
|
}
|
|
|
|
accMsg.TermsOfServiceAgreed = tosAgreed
|
|
|
|
|
|
|
|
hmac, err := base64.RawURLEncoding.DecodeString(hmacEncoded)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("acme: could not decode hmac key: %s", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
eabJWS, err := c.jws.signEABContent(c.directory.NewAccountURL, kid, hmac)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("acme: error signing eab content: %s", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
eabPayload := eabJWS.FullSerialize()
|
|
|
|
|
|
|
|
accMsg.ExternalAccountBinding = []byte(eabPayload)
|
|
|
|
|
|
|
|
var serverReg accountMessage
|
|
|
|
hdr, err := postJSON(c.jws, c.directory.NewAccountURL, accMsg, &serverReg)
|
|
|
|
if err != nil {
|
|
|
|
remoteErr, ok := err.(RemoteError)
|
|
|
|
if ok && remoteErr.StatusCode == 409 {
|
|
|
|
} else {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-06-08 00:36:07 +00:00
|
|
|
}
|
|
|
|
|
2018-05-30 17:53:04 +00:00
|
|
|
reg := &RegistrationResource{
|
|
|
|
URI: hdr.Get("Location"),
|
|
|
|
Body: serverReg,
|
|
|
|
}
|
|
|
|
c.jws.kid = reg.URI
|
|
|
|
|
2015-06-08 00:36:07 +00:00
|
|
|
return reg, nil
|
|
|
|
}
|
|
|
|
|
2018-05-30 17:53:04 +00:00
|
|
|
// ResolveAccountByKey will attempt to look up an account using the given account key
|
|
|
|
// and return its registration resource.
|
|
|
|
func (c *Client) ResolveAccountByKey() (*RegistrationResource, error) {
|
2018-06-21 17:06:16 +00:00
|
|
|
log.Infof("acme: Trying to resolve account by key")
|
2018-05-30 17:53:04 +00:00
|
|
|
|
|
|
|
acc := accountMessage{OnlyReturnExisting: true}
|
|
|
|
hdr, err := postJSON(c.jws, c.directory.NewAccountURL, acc, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
accountLink := hdr.Get("Location")
|
|
|
|
if accountLink == "" {
|
|
|
|
return nil, errors.New("Server did not return the account link")
|
|
|
|
}
|
|
|
|
|
|
|
|
var retAccount accountMessage
|
|
|
|
c.jws.kid = accountLink
|
|
|
|
_, err = postJSON(c.jws, accountLink, accountMessage{}, &retAccount)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &RegistrationResource{URI: accountLink, Body: retAccount}, nil
|
|
|
|
}
|
|
|
|
|
2016-06-06 18:45:22 +00:00
|
|
|
// DeleteRegistration deletes the client's user registration from the ACME
|
|
|
|
// server.
|
|
|
|
func (c *Client) DeleteRegistration() error {
|
|
|
|
if c == nil || c.user == nil {
|
|
|
|
return errors.New("acme: cannot unregister a nil client or user")
|
|
|
|
}
|
2018-06-21 17:06:16 +00:00
|
|
|
log.Infof("acme: Deleting account for %s", c.user.GetEmail())
|
2016-06-06 18:45:22 +00:00
|
|
|
|
2018-05-30 17:53:04 +00:00
|
|
|
accMsg := accountMessage{
|
|
|
|
Status: "deactivated",
|
2016-06-06 18:45:22 +00:00
|
|
|
}
|
|
|
|
|
2018-05-30 17:53:04 +00:00
|
|
|
_, err := postJSON(c.jws, c.user.GetRegistration().URI, accMsg, nil)
|
|
|
|
return err
|
2016-06-06 18:45:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// QueryRegistration runs a POST request on the client's registration and
|
|
|
|
// returns the result.
|
|
|
|
//
|
|
|
|
// This is similar to the Register function, but acting on an existing
|
|
|
|
// registration link and resource.
|
|
|
|
func (c *Client) QueryRegistration() (*RegistrationResource, error) {
|
|
|
|
if c == nil || c.user == nil {
|
|
|
|
return nil, errors.New("acme: cannot query the registration of a nil client or user")
|
|
|
|
}
|
|
|
|
// Log the URL here instead of the email as the email may not be set
|
2018-06-21 17:06:16 +00:00
|
|
|
log.Infof("acme: Querying account for %s", c.user.GetRegistration().URI)
|
2016-06-06 18:45:22 +00:00
|
|
|
|
2018-05-30 17:53:04 +00:00
|
|
|
accMsg := accountMessage{}
|
2016-06-06 18:45:22 +00:00
|
|
|
|
2018-05-30 17:53:04 +00:00
|
|
|
var serverReg accountMessage
|
|
|
|
_, err := postJSON(c.jws, c.user.GetRegistration().URI, accMsg, &serverReg)
|
2016-06-06 18:45:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
reg := &RegistrationResource{Body: serverReg}
|
|
|
|
|
|
|
|
// Location: header is not returned so this needs to be populated off of
|
|
|
|
// existing URI
|
|
|
|
reg.URI = c.user.GetRegistration().URI
|
|
|
|
|
|
|
|
return reg, nil
|
|
|
|
}
|
|
|
|
|
2016-02-12 01:00:41 +00:00
|
|
|
// ObtainCertificateForCSR tries to obtain a certificate matching the CSR passed into it.
|
|
|
|
// The domains are inferred from the CommonName and SubjectAltNames, if any. The private key
|
|
|
|
// for this CSR is not required.
|
|
|
|
// If bundle is true, the []byte contains both the issuer certificate and
|
|
|
|
// your issued certificate as a bundle.
|
|
|
|
// This function will never return a partial certificate. If one domain in the list fails,
|
|
|
|
// the whole certificate will fail.
|
2018-05-30 17:53:04 +00:00
|
|
|
func (c *Client) ObtainCertificateForCSR(csr x509.CertificateRequest, bundle bool) (*CertificateResource, error) {
|
2016-02-12 01:00:41 +00:00
|
|
|
// figure out what domains it concerns
|
|
|
|
// start with the common name
|
2016-06-08 02:50:52 +00:00
|
|
|
domains := []string{csr.Subject.CommonName}
|
2016-02-12 01:00:41 +00:00
|
|
|
|
|
|
|
// loop over the SubjectAltName DNS names
|
|
|
|
DNSNames:
|
2016-06-08 02:50:52 +00:00
|
|
|
for _, sanName := range csr.DNSNames {
|
2016-02-12 01:00:41 +00:00
|
|
|
for _, existingName := range domains {
|
|
|
|
if existingName == sanName {
|
|
|
|
// duplicate; skip this name
|
|
|
|
continue DNSNames
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// name is unique
|
|
|
|
domains = append(domains, sanName)
|
|
|
|
}
|
|
|
|
|
|
|
|
if bundle {
|
2018-06-21 17:06:16 +00:00
|
|
|
log.Infof("[%s] acme: Obtaining bundled SAN certificate given a CSR", strings.Join(domains, ", "))
|
2016-02-12 01:00:41 +00:00
|
|
|
} else {
|
2018-06-21 17:06:16 +00:00
|
|
|
log.Infof("[%s] acme: Obtaining SAN certificate given a CSR", strings.Join(domains, ", "))
|
2016-02-12 01:00:41 +00:00
|
|
|
}
|
|
|
|
|
2018-05-30 17:53:04 +00:00
|
|
|
order, err := c.createOrderForIdentifiers(domains)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
authz, err := c.getAuthzForOrder(order)
|
|
|
|
if err != nil {
|
|
|
|
// If any challenge fails, return. Do not generate partial SAN certificates.
|
|
|
|
/*for _, auth := range authz {
|
2017-04-26 23:46:52 +00:00
|
|
|
c.disableAuthz(auth)
|
2018-05-30 17:53:04 +00:00
|
|
|
}*/
|
|
|
|
return nil, err
|
2016-02-12 01:00:41 +00:00
|
|
|
}
|
|
|
|
|
2018-05-30 17:53:04 +00:00
|
|
|
err = c.solveChallengeForAuthz(authz)
|
|
|
|
if err != nil {
|
|
|
|
// If any challenge fails, return. Do not generate partial SAN certificates.
|
|
|
|
return nil, err
|
2016-02-12 01:00:41 +00:00
|
|
|
}
|
|
|
|
|
2018-06-21 17:06:16 +00:00
|
|
|
log.Infof("[%s] acme: Validations succeeded; requesting certificates", strings.Join(domains, ", "))
|
2016-02-12 01:00:41 +00:00
|
|
|
|
2018-05-30 17:53:04 +00:00
|
|
|
failures := make(ObtainError)
|
|
|
|
cert, err := c.requestCertificateForCsr(order, bundle, csr.Raw, nil)
|
2016-02-12 01:00:41 +00:00
|
|
|
if err != nil {
|
2018-05-30 17:53:04 +00:00
|
|
|
for _, chln := range authz {
|
|
|
|
failures[chln.Identifier.Value] = err
|
2016-02-12 01:00:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-09 10:37:30 +00:00
|
|
|
if cert != nil {
|
|
|
|
// Add the CSR to the certificate so that it can be used for renewals.
|
|
|
|
cert.CSR = pemEncode(&csr)
|
|
|
|
}
|
2016-06-08 02:50:52 +00:00
|
|
|
|
2018-05-30 17:53:04 +00:00
|
|
|
// do not return an empty failures map, because
|
|
|
|
// it would still be a non-nil error value
|
|
|
|
if len(failures) > 0 {
|
|
|
|
return cert, failures
|
|
|
|
}
|
|
|
|
return cert, nil
|
2016-02-12 01:00:41 +00:00
|
|
|
}
|
|
|
|
|
2015-12-05 22:07:12 +00:00
|
|
|
// ObtainCertificate tries to obtain a single certificate using all domains passed into it.
|
2015-11-18 18:53:42 +00:00
|
|
|
// The first domain in domains is used for the CommonName field of the certificate, all other
|
2016-01-08 09:14:41 +00:00
|
|
|
// domains are added using the Subject Alternate Names extension. A new private key is generated
|
|
|
|
// for every invocation of this function. If you do not want that you can supply your own private key
|
|
|
|
// in the privKey parameter. If this parameter is non-nil it will be used instead of generating a new one.
|
2015-11-18 18:53:42 +00:00
|
|
|
// If bundle is true, the []byte contains both the issuer certificate and
|
|
|
|
// your issued certificate as a bundle.
|
2015-12-27 19:56:02 +00:00
|
|
|
// This function will never return a partial certificate. If one domain in the list fails,
|
|
|
|
// the whole certificate will fail.
|
2018-05-30 17:53:04 +00:00
|
|
|
func (c *Client) ObtainCertificate(domains []string, bundle bool, privKey crypto.PrivateKey, mustStaple bool) (*CertificateResource, error) {
|
|
|
|
if len(domains) == 0 {
|
|
|
|
return nil, errors.New("No domains to obtain a certificate for")
|
|
|
|
}
|
|
|
|
|
2015-11-11 00:01:15 +00:00
|
|
|
if bundle {
|
2018-06-21 17:06:16 +00:00
|
|
|
log.Infof("[%s] acme: Obtaining bundled SAN certificate", strings.Join(domains, ", "))
|
2015-11-11 00:01:15 +00:00
|
|
|
} else {
|
2018-06-21 17:06:16 +00:00
|
|
|
log.Infof("[%s] acme: Obtaining SAN certificate", strings.Join(domains, ", "))
|
2015-11-11 00:01:15 +00:00
|
|
|
}
|
|
|
|
|
2018-05-30 17:53:04 +00:00
|
|
|
order, err := c.createOrderForIdentifiers(domains)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
authz, err := c.getAuthzForOrder(order)
|
|
|
|
if err != nil {
|
|
|
|
// If any challenge fails, return. Do not generate partial SAN certificates.
|
|
|
|
/*for _, auth := range authz {
|
2017-04-26 23:46:52 +00:00
|
|
|
c.disableAuthz(auth)
|
2018-05-30 17:53:04 +00:00
|
|
|
}*/
|
|
|
|
return nil, err
|
2015-11-11 00:01:15 +00:00
|
|
|
}
|
|
|
|
|
2018-05-30 17:53:04 +00:00
|
|
|
err = c.solveChallengeForAuthz(authz)
|
|
|
|
if err != nil {
|
|
|
|
// If any challenge fails, return. Do not generate partial SAN certificates.
|
|
|
|
return nil, err
|
2015-11-11 00:01:15 +00:00
|
|
|
}
|
|
|
|
|
2018-06-21 17:06:16 +00:00
|
|
|
log.Infof("[%s] acme: Validations succeeded; requesting certificates", strings.Join(domains, ", "))
|
2015-11-11 00:01:15 +00:00
|
|
|
|
2018-05-30 17:53:04 +00:00
|
|
|
failures := make(ObtainError)
|
|
|
|
cert, err := c.requestCertificateForOrder(order, bundle, privKey, mustStaple)
|
2015-11-11 00:01:15 +00:00
|
|
|
if err != nil {
|
2018-05-30 17:53:04 +00:00
|
|
|
for _, auth := range authz {
|
|
|
|
failures[auth.Identifier.Value] = err
|
2015-11-11 00:01:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-30 17:53:04 +00:00
|
|
|
// do not return an empty failures map, because
|
|
|
|
// it would still be a non-nil error value
|
|
|
|
if len(failures) > 0 {
|
|
|
|
return cert, failures
|
|
|
|
}
|
|
|
|
return cert, nil
|
2015-11-11 00:01:15 +00:00
|
|
|
}
|
|
|
|
|
2015-10-24 01:55:18 +00:00
|
|
|
// RevokeCertificate takes a PEM encoded certificate or bundle and tries to revoke it at the CA.
|
2015-09-27 12:51:44 +00:00
|
|
|
func (c *Client) RevokeCertificate(certificate []byte) error {
|
2015-10-24 01:55:18 +00:00
|
|
|
certificates, err := parsePEMBundle(certificate)
|
2015-10-19 01:18:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-10-24 02:31:12 +00:00
|
|
|
x509Cert := certificates[0]
|
2015-10-24 01:55:18 +00:00
|
|
|
if x509Cert.IsCA {
|
2015-10-24 02:31:12 +00:00
|
|
|
return fmt.Errorf("Certificate bundle starts with a CA certificate")
|
2015-10-24 01:55:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
encodedCert := base64.URLEncoding.EncodeToString(x509Cert.Raw)
|
2015-09-27 12:51:44 +00:00
|
|
|
|
2018-05-30 17:53:04 +00:00
|
|
|
_, err = postJSON(c.jws, c.directory.RevokeCertURL, revokeCertMessage{Certificate: encodedCert}, nil)
|
2015-12-05 15:59:15 +00:00
|
|
|
return err
|
2015-09-27 12:51:44 +00:00
|
|
|
}
|
|
|
|
|
2015-10-18 22:42:04 +00:00
|
|
|
// RenewCertificate takes a CertificateResource and tries to renew the certificate.
|
2015-10-25 22:37:26 +00:00
|
|
|
// If the renewal process succeeds, the new certificate will ge returned in a new CertResource.
|
2015-10-18 22:42:04 +00:00
|
|
|
// Please be aware that this function will return a new certificate in ANY case that is not an error.
|
|
|
|
// If the server does not provide us with a new cert on a GET request to the CertURL
|
2015-10-19 01:18:06 +00:00
|
|
|
// this function will start a new-cert flow where a new certificate gets generated.
|
2015-10-24 01:55:18 +00:00
|
|
|
// If bundle is true, the []byte contains both the issuer certificate and
|
|
|
|
// your issued certificate as a bundle.
|
2016-01-08 09:14:41 +00:00
|
|
|
// For private key reuse the PrivateKey property of the passed in CertificateResource should be non-nil.
|
2018-05-30 17:53:04 +00:00
|
|
|
func (c *Client) RenewCertificate(cert CertificateResource, bundle, mustStaple bool) (*CertificateResource, error) {
|
2015-10-18 22:42:04 +00:00
|
|
|
// Input certificate is PEM encoded. Decode it here as we may need the decoded
|
2015-10-24 01:55:18 +00:00
|
|
|
// cert later on in the renewal process. The input may be a bundle or a single certificate.
|
|
|
|
certificates, err := parsePEMBundle(cert.Certificate)
|
2015-10-18 22:42:04 +00:00
|
|
|
if err != nil {
|
2018-05-30 17:53:04 +00:00
|
|
|
return nil, err
|
2015-10-18 22:42:04 +00:00
|
|
|
}
|
|
|
|
|
2015-10-24 02:31:12 +00:00
|
|
|
x509Cert := certificates[0]
|
2015-10-24 01:55:18 +00:00
|
|
|
if x509Cert.IsCA {
|
2018-05-30 17:53:04 +00:00
|
|
|
return nil, fmt.Errorf("[%s] Certificate bundle starts with a CA certificate", cert.Domain)
|
2015-10-24 01:55:18 +00:00
|
|
|
}
|
|
|
|
|
2015-10-18 22:42:04 +00:00
|
|
|
// This is just meant to be informal for the user.
|
|
|
|
timeLeft := x509Cert.NotAfter.Sub(time.Now().UTC())
|
2018-06-21 17:06:16 +00:00
|
|
|
log.Infof("[%s] acme: Trying renewal with %d hours remaining", cert.Domain, int(timeLeft.Hours()))
|
2015-10-18 22:42:04 +00:00
|
|
|
|
2016-10-17 09:12:54 +00:00
|
|
|
// We always need to request a new certificate to renew.
|
2016-06-08 02:50:52 +00:00
|
|
|
// Start by checking to see if the certificate was based off a CSR, and
|
|
|
|
// use that if it's defined.
|
|
|
|
if len(cert.CSR) > 0 {
|
|
|
|
csr, err := pemDecodeTox509CSR(cert.CSR)
|
|
|
|
if err != nil {
|
2018-05-30 17:53:04 +00:00
|
|
|
return nil, err
|
2016-06-08 02:50:52 +00:00
|
|
|
}
|
|
|
|
newCert, failures := c.ObtainCertificateForCSR(*csr, bundle)
|
2018-05-30 17:53:04 +00:00
|
|
|
return newCert, failures
|
2016-06-08 02:50:52 +00:00
|
|
|
}
|
|
|
|
|
2016-01-08 09:14:41 +00:00
|
|
|
var privKey crypto.PrivateKey
|
|
|
|
if cert.PrivateKey != nil {
|
|
|
|
privKey, err = parsePEMPrivateKey(cert.PrivateKey)
|
|
|
|
if err != nil {
|
2018-05-30 17:53:04 +00:00
|
|
|
return nil, err
|
2016-01-08 09:14:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-29 13:10:57 +00:00
|
|
|
var domains []string
|
|
|
|
// check for SAN certificate
|
|
|
|
if len(x509Cert.DNSNames) > 1 {
|
|
|
|
domains = append(domains, x509Cert.Subject.CommonName)
|
|
|
|
for _, sanDomain := range x509Cert.DNSNames {
|
|
|
|
if sanDomain == x509Cert.Subject.CommonName {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
domains = append(domains, sanDomain)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
domains = append(domains, x509Cert.Subject.CommonName)
|
|
|
|
}
|
|
|
|
|
2018-05-30 17:53:04 +00:00
|
|
|
newCert, err := c.ObtainCertificate(domains, bundle, privKey, mustStaple)
|
|
|
|
return newCert, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) createOrderForIdentifiers(domains []string) (orderResource, error) {
|
|
|
|
|
|
|
|
var identifiers []identifier
|
|
|
|
for _, domain := range domains {
|
|
|
|
identifiers = append(identifiers, identifier{Type: "dns", Value: domain})
|
|
|
|
}
|
|
|
|
|
|
|
|
order := orderMessage{
|
|
|
|
Identifiers: identifiers,
|
|
|
|
}
|
|
|
|
|
|
|
|
var response orderMessage
|
|
|
|
hdr, err := postJSON(c.jws, c.directory.NewOrderURL, order, &response)
|
|
|
|
if err != nil {
|
|
|
|
return orderResource{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
orderRes := orderResource{
|
|
|
|
URL: hdr.Get("Location"),
|
|
|
|
Domains: domains,
|
|
|
|
orderMessage: response,
|
|
|
|
}
|
|
|
|
return orderRes, nil
|
2015-10-18 22:42:04 +00:00
|
|
|
}
|
|
|
|
|
2018-09-08 09:56:51 +00:00
|
|
|
// an authz with the solver we have chosen and the index of the challenge associated with it
|
|
|
|
type selectedAuthSolver struct {
|
|
|
|
authz authorization
|
|
|
|
challengeIndex int
|
|
|
|
solver solver
|
|
|
|
}
|
|
|
|
|
2015-06-10 23:11:14 +00:00
|
|
|
// Looks through the challenge combinations to find a solvable match.
|
|
|
|
// Then solves the challenges in series and returns.
|
2018-05-30 17:53:04 +00:00
|
|
|
func (c *Client) solveChallengeForAuthz(authorizations []authorization) error {
|
|
|
|
failures := make(ObtainError)
|
|
|
|
|
2018-09-08 09:56:51 +00:00
|
|
|
authSolvers := []*selectedAuthSolver{}
|
|
|
|
|
|
|
|
// loop through the resources, basically through the domains. First pass just selects a solver for each authz.
|
2018-05-30 17:53:04 +00:00
|
|
|
for _, authz := range authorizations {
|
|
|
|
if authz.Status == "valid" {
|
2016-08-16 20:00:17 +00:00
|
|
|
// Boulder might recycle recent validated authz (see issue #267)
|
2018-06-21 17:06:16 +00:00
|
|
|
log.Infof("[%s] acme: Authorization already valid; skipping challenge", authz.Identifier.Value)
|
2016-08-16 19:50:56 +00:00
|
|
|
continue
|
|
|
|
}
|
2018-05-30 17:53:04 +00:00
|
|
|
if i, solver := c.chooseSolver(authz, authz.Identifier.Value); solver != nil {
|
2018-09-08 09:56:51 +00:00
|
|
|
authSolvers = append(authSolvers, &selectedAuthSolver{
|
|
|
|
authz: authz,
|
|
|
|
challengeIndex: i,
|
|
|
|
solver: solver,
|
|
|
|
})
|
2015-06-11 22:15:13 +00:00
|
|
|
} else {
|
2018-05-30 17:53:04 +00:00
|
|
|
failures[authz.Identifier.Value] = fmt.Errorf("[%s] acme: Could not determine solvers", authz.Identifier.Value)
|
2015-06-11 22:15:13 +00:00
|
|
|
}
|
2015-06-10 13:11:01 +00:00
|
|
|
}
|
2015-06-11 22:15:13 +00:00
|
|
|
|
2018-09-21 15:28:50 +00:00
|
|
|
// for all valid presolvers, first submit the challenges so they have max time to propagate
|
2018-09-08 09:56:51 +00:00
|
|
|
for _, item := range authSolvers {
|
|
|
|
authz := item.authz
|
|
|
|
i := item.challengeIndex
|
|
|
|
if presolver, ok := item.solver.(presolver); ok {
|
|
|
|
if err := presolver.PreSolve(authz.Challenges[i], authz.Identifier.Value); err != nil {
|
|
|
|
failures[authz.Identifier.Value] = err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
// clean all created TXT records
|
|
|
|
for _, item := range authSolvers {
|
|
|
|
if cleanup, ok := item.solver.(cleanup); ok {
|
|
|
|
if failures[item.authz.Identifier.Value] != nil {
|
|
|
|
// already failed in previous loop
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
err := cleanup.CleanUp(item.authz.Challenges[item.challengeIndex], item.authz.Identifier.Value)
|
|
|
|
if err != nil {
|
|
|
|
log.Warnf("Error cleaning up %s: %v ", item.authz.Identifier.Value, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// finally solve all challenges for real
|
|
|
|
for _, item := range authSolvers {
|
|
|
|
authz := item.authz
|
|
|
|
i := item.challengeIndex
|
|
|
|
if failures[authz.Identifier.Value] != nil {
|
|
|
|
// already failed in previous loop
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err := item.solver.Solve(authz.Challenges[i], authz.Identifier.Value); err != nil {
|
|
|
|
failures[authz.Identifier.Value] = err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-30 17:53:04 +00:00
|
|
|
// be careful not to return an empty failures map, for
|
|
|
|
// even an empty ObtainError is a non-nil error value
|
|
|
|
if len(failures) > 0 {
|
|
|
|
return failures
|
|
|
|
}
|
|
|
|
return nil
|
2015-06-11 22:15:13 +00:00
|
|
|
}
|
|
|
|
|
2018-05-30 17:53:04 +00:00
|
|
|
// Checks all challenges from the server in order and returns the first matching solver.
|
|
|
|
func (c *Client) chooseSolver(auth authorization, domain string) (int, solver) {
|
|
|
|
for i, challenge := range auth.Challenges {
|
|
|
|
if solver, ok := c.solvers[Challenge(challenge.Type)]; ok {
|
|
|
|
return i, solver
|
2015-06-11 22:15:13 +00:00
|
|
|
}
|
2018-06-21 17:06:16 +00:00
|
|
|
log.Infof("[%s] acme: Could not find solver for: %s", domain, challenge.Type)
|
2015-06-11 22:15:13 +00:00
|
|
|
}
|
2018-05-30 17:53:04 +00:00
|
|
|
return 0, nil
|
2015-06-10 13:11:01 +00:00
|
|
|
}
|
|
|
|
|
2015-06-10 23:11:14 +00:00
|
|
|
// Get the challenges needed to proof our identifier to the ACME server.
|
2018-05-30 17:53:04 +00:00
|
|
|
func (c *Client) getAuthzForOrder(order orderResource) ([]authorization, error) {
|
|
|
|
resc, errc := make(chan authorization), make(chan domainError)
|
2015-06-10 13:11:01 +00:00
|
|
|
|
2017-03-30 00:06:43 +00:00
|
|
|
delay := time.Second / overallRequestLimit
|
2017-03-13 21:41:19 +00:00
|
|
|
|
2018-05-30 17:53:04 +00:00
|
|
|
for _, authzURL := range order.Authorizations {
|
2017-03-13 21:41:19 +00:00
|
|
|
time.Sleep(delay)
|
|
|
|
|
2018-05-30 17:53:04 +00:00
|
|
|
go func(authzURL string) {
|
2015-12-05 15:59:15 +00:00
|
|
|
var authz authorization
|
2018-05-30 17:53:04 +00:00
|
|
|
_, err := getJSON(authzURL, &authz)
|
2015-06-08 21:54:15 +00:00
|
|
|
if err != nil {
|
2018-05-30 17:53:04 +00:00
|
|
|
errc <- domainError{Domain: authz.Identifier.Value, Error: err}
|
2015-06-08 21:54:15 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-05-30 17:53:04 +00:00
|
|
|
resc <- authz
|
|
|
|
}(authzURL)
|
2015-06-08 21:54:15 +00:00
|
|
|
}
|
|
|
|
|
2018-05-30 17:53:04 +00:00
|
|
|
var responses []authorization
|
|
|
|
failures := make(ObtainError)
|
|
|
|
for i := 0; i < len(order.Authorizations); i++ {
|
2015-06-08 21:54:15 +00:00
|
|
|
select {
|
|
|
|
case res := <-resc:
|
2018-05-30 17:53:04 +00:00
|
|
|
responses = append(responses, res)
|
2015-06-08 21:54:15 +00:00
|
|
|
case err := <-errc:
|
2015-11-02 00:01:00 +00:00
|
|
|
failures[err.Domain] = err.Error
|
2015-06-08 21:54:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-30 17:53:04 +00:00
|
|
|
logAuthz(order)
|
2017-02-19 04:30:33 +00:00
|
|
|
|
2015-06-08 21:54:15 +00:00
|
|
|
close(resc)
|
|
|
|
close(errc)
|
|
|
|
|
2018-05-30 17:53:04 +00:00
|
|
|
// be careful to not return an empty failures map;
|
|
|
|
// even if empty, they become non-nil error values
|
|
|
|
if len(failures) > 0 {
|
|
|
|
return responses, failures
|
|
|
|
}
|
|
|
|
return responses, nil
|
2015-06-08 21:54:15 +00:00
|
|
|
}
|
|
|
|
|
2018-05-30 17:53:04 +00:00
|
|
|
func logAuthz(order orderResource) {
|
|
|
|
for i, auth := range order.Authorizations {
|
2018-06-21 17:06:16 +00:00
|
|
|
log.Infof("[%s] AuthURL: %s", order.Identifiers[i].Value, auth)
|
2017-02-19 04:30:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-26 23:46:52 +00:00
|
|
|
// cleanAuthz loops through the passed in slice and disables any auths which are not "valid"
|
2018-05-30 17:53:04 +00:00
|
|
|
func (c *Client) disableAuthz(authURL string) error {
|
2017-04-26 23:46:52 +00:00
|
|
|
var disabledAuth authorization
|
2018-05-30 17:53:04 +00:00
|
|
|
_, err := postJSON(c.jws, authURL, deactivateAuthMessage{Status: "deactivated"}, &disabledAuth)
|
2017-04-26 23:46:52 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-05-30 17:53:04 +00:00
|
|
|
func (c *Client) requestCertificateForOrder(order orderResource, bundle bool, privKey crypto.PrivateKey, mustStaple bool) (*CertificateResource, error) {
|
2015-11-11 00:01:15 +00:00
|
|
|
|
2016-01-08 09:14:41 +00:00
|
|
|
var err error
|
|
|
|
if privKey == nil {
|
2016-01-27 01:01:39 +00:00
|
|
|
privKey, err = generatePrivateKey(c.keyType)
|
2016-01-08 09:14:41 +00:00
|
|
|
if err != nil {
|
2018-05-30 17:53:04 +00:00
|
|
|
return nil, err
|
2016-01-08 09:14:41 +00:00
|
|
|
}
|
2015-11-11 00:01:15 +00:00
|
|
|
}
|
|
|
|
|
2016-02-12 01:00:41 +00:00
|
|
|
// determine certificate name(s) based on the authorization resources
|
2018-05-30 17:53:04 +00:00
|
|
|
commonName := order.Domains[0]
|
2018-06-30 23:06:46 +00:00
|
|
|
|
|
|
|
// ACME draft Section 7.4 "Applying for Certificate Issuance"
|
|
|
|
// https://tools.ietf.org/html/draft-ietf-acme-acme-12#section-7.4
|
|
|
|
// says:
|
|
|
|
// Clients SHOULD NOT make any assumptions about the sort order of
|
|
|
|
// "identifiers" or "authorizations" elements in the returned order
|
|
|
|
// object.
|
|
|
|
san := []string{commonName}
|
2018-05-30 17:53:04 +00:00
|
|
|
for _, auth := range order.Identifiers {
|
2018-06-30 23:06:46 +00:00
|
|
|
if auth.Value != commonName {
|
|
|
|
san = append(san, auth.Value)
|
|
|
|
}
|
2015-10-18 00:16:15 +00:00
|
|
|
}
|
|
|
|
|
2015-10-24 01:55:18 +00:00
|
|
|
// TODO: should the CSR be customizable?
|
2018-05-30 17:53:04 +00:00
|
|
|
csr, err := generateCsr(privKey, commonName, san, mustStaple)
|
2015-10-18 00:16:15 +00:00
|
|
|
if err != nil {
|
2018-05-30 17:53:04 +00:00
|
|
|
return nil, err
|
2015-10-18 00:16:15 +00:00
|
|
|
}
|
2015-06-13 01:55:53 +00:00
|
|
|
|
2018-05-30 17:53:04 +00:00
|
|
|
return c.requestCertificateForCsr(order, bundle, csr, pemEncode(privKey))
|
2016-02-12 01:00:41 +00:00
|
|
|
}
|
|
|
|
|
2018-05-30 17:53:04 +00:00
|
|
|
func (c *Client) requestCertificateForCsr(order orderResource, bundle bool, csr []byte, privateKeyPem []byte) (*CertificateResource, error) {
|
|
|
|
commonName := order.Domains[0]
|
2016-02-12 01:00:41 +00:00
|
|
|
|
2018-05-30 17:53:04 +00:00
|
|
|
csrString := base64.RawURLEncoding.EncodeToString(csr)
|
|
|
|
var retOrder orderMessage
|
2018-06-30 23:06:46 +00:00
|
|
|
_, err := postJSON(c.jws, order.Finalize, csrMessage{Csr: csrString}, &retOrder)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2015-10-18 00:16:15 +00:00
|
|
|
}
|
|
|
|
|
2018-05-30 17:53:04 +00:00
|
|
|
if retOrder.Status == "invalid" {
|
2018-06-30 23:06:46 +00:00
|
|
|
return nil, err
|
2015-10-18 00:16:15 +00:00
|
|
|
}
|
|
|
|
|
2017-01-15 15:54:49 +00:00
|
|
|
certRes := CertificateResource{
|
2018-05-30 17:53:04 +00:00
|
|
|
Domain: commonName,
|
|
|
|
CertURL: retOrder.Certificate,
|
2017-01-15 15:54:49 +00:00
|
|
|
PrivateKey: privateKeyPem,
|
|
|
|
}
|
2015-10-18 00:16:15 +00:00
|
|
|
|
2018-05-30 17:53:04 +00:00
|
|
|
if retOrder.Status == "valid" {
|
|
|
|
// if the certificate is available right away, short cut!
|
|
|
|
ok, err := c.checkCertResponse(retOrder, &certRes, bundle)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if ok {
|
|
|
|
return &certRes, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-25 21:22:42 +00:00
|
|
|
stopTimer := time.NewTimer(30 * time.Second)
|
|
|
|
defer stopTimer.Stop()
|
|
|
|
retryTick := time.NewTicker(500 * time.Millisecond)
|
|
|
|
defer retryTick.Stop()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-stopTimer.C:
|
|
|
|
return nil, errors.New("certificate polling timed out")
|
|
|
|
case <-retryTick.C:
|
|
|
|
_, err := getJSON(order.URL, &retOrder)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
done, err := c.checkCertResponse(retOrder, &certRes, bundle)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if done {
|
|
|
|
return &certRes, nil
|
|
|
|
}
|
2017-01-15 15:54:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-10-24 01:55:18 +00:00
|
|
|
|
2018-05-30 17:53:04 +00:00
|
|
|
// checkCertResponse checks to see if the certificate is ready and a link is contained in the
|
|
|
|
// response. if so, loads it into certRes and returns true. If the cert
|
|
|
|
// is not yet ready, it returns false. The certRes input
|
2017-01-15 15:54:49 +00:00
|
|
|
// should already have the Domain (common name) field populated. If bundle is
|
|
|
|
// true, the certificate will be bundled with the issuer's cert.
|
2018-05-30 17:53:04 +00:00
|
|
|
func (c *Client) checkCertResponse(order orderMessage, certRes *CertificateResource, bundle bool) (bool, error) {
|
|
|
|
|
|
|
|
switch order.Status {
|
|
|
|
case "valid":
|
|
|
|
resp, err := httpGet(order.Certificate)
|
2017-01-15 15:54:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2016-12-13 23:22:48 +00:00
|
|
|
|
2018-05-30 17:53:04 +00:00
|
|
|
cert, err := ioutil.ReadAll(limitReader(resp.Body, maxBodySize))
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2015-10-24 02:31:12 +00:00
|
|
|
|
2018-07-01 00:35:39 +00:00
|
|
|
// The issuer certificate link may be supplied via an "up" link
|
|
|
|
// in the response headers of a new certificate. See
|
|
|
|
// https://tools.ietf.org/html/draft-ietf-acme-acme-12#section-7.4.2
|
2018-05-30 17:53:04 +00:00
|
|
|
links := parseLinks(resp.Header["Link"])
|
|
|
|
if link, ok := links["up"]; ok {
|
|
|
|
issuerCert, err := c.getIssuerCertificate(link)
|
2015-10-18 00:16:15 +00:00
|
|
|
|
2015-10-18 15:27:59 +00:00
|
|
|
if err != nil {
|
2017-01-15 15:54:49 +00:00
|
|
|
// If we fail to acquire the issuer cert, return the issued certificate - do not fail.
|
2018-06-21 17:06:16 +00:00
|
|
|
log.Warnf("[%s] acme: Could not bundle issuer certificate: %v", certRes.Domain, err)
|
2017-01-15 15:54:49 +00:00
|
|
|
} else {
|
|
|
|
issuerCert = pemEncode(derCertificateBytes(issuerCert))
|
2015-10-18 15:27:59 +00:00
|
|
|
|
2017-01-15 15:54:49 +00:00
|
|
|
// If bundle is true, we want to return a certificate bundle.
|
|
|
|
// To do this, we append the issuer cert to the issued cert.
|
|
|
|
if bundle {
|
2018-05-30 17:53:04 +00:00
|
|
|
cert = append(cert, issuerCert...)
|
2017-01-15 15:54:49 +00:00
|
|
|
}
|
2015-10-18 15:27:59 +00:00
|
|
|
|
2018-05-30 17:53:04 +00:00
|
|
|
certRes.IssuerCertificate = issuerCert
|
|
|
|
}
|
2018-07-01 00:35:39 +00:00
|
|
|
} else {
|
|
|
|
// Get issuerCert from bundled response from Let's Encrypt
|
|
|
|
// See https://community.letsencrypt.org/t/acme-v2-no-up-link-in-response/64962
|
|
|
|
_, rest := pem.Decode(cert)
|
|
|
|
if rest != nil {
|
|
|
|
certRes.IssuerCertificate = rest
|
|
|
|
}
|
2015-06-13 01:55:53 +00:00
|
|
|
}
|
2017-01-15 15:54:49 +00:00
|
|
|
|
2018-05-30 17:53:04 +00:00
|
|
|
certRes.Certificate = cert
|
|
|
|
certRes.CertURL = order.Certificate
|
|
|
|
certRes.CertStableURL = order.Certificate
|
2018-06-21 17:06:16 +00:00
|
|
|
log.Infof("[%s] Server responded with a certificate.", certRes.Domain)
|
2018-05-30 17:53:04 +00:00
|
|
|
return true, nil
|
2017-01-15 15:54:49 +00:00
|
|
|
|
2018-05-30 17:53:04 +00:00
|
|
|
case "processing":
|
2017-01-15 15:54:49 +00:00
|
|
|
return false, nil
|
2018-05-30 17:53:04 +00:00
|
|
|
case "invalid":
|
2018-06-21 17:06:16 +00:00
|
|
|
return false, errors.New("order has invalid state: invalid")
|
|
|
|
default:
|
|
|
|
return false, nil
|
2015-06-13 01:55:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-13 23:22:48 +00:00
|
|
|
// getIssuerCertificate requests the issuer certificate
|
2015-10-24 01:55:18 +00:00
|
|
|
func (c *Client) getIssuerCertificate(url string) ([]byte, error) {
|
2018-06-21 17:06:16 +00:00
|
|
|
log.Infof("acme: Requesting issuer cert from %s", url)
|
2015-12-30 22:01:21 +00:00
|
|
|
resp, err := httpGet(url)
|
2015-10-24 01:55:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-12-21 00:42:33 +00:00
|
|
|
defer resp.Body.Close()
|
2015-10-24 01:55:18 +00:00
|
|
|
|
2017-01-15 15:54:49 +00:00
|
|
|
issuerBytes, err := ioutil.ReadAll(limitReader(resp.Body, maxBodySize))
|
2015-10-24 01:55:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = x509.ParseCertificate(issuerBytes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return issuerBytes, err
|
|
|
|
}
|
|
|
|
|
2015-06-08 00:36:07 +00:00
|
|
|
func parseLinks(links []string) map[string]string {
|
|
|
|
aBrkt := regexp.MustCompile("[<>]")
|
|
|
|
slver := regexp.MustCompile("(.+) *= *\"(.+)\"")
|
|
|
|
linkMap := make(map[string]string)
|
|
|
|
|
|
|
|
for _, link := range links {
|
|
|
|
|
|
|
|
link = aBrkt.ReplaceAllString(link, "")
|
|
|
|
parts := strings.Split(link, ";")
|
|
|
|
|
|
|
|
matches := slver.FindStringSubmatch(parts[1])
|
|
|
|
if len(matches) > 0 {
|
|
|
|
linkMap[matches[2]] = parts[0]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return linkMap
|
|
|
|
}
|
2015-12-05 11:51:30 +00:00
|
|
|
|
|
|
|
// validate makes the ACME server start validating a
|
|
|
|
// challenge response, only returning once it is done.
|
2018-05-30 17:53:04 +00:00
|
|
|
func validate(j *jws, domain, uri string, c challenge) error {
|
|
|
|
var chlng challenge
|
2015-12-05 11:51:30 +00:00
|
|
|
|
2018-05-30 17:53:04 +00:00
|
|
|
hdr, err := postJSON(j, uri, c, &chlng)
|
2015-12-05 15:59:15 +00:00
|
|
|
if err != nil {
|
2015-12-05 11:51:30 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// After the path is sent, the ACME server will access our server.
|
|
|
|
// Repeatedly check the server for an updated status on our request.
|
|
|
|
for {
|
2018-05-30 17:53:04 +00:00
|
|
|
switch chlng.Status {
|
2015-12-05 11:51:30 +00:00
|
|
|
case "valid":
|
2018-06-21 17:06:16 +00:00
|
|
|
log.Infof("[%s] The server validated our request", domain)
|
2015-12-05 11:51:30 +00:00
|
|
|
return nil
|
|
|
|
case "pending":
|
2018-05-31 15:22:37 +00:00
|
|
|
case "processing":
|
2015-12-05 11:51:30 +00:00
|
|
|
case "invalid":
|
2018-05-30 17:53:04 +00:00
|
|
|
return handleChallengeError(chlng)
|
2015-12-05 11:51:30 +00:00
|
|
|
default:
|
2018-05-30 17:53:04 +00:00
|
|
|
return errors.New("the server returned an unexpected state")
|
2015-12-05 11:51:30 +00:00
|
|
|
}
|
|
|
|
|
2015-12-05 21:32:53 +00:00
|
|
|
ra, err := strconv.Atoi(hdr.Get("Retry-After"))
|
|
|
|
if err != nil {
|
|
|
|
// The ACME server MUST return a Retry-After.
|
|
|
|
// If it doesn't, we'll just poll hard.
|
2018-05-30 17:53:04 +00:00
|
|
|
ra = 5
|
2015-12-05 11:51:30 +00:00
|
|
|
}
|
2015-12-05 21:32:53 +00:00
|
|
|
time.Sleep(time.Duration(ra) * time.Second)
|
2015-12-05 11:51:30 +00:00
|
|
|
|
2018-05-30 17:53:04 +00:00
|
|
|
hdr, err = getJSON(uri, &chlng)
|
2015-12-05 21:32:53 +00:00
|
|
|
if err != nil {
|
2015-12-05 11:51:30 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|