@marcel91200 / caddy Mirror · read-only
Fork 0

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS

Mirrored from https://github.com/Slazee/caddy.git

Synced Nov 11, 01:00 UTC

master
go 94 lines 2.65 KB
Raw
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 // Provision implements caddy.Provisioner.
34 func (pl PEMLoader) Provision(ctx caddy.Context) error {
35 repl, ok := ctx.Value(caddy.ReplacerCtxKey).(*caddy.Replacer)
36 if !ok {
37 repl = caddy.NewReplacer()
38 }
39 for k, pair := range pl {
40 for i, tag := range pair.Tags {
41 pair.Tags[i] = repl.ReplaceKnown(tag, "")
42 }
43 pl[k] = CertKeyPEMPair{
44 CertificatePEM: repl.ReplaceKnown(pair.CertificatePEM, ""),
45 KeyPEM: repl.ReplaceKnown(pair.KeyPEM, ""),
46 Tags: pair.Tags,
47 }
48 }
49 return nil
50 }
51
52 // CaddyModule returns the Caddy module information.
53 func (PEMLoader) CaddyModule() caddy.ModuleInfo {
54 return caddy.ModuleInfo{
55 ID: "tls.certificates.load_pem",
56 New: func() caddy.Module { return new(PEMLoader) },
57 }
58 }
59
60 // CertKeyPEMPair pairs certificate and key PEM blocks.
61 type CertKeyPEMPair struct {
62 // The certificate (public key) in PEM format.
63 CertificatePEM string `json:"certificate"`
64
65 // The private key in PEM format.
66 KeyPEM string `json:"key"`
67
68 // Arbitrary values to associate with this certificate.
69 // Can be useful when you want to select a particular
70 // certificate when there may be multiple valid candidates.
71 Tags []string `json:"tags,omitempty"`
72 }
73
74 // LoadCertificates returns the certificates contained in pl.
75 func (pl PEMLoader) LoadCertificates() ([]Certificate, error) {
76 certs := make([]Certificate, 0, len(pl))
77 for i, pair := range pl {
78 cert, err := tls.X509KeyPair([]byte(pair.CertificatePEM), []byte(pair.KeyPEM))
79 if err != nil {
80 return nil, fmt.Errorf("PEM pair %d: %v", i, err)
81 }
82 certs = append(certs, Certificate{
83 Certificate: cert,
84 Tags: pair.Tags,
85 })
86 }
87 return certs, nil
88 }
89
90 // Interface guard
91 var (
92 _ CertificateLoader = PEMLoader(nil)
93 _ caddy.Provisioner = PEMLoader(nil)
94 )