您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

imgtrust.c 2.7KB

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