You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cms.c 19KB

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