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 
26 #include <stdint.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <getopt.h>
30 #include <ipxe/image.h>
31 #include <ipxe/command.h>
32 #include <ipxe/parseopt.h>
33 #include <usr/imgmgmt.h>
34 #include <usr/imgtrust.h>
35 
36 /** @file
37  *
38  * Image trust management commands
39  *
40  */
41 
42 /** "imgtrust" options */
44  /** Allow trusted images */
45  int allow;
46  /** Make trust requirement permanent */
47  int permanent;
48 };
49 
50 /** "imgtrust" option list */
51 static struct option_descriptor imgtrust_opts[] = {
52  OPTION_DESC ( "allow", 'a', no_argument,
53  struct imgtrust_options, allow, parse_flag ),
54  OPTION_DESC ( "permanent", 'p', no_argument,
55  struct imgtrust_options, permanent, parse_flag ),
56 };
57 
58 /** "imgtrust" command descriptor */
61 
62 /**
63  * The "imgtrust" command
64  *
65  * @v argc Argument count
66  * @v argv Argument list
67  * @ret rc Return status code
68  */
69 static int imgtrust_exec ( int argc, char **argv ) {
70  struct imgtrust_options opts;
71  int rc;
72 
73  /* Parse options */
74  if ( ( rc = parse_options ( argc, argv, &imgtrust_cmd, &opts ) ) != 0 )
75  return rc;
76 
77  /* Set trust requirement */
78  if ( ( rc = image_set_trust ( ( ! opts.allow ),
79  opts.permanent ) ) != 0 ) {
80  printf ( "Could not set image trust requirement: %s\n",
81  strerror ( rc ) );
82  return rc;
83  }
84 
85  return 0;
86 }
87 
88 /** "imgverify" options */
90  /** Required signer common name */
91  char *signer;
92  /** Keep signature after verification */
93  int keep;
94  /** Download timeout */
95  unsigned long timeout;
96 };
97 
98 /** "imgverify" option list */
99 static struct option_descriptor imgverify_opts[] = {
100  OPTION_DESC ( "signer", 's', required_argument,
101  struct imgverify_options, signer, parse_string ),
102  OPTION_DESC ( "keep", 'k', no_argument,
103  struct imgverify_options, keep, parse_flag ),
104  OPTION_DESC ( "timeout", 't', required_argument,
106 };
107 
108 /** "imgverify" command descriptor */
111  "<uri|image> <signature uri|image>" );
112 
113 /**
114  * The "imgverify" command
115  *
116  * @v argc Argument count
117  * @v argv Argument list
118  * @ret rc Return status code
119  */
120 static int imgverify_exec ( int argc, char **argv ) {
121  struct imgverify_options opts;
122  const char *image_name_uri;
123  const char *signature_name_uri;
124  struct image *image;
125  struct image *signature;
126  int rc;
127 
128  /* Parse options */
129  if ( ( rc = parse_options ( argc, argv, &imgverify_cmd, &opts ) ) != 0 )
130  return rc;
131 
132  /* Parse image name/URI string */
133  image_name_uri = argv[optind];
134 
135  /* Parse signature name/URI string */
136  signature_name_uri = argv[ optind + 1 ];
137 
138  /* Acquire the image */
139  if ( ( rc = imgacquire ( image_name_uri, opts.timeout, &image ) ) != 0 )
140  goto err_acquire_image;
141 
142  /* Acquire the signature image */
143  if ( ( rc = imgacquire ( signature_name_uri, opts.timeout,
144  &signature ) ) != 0 )
145  goto err_acquire_signature;
146 
147  /* Verify image */
148  if ( ( rc = imgverify ( image, signature, opts.signer ) ) != 0 ) {
149  printf ( "Could not verify: %s\n", strerror ( rc ) );
150  goto err_verify;
151  }
152 
153  /* Success */
154  rc = 0;
155 
156  err_verify:
157  /* Discard signature unless --keep was specified */
158  if ( ! opts.keep )
160  err_acquire_signature:
161  err_acquire_image:
162  return rc;
163 }
164 
165 /** Image trust management commands */
166 COMMAND ( imgtrust, imgtrust_exec );
int image_set_trust(int require_trusted, int permanent)
Change image trust requirement.
Definition: image.c:583
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:51
int printf(const char *fmt,...)
Write a formatted string to the console.
Definition: vsprintf.c:464
static struct option_descriptor imgtrust_opts[]
"imgtrust" option list
int optind
Current option index.
Definition: getopt.c:51
"imgtrust" options
int allow
Allow trusted images.
int parse_timeout(char *text, unsigned long *value)
Parse timeout value (in ms)
Definition: parseopt.c:114
int parse_options(int argc, char **argv, struct command_descriptor *cmd, void *opts)
Parse command-line options.
Definition: parseopt.c:484
int permanent
Make trust requirement permanent.
An executable image.
Definition: image.h:23
A command descriptor.
Definition: parseopt.h:77
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:73
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:226
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
Command line option parsing.
Option does not take an argument.
Definition: getopt.h:16
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:357
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:67
Option requires an argument.
Definition: getopt.h:18
static int imgtrust_exec(int argc, char **argv)
The "imgtrust" command.
A command-line option descriptor.
Definition: parseopt.h:23
static union @447 opts
"cert<xxx>" option list
#define COMMAND_DESC(_struct, _options, _min_args, _max_args, _usage)
Construct command descriptor.
Definition: parseopt.h:108
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:321
String functions.
int imgacquire(const char *name_uri, unsigned long timeout, struct image **image)
Acquire an image.
Definition: imgmgmt.c:142
static struct command_descriptor imgverify_cmd
"imgverify" command descriptor