Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions .github/workflows/selftest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,33 @@ jobs:
exit 1
fi

# The release signer callers run from their harness checkout
# (tools/sign-openpgp): its tests sign with a development key and check the
# signatures with gpgv, as an appliance or device does.
sign-openpgp:
runs-on: ubuntu-latest
timeout-minutes: 10
defaults:
run:
working-directory: tools/sign-openpgp
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
- uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0
with:
go-version-file: tools/sign-openpgp/go.mod
cache: false
- name: gofmt, go mod tidy, go vet
run: |
test -z "$(gofmt -l .)"
go mod tidy -diff
go vet ./...
- name: unit tests
run: |
command -v gpgv
go test -race -count=1 ./...

gate-pass:
uses: ./.github/workflows/go-gate.yml
with:
Expand Down
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,28 @@ them from [templates/workflows](templates/workflows). A change to the logic is
made once here and reaches every repository when `v1` moves after a green
self-test; a change to triggers or settings needs a pull request per repository.

## Release signatures

[tools/sign-openpgp](tools/sign-openpgp) makes the detached OpenPGP signatures
(`<file>.asc`) that an install or update checks before it runs or uses what it
downloaded — with `gpgv` or `sqv`, which every apt-based host has. The private
key stays in Cloud KMS (`-kms-key`, Application Default Credentials: the
release job's workload identity) or OpenBao transit (`-signer`); the tool
hashes locally and sends only digests. A release job runs it from its harness
checkout (add `tools/sign-openpgp` to the sparse checkout, set up Go from its
`go.mod`):

```sh
cd "$HARNESS/tools/sign-openpgp"
go run . sign -cert "$CERT" -kms-key "$KMS_KEY_VERSION" FILE... # writes FILE.asc
go run . verify -cert "$CERT" FILE...
```

`cert` makes the public certificate once (`-uid` names it), `export` its base64
form for a script to embed, `fingerprint` the fingerprint the operator docs
publish, `keygen` a development key for tests. The commands are documented at
the top of [main.go](tools/sign-openpgp/main.go).

## Protected main branch

Each repository protects `main` with a ruleset: changes only through pull
Expand Down
15 changes: 15 additions & 0 deletions tools/sign-openpgp/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module github.com/ironflock/workflows/tools/sign-openpgp

go 1.27.3

require (
github.com/ProtonMail/go-crypto v1.5.2
golang.org/x/oauth2 v0.37.0
)

require (
cloud.google.com/go/compute/metadata v0.3.0 // indirect
github.com/cloudflare/circl v1.6.3 // indirect
golang.org/x/crypto v0.41.0 // indirect
golang.org/x/sys v0.35.0 // indirect
)
12 changes: 12 additions & 0 deletions tools/sign-openpgp/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
cloud.google.com/go/compute/metadata v0.3.0 h1:Tz+eQXMEqDIKRsmY3cHTL6FVaynIjX2QxYC4trgAKZc=
cloud.google.com/go/compute/metadata v0.3.0/go.mod h1:zFmK7XCadkQkj6TtorcaGlCW1hT1fIilQDwofLpJ20k=
github.com/ProtonMail/go-crypto v1.5.2 h1:cucYnvqcY7UOXVD//mSyjeaPY0SSN3v5cDkYPxumINk=
github.com/ProtonMail/go-crypto v1.5.2/go.mod h1:/RaSu30DaKO4RY+XdV/ACcCcZkGr7AhUIduq5sjzzCo=
github.com/cloudflare/circl v1.6.3 h1:9GPOhQGF9MCYUeXyMYlqTR6a5gTrgR/fBLXvUgtVcg8=
github.com/cloudflare/circl v1.6.3/go.mod h1:2eXP6Qfat4O/Yhh8BznvKnJ+uzEoTQ6jVKJRn81BiS4=
golang.org/x/crypto v0.41.0 h1:WKYxWedPGCTVVl5+WHSSrOBT0O8lx32+zxmHxijgXp4=
golang.org/x/crypto v0.41.0/go.mod h1:pO5AFd7FA68rFak7rOAGVuygIISepHftHnr8dr6+sUc=
golang.org/x/oauth2 v0.37.0 h1:JUlcxA8oAtauLfiH8FX2/FkAWHAdi0QtGCGc+hofE98=
golang.org/x/oauth2 v0.37.0/go.mod h1:IxwZNxUULJmpBFf9K/9NTMSIfZZuvuTy1gGxhigP/58=
golang.org/x/sys v0.35.0 h1:vz1N37gP5bs89s7He8XuIYXpyY0+QlsKmzipCbUtyxI=
golang.org/x/sys v0.35.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
185 changes: 185 additions & 0 deletions tools/sign-openpgp/kms.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
package main

// The Cloud KMS backend: -kms-key <key version resource> signs every digest
// with an asymmetric KMS key (RSA_SIGN_PKCS1_4096_SHA256, not exportable)
// through the KMS REST API, as the Application Default Credentials (in CI the
// workload identity credentials google-github-actions/auth writes). Only the
// SHA-256 digests leave this process; CRC32C checks guard both directions, as
// KMS recommends.

import (
"bytes"
"context"
"crypto/rsa"
"crypto/x509"
"encoding/base64"
"encoding/json"
"encoding/pem"
"errors"
"fmt"
"hash/crc32"
"io"
"net/http"
"strconv"
"time"

"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
)

// kmsAPI is what the signer needs from Cloud KMS; the tests put a fake behind it.
type kmsAPI interface {
asymmetricSign(ctx context.Context, name string, digest []byte) ([]byte, error)
publicKey(ctx context.Context, name string) (*rsa.PublicKey, error)
}

// newKMS returns the real client; a variable so the tests can replace it.
var newKMS = func(ctx context.Context) (kmsAPI, error) {
creds, err := google.FindDefaultCredentials(ctx, "https://www.googleapis.com/auth/cloudkms")
if err != nil {
return nil, fmt.Errorf("kms: no Google credentials: %w", err)
}
return &kmsREST{client: oauth2.NewClient(ctx, creds.TokenSource), endpoint: "https://cloudkms.googleapis.com/v1/"}, nil
}

type kmsREST struct {
client *http.Client
endpoint string
}

var crc32c = crc32.MakeTable(crc32.Castagnoli)

func (k *kmsREST) call(ctx context.Context, method, path string, body, into any) error {
var in io.Reader
if body != nil {
data, err := json.Marshal(body)
if err != nil {
return err
}
in = bytes.NewReader(data)
}
ctx, cancel := context.WithTimeout(ctx, time.Minute)
defer cancel()
req, err := http.NewRequestWithContext(ctx, method, k.endpoint+path, in)
if err != nil {
return err
}
if body != nil {
req.Header.Set("Content-Type", "application/json")
}
resp, err := k.client.Do(req)
if err != nil {
return fmt.Errorf("kms: %w", err)
}
defer func() { _ = resp.Body.Close() }()
data, err := io.ReadAll(io.LimitReader(resp.Body, 1<<20))
if err != nil {
return fmt.Errorf("kms: %w", err)
}
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("kms: %s %s: %s: %s", method, path, resp.Status, bytes.TrimSpace(data))
}
return json.Unmarshal(data, into)
}

