1
+// Copyright 2015 Matthew Holt and The Caddy Authors
2
+//
3
+// Licensed under the Apache License, Version 2.0 (the "License");
4
+// you may not use this file except in compliance with the License.
5
+// You may obtain a copy of the License at
6
+//
7
+// http://www.apache.org/licenses/LICENSE-2.0
8
+//
9
+// Unless required by applicable law or agreed to in writing, software
10
+// distributed under the License is distributed on an "AS IS" BASIS,
11
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+// See the License for the specific language governing permissions and
13
+// limitations under the License.
14
+
15
+package caddytls
16
+
17
+import (
18
+ "crypto/tls"
19
+ "fmt"
20
+
21
+ "github.com/caddyserver/caddy/v2"
22
+)
23
+
24
+func init() {
25
+ caddy.RegisterModule(PEMLoader{})
26
+}
27
+
28
+// PEMLoader loads certificates and their associated keys by
29
+// decoding their PEM blocks directly. This has the advantage
30
+// of not needing to store them on disk at all.
31
+type PEMLoader []CertKeyPEMPair
32
+
33
+// CaddyModule returns the Caddy module information.
34
+func (PEMLoader) CaddyModule() caddy.ModuleInfo {
35
+ return caddy.ModuleInfo{
36
+ Name: "tls.certificates.load_pem",
37
+ New: func() caddy.Module { return PEMLoader{} },
38
+ }
39
+}
40
+
41
+// CertKeyPEMPair pairs certificate and key PEM blocks.
42
+type CertKeyPEMPair struct {
43
+ CertificatePEM string `json:"certificate"`
44
+ KeyPEM string `json:"key"`
45
+ Tags []string `json:"tags,omitempty"`
46
+}
47
+
48
+// LoadCertificates returns the certificates contained in pl.
49
+func (pl PEMLoader) LoadCertificates() ([]Certificate, error) {
50
+ var certs []Certificate
51
+ for i, pair := range pl {
52
+ cert, err := tls.X509KeyPair([]byte(pair.CertificatePEM), []byte(pair.KeyPEM))
53
+ if err != nil {
54
+ return nil, fmt.Errorf("PEM pair %d: %v", i, err)
55
+ }
56
+ certs = append(certs, Certificate{
57
+ Certificate: cert,
58
+ Tags: pair.Tags,
59
+ })
60
+ }
61
+ return certs, nil
62
+}
63
+
64
+// Interface guard
65
+var _ CertificateLoader = (PEMLoader)(nil)