2 * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
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.
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.
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
19 * You can also choose to distribute this program under the terms of
20 * the Unmodified Binary Distribution Licence (as given in the file
21 * COPYING.UBDL), provided that you have satisfied its requirements.
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
28 * Cryptographic Message Syntax (PKCS #7)
30 * The format of CMS messages is defined in RFC 5652.
38 #include <ipxe/asn1.h>
39 #include <ipxe/x509.h>
40 #include <ipxe/malloc.h>
41 #include <ipxe/uaccess.h>
44 /* Disambiguate the various error causes */
45 #define EACCES_NON_SIGNING \
46 __einfo_error ( EINFO_EACCES_NON_SIGNING )
47 #define EINFO_EACCES_NON_SIGNING \
48 __einfo_uniqify ( EINFO_EACCES, 0x01, "Not a signing certificate" )
49 #define EACCES_NON_CODE_SIGNING \
50 __einfo_error ( EINFO_EACCES_NON_CODE_SIGNING )
51 #define EINFO_EACCES_NON_CODE_SIGNING \
52 __einfo_uniqify ( EINFO_EACCES, 0x02, "Not a code-signing certificate" )
53 #define EACCES_WRONG_NAME \
54 __einfo_error ( EINFO_EACCES_WRONG_NAME )
55 #define EINFO_EACCES_WRONG_NAME \
56 __einfo_uniqify ( EINFO_EACCES, 0x04, "Incorrect certificate name" )
57 #define EACCES_NO_SIGNATURES \
58 __einfo_error ( EINFO_EACCES_NO_SIGNATURES )
59 #define EINFO_EACCES_NO_SIGNATURES \
60 __einfo_uniqify ( EINFO_EACCES, 0x05, "No signatures present" )
61 #define EINVAL_DIGEST \
62 __einfo_error ( EINFO_EINVAL_DIGEST )
63 #define EINFO_EINVAL_DIGEST \
64 __einfo_uniqify ( EINFO_EINVAL, 0x01, "Not a digest algorithm" )
65 #define EINVAL_PUBKEY \
66 __einfo_error ( EINFO_EINVAL_PUBKEY )
67 #define EINFO_EINVAL_PUBKEY \
68 __einfo_uniqify ( EINFO_EINVAL, 0x02, "Not a public-key algorithm" )
69 #define ENOTSUP_SIGNEDDATA \
70 __einfo_error ( EINFO_ENOTSUP_SIGNEDDATA )
71 #define EINFO_ENOTSUP_SIGNEDDATA \
72 __einfo_uniqify ( EINFO_ENOTSUP, 0x01, "Not a digital signature" )
74 /** "pkcs7-signedData" object identifier */
75 static uint8_t oid_signeddata[] = { ASN1_OID_SIGNEDDATA };
77 /** "pkcs7-signedData" object identifier cursor */
78 static struct asn1_cursor oid_signeddata_cursor =
79 ASN1_OID_CURSOR ( oid_signeddata );
82 * Parse CMS signature content type
84 * @v sig CMS signature
86 * @ret rc Return status code
88 static int cms_parse_content_type ( struct cms_signature *sig,
89 const struct asn1_cursor *raw ) {
90 struct asn1_cursor cursor;
92 /* Enter contentType */
93 memcpy ( &cursor, raw, sizeof ( cursor ) );
94 asn1_enter ( &cursor, ASN1_OID );
96 /* Check OID is pkcs7-signedData */
97 if ( asn1_compare ( &cursor, &oid_signeddata_cursor ) != 0 ) {
98 DBGC ( sig, "CMS %p does not contain signedData:\n", sig );
99 DBGC_HDA ( sig, 0, raw->data, raw->len );
100 return -ENOTSUP_SIGNEDDATA;
103 DBGC ( sig, "CMS %p contains signedData\n", sig );
108 * Parse CMS signature certificate list
110 * @v sig CMS signature
111 * @v raw ASN.1 cursor
112 * @ret rc Return status code
114 static int cms_parse_certificates ( struct cms_signature *sig,
115 const struct asn1_cursor *raw ) {
116 struct asn1_cursor cursor;
117 struct x509_certificate *cert;
120 /* Enter certificates */
121 memcpy ( &cursor, raw, sizeof ( cursor ) );
122 asn1_enter ( &cursor, ASN1_EXPLICIT_TAG ( 0 ) );
124 /* Add each certificate */
125 while ( cursor.len ) {
127 /* Add certificate to chain */
128 if ( ( rc = x509_append_raw ( sig->certificates, cursor.data,
129 cursor.len ) ) != 0 ) {
130 DBGC ( sig, "CMS %p could not append certificate: %s\n",
131 sig, strerror ( rc) );
132 DBGC_HDA ( sig, 0, cursor.data, cursor.len );
135 cert = x509_last ( sig->certificates );
136 DBGC ( sig, "CMS %p found certificate %s\n",
137 sig, x509_name ( cert ) );
139 /* Move to next certificate */
140 asn1_skip_any ( &cursor );
147 * Identify CMS signature certificate by issuer and serial number
149 * @v sig CMS signature
151 * @v serial Serial number
152 * @ret cert X.509 certificate, or NULL if not found
154 static struct x509_certificate *
155 cms_find_issuer_serial ( struct cms_signature *sig,
156 const struct asn1_cursor *issuer,
157 const struct asn1_cursor *serial ) {
158 struct x509_link *link;
159 struct x509_certificate *cert;
161 /* Scan through certificate list */
162 list_for_each_entry ( link, &sig->certificates->links, list ) {
164 /* Check issuer and serial number */
166 if ( ( asn1_compare ( issuer, &cert->issuer.raw ) == 0 ) &&
167 ( asn1_compare ( serial, &cert->serial.raw ) == 0 ) )
175 * Parse CMS signature signer identifier
177 * @v sig CMS signature
178 * @v info Signer information to fill in
179 * @v raw ASN.1 cursor
180 * @ret rc Return status code
182 static int cms_parse_signer_identifier ( struct cms_signature *sig,
183 struct cms_signer_info *info,
184 const struct asn1_cursor *raw ) {
185 struct asn1_cursor cursor;
186 struct asn1_cursor serial;
187 struct asn1_cursor issuer;
188 struct x509_certificate *cert;
191 /* Enter issuerAndSerialNumber */
192 memcpy ( &cursor, raw, sizeof ( cursor ) );
193 asn1_enter ( &cursor, ASN1_SEQUENCE );
195 /* Identify issuer */
196 memcpy ( &issuer, &cursor, sizeof ( issuer ) );
197 if ( ( rc = asn1_shrink ( &issuer, ASN1_SEQUENCE ) ) != 0 ) {
198 DBGC ( sig, "CMS %p/%p could not locate issuer: %s\n",
199 sig, info, strerror ( rc ) );
200 DBGC_HDA ( sig, 0, raw->data, raw->len );
203 DBGC ( sig, "CMS %p/%p issuer is:\n", sig, info );
204 DBGC_HDA ( sig, 0, issuer.data, issuer.len );
205 asn1_skip_any ( &cursor );
207 /* Identify serialNumber */
208 memcpy ( &serial, &cursor, sizeof ( serial ) );
209 if ( ( rc = asn1_shrink ( &serial, ASN1_INTEGER ) ) != 0 ) {
210 DBGC ( sig, "CMS %p/%p could not locate serialNumber: %s\n",
211 sig, info, strerror ( rc ) );
212 DBGC_HDA ( sig, 0, raw->data, raw->len );
215 DBGC ( sig, "CMS %p/%p serial number is:\n", sig, info );
216 DBGC_HDA ( sig, 0, serial.data, serial.len );
218 /* Identify certificate */
219 cert = cms_find_issuer_serial ( sig, &issuer, &serial );
221 DBGC ( sig, "CMS %p/%p could not identify signer's "
222 "certificate\n", sig, info );
226 /* Append certificate to chain */
227 if ( ( rc = x509_append ( info->chain, cert ) ) != 0 ) {
228 DBGC ( sig, "CMS %p/%p could not append certificate: %s\n",
229 sig, info, strerror ( rc ) );
233 /* Append remaining certificates to chain */
234 if ( ( rc = x509_auto_append ( info->chain,
235 sig->certificates ) ) != 0 ) {
236 DBGC ( sig, "CMS %p/%p could not append certificates: %s\n",
237 sig, info, strerror ( rc ) );
245 * Parse CMS signature digest algorithm
247 * @v sig CMS signature
248 * @v info Signer information to fill in
249 * @v raw ASN.1 cursor
250 * @ret rc Return status code
252 static int cms_parse_digest_algorithm ( struct cms_signature *sig,
253 struct cms_signer_info *info,
254 const struct asn1_cursor *raw ) {
255 struct asn1_algorithm *algorithm;
258 /* Identify algorithm */
259 if ( ( rc = asn1_digest_algorithm ( raw, &algorithm ) ) != 0 ) {
260 DBGC ( sig, "CMS %p/%p could not identify digest algorithm: "
261 "%s\n", sig, info, strerror ( rc ) );
262 DBGC_HDA ( sig, 0, raw->data, raw->len );
266 /* Record digest algorithm */
267 info->digest = algorithm->digest;
268 DBGC ( sig, "CMS %p/%p digest algorithm is %s\n",
269 sig, info, algorithm->name );
275 * Parse CMS signature algorithm
277 * @v sig CMS signature
278 * @v info Signer information to fill in
279 * @v raw ASN.1 cursor
280 * @ret rc Return status code
282 static int cms_parse_signature_algorithm ( struct cms_signature *sig,
283 struct cms_signer_info *info,
284 const struct asn1_cursor *raw ) {
285 struct asn1_algorithm *algorithm;
288 /* Identify algorithm */
289 if ( ( rc = asn1_pubkey_algorithm ( raw, &algorithm ) ) != 0 ) {
290 DBGC ( sig, "CMS %p/%p could not identify public-key "
291 "algorithm: %s\n", sig, info, strerror ( rc ) );
292 DBGC_HDA ( sig, 0, raw->data, raw->len );
296 /* Record signature algorithm */
297 info->pubkey = algorithm->pubkey;
298 DBGC ( sig, "CMS %p/%p public-key algorithm is %s\n",
299 sig, info, algorithm->name );
305 * Parse CMS signature value
307 * @v sig CMS signature
308 * @v info Signer information to fill in
309 * @v raw ASN.1 cursor
310 * @ret rc Return status code
312 static int cms_parse_signature_value ( struct cms_signature *sig,
313 struct cms_signer_info *info,
314 const struct asn1_cursor *raw ) {
315 struct asn1_cursor cursor;
318 /* Enter signature */
319 memcpy ( &cursor, raw, sizeof ( cursor ) );
320 if ( ( rc = asn1_enter ( &cursor, ASN1_OCTET_STRING ) ) != 0 ) {
321 DBGC ( sig, "CMS %p/%p could not locate signature:\n",
323 DBGC_HDA ( sig, 0, raw->data, raw->len );
327 /* Record signature */
328 info->signature_len = cursor.len;
329 info->signature = malloc ( info->signature_len );
330 if ( ! info->signature )
332 memcpy ( info->signature, cursor.data, info->signature_len );
333 DBGC ( sig, "CMS %p/%p signature value is:\n", sig, info );
334 DBGC_HDA ( sig, 0, info->signature, info->signature_len );
340 * Parse CMS signature signer information
342 * @v sig CMS signature
343 * @v info Signer information to fill in
344 * @v raw ASN.1 cursor
345 * @ret rc Return status code
347 static int cms_parse_signer_info ( struct cms_signature *sig,
348 struct cms_signer_info *info,
349 const struct asn1_cursor *raw ) {
350 struct asn1_cursor cursor;
353 /* Enter signerInfo */
354 memcpy ( &cursor, raw, sizeof ( cursor ) );
355 asn1_enter ( &cursor, ASN1_SEQUENCE );
358 asn1_skip ( &cursor, ASN1_INTEGER );
361 if ( ( rc = cms_parse_signer_identifier ( sig, info, &cursor ) ) != 0 )
363 asn1_skip_any ( &cursor );
365 /* Parse digestAlgorithm */
366 if ( ( rc = cms_parse_digest_algorithm ( sig, info, &cursor ) ) != 0 )
368 asn1_skip_any ( &cursor );
370 /* Skip signedAttrs, if present */
371 asn1_skip_if_exists ( &cursor, ASN1_EXPLICIT_TAG ( 0 ) );
373 /* Parse signatureAlgorithm */
374 if ( ( rc = cms_parse_signature_algorithm ( sig, info, &cursor ) ) != 0)
376 asn1_skip_any ( &cursor );
378 /* Parse signature */
379 if ( ( rc = cms_parse_signature_value ( sig, info, &cursor ) ) != 0 )
386 * Parse CMS signature from ASN.1 data
388 * @v sig CMS signature
389 * @v raw ASN.1 cursor
390 * @ret rc Return status code
392 static int cms_parse ( struct cms_signature *sig,
393 const struct asn1_cursor *raw ) {
394 struct asn1_cursor cursor;
395 struct cms_signer_info *info;
398 /* Enter contentInfo */
399 memcpy ( &cursor, raw, sizeof ( cursor ) );
400 asn1_enter ( &cursor, ASN1_SEQUENCE );
402 /* Parse contentType */
404 if ( ( rc = cms_parse_content_type ( sig, &cursor ) ) != 0 )
406 asn1_skip_any ( &cursor );
409 asn1_enter ( &cursor, ASN1_EXPLICIT_TAG ( 0 ) );
411 /* Enter signedData */
412 asn1_enter ( &cursor, ASN1_SEQUENCE );
415 asn1_skip ( &cursor, ASN1_INTEGER );
417 /* Skip digestAlgorithms */
418 asn1_skip ( &cursor, ASN1_SET );
420 /* Skip encapContentInfo */
421 asn1_skip ( &cursor, ASN1_SEQUENCE );
423 /* Parse certificates */
424 if ( ( rc = cms_parse_certificates ( sig, &cursor ) ) != 0 )
426 asn1_skip_any ( &cursor );
428 /* Skip crls, if present */
429 asn1_skip_if_exists ( &cursor, ASN1_EXPLICIT_TAG ( 1 ) );
431 /* Enter signerInfos */
432 asn1_enter ( &cursor, ASN1_SET );
434 /* Add each signerInfo. Errors are handled by ensuring that
435 * cms_put() will always be able to free any allocated memory.
437 while ( cursor.len ) {
439 /* Allocate signer information block */
440 info = zalloc ( sizeof ( *info ) );
443 list_add ( &info->list, &sig->info );
445 /* Allocate certificate chain */
446 info->chain = x509_alloc_chain();
450 /* Parse signerInfo */
451 if ( ( rc = cms_parse_signer_info ( sig, info,
454 asn1_skip_any ( &cursor );
463 * @v refcnt Reference count
465 static void cms_free ( struct refcnt *refcnt ) {
466 struct cms_signature *sig =
467 container_of ( refcnt, struct cms_signature, refcnt );
468 struct cms_signer_info *info;
469 struct cms_signer_info *tmp;
471 list_for_each_entry_safe ( info, tmp, &sig->info, list ) {
472 list_del ( &info->list );
473 x509_chain_put ( info->chain );
474 free ( info->signature );
477 x509_chain_put ( sig->certificates );
482 * Create CMS signature
484 * @v data Raw signature data
485 * @v len Length of raw data
486 * @ret sig CMS signature
487 * @ret rc Return status code
489 * On success, the caller holds a reference to the CMS signature, and
490 * is responsible for ultimately calling cms_put().
492 int cms_signature ( const void *data, size_t len, struct cms_signature **sig ) {
493 struct asn1_cursor cursor;
496 /* Allocate and initialise signature */
497 *sig = zalloc ( sizeof ( **sig ) );
502 ref_init ( &(*sig)->refcnt, cms_free );
503 INIT_LIST_HEAD ( &(*sig)->info );
505 /* Allocate certificate list */
506 (*sig)->certificates = x509_alloc_chain();
507 if ( ! (*sig)->certificates ) {
509 goto err_alloc_chain;
512 /* Initialise cursor */
515 asn1_shrink_any ( &cursor );
517 /* Parse signature */
518 if ( ( rc = cms_parse ( *sig, &cursor ) ) != 0 )
531 * Calculate digest of CMS-signed data
533 * @v sig CMS signature
534 * @v info Signer information
535 * @v data Signed data
536 * @v len Length of signed data
537 * @v out Digest output
539 static void cms_digest ( struct cms_signature *sig,
540 struct cms_signer_info *info,
541 userptr_t data, size_t len, void *out ) {
542 struct digest_algorithm *digest = info->digest;
543 uint8_t ctx[ digest->ctxsize ];
544 uint8_t block[ digest->blocksize ];
548 /* Initialise digest */
549 digest_init ( digest, ctx );
551 /* Process data one block at a time */
554 if ( frag_len > sizeof ( block ) )
555 frag_len = sizeof ( block );
556 copy_from_user ( block, data, offset, frag_len );
557 digest_update ( digest, ctx, block, frag_len );
562 /* Finalise digest */
563 digest_final ( digest, ctx, out );
565 DBGC ( sig, "CMS %p/%p digest value:\n", sig, info );
566 DBGC_HDA ( sig, 0, out, digest->digestsize );
570 * Verify digest of CMS-signed data
572 * @v sig CMS signature
573 * @v info Signer information
574 * @v cert Corresponding certificate
575 * @v data Signed data
576 * @v len Length of signed data
577 * @ret rc Return status code
579 static int cms_verify_digest ( struct cms_signature *sig,
580 struct cms_signer_info *info,
581 struct x509_certificate *cert,
582 userptr_t data, size_t len ) {
583 struct digest_algorithm *digest = info->digest;
584 struct pubkey_algorithm *pubkey = info->pubkey;
585 struct x509_public_key *public_key = &cert->subject.public_key;
586 uint8_t digest_out[ digest->digestsize ];
587 uint8_t ctx[ pubkey->ctxsize ];
590 /* Generate digest */
591 cms_digest ( sig, info, data, len, digest_out );
593 /* Initialise public-key algorithm */
594 if ( ( rc = pubkey_init ( pubkey, ctx, public_key->raw.data,
595 public_key->raw.len ) ) != 0 ) {
596 DBGC ( sig, "CMS %p/%p could not initialise public key: %s\n",
597 sig, info, strerror ( rc ) );
602 if ( ( rc = pubkey_verify ( pubkey, ctx, digest, digest_out,
604 info->signature_len ) ) != 0 ) {
605 DBGC ( sig, "CMS %p/%p signature verification failed: %s\n",
606 sig, info, strerror ( rc ) );
611 pubkey_final ( pubkey, ctx );
617 * Verify CMS signature signer information
619 * @v sig CMS signature
620 * @v info Signer information
621 * @v data Signed data
622 * @v len Length of signed data
623 * @v time Time at which to validate certificates
624 * @v store Certificate store, or NULL to use default
625 * @v root Root certificate list, or NULL to use default
626 * @ret rc Return status code
628 static int cms_verify_signer_info ( struct cms_signature *sig,
629 struct cms_signer_info *info,
630 userptr_t data, size_t len,
631 time_t time, struct x509_chain *store,
632 struct x509_root *root ) {
633 struct x509_certificate *cert;
636 /* Validate certificate chain */
637 if ( ( rc = x509_validate_chain ( info->chain, time, store,
639 DBGC ( sig, "CMS %p/%p could not validate chain: %s\n",
640 sig, info, strerror ( rc ) );
644 /* Extract code-signing certificate */
645 cert = x509_first ( info->chain );
646 assert ( cert != NULL );
648 /* Check that certificate can create digital signatures */
649 if ( ! ( cert->extensions.usage.bits & X509_DIGITAL_SIGNATURE ) ) {
650 DBGC ( sig, "CMS %p/%p certificate cannot create signatures\n",
652 return -EACCES_NON_SIGNING;
655 /* Check that certificate can sign code */
656 if ( ! ( cert->extensions.ext_usage.bits & X509_CODE_SIGNING ) ) {
657 DBGC ( sig, "CMS %p/%p certificate is not code-signing\n",
659 return -EACCES_NON_CODE_SIGNING;
663 if ( ( rc = cms_verify_digest ( sig, info, cert, data, len ) ) != 0 )
670 * Verify CMS signature
672 * @v sig CMS signature
673 * @v data Signed data
674 * @v len Length of signed data
675 * @v name Required common name, or NULL to check all signatures
676 * @v time Time at which to validate certificates
677 * @v store Certificate store, or NULL to use default
678 * @v root Root certificate list, or NULL to use default
679 * @ret rc Return status code
681 int cms_verify ( struct cms_signature *sig, userptr_t data, size_t len,
682 const char *name, time_t time, struct x509_chain *store,
683 struct x509_root *root ) {
684 struct cms_signer_info *info;
685 struct x509_certificate *cert;
689 /* Verify using all signerInfos */
690 list_for_each_entry ( info, &sig->info, list ) {
691 cert = x509_first ( info->chain );
692 if ( name && ( x509_check_name ( cert, name ) != 0 ) )
694 if ( ( rc = cms_verify_signer_info ( sig, info, data, len, time,
695 store, root ) ) != 0 )
700 /* Check that we have verified at least one signature */
703 DBGC ( sig, "CMS %p had no signatures matching name "
705 return -EACCES_WRONG_NAME;
707 DBGC ( sig, "CMS %p had no signatures\n", sig );
708 return -EACCES_NO_SIGNATURES;