2015-06-13 01:57:05 +00:00
|
|
|
package acme
|
|
|
|
|
|
|
|
import (
|
2015-10-23 23:13:50 +00:00
|
|
|
"bytes"
|
2015-10-23 14:24:02 +00:00
|
|
|
"crypto"
|
|
|
|
"crypto/ecdsa"
|
|
|
|
"crypto/elliptic"
|
2015-06-13 01:57:05 +00:00
|
|
|
"crypto/rand"
|
|
|
|
"crypto/rsa"
|
|
|
|
"crypto/x509"
|
|
|
|
"crypto/x509/pkix"
|
2018-05-30 17:53:04 +00:00
|
|
|
"encoding/asn1"
|
2015-11-16 22:57:04 +00:00
|
|
|
"encoding/base64"
|
2015-06-13 01:57:05 +00:00
|
|
|
"encoding/pem"
|
2015-10-23 23:13:50 +00:00
|
|
|
"errors"
|
2015-10-18 19:18:36 +00:00
|
|
|
"fmt"
|
2015-12-18 21:33:30 +00:00
|
|
|
"io"
|
2015-10-23 23:13:50 +00:00
|
|
|
"io/ioutil"
|
2015-10-16 19:05:16 +00:00
|
|
|
"math/big"
|
2015-10-23 23:13:50 +00:00
|
|
|
"net/http"
|
2015-10-16 19:05:16 +00:00
|
|
|
"time"
|
2015-10-23 14:24:02 +00:00
|
|
|
|
2015-10-23 23:13:50 +00:00
|
|
|
"golang.org/x/crypto/ocsp"
|
2018-05-30 17:53:04 +00:00
|
|
|
jose "gopkg.in/square/go-jose.v2"
|
2015-06-13 01:57:05 +00:00
|
|
|
)
|
|
|
|
|
2016-01-27 01:01:39 +00:00
|
|
|
// KeyType represents the key algo as well as the key size or curve to use.
|
|
|
|
type KeyType string
|
2015-10-18 01:29:26 +00:00
|
|
|
type derCertificateBytes []byte
|
|
|
|
|
2016-02-14 21:31:17 +00:00
|
|
|
// Constants for all key types we support.
|
2015-10-23 14:24:02 +00:00
|
|
|
const (
|
2016-01-27 01:01:39 +00:00
|
|
|
EC256 = KeyType("P256")
|
2016-02-14 21:31:17 +00:00
|
|
|
EC384 = KeyType("P384")
|
2016-01-27 01:01:39 +00:00
|
|
|
RSA2048 = KeyType("2048")
|
|
|
|
RSA4096 = KeyType("4096")
|
|
|
|
RSA8192 = KeyType("8192")
|
2015-10-23 14:24:02 +00:00
|
|
|
)
|
|
|
|
|
2015-10-27 22:55:50 +00:00
|
|
|
const (
|
|
|
|
// OCSPGood means that the certificate is valid.
|
|
|
|
OCSPGood = ocsp.Good
|
|
|
|
// OCSPRevoked means that the certificate has been deliberately revoked.
|
|
|
|
OCSPRevoked = ocsp.Revoked
|
|
|
|
// OCSPUnknown means that the OCSP responder doesn't know about the certificate.
|
|
|
|
OCSPUnknown = ocsp.Unknown
|
|
|
|
// OCSPServerFailed means that the OCSP responder failed to process the request.
|
|
|
|
OCSPServerFailed = ocsp.ServerFailed
|
|
|
|
)
|
|
|
|
|
2016-10-27 09:22:10 +00:00
|
|
|
// Constants for OCSP must staple
|
|
|
|
var (
|
|
|
|
tlsFeatureExtensionOID = asn1.ObjectIdentifier{1, 3, 6, 1, 5, 5, 7, 1, 24}
|
|
|
|
ocspMustStapleFeature = []byte{0x30, 0x03, 0x02, 0x01, 0x05}
|
|
|
|
)
|
|
|
|
|
2015-10-27 22:55:50 +00:00
|
|
|
// GetOCSPForCert takes a PEM encoded cert or cert bundle returning the raw OCSP response,
|
2015-12-31 23:07:18 +00:00
|
|
|
// the parsed response, and an error, if any. The returned []byte can be passed directly
|
|
|
|
// into the OCSPStaple property of a tls.Certificate. If the bundle only contains the
|
|
|
|
// issued certificate, this function will try to get the issuer certificate from the
|
|
|
|
// IssuingCertificateURL in the certificate. If the []byte and/or ocsp.Response return
|
|
|
|
// values are nil, the OCSP status may be assumed OCSPUnknown.
|
|
|
|
func GetOCSPForCert(bundle []byte) ([]byte, *ocsp.Response, error) {
|
2015-10-23 23:13:50 +00:00
|
|
|
certificates, err := parsePEMBundle(bundle)
|
|
|
|
if err != nil {
|
2015-12-31 23:07:18 +00:00
|
|
|
return nil, nil, err
|
2015-10-23 23:13:50 +00:00
|
|
|
}
|
|
|
|
|
2016-02-14 06:24:19 +00:00
|
|
|
// We expect the certificate slice to be ordered downwards the chain.
|
|
|
|
// SRV CRT -> CA. We need to pull the leaf and issuer certs out of it,
|
|
|
|
// which should always be the first two certificates. If there's no
|
|
|
|
// OCSP server listed in the leaf cert, there's nothing to do. And if
|
|
|
|
// we have only one certificate so far, we need to get the issuer cert.
|
|
|
|
issuedCert := certificates[0]
|
|
|
|
if len(issuedCert.OCSPServer) == 0 {
|
|
|
|
return nil, nil, errors.New("no OCSP server specified in cert")
|
|
|
|
}
|
2015-10-23 23:13:50 +00:00
|
|
|
if len(certificates) == 1 {
|
|
|
|
// TODO: build fallback. If this fails, check the remaining array entries.
|
2016-02-14 06:24:19 +00:00
|
|
|
if len(issuedCert.IssuingCertificateURL) == 0 {
|
2015-12-31 23:07:18 +00:00
|
|
|
return nil, nil, errors.New("no issuing certificate URL")
|
2015-10-29 03:36:02 +00:00
|
|
|
}
|
|
|
|
|
2018-09-24 19:07:20 +00:00
|
|
|
resp, errC := httpGet(issuedCert.IssuingCertificateURL[0])
|
|
|
|
if errC != nil {
|
|
|
|
return nil, nil, errC
|
2015-10-23 23:13:50 +00:00
|
|
|
}
|
2015-10-30 21:38:55 +00:00
|
|
|
defer resp.Body.Close()
|
2015-10-23 23:13:50 +00:00
|
|
|
|
2018-09-24 19:07:20 +00:00
|
|
|
issuerBytes, errC := ioutil.ReadAll(limitReader(resp.Body, 1024*1024))
|
|
|
|
if errC != nil {
|
|
|
|
return nil, nil, errC
|
2015-10-23 23:13:50 +00:00
|
|
|
}
|
|
|
|
|
2018-09-24 19:07:20 +00:00
|
|
|
issuerCert, errC := x509.ParseCertificate(issuerBytes)
|
|
|
|
if errC != nil {
|
|
|
|
return nil, nil, errC
|
2015-10-23 23:13:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Insert it into the slice on position 0
|
2015-10-24 02:31:12 +00:00
|
|
|
// We want it ordered right SRV CRT -> CA
|
|
|
|
certificates = append(certificates, issuerCert)
|
2015-10-23 23:13:50 +00:00
|
|
|
}
|
2015-10-24 02:31:12 +00:00
|
|
|
issuerCert := certificates[1]
|
2015-10-23 23:13:50 +00:00
|
|
|
|
|
|
|
// Finally kick off the OCSP request.
|
|
|
|
ocspReq, err := ocsp.CreateRequest(issuedCert, issuerCert, nil)
|
|
|
|
if err != nil {
|
2015-12-31 23:07:18 +00:00
|
|
|
return nil, nil, err
|
2015-10-23 23:13:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
reader := bytes.NewReader(ocspReq)
|
2015-12-30 22:01:21 +00:00
|
|
|
req, err := httpPost(issuedCert.OCSPServer[0], "application/ocsp-request", reader)
|
2015-10-23 23:13:50 +00:00
|
|
|
if err != nil {
|
2015-12-31 23:07:18 +00:00
|
|
|
return nil, nil, err
|
2015-10-23 23:13:50 +00:00
|
|
|
}
|
2015-10-30 21:38:55 +00:00
|
|
|
defer req.Body.Close()
|
2015-12-24 08:57:09 +00:00
|
|
|
|
2015-12-18 21:33:30 +00:00
|
|
|
ocspResBytes, err := ioutil.ReadAll(limitReader(req.Body, 1024*1024))
|
2018-05-30 17:53:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2015-10-27 21:31:56 +00:00
|
|
|
ocspRes, err := ocsp.ParseResponse(ocspResBytes, issuerCert)
|
2015-10-23 23:13:50 +00:00
|
|
|
if err != nil {
|
2015-12-31 23:07:18 +00:00
|
|
|
return nil, nil, err
|
2015-10-23 23:13:50 +00:00
|
|
|
}
|
|
|
|
|
2015-12-31 23:07:18 +00:00
|
|
|
return ocspResBytes, ocspRes, nil
|
2015-10-23 23:13:50 +00:00
|
|
|
}
|
|
|
|
|
2015-11-16 22:57:04 +00:00
|
|
|
func getKeyAuthorization(token string, key interface{}) (string, error) {
|
2016-01-27 01:01:39 +00:00
|
|
|
var publicKey crypto.PublicKey
|
|
|
|
switch k := key.(type) {
|
|
|
|
case *ecdsa.PrivateKey:
|
|
|
|
publicKey = k.Public()
|
|
|
|
case *rsa.PrivateKey:
|
|
|
|
publicKey = k.Public()
|
|
|
|
}
|
|
|
|
|
2015-11-16 22:57:04 +00:00
|
|
|
// Generate the Key Authorization for the challenge
|
2018-05-30 17:53:04 +00:00
|
|
|
jwk := &jose.JSONWebKey{Key: publicKey}
|
2015-11-16 22:57:04 +00:00
|
|
|
if jwk == nil {
|
2018-05-30 17:53:04 +00:00
|
|
|
return "", errors.New("could not generate JWK from key")
|
2015-11-16 22:57:04 +00:00
|
|
|
}
|
|
|
|
thumbBytes, err := jwk.Thumbprint(crypto.SHA256)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
// unpad the base64URL
|
2018-05-30 17:53:04 +00:00
|
|
|
keyThumb := base64.RawURLEncoding.EncodeToString(thumbBytes)
|
2015-11-16 22:57:04 +00:00
|
|
|
|
|
|
|
return token + "." + keyThumb, nil
|
|
|
|
}
|
|
|
|
|
2015-10-24 01:55:18 +00:00
|
|
|
// parsePEMBundle parses a certificate bundle from top to bottom and returns
|
|
|
|
// a slice of x509 certificates. This function will error if no certificates are found.
|
2015-10-23 23:13:50 +00:00
|
|
|
func parsePEMBundle(bundle []byte) ([]*x509.Certificate, error) {
|
|
|
|
var certificates []*x509.Certificate
|
2016-01-11 17:02:28 +00:00
|
|
|
var certDERBlock *pem.Block
|
2015-10-23 23:13:50 +00:00
|
|
|
|
2016-01-11 17:02:28 +00:00
|
|
|
for {
|
|
|
|
certDERBlock, bundle = pem.Decode(bundle)
|
|
|
|
if certDERBlock == nil {
|
|
|
|
break
|
2015-10-23 23:13:50 +00:00
|
|
|
}
|
|
|
|
|
2016-01-11 17:02:28 +00:00
|
|
|
if certDERBlock.Type == "CERTIFICATE" {
|
|
|
|
cert, err := x509.ParseCertificate(certDERBlock.Bytes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
certificates = append(certificates, cert)
|
2015-10-23 23:13:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(certificates) == 0 {
|
2018-05-30 17:53:04 +00:00
|
|
|
return nil, errors.New("no certificates were found while parsing the bundle")
|
2015-10-23 23:13:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return certificates, nil
|
|
|
|
}
|
|
|
|
|
2016-01-08 09:14:41 +00:00
|
|
|
func parsePEMPrivateKey(key []byte) (crypto.PrivateKey, error) {
|
|
|
|
keyBlock, _ := pem.Decode(key)
|
|
|
|
|
|
|
|
switch keyBlock.Type {
|
|
|
|
case "RSA PRIVATE KEY":
|
|
|
|
return x509.ParsePKCS1PrivateKey(keyBlock.Bytes)
|
|
|
|
case "EC PRIVATE KEY":
|
|
|
|
return x509.ParseECPrivateKey(keyBlock.Bytes)
|
|
|
|
default:
|
2018-05-30 17:53:04 +00:00
|
|
|
return nil, errors.New("unknown PEM header value")
|
2016-01-08 09:14:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-27 01:01:39 +00:00
|
|
|
func generatePrivateKey(keyType KeyType) (crypto.PrivateKey, error) {
|
|
|
|
|
|
|
|
switch keyType {
|
|
|
|
case EC256:
|
|
|
|
return ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
|
|
|
case EC384:
|
2015-10-23 14:24:02 +00:00
|
|
|
return ecdsa.GenerateKey(elliptic.P384(), rand.Reader)
|
2016-01-27 01:01:39 +00:00
|
|
|
case RSA2048:
|
|
|
|
return rsa.GenerateKey(rand.Reader, 2048)
|
|
|
|
case RSA4096:
|
|
|
|
return rsa.GenerateKey(rand.Reader, 4096)
|
|
|
|
case RSA8192:
|
|
|
|
return rsa.GenerateKey(rand.Reader, 8192)
|
2015-10-23 14:24:02 +00:00
|
|
|
}
|
|
|
|
|
2018-05-30 17:53:04 +00:00
|
|
|
return nil, fmt.Errorf("invalid KeyType: %s", keyType)
|
2015-06-13 01:57:05 +00:00
|
|
|
}
|
|
|
|
|
2016-10-27 09:22:10 +00:00
|
|
|
func generateCsr(privateKey crypto.PrivateKey, domain string, san []string, mustStaple bool) ([]byte, error) {
|
2015-06-13 01:57:05 +00:00
|
|
|
template := x509.CertificateRequest{
|
2018-06-30 23:06:46 +00:00
|
|
|
Subject: pkix.Name{CommonName: domain},
|
2015-06-13 01:57:05 +00:00
|
|
|
}
|
|
|
|
|
2015-11-11 00:01:15 +00:00
|
|
|
if len(san) > 0 {
|
|
|
|
template.DNSNames = san
|
|
|
|
}
|
|
|
|
|
2016-10-27 09:22:10 +00:00
|
|
|
if mustStaple {
|
2016-12-06 07:41:28 +00:00
|
|
|
template.ExtraExtensions = append(template.ExtraExtensions, pkix.Extension{
|
2016-10-27 09:22:10 +00:00
|
|
|
Id: tlsFeatureExtensionOID,
|
|
|
|
Value: ocspMustStapleFeature,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-06-13 01:57:05 +00:00
|
|
|
return x509.CreateCertificateRequest(rand.Reader, &template, privateKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
func pemEncode(data interface{}) []byte {
|
|
|
|
var pemBlock *pem.Block
|
|
|
|
switch key := data.(type) {
|
2016-01-27 01:01:39 +00:00
|
|
|
case *ecdsa.PrivateKey:
|
|
|
|
keyBytes, _ := x509.MarshalECPrivateKey(key)
|
|
|
|
pemBlock = &pem.Block{Type: "EC PRIVATE KEY", Bytes: keyBytes}
|
2015-06-13 01:57:05 +00:00
|
|
|
case *rsa.PrivateKey:
|
|
|
|
pemBlock = &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(key)}
|
2016-06-08 02:50:52 +00:00
|
|
|
case *x509.CertificateRequest:
|
|
|
|
pemBlock = &pem.Block{Type: "CERTIFICATE REQUEST", Bytes: key.Raw}
|
2015-10-18 01:29:26 +00:00
|
|
|
case derCertificateBytes:
|
2015-10-18 02:58:14 +00:00
|
|
|
pemBlock = &pem.Block{Type: "CERTIFICATE", Bytes: []byte(data.(derCertificateBytes))}
|
2015-06-13 01:57:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return pem.EncodeToMemory(pemBlock)
|
|
|
|
}
|
2015-10-16 19:05:16 +00:00
|
|
|
|
2015-10-18 19:18:36 +00:00
|
|
|
func pemDecode(data []byte) (*pem.Block, error) {
|
|
|
|
pemBlock, _ := pem.Decode(data)
|
|
|
|
if pemBlock == nil {
|
|
|
|
return nil, fmt.Errorf("Pem decode did not yield a valid block. Is the certificate in the right format?")
|
|
|
|
}
|
|
|
|
|
|
|
|
return pemBlock, nil
|
|
|
|
}
|
|
|
|
|
2016-06-08 02:50:52 +00:00
|
|
|
func pemDecodeTox509CSR(pem []byte) (*x509.CertificateRequest, error) {
|
|
|
|
pemBlock, err := pemDecode(pem)
|
|
|
|
if pemBlock == nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if pemBlock.Type != "CERTIFICATE REQUEST" {
|
|
|
|
return nil, fmt.Errorf("PEM block is not a certificate request")
|
|
|
|
}
|
|
|
|
|
|
|
|
return x509.ParseCertificateRequest(pemBlock.Bytes)
|
|
|
|
}
|
|
|
|
|
2015-10-18 19:18:36 +00:00
|
|
|
// GetPEMCertExpiration returns the "NotAfter" date of a PEM encoded certificate.
|
|
|
|
// The certificate has to be PEM encoded. Any other encodings like DER will fail.
|
|
|
|
func GetPEMCertExpiration(cert []byte) (time.Time, error) {
|
|
|
|
pemBlock, err := pemDecode(cert)
|
|
|
|
if pemBlock == nil {
|
|
|
|
return time.Time{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return getCertExpiration(pemBlock.Bytes)
|
|
|
|
}
|
|
|
|
|
|
|
|
// getCertExpiration returns the "NotAfter" date of a DER encoded certificate.
|
|
|
|
func getCertExpiration(cert []byte) (time.Time, error) {
|
2015-10-16 19:05:16 +00:00
|
|
|
pCert, err := x509.ParseCertificate(cert)
|
|
|
|
if err != nil {
|
|
|
|
return time.Time{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return pCert.NotAfter, nil
|
|
|
|
}
|
|
|
|
|
2018-06-13 23:20:56 +00:00
|
|
|
func generatePemCert(privKey *rsa.PrivateKey, domain string, extensions []pkix.Extension) ([]byte, error) {
|
|
|
|
derBytes, err := generateDerCert(privKey, time.Time{}, domain, extensions)
|
2015-10-16 19:05:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: derBytes}), nil
|
|
|
|
}
|
|
|
|
|
2018-06-13 23:20:56 +00:00
|
|
|
func generateDerCert(privKey *rsa.PrivateKey, expiration time.Time, domain string, extensions []pkix.Extension) ([]byte, error) {
|
2015-10-16 19:05:16 +00:00
|
|
|
serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128)
|
|
|
|
serialNumber, err := rand.Int(rand.Reader, serialNumberLimit)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-10-17 22:25:46 +00:00
|
|
|
if expiration.IsZero() {
|
2015-10-16 19:05:16 +00:00
|
|
|
expiration = time.Now().Add(365)
|
|
|
|
}
|
|
|
|
|
|
|
|
template := x509.Certificate{
|
|
|
|
SerialNumber: serialNumber,
|
|
|
|
Subject: pkix.Name{
|
|
|
|
CommonName: "ACME Challenge TEMP",
|
|
|
|
},
|
|
|
|
NotBefore: time.Now(),
|
|
|
|
NotAfter: expiration,
|
|
|
|
|
|
|
|
KeyUsage: x509.KeyUsageKeyEncipherment,
|
|
|
|
BasicConstraintsValid: true,
|
|
|
|
DNSNames: []string{domain},
|
2018-06-13 23:20:56 +00:00
|
|
|
ExtraExtensions: extensions,
|
2015-10-16 19:05:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return x509.CreateCertificate(rand.Reader, &template, &template, &privKey.PublicKey, privKey)
|
|
|
|
}
|
2015-12-18 21:33:30 +00:00
|
|
|
|
|
|
|
func limitReader(rd io.ReadCloser, numBytes int64) io.ReadCloser {
|
|
|
|
return http.MaxBytesReader(nil, rd, numBytes)
|
|
|
|
}
|