@@ -95,40 +95,13 @@ func (i *Issuer) getInstanceInfo(ctx context.Context, tpm io.ReadWriteCloser, _
9595 return nil , fmt .Errorf ("getting quote: %w" , err )
9696 }
9797
98- // Read the vTPM AK certificate from TPM NV index
99- // This certificate is signed by Azure and needs to be validated on the validator side
100- // If reading fails, we log a warning and continue - the validator will decide if this is critical
101- var cleanCertDER []byte
102- certDERRaw , err := tpm2 .NVReadEx (tpm , tpmAkCertIdx , tpm2 .HandleOwner , "" , 0 )
103- if err != nil {
104- i .log .Warn (fmt .Sprintf ("Failed to read attestation key certificate from TPM: %v" , err ))
105- } else {
106- i .log .Debug (fmt .Sprintf ("Read %d bytes from TPM AK cert index" , len (certDERRaw )))
107-
108- // The TPM NV index contains trailing data. We need to extract just the certificate.
109- // X.509 DER certificates start with 0x30 (SEQUENCE) followed by length encoding
110- cleanCertDER , err = extractDERCertificate (certDERRaw )
111- if err != nil {
112- i .log .Warn (fmt .Sprintf ("Failed to extract certificate from TPM data: %v" , err ))
113- cleanCertDER = nil
114- } else {
115- i .log .Debug (fmt .Sprintf ("Extracted %d bytes certificate from %d bytes TPM data" , len (cleanCertDER ), len (certDERRaw )))
116-
117- // Verify we can parse the extracted certificate
118- _ , err = x509 .ParseCertificate (cleanCertDER )
119- if err != nil {
120- i .log .Warn (fmt .Sprintf ("Failed to parse extracted attestation key certificate: %v" , err ))
121- cleanCertDER = nil
122- } else {
123- i .log .Debug ("Successfully extracted and validated AK certificate format" )
124- }
125- }
126- }
98+ // Read and extract the vTPM AK certificate (returns nil on failure with warnings logged)
99+ akCert := i .readAKCertificateFromTPM (tpm )
127100
128101 instanceInfo := InstanceInfo {
129102 AttestationReport : quote ,
130103 RuntimeData : runtimeData ,
131- AkCert : cleanCertDER , // Use the clean certificate
104+ AkCert : akCert , // Use the clean certificate
132105 }
133106 instanceInfoJSON , err := json .Marshal (instanceInfo )
134107 if err != nil {
@@ -137,6 +110,38 @@ func (i *Issuer) getInstanceInfo(ctx context.Context, tpm io.ReadWriteCloser, _
137110 return instanceInfoJSON , nil
138111}
139112
113+ // readAKCertificateFromTPM reads and extracts the attestation key certificate from TPM.
114+ // Returns the clean DER-encoded certificate, or nil if reading/extraction fails.
115+ // Failures are logged as warnings since AK cert verification is optional.
116+ func (i * Issuer ) readAKCertificateFromTPM (tpm io.ReadWriteCloser ) []byte {
117+ certDERRaw , err := tpm2 .NVReadEx (tpm , tpmAkCertIdx , tpm2 .HandleOwner , "" , 0 )
118+ if err != nil {
119+ i .log .Warn (fmt .Sprintf ("Failed to read attestation key certificate from TPM: %v" , err ))
120+ return nil
121+ }
122+
123+ i .log .Debug (fmt .Sprintf ("Read %d bytes from TPM AK cert index" , len (certDERRaw )))
124+
125+ // The TPM NV index contains trailing data. We need to extract just the certificate.
126+ // X.509 DER certificates start with 0x30 (SEQUENCE) followed by length encoding
127+ cleanCertDER , err := extractDERCertificate (certDERRaw )
128+ if err != nil {
129+ i .log .Warn (fmt .Sprintf ("Failed to extract certificate from TPM data: %v" , err ))
130+ return nil
131+ }
132+
133+ i .log .Debug (fmt .Sprintf ("Extracted %d bytes certificate from %d bytes TPM data" , len (cleanCertDER ), len (certDERRaw )))
134+
135+ // Verify we can parse the extracted certificate
136+ _ , err = x509 .ParseCertificate (cleanCertDER )
137+ if err != nil {
138+ i .log .Warn (fmt .Sprintf ("Failed to parse extracted attestation key certificate: %v" , err ))
139+ return nil
140+ }
141+
142+ return cleanCertDER
143+ }
144+
140145// extractDERCertificate extracts a clean X.509 DER certificate from raw TPM data.
141146// The TPM NV index may contain trailing data, so this function parses the DER
142147// structure to extract exactly the certificate bytes.
0 commit comments