|
| 1 | +package node |
| 2 | + |
| 3 | +/* |
| 4 | +Apache License 2.0 |
| 5 | +
|
| 6 | +Copyright 2026 Shane |
| 7 | +
|
| 8 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | +you may not use this file except in compliance with the License. |
| 10 | +You may obtain a copy of the License at |
| 11 | +
|
| 12 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | +
|
| 14 | +Unless required by applicable law or agreed to in writing, software |
| 15 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | +See the License for the specific language governing permissions and |
| 18 | +limitations under the License. |
| 19 | +*/ |
| 20 | + |
| 21 | +import ( |
| 22 | + "context" |
| 23 | + "crypto" |
| 24 | + "strings" |
| 25 | + "testing" |
| 26 | + "time" |
| 27 | + |
| 28 | + "google.golang.org/grpc/codes" |
| 29 | + "google.golang.org/grpc/status" |
| 30 | + |
| 31 | + "github.com/CryptOS-PKI/cryptos/internal/config" |
| 32 | +) |
| 33 | + |
| 34 | +// A configured time source that has not synced yet closes the gate: every |
| 35 | +// signing path refuses with FailedPrecondition, before the CA key is loaded. |
| 36 | +func TestClockGateRefusesSigningWhileUnsynced(t *testing.T) { |
| 37 | + f := newSignerFixture(t) |
| 38 | + cfg := caProfileConfig(config.RoleIssuing) |
| 39 | + loaded := false |
| 40 | + load, issuer, get := f.loaders(cfg, new(bool)) |
| 41 | + wrapped := func(ctx context.Context) (crypto.Signer, func(), error) { |
| 42 | + loaded = true |
| 43 | + return load(ctx) |
| 44 | + } |
| 45 | + s := NewCASigner(wrapped, issuer, get).WithClockGate(func() bool { return false }) |
| 46 | + |
| 47 | + _, err := s.IssueLeaf(context.Background(), makeCSR(t, "node.example"), "leaf-server") |
| 48 | + wantCode(t, err, codes.FailedPrecondition) |
| 49 | + if !strings.Contains(status.Convert(err).Message(), "allow_unsynced_clock") { |
| 50 | + t.Fatalf("refusal %q does not name the override", err) |
| 51 | + } |
| 52 | + _, _, err = s.IssueLeafForNames(context.Background(), makeCSR(t, "node.example"), "leaf-server", []string{"node.example"}) |
| 53 | + wantCode(t, err, codes.FailedPrecondition) |
| 54 | + _, _, err = s.IssueLeafWithRequestSANs(context.Background(), makeCSR(t, "node.example"), "leaf-server", nil) |
| 55 | + wantCode(t, err, codes.FailedPrecondition) |
| 56 | + |
| 57 | + sub := NewCASigner(wrapped, issuer, func(context.Context) (*config.Config, error) { |
| 58 | + return caProfileConfig(config.RoleIntermediate), nil |
| 59 | + }).WithClockGate(func() bool { return false }) |
| 60 | + _, _, _, err = sub.SignSubordinate(context.Background(), makeCSR(t, "Child CA"), "sub-ca") |
| 61 | + wantCode(t, err, codes.FailedPrecondition) |
| 62 | + |
| 63 | + if loaded { |
| 64 | + t.Fatal("the CA key was loaded although the clock gate refused") |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +func TestClockGateAllowsSigningOnceSynced(t *testing.T) { |
| 69 | + f := newSignerFixture(t) |
| 70 | + load, issuer, get := f.loaders(caProfileConfig(config.RoleIssuing), new(bool)) |
| 71 | + s := NewCASigner(load, issuer, get).WithClockGate(func() bool { return true }) |
| 72 | + if _, err := s.IssueLeaf(context.Background(), makeCSR(t, "node.example"), "leaf-server"); err != nil { |
| 73 | + t.Fatalf("IssueLeaf with a synced clock: %v", err) |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +func TestClockGateOverrideAllowsSigningWhileUnsynced(t *testing.T) { |
| 78 | + f := newSignerFixture(t) |
| 79 | + cfg := caProfileConfig(config.RoleIntermediate) |
| 80 | + cfg.PKI.AllowUnsyncedClock = true |
| 81 | + load, issuer, get := f.loaders(cfg, new(bool)) |
| 82 | + s := NewCASigner(load, issuer, get).WithClockGate(func() bool { return false }) |
| 83 | + if _, _, _, err := s.SignSubordinate(context.Background(), makeCSR(t, "Child CA"), "sub-ca"); err != nil { |
| 84 | + t.Fatalf("SignSubordinate with allow_unsynced_clock: %v", err) |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +// With no time source the node wires no gate (or one that always passes), so |
| 89 | +// signing is never gated. |
| 90 | +func TestNoClockGateNeverGates(t *testing.T) { |
| 91 | + f := newSignerFixture(t) |
| 92 | + load, issuer, get := f.loaders(caProfileConfig(config.RoleIssuing), new(bool)) |
| 93 | + s := NewCASigner(load, issuer, get) |
| 94 | + if _, err := s.IssueLeaf(context.Background(), makeCSR(t, "node.example"), "leaf-server"); err != nil { |
| 95 | + t.Fatalf("IssueLeaf with no clock gate: %v", err) |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +// Every certificate the signer returns reports its notBefore, so the clock |
| 100 | +// floor can be raised past it. |
| 101 | +func TestIssuedHookSeesEveryNotBefore(t *testing.T) { |
| 102 | + f := newSignerFixture(t) |
| 103 | + load, issuer, get := f.loaders(caProfileConfig(config.RoleIssuing), new(bool)) |
| 104 | + var seen []time.Time |
| 105 | + s := NewCASigner(load, issuer, get).WithIssuedHook(func(nb time.Time) { seen = append(seen, nb) }) |
| 106 | + before := time.Now().Add(-time.Second) |
| 107 | + if _, err := s.IssueLeaf(context.Background(), makeCSR(t, "node.example"), "leaf-server"); err != nil { |
| 108 | + t.Fatal(err) |
| 109 | + } |
| 110 | + if len(seen) != 1 || seen[0].Before(before) { |
| 111 | + t.Fatalf("issued hook saw %v", seen) |
| 112 | + } |
| 113 | +} |
0 commit comments