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.

imgtrust.c 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. * 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.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. #include <stdlib.h>
  25. #include <errno.h>
  26. #include <time.h>
  27. #include <syslog.h>
  28. #include <ipxe/uaccess.h>
  29. #include <ipxe/image.h>
  30. #include <ipxe/cms.h>
  31. #include <ipxe/validator.h>
  32. #include <ipxe/monojob.h>
  33. #include <usr/imgtrust.h>
  34. /** @file
  35. *
  36. * Image trust management
  37. *
  38. */
  39. /**
  40. * Verify image using downloaded signature
  41. *
  42. * @v image Image to verify
  43. * @v signature Image containing signature
  44. * @v name Required common name, or NULL to allow any name
  45. * @ret rc Return status code
  46. */
  47. int imgverify ( struct image *image, struct image *signature,
  48. const char *name ) {
  49. struct asn1_cursor *data;
  50. struct cms_signature *sig;
  51. struct cms_signer_info *info;
  52. time_t now;
  53. int next;
  54. int rc;
  55. /* Mark image as untrusted */
  56. image_untrust ( image );
  57. /* Get raw signature data */
  58. next = image_asn1 ( signature, 0, &data );
  59. if ( next < 0 ) {
  60. rc = next;
  61. goto err_asn1;
  62. }
  63. /* Parse signature */
  64. if ( ( rc = cms_signature ( data->data, data->len, &sig ) ) != 0 )
  65. goto err_parse;
  66. /* Free raw signature data */
  67. free ( data );
  68. data = NULL;
  69. /* Complete all certificate chains */
  70. list_for_each_entry ( info, &sig->info, list ) {
  71. if ( ( rc = create_validator ( &monojob, info->chain ) ) != 0 )
  72. goto err_create_validator;
  73. if ( ( rc = monojob_wait ( NULL, 0 ) ) != 0 )
  74. goto err_validator_wait;
  75. }
  76. /* Use signature to verify image */
  77. now = time ( NULL );
  78. if ( ( rc = cms_verify ( sig, image->data, image->len,
  79. name, now, NULL, NULL ) ) != 0 )
  80. goto err_verify;
  81. /* Drop reference to signature */
  82. cms_put ( sig );
  83. sig = NULL;
  84. /* Mark image as trusted */
  85. image_trust ( image );
  86. syslog ( LOG_NOTICE, "Image \"%s\" signature OK\n", image->name );
  87. return 0;
  88. err_verify:
  89. err_validator_wait:
  90. err_create_validator:
  91. cms_put ( sig );
  92. err_parse:
  93. free ( data );
  94. err_asn1:
  95. syslog ( LOG_ERR, "Image \"%s\" signature bad: %s\n",
  96. image->name, strerror ( rc ) );
  97. return rc;
  98. }