iPXE
image_trust_cmd.c
Go to the documentation of this file.
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
24FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25FILE_SECBOOT ( PERMITTED );
26
27#include <stdint.h>
28#include <stdio.h>
29#include <string.h>
30#include <getopt.h>
31#include <ipxe/image.h>
32#include <ipxe/command.h>
33#include <ipxe/parseopt.h>
34#include <usr/imgmgmt.h>
35#include <usr/imgtrust.h>
36
37/** @file
38 *
39 * Image trust management commands
40 *
41 */
42
43/** "imgtrust" options */
45 /** Allow trusted images */
46 int allow;
47 /** Make trust requirement permanent */
49};
50
51/** "imgtrust" option list */
53 OPTION_DESC ( "allow", 'a', no_argument,
54 struct imgtrust_options, allow, parse_flag ),
55 OPTION_DESC ( "permanent", 'p', no_argument,
56 struct imgtrust_options, permanent, parse_flag ),
57};
58
59/** "imgtrust" command descriptor */
62
63/**
64 * The "imgtrust" command
65 *
66 * @v argc Argument count
67 * @v argv Argument list
68 * @ret rc Return status code
69 */
70static int imgtrust_exec ( int argc, char **argv ) {
72 int rc;
73
74 /* Parse options */
75 if ( ( rc = parse_options ( argc, argv, &imgtrust_cmd, &opts ) ) != 0 )
76 return rc;
77
78 /* Set trust requirement */
79 if ( ( rc = image_set_trust ( ( ! opts.allow ),
80 opts.permanent ) ) != 0 ) {
81 printf ( "Could not set image trust requirement: %s\n",
82 strerror ( rc ) );
83 return rc;
84 }
85
86 return 0;
87}
88
89/** "imgverify" options */
91 /** Required signer common name */
92 char *signer;
93 /** Keep signature after verification */
94 int keep;
95 /** Download timeout */
96 unsigned long timeout;
97 /** Display only error messages */
98 int quiet;
99};
100
101/** "imgverify" option list */
103 OPTION_DESC ( "signer", 's', required_argument,
104 struct imgverify_options, signer, parse_string ),
105 OPTION_DESC ( "keep", 'k', no_argument,
106 struct imgverify_options, keep, parse_flag ),
107 OPTION_DESC ( "timeout", 't', required_argument,
109 OPTION_DESC ( "quiet", 'q', no_argument,
110 struct imgverify_options, quiet, parse_flag ),
111};
112
113/** "imgverify" command descriptor */
116 "<uri|image> <signature uri|image>" );
117
118/**
119 * The "imgverify" command
120 *
121 * @v argc Argument count
122 * @v argv Argument list
123 * @ret rc Return status code
124 */
125static int imgverify_exec ( int argc, char **argv ) {
126 struct imgverify_options opts;
127 const char *image_name_uri;
128 const char *signature_name_uri;
129 struct image *image;
130 struct image *signature;
131 int rc;
132
133 /* Parse options */
134 if ( ( rc = parse_options ( argc, argv, &imgverify_cmd, &opts ) ) != 0 )
135 return rc;
136
137 /* Parse image name/URI string */
138 image_name_uri = argv[optind];
139
140 /* Parse signature name/URI string */
141 signature_name_uri = argv[ optind + 1 ];
142
143 /* Acquire the image */
144 if ( ( rc = imgacquire ( image_name_uri, opts.timeout, opts.quiet,
145 &image ) ) != 0 )
146 goto err_acquire_image;
147
148 /* Acquire the signature image */
149 if ( ( rc = imgacquire ( signature_name_uri, opts.timeout, opts.quiet,
150 &signature ) ) != 0 )
151 goto err_acquire_signature;
152
153 /* Verify image */
154 if ( ( rc = imgverify ( image, signature, opts.signer ) ) != 0 ) {
155 printf ( "Could not verify: %s\n", strerror ( rc ) );
156 goto err_verify;
157 }
158
159 /* Success */
160 rc = 0;
161
162 err_verify:
163 /* Discard signature unless --keep was specified */
164 if ( ! opts.keep )
166 err_acquire_signature:
167 err_acquire_image:
168 return rc;
169}
170
171/** Image trust management commands */
172COMMAND ( imgtrust, imgtrust_exec );
#define NULL
NULL pointer (VOID *).
Definition Base.h:321
u8 signature
CPU signature.
Definition CIB_PRM.h:7
struct arbelprm_rc_send_wqe rc
Definition arbel.h:3
static union @024010030001061367220137227263210031030210157031 opts
"cert<xxx>" option list
#define COMMAND(name, exec)
Definition command.h:27
void timeout(int)
int optind
Current option index.
Definition getopt.c:52
Parse command-line options.
@ required_argument
Option requires an argument.
Definition getopt.h:19
@ no_argument
Option does not take an argument.
Definition getopt.h:17
#define FILE_LICENCE(_licence)
Declare a particular licence as applying to a file.
Definition compiler.h:921
#define FILE_SECBOOT(_status)
Declare a file's UEFI Secure Boot permission status.
Definition compiler.h:951
void unregister_image(struct image *image)
Unregister executable image.
Definition image.c:390
int image_set_trust(int require_trusted, int permanent)
Change image trust requirement.
Definition image.c:616
Executable images.
static struct option_descriptor imgtrust_opts[]
"imgtrust" option list
static struct command_descriptor imgverify_cmd
"imgverify" command descriptor
static struct command_descriptor imgtrust_cmd
"imgtrust" command descriptor
static int imgverify_exec(int argc, char **argv)
The "imgverify" command.
static struct option_descriptor imgverify_opts[]
"imgverify" option list
static int imgtrust_exec(int argc, char **argv)
The "imgtrust" command.
int imgacquire(const char *name_uri, unsigned long timeout, int quiet, struct image **image)
Acquire an image.
Definition imgmgmt.c:148
Image management.
int imgverify(struct image *image, struct image *signature, const char *name)
Verify image using downloaded signature.
Definition imgtrust.c:52
Image trust management.
String functions.
int parse_flag(char *text __unused, int *flag)
Parse flag.
Definition parseopt.c:227
int parse_string(char *text, char **value)
Parse string value.
Definition parseopt.c:74
int parse_timeout(char *text, unsigned long *value)
Parse timeout value (in ms).
Definition parseopt.c:115
int parse_options(int argc, char **argv, struct command_descriptor *cmd, void *opts)
Parse command-line options.
Definition parseopt.c:485
Command line option parsing.
#define COMMAND_DESC(_struct, _options, _min_args, _max_args, _usage)
Construct command descriptor.
Definition parseopt.h:109
#define OPTION_DESC(_longopt, _shortopt, _has_arg, _struct, _field, _parse)
Construct option descriptor.
Definition parseopt.h:68
char * strerror(int errno)
Retrieve string representation of error number.
Definition strerror.c:79
A command descriptor.
Definition parseopt.h:78
An executable image.
Definition image.h:24
"imgtrust" options
int allow
Allow trusted images.
int permanent
Make trust requirement permanent.
"imgverify" options
unsigned long timeout
Download timeout.
char * signer
Required signer common name.
int quiet
Display only error messages.
int keep
Keep signature after verification.
A command-line option descriptor.
Definition parseopt.h:24
int printf(const char *fmt,...)
Write a formatted string to the console.
Definition vsprintf.c:485