@@ -17,6 +17,7 @@ limitations under the License.
1717package cacert
1818
1919import (
20+ "bytes"
2021 "context"
2122 "crypto/ecdsa"
2223 "crypto/elliptic"
@@ -87,7 +88,7 @@ const (
8788//
8889// They used to be certificate armour wrapped around a truncated DER prefix,
8990// which the write-path guard accepted because it only matched the BEGIN line.
90- // Now that the guard parses what it publishes (see assertCertificateChain ), a
91+ // Now that the guard parses what it publishes (see certificateChainPEM ), a
9192// fixture has to be the thing it claims to be — and the fact that these two
9293// constants had to change at all is the clearest statement of what the old
9394// guard was worth.
@@ -133,6 +134,23 @@ func mustCertPEM(commonName string) string {
133134 return string (pem .EncodeToMemory (& pem.Block {Type : "CERTIFICATE" , Bytes : der }))
134135}
135136
137+ // mustPrivateKeyDER returns a RAW DER-encoded PKCS#8 private key, with no PEM
138+ // armour at all. It is the fixture for the smuggling cases: key material that
139+ // wears no "-----BEGIN ... PRIVATE KEY-----" header slips past the header guard,
140+ // and pem.Decode skips it as inter-block noise, so only a projection rebuilt
141+ // from the parsed certificates can keep it away from the tenant.
142+ func mustPrivateKeyDER () []byte {
143+ key , err := ecdsa .GenerateKey (elliptic .P256 (), rand .Reader )
144+ if err != nil {
145+ panic ("generate test private key: " + err .Error ())
146+ }
147+ der , err := x509 .MarshalPKCS8PrivateKey (key )
148+ if err != nil {
149+ panic ("marshal test private key: " + err .Error ())
150+ }
151+ return der
152+ }
153+
136154// mustPublicKeyDER returns a DER-encoded public key, for the fixture that
137155// checks a well-formed PEM block of the WRONG type is still refused.
138156func mustPublicKeyDER () []byte {
@@ -2046,22 +2064,30 @@ func TestProjectionData_RefusesCertificateArmouredNonCertificate(t *testing.T) {
20462064// TestProjectionData_AcceptsRealCertificates keeps the guard from failing closed
20472065// on the values it exists to publish: a single CA, and the multi-certificate
20482066// bundle an intermediate chain arrives as.
2067+ //
2068+ // The projection is the certificate blocks re-encoded, not the input echoed
2069+ // back. For a value that is already canonical PEM the two are byte-identical
2070+ // (want == in); a value with surrounding whitespace comes back as the bare
2071+ // blocks, because everything the guard did not parse as a certificate is
2072+ // dropped.
20492073func TestProjectionData_AcceptsRealCertificates (t * testing.T ) {
20502074 bundle := testCA + testCARot
2051- for name , value := range map [string ]string {
2052- "one certificate" : testCA ,
2053- "a chain of two" : bundle ,
2054- "trailing whitespace" : testCA + "\n \n \n " ,
2075+ for _ , tc := range []struct {
2076+ name string
2077+ in string
2078+ want string
2079+ }{
2080+ {"one certificate" , testCA , testCA },
2081+ {"a chain of two" , bundle , bundle },
2082+ {"trailing whitespace" , testCA + "\n \n \n " , testCA },
20552083 } {
2056- t .Run (name , func (t * testing.T ) {
2057- got , err := projectionData ([]byte (value ))
2084+ t .Run (tc . name , func (t * testing.T ) {
2085+ got , err := projectionData ([]byte (tc . in ))
20582086 if err != nil {
2059- t .Fatalf ("projectionData(%s) = %v, want accepted" , name , err )
2087+ t .Fatalf ("projectionData(%s) = %v, want accepted" , tc . name , err )
20602088 }
2061- // The value is published verbatim: the guard validates, it never
2062- // rewrites what the engine minted.
2063- if string (got [caCertKey ]) != value {
2064- t .Errorf ("projectionData must publish the value verbatim, got %q want %q" , got [caCertKey ], value )
2089+ if string (got [caCertKey ]) != tc .want {
2090+ t .Errorf ("projectionData(%s) published %q, want the validated blocks %q" , tc .name , got [caCertKey ], tc .want )
20652091 }
20662092 })
20672093 }
@@ -2089,6 +2115,82 @@ func TestProjectionData_KeyGuardWinsOverTheParse(t *testing.T) {
20892115 }
20902116}
20912117
2118+ // TestProjectionData_StripsKeyMaterialSmuggledAroundBlocks is the write path's
2119+ // deepest guarantee: the projection is REBUILT from the certificate blocks it
2120+ // validated, never copied from the input.
2121+ //
2122+ // The header guard (containsPrivateKey) only sees "-----BEGIN ... PRIVATE
2123+ // KEY-----". A raw PKCS#8 DER key, or a JSON Web Key, carries no such header, so
2124+ // it walks past that guard. pem.Decode then skips it — text before the first
2125+ // block and between blocks is silently dropped by the decoder — so the
2126+ // certificate loop never inspects it either. A projection that cloned the input
2127+ // once it validated would therefore hand the tenant a trust anchor with a
2128+ // private key tucked in front of, or between, the certificates. Rebuilding from
2129+ // the parsed DER makes that byte structurally unreachable.
2130+ func TestProjectionData_StripsKeyMaterialSmuggledAroundBlocks (t * testing.T ) {
2131+ derKey := mustPrivateKeyDER ()
2132+ // A private JWK: text, no PEM header, sits before the certificate.
2133+ jwkKey := `{"kty":"EC","crv":"P-256","d":"c2VjcmV0LXByaXZhdGUta2V5LW1hdGVyaWFs"}`
2134+
2135+ concat := func (parts ... []byte ) []byte { return bytes .Join (parts , nil ) }
2136+ for _ , tc := range []struct {
2137+ name string
2138+ in []byte
2139+ want string
2140+ }{
2141+ {
2142+ // Raw DER key, newline, then a real certificate.
2143+ name : "leading DER key" ,
2144+ in : concat (derKey , []byte ("\n " ), []byte (testCA )),
2145+ want : testCA ,
2146+ },
2147+ {
2148+ // A private JWK ahead of the certificate.
2149+ name : "leading JWK key" ,
2150+ in : []byte (jwkKey + "\n " + testCA ),
2151+ want : testCA ,
2152+ },
2153+ {
2154+ // Key bytes wedged BETWEEN two certificate blocks.
2155+ name : "interstitial DER key" ,
2156+ in : concat ([]byte (testCA ), derKey , []byte ("\n " ), []byte (testCARot )),
2157+ want : testCA + testCARot ,
2158+ },
2159+ } {
2160+ t .Run (tc .name , func (t * testing.T ) {
2161+ got , err := projectionData (tc .in )
2162+ if err != nil {
2163+ t .Fatalf ("projectionData(%s) = %v, want the value accepted and sanitized" , tc .name , err )
2164+ }
2165+ if string (got [caCertKey ]) != tc .want {
2166+ t .Errorf ("projectionData(%s) published %q, want only the validated blocks %q" , tc .name , got [caCertKey ], tc .want )
2167+ }
2168+ if bytes .Contains (got [caCertKey ], derKey ) {
2169+ t .Errorf ("projectionData(%s) leaked raw DER private-key material into the tenant projection" , tc .name )
2170+ }
2171+ })
2172+ }
2173+ }
2174+
2175+ // TestProjectionData_DropsHumanReadablePreamble pins that the text dump
2176+ // `openssl x509 -text` prints before each certificate — which pem.Decode
2177+ // tolerates — is not republished to the tenant. The projection carries the
2178+ // certificate blocks and nothing else.
2179+ func TestProjectionData_DropsHumanReadablePreamble (t * testing.T ) {
2180+ preamble := "Certificate:\n Data:\n Version: 3 (0x2)\n "
2181+ in := preamble + testCA + preamble + testCARot
2182+ got , err := projectionData ([]byte (in ))
2183+ if err != nil {
2184+ t .Fatalf ("projectionData(preamble+chain) = %v, want accepted" , err )
2185+ }
2186+ if string (got [caCertKey ]) != testCA + testCARot {
2187+ t .Errorf ("projectionData must publish only the certificate blocks, got %q" , got [caCertKey ])
2188+ }
2189+ if bytes .Contains (got [caCertKey ], []byte ("Certificate:" )) {
2190+ t .Errorf ("projectionData leaked human-readable preamble into the tenant projection" )
2191+ }
2192+ }
2193+
20922194func TestIsSource (t * testing.T ) {
20932195 for _ , tc := range []struct {
20942196 name string
0 commit comments