aboutsummaryrefslogtreecommitdiff
path: root/certificate.go
blob: 992acb7701cc564fdf4123c26bc6049ef9458834 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package acme

import (
	"crypto"
	"crypto/tls"
)

type CSR struct {
	Resource Resource `json:"resource"` // new-cert
	CSR      string   `json:"csr"`
}

func (p *Provider) Bundle(key crypto.PrivateKey, altnames []string, email string) (tls.Certificate, error) {
	cert := tls.Certificate{PrivateKey: key}
	csr, err := NewCSR(key, altnames, email)
	if err != nil {
		return cert, err
	}
	crt, up, err := p.RequestCert(csr)
	if err != nil {
		return cert, err
	}
	ca, err := p.GetCert(up)
	if err != nil {
		return cert, err
	}
	cert.Certificate = [][]byte{crt, ca}
	return cert, nil
}

func (p *Provider) RequestCert(csr string) ([]byte, string, error) {
	req := &CSR{
		Resource: ResNewCert,
		CSR:      csr,
	}
	resp, err := p.postJson(p.NewCert, req)
	if err != nil {
		return nil, "", err
	}
	ns := parseHeader(resp)
	crt, err := parseCert(resp)
	if err != nil {
		return nil, "", err
	}
	return crt, ns.Link["up"], nil
}

func (p *Provider) GetCert(uri string) ([]byte, error) {
	resp, err := p.Get(uri)
	if err != nil {
		return nil, err
	}
	return parseCert(resp)
}