Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

image_trust_cmd.c 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. FILE_LICENCE ( GPL2_OR_LATER );
  19. #include <stdint.h>
  20. #include <stdio.h>
  21. #include <getopt.h>
  22. #include <ipxe/image.h>
  23. #include <ipxe/command.h>
  24. #include <ipxe/parseopt.h>
  25. #include <usr/imgmgmt.h>
  26. #include <usr/imgtrust.h>
  27. /** @file
  28. *
  29. * Image trust management commands
  30. *
  31. */
  32. /** "imgtrust" options */
  33. struct imgtrust_options {
  34. /** Allow trusted images */
  35. int allow;
  36. /** Make trust requirement permanent */
  37. int permanent;
  38. };
  39. /** "imgtrust" option list */
  40. static struct option_descriptor imgtrust_opts[] = {
  41. OPTION_DESC ( "allow", 'a', no_argument,
  42. struct imgtrust_options, allow, parse_flag ),
  43. OPTION_DESC ( "permanent", 'p', no_argument,
  44. struct imgtrust_options, permanent, parse_flag ),
  45. };
  46. /** "imgtrust" command descriptor */
  47. static struct command_descriptor imgtrust_cmd =
  48. COMMAND_DESC ( struct imgtrust_options, imgtrust_opts, 0, 0,
  49. "[--allow] [--permanent]" );
  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. const 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. "[--signer <signer>] [--keep] <uri|image> "
  90. "<signature uri|image>" );
  91. /**
  92. * The "imgverify" command
  93. *
  94. * @v argc Argument count
  95. * @v argv Argument list
  96. * @ret rc Return status code
  97. */
  98. static int imgverify_exec ( int argc, char **argv ) {
  99. struct imgverify_options opts;
  100. const char *image_name_uri;
  101. const char *signature_name_uri;
  102. struct image *image;
  103. struct image *signature;
  104. int rc;
  105. /* Parse options */
  106. if ( ( rc = parse_options ( argc, argv, &imgverify_cmd, &opts ) ) != 0 )
  107. return rc;
  108. /* Parse image name/URI string */
  109. image_name_uri = argv[optind];
  110. /* Parse signature name/URI string */
  111. signature_name_uri = argv[ optind + 1 ];
  112. /* Acquire the image */
  113. if ( ( rc = imgacquire ( image_name_uri, &image ) ) != 0 )
  114. goto err_acquire_image;
  115. /* Acquire the signature image */
  116. if ( ( rc = imgacquire ( signature_name_uri, &signature ) ) != 0 )
  117. goto err_acquire_signature;
  118. /* Verify image */
  119. if ( ( rc = imgverify ( image, signature, opts.signer ) ) != 0 ) {
  120. printf ( "Could not verify: %s\n", strerror ( rc ) );
  121. goto err_verify;
  122. }
  123. /* Success */
  124. rc = 0;
  125. err_verify:
  126. /* Discard signature unless --keep was specified */
  127. if ( ! opts.keep )
  128. unregister_image ( signature );
  129. err_acquire_signature:
  130. err_acquire_image:
  131. return rc;
  132. }
  133. /** Image trust management commands */
  134. struct command image_trust_commands[] __command = {
  135. {
  136. .name = "imgtrust",
  137. .exec = imgtrust_exec,
  138. },
  139. {
  140. .name = "imgverify",
  141. .exec = imgverify_exec,
  142. },
  143. };
  144. /* Drag in objects typically required for signature verification */
  145. REQUIRE_OBJECT ( rsa );
  146. REQUIRE_OBJECT ( md5 );
  147. REQUIRE_OBJECT ( sha1 );
  148. REQUIRE_OBJECT ( sha256 );