Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / ipxe / src / hci / commands / image_trust_cmd.c
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
20 FILE_LICENCE ( GPL2_OR_LATER );
21
22 #include <stdint.h>
23 #include <stdio.h>
24 #include <getopt.h>
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>
30
31 /** @file
32  *
33  * Image trust management commands
34  *
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
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 ),
51 };
52
53 /** "imgtrust" command descriptor */
54 static struct command_descriptor imgtrust_cmd =
55         COMMAND_DESC ( struct imgtrust_options, imgtrust_opts, 0, 0, NULL );
56
57 /**
58  * The "imgtrust" command
59  *
60  * @v argc              Argument count
61  * @v argv              Argument list
62  * @ret rc              Return status code
63  */
64 static int imgtrust_exec ( int argc, char **argv ) {
65         struct imgtrust_options opts;
66         int rc;
67
68         /* Parse options */
69         if ( ( rc = parse_options ( argc, argv, &imgtrust_cmd, &opts ) ) != 0 )
70                 return rc;
71
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",
76                          strerror ( rc ) );
77                 return rc;
78         }
79
80         return 0;
81 }
82
83 /** "imgverify" options */
84 struct imgverify_options {
85         /** Required signer common name */
86         char *signer;
87         /** Keep signature after verification */
88         int keep;
89         /** Download timeout */
90         unsigned long timeout;
91 };
92
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),
101 };
102
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>" );
107
108 /**
109  * The "imgverify" command
110  *
111  * @v argc              Argument count
112  * @v argv              Argument list
113  * @ret rc              Return status code
114  */
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;
119         struct image *image;
120         struct image *signature;
121         int rc;
122
123         /* Parse options */
124         if ( ( rc = parse_options ( argc, argv, &imgverify_cmd, &opts ) ) != 0 )
125                 return rc;
126
127         /* Parse image name/URI string */
128         image_name_uri = argv[optind];
129
130         /* Parse signature name/URI string */
131         signature_name_uri = argv[ optind + 1 ];
132
133         /* Acquire the image */
134         if ( ( rc = imgacquire ( image_name_uri, opts.timeout, &image ) ) != 0 )
135                 goto err_acquire_image;
136
137         /* Acquire the signature image */
138         if ( ( rc = imgacquire ( signature_name_uri, opts.timeout,
139                                  &signature ) ) != 0 )
140                 goto err_acquire_signature;
141
142         /* Verify image */
143         if ( ( rc = imgverify ( image, signature, opts.signer ) ) != 0 ) {
144                 printf ( "Could not verify: %s\n", strerror ( rc ) );
145                 goto err_verify;
146         }
147
148         /* Success */
149         rc = 0;
150
151  err_verify:
152         /* Discard signature unless --keep was specified */
153         if ( ! opts.keep )
154                 unregister_image ( signature );
155  err_acquire_signature:
156  err_acquire_image:
157         return rc;
158 }
159
160 /** Image trust management commands */
161 struct command image_trust_commands[] __command = {
162         {
163                 .name = "imgtrust",
164                 .exec = imgtrust_exec,
165         },
166         {
167                 .name = "imgverify",
168                 .exec = imgverify_exec,
169         },
170 };
171
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 );