@@ -95,40 +95,17 @@ 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 )
98+ // Read and extract the vTPM AK certificate. If this fails, we log a warning and continue without it
99+ akCert , err := i .readAKCertificateFromTPM (tpm )
103100 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- }
101+ i .log .Warn (fmt .Sprintf ("Failed to read AK certificate: %v" , err ))
102+ akCert = nil
126103 }
127104
128105 instanceInfo := InstanceInfo {
129106 AttestationReport : quote ,
130107 RuntimeData : runtimeData ,
131- AkCert : cleanCertDER , // Use the clean certificate
108+ AkCert : akCert , // Use the clean certificate
132109 }
133110 instanceInfoJSON , err := json .Marshal (instanceInfo )
134111 if err != nil {
@@ -137,6 +114,34 @@ func (i *Issuer) getInstanceInfo(ctx context.Context, tpm io.ReadWriteCloser, _
137114 return instanceInfoJSON , nil
138115}
139116
117+ // readAKCertificateFromTPM reads and extracts the attestation key certificate from TPM.
118+ // Returns the clean DER-encoded certificate or an error if reading/extraction fails.
119+ func (i * Issuer ) readAKCertificateFromTPM (tpm io.ReadWriteCloser ) ([]byte , error ) {
120+ certDERRaw , err := tpm2 .NVReadEx (tpm , tpmAkCertIdx , tpm2 .HandleOwner , "" , 0 )
121+ if err != nil {
122+ return nil , fmt .Errorf ("reading attestation key certificate from TPM: %w" , err )
123+ }
124+
125+ i .log .Debug (fmt .Sprintf ("Read %d bytes from TPM AK cert index" , len (certDERRaw )))
126+
127+ // The TPM NV index contains trailing data. We need to extract just the certificate.
128+ // X.509 DER certificates start with 0x30 (SEQUENCE) followed by length encoding
129+ cleanCertDER , err := extractDERCertificate (certDERRaw )
130+ if err != nil {
131+ return nil , fmt .Errorf ("extracting certificate from TPM data: %w" , err )
132+ }
133+
134+ i .log .Debug (fmt .Sprintf ("Extracted %d bytes certificate from %d bytes TPM data" , len (cleanCertDER ), len (certDERRaw )))
135+
136+ // Verify we can parse the extracted certificate
137+ _ , err = x509 .ParseCertificate (cleanCertDER )
138+ if err != nil {
139+ return nil , fmt .Errorf ("parsing extracted attestation key certificate: %w" , err )
140+ }
141+
142+ return cleanCertDER , nil
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