aboutsummaryrefslogtreecommitdiff
path: root/certificate.go
blob: 195d56f7fb1e1a6e2d9488a387c42713a412cad6 (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
package acme

import "crypto/x509"

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

func (p *Provider) Bundle(s Signer, d *Desire) error {
	crt, up, err := p.RequestCert(s, d)
	if err != nil {
		return err
	}
	ca, err := p.GetCert(up)
	if err != nil {
		return err
	}
	d.cert = []*x509.Certificate{crt, ca}
	return nil
}

func (p *Provider) RequestCert(s Signer, d *Desire) (*x509.Certificate, string, error) {
	csr, err := d.CSR()
	if err != nil {
		return nil, "", err
	}
	req := &CSR{
		Resource: ResNewCert,
		CSR:      csr,
	}
	resp, err := p.post(p.NewCert, s, 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) (*x509.Certificate, error) {
	resp, err := p.Get(uri)
	if err != nil {
		return nil, err
	}
	return parseCert(resp)
}