/
pem.go
53 lines (43 loc) · 878 Bytes
/
pem.go
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
package net
import (
"crypto/tls"
"encoding/json"
"github.com/stuphlabs/pullcord/config"
)
func init() {
e := config.RegisterResourceType(
"pem",
func() json.Unmarshaler {
return new(PemConfig)
},
)
if e != nil {
panic(e)
}
}
// PemConfig implements a TlsCertificateGetter using a single PEM encoded key
// and certificate pair.
type PemConfig struct {
Cert []byte
Key []byte
}
// UnmarshalJSON implements encoding/json.Unmarshaler.
func (p *PemConfig) UnmarshalJSON(d []byte) error {
var t struct {
Cert string
Key string
}
if e := json.Unmarshal(d, &t); e != nil {
return e
}
p.Cert = []byte(t.Cert)
p.Key = []byte(t.Key)
return nil
}
// GetCertificate implements TlsCertificateGetter.
func (p *PemConfig) GetCertificate(
*tls.ClientHelloInfo,
) (*tls.Certificate, error) {
c, e := tls.X509KeyPair(p.Cert, p.Key)
return &c, e
}