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.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. FILE_LICENCE ( GPL2_OR_LATER );
  19. #include <stdlib.h>
  20. #include <errno.h>
  21. #include <time.h>
  22. #include <ipxe/uaccess.h>
  23. #include <ipxe/image.h>
  24. #include <ipxe/cms.h>
  25. #include <usr/imgtrust.h>
  26. /** @file
  27. *
  28. * Image trust management
  29. *
  30. */
  31. /**
  32. * Verify image using downloaded signature
  33. *
  34. * @v image Image to verify
  35. * @v signature Image containing signature
  36. * @v name Required common name, or NULL to allow any name
  37. * @ret rc Return status code
  38. */
  39. int imgverify ( struct image *image, struct image *signature,
  40. const char *name ) {
  41. size_t len;
  42. void *data;
  43. struct cms_signature sig;
  44. time_t now;
  45. int rc;
  46. /* Mark image as untrusted */
  47. image_untrust ( image );
  48. /* Copy signature to internal memory */
  49. len = signature->len;
  50. data = malloc ( len );
  51. if ( ! data ) {
  52. rc = -ENOMEM;
  53. goto err_alloc;
  54. }
  55. copy_from_user ( data, signature->data, 0, len );
  56. /* Parse signature */
  57. if ( ( rc = cms_parse ( &sig, data, len ) ) != 0 )
  58. goto err_parse;
  59. /* Use signature to verify image */
  60. now = time ( NULL );
  61. if ( ( rc = cms_verify ( &sig, image->data, image->len,
  62. name, now, NULL ) ) != 0 )
  63. goto err_verify;
  64. /* Mark image as trusted */
  65. image_trust ( image );
  66. err_verify:
  67. err_parse:
  68. free ( data );
  69. err_alloc:
  70. return rc;
  71. }