|
| 1 | +/* |
| 2 | + * Copyright (c) 2016 Moriyoshi Koizumi |
| 3 | + * |
| 4 | + * All rights reserved. |
| 5 | + * |
| 6 | + * Redistribution and use in source and binary forms, with or without |
| 7 | + * modification, are permitted provided that the following conditions are |
| 8 | + * met: |
| 9 | + * |
| 10 | + * * Redistributions of source code must retain the above copyright |
| 11 | + * notice, this list of conditions and the following disclaimer. |
| 12 | + * * Redistributions in binary form must reproduce the above |
| 13 | + * copyright notice, this list of conditions and the following disclaimer |
| 14 | + * in the documentation and/or other materials provided with the |
| 15 | + * distribution. |
| 16 | + * * Neither the name of Elazar Leibovich. nor the names of its |
| 17 | + * contributors may be used to endorse or promote products derived from |
| 18 | + * this software without specific prior written permission. |
| 19 | + * |
| 20 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | + */ |
| 32 | +package main |
| 33 | + |
| 34 | +import ( |
| 35 | + "crypto" |
| 36 | + "crypto/tls" |
| 37 | + "crypto/x509" |
| 38 | + "encoding/pem" |
| 39 | + "fmt" |
| 40 | + "github.com/Sirupsen/logrus" |
| 41 | + "io/ioutil" |
| 42 | + "os" |
| 43 | + "path/filepath" |
| 44 | + "strings" |
| 45 | + "time" |
| 46 | +) |
| 47 | + |
| 48 | +type CertCache struct { |
| 49 | + CacheDir string |
| 50 | + Logger *logrus.Logger |
| 51 | + certs map[string]*tls.Certificate |
| 52 | + issuerCert *x509.Certificate |
| 53 | + privateKey crypto.PrivateKey |
| 54 | +} |
| 55 | + |
| 56 | +const certificateFileName = "cert.pem" |
| 57 | +const certificateBlockName = "CERTIFICATE" |
| 58 | + |
| 59 | +func buildKeyString(hosts []string) string { |
| 60 | + key := strings.Join(hosts, ";") |
| 61 | + return key |
| 62 | +} |
| 63 | + |
| 64 | +func (c *CertCache) writeCertificate(key string, cert *tls.Certificate) (err error) { |
| 65 | + leadingDirs, ok := c.buildPathToCachedCert(key) |
| 66 | + if !ok { |
| 67 | + return |
| 68 | + } |
| 69 | + err = os.MkdirAll(leadingDirs, os.FileMode(0777)) |
| 70 | + if err != nil { |
| 71 | + return |
| 72 | + } |
| 73 | + path := filepath.Join(leadingDirs, certificateFileName) |
| 74 | + w, err := os.OpenFile(path, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, os.FileMode(0666)) |
| 75 | + if err != nil { |
| 76 | + return |
| 77 | + } |
| 78 | + defer w.Close() |
| 79 | + err = pem.Encode(w, &pem.Block{Type: certificateBlockName, Bytes: cert.Certificate[0]}) |
| 80 | + if err != nil { |
| 81 | + return |
| 82 | + } |
| 83 | + return nil |
| 84 | +} |
| 85 | + |
| 86 | +func (c *CertCache) buildPathToCachedCert(key string) (string, bool) { |
| 87 | + if c.CacheDir == "" { |
| 88 | + return "", false |
| 89 | + } |
| 90 | + return filepath.Join(c.CacheDir, key), true |
| 91 | +} |
| 92 | + |
| 93 | +func (c *CertCache) readAndValidateCertificate(key string, hosts []string, now time.Time) (*tls.Certificate, error) { |
| 94 | + leadingDirs, ok := c.buildPathToCachedCert(key) |
| 95 | + if !ok { |
| 96 | + return nil, nil |
| 97 | + } |
| 98 | + path := filepath.Join(leadingDirs, certificateFileName) |
| 99 | + pemBytes, err := ioutil.ReadFile(path) |
| 100 | + if err != nil { |
| 101 | + return nil, err |
| 102 | + } |
| 103 | + certDerBytes := []byte(nil) |
| 104 | + for { |
| 105 | + var pemBlock *pem.Block |
| 106 | + pemBlock, pemBytes = pem.Decode(pemBytes) |
| 107 | + if pemBlock == nil { |
| 108 | + break |
| 109 | + } |
| 110 | + if pemBlock.Type == certificateBlockName { |
| 111 | + certDerBytes = pemBlock.Bytes |
| 112 | + break |
| 113 | + } |
| 114 | + } |
| 115 | + if certDerBytes == nil { |
| 116 | + return nil, fmt.Errorf("No valid certificate contained in %s", path) |
| 117 | + } |
| 118 | + x509Cert, err := x509.ParseCertificate(certDerBytes) |
| 119 | + if err != nil { |
| 120 | + return nil, fmt.Errorf("Invalid certificate found in %s (%s)", path, err.Error()) |
| 121 | + } |
| 122 | + x509Cert.RawIssuer = c.issuerCert.Raw |
| 123 | + err = x509Cert.CheckSignatureFrom(c.issuerCert) |
| 124 | + if err != nil { |
| 125 | + return nil, fmt.Errorf("Invalid certificate found in %s (%s)", path, err.Error()) |
| 126 | + } |
| 127 | + if !now.Before(x509Cert.NotAfter) { |
| 128 | + return nil, fmt.Errorf("Ceritificate no longer valid (not after: %s, now: %s)", x509Cert.NotAfter.Format(time.RFC1123), now.Format(time.RFC1123)) |
| 129 | + } |
| 130 | + |
| 131 | +outer: |
| 132 | + for _, a := range hosts { |
| 133 | + for _, b := range x509Cert.DNSNames { |
| 134 | + if a == b { |
| 135 | + break outer |
| 136 | + } |
| 137 | + } |
| 138 | + return nil, fmt.Errorf("Certificate does not cover the host name %s", a) |
| 139 | + } |
| 140 | + |
| 141 | + return &tls.Certificate{ |
| 142 | + Certificate: [][]byte{certDerBytes, c.issuerCert.Raw}, |
| 143 | + PrivateKey: c.privateKey, |
| 144 | + }, nil |
| 145 | +} |
| 146 | + |
| 147 | +func (c *CertCache) readCertificate(key string, hosts []string, now time.Time) (cert *tls.Certificate, err error) { |
| 148 | + cert, err = c.readAndValidateCertificate( |
| 149 | + key, |
| 150 | + hosts, |
| 151 | + now, |
| 152 | + ) |
| 153 | + if err != nil { |
| 154 | + c.Logger.Warn(err.Error()) |
| 155 | + err = nil |
| 156 | + } |
| 157 | + return |
| 158 | +} |
| 159 | + |
| 160 | +func (c *CertCache) Put(hosts []string, cert *tls.Certificate) error { |
| 161 | + key := buildKeyString(hosts) |
| 162 | + c.certs[key] = cert |
| 163 | + return c.writeCertificate(key, cert) |
| 164 | +} |
| 165 | + |
| 166 | +func (c *CertCache) Get(hosts []string, now time.Time) (cert *tls.Certificate, err error) { |
| 167 | + key := buildKeyString(hosts) |
| 168 | + cert, ok := c.certs[key] |
| 169 | + if !ok { |
| 170 | + c.Logger.Debug("Certificate not found in in-process cache") |
| 171 | + cert, err = c.readCertificate(key, hosts, now) |
| 172 | + if err != nil { |
| 173 | + return |
| 174 | + } |
| 175 | + if cert != nil { |
| 176 | + c.certs[key] = cert |
| 177 | + } |
| 178 | + } |
| 179 | + return |
| 180 | +} |
| 181 | + |
| 182 | +func NewCertCache(cacheDir string, logger *logrus.Logger, issuerCert *x509.Certificate, privateKey crypto.PrivateKey) *CertCache { |
| 183 | + return &CertCache{ |
| 184 | + CacheDir: cacheDir, |
| 185 | + Logger: logger, |
| 186 | + certs: make(map[string]*tls.Certificate), |
| 187 | + issuerCert: issuerCert, |
| 188 | + privateKey: privateKey, |
| 189 | + } |
| 190 | +} |
0 commit comments