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 
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25 FILE_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 */
48  int permanent;
49 };
50 
51 /** "imgtrust" option list */
52 static struct option_descriptor imgtrust_opts[] = {
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  */
70 static int imgtrust_exec ( int argc, char **argv ) {
71  struct imgtrust_options opts;
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 };
98 
99 /** "imgverify" option list */
101  OPTION_DESC ( "signer", 's', required_argument,
102  struct imgverify_options, signer, parse_string ),
103  OPTION_DESC ( "keep", 'k', no_argument,
104  struct imgverify_options, keep, parse_flag ),
105  OPTION_DESC ( "timeout", 't', required_argument,
107 };
108 
109 /** "imgverify" command descriptor */
112  "<uri|image> <signature uri|image>" );
113 
114 /**
115  * The "imgverify" command
116  *
117  * @v argc Argument count
118  * @v argv Argument list
119  * @ret rc Return status code
120  */
121 static int imgverify_exec ( int argc, char **argv ) {
122  struct imgverify_options opts;
123  const char *image_name_uri;
124  const char *signature_name_uri;
125  struct image *image;
126  struct image *signature;
127  int rc;
128 
129  /* Parse options */
130  if ( ( rc = parse_options ( argc, argv, &imgverify_cmd, &opts ) ) != 0 )
131  return rc;
132 
133  /* Parse image name/URI string */
134  image_name_uri = argv[optind];
135 
136  /* Parse signature name/URI string */
137  signature_name_uri = argv[ optind + 1 ];
138 
139  /* Acquire the image */
140  if ( ( rc = imgacquire ( image_name_uri, opts.timeout, &image ) ) != 0 )
141  goto err_acquire_image;
142 
143  /* Acquire the signature image */
144  if ( ( rc = imgacquire ( signature_name_uri, opts.timeout,
145  &signature ) ) != 0 )
146  goto err_acquire_signature;
147 
148  /* Verify image */
149  if ( ( rc = imgverify ( image, signature, opts.signer ) ) != 0 ) {
150  printf ( "Could not verify: %s\n", strerror ( rc ) );
151  goto err_verify;
152  }
153 
154  /* Success */
155  rc = 0;
156 
157  err_verify:
158  /* Discard signature unless --keep was specified */
159  if ( ! opts.keep )
161  err_acquire_signature:
162  err_acquire_image:
163  return rc;
164 }
165 
166 /** Image trust management commands */
167 COMMAND ( imgtrust, imgtrust_exec );
int image_set_trust(int require_trusted, int permanent)
Change image trust requirement.
Definition: image.c:584
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
int imgverify(struct image *image, struct image *signature, const char *name)
Verify image using downloaded signature.
Definition: imgtrust.c:52
int printf(const char *fmt,...)
Write a formatted string to the console.
Definition: vsprintf.c:465
static struct option_descriptor imgtrust_opts[]
"imgtrust" option list
int optind
Current option index.
Definition: getopt.c:52
"imgtrust" options
int allow
Allow trusted images.
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
int permanent
Make trust requirement permanent.
An executable image.
Definition: image.h:24
A command descriptor.
Definition: parseopt.h:78
int keep
Keep signature after verification.
"imgverify" options
Parse command-line options.
int parse_string(char *text, char **value)
Parse string value.
Definition: parseopt.c:74
Executable images.
static int imgverify_exec(int argc, char **argv)
The "imgverify" command.
Image trust management.
int parse_flag(char *text __unused, int *flag)
Parse flag.
Definition: parseopt.c:227
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:79
FILE_SECBOOT(PERMITTED)
Command line option parsing.
Option does not take an argument.
Definition: getopt.h:17
static struct command_descriptor imgtrust_cmd
"imgtrust" command descriptor
COMMAND(imgtrust, imgtrust_exec)
Image trust management commands.
static struct option_descriptor imgverify_opts[]
"imgverify" option list
void unregister_image(struct image *image)
Unregister executable image.
Definition: image.c:358
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
Image management.
#define OPTION_DESC(_longopt, _shortopt, _has_arg, _struct, _field, _parse)
Construct option descriptor.
Definition: parseopt.h:68
Option requires an argument.
Definition: getopt.h:19
static int imgtrust_exec(int argc, char **argv)
The "imgtrust" command.
A command-line option descriptor.
Definition: parseopt.h:24
static union @447 opts
"cert<xxx>" option list
#define COMMAND_DESC(_struct, _options, _min_args, _max_args, _usage)
Construct command descriptor.
Definition: parseopt.h:109
void timeout(int)
unsigned long timeout
Download timeout.
u8 signature
CPU signature.
Definition: CIB_PRM.h:35
char * signer
Required signer common name.
#define NULL
NULL pointer (VOID *)
Definition: Base.h:322
String functions.
int imgacquire(const char *name_uri, unsigned long timeout, struct image **image)
Acquire an image.
Definition: imgmgmt.c:143
static struct command_descriptor imgverify_cmd
"imgverify" command descriptor