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
| 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/x509" |
| 19 | "fmt" |
| 20 | |
| 21 | "github.com/caddyserver/caddy/v2" |
| 22 | "github.com/caddyserver/caddy/v2/caddyconfig/caddyfile" |
| 23 | ) |
| 24 | |
| 25 | func init() { |
| 26 | caddy.RegisterModule(LeafPEMLoader{}) |
| 27 | } |
| 28 | |
| 29 | // LeafPEMLoader loads leaf certificates by |
| 30 | // decoding their PEM blocks directly. This has the advantage |
| 31 | // of not needing to store them on disk at all. |
| 32 | type LeafPEMLoader struct { |
| 33 | Certificates []string `json:"certificates,omitempty"` |
| 34 | } |
| 35 | |
| 36 | // Provision implements caddy.Provisioner. |
| 37 | func (pl *LeafPEMLoader) Provision(ctx caddy.Context) error { |
| 38 | repl, ok := ctx.Value(caddy.ReplacerCtxKey).(*caddy.Replacer) |
| 39 | if !ok { |
| 40 | repl = caddy.NewReplacer() |
| 41 | } |
| 42 | for i, cert := range pl.Certificates { |
| 43 | pl.Certificates[i] = repl.ReplaceKnown(cert, "") |
| 44 | } |
| 45 | return nil |
| 46 | } |
| 47 | |
| 48 | // CaddyModule returns the Caddy module information. |
| 49 | func (LeafPEMLoader) CaddyModule() caddy.ModuleInfo { |
| 50 | return caddy.ModuleInfo{ |
| 51 | ID: "tls.leaf_cert_loader.pem", |
| 52 | New: func() caddy.Module { return new(LeafPEMLoader) }, |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | // UnmarshalCaddyfile implements caddyfile.Unmarshaler. |
| 57 | func (fl *LeafPEMLoader) UnmarshalCaddyfile(d *caddyfile.Dispenser) error { |
| 58 | d.NextArg() |
| 59 | fl.Certificates = append(fl.Certificates, d.RemainingArgs()...) |
| 60 | return nil |
| 61 | } |
| 62 | |
| 63 | // LoadLeafCertificates returns the certificates contained in pl. |
| 64 | func (pl LeafPEMLoader) LoadLeafCertificates() ([]*x509.Certificate, error) { |
| 65 | certs := make([]*x509.Certificate, 0, len(pl.Certificates)) |
| 66 | for i, cert := range pl.Certificates { |
| 67 | derBytes, err := convertPEMToDER([]byte(cert)) |
| 68 | if err != nil { |
| 69 | return nil, fmt.Errorf("PEM leaf certificate loader, cert %d: %v", i, err) |
| 70 | } |
| 71 | cert, err := x509.ParseCertificate(derBytes) |
| 72 | if err != nil { |
| 73 | return nil, fmt.Errorf("PEM cert %d: %v", i, err) |
| 74 | } |
| 75 | certs = append(certs, cert) |
| 76 | } |
| 77 | return certs, nil |
| 78 | } |
| 79 | |
| 80 | // Interface guard |
| 81 | var ( |
| 82 | _ LeafCertificateLoader = (*LeafPEMLoader)(nil) |
| 83 | _ caddy.Provisioner = (*LeafPEMLoader)(nil) |
| 84 | ) |