iPXE
image_crypt_cmd.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2024 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/imgcrypt.h>
34 
35 /** @file
36  *
37  * Image encryption management commands
38  *
39  */
40 
41 /** "imgdecrypt" options */
43  /** Decrypted image name */
44  char *name;
45  /** Keep envelope after decryption */
46  int keep;
47  /** Download timeout */
48  unsigned long timeout;
49 };
50 
51 /** "imgdecrypt" option list */
53  OPTION_DESC ( "name", 'n', required_argument,
55  OPTION_DESC ( "keep", 'k', no_argument,
56  struct imgdecrypt_options, keep, parse_flag ),
57  OPTION_DESC ( "timeout", 't', required_argument,
59 };
60 
61 /** "imgdecrypt" command descriptor */
64  "<uri|image> <envelope uri|image>" );
65 
66 /**
67  * The "imgdecrypt" command
68  *
69  * @v argc Argument count
70  * @v argv Argument list
71  * @ret rc Return status code
72  */
73 static int imgdecrypt_exec ( int argc, char **argv ) {
74  struct imgdecrypt_options opts;
75  const char *image_name_uri;
76  const char *envelope_name_uri;
77  struct image *image;
78  struct image *envelope;
79  int rc;
80 
81  /* Parse options */
82  if ( ( rc = parse_options ( argc, argv, &imgdecrypt_cmd, &opts ) ) != 0)
83  return rc;
84 
85  /* Parse image name/URI string */
86  image_name_uri = argv[optind];
87 
88  /* Parse envelope name/URI string */
89  envelope_name_uri = argv[ optind + 1 ];
90 
91  /* Acquire the image */
92  if ( ( rc = imgacquire ( image_name_uri, opts.timeout, &image ) ) != 0 )
93  goto err_acquire_image;
94 
95  /* Acquire the envelope image */
96  if ( ( rc = imgacquire ( envelope_name_uri, opts.timeout,
97  &envelope ) ) != 0 )
98  goto err_acquire_envelope;
99 
100  /* Decrypt image */
101  if ( ( rc = imgdecrypt ( image, envelope, opts.name ) ) != 0 ) {
102  printf ( "Could not decrypt: %s\n", strerror ( rc ) );
103  goto err_decrypt;
104  }
105 
106  /* Success */
107  rc = 0;
108 
109  err_decrypt:
110  /* Discard envelope unless --keep was specified */
111  if ( ! opts.keep )
112  unregister_image ( envelope );
113  err_acquire_envelope:
114  err_acquire_image:
115  return rc;
116 }
117 
118 /** Image encryption management commands */
119 struct command image_crypt_commands[] __command = {
120  {
121  .name = "imgdecrypt",
122  .exec = imgdecrypt_exec,
123  },
124 };
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
const char * name
Definition: ath9k_hw.c:1984
int printf(const char *fmt,...)
Write a formatted string to the console.
Definition: vsprintf.c:464
int optind
Current option index.
Definition: getopt.c:51
int keep
Keep envelope after decryption.
"imgdecrypt" options
A command-line command.
Definition: command.h:9
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
unsigned long timeout
Download timeout.
An executable image.
Definition: image.h:24
A command descriptor.
Definition: parseopt.h:77
Parse command-line options.
int parse_string(char *text, char **value)
Parse string value.
Definition: parseopt.c:73
Executable images.
static int imgdecrypt_exec(int argc, char **argv)
The "imgdecrypt" command.
int imgdecrypt(struct image *image, struct image *envelope, const char *name)
Decrypt image using downloaded envelope.
Definition: imgcrypt.c:47
static struct option_descriptor imgdecrypt_opts[]
"imgdecrypt" option list
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
struct command image_crypt_commands [] __command
Image encryption management commands.
char * name
Decrypted image name.
const char * name
Name of the command.
Definition: command.h:11
void unregister_image(struct image *image)
Unregister executable image.
Definition: image.c:322
Image management.
static struct command_descriptor imgdecrypt_cmd
"imgdecrypt" command descriptor
#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
A command-line option descriptor.
Definition: parseopt.h:23
#define COMMAND_DESC(_struct, _options, _min_args, _max_args, _usage)
Construct command descriptor.
Definition: parseopt.h:108
void timeout(int)
static union @438 opts
"cert<xxx>" option list
Image encryption management.
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
int imgacquire(const char *name_uri, unsigned long timeout, struct image **image)
Acquire an image.
Definition: imgmgmt.c:141