|
| 1 | +package ceremony |
| 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 | + "bytes" |
| 23 | + "strings" |
| 24 | + "testing" |
| 25 | + |
| 26 | + "google.golang.org/grpc/codes" |
| 27 | + "google.golang.org/grpc/status" |
| 28 | + |
| 29 | + cryptosv1 "github.com/CryptOS-PKI/api/go/cryptos/v1" |
| 30 | +) |
| 31 | + |
| 32 | +// TestStart_RSAOnTPMWithoutSize_FailsPrecondition runs the root ceremony with |
| 33 | +// root_key_alg RSA-3072 against the in-process simulator, which, like many |
| 34 | +// real TPM parts, implements RSA-2048 only. The ceremony must stop with |
| 35 | +// FailedPrecondition naming the algorithm, emit nothing, commit no identity, |
| 36 | +// and leave the node able to run the ceremony again with an algorithm the TPM |
| 37 | +// has. It must never fall back to a software key or a smaller size. |
| 38 | +func TestStart_RSAOnTPMWithoutSize_FailsPrecondition(t *testing.T) { |
| 39 | + h, ctx := newHarness(t) |
| 40 | + c := &collector{} |
| 41 | + rsaYAML := bytes.Replace(machineYAML(h.adminFP), []byte("root_key_alg: ECDSA-P384"), []byte("root_key_alg: RSA-3072"), 1) |
| 42 | + |
| 43 | + err := h.engine.Start(ctx, &cryptosv1.StartCeremonyRequest{ |
| 44 | + Kind: cryptosv1.CeremonyKind_CEREMONY_KIND_FIRST_BOOT_ROOT, |
| 45 | + MachineConfigYaml: rsaYAML, |
| 46 | + }, c.send) |
| 47 | + if status.Code(err) != codes.FailedPrecondition { |
| 48 | + t.Fatalf("Start(RSA-3072 on a TPM without it): code = %v, want FailedPrecondition (err=%v)", status.Code(err), err) |
| 49 | + } |
| 50 | + if !strings.Contains(err.Error(), "RSA-3072") { |
| 51 | + t.Errorf("error %q does not name RSA-3072", err) |
| 52 | + } |
| 53 | + if len(c.kinds) != 0 { |
| 54 | + t.Errorf("events emitted on a refused ceremony: %v", c.kinds) |
| 55 | + } |
| 56 | + if ok, _ := h.store.HasIdentity(ctx); ok { |
| 57 | + t.Fatal("identity established despite the TPM lacking RSA-3072") |
| 58 | + } |
| 59 | + |
| 60 | + retry := &collector{} |
| 61 | + if err := h.engine.Start(ctx, &cryptosv1.StartCeremonyRequest{ |
| 62 | + Kind: cryptosv1.CeremonyKind_CEREMONY_KIND_FIRST_BOOT_ROOT, |
| 63 | + MachineConfigYaml: machineYAML(h.adminFP), |
| 64 | + }, retry.send); err != nil { |
| 65 | + t.Fatalf("retry with ECDSA-P384: %v", err) |
| 66 | + } |
| 67 | + assertOrder(t, retry.kinds, wantOrder()) |
| 68 | +} |
0 commit comments