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

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