|
@@ -0,0 +1,172 @@
|
|
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., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
17
|
+ */
|
|
18
|
+
|
|
19
|
+FILE_LICENCE ( GPL2_OR_LATER );
|
|
20
|
+
|
|
21
|
+#include <stdint.h>
|
|
22
|
+#include <stdio.h>
|
|
23
|
+#include <getopt.h>
|
|
24
|
+#include <ipxe/image.h>
|
|
25
|
+#include <ipxe/command.h>
|
|
26
|
+#include <ipxe/parseopt.h>
|
|
27
|
+#include <usr/imgmgmt.h>
|
|
28
|
+#include <usr/imgtrust.h>
|
|
29
|
+
|
|
30
|
+/** @file
|
|
31
|
+ *
|
|
32
|
+ * Image trust management commands
|
|
33
|
+ *
|
|
34
|
+ */
|
|
35
|
+
|
|
36
|
+/** "imgtrust" options */
|
|
37
|
+struct imgtrust_options {
|
|
38
|
+ /** Allow trusted images */
|
|
39
|
+ int allow;
|
|
40
|
+ /** Make trust requirement permanent */
|
|
41
|
+ int permanent;
|
|
42
|
+};
|
|
43
|
+
|
|
44
|
+/** "imgtrust" option list */
|
|
45
|
+static struct option_descriptor imgtrust_opts[] = {
|
|
46
|
+ OPTION_DESC ( "allow", 'a', no_argument,
|
|
47
|
+ struct imgtrust_options, allow, parse_flag ),
|
|
48
|
+ OPTION_DESC ( "permanent", 'p', no_argument,
|
|
49
|
+ struct imgtrust_options, permanent, parse_flag ),
|
|
50
|
+};
|
|
51
|
+
|
|
52
|
+/** "imgtrust" command descriptor */
|
|
53
|
+static struct command_descriptor imgtrust_cmd =
|
|
54
|
+ COMMAND_DESC ( struct imgtrust_options, imgtrust_opts, 0, 0,
|
|
55
|
+ "[--allow] [--permanent]" );
|
|
56
|
+
|
|
57
|
+/**
|
|
58
|
+ * The "imgtrust" command
|
|
59
|
+ *
|
|
60
|
+ * @v argc Argument count
|
|
61
|
+ * @v argv Argument list
|
|
62
|
+ * @ret rc Return status code
|
|
63
|
+ */
|
|
64
|
+static int imgtrust_exec ( int argc, char **argv ) {
|
|
65
|
+ struct imgtrust_options opts;
|
|
66
|
+ int rc;
|
|
67
|
+
|
|
68
|
+ /* Parse options */
|
|
69
|
+ if ( ( rc = parse_options ( argc, argv, &imgtrust_cmd, &opts ) ) != 0 )
|
|
70
|
+ return rc;
|
|
71
|
+
|
|
72
|
+ /* Set trust requirement */
|
|
73
|
+ if ( ( rc = image_set_trust ( ( ! opts.allow ),
|
|
74
|
+ opts.permanent ) ) != 0 ) {
|
|
75
|
+ printf ( "Could not set image trust requirement: %s\n",
|
|
76
|
+ strerror ( rc ) );
|
|
77
|
+ return rc;
|
|
78
|
+ }
|
|
79
|
+
|
|
80
|
+ return 0;
|
|
81
|
+}
|
|
82
|
+
|
|
83
|
+/** "imgverify" options */
|
|
84
|
+struct imgverify_options {
|
|
85
|
+ /** Required signer common name */
|
|
86
|
+ const char *signer;
|
|
87
|
+ /** Keep signature after verification */
|
|
88
|
+ int keep;
|
|
89
|
+};
|
|
90
|
+
|
|
91
|
+/** "imgverify" option list */
|
|
92
|
+static struct option_descriptor imgverify_opts[] = {
|
|
93
|
+ OPTION_DESC ( "signer", 's', required_argument,
|
|
94
|
+ struct imgverify_options, signer, parse_string ),
|
|
95
|
+ OPTION_DESC ( "keep", 'k', no_argument,
|
|
96
|
+ struct imgverify_options, keep, parse_flag ),
|
|
97
|
+};
|
|
98
|
+
|
|
99
|
+/** "imgverify" command descriptor */
|
|
100
|
+static struct command_descriptor imgverify_cmd =
|
|
101
|
+ COMMAND_DESC ( struct imgverify_options, imgverify_opts, 2, 2,
|
|
102
|
+ "[--signer <signer>] [--keep] <uri|image> "
|
|
103
|
+ "<signature uri|image>" );
|
|
104
|
+
|
|
105
|
+/**
|
|
106
|
+ * The "imgverify" command
|
|
107
|
+ *
|
|
108
|
+ * @v argc Argument count
|
|
109
|
+ * @v argv Argument list
|
|
110
|
+ * @ret rc Return status code
|
|
111
|
+ */
|
|
112
|
+static int imgverify_exec ( int argc, char **argv ) {
|
|
113
|
+ struct imgverify_options opts;
|
|
114
|
+ const char *image_name_uri;
|
|
115
|
+ const char *signature_name_uri;
|
|
116
|
+ struct image *image;
|
|
117
|
+ struct image *signature;
|
|
118
|
+ int rc;
|
|
119
|
+
|
|
120
|
+ /* Parse options */
|
|
121
|
+ if ( ( rc = parse_options ( argc, argv, &imgverify_cmd, &opts ) ) != 0 )
|
|
122
|
+ return rc;
|
|
123
|
+
|
|
124
|
+ /* Parse image name/URI string */
|
|
125
|
+ image_name_uri = argv[optind];
|
|
126
|
+
|
|
127
|
+ /* Parse signature name/URI string */
|
|
128
|
+ signature_name_uri = argv[ optind + 1 ];
|
|
129
|
+
|
|
130
|
+ /* Acquire the image */
|
|
131
|
+ if ( ( rc = imgacquire ( image_name_uri, &image ) ) != 0 )
|
|
132
|
+ goto err_acquire_image;
|
|
133
|
+
|
|
134
|
+ /* Acquire the signature image */
|
|
135
|
+ if ( ( rc = imgacquire ( signature_name_uri, &signature ) ) != 0 )
|
|
136
|
+ goto err_acquire_signature;
|
|
137
|
+
|
|
138
|
+ /* Verify image */
|
|
139
|
+ if ( ( rc = imgverify ( image, signature, opts.signer ) ) != 0 ) {
|
|
140
|
+ printf ( "Could not verify: %s\n", strerror ( rc ) );
|
|
141
|
+ goto err_verify;
|
|
142
|
+ }
|
|
143
|
+
|
|
144
|
+ /* Success */
|
|
145
|
+ rc = 0;
|
|
146
|
+
|
|
147
|
+ err_verify:
|
|
148
|
+ /* Discard signature unless --keep was specified */
|
|
149
|
+ if ( ! opts.keep )
|
|
150
|
+ unregister_image ( signature );
|
|
151
|
+ err_acquire_signature:
|
|
152
|
+ err_acquire_image:
|
|
153
|
+ return rc;
|
|
154
|
+}
|
|
155
|
+
|
|
156
|
+/** Image trust management commands */
|
|
157
|
+struct command image_trust_commands[] __command = {
|
|
158
|
+ {
|
|
159
|
+ .name = "imgtrust",
|
|
160
|
+ .exec = imgtrust_exec,
|
|
161
|
+ },
|
|
162
|
+ {
|
|
163
|
+ .name = "imgverify",
|
|
164
|
+ .exec = imgverify_exec,
|
|
165
|
+ },
|
|
166
|
+};
|
|
167
|
+
|
|
168
|
+/* Drag in objects typically required for signature verification */
|
|
169
|
+REQUIRE_OBJECT ( rsa );
|
|
170
|
+REQUIRE_OBJECT ( md5 );
|
|
171
|
+REQUIRE_OBJECT ( sha1 );
|
|
172
|
+REQUIRE_OBJECT ( sha256 );
|