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.

image_trust_cmd.c 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 <stdint.h>
  21. #include <stdio.h>
  22. #include <getopt.h>
  23. #include <ipxe/image.h>
  24. #include <ipxe/command.h>
  25. #include <ipxe/parseopt.h>
  26. #include <usr/imgmgmt.h>
  27. #include <usr/imgtrust.h>
  28. /** @file
  29. *
  30. * Image trust management commands
  31. *
  32. */
  33. /** "imgtrust" options */
  34. struct imgtrust_options {
  35. /** Allow trusted images */
  36. int allow;
  37. /** Make trust requirement permanent */
  38. int permanent;
  39. };
  40. /** "imgtrust" option list */
  41. static struct option_descriptor imgtrust_opts[] = {
  42. OPTION_DESC ( "allow", 'a', no_argument,
  43. struct imgtrust_options, allow, parse_flag ),
  44. OPTION_DESC ( "permanent", 'p', no_argument,
  45. struct imgtrust_options, permanent, parse_flag ),
  46. };
  47. /** "imgtrust" command descriptor */
  48. static struct command_descriptor imgtrust_cmd =
  49. COMMAND_DESC ( struct imgtrust_options, imgtrust_opts, 0, 0, NULL );
  50. /**
  51. * The "imgtrust" command
  52. *
  53. * @v argc Argument count
  54. * @v argv Argument list
  55. * @ret rc Return status code
  56. */
  57. static int imgtrust_exec ( int argc, char **argv ) {
  58. struct imgtrust_options opts;
  59. int rc;
  60. /* Parse options */
  61. if ( ( rc = parse_options ( argc, argv, &imgtrust_cmd, &opts ) ) != 0 )
  62. return rc;
  63. /* Set trust requirement */
  64. if ( ( rc = image_set_trust ( ( ! opts.allow ),
  65. opts.permanent ) ) != 0 ) {
  66. printf ( "Could not set image trust requirement: %s\n",
  67. strerror ( rc ) );
  68. return rc;
  69. }
  70. return 0;
  71. }
  72. /** "imgverify" options */
  73. struct imgverify_options {
  74. /** Required signer common name */
  75. char *signer;
  76. /** Keep signature after verification */
  77. int keep;
  78. };
  79. /** "imgverify" option list */
  80. static struct option_descriptor imgverify_opts[] = {
  81. OPTION_DESC ( "signer", 's', required_argument,
  82. struct imgverify_options, signer, parse_string ),
  83. OPTION_DESC ( "keep", 'k', no_argument,
  84. struct imgverify_options, keep, parse_flag ),
  85. };
  86. /** "imgverify" command descriptor */
  87. static struct command_descriptor imgverify_cmd =
  88. COMMAND_DESC ( struct imgverify_options, imgverify_opts, 2, 2,
  89. "<uri|image> <signature uri|image>" );
  90. /**
  91. * The "imgverify" command
  92. *
  93. * @v argc Argument count
  94. * @v argv Argument list
  95. * @ret rc Return status code
  96. */
  97. static int imgverify_exec ( int argc, char **argv ) {
  98. struct imgverify_options opts;
  99. const char *image_name_uri;
  100. const char *signature_name_uri;
  101. struct image *image;
  102. struct image *signature;
  103. int rc;
  104. /* Parse options */
  105. if ( ( rc = parse_options ( argc, argv, &imgverify_cmd, &opts ) ) != 0 )
  106. return rc;
  107. /* Parse image name/URI string */
  108. image_name_uri = argv[optind];
  109. /* Parse signature name/URI string */
  110. signature_name_uri = argv[ optind + 1 ];
  111. /* Acquire the image */
  112. if ( ( rc = imgacquire ( image_name_uri, &image ) ) != 0 )
  113. goto err_acquire_image;
  114. /* Acquire the signature image */
  115. if ( ( rc = imgacquire ( signature_name_uri, &signature ) ) != 0 )
  116. goto err_acquire_signature;
  117. /* Verify image */
  118. if ( ( rc = imgverify ( image, signature, opts.signer ) ) != 0 ) {
  119. printf ( "Could not verify: %s\n", strerror ( rc ) );
  120. goto err_verify;
  121. }
  122. /* Success */
  123. rc = 0;
  124. err_verify:
  125. /* Discard signature unless --keep was specified */
  126. if ( ! opts.keep )
  127. unregister_image ( signature );
  128. err_acquire_signature:
  129. err_acquire_image:
  130. return rc;
  131. }
  132. /** Image trust management commands */
  133. struct command image_trust_commands[] __command = {
  134. {
  135. .name = "imgtrust",
  136. .exec = imgtrust_exec,
  137. },
  138. {
  139. .name = "imgverify",
  140. .exec = imgverify_exec,
  141. },
  142. };
  143. /* Drag in objects typically required for signature verification */
  144. REQUIRE_OBJECT ( rsa );
  145. REQUIRE_OBJECT ( md5 );
  146. REQUIRE_OBJECT ( sha1 );
  147. REQUIRE_OBJECT ( sha256 );