@@ -168,29 +168,57 @@ int main(int argc, char *argv[])
168168 exit (1 );
169169 printf ("Signature of size %d\n" , sigsize );
170170
171- int outlen = OFFSET_OF ( EFI_VARIABLE_AUTHENTICATION_2 , AuthInfo . CertData ) + sigsize ;
172- EFI_VARIABLE_AUTHENTICATION_2 * var_auth = malloc (outlen );
171+ int outlen = sizeof ( EFI_TIME ) + sizeof ( WIN_CERTIFICATE ) + sizeof ( EFI_GUID ) + sigsize + st . st_size ;
172+ unsigned char * var_auth = malloc (outlen );
173173
174- var_auth -> TimeStamp = timestamp ;
175- var_auth -> AuthInfo .CertType = EFI_CERT_TYPE_PKCS7_GUID ;
176- var_auth -> AuthInfo .Hdr .dwLength = sigsize + OFFSET_OF (WIN_CERTIFICATE_UEFI_GUID , CertData );
177- var_auth -> AuthInfo .Hdr .wRevision = 0x0200 ;
178- var_auth -> AuthInfo .Hdr .wCertificateType = WIN_CERT_TYPE_EFI_GUID ;
174+ // The length of the entire certificate, including the length of the header, in bytes.
175+ // -- Is this correct? What about sizeof(EFI_TIME) and st.st_size?
176+ UINT32 dwLength = sizeof (WIN_CERTIFICATE ) + sizeof (EFI_GUID ) + sigsize ;
179177
180- memcpy (var_auth -> AuthInfo .CertData , sigbuf , sigsize );
181- sigbuf = var_auth -> AuthInfo .CertData ;
182- printf ("Signature at: %ld\n" , sigbuf - (unsigned char * )var_auth );
178+ // The revision level of the WIN_CERTIFICATE structure. The current revision level is 0x0200.
179+ UINT16 wRevision = 0x0200 ;
180+
181+ // The certificate type. See WIN_CERT_TYPE_xxx for the UEFI certificate types.
182+ // The UEFI specification reserves the range of certificate type values from 0x0EF0 to 0x0EFF.
183+ UINT16 wCertificateType = WIN_CERT_TYPE_EFI_GUID ;
184+
185+ // This is the unique id which determines the format of the CertData.
186+ EFI_GUID certType = EFI_CERT_TYPE_PKCS7_GUID ;
187+
188+ // EFI_TIME TimeStamp
189+ memcpy (var_auth , & timestamp , sizeof (EFI_TIME ));
190+
191+ // UINT32 AuthInfo.Hdr.dwLength
192+ memcpy (var_auth + sizeof (EFI_TIME ), & dwLength , sizeof (UINT32 ));
193+
194+ // UINT16 AuthInfo.Hdr.wRevision
195+ memcpy (var_auth + sizeof (EFI_TIME ) + sizeof (UINT32 ), & wRevision , sizeof (UINT16 ));
196+
197+ // UINT16 AuthInfo.Hdr.wCertificateType
198+ memcpy (var_auth + sizeof (EFI_TIME ) + sizeof (UINT32 ) + sizeof (UINT16 ),
199+ & wCertificateType , sizeof (UINT16 ));
200+
201+ // EFI_GUID AuthInfo.CertType
202+ memcpy (var_auth + sizeof (EFI_TIME ) + sizeof (WIN_CERTIFICATE ),
203+ & certType , sizeof (EFI_GUID ));
204+
205+ // AuthInfo.CertData
206+ memcpy (var_auth + sizeof (EFI_TIME ) + sizeof (WIN_CERTIFICATE ) + sizeof (EFI_GUID ),
207+ sigbuf , sigsize );
208+
209+ // Authentication header complete, now write the payload
210+ memcpy (var_auth + sizeof (EFI_TIME ) + sizeof (WIN_CERTIFICATE ) + sizeof (EFI_GUID ) + sigsize ,
211+ signbuf + signbuf_header_len , st .st_size );
183212
184213 int fdoutfile = open (outfile , O_CREAT |O_WRONLY |O_TRUNC , S_IWUSR |S_IRUSR );
185214 if (fdoutfile == -1 ) {
186215 fprintf (stderr , "failed to open %s: " , outfile );
187216 perror ("" );
188217 exit (1 );
189218 }
190- /* first we write the authentication header */
219+
191220 write (fdoutfile , var_auth , outlen );
192- /* Then we write the payload */
193- write (fdoutfile , signbuf + signbuf_header_len , st .st_size );
221+
194222 /* so now the file is complete and can be fed straight into
195223 * SetVariable() as an authenticated variable update */
196224 close (fdoutfile );
0 commit comments