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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. /** Download timeout */
  79. unsigned long timeout;
  80. };
  81. /** "imgverify" option list */
  82. static struct option_descriptor imgverify_opts[] = {
  83. OPTION_DESC ( "signer", 's', required_argument,
  84. struct imgverify_options, signer, parse_string ),
  85. OPTION_DESC ( "keep", 'k', no_argument,
  86. struct imgverify_options, keep, parse_flag ),
  87. OPTION_DESC ( "timeout", 't', required_argument,
  88. struct imgverify_options, timeout, parse_timeout),
  89. };
  90. /** "imgverify" command descriptor */
  91. static struct command_descriptor imgverify_cmd =
  92. COMMAND_DESC ( struct imgverify_options, imgverify_opts, 2, 2,
  93. "<uri|image> <signature uri|image>" );
  94. /**
  95. * The "imgverify" command
  96. *
  97. * @v argc Argument count
  98. * @v argv Argument list
  99. * @ret rc Return status code
  100. */
  101. static int imgverify_exec ( int argc, char **argv ) {
  102. struct imgverify_options opts;
  103. const char *image_name_uri;
  104. const char *signature_name_uri;
  105. struct image *image;
  106. struct image *signature;
  107. int rc;
  108. /* Parse options */
  109. if ( ( rc = parse_options ( argc, argv, &imgverify_cmd, &opts ) ) != 0 )
  110. return rc;
  111. /* Parse image name/URI string */
  112. image_name_uri = argv[optind];
  113. /* Parse signature name/URI string */
  114. signature_name_uri = argv[ optind + 1 ];
  115. /* Acquire the image */
  116. if ( ( rc = imgacquire ( image_name_uri, opts.timeout, &image ) ) != 0 )
  117. goto err_acquire_image;
  118. /* Acquire the signature image */
  119. if ( ( rc = imgacquire ( signature_name_uri, opts.timeout,
  120. &signature ) ) != 0 )
  121. goto err_acquire_signature;
  122. /* Verify image */
  123. if ( ( rc = imgverify ( image, signature, opts.signer ) ) != 0 ) {
  124. printf ( "Could not verify: %s\n", strerror ( rc ) );
  125. goto err_verify;
  126. }
  127. /* Success */
  128. rc = 0;
  129. err_verify:
  130. /* Discard signature unless --keep was specified */
  131. if ( ! opts.keep )
  132. unregister_image ( signature );
  133. err_acquire_signature:
  134. err_acquire_image:
  135. return rc;
  136. }
  137. /** Image trust management commands */
  138. struct command image_trust_commands[] __command = {
  139. {
  140. .name = "imgtrust",
  141. .exec = imgtrust_exec,
  142. },
  143. {
  144. .name = "imgverify",
  145. .exec = imgverify_exec,
  146. },
  147. };
  148. /* Drag in objects typically required for signature verification */
  149. REQUIRE_OBJECT ( rsa );
  150. REQUIRE_OBJECT ( md5 );
  151. REQUIRE_OBJECT ( sha1 );
  152. REQUIRE_OBJECT ( sha256 );