2016-01-04 18:26:32 +00:00
|
|
|
package letsencrypt
|
|
|
|
|
|
|
|
import (
|
2016-01-06 21:19:06 +00:00
|
|
|
"crypto/tls"
|
2016-01-04 18:26:32 +00:00
|
|
|
"encoding/json"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2016-01-06 21:19:06 +00:00
|
|
|
"sync"
|
2016-01-04 18:26:32 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/khlieng/dispatch/Godeps/_workspace/src/github.com/xenolf/lego/acme"
|
|
|
|
)
|
|
|
|
|
|
|
|
const URL = "https://acme-v01.api.letsencrypt.org/directory"
|
|
|
|
const KeySize = 2048
|
|
|
|
|
|
|
|
var directory Directory
|
|
|
|
|
2016-01-06 21:19:06 +00:00
|
|
|
func Run(dir, domain, email, port string) (*state, error) {
|
2016-01-04 18:26:32 +00:00
|
|
|
directory = Directory(dir)
|
|
|
|
|
|
|
|
user, err := getUser(email)
|
|
|
|
if err != nil {
|
2016-01-06 21:19:06 +00:00
|
|
|
return nil, err
|
2016-01-04 18:26:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
client, err := acme.NewClient(URL, &user, KeySize)
|
|
|
|
client.ExcludeChallenges([]string{"tls-sni-01"})
|
|
|
|
client.SetHTTPPort(port)
|
|
|
|
|
|
|
|
if user.Registration == nil {
|
|
|
|
user.Registration, err = client.Register()
|
|
|
|
if err != nil {
|
2016-01-06 21:19:06 +00:00
|
|
|
return nil, err
|
2016-01-04 18:26:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = client.AgreeToTOS()
|
|
|
|
if err != nil {
|
2016-01-06 21:19:06 +00:00
|
|
|
return nil, err
|
2016-01-04 18:26:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = saveUser(user)
|
|
|
|
if err != nil {
|
2016-01-06 21:19:06 +00:00
|
|
|
return nil, err
|
2016-01-04 18:26:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-06 21:19:06 +00:00
|
|
|
s := &state{
|
|
|
|
client: client,
|
|
|
|
domain: domain,
|
|
|
|
}
|
|
|
|
|
2016-01-04 18:26:32 +00:00
|
|
|
if certExists(domain) {
|
2016-01-06 21:19:06 +00:00
|
|
|
if !s.renew() {
|
|
|
|
err = s.loadCert()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s.refreshOCSP()
|
2016-01-04 18:26:32 +00:00
|
|
|
} else {
|
2016-01-06 21:19:06 +00:00
|
|
|
err = s.obtain()
|
2016-01-04 18:26:32 +00:00
|
|
|
if err != nil {
|
2016-01-06 21:19:06 +00:00
|
|
|
return nil, err
|
2016-01-04 18:26:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-06 21:19:06 +00:00
|
|
|
go s.maintain()
|
2016-01-04 18:26:32 +00:00
|
|
|
|
2016-01-06 21:19:06 +00:00
|
|
|
return s, nil
|
2016-01-04 18:26:32 +00:00
|
|
|
}
|
|
|
|
|
2016-01-06 21:19:06 +00:00
|
|
|
type state struct {
|
|
|
|
client *acme.Client
|
|
|
|
domain string
|
|
|
|
cert *tls.Certificate
|
|
|
|
certPEM []byte
|
|
|
|
lock sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *state) GetCertificate(hello *tls.ClientHelloInfo) (*tls.Certificate, error) {
|
|
|
|
s.lock.Lock()
|
|
|
|
cert := s.cert
|
|
|
|
s.lock.Unlock()
|
|
|
|
|
|
|
|
return cert, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *state) getCertPEM() []byte {
|
|
|
|
s.lock.Lock()
|
|
|
|
certPEM := s.certPEM
|
|
|
|
s.lock.Unlock()
|
|
|
|
|
|
|
|
return certPEM
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *state) setCert(meta acme.CertificateResource) {
|
|
|
|
cert, err := tls.X509KeyPair(meta.Certificate, meta.PrivateKey)
|
|
|
|
if err == nil {
|
|
|
|
s.lock.Lock()
|
|
|
|
if s.cert != nil {
|
|
|
|
cert.OCSPStaple = s.cert.OCSPStaple
|
|
|
|
}
|
|
|
|
|
|
|
|
s.cert = &cert
|
|
|
|
s.certPEM = meta.Certificate
|
|
|
|
s.lock.Unlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *state) setOCSP(ocsp []byte) {
|
|
|
|
cert := tls.Certificate{
|
|
|
|
OCSPStaple: ocsp,
|
|
|
|
}
|
|
|
|
|
|
|
|
s.lock.Lock()
|
|
|
|
if s.cert != nil {
|
|
|
|
cert.Certificate = s.cert.Certificate
|
|
|
|
cert.PrivateKey = s.cert.PrivateKey
|
|
|
|
}
|
|
|
|
s.cert = &cert
|
|
|
|
s.lock.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *state) obtain() error {
|
|
|
|
cert, errors := s.client.ObtainCertificate([]string{s.domain}, true)
|
|
|
|
if err := errors[s.domain]; err != nil {
|
2016-01-04 18:26:32 +00:00
|
|
|
if _, ok := err.(acme.TOSError); ok {
|
2016-01-06 21:19:06 +00:00
|
|
|
err := s.client.AgreeToTOS()
|
2016-01-04 18:26:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-01-06 21:19:06 +00:00
|
|
|
return s.obtain()
|
2016-01-04 18:26:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-01-06 21:19:06 +00:00
|
|
|
s.setCert(cert)
|
|
|
|
s.refreshOCSP()
|
|
|
|
|
2016-01-04 18:26:32 +00:00
|
|
|
err := saveCert(cert)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-06 21:19:06 +00:00
|
|
|
func (s *state) renew() bool {
|
|
|
|
cert, err := ioutil.ReadFile(directory.Cert(s.domain))
|
2016-01-04 18:26:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
exp, err := acme.GetPEMCertExpiration(cert)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
daysLeft := int(exp.Sub(time.Now().UTC()).Hours() / 24)
|
|
|
|
|
|
|
|
if daysLeft <= 30 {
|
2016-01-06 21:19:06 +00:00
|
|
|
metaBytes, err := ioutil.ReadFile(directory.Meta(s.domain))
|
2016-01-04 18:26:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-01-06 21:19:06 +00:00
|
|
|
key, err := ioutil.ReadFile(directory.Key(s.domain))
|
2016-01-04 18:26:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
var meta acme.CertificateResource
|
|
|
|
err = json.Unmarshal(metaBytes, &meta)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
meta.Certificate = cert
|
|
|
|
meta.PrivateKey = key
|
|
|
|
|
|
|
|
Renew:
|
2016-01-06 21:19:06 +00:00
|
|
|
newMeta, err := s.client.RenewCertificate(meta, true)
|
2016-01-04 18:26:32 +00:00
|
|
|
if err != nil {
|
|
|
|
if _, ok := err.(acme.TOSError); ok {
|
2016-01-06 21:19:06 +00:00
|
|
|
err := s.client.AgreeToTOS()
|
2016-01-04 18:26:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
goto Renew
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-01-06 21:19:06 +00:00
|
|
|
s.setCert(newMeta)
|
|
|
|
|
2016-01-04 18:26:32 +00:00
|
|
|
err = saveCert(newMeta)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-01-06 21:19:06 +00:00
|
|
|
func (s *state) refreshOCSP() {
|
|
|
|
ocsp, resp, err := acme.GetOCSPForCert(s.getCertPEM())
|
|
|
|
if err == nil && resp.Status == acme.OCSPGood {
|
|
|
|
s.setOCSP(ocsp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *state) maintain() {
|
|
|
|
renew := time.Tick(24 * time.Hour)
|
|
|
|
ocsp := time.Tick(1 * time.Hour)
|
2016-01-04 18:26:32 +00:00
|
|
|
for {
|
2016-01-06 21:19:06 +00:00
|
|
|
select {
|
|
|
|
case <-renew:
|
|
|
|
s.renew()
|
|
|
|
|
|
|
|
case <-ocsp:
|
|
|
|
s.refreshOCSP()
|
2016-01-04 18:26:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-06 21:19:06 +00:00
|
|
|
func (s *state) loadCert() error {
|
|
|
|
cert, err := ioutil.ReadFile(directory.Cert(s.domain))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
key, err := ioutil.ReadFile(directory.Key(s.domain))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
s.setCert(acme.CertificateResource{
|
|
|
|
Certificate: cert,
|
|
|
|
PrivateKey: key,
|
|
|
|
})
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-04 18:26:32 +00:00
|
|
|
func certExists(domain string) bool {
|
|
|
|
if _, err := os.Stat(directory.Cert(domain)); err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if _, err := os.Stat(directory.Key(domain)); err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func saveCert(cert acme.CertificateResource) error {
|
|
|
|
err := os.MkdirAll(directory.Domain(cert.Domain), 0700)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = ioutil.WriteFile(directory.Cert(cert.Domain), cert.Certificate, 0600)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = ioutil.WriteFile(directory.Key(cert.Domain), cert.PrivateKey, 0600)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
jsonBytes, err := json.MarshalIndent(&cert, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = ioutil.WriteFile(directory.Meta(cert.Domain), jsonBytes, 0600)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|