Skip to content

Commit ca5cbba

Browse files
committed
sbsign/verify: add tests and comments
Signed-off-by: Stephan Mueller <smueller@chronox.de>
1 parent 909f22e commit ca5cbba

File tree

3 files changed

+141
-51
lines changed

3 files changed

+141
-51
lines changed

apps/src/sbsign.c

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -128,28 +128,30 @@ static int set_default_outfilename(struct pkcs7_generator_opts *opts,
128128
return ret;
129129
}
130130

131+
/* Set the authenticated attribute OID */
131132
int lc_spc_attribute_type_OID_enc(void *context, uint8_t *data,
132133
size_t *avail_datalen, uint8_t *tag)
133134
{
134-
/* SPC_PE_IMAGE_DATAOBJ OID (1.3.6.1.4.1.311.2.1.15) */
135-
static const uint8_t spc_indirect_data_objid[] = {
136-
0x2b, 0x6, 0x1, 0x4, 0x1, 0x82, 0x37, 0x2, 0x1, 0xf,
137-
};
135+
const uint8_t *oid_data;
136+
size_t oid_datalen;
138137
int ret;
139138

140139
(void)context;
141140
(void)tag;
142141

143-
CKINT(lc_x509_sufficient_size(avail_datalen,
144-
sizeof(spc_indirect_data_objid)));
142+
/* SPC_PE_IMAGE_DATAOBJ OID (1.3.6.1.4.1.311.2.1.15) */
143+
CKINT(lc_OID_to_data(OID_msPeImageDataObjId, &oid_data, &oid_datalen));
144+
145+
CKINT(lc_x509_sufficient_size(avail_datalen, oid_datalen));
145146

146-
memcpy(data, spc_indirect_data_objid, sizeof(spc_indirect_data_objid));
147-
*avail_datalen -= sizeof(spc_indirect_data_objid);
147+
memcpy(data, oid_data, oid_datalen);
148+
*avail_datalen -= oid_datalen;
148149

149150
out:
150151
return ret;
151152
}
152153

154+
/* Set the "obsolete" file name */
153155
int lc_spc_filename_obsolete_enc(void *context, uint8_t *data,
154156
size_t *avail_datalen, uint8_t *tag)
155157
{
@@ -172,6 +174,7 @@ int lc_spc_filename_obsolete_enc(void *context, uint8_t *data,
172174
return ret;
173175
}
174176

177+
/* Create and set the SpcPeImageData */
175178
int lc_spc_pe_image_data_enc(void *context, uint8_t *data,
176179
size_t *avail_datalen, uint8_t *tag)
177180
{
@@ -191,6 +194,7 @@ int lc_spc_pe_image_data_enc(void *context, uint8_t *data,
191194
return ret;
192195
}
193196

197+
/* Set message digest hash type */
194198
int lc_spc_digest_algorithm_OID_enc(void *context, uint8_t *data,
195199
size_t *avail_datalen, uint8_t *tag)
196200
{
@@ -231,6 +235,7 @@ int lc_spc_digest_algorithm_OID_enc(void *context, uint8_t *data,
231235
return ret;
232236
}
233237

238+
/* Write the actual image message digest into PKCS#7 message */
234239
int lc_spc_file_digest_enc(void *context, uint8_t *data, size_t *avail_datalen,
235240
uint8_t *tag)
236241
{
@@ -292,7 +297,8 @@ static int pkcs7_gen_message_sbsign(struct pkcs7_generator_opts *opts)
292297

293298
/*
294299
* As defined in the "Windows Authenticode Portable Executable Signature
295-
* Format" The content must be set to SpcIndirectDataContent
300+
* Format" The content must be set to SpcIndirectDataContent. This
301+
* content is generated here.
296302
*/
297303
avail_datalen = LC_AUTHENTICODE_SPC_INDIRECT_DATA_CONTENT_SIZE;
298304
CKINT(lc_asn1_ber_encoder(
@@ -301,20 +307,22 @@ static int pkcs7_gen_message_sbsign(struct pkcs7_generator_opts *opts)
301307
datalen =
302308
LC_AUTHENTICODE_SPC_INDIRECT_DATA_CONTENT_SIZE - avail_datalen;
303309

310+
/* Initialize the encoding context */
304311
CKINT(lc_pkcs7_encode_ctx_init(&ws->ctx));
305312

306-
/*
307-
* Set the data type to messageDigest
308-
*/
313+
/* Set the data type to messageDigest */
309314
CKINT(lc_pkcs7_encode_ctx_set_signer_data_type(&ws->ctx,
310315
OID_messageDigest));
311316

312317
/*
313-
* Set and embed the SpcIndirectDataContent into the PKCS#7 message.
318+
* Set and embed the SpcIndirectDataContent into the PKCS#7 message
319+
* using SPC_INDIRECT_DATA_OBJID (1.3.6.1.4.1.311.2.1.4).
314320
*/
315321
CKINT(lc_pkcs7_set_data_with_type(
316322
pkcs7, ws->authenticode_SpcIndirectDataContent, datalen,
317323
lc_pkcs7_set_data_embed, OID_msIndirectData));
324+
325+
/* Set the PKCS#7 message structure to be encoded */
318326
CKINT(lc_pkcs7_encode_ctx_set_pkcs7(&ws->ctx, pkcs7));
319327

320328
/*
@@ -327,11 +335,13 @@ static int pkcs7_gen_message_sbsign(struct pkcs7_generator_opts *opts)
327335
// sizeof(spc_sp_opus_info_objid),
328336
// ));
329337

338+
/* Perform the actual message encoding. */
330339
avail_datalen = ASN1_MAX_DATASIZE;
331340
CKINT_LOG(lc_pkcs7_encode_ctx(&ws->ctx, ws->data, &avail_datalen),
332341
"Message generation failed\n");
333342
datalen = ASN1_MAX_DATASIZE - avail_datalen;
334343

344+
/* Add the encoded PKCS#7 message block with signature to image */
335345
CKINT(image_add_signature(&ws->image, ws->data, datalen));
336346
if (opts->infile_flags == lc_pkcs7_set_data_embed) {
337347
image_write(&ws->image, outfile_p);

apps/src/sbverify.c

Lines changed: 47 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -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 */
175176
int 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 */
194195
int 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 */
209208
int 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 */
231231
int 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

423452
out:
424453
pkcs7_clean_opts(&parsed_opts);

0 commit comments

Comments
 (0)