| 1 |
// Copyright The Notary Project Authors. |
| 2 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 |
// you may not use this file except in compliance with the License. |
| 4 |
// You may obtain a copy of the License at |
| 5 |
// |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
// Package crl provides functionalities for crl revocation check. |
| 15 |
package crl |
| 16 |
|
| 17 |
import ( |
| 18 |
"context" |
| 19 |
"crypto/sha256" |
| 20 |
"crypto/x509" |
| 21 |
"encoding/hex" |
| 22 |
"encoding/json" |
| 23 |
"errors" |
| 24 |
"fmt" |
| 25 |
"io/fs" |
| 26 |
"os" |
| 27 |
"path/filepath" |
| 28 |
"time" |
| 29 |
|
| 30 |
corecrl "github.com/notaryproject/notation-core-go/revocation/crl" |
| 31 |
"github.com/notaryproject/notation-go/internal/file" |
| 32 |
"github.com/notaryproject/notation-go/log" |
| 33 |
) |
| 34 |
|
| 35 |
// FileCache implements corecrl.Cache. |
| 36 |
// |
| 37 |
// Key: url of the CRL. |
| 38 |
// |
| 39 |
// Value: corecrl.Bundle. |
| 40 |
// |
| 41 |
// This cache builds on top of the UNIX file system to leverage the file system's |
| 42 |
// atomic operations. The `rename` and `remove` operations will unlink the old |
| 43 |
// file but keep the inode and file descriptor for existing processes to access |
| 44 |
// the file. The old inode will be dereferenced when all processes close the old |
| 45 |
// file descriptor. Additionally, the operations are proven to be atomic on |
| 46 |
// UNIX-like platforms, so there is no need to handle file locking. |
| 47 |
// |
| 48 |
// NOTE: For Windows, the `open`, `rename` and `remove` operations need file |
| 49 |
// locking to ensure atomicity. The current implementation does not handle |
| 50 |
// file locking, so the concurrent write from multiple processes may be failed. |
| 51 |
// Please do not use this cache in a multi-process environment on Windows. |
| 52 |
type FileCache struct { |
| 53 |
// root is the root directory of the cache |
| 54 |
root string |
| 55 |
} |
| 56 |
|
| 57 |
// fileCacheContent is the actual content saved in a FileCache |
| 58 |
type fileCacheContent struct { |
| 59 |
// BaseCRL is the ASN.1 encoded base CRL |
| 60 |
BaseCRL []byte `json:"baseCRL"` |
| 61 |
|
| 62 |
// DeltaCRL is the ASN.1 encoded delta CRL |
| 63 |
DeltaCRL []byte `json:"deltaCRL,omitempty"` |
| 64 |
} |
| 65 |
|
| 66 |
// NewFileCache creates a FileCache with root as the root directory |
| 67 |
// |
| 68 |
// An example for root is `dir.CacheFS().SysPath(dir.PathCRLCache)` |
| 69 |
func NewFileCache(root string) (*FileCache, error) { |
| 70 |
if err := os.MkdirAll(root, 0700); err != nil { |
| 71 |
return nil, fmt.Errorf("failed to create crl file cache: %w", err) |
| 72 |
} |
| 73 |
return &FileCache{ |
| 74 |
root: root, |
| 75 |
}, nil |
| 76 |
} |
| 77 |
|
| 78 |
// Get retrieves CRL bundle from c given url as key. If the key does not exist |
| 79 |
// or the content has expired, corecrl.ErrCacheMiss is returned. |
| 80 |
func (c *FileCache) Get(ctx context.Context, url string) (*corecrl.Bundle, error) { |
| 81 |
logger := log.GetLogger(ctx) |
| 82 |
logger.Debugf("Retrieving crl bundle from file cache with key %q ...", url) |
| 83 |
|
| 84 |
// get content from file cache |
| 85 |
contentBytes, err := os.ReadFile(filepath.Join(c.root, c.fileName(url))) |
| 86 |
if err != nil { |
| 87 |
if errors.Is(err, fs.ErrNotExist) { |
| 88 |
logger.Debugf("CRL file cache miss. Key %q does not exist", url) |
| 89 |
return nil, corecrl.ErrCacheMiss |
| 90 |
} |
| 91 |
return nil, fmt.Errorf("failed to get crl bundle from file cache with key %q: %w", url, err) |
| 92 |
} |
| 93 |
|
| 94 |
// decode content to crl Bundle |
| 95 |
var content fileCacheContent |
| 96 |
if err := json.Unmarshal(contentBytes, &content); err != nil { |
| 97 |
return nil, fmt.Errorf("failed to decode file retrieved from file cache: %w", err) |
| 98 |
} |
| 99 |
var bundle corecrl.Bundle |
| 100 |
bundle.BaseCRL, err = x509.ParseRevocationList(content.BaseCRL) |
| 101 |
if err != nil { |
| 102 |
return nil, fmt.Errorf("failed to parse base CRL of file retrieved from file cache: %w", err) |
| 103 |
} |
| 104 |
if content.DeltaCRL != nil { |
| 105 |
bundle.DeltaCRL, err = x509.ParseRevocationList(content.DeltaCRL) |
| 106 |
if err != nil { |
| 107 |
return nil, fmt.Errorf("failed to parse delta CRL of file retrieved from file cache: %w", err) |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
// check expiry |
| 112 |
if err := checkExpiry(ctx, bundle.BaseCRL.NextUpdate); err != nil { |
| 113 |
return nil, fmt.Errorf("check BaseCRL expiry failed: %w", err) |
| 114 |
} |
| 115 |
if bundle.DeltaCRL != nil { |
| 116 |
if err := checkExpiry(ctx, bundle.DeltaCRL.NextUpdate); err != nil { |
| 117 |
return nil, fmt.Errorf("check DeltaCRL expiry failed: %w", err) |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
return &bundle, nil |
| 122 |
} |
| 123 |
|
| 124 |
// Set stores the CRL bundle in c with url as key. |
| 125 |
func (c *FileCache) Set(ctx context.Context, url string, bundle *corecrl.Bundle) error { |
| 126 |
logger := log.GetLogger(ctx) |
| 127 |
logger.Debugf("Storing crl bundle to file cache with key %q ...", url) |
| 128 |
|
| 129 |
if bundle == nil { |
| 130 |
return errors.New("failed to store crl bundle in file cache: bundle cannot be nil") |
| 131 |
} |
| 132 |
if bundle.BaseCRL == nil { |
| 133 |
return errors.New("failed to store crl bundle in file cache: bundle BaseCRL cannot be nil") |
| 134 |
} |
| 135 |
|
| 136 |
// actual content to be saved in the cache |
| 137 |
content := fileCacheContent{ |
| 138 |
BaseCRL: bundle.BaseCRL.Raw, |
| 139 |
} |
| 140 |
if bundle.DeltaCRL != nil { |
| 141 |
content.DeltaCRL = bundle.DeltaCRL.Raw |
| 142 |
} |
| 143 |
contentBytes, err := json.Marshal(content) |
| 144 |
if err != nil { |
| 145 |
return fmt.Errorf("failed to store crl bundle in file cache: %w", err) |
| 146 |
} |
| 147 |
if err := file.WriteFile(c.root, filepath.Join(c.root, c.fileName(url)), contentBytes); err != nil { |
| 148 |
return fmt.Errorf("failed to store crl bundle in file cache: %w", err) |
| 149 |
} |
| 150 |
return nil |
| 151 |
} |
| 152 |
|
| 153 |
// fileName returns the filename of the content stored in c |
| 154 |
func (c *FileCache) fileName(url string) string { |
| 155 |
hash := sha256.Sum256([]byte(url)) |
| 156 |
return hex.EncodeToString(hash[:]) |
| 157 |
} |
| 158 |
|
| 159 |
// checkExpiry returns nil when nextUpdate is bounded before current time |
| 160 |
func checkExpiry(ctx context.Context, nextUpdate time.Time) error { |
| 161 |
logger := log.GetLogger(ctx) |
| 162 |
|
| 163 |
if nextUpdate.IsZero() { |
| 164 |
return errors.New("crl bundle retrieved from file cache does not contain valid NextUpdate") |
| 165 |
} |
| 166 |
if time.Now().After(nextUpdate) { |
| 167 |
logger.Debugf("CRL bundle retrieved from file cache has expired at %s", nextUpdate) |
| 168 |
return corecrl.ErrCacheMiss |
| 169 |
} |
| 170 |
return nil |
| 171 |
} |