Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / ipxe / src / crypto / cms.c
1 /*
2  * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA.
18  */
19
20 FILE_LICENCE ( GPL2_OR_LATER );
21
22 /** @file
23  *
24  * Cryptographic Message Syntax (PKCS #7)
25  *
26  * The format of CMS messages is defined in RFC 5652.
27  *
28  */
29
30 #include <stdint.h>
31 #include <string.h>
32 #include <time.h>
33 #include <errno.h>
34 #include <ipxe/asn1.h>
35 #include <ipxe/x509.h>
36 #include <ipxe/malloc.h>
37 #include <ipxe/uaccess.h>
38 #include <ipxe/cms.h>
39
40 /* Disambiguate the various error causes */
41 #define EACCES_NON_SIGNING \
42         __einfo_error ( EINFO_EACCES_NON_SIGNING )
43 #define EINFO_EACCES_NON_SIGNING \
44         __einfo_uniqify ( EINFO_EACCES, 0x01, "Not a signing certificate" )
45 #define EACCES_NON_CODE_SIGNING \
46         __einfo_error ( EINFO_EACCES_NON_CODE_SIGNING )
47 #define EINFO_EACCES_NON_CODE_SIGNING \
48         __einfo_uniqify ( EINFO_EACCES, 0x02, "Not a code-signing certificate" )
49 #define EACCES_WRONG_NAME \
50         __einfo_error ( EINFO_EACCES_WRONG_NAME )
51 #define EINFO_EACCES_WRONG_NAME \
52         __einfo_uniqify ( EINFO_EACCES, 0x04, "Incorrect certificate name" )
53 #define EACCES_NO_SIGNATURES \
54         __einfo_error ( EINFO_EACCES_NO_SIGNATURES )
55 #define EINFO_EACCES_NO_SIGNATURES \
56         __einfo_uniqify ( EINFO_EACCES, 0x05, "No signatures present" )
57 #define EINVAL_DIGEST \
58         __einfo_error ( EINFO_EINVAL_DIGEST )
59 #define EINFO_EINVAL_DIGEST \
60         __einfo_uniqify ( EINFO_EINVAL, 0x01, "Not a digest algorithm" )
61 #define EINVAL_PUBKEY \
62         __einfo_error ( EINFO_EINVAL_PUBKEY )
63 #define EINFO_EINVAL_PUBKEY \
64         __einfo_uniqify ( EINFO_EINVAL, 0x02, "Not a public-key algorithm" )
65 #define ENOTSUP_SIGNEDDATA \
66         __einfo_error ( EINFO_ENOTSUP_SIGNEDDATA )
67 #define EINFO_ENOTSUP_SIGNEDDATA \
68         __einfo_uniqify ( EINFO_ENOTSUP, 0x01, "Not a digital signature" )
69
70 /** "pkcs7-signedData" object identifier */
71 static uint8_t oid_signeddata[] = { ASN1_OID_SIGNEDDATA };
72
73 /** "pkcs7-signedData" object identifier cursor */
74 static struct asn1_cursor oid_signeddata_cursor =
75         ASN1_OID_CURSOR ( oid_signeddata );
76
77 /**
78  * Parse CMS signature content type
79  *
80  * @v sig               CMS signature
81  * @v raw               ASN.1 cursor
82  * @ret rc              Return status code
83  */
84 static int cms_parse_content_type ( struct cms_signature *sig,
85                                     const struct asn1_cursor *raw ) {
86         struct asn1_cursor cursor;
87
88         /* Enter contentType */
89         memcpy ( &cursor, raw, sizeof ( cursor ) );
90         asn1_enter ( &cursor, ASN1_OID );
91
92         /* Check OID is pkcs7-signedData */
93         if ( asn1_compare ( &cursor, &oid_signeddata_cursor ) != 0 ) {
94                 DBGC ( sig, "CMS %p does not contain signedData:\n", sig );
95                 DBGC_HDA ( sig, 0, raw->data, raw->len );
96                 return -ENOTSUP_SIGNEDDATA;
97         }
98
99         DBGC ( sig, "CMS %p contains signedData\n", sig );
100         return 0;
101 }
102
103 /**
104  * Parse CMS signature certificate list
105  *
106  * @v sig               CMS signature
107  * @v raw               ASN.1 cursor
108  * @ret rc              Return status code
109  */
110 static int cms_parse_certificates ( struct cms_signature *sig,
111                                     const struct asn1_cursor *raw ) {
112         struct asn1_cursor cursor;
113         struct x509_certificate *cert;
114         int rc;
115
116         /* Enter certificates */
117         memcpy ( &cursor, raw, sizeof ( cursor ) );
118         asn1_enter ( &cursor, ASN1_EXPLICIT_TAG ( 0 ) );
119
120         /* Add each certificate */
121         while ( cursor.len ) {
122
123                 /* Add certificate to chain */
124                 if ( ( rc = x509_append_raw ( sig->certificates, cursor.data,
125                                               cursor.len ) ) != 0 ) {
126                         DBGC ( sig, "CMS %p could not append certificate: %s\n",
127                                sig, strerror ( rc) );
128                         DBGC_HDA ( sig, 0, cursor.data, cursor.len );
129                         return rc;
130                 }
131                 cert = x509_last ( sig->certificates );
132                 DBGC ( sig, "CMS %p found certificate %s\n",
133                        sig, x509_name ( cert ) );
134
135                 /* Move to next certificate */
136                 asn1_skip_any ( &cursor );
137         }
138
139         return 0;
140 }
141
142 /**
143  * Identify CMS signature certificate by issuer and serial number
144  *
145  * @v sig               CMS signature
146  * @v issuer            Issuer
147  * @v serial            Serial number
148  * @ret cert            X.509 certificate, or NULL if not found
149  */
150 static struct x509_certificate *
151 cms_find_issuer_serial ( struct cms_signature *sig,
152                          const struct asn1_cursor *issuer,
153                          const struct asn1_cursor *serial ) {
154         struct x509_link *link;
155         struct x509_certificate *cert;
156
157         /* Scan through certificate list */
158         list_for_each_entry ( link, &sig->certificates->links, list ) {
159
160                 /* Check issuer and serial number */
161                 cert = link->cert;
162                 if ( ( asn1_compare ( issuer, &cert->issuer.raw ) == 0 ) &&
163                      ( asn1_compare ( serial, &cert->serial.raw ) == 0 ) )
164                         return cert;
165         }
166
167         return NULL;
168 }
169
170 /**
171  * Parse CMS signature signer identifier
172  *
173  * @v sig               CMS signature
174  * @v info              Signer information to fill in
175  * @v raw               ASN.1 cursor
176  * @ret rc              Return status code
177  */
178 static int cms_parse_signer_identifier ( struct cms_signature *sig,
179                                          struct cms_signer_info *info,
180                                          const struct asn1_cursor *raw ) {
181         struct asn1_cursor cursor;
182         struct asn1_cursor serial;
183         struct asn1_cursor issuer;
184         struct x509_certificate *cert;
185         int rc;
186
187         /* Enter issuerAndSerialNumber */
188         memcpy ( &cursor, raw, sizeof ( cursor ) );
189         asn1_enter ( &cursor, ASN1_SEQUENCE );
190
191         /* Identify issuer */
192         memcpy ( &issuer, &cursor, sizeof ( issuer ) );
193         if ( ( rc = asn1_shrink ( &issuer, ASN1_SEQUENCE ) ) != 0 ) {
194                 DBGC ( sig, "CMS %p/%p could not locate issuer: %s\n",
195                        sig, info, strerror ( rc ) );
196                 DBGC_HDA ( sig, 0, raw->data, raw->len );
197                 return rc;
198         }
199         DBGC ( sig, "CMS %p/%p issuer is:\n", sig, info );
200         DBGC_HDA ( sig, 0, issuer.data, issuer.len );
201         asn1_skip_any ( &cursor );
202
203         /* Identify serialNumber */
204         memcpy ( &serial, &cursor, sizeof ( serial ) );
205         if ( ( rc = asn1_shrink ( &serial, ASN1_INTEGER ) ) != 0 ) {
206                 DBGC ( sig, "CMS %p/%p could not locate serialNumber: %s\n",
207                        sig, info, strerror ( rc ) );
208                 DBGC_HDA ( sig, 0, raw->data, raw->len );
209                 return rc;
210         }
211         DBGC ( sig, "CMS %p/%p serial number is:\n", sig, info );
212         DBGC_HDA ( sig, 0, serial.data, serial.len );
213
214         /* Identify certificate */
215         cert = cms_find_issuer_serial ( sig, &issuer, &serial );
216         if ( ! cert ) {
217                 DBGC ( sig, "CMS %p/%p could not identify signer's "
218                        "certificate\n", sig, info );
219                 return -ENOENT;
220         }
221
222         /* Append certificate to chain */
223         if ( ( rc = x509_append ( info->chain, cert ) ) != 0 ) {
224                 DBGC ( sig, "CMS %p/%p could not append certificate: %s\n",
225                        sig, info, strerror ( rc ) );
226                 return rc;
227         }
228
229         /* Append remaining certificates to chain */
230         if ( ( rc = x509_auto_append ( info->chain,
231                                        sig->certificates ) ) != 0 ) {
232                 DBGC ( sig, "CMS %p/%p could not append certificates: %s\n",
233                        sig, info, strerror ( rc ) );
234                 return rc;
235         }
236
237         return 0;
238 }
239
240 /**
241  * Parse CMS signature digest algorithm
242  *
243  * @v sig               CMS signature
244  * @v info              Signer information to fill in
245  * @v raw               ASN.1 cursor
246  * @ret rc              Return status code
247  */
248 static int cms_parse_digest_algorithm ( struct cms_signature *sig,
249                                         struct cms_signer_info *info,
250                                         const struct asn1_cursor *raw ) {
251         struct asn1_algorithm *algorithm;
252         int rc;
253
254         /* Identify algorithm */
255         if ( ( rc = asn1_digest_algorithm ( raw, &algorithm ) ) != 0 ) {
256                 DBGC ( sig, "CMS %p/%p could not identify digest algorithm: "
257                        "%s\n", sig, info, strerror ( rc ) );
258                 DBGC_HDA ( sig, 0, raw->data, raw->len );
259                 return rc;
260         }
261
262         /* Record digest algorithm */
263         info->digest = algorithm->digest;
264         DBGC ( sig, "CMS %p/%p digest algorithm is %s\n",
265                sig, info, algorithm->name );
266
267         return 0;
268 }
269
270 /**
271  * Parse CMS signature algorithm
272  *
273  * @v sig               CMS signature
274  * @v info              Signer information to fill in
275  * @v raw               ASN.1 cursor
276  * @ret rc              Return status code
277  */
278 static int cms_parse_signature_algorithm ( struct cms_signature *sig,
279                                            struct cms_signer_info *info,
280                                            const struct asn1_cursor *raw ) {
281         struct asn1_algorithm *algorithm;
282         int rc;
283
284         /* Identify algorithm */
285         if ( ( rc = asn1_pubkey_algorithm ( raw, &algorithm ) ) != 0 ) {
286                 DBGC ( sig, "CMS %p/%p could not identify public-key "
287                        "algorithm: %s\n", sig, info, strerror ( rc ) );
288                 DBGC_HDA ( sig, 0, raw->data, raw->len );
289                 return rc;
290         }
291
292         /* Record signature algorithm */
293         info->pubkey = algorithm->pubkey;
294         DBGC ( sig, "CMS %p/%p public-key algorithm is %s\n",
295                sig, info, algorithm->name );
296
297         return 0;
298 }
299
300 /**
301  * Parse CMS signature value
302  *
303  * @v sig               CMS signature
304  * @v info              Signer information to fill in
305  * @v raw               ASN.1 cursor
306  * @ret rc              Return status code
307  */
308 static int cms_parse_signature_value ( struct cms_signature *sig,
309                                        struct cms_signer_info *info,
310                                        const struct asn1_cursor *raw ) {
311         struct asn1_cursor cursor;
312         int rc;
313
314         /* Enter signature */
315         memcpy ( &cursor, raw, sizeof ( cursor ) );
316         if ( ( rc = asn1_enter ( &cursor, ASN1_OCTET_STRING ) ) != 0 ) {
317                 DBGC ( sig, "CMS %p/%p could not locate signature:\n",
318                        sig, info );
319                 DBGC_HDA ( sig, 0, raw->data, raw->len );
320                 return rc;
321         }
322
323         /* Record signature */
324         info->signature_len = cursor.len;
325         info->signature = malloc ( info->signature_len );
326         if ( ! info->signature )
327                 return -ENOMEM;
328         memcpy ( info->signature, cursor.data, info->signature_len );
329         DBGC ( sig, "CMS %p/%p signature value is:\n", sig, info );
330         DBGC_HDA ( sig, 0, info->signature, info->signature_len );
331
332         return 0;
333 }
334
335 /**
336  * Parse CMS signature signer information
337  *
338  * @v sig               CMS signature
339  * @v info              Signer information to fill in
340  * @v raw               ASN.1 cursor
341  * @ret rc              Return status code
342  */
343 static int cms_parse_signer_info ( struct cms_signature *sig,
344                                    struct cms_signer_info *info,
345                                    const struct asn1_cursor *raw ) {
346         struct asn1_cursor cursor;
347         int rc;
348
349         /* Enter signerInfo */
350         memcpy ( &cursor, raw, sizeof ( cursor ) );
351         asn1_enter ( &cursor, ASN1_SEQUENCE );
352
353         /* Skip version */
354         asn1_skip ( &cursor, ASN1_INTEGER );
355
356         /* Parse sid */
357         if ( ( rc = cms_parse_signer_identifier ( sig, info, &cursor ) ) != 0 )
358                 return rc;
359         asn1_skip_any ( &cursor );
360
361         /* Parse digestAlgorithm */
362         if ( ( rc = cms_parse_digest_algorithm ( sig, info, &cursor ) ) != 0 )
363                 return rc;
364         asn1_skip_any ( &cursor );
365
366         /* Skip signedAttrs, if present */
367         asn1_skip_if_exists ( &cursor, ASN1_EXPLICIT_TAG ( 0 ) );
368
369         /* Parse signatureAlgorithm */
370         if ( ( rc = cms_parse_signature_algorithm ( sig, info, &cursor ) ) != 0)
371                 return rc;
372         asn1_skip_any ( &cursor );
373
374         /* Parse signature */
375         if ( ( rc = cms_parse_signature_value ( sig, info, &cursor ) ) != 0 )
376                 return rc;
377
378         return 0;
379 }
380
381 /**
382  * Parse CMS signature from ASN.1 data
383  *
384  * @v sig               CMS signature
385  * @v raw               ASN.1 cursor
386  * @ret rc              Return status code
387  */
388 static int cms_parse ( struct cms_signature *sig,
389                        const struct asn1_cursor *raw ) {
390         struct asn1_cursor cursor;
391         struct cms_signer_info *info;
392         int rc;
393
394         /* Enter contentInfo */
395         memcpy ( &cursor, raw, sizeof ( cursor ) );
396         asn1_enter ( &cursor, ASN1_SEQUENCE );
397
398         /* Parse contentType */
399
400         if ( ( rc = cms_parse_content_type ( sig, &cursor ) ) != 0 )
401                 return rc;
402         asn1_skip_any ( &cursor );
403
404         /* Enter content */
405         asn1_enter ( &cursor, ASN1_EXPLICIT_TAG ( 0 ) );
406
407         /* Enter signedData */
408         asn1_enter ( &cursor, ASN1_SEQUENCE );
409
410         /* Skip version */
411         asn1_skip ( &cursor, ASN1_INTEGER );
412
413         /* Skip digestAlgorithms */
414         asn1_skip ( &cursor, ASN1_SET );
415
416         /* Skip encapContentInfo */
417         asn1_skip ( &cursor, ASN1_SEQUENCE );
418
419         /* Parse certificates */
420         if ( ( rc = cms_parse_certificates ( sig, &cursor ) ) != 0 )
421                 return rc;
422         asn1_skip_any ( &cursor );
423
424         /* Skip crls, if present */
425         asn1_skip_if_exists ( &cursor, ASN1_EXPLICIT_TAG ( 1 ) );
426
427         /* Enter signerInfos */
428         asn1_enter ( &cursor, ASN1_SET );
429
430         /* Add each signerInfo.  Errors are handled by ensuring that
431          * cms_put() will always be able to free any allocated memory.
432          */
433         while ( cursor.len ) {
434
435                 /* Allocate signer information block */
436                 info = zalloc ( sizeof ( *info ) );
437                 if ( ! info )
438                         return -ENOMEM;
439                 list_add ( &info->list, &sig->info );
440
441                 /* Allocate certificate chain */
442                 info->chain = x509_alloc_chain();
443                 if ( ! info->chain )
444                         return -ENOMEM;
445
446                 /* Parse signerInfo */
447                 if ( ( rc = cms_parse_signer_info ( sig, info,
448                                                     &cursor ) ) != 0 )
449                         return rc;
450                 asn1_skip_any ( &cursor );
451         }
452
453         return 0;
454 }
455
456 /**
457  * Free CMS signature
458  *
459  * @v refcnt            Reference count
460  */
461 static void cms_free ( struct refcnt *refcnt ) {
462         struct cms_signature *sig =
463                 container_of ( refcnt, struct cms_signature, refcnt );
464         struct cms_signer_info *info;
465         struct cms_signer_info *tmp;
466
467         list_for_each_entry_safe ( info, tmp, &sig->info, list ) {
468                 list_del ( &info->list );
469                 x509_chain_put ( info->chain );
470                 free ( info->signature );
471                 free ( info );
472         }
473         x509_chain_put ( sig->certificates );
474         free ( sig );
475 }
476
477 /**
478  * Create CMS signature
479  *
480  * @v data              Raw signature data
481  * @v len               Length of raw data
482  * @ret sig             CMS signature
483  * @ret rc              Return status code
484  *
485  * On success, the caller holds a reference to the CMS signature, and
486  * is responsible for ultimately calling cms_put().
487  */
488 int cms_signature ( const void *data, size_t len, struct cms_signature **sig ) {
489         struct asn1_cursor cursor;
490         int rc;
491
492         /* Allocate and initialise signature */
493         *sig = zalloc ( sizeof ( **sig ) );
494         if ( ! *sig ) {
495                 rc = -ENOMEM;
496                 goto err_alloc;
497         }
498         ref_init ( &(*sig)->refcnt, cms_free );
499         INIT_LIST_HEAD ( &(*sig)->info );
500
501         /* Allocate certificate list */
502         (*sig)->certificates = x509_alloc_chain();
503         if ( ! (*sig)->certificates ) {
504                 rc = -ENOMEM;
505                 goto err_alloc_chain;
506         }
507
508         /* Initialise cursor */
509         cursor.data = data;
510         cursor.len = len;
511         asn1_shrink_any ( &cursor );
512
513         /* Parse signature */
514         if ( ( rc = cms_parse ( *sig, &cursor ) ) != 0 )
515                 goto err_parse;
516
517         return 0;
518
519  err_parse:
520  err_alloc_chain:
521         cms_put ( *sig );
522  err_alloc:
523         return rc;
524 }
525
526 /**
527  * Calculate digest of CMS-signed data
528  *
529  * @v sig               CMS signature
530  * @v info              Signer information
531  * @v data              Signed data
532  * @v len               Length of signed data
533  * @v out               Digest output
534  */
535 static void cms_digest ( struct cms_signature *sig,
536                          struct cms_signer_info *info,
537                          userptr_t data, size_t len, void *out ) {
538         struct digest_algorithm *digest = info->digest;
539         uint8_t ctx[ digest->ctxsize ];
540         uint8_t block[ digest->blocksize ];
541         size_t offset = 0;
542         size_t frag_len;
543
544         /* Initialise digest */
545         digest_init ( digest, ctx );
546
547         /* Process data one block at a time */
548         while ( len ) {
549                 frag_len = len;
550                 if ( frag_len > sizeof ( block ) )
551                         frag_len = sizeof ( block );
552                 copy_from_user ( block, data, offset, frag_len );
553                 digest_update ( digest, ctx, block, frag_len );
554                 offset += frag_len;
555                 len -= frag_len;
556         }
557
558         /* Finalise digest */
559         digest_final ( digest, ctx, out );
560
561         DBGC ( sig, "CMS %p/%p digest value:\n", sig, info );
562         DBGC_HDA ( sig, 0, out, digest->digestsize );
563 }
564
565 /**
566  * Verify digest of CMS-signed data
567  *
568  * @v sig               CMS signature
569  * @v info              Signer information
570  * @v cert              Corresponding certificate
571  * @v data              Signed data
572  * @v len               Length of signed data
573  * @ret rc              Return status code
574  */
575 static int cms_verify_digest ( struct cms_signature *sig,
576                                struct cms_signer_info *info,
577                                struct x509_certificate *cert,
578                                userptr_t data, size_t len ) {
579         struct digest_algorithm *digest = info->digest;
580         struct pubkey_algorithm *pubkey = info->pubkey;
581         struct x509_public_key *public_key = &cert->subject.public_key;
582         uint8_t digest_out[ digest->digestsize ];
583         uint8_t ctx[ pubkey->ctxsize ];
584         int rc;
585
586         /* Generate digest */
587         cms_digest ( sig, info, data, len, digest_out );
588
589         /* Initialise public-key algorithm */
590         if ( ( rc = pubkey_init ( pubkey, ctx, public_key->raw.data,
591                                   public_key->raw.len ) ) != 0 ) {
592                 DBGC ( sig, "CMS %p/%p could not initialise public key: %s\n",
593                        sig, info, strerror ( rc ) );
594                 goto err_init;
595         }
596
597         /* Verify digest */
598         if ( ( rc = pubkey_verify ( pubkey, ctx, digest, digest_out,
599                                     info->signature,
600                                     info->signature_len ) ) != 0 ) {
601                 DBGC ( sig, "CMS %p/%p signature verification failed: %s\n",
602                        sig, info, strerror ( rc ) );
603                 goto err_verify;
604         }
605
606  err_verify:
607         pubkey_final ( pubkey, ctx );
608  err_init:
609         return rc;
610 }
611
612 /**
613  * Verify CMS signature signer information
614  *
615  * @v sig               CMS signature
616  * @v info              Signer information
617  * @v data              Signed data
618  * @v len               Length of signed data
619  * @v time              Time at which to validate certificates
620  * @v store             Certificate store, or NULL to use default
621  * @v root              Root certificate list, or NULL to use default
622  * @ret rc              Return status code
623  */
624 static int cms_verify_signer_info ( struct cms_signature *sig,
625                                     struct cms_signer_info *info,
626                                     userptr_t data, size_t len,
627                                     time_t time, struct x509_chain *store,
628                                     struct x509_root *root ) {
629         struct x509_certificate *cert;
630         int rc;
631
632         /* Validate certificate chain */
633         if ( ( rc = x509_validate_chain ( info->chain, time, store,
634                                           root ) ) != 0 ) {
635                 DBGC ( sig, "CMS %p/%p could not validate chain: %s\n",
636                        sig, info, strerror ( rc ) );
637                 return rc;
638         }
639
640         /* Extract code-signing certificate */
641         cert = x509_first ( info->chain );
642         assert ( cert != NULL );
643
644         /* Check that certificate can create digital signatures */
645         if ( ! ( cert->extensions.usage.bits & X509_DIGITAL_SIGNATURE ) ) {
646                 DBGC ( sig, "CMS %p/%p certificate cannot create signatures\n",
647                        sig, info );
648                 return -EACCES_NON_SIGNING;
649         }
650
651         /* Check that certificate can sign code */
652         if ( ! ( cert->extensions.ext_usage.bits & X509_CODE_SIGNING ) ) {
653                 DBGC ( sig, "CMS %p/%p certificate is not code-signing\n",
654                        sig, info );
655                 return -EACCES_NON_CODE_SIGNING;
656         }
657
658         /* Verify digest */
659         if ( ( rc = cms_verify_digest ( sig, info, cert, data, len ) ) != 0 )
660                 return rc;
661
662         return 0;
663 }
664
665 /**
666  * Verify CMS signature
667  *
668  * @v sig               CMS signature
669  * @v data              Signed data
670  * @v len               Length of signed data
671  * @v name              Required common name, or NULL to check all signatures
672  * @v time              Time at which to validate certificates
673  * @v store             Certificate store, or NULL to use default
674  * @v root              Root certificate list, or NULL to use default
675  * @ret rc              Return status code
676  */
677 int cms_verify ( struct cms_signature *sig, userptr_t data, size_t len,
678                  const char *name, time_t time, struct x509_chain *store,
679                  struct x509_root *root ) {
680         struct cms_signer_info *info;
681         struct x509_certificate *cert;
682         int count = 0;
683         int rc;
684
685         /* Verify using all signerInfos */
686         list_for_each_entry ( info, &sig->info, list ) {
687                 cert = x509_first ( info->chain );
688                 if ( name && ( x509_check_name ( cert, name ) != 0 ) )
689                         continue;
690                 if ( ( rc = cms_verify_signer_info ( sig, info, data, len, time,
691                                                      store, root ) ) != 0 )
692                         return rc;
693                 count++;
694         }
695
696         /* Check that we have verified at least one signature */
697         if ( count == 0 ) {
698                 if ( name ) {
699                         DBGC ( sig, "CMS %p had no signatures matching name "
700                                "%s\n", sig, name );
701                         return -EACCES_WRONG_NAME;
702                 } else {
703                         DBGC ( sig, "CMS %p had no signatures\n", sig );
704                         return -EACCES_NO_SIGNATURES;
705                 }
706         }
707
708         return 0;
709 }