Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

imgtrust.c 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 <syslog.h>
  23. #include <ipxe/uaccess.h>
  24. #include <ipxe/image.h>
  25. #include <ipxe/cms.h>
  26. #include <usr/imgtrust.h>
  27. /** @file
  28. *
  29. * Image trust management
  30. *
  31. */
  32. /**
  33. * Verify image using downloaded signature
  34. *
  35. * @v image Image to verify
  36. * @v signature Image containing signature
  37. * @v name Required common name, or NULL to allow any name
  38. * @ret rc Return status code
  39. */
  40. int imgverify ( struct image *image, struct image *signature,
  41. const char *name ) {
  42. size_t len;
  43. void *data;
  44. struct cms_signature sig;
  45. time_t now;
  46. int rc;
  47. /* Mark image as untrusted */
  48. image_untrust ( image );
  49. /* Copy signature to internal memory */
  50. len = signature->len;
  51. data = malloc ( len );
  52. if ( ! data ) {
  53. rc = -ENOMEM;
  54. goto err_alloc;
  55. }
  56. copy_from_user ( data, signature->data, 0, len );
  57. /* Parse signature */
  58. if ( ( rc = cms_parse ( &sig, data, len ) ) != 0 )
  59. goto err_parse;
  60. /* Use signature to verify image */
  61. now = time ( NULL );
  62. if ( ( rc = cms_verify ( &sig, image->data, image->len,
  63. name, now, NULL ) ) != 0 )
  64. goto err_verify;
  65. /* Mark image as trusted */
  66. image_trust ( image );
  67. syslog ( LOG_NOTICE, "Image \"%s\" signature OK\n", image->name );
  68. /* Free internal copy of signature */
  69. free ( data );
  70. return 0;
  71. err_verify:
  72. err_parse:
  73. free ( data );
  74. err_alloc:
  75. syslog ( LOG_ERR, "Image \"%s\" signature bad: %s\n",
  76. image->name, strerror ( rc ) );
  77. return rc;
  78. }