@@ -172,25 +172,26 @@ print_certificate_store_certs(const struct pkcs7_generator_opts *parsed_opts)
172172 return 0 ;
173173}
174174
175+ /* Verify the authenticated attribute OID */
175176int lc_spc_attribute_type_OID (void * context , size_t hdrlen , unsigned char tag ,
176177 const uint8_t * value , size_t vlen )
177178{
178- /* SPC_PE_IMAGE_DATAOBJ OID (1.3.6.1.4.1.311.2.1.15) */
179- static const uint8_t spc_indirect_data_objid [] = {
180- 0x2b , 0x6 , 0x1 , 0x4 , 0x1 , 0x82 , 0x37 , 0x2 , 0x1 , 0xf ,
181- };
179+ enum OID oid ;
182180
183181 (void )context ;
184182 (void )hdrlen ;
185183 (void )tag ;
186184
187- if (lc_memcmp_secure (value , vlen , spc_indirect_data_objid ,
188- sizeof (spc_indirect_data_objid )))
185+ oid = lc_look_up_OID (value , vlen );
186+
187+ /* SPC_PE_IMAGE_DATAOBJ OID (1.3.6.1.4.1.311.2.1.15) */
188+ if (oid != OID_msPeImageDataObjId )
189189 return - EINVAL ;
190190
191191 return 0 ;
192192}
193193
194+ /* No parsing and verification of the SpcPeImageData */
194195int lc_spc_pe_image_data (void * context , size_t hdrlen , unsigned char tag ,
195196 const uint8_t * value , size_t vlen )
196197{
@@ -200,12 +201,10 @@ int lc_spc_pe_image_data(void *context, size_t hdrlen, unsigned char tag,
200201 (void )value ;
201202 (void )vlen ;
202203
203- /*
204- * We are not decoding lc_authenticode_SpcPeImageData_encoder
205- */
206204 return 0 ;
207205}
208206
207+ /* Retrieve message digest hash type */
209208int lc_spc_digest_algorithm_OID (void * context , size_t hdrlen , unsigned char tag ,
210209 const uint8_t * value , size_t vlen )
211210{
@@ -228,6 +227,7 @@ int lc_spc_digest_algorithm_OID(void *context, size_t hdrlen, unsigned char tag,
228227 return ret ;
229228}
230229
230+ /* Retrieve the image message digest from the PKCS#7 message */
231231int lc_spc_file_digest (void * context , size_t hdrlen , unsigned char tag ,
232232 const uint8_t * value , size_t vlen )
233233{
@@ -242,7 +242,7 @@ int lc_spc_file_digest(void *context, size_t hdrlen, unsigned char tag,
242242 return 0 ;
243243}
244244
245- static int sbverify_dump_file (struct pkcs7_generator_opts * opts , int verbose )
245+ static int sbverify_file (struct pkcs7_generator_opts * opts , int verbose )
246246{
247247// static const uint8_t spc_sp_opus_info_objid[] = {
248248// 0x2b, 0x6, 0x1, 0x4, 0x1, 0x82, 0x37, 0x2, 0x1, 0xc,
@@ -263,31 +263,39 @@ static int sbverify_dump_file(struct pkcs7_generator_opts *opts, int verbose)
263263 int ret ;
264264 LC_DECLARE_MEM (ws , struct workspace , sizeof (uint64_t ));
265265
266+ /* Open the binary image file */
266267 CKINT (get_data (opts -> infile , & image_buf , & image_size ,
267268 lc_pem_flag_nopem ));
268269
269- /* Parse image */
270+ /* Parse binar image */
270271 CKINT (image_load (image_buf , image_size , & ws -> image ));
271272
273+ /* Loop over all signatures */
272274 for (;;) {
273275 if (opts -> pkcs7_msg ) {
276+ /* Only one detached signature can be processed */
274277 if (signum > 0 )
275278 break ;
276279
277- CKINT (get_data (opts -> infile , & detached_sig_buf ,
280+ /* Read detached signature */
281+ CKINT (get_data (opts -> pkcs7_msg , & detached_sig_buf ,
278282 & detached_sig_buflen ,
279283 lc_pem_flag_nopem ));
280284
285+ /* Set the read detached signature for processing */
281286 signature = detached_sig_buf ;
282287 signaturelen = detached_sig_buflen ;
283288 } else {
289+ /* Fetch the embedded signature from image */
284290 ret = image_get_signature (& ws -> image , signum ,
285291 & signature , & signaturelen );
286292 if (ret ) {
287293 if (signum > 0 ) {
294+ /* We processed all signatures */
288295 ret = 0 ;
289296 break ;
290297 } else {
298+ /* Failure in reading the signature */
291299 fprintf (stderr ,
292300 "Unable to read signature data from %s\n" ,
293301 opts -> infile );
@@ -300,23 +308,35 @@ static int sbverify_dump_file(struct pkcs7_generator_opts *opts, int verbose)
300308 if (verbose || opts -> print_pkcs7 )
301309 printf ("signature %d\n" , signum );
302310
311+ /* Initialize the decoding context */
303312 CKINT (lc_pkcs7_decode_ctx_init (& ws -> ctx ));
304313
314+ /*
315+ * Set the PKCS#7 message buffer where the decoding result goes
316+ * to.
317+ */
305318 CKINT (lc_pkcs7_decode_ctx_set_pkcs7 (& ws -> ctx , pkcs7 ));
306319
320+ /*
321+ * The authenticated attributes data type shall be
322+ * SPC_INDIRECT_DATA_OBJID (1.3.6.1.4.1.311.2.1.4)
323+ */
307324 CKINT (lc_pkcs7_decode_ctx_set_aa_content_type (
308325 & ws -> ctx , OID_msIndirectData ));
309326
327+ /* Perform the actual message decoding */
310328 CKINT_LOG (lc_pkcs7_decode_ctx (& ws -> ctx , signature ,
311329 signaturelen ),
312330 "Unable to parse signature data\n" );
313331
314- /*
315- * Now, if we have data with the PKCS7 message, attempt to verify it
316- * (i.e. perform a signature verification).
317- */
332+ /* Get the embedded data. */
318333 CKINT (lc_pkcs7_get_content_data (opts -> pkcs7 , & avail_data ,
319334 & avail_datalen ));
335+
336+ /*
337+ * Perform the verification of the parsed message and its
338+ * authenticated attributes.
339+ */
320340 CKINT_LOG (lc_pkcs7_verify (
321341 opts -> pkcs7 ,
322342 opts -> use_trust_store ? & opts -> trust_store :
@@ -325,7 +345,11 @@ static int sbverify_dump_file(struct pkcs7_generator_opts *opts, int verbose)
325345 NULL ),
326346 "Unable to verify signature\n" );
327347
328- /* Attempt to decode the signature */
348+ /*
349+ * Attempt to decode the embedded data as
350+ * SpcIndirectDataContent. Here we will get the signed image
351+ * hash.
352+ */
329353 CKINT (lc_asn1_ber_decoder (
330354 & lc_authenticode_SpcIndirectDataContent_decoder ,
331355 & ws -> authenticode_ctx , avail_data , avail_datalen ));
@@ -334,6 +358,11 @@ static int sbverify_dump_file(struct pkcs7_generator_opts *opts, int verbose)
334358 CKINT (image_hash (& ws -> image , ws -> authenticode_ctx .hash ,
335359 ws -> image_digest , & opts -> aux_datalen ));
336360
361+ /*
362+ * Perform the actual authentication of the image:
363+ * comparing the newly calculated hash with the signed hash from
364+ * the PKCS#7 message.
365+ */
337366 if (lc_memcmp_secure (
338367 ws -> image_digest , opts -> aux_datalen ,
339368 ws -> authenticode_ctx .decoded_image_digest ,
@@ -418,7 +447,7 @@ int main(int argc, char **argv)
418447 if (parsed_opts .infile )
419448 CKINT (pkcs7_set_data (& parsed_opts ));
420449
421- CKINT (sbverify_dump_file (& parsed_opts , verbose ));
450+ CKINT (sbverify_file (& parsed_opts , verbose ));
422451
423452out :
424453 pkcs7_clean_opts (& parsed_opts );
0 commit comments