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

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