func (k *kmsREST) asymmetricSign(ctx context.Context, name string, digest []byte) ([]byte, error) {
var answer struct {
Signature string `json:"signature"`
SignatureCrc32c string `json:"signatureCrc32c"`
VerifiedDigestCrc32c bool `json:"verifiedDigestCrc32c"`
Name string `json:"name"`
}
request := map[string]any{
"digest": map[string]string{"sha256": base64.StdEncoding.EncodeToString(digest)},
"digestCrc32c": strconv.FormatUint(uint64(crc32.Checksum(digest, crc32c)), 10),
}
if err := k.call(ctx, http.MethodPost, name+":asymmetricSign", request, &answer); err != nil {
return nil, err
}
if !answer.VerifiedDigestCrc32c {
return nil, errors.New("kms: the digest was damaged on the way to KMS")
}
if answer.Name != name {
return nil, fmt.Errorf("kms: signed with %q, not %q", answer.Name, name)
}
sig, err := base64.StdEncoding.DecodeString(answer.Signature)
if err != nil {
return nil, fmt.Errorf("kms: the signature is not base64: %w", err)
}
if strconv.FormatUint(uint64(crc32.Checksum(sig, crc32c)), 10) != answer.SignatureCrc32c {
return nil, errors.New("kms: the signature was damaged on the way back")
}
return sig, nil
}

func (k *kmsREST) publicKey(ctx context.Context, name string) (*rsa.PublicKey, error) {
var answer struct {
Pem string `json:"pem"`
Algorithm string `json:"algorithm"`
PemCrc32c string `json:"pemCrc32c"`
}
if err := k.call(ctx, http.MethodGet, name+"/publicKey", nil, &answer); err != nil {
return nil, err
}
if answer.Algorithm != "RSA_SIGN_PKCS1_4096_SHA256" {
return nil, fmt.Errorf("kms: %s is %s, want RSA_SIGN_PKCS1_4096_SHA256", name, answer.Algorithm)
}
if answer.PemCrc32c != "" && strconv.FormatUint(uint64(crc32.Checksum([]byte(answer.Pem), crc32c)), 10) != answer.PemCrc32c {
return nil, errors.New("kms: the public key was damaged on the way")
}
return parsePublicKeyPEM([]byte(answer.Pem), name)
}

// kmsBackend signs with the KMS key, after checking that it is the key the
// certificate names (a rotated key version would sign for another cert).
func kmsBackend(name string, pub *rsa.PublicKey) (func([][]byte) ([][]byte, error), error) {
ctx := context.Background()
api, err := newKMS(ctx)
if err != nil {
return nil, err
}
kmsPub, err := api.publicKey(ctx, name)
if err != nil {
return nil, err
}
if !kmsPub.Equal(pub) {
return nil, fmt.Errorf("kms: %s is not the certificate's key", name)
}
return func(digests [][]byte) ([][]byte, error) {
out := make([][]byte, len(digests))
for i, d := range digests {
sig, err := api.asymmetricSign(ctx, name, d)
if err != nil {
return nil, err
}
out[i] = sig
}
return out, nil
}, nil
}

// kmsPublicKey fetches the key version's public key (for `cert`).
func kmsPublicKey(name string) (*rsa.PublicKey, error) {
ctx := context.Background()
api, err := newKMS(ctx)
if err != nil {
return nil, err
}
return api.publicKey(ctx, name)
}

func parsePublicKeyPEM(data []byte, source string) (*rsa.PublicKey, error) {
block, _ := pem.Decode(data)
if block == nil {
return nil, fmt.Errorf("%s: no PEM key", source)
}
key, err := x509.ParsePKIXPublicKey(block.Bytes)
if err != nil {
return nil, fmt.Errorf("%s: %w", source, err)
}
rsaKey, ok := key.(*rsa.PublicKey)
if !ok || rsaKey.N.BitLen() < 3072 {
return nil, fmt.Errorf("%s: not an RSA key of 3072 bits or more", source)
}
return rsaKey, nil
}
Loading
Loading