|
| 1 | +package init |
| 2 | + |
| 3 | +/* |
| 4 | +Copyright The CryptOS Authors. |
| 5 | +
|
| 6 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +you may not use this file except in compliance with the License. |
| 8 | +You may obtain a copy of the License at |
| 9 | +
|
| 10 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +
|
| 12 | +Unless required by applicable law or agreed to in writing, software |
| 13 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +See the License for the specific language governing permissions and |
| 16 | +limitations under the License. |
| 17 | +*/ |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "crypto" |
| 22 | + "crypto/ecdsa" |
| 23 | + "crypto/elliptic" |
| 24 | + "crypto/rand" |
| 25 | + "crypto/x509" |
| 26 | + "crypto/x509/pkix" |
| 27 | + "testing" |
| 28 | + "time" |
| 29 | + |
| 30 | + "golang.org/x/crypto/ocsp" |
| 31 | + |
| 32 | + "github.com/CryptOS-PKI/cryptos-node/internal/bootstrap" |
| 33 | + "github.com/CryptOS-PKI/cryptos-node/internal/config" |
| 34 | + "github.com/CryptOS-PKI/cryptos-node/internal/node" |
| 35 | + "github.com/CryptOS-PKI/cryptos-node/internal/revocation" |
| 36 | + "github.com/CryptOS-PKI/cryptos-node/internal/storage/etcd" |
| 37 | +) |
| 38 | + |
| 39 | +// rotationRig wires an established Intermediate the way run.go does, with a |
| 40 | +// real rekeyer and enroller, so a test can issue leaves, re-key and query /ocsp. |
| 41 | +type rotationRig struct { |
| 42 | + store *node.Store |
| 43 | + revStore *revocation.Store |
| 44 | + parent *rekeyParent |
| 45 | + rekeyer *nodeRekeyer |
| 46 | + signer *node.CASigner |
| 47 | + responder *ocspResponder |
| 48 | + ocspFn func(context.Context, []byte) ([]byte, error) |
| 49 | +} |
| 50 | + |
| 51 | +func newRotationRig(t *testing.T) (*rotationRig, context.Context) { |
| 52 | + t.Helper() |
| 53 | + srv, err := etcd.Open(t.TempDir()) |
| 54 | + if err != nil { |
| 55 | + t.Fatalf("etcd.Open: %v", err) |
| 56 | + } |
| 57 | + t.Cleanup(func() { _ = srv.Close() }) |
| 58 | + cli, err := srv.Client() |
| 59 | + if err != nil { |
| 60 | + t.Fatalf("etcd.Client: %v", err) |
| 61 | + } |
| 62 | + t.Cleanup(func() { _ = cli.Close() }) |
| 63 | + store, err := node.New(cli) |
| 64 | + if err != nil { |
| 65 | + t.Fatalf("node.New: %v", err) |
| 66 | + } |
| 67 | + ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) |
| 68 | + t.Cleanup(cancel) |
| 69 | + revStore := revocation.NewStore(cli) |
| 70 | + parent := newRekeyParent(t) |
| 71 | + |
| 72 | + key, err := ecdsa.GenerateKey(elliptic.P384(), rand.Reader) |
| 73 | + if err != nil { |
| 74 | + t.Fatalf("GenerateKey: %v", err) |
| 75 | + } |
| 76 | + blob, err := x509.MarshalECPrivateKey(key) |
| 77 | + if err != nil { |
| 78 | + t.Fatalf("MarshalECPrivateKey: %v", err) |
| 79 | + } |
| 80 | + pub, err := x509.MarshalPKIXPublicKey(&key.PublicKey) |
| 81 | + if err != nil { |
| 82 | + t.Fatalf("MarshalPKIXPublicKey: %v", err) |
| 83 | + } |
| 84 | + csr, err := x509.CreateCertificateRequest(rand.Reader, &x509.CertificateRequest{Subject: pkix.Name{CommonName: "Child Issuing CA"}}, key) |
| 85 | + if err != nil { |
| 86 | + t.Fatalf("CreateCertificateRequest: %v", err) |
| 87 | + } |
| 88 | + if err := store.StageSubordinate(ctx, csr, blob, pub); err != nil { |
| 89 | + t.Fatalf("StageSubordinate: %v", err) |
| 90 | + } |
| 91 | + if err := store.CommitSubordinateCert(ctx, [][]byte{parent.signIntermediate(t, &key.PublicKey, ""), parent.der}); err != nil { |
| 92 | + t.Fatalf("CommitSubordinateCert: %v", err) |
| 93 | + } |
| 94 | + |
| 95 | + backend := NewSoftRootBackend() |
| 96 | + load := func(ctx context.Context) (crypto.Signer, func(), error) { |
| 97 | + priv, pub, _, err := store.RootKeyBlobs(ctx) |
| 98 | + if err != nil { |
| 99 | + return nil, nil, err |
| 100 | + } |
| 101 | + s, err := backend.LoadKey(priv, pub) |
| 102 | + if err != nil { |
| 103 | + return nil, nil, err |
| 104 | + } |
| 105 | + return s, func() { _ = s.Close() }, nil |
| 106 | + } |
| 107 | + issuer := func(ctx context.Context) (*x509.Certificate, error) { |
| 108 | + id, err := store.Identity(ctx) |
| 109 | + if err != nil { |
| 110 | + return nil, err |
| 111 | + } |
| 112 | + return x509.ParseCertificate(id.ChainDer[0]) |
| 113 | + } |
| 114 | + |
| 115 | + trust, err := bootstrap.LoadTrust(certPEM(parent.der), "") |
| 116 | + if err != nil { |
| 117 | + t.Fatalf("LoadTrust: %v", err) |
| 118 | + } |
| 119 | + enr, err := node.NewSubordinateEnroller(store, trust) |
| 120 | + if err != nil { |
| 121 | + t.Fatalf("NewSubordinateEnroller: %v", err) |
| 122 | + } |
| 123 | + rekeyer, err := newRekeyer(store, backend, rekeyConfig(), enr) |
| 124 | + if err != nil { |
| 125 | + t.Fatalf("newRekeyer: %v", err) |
| 126 | + } |
| 127 | + |
| 128 | + cfg := &config.Config{PKI: config.PKI{Profiles: []config.CertificateProfile{ |
| 129 | + {Name: "leaf", ValidityDays: 30, KeyUsage: []string{"digital_signature"}, ExtKeyUsage: []string{"server_auth"}}, |
| 130 | + }}} |
| 131 | + signer := node.NewCASigner(load, issuer, func(context.Context) (*config.Config, error) { return cfg, nil }). |
| 132 | + WithRecorder(IssuedRecorder(revStore)) |
| 133 | + revoker := &nodeRevoker{store: revStore, crlBuilder: revocation.NewCRLBuilder(revStore, time.Hour), load: load, issuer: issuer} |
| 134 | + responder := newOCSPResponder(store, load, issuer, 0) |
| 135 | + return &rotationRig{ |
| 136 | + store: store, |
| 137 | + revStore: revStore, |
| 138 | + parent: parent, |
| 139 | + rekeyer: rekeyer, |
| 140 | + signer: signer, |
| 141 | + responder: responder, |
| 142 | + ocspFn: revoker.ocspFn(revocation.NewOCSPResponder(revStore), responder), |
| 143 | + }, ctx |
| 144 | +} |
| 145 | + |
| 146 | +// issueLeaf returns a new leaf and the CA certificate that issued it. |
| 147 | +func (r *rotationRig) issueLeaf(t *testing.T, ctx context.Context) (leaf, ca *x509.Certificate) { |
| 148 | + t.Helper() |
| 149 | + key, err := ecdsa.GenerateKey(elliptic.P384(), rand.Reader) |
| 150 | + if err != nil { |
| 151 | + t.Fatalf("GenerateKey: %v", err) |
| 152 | + } |
| 153 | + csr, err := x509.CreateCertificateRequest(rand.Reader, &x509.CertificateRequest{Subject: pkix.Name{CommonName: "svc.example"}}, key) |
| 154 | + if err != nil { |
| 155 | + t.Fatalf("CreateCertificateRequest: %v", err) |
| 156 | + } |
| 157 | + chain, _, err := r.signer.IssueLeafForNames(ctx, csr, "leaf", nil) |
| 158 | + if err != nil { |
| 159 | + t.Fatalf("IssueLeafForNames: %v", err) |
| 160 | + } |
| 161 | + if leaf, err = x509.ParseCertificate(chain[0]); err != nil { |
| 162 | + t.Fatalf("ParseCertificate leaf: %v", err) |
| 163 | + } |
| 164 | + if ca, err = x509.ParseCertificate(chain[1]); err != nil { |
| 165 | + t.Fatalf("ParseCertificate CA: %v", err) |
| 166 | + } |
| 167 | + return leaf, ca |
| 168 | +} |
| 169 | + |
| 170 | +func (r *rotationRig) ensureResponder(t *testing.T, ctx context.Context) *x509.Certificate { |
| 171 | + t.Helper() |
| 172 | + cert, _, err := r.responder.ensure(ctx) |
| 173 | + if err != nil { |
| 174 | + t.Fatalf("ensure: %v", err) |
| 175 | + } |
| 176 | + return cert |
| 177 | +} |
| 178 | + |
| 179 | +// reKey runs a full rotation: begin, parent signature, complete. |
| 180 | +func (r *rotationRig) reKey(t *testing.T, ctx context.Context) { |
| 181 | + t.Helper() |
| 182 | + csr, err := r.rekeyer.BeginRotation(ctx) |
| 183 | + if err != nil { |
| 184 | + t.Fatalf("BeginRotation: %v", err) |
| 185 | + } |
| 186 | + der := r.parent.signCSR(t, csr, "Child Issuing CA") |
| 187 | + if _, err := r.rekeyer.CompleteRotation(ctx, [][]byte{der, r.parent.der}); err != nil { |
| 188 | + t.Fatalf("CompleteRotation: %v", err) |
| 189 | + } |
| 190 | +} |
| 191 | + |
| 192 | +// query asks /ocsp about leaf, naming issuer in the CertID, and validates the |
| 193 | +// response against that same issuer, as a relying party does. |
| 194 | +func (r *rotationRig) query(t *testing.T, ctx context.Context, leaf, issuer *x509.Certificate, opts *ocsp.RequestOptions) (*ocsp.Response, error) { |
| 195 | + t.Helper() |
| 196 | + reqDER, err := ocsp.CreateRequest(leaf, issuer, opts) |
| 197 | + if err != nil { |
| 198 | + t.Fatalf("ocsp.CreateRequest: %v", err) |
| 199 | + } |
| 200 | + body, err := r.ocspFn(ctx, reqDER) |
| 201 | + if err != nil { |
| 202 | + t.Fatalf("ocspFn: %v", err) |
| 203 | + } |
| 204 | + return ocsp.ParseResponseForCert(body, leaf, issuer) |
| 205 | +} |
| 206 | + |
| 207 | +func TestOCSPAnswersAnOldKeyLeafWithThePreviousResponder(t *testing.T) { |
| 208 | + r, ctx := newRotationRig(t) |
| 209 | + leaf, oldCA := r.issueLeaf(t, ctx) |
| 210 | + oldResponder := r.ensureResponder(t, ctx) |
| 211 | + |
| 212 | + r.reKey(t, ctx) |
| 213 | + |
| 214 | + got, err := r.query(t, ctx, leaf, oldCA, nil) |
| 215 | + if err != nil { |
| 216 | + t.Fatalf("response does not validate against the old Intermediate: %v", err) |
| 217 | + } |
| 218 | + if got.Status != ocsp.Good { |
| 219 | + t.Errorf("status = %d, want Good", got.Status) |
| 220 | + } |
| 221 | + if got.Certificate == nil || got.Certificate.SerialNumber.Cmp(oldResponder.SerialNumber) != 0 { |
| 222 | + t.Errorf("response embeds responder %v, want the retired responder %s", got.Certificate, oldResponder.SerialNumber.Text(16)) |
| 223 | + } |
| 224 | +} |
| 225 | + |
| 226 | +func TestOCSPReportsARevokedOldKeyLeaf(t *testing.T) { |
| 227 | + r, ctx := newRotationRig(t) |
| 228 | + leaf, oldCA := r.issueLeaf(t, ctx) |
| 229 | + r.ensureResponder(t, ctx) |
| 230 | + r.reKey(t, ctx) |
| 231 | + |
| 232 | + if _, err := r.revStore.Revoke(ctx, leaf.SerialNumber.Text(16), ocsp.KeyCompromise, time.Now().UTC()); err != nil { |
| 233 | + t.Fatalf("Revoke: %v", err) |
| 234 | + } |
| 235 | + |
| 236 | + got, err := r.query(t, ctx, leaf, oldCA, nil) |
| 237 | + if err != nil { |
| 238 | + t.Fatalf("response does not validate against the old Intermediate: %v", err) |
| 239 | + } |
| 240 | + if got.Status != ocsp.Revoked || got.RevocationReason != ocsp.KeyCompromise { |
| 241 | + t.Errorf("status = %d reason = %d, want Revoked keyCompromise", got.Status, got.RevocationReason) |
| 242 | + } |
| 243 | +} |
| 244 | + |
| 245 | +func TestOCSPCurrentKeyRequestsKeepTheCurrentResponder(t *testing.T) { |
| 246 | + r, ctx := newRotationRig(t) |
| 247 | + r.issueLeaf(t, ctx) |
| 248 | + oldResponder := r.ensureResponder(t, ctx) |
| 249 | + r.reKey(t, ctx) |
| 250 | + |
| 251 | + leaf, newCA := r.issueLeaf(t, ctx) |
| 252 | + got, err := r.query(t, ctx, leaf, newCA, nil) |
| 253 | + if err != nil { |
| 254 | + t.Fatalf("response does not validate against the new Intermediate: %v", err) |
| 255 | + } |
| 256 | + if got.Certificate == nil || got.Certificate.SerialNumber.Cmp(oldResponder.SerialNumber) == 0 { |
| 257 | + t.Error("a request naming the current key was answered by the retired responder") |
| 258 | + } |
| 259 | +} |
| 260 | + |
| 261 | +// RFC 6960 section 4.2.1: the response names the same CertID as the request, |
| 262 | +// including its hash algorithm. |
| 263 | +func TestOCSPEchoesTheHashAlgorithmOfTheRequest(t *testing.T) { |
| 264 | + r, ctx := newRotationRig(t) |
| 265 | + oldLeaf, oldCA := r.issueLeaf(t, ctx) |
| 266 | + r.ensureResponder(t, ctx) |
| 267 | + r.reKey(t, ctx) |
| 268 | + newLeaf, newCA := r.issueLeaf(t, ctx) |
| 269 | + |
| 270 | + for _, tc := range []struct { |
| 271 | + name string |
| 272 | + leaf, issuer *x509.Certificate |
| 273 | + }{ |
| 274 | + {"previous key", oldLeaf, oldCA}, |
| 275 | + {"current key", newLeaf, newCA}, |
| 276 | + } { |
| 277 | + t.Run(tc.name, func(t *testing.T) { |
| 278 | + got, err := r.query(t, ctx, tc.leaf, tc.issuer, &ocsp.RequestOptions{Hash: crypto.SHA256}) |
| 279 | + if err != nil { |
| 280 | + t.Fatalf("response does not validate: %v", err) |
| 281 | + } |
| 282 | + if got.IssuerHash != crypto.SHA256 { |
| 283 | + t.Errorf("response CertID hash = %v, want SHA-256", got.IssuerHash) |
| 284 | + } |
| 285 | + }) |
| 286 | + } |
| 287 | +} |
| 288 | + |
| 289 | +func TestSecondReKeyReplacesThePreviousResponder(t *testing.T) { |
| 290 | + r, ctx := newRotationRig(t) |
| 291 | + r.ensureResponder(t, ctx) |
| 292 | + r.reKey(t, ctx) |
| 293 | + |
| 294 | + secondLeaf, secondCA := r.issueLeaf(t, ctx) |
| 295 | + secondResponder := r.ensureResponder(t, ctx) |
| 296 | + r.reKey(t, ctx) |
| 297 | + |
| 298 | + got, err := r.query(t, ctx, secondLeaf, secondCA, nil) |
| 299 | + if err != nil { |
| 300 | + t.Fatalf("response does not validate against the second key: %v", err) |
| 301 | + } |
| 302 | + if got.Certificate == nil || got.Certificate.SerialNumber.Cmp(secondResponder.SerialNumber) != 0 { |
| 303 | + t.Error("the slot does not hold the responder retired by the second re-key") |
| 304 | + } |
| 305 | +} |
| 306 | + |
| 307 | +// With no OCSP traffic or ticker between two re-keys, the stored responder is |
| 308 | +// still the one under the first key. It must not be paired with the second CA |
| 309 | +// certificate; the slot keeps the first key's pair, which still matches. |
| 310 | +func TestReKeyKeepsTheSlotWhenTheStoredResponderIsStale(t *testing.T) { |
| 311 | + r, ctx := newRotationRig(t) |
| 312 | + firstLeaf, firstCA := r.issueLeaf(t, ctx) |
| 313 | + r.ensureResponder(t, ctx) |
| 314 | + r.reKey(t, ctx) |
| 315 | + r.reKey(t, ctx) |
| 316 | + |
| 317 | + got, err := r.query(t, ctx, firstLeaf, firstCA, nil) |
| 318 | + if err != nil { |
| 319 | + t.Fatalf("response does not validate against the first key: %v", err) |
| 320 | + } |
| 321 | + if got.Status != ocsp.Good { |
| 322 | + t.Errorf("status = %d, want Good", got.Status) |
| 323 | + } |
| 324 | +} |
| 325 | + |
| 326 | +func TestExpiredPreviousResponderIsDropped(t *testing.T) { |
| 327 | + r, ctx := newRotationRig(t) |
| 328 | + retired := r.ensureResponder(t, ctx) |
| 329 | + r.reKey(t, ctx) |
| 330 | + |
| 331 | + if err := r.responder.dropExpiredPrevious(ctx, retired.NotAfter.Add(-time.Minute)); err != nil { |
| 332 | + t.Fatalf("dropExpiredPrevious before expiry: %v", err) |
| 333 | + } |
| 334 | + if _, _, _, ok, err := r.store.PreviousOCSPResponder(ctx); err != nil || !ok { |
| 335 | + t.Fatalf("slot before expiry ok=%v err=%v, want it kept", ok, err) |
| 336 | + } |
| 337 | + |
| 338 | + if err := r.responder.dropExpiredPrevious(ctx, retired.NotAfter.Add(time.Minute)); err != nil { |
| 339 | + t.Fatalf("dropExpiredPrevious after expiry: %v", err) |
| 340 | + } |
| 341 | + if _, _, _, ok, err := r.store.PreviousOCSPResponder(ctx); err != nil || ok { |
| 342 | + t.Fatalf("slot after expiry ok=%v err=%v, want it empty", ok, err) |
| 343 | + } |
| 344 | +} |
| 345 | + |
| 346 | +// An expired slot must not answer even if the ticker has not dropped it yet. |
| 347 | +func TestExpiredPreviousResponderDoesNotAnswer(t *testing.T) { |
| 348 | + r, ctx := newRotationRig(t) |
| 349 | + leaf, oldCA := r.issueLeaf(t, ctx) |
| 350 | + retired := r.ensureResponder(t, ctx) |
| 351 | + r.reKey(t, ctx) |
| 352 | + |
| 353 | + reqDER, err := ocsp.CreateRequest(leaf, oldCA, nil) |
| 354 | + if err != nil { |
| 355 | + t.Fatalf("ocsp.CreateRequest: %v", err) |
| 356 | + } |
| 357 | + if _, ok := r.responder.previousFor(ctx, reqDER, retired.NotAfter.Add(time.Minute)); ok { |
| 358 | + t.Error("an expired previous responder was selected") |
| 359 | + } |
| 360 | +} |
0 commit comments