These changes are the raw update to qemu-2.6.
[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  * 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.
22  */
23
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25
26 #include <stdint.h>
27 #include <stdio.h>
28 #include <getopt.h>
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>
34
35 /** @file
36  *
37  * Image trust management commands
38  *
39  */
40
41 /** "imgtrust" options */
42 struct imgtrust_options {
43         /** Allow trusted images */
44         int allow;
45         /** Make trust requirement permanent */
46         int permanent;
47 };
48
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 ),
55 };
56
57 /** "imgtrust" command descriptor */
58 static struct command_descriptor imgtrust_cmd =
59         COMMAND_DESC ( struct imgtrust_options, imgtrust_opts, 0, 0, NULL );
60
61 /**
62  * The "imgtrust" command
63  *
64  * @v argc              Argument count
65  * @v argv              Argument list
66  * @ret rc              Return status code
67  */
68 static int imgtrust_exec ( int argc, char **argv ) {
69         struct imgtrust_options opts;
70         int rc;
71
72         /* Parse options */
73         if ( ( rc = parse_options ( argc, argv, &imgtrust_cmd, &opts ) ) != 0 )
74                 return rc;
75
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",
80                          strerror ( rc ) );
81                 return rc;
82         }
83
84         return 0;
85 }
86
87 /** "imgverify" options */
88 struct imgverify_options {
89         /** Required signer common name */
90         char *signer;
91         /** Keep signature after verification */
92         int keep;
93         /** Download timeout */
94         unsigned long timeout;
95 };
96
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),
105 };
106
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>" );
111
112 /**
113  * The "imgverify" command
114  *
115  * @v argc              Argument count
116  * @v argv              Argument list
117  * @ret rc              Return status code
118  */
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;
123         struct image *image;
124         struct image *signature;
125         int rc;
126
127         /* Parse options */
128         if ( ( rc = parse_options ( argc, argv, &imgverify_cmd, &opts ) ) != 0 )
129                 return rc;
130
131         /* Parse image name/URI string */
132         image_name_uri = argv[optind];
133
134         /* Parse signature name/URI string */
135         signature_name_uri = argv[ optind + 1 ];
136
137         /* Acquire the image */
138         if ( ( rc = imgacquire ( image_name_uri, opts.timeout, &image ) ) != 0 )
139                 goto err_acquire_image;
140
141         /* Acquire the signature image */
142         if ( ( rc = imgacquire ( signature_name_uri, opts.timeout,
143                                  &signature ) ) != 0 )
144                 goto err_acquire_signature;
145
146         /* Verify image */
147         if ( ( rc = imgverify ( image, signature, opts.signer ) ) != 0 ) {
148                 printf ( "Could not verify: %s\n", strerror ( rc ) );
149                 goto err_verify;
150         }
151
152         /* Success */
153         rc = 0;
154
155  err_verify:
156         /* Discard signature unless --keep was specified */
157         if ( ! opts.keep )
158                 unregister_image ( signature );
159  err_acquire_signature:
160  err_acquire_image:
161         return rc;
162 }
163
164 /** Image trust management commands */
165 struct command image_trust_commands[] __command = {
166         {
167                 .name = "imgtrust",
168                 .exec = imgtrust_exec,
169         },
170         {
171                 .name = "imgverify",
172                 .exec = imgverify_exec,
173         },
174 };
175
176 /* Drag in objects via command list */
177 REQUIRING_SYMBOL ( image_trust_commands );
178
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 );