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
24FILE_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/imgcrypt.h>
35
36/** @file
37 *
38 * Image encryption management commands
39 *
40 */
41
42/** "imgdecrypt" options */
44 /** Decrypted image name */
45 char *name;
46 /** Keep envelope after decryption */
47 int keep;
48 /** Download timeout */
49 unsigned long timeout;
50 /** Display only error messages */
51 int quiet;
52};
53
54/** "imgdecrypt" option list */
56 OPTION_DESC ( "name", 'n', required_argument,
58 OPTION_DESC ( "keep", 'k', no_argument,
59 struct imgdecrypt_options, keep, parse_flag ),
60 OPTION_DESC ( "timeout", 't', required_argument,
62 OPTION_DESC ( "quiet", 'q', no_argument,
63 struct imgdecrypt_options, quiet, parse_flag ),
64};
65
66/** "imgdecrypt" command descriptor */
69 "<uri|image> <envelope uri|image>" );
70
71/**
72 * The "imgdecrypt" command
73 *
74 * @v argc Argument count
75 * @v argv Argument list
76 * @ret rc Return status code
77 */
78static int imgdecrypt_exec ( int argc, char **argv ) {
80 const char *image_name_uri;
81 const char *envelope_name_uri;
82 struct image *image;
83 struct image *envelope;
84 int rc;
85
86 /* Parse options */
87 if ( ( rc = parse_options ( argc, argv, &imgdecrypt_cmd, &opts ) ) != 0)
88 return rc;
89
90 /* Parse image name/URI string */
91 image_name_uri = argv[optind];
92
93 /* Parse envelope name/URI string */
94 envelope_name_uri = argv[ optind + 1 ];
95
96 /* Acquire the image */
97 if ( ( rc = imgacquire ( image_name_uri, opts.timeout, opts.quiet,
98 &image ) ) != 0 )
99 goto err_acquire_image;
100
101 /* Acquire the envelope image */
102 if ( ( rc = imgacquire ( envelope_name_uri, opts.timeout, opts.quiet,
103 &envelope ) ) != 0 )
104 goto err_acquire_envelope;
105
106 /* Decrypt image */
107 if ( ( rc = imgdecrypt ( image, envelope, opts.name ) ) != 0 ) {
108 printf ( "Could not decrypt: %s\n", strerror ( rc ) );
109 goto err_decrypt;
110 }
111
112 /* Success */
113 rc = 0;
114
115 err_decrypt:
116 /* Discard envelope unless --keep was specified */
117 if ( ! opts.keep )
118 unregister_image ( envelope );
119 err_acquire_envelope:
120 err_acquire_image:
121 return rc;
122}
123
124/** Image encryption management commands */
struct arbelprm_rc_send_wqe rc
Definition arbel.h:3
const char * name
Definition ath9k_hw.c:1986
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
void unregister_image(struct image *image)
Unregister executable image.
Definition image.c:390
Executable images.
static struct option_descriptor imgdecrypt_opts[]
"imgdecrypt" option list
static struct command_descriptor imgdecrypt_cmd
"imgdecrypt" command descriptor
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
Image encryption management.
int imgacquire(const char *name_uri, unsigned long timeout, int quiet, struct image **image)
Acquire an image.
Definition imgmgmt.c:148
Image 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
"imgdecrypt" options
int quiet
Display only error messages.
int keep
Keep envelope after decryption.
char * name
Decrypted image name.
unsigned long timeout
Download timeout.
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