Skip to content

Commit b7cddd7

Browse files
authored
feat(cryptosctl): build for Windows by cutting the TPM import chain (#392)
The CLI reached the Linux-only TPM transport only because internal/config needed the key algorithm type. That type now lives in internal/keyalg, so cryptosctl links no go-tpm code on any OS. task ci cross-builds both Windows targets so the build can't silently break again. Refs #275 Signed-off-by: Bugs5382 <12115015+Bugs5382@users.noreply.github.com> Co-authored-by: Bugs5382 <12115015+Bugs5382@users.noreply.github.com>
1 parent 4cfd78c commit b7cddd7

7 files changed

Lines changed: 106 additions & 13 deletions

File tree

‎README.md‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ Requires Go 1.26.8+ (the `go` line in `go.mod`; an older local Go downloads that
6363
task ci # fmt + proto lint + generated-code check + lint + vet + test + build
6464
task generate # regenerate gen/go from proto/ with the pinned plugins (task tools)
6565
task build # produces bin/init, bin/cryptosctl and bin/cryptos-install, stamped with the build identity
66+
task build:windows # cross-builds bin/cryptosctl-windows-amd64.exe and -arm64.exe
6667
task license # re-inject Apache 2.0 headers via golic
6768
task e2e:kind # Linux + docker: cert-manager in kind gets a certificate over ACME
6869
```
@@ -81,7 +82,7 @@ The release assets are unsigned and carry no upgrade anchor. `cryptosctl image s
8182

8283
GitHub Actions:
8384

84-
- **`ci-go`** ([`ci-go.yml`](.github/workflows/ci-go.yml)) — `task ci` (format, proto lint, generated-code check, lint, vet, test, build) on every pull request + push to `main`, on a GitHub-hosted Linux runner, with `swtpm` installed for the TPM-held RSA CA test. Draft pull requests are skipped; CI runs when the PR is marked ready.
85+
- **`ci-go`** ([`ci-go.yml`](.github/workflows/ci-go.yml)) — `task ci` (format, proto lint, generated-code check, lint, vet, test, build, Windows cross-build of `cryptosctl`) on every pull request + push to `main`, on a GitHub-hosted Linux runner, with `swtpm` installed for the TPM-held RSA CA test. Draft pull requests are skipped; CI runs when the PR is marked ready.
8586

8687
After a stacked pull request is retargeted onto `main`, CI starts on its next push, or when it is toggled to draft and back to ready.
8788

@@ -123,7 +124,7 @@ Code signatures stay verifiable after the signing certificate expires when they
123124
Most `config apply` changes report `requires_reboot=true`. Restart the node through its orderly shutdown rather than a hypervisor hard reset:
124125

125126
> [!NOTE]
126-
> `cryptosctl` runs on Linux and macOS today. A Windows build is coming.
127+
> `cryptosctl` runs on Linux, macOS and Windows (amd64 and arm64).
127128
128129
```sh
129130
cryptosctl --endpoint pki-root.example:443 reboot --confirm "Example Root CA G1"

‎Taskfile.yml‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,15 @@ tasks:
5757
- go build -ldflags "{{.BUILDINFO}}" -o bin/cryptosctl ./cmd/cryptosctl
5858
- go build -ldflags "{{.BUILDINFO}}" -o bin/cryptos-install ./cmd/cryptos-install
5959

60+
build:windows:
61+
desc: Cross-build cryptosctl for Windows (amd64 and arm64) so the build can't silently break
62+
env:
63+
CGO_ENABLED: "0"
64+
GOOS: windows
65+
cmds:
66+
- GOARCH=amd64 go build -o bin/cryptosctl-windows-amd64.exe ./cmd/cryptosctl
67+
- GOARCH=arm64 go build -o bin/cryptosctl-windows-arm64.exe ./cmd/cryptosctl
68+
6069
proto:fmt:
6170
desc: Format the node API protos
6271
cmds:
@@ -135,6 +144,7 @@ tasks:
135144
- task: vet
136145
- task: test
137146
- task: build
147+
- task: build:windows
138148

139149
e2e:kind:
140150
desc: "[Linux] cert-manager in a kind cluster enrols over ACME with an in-process Intermediate (needs docker; skips without it)"

‎cmd/cryptosctl/deps_test.go‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package main
2+
3+
/*
4+
/*
5+
Copyright The CryptOS Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
*/
19+
20+
import (
21+
"os"
22+
"os/exec"
23+
"strings"
24+
"testing"
25+
)
26+
27+
// TestCLIDoesNotLinkTheTPMPackage keeps the operator CLI buildable on every
28+
// workstation OS. internal/tpm pulls in a Linux-only transport, so one import
29+
// of it from the CLI's graph breaks the Windows build.
30+
func TestCLIDoesNotLinkTheTPMPackage(t *testing.T) {
31+
const tpmPkg = "github.com/CryptOS-PKI/cryptos-node/internal/tpm"
32+
for _, goos := range []string{"linux", "darwin", "windows"} {
33+
t.Run(goos, func(t *testing.T) {
34+
cmd := exec.Command("go", "list", "-e", "-deps", ".")
35+
cmd.Env = append(os.Environ(), "GOOS="+goos, "CGO_ENABLED=0")
36+
out, err := cmd.Output()
37+
if err != nil {
38+
t.Fatalf("go list: %v", err)
39+
}
40+
for _, pkg := range strings.Fields(string(out)) {
41+
if pkg == tpmPkg {
42+
t.Fatalf("cryptosctl on %s links %s", goos, tpmPkg)
43+
}
44+
}
45+
})
46+
}
47+
}

‎cmd/cryptosctl/escrow.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ func newExportKeyCmd(opts *globalOpts) *cobra.Command {
8282
if err := os.WriteFile(outFile, resp.GetEnvelope(), 0o600); err != nil {
8383
return fmt.Errorf("write backup: %w", err)
8484
}
85-
_, err = fmt.Fprintf(cmd.OutOrStdout(), "wrote encrypted CA key backup to %s (%d bytes, mode 0600)\n", outFile, len(resp.GetEnvelope()))
85+
_, err = fmt.Fprintf(cmd.OutOrStdout(), "wrote encrypted CA key backup to %s (%d bytes)\n", outFile, len(resp.GetEnvelope()))
8686
return err
8787
},
8888
}

‎internal/config/config.go‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import (
3535
nodev1 "github.com/CryptOS-PKI/cryptos-node/gen/go/cryptos/node/v1"
3636
"github.com/CryptOS-PKI/cryptos-node/internal/bootstrap"
3737
"github.com/CryptOS-PKI/cryptos-node/internal/ca"
38-
"github.com/CryptOS-PKI/cryptos-node/internal/tpm"
38+
"github.com/CryptOS-PKI/cryptos-node/internal/keyalg"
3939
)
4040

4141
// APIVersion is the only api/kind pair accepted in Phase 1. Validator
@@ -98,14 +98,14 @@ func (a RootKeyAlg) Valid() bool {
9898
// KeyAlgorithm maps the config vocabulary to the algorithm the key backend
9999
// generates. Without this the configured value would be validated and then
100100
// ignored, which is what happened while ECDSA P-384 was the only option.
101-
func (a RootKeyAlg) KeyAlgorithm() (tpm.KeyAlgorithm, error) {
101+
func (a RootKeyAlg) KeyAlgorithm() (keyalg.Algorithm, error) {
102102
switch a {
103103
case RootKeyECDSAP384:
104-
return tpm.AlgorithmECDSAP384, nil
104+
return keyalg.ECDSAP384, nil
105105
case RootKeyRSA3072:
106-
return tpm.AlgorithmRSA3072, nil
106+
return keyalg.RSA3072, nil
107107
case RootKeyRSA4096:
108-
return tpm.AlgorithmRSA4096, nil
108+
return keyalg.RSA4096, nil
109109
default:
110110
return 0, fmt.Errorf("config: unsupported key algorithm %q, must be one of %s", a, supportedRootKeyAlgs())
111111
}

‎internal/keyalg/keyalg.go‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Package keyalg names the CA key algorithms. It sits apart from internal/tpm
2+
// so that internal/config, and through it cryptosctl, can use the vocabulary
3+
// without linking the Linux-only TPM transport.
4+
package keyalg
5+
6+
/*
7+
Copyright The CryptOS Authors.
8+
9+
Licensed under the Apache License, Version 2.0 (the "License");
10+
you may not use this file except in compliance with the License.
11+
You may obtain a copy of the License at
12+
13+
http://www.apache.org/licenses/LICENSE-2.0
14+
15+
Unless required by applicable law or agreed to in writing, software
16+
distributed under the License is distributed on an "AS IS" BASIS,
17+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
See the License for the specific language governing permissions and
19+
limitations under the License.
20+
*/
21+
22+
// Algorithm selects the algorithm and parameters for a created signing key.
23+
type Algorithm int
24+
25+
const (
26+
// ECDSAP384 pairs ECDSA on NIST P-384 with SHA-384.
27+
ECDSAP384 Algorithm = iota + 1
28+
29+
// RSA2048, RSA3072 and RSA4096 are RSA CA keys at the named modulus size.
30+
RSA2048
31+
RSA3072
32+
RSA4096
33+
)

‎internal/tpm/key.go‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,26 @@ import (
2626

2727
"github.com/google/go-tpm/tpm2"
2828
"github.com/google/go-tpm/tpm2/transport"
29+
30+
"github.com/CryptOS-PKI/cryptos-node/internal/keyalg"
2931
)
3032

3133
// KeyAlgorithm selects the algorithm and parameters for a created signing key.
32-
type KeyAlgorithm int
34+
type KeyAlgorithm = keyalg.Algorithm
3335

3436
const (
3537
// AlgorithmECDSAP384 pairs ECDSA on NIST P-384 with SHA-384.
36-
AlgorithmECDSAP384 KeyAlgorithm = iota + 1
38+
AlgorithmECDSAP384 = keyalg.ECDSAP384
3739

3840
// AlgorithmRSA2048, AlgorithmRSA3072 and AlgorithmRSA4096 are RSA CA keys
3941
// at the named modulus size. RSA-3072 and RSA-4096 can be created inside
4042
// the TPM when the part implements that size; CreateKey checks first and
4143
// fails with ErrKeyAlgorithmUnsupported rather than creating anything
4244
// else. RSA-2048 is below MinRSAKeyBits and is refused here; it remains
4345
// only for the software key backend.
44-
AlgorithmRSA2048
45-
AlgorithmRSA3072
46-
AlgorithmRSA4096
46+
AlgorithmRSA2048 = keyalg.RSA2048
47+
AlgorithmRSA3072 = keyalg.RSA3072
48+
AlgorithmRSA4096 = keyalg.RSA4096
4749
)
4850

4951
// MinRSAKeyBits is the smallest RSA key CreateKey will make in the TPM. It

0 commit comments

Comments
 (0)