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
20 FILE_LICENCE ( GPL2_OR_LATER );
25 #include <ipxe/image.h>
26 #include <ipxe/command.h>
27 #include <ipxe/parseopt.h>
28 #include <usr/imgmgmt.h>
29 #include <usr/imgtrust.h>
33 * Image trust management commands
37 /** "imgtrust" options */
38 struct imgtrust_options {
39 /** Allow trusted images */
41 /** Make trust requirement permanent */
45 /** "imgtrust" option list */
46 static struct option_descriptor imgtrust_opts[] = {
47 OPTION_DESC ( "allow", 'a', no_argument,
48 struct imgtrust_options, allow, parse_flag ),
49 OPTION_DESC ( "permanent", 'p', no_argument,
50 struct imgtrust_options, permanent, parse_flag ),
53 /** "imgtrust" command descriptor */
54 static struct command_descriptor imgtrust_cmd =
55 COMMAND_DESC ( struct imgtrust_options, imgtrust_opts, 0, 0, NULL );
58 * The "imgtrust" command
60 * @v argc Argument count
61 * @v argv Argument list
62 * @ret rc Return status code
64 static int imgtrust_exec ( int argc, char **argv ) {
65 struct imgtrust_options opts;
69 if ( ( rc = parse_options ( argc, argv, &imgtrust_cmd, &opts ) ) != 0 )
72 /* Set trust requirement */
73 if ( ( rc = image_set_trust ( ( ! opts.allow ),
74 opts.permanent ) ) != 0 ) {
75 printf ( "Could not set image trust requirement: %s\n",
83 /** "imgverify" options */
84 struct imgverify_options {
85 /** Required signer common name */
87 /** Keep signature after verification */
89 /** Download timeout */
90 unsigned long timeout;
93 /** "imgverify" option list */
94 static struct option_descriptor imgverify_opts[] = {
95 OPTION_DESC ( "signer", 's', required_argument,
96 struct imgverify_options, signer, parse_string ),
97 OPTION_DESC ( "keep", 'k', no_argument,
98 struct imgverify_options, keep, parse_flag ),
99 OPTION_DESC ( "timeout", 't', required_argument,
100 struct imgverify_options, timeout, parse_timeout),
103 /** "imgverify" command descriptor */
104 static struct command_descriptor imgverify_cmd =
105 COMMAND_DESC ( struct imgverify_options, imgverify_opts, 2, 2,
106 "<uri|image> <signature uri|image>" );
109 * The "imgverify" command
111 * @v argc Argument count
112 * @v argv Argument list
113 * @ret rc Return status code
115 static int imgverify_exec ( int argc, char **argv ) {
116 struct imgverify_options opts;
117 const char *image_name_uri;
118 const char *signature_name_uri;
120 struct image *signature;
124 if ( ( rc = parse_options ( argc, argv, &imgverify_cmd, &opts ) ) != 0 )
127 /* Parse image name/URI string */
128 image_name_uri = argv[optind];
130 /* Parse signature name/URI string */
131 signature_name_uri = argv[ optind + 1 ];
133 /* Acquire the image */
134 if ( ( rc = imgacquire ( image_name_uri, opts.timeout, &image ) ) != 0 )
135 goto err_acquire_image;
137 /* Acquire the signature image */
138 if ( ( rc = imgacquire ( signature_name_uri, opts.timeout,
139 &signature ) ) != 0 )
140 goto err_acquire_signature;
143 if ( ( rc = imgverify ( image, signature, opts.signer ) ) != 0 ) {
144 printf ( "Could not verify: %s\n", strerror ( rc ) );
152 /* Discard signature unless --keep was specified */
154 unregister_image ( signature );
155 err_acquire_signature:
160 /** Image trust management commands */
161 struct command image_trust_commands[] __command = {
164 .exec = imgtrust_exec,
168 .exec = imgverify_exec,
172 /* Drag in objects typically required for signature verification */
173 REQUIRE_OBJECT ( rsa );
174 REQUIRE_OBJECT ( md5 );
175 REQUIRE_OBJECT ( sha1 );
176 REQUIRE_OBJECT ( sha256 );