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

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

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

func (p *Provider) RequestCert(s Signer, d *Desire, csr string) ([]byte, string, error) {
	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) ([]byte, error) {
	resp, err := p.Get(uri)
	if err != nil {
		return nil, err
	}
	return parseCert(resp)
}