Browse Source

Pin Google Cloud Attestation's root for Confidential Space tokens

PKI tokens are verified against "Confidential Space Root CA" (RSA 4096,
2024-01-19 to 2034-01-16). It was fetched from
https://confidentialcomputing.googleapis.com/.well-known/confidential_space_root.crt,
which .well-known/attestation-pki-root names. Its SHA-1 fingerprint,
B9:51:20:74:2C:24:E3:AA:34:04:2E:1C:3B:A3:AA:D2:8B:21:23:21, matches
Google's documentation. Google asks relying parties to store the root
and rotates it every ten years. confidentialspace.Roots returns it, and
a test asserts both fingerprints, so a changed file fails.

Also: an eat_nonce claim of null now decodes as no nonces, not as one
empty nonce.

Claude-Session: https://claude.ai/code/session_01CjfEMLGpnHQaWsmxe1zmLF
Josh Drake 1 tháng trước cách đây
mục cha
commit
e6c71f6b2e

+ 35 - 0
attestation/confidentialspace/confidential_space_root.crt

@@ -0,0 +1,35 @@
+-----BEGIN CERTIFICATE-----
+MIIGCDCCA/CgAwIBAgITYBvRy5g9aYYMh7tJS7pFwafL6jANBgkqhkiG9w0BAQsF
+ADCBizELMAkGA1UEBhMCVVMxEzARBgNVBAgTCkNhbGlmb3JuaWExFjAUBgNVBAcT
+DU1vdW50YWluIFZpZXcxEzARBgNVBAoTCkdvb2dsZSBMTEMxFTATBgNVBAsTDEdv
+b2dsZSBDbG91ZDEjMCEGA1UEAxMaQ29uZmlkZW50aWFsIFNwYWNlIFJvb3QgQ0Ew
+HhcNMjQwMTE5MjIxMDUwWhcNMzQwMTE2MjIxMDQ5WjCBizELMAkGA1UEBhMCVVMx
+EzARBgNVBAgTCkNhbGlmb3JuaWExFjAUBgNVBAcTDU1vdW50YWluIFZpZXcxEzAR
+BgNVBAoTCkdvb2dsZSBMTEMxFTATBgNVBAsTDEdvb2dsZSBDbG91ZDEjMCEGA1UE
+AxMaQ29uZmlkZW50aWFsIFNwYWNlIFJvb3QgQ0EwggIiMA0GCSqGSIb3DQEBAQUA
+A4ICDwAwggIKAoICAQCvRuZasczAqhMZe1ODHJ6MFLX8EYVV+RN7xiO9GpuA53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bHBKWK3UykaSCxnEg8uaNx/rhNaOWuWtos4qL00dYyGV7ZXg4fpAq7244QUgkWV
+AtVcU2SPBjDd30OFHASnenDHRzQdOtHaxLp4a4WaY3jb2V6Sn3LfE8zSy6GevxmN
+COIWW3xnPF8rwKz4ABEPqECe37zzu3W1nzZAFtdkhPBNnlWYkIusTMtU+8v6EPKp
+GIIRphpaDhtGPJQukpENOfk2728lenPycRfjxwA96UKWq0dKZC45MwBEK9Jngn8Q
+cPmpPmx7pSMkSxEX2Vos2JNaNmCKJd2VaXz8M6F2cxscRdh9TbAYAjGEEjE1nLUH
+2YHDS8Y7xYNFIDSFaJAlqGcCUbzjGhrwHGj4voTe9ZvlmngrcA/ptSuBidvsnRDw
+kNPLowCd0NqxYYSLNL7GroYCFPxoBpr+++4vsCaXalbs8iJxdU2EPqG4MB4xWKYg
+uyT5CnJulxSC5CT1
+-----END CERTIFICATE-----

+ 4 - 0
attestation/confidentialspace/confidentialspace.go

@@ -41,6 +41,10 @@ type Nonces []string
 
 // UnmarshalJSON implements [json.Unmarshaler].
 func (n *Nonces) UnmarshalJSON(data []byte) error {
+	if string(data) == "null" {
+		*n = nil
+		return nil
+	}
 	var s string
 	if err := json.Unmarshal(data, &s); err == nil {
 		*n = Nonces{s}

+ 1 - 0
attestation/confidentialspace/confidentialspace_test.go

@@ -32,6 +32,7 @@ func TestNonces_UnmarshalJSON(t *testing.T) {
 	}{
 		{"ok string", `"nonce-0123"`, Nonces{"nonce-0123"}, false},
 		{"ok array", `["nonce-0123","nonce-4567"]`, Nonces{"nonce-0123", "nonce-4567"}, false},
+		{"ok null", `null`, nil, false},
 		{"fail number", `123`, nil, true},
 		{"fail object", `{"a":"b"}`, nil, true},
 	}

+ 35 - 0
attestation/confidentialspace/roots.go

@@ -0,0 +1,35 @@
+package confidentialspace
+
+import (
+	"crypto/x509"
+	_ "embed"
+	"sync"
+)
+
+// rootPEM is the root of Google Cloud Attestation's PKI tokens, "Confidential
+// Space Root CA", valid from 2024-01-19 to 2034-01-16. Google publishes its
+// location at
+// https://confidentialcomputing.googleapis.com/.well-known/attestation-pki-root,
+// which names
+// https://confidentialcomputing.googleapis.com/.well-known/confidential_space_root.crt,
+// documents its SHA-1 fingerprint as
+// B9:51:20:74:2C:24:E3:AA:34:04:2E:1C:3B:A3:AA:D2:8B:21:23:21, asks relying
+// parties to store it, and rotates it every ten years. Its SHA-256 fingerprint
+// is 14:8B:29:38:21:BB:0C:6A:31:7F:41:3C:8B:A4:75:81:40:91:CB:22:D4:9B:9E:3C:94:19:8D:B8:E8:F8:6C:39.
+//
+//go:embed confidential_space_root.crt
+var rootPEM []byte
+
+var roots = sync.OnceValue(func() *x509.CertPool {
+	pool := x509.NewCertPool()
+	if !pool.AppendCertsFromPEM(rootPEM) {
+		panic("confidentialspace: the embedded root is not valid PEM")
+	}
+	return pool
+})
+
+// Roots returns a pool with Google Cloud Attestation's root, the default for
+// verifying PKI tokens.
+func Roots() *x509.CertPool {
+	return roots()
+}

+ 37 - 0
attestation/confidentialspace/roots_test.go

@@ -0,0 +1,37 @@
+package confidentialspace
+
+import (
+	"crypto/sha1" //nolint:gosec // Google documents the root's SHA-1 fingerprint
+	"crypto/sha256"
+	"crypto/x509"
+	"encoding/hex"
+	"encoding/pem"
+	"testing"
+	"time"
+
+	"github.com/stretchr/testify/assert"
+	"github.com/stretchr/testify/require"
+)
+
+// TestRoots checks the embedded root against the SHA-1 fingerprint Google
+// documents and the SHA-256 fingerprint recorded with it, so that a changed
+// file fails.
+func TestRoots(t *testing.T) {
+	block, rest := pem.Decode(rootPEM)
+	require.NotNil(t, block)
+	assert.Empty(t, rest)
+	cert, err := x509.ParseCertificate(block.Bytes)
+	require.NoError(t, err)
+
+	sha1Sum := sha1.Sum(cert.Raw) //nolint:gosec // see the import
+	sha256Sum := sha256.Sum256(cert.Raw)
+	assert.Equal(t, "b95120742c24e3aa34042e1c3ba3aad28b212321", hex.EncodeToString(sha1Sum[:]))
+	assert.Equal(t, "148b293821bb0c6a317f413c8ba475814091cb22d49b9e3c94198db8e8f86c39", hex.EncodeToString(sha256Sum[:]))
+	assert.Equal(t, "Confidential Space Root CA", cert.Subject.CommonName)
+	assert.True(t, cert.IsCA)
+	assert.True(t, cert.NotAfter.After(time.Date(2034, time.January, 1, 0, 0, 0, 0, time.UTC)))
+
+	// The pool verifies the root itself.
+	_, err = cert.Verify(x509.VerifyOptions{Roots: Roots(), CurrentTime: time.Date(2027, time.January, 1, 0, 0, 0, 0, time.UTC)})
+	assert.NoError(t, err)
+}