2 * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
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.
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.
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
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.
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
29 #include <ipxe/image.h>
30 #include <ipxe/command.h>
31 #include <ipxe/parseopt.h>
32 #include <usr/imgmgmt.h>
33 #include <usr/imgtrust.h>
37 * Image trust management commands
41 /** "imgtrust" options */
42 struct imgtrust_options {
43 /** Allow trusted images */
45 /** Make trust requirement permanent */
49 /** "imgtrust" option list */
50 static struct option_descriptor imgtrust_opts[] = {
51 OPTION_DESC ( "allow", 'a', no_argument,
52 struct imgtrust_options, allow, parse_flag ),
53 OPTION_DESC ( "permanent", 'p', no_argument,
54 struct imgtrust_options, permanent, parse_flag ),
57 /** "imgtrust" command descriptor */
58 static struct command_descriptor imgtrust_cmd =
59 COMMAND_DESC ( struct imgtrust_options, imgtrust_opts, 0, 0, NULL );
62 * The "imgtrust" command
64 * @v argc Argument count
65 * @v argv Argument list
66 * @ret rc Return status code
68 static int imgtrust_exec ( int argc, char **argv ) {
69 struct imgtrust_options opts;
73 if ( ( rc = parse_options ( argc, argv, &imgtrust_cmd, &opts ) ) != 0 )
76 /* Set trust requirement */
77 if ( ( rc = image_set_trust ( ( ! opts.allow ),
78 opts.permanent ) ) != 0 ) {
79 printf ( "Could not set image trust requirement: %s\n",
87 /** "imgverify" options */
88 struct imgverify_options {
89 /** Required signer common name */
91 /** Keep signature after verification */
93 /** Download timeout */
94 unsigned long timeout;
97 /** "imgverify" option list */
98 static struct option_descriptor imgverify_opts[] = {
99 OPTION_DESC ( "signer", 's', required_argument,
100 struct imgverify_options, signer, parse_string ),
101 OPTION_DESC ( "keep", 'k', no_argument,
102 struct imgverify_options, keep, parse_flag ),
103 OPTION_DESC ( "timeout", 't', required_argument,
104 struct imgverify_options, timeout, parse_timeout),
107 /** "imgverify" command descriptor */
108 static struct command_descriptor imgverify_cmd =
109 COMMAND_DESC ( struct imgverify_options, imgverify_opts, 2, 2,
110 "<uri|image> <signature uri|image>" );
113 * The "imgverify" command
115 * @v argc Argument count
116 * @v argv Argument list
117 * @ret rc Return status code
119 static int imgverify_exec ( int argc, char **argv ) {
120 struct imgverify_options opts;
121 const char *image_name_uri;
122 const char *signature_name_uri;
124 struct image *signature;
128 if ( ( rc = parse_options ( argc, argv, &imgverify_cmd, &opts ) ) != 0 )
131 /* Parse image name/URI string */
132 image_name_uri = argv[optind];
134 /* Parse signature name/URI string */
135 signature_name_uri = argv[ optind + 1 ];
137 /* Acquire the image */
138 if ( ( rc = imgacquire ( image_name_uri, opts.timeout, &image ) ) != 0 )
139 goto err_acquire_image;
141 /* Acquire the signature image */
142 if ( ( rc = imgacquire ( signature_name_uri, opts.timeout,
143 &signature ) ) != 0 )
144 goto err_acquire_signature;
147 if ( ( rc = imgverify ( image, signature, opts.signer ) ) != 0 ) {
148 printf ( "Could not verify: %s\n", strerror ( rc ) );
156 /* Discard signature unless --keep was specified */
158 unregister_image ( signature );
159 err_acquire_signature:
164 /** Image trust management commands */
165 struct command image_trust_commands[] __command = {
168 .exec = imgtrust_exec,
172 .exec = imgverify_exec,
176 /* Drag in objects via command list */
177 REQUIRING_SYMBOL ( image_trust_commands );
179 /* Drag in objects typically required for signature verification */
180 REQUIRE_OBJECT ( rsa );
181 REQUIRE_OBJECT ( md5 );
182 REQUIRE_OBJECT ( sha1 );
183 REQUIRE_OBJECT ( sha256 );