@@ -34,6 +34,7 @@ import (
3434
3535 "github.com/smallstep/pkcs7"
3636 expirationcache "k8s.io/client-go/tools/cache"
37+ "k8s.io/klog/v2"
3738)
3839
3940const (
@@ -144,9 +145,7 @@ func verifyAttestedDocument(signature string, body []byte) (*attestedData, error
144145}
145146
146147// verifyAttestedDocumentWithRootAndFetcher verifies a PKCS7 attested document
147- // using the supplied root pool and intermediate fetcher. It prefers the
148- // embedded PKCS7 certificates and only consults the fetcher when the embedded
149- // chain does not already verify.
148+ // using the supplied root pool and intermediate fetcher.
150149func verifyAttestedDocumentWithRootAndFetcher (signature string , body []byte , rootCertPool * x509.CertPool , fetchIntermediates func (* x509.Certificate ) (* x509.CertPool , error )) (* attestedData , error ) {
151150 if rootCertPool == nil {
152151 return nil , fmt .Errorf ("root certificate pool is required" )
@@ -160,17 +159,16 @@ func verifyAttestedDocumentWithRootAndFetcher(signature string, body []byte, roo
160159 return nil , err
161160 }
162161
163- embeddedOnlyIntermediates := x509 .NewCertPool ()
164- if err := verifySignerCertChain (signer , p7 .Certificates , rootCertPool , embeddedOnlyIntermediates ); err != nil {
165- // Signer SAN was validated above; only now is it safe to hit the network.
166- intermediateCerts , fetchErr := fetchIntermediates (signer )
167- if fetchErr != nil {
168- return nil , fmt .Errorf ("verifying PKCS7 certificate chain: embedded=%w; fetch=%w" , err , fetchErr )
169- }
170- if err := verifySignerCertChain (signer , p7 .Certificates , rootCertPool , intermediateCerts ); err != nil {
171- return nil , fmt .Errorf ("verifying PKCS7 certificate chain: %w" , err )
172- }
162+ // Signer SAN was validated above; now it is safe to hit the network.
163+ klog .V (2 ).Infof ("Fetching intermediate certificates for signer issuer %q" , signer .Issuer )
164+ intermediateCerts , err := fetchIntermediates (signer )
165+ if err != nil {
166+ return nil , fmt .Errorf ("fetching intermediate certificates: %w" , err )
167+ }
168+ if err := verifySignerCertChain (signer , p7 .Certificates , rootCertPool , intermediateCerts ); err != nil {
169+ return nil , fmt .Errorf ("verifying PKCS7 certificate chain: %w" , err )
173170 }
171+ klog .V (2 ).Infof ("PKCS7 certificate chain verified for signer issuer %q" , signer .Issuer )
174172
175173 return parseAndValidateAttestedDocumentContent (p7 .Content , body )
176174}
@@ -211,24 +209,29 @@ func parseAndValidatePKCS7Signer(signature string) (*pkcs7.PKCS7, *x509.Certific
211209 if err != nil {
212210 return nil , nil , fmt .Errorf ("decoding PKCS7 signature: %w" , err )
213211 }
212+ klog .V (4 ).Infof ("Decoded PKCS7 signature (%d bytes)" , len (sigBytes ))
214213
215214 p7 , err := pkcs7 .Parse (sigBytes )
216215 if err != nil {
217216 return nil , nil , fmt .Errorf ("parsing PKCS7 signature: %w" , err )
218217 }
218+ klog .V (8 ).Infof ("Parsed PKCS7 structure with %d embedded certificate(s)" , len (p7 .Certificates ))
219219
220220 // Verify the PKCS7 signature against the embedded leaf certificate.
221221 if err := p7 .Verify (); err != nil {
222222 return nil , nil , fmt .Errorf ("verifying PKCS7 signature: %w" , err )
223223 }
224+ klog .V (4 ).Infof ("PKCS7 self-signature verified" )
224225
225226 signer := p7 .GetOnlySigner ()
226227 if signer == nil {
227228 return nil , nil , fmt .Errorf ("getting PKCS7 signer certificate" )
228229 }
230+ klog .V (8 ).Infof ("PKCS7 signer certificate: subject=%q issuer=%q SANs=%v" , signer .Subject , signer .Issuer , signer .DNSNames )
229231 if err := validateAzureMetadataSignerSAN (signer ); err != nil {
230232 return nil , nil , fmt .Errorf ("validating PKCS7 signer SAN: %w" , err )
231233 }
234+ klog .V (4 ).Infof ("PKCS7 signer SAN validated as Azure metadata endpoint" )
232235
233236 return p7 , signer , nil
234237}
@@ -248,11 +251,14 @@ func parseAndValidateAttestedDocumentContent(content []byte, body []byte) (*atte
248251 if err := json .Unmarshal (content , & data ); err != nil {
249252 return nil , fmt .Errorf ("unmarshalling attested data: %w" , err )
250253 }
254+ klog .V (4 ).Infof ("Attested document content: vmId=%q subscriptionId=%q nonce=%q createdOn=%q expiresOn=%q" , data .VMId , data .SubscriptionId , data .Nonce , data .TimeStamp .CreatedOn , data .TimeStamp .ExpiresOn )
251255
252256 // Verify the nonce matches the request body hash (replay protection).
253- if data .Nonce != nonceForBody (body ) {
254- return nil , fmt .Errorf ("attested document nonce mismatch" )
257+ expectedNonce := nonceForBody (body )
258+ if data .Nonce != expectedNonce {
259+ return nil , fmt .Errorf ("attested document nonce mismatch: got=%q expected=%q" , data .Nonce , expectedNonce )
255260 }
261+ klog .V (4 ).Infof ("Attested document nonce verified" )
256262
257263 // Verify the attested document has not expired.
258264 if data .TimeStamp .ExpiresOn != "" {
@@ -263,6 +269,7 @@ func parseAndValidateAttestedDocumentContent(content []byte, body []byte) (*atte
263269 if time .Now ().After (expiresOn ) {
264270 return nil , fmt .Errorf ("attested document expired at %s" , data .TimeStamp .ExpiresOn )
265271 }
272+ klog .V (4 ).Infof ("Attested document not expired (expiresOn=%s)" , expiresOn .Format (time .RFC3339 ))
266273 }
267274
268275 return & data , nil
@@ -319,12 +326,14 @@ func intermediateCertPoolWithCaches(signer *x509.Certificate, fetch func(*x509.C
319326 // Positive cache wins over negative: a successful later fetch overwrites
320327 // any stale negative entry, which expires on its own shorter TTL.
321328 if obj , ok , _ := positive .GetByKey (keyStr ); ok {
329+ klog .V (4 ).Infof ("Intermediate certificate cache hit (positive) for signer issuer %q" , signer .Issuer )
322330 return obj .(* intermediateCertCacheEntry ).pool , nil
323331 }
324332 if _ , ok , _ := negative .GetByKey (keyStr ); ok {
325- return nil , fmt .Errorf ("intermediate certificate fetch recently failed for signer issuer (cached)" )
333+ return nil , fmt .Errorf ("intermediate certificate fetch recently failed for signer issuer %q (cached)" , signer . Issuer )
326334 }
327335
336+ klog .V (2 ).Infof ("Intermediate certificate cache miss for signer issuer %q; fetching from Microsoft PKI" , signer .Issuer )
328337 pool , fetchErr := fetch (signer )
329338 entry .pool = pool
330339 if fetchErr != nil {
@@ -373,19 +382,23 @@ func fetchIntermediateCertsFromBaseURL(client *http.Client, baseURL string, sign
373382 pool := x509 .NewCertPool ()
374383 matched := 0
375384 for _ , url := range urls {
385+ klog .V (4 ).Infof ("Fetching intermediate certificate from %s" , url )
376386 cert , err := fetchCertificate (client , url )
377387 if err != nil {
378388 return nil , err
379389 }
380390 if err := validateFetchedIntermediateForSigner (signer , cert ); err != nil {
391+ klog .V (4 ).Infof ("Fetched certificate from %s did not match signer issuer: %v" , url , err )
381392 continue
382393 }
394+ klog .V (4 ).Infof ("Fetched intermediate certificate from %s matched signer issuer (subject=%q)" , url , cert .Subject )
383395 pool .AddCert (cert )
384396 matched ++
385397 }
386398 if matched == 0 {
387- return nil , fmt .Errorf ("no fetched intermediate certificates matched signer issuer" )
399+ return nil , fmt .Errorf ("no fetched intermediate certificates matched signer issuer %q" , signer . Issuer )
388400 }
401+ klog .V (2 ).Infof ("Fetched %d intermediate certificate(s) matching signer issuer %q from %d candidate URL(s)" , matched , signer .Issuer , len (urls ))
389402
390403 return pool , nil
391404}
0 commit comments