blob: 6f9dc23366d7ddeff5c269adeeb0459f255542f3 [file] [log] [blame]
Randall Spangler7d6898d2010-06-11 09:22:13 -07001/* Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
2 * Use of this source code is governed by a BSD-style license that can be
3 * found in the LICENSE file.
4 *
5 * Verified boot key utility
6 */
7
Randall Spanglerd55c6452010-06-10 12:43:51 -07008#include <getopt.h>
9#include <inttypes.h> /* For PRIu64 */
10#include <stdio.h>
11#include <stdlib.h>
12
13#include "cryptolib.h"
14#include "host_common.h"
15#include "vboot_common.h"
16
17
18/* Command line options */
19enum {
20 OPT_IN = 1000,
21 OPT_OUT,
22 OPT_KEY_VERSION,
23 OPT_ALGORITHM,
24 OPT_MODE_PACK,
25 OPT_MODE_UNPACK,
26};
27
28static struct option long_opts[] = {
29 {"in", 1, 0, OPT_IN },
30 {"out", 1, 0, OPT_OUT },
31 {"version", 1, 0, OPT_KEY_VERSION },
32 {"algorithm", 1, 0, OPT_ALGORITHM },
33 {"pack", 0, 0, OPT_MODE_PACK },
34 {"unpack", 0, 0, OPT_MODE_UNPACK },
35 {NULL, 0, 0, 0}
36};
37
38
39/* Print help and return error */
40static int PrintHelp(void) {
41 int i;
42
43 puts("vbutil_key - Verified boot key utility\n"
44 "\n"
45 "Usage: vbutil_key <--pack|--unpack> [OPTIONS]\n"
46 "\n"
47 "For '--pack', required OPTIONS are:\n"
48 " --in <infile> Input key in .keyb format\n"
49 " --out <outfile> Output file for .vbpubk format\n"
50 " --version <number> Key version number\n"
51 " --algorithm <algoid> Signing algorithm for key, one of:");
52
53 for (i = 0; i < kNumAlgorithms; i++)
54 printf(" %d (%s)\n", i, algo_strings[i]);
55
56 puts("\n"
57 "For '--unpack', required OPTIONS are:\n"
58 " --in <infile> Input key in .vbpubk format\n"
59 "Optional OPTIONS are:\n"
60 " --out <outfile> Output file for .keyb format\n"
61 "");
62 return 1;
63}
64
65
66/* Pack a .keyb file into a .vbpubk */
67static int Pack(const char *infile, const char *outfile, uint64_t algorithm,
68 uint64_t version) {
69 VbPublicKey* key;
70
71 if (!infile || !outfile) {
72 fprintf(stderr, "vbutil_key: Must specify --in and --out\n");
73 return 1;
74 }
75
76 key = PublicKeyReadKeyb(infile, algorithm, version);
77 if (!key) {
78 fprintf(stderr, "vbutil_key: Error reading key.\n");
79 return 1;
80 }
81
82 if (0 != PublicKeyWrite(outfile, key)) {
83 fprintf(stderr, "vbutil_key: Error writing key.\n");
84 return 1;
85 }
86
87 Free(key);
88 return 0;
89}
90
91
92/* Unpack a .vbpubk */
93static int Unpack(const char *infile, const char *outfile) {
94 VbPublicKey* key;
95
96 if (!infile) {
97 fprintf(stderr, "vbutil_key: Must specify --in\n");
98 return 1;
99 }
100
101 key = PublicKeyRead(infile);
102 if (!key) {
103 fprintf(stderr, "vbutil_key: Error reading key.\n");
104 return 1;
105 }
106
107 printf("Key file: %s\n", infile);
108 printf("Algorithm: %" PRIu64 " %s\n", key->algorithm,
109 (key->algorithm < kNumAlgorithms ?
110 algo_strings[key->algorithm] : "(invalid)"));
111 printf("Version: %" PRIu64 "\n", key->key_version);
112
113 /* TODO: write key data, if any */
114
115 Free(key);
116 return 0;
117}
118
119
120int main(int argc, char* argv[]) {
121
122 char *infile = NULL;
123 char *outfile = NULL;
124 int mode = 0;
125 int parse_error = 0;
126 uint64_t version = 1;
127 uint64_t algorithm = kNumAlgorithms;
128 char* e;
129 int i;
130
131 while ((i = getopt_long(argc, argv, "", long_opts, NULL)) != -1) {
132 switch (i) {
133 case '?':
134 /* Unhandled option */
135 printf("Unknown option\n");
136 parse_error = 1;
137 break;
138
139 case OPT_IN:
140 infile = optarg;
141 break;
142
143 case OPT_OUT:
144 outfile = optarg;
145 break;
146
147 case OPT_KEY_VERSION:
148 version = strtoul(optarg, &e, 0);
149 if (!*optarg || (e && *e)) {
150 printf("Invalid --version\n");
151 parse_error = 1;
152 }
153 break;
154
155 case OPT_ALGORITHM:
156 algorithm = strtoul(optarg, &e, 0);
157 if (!*optarg || (e && *e)) {
158 printf("Invalid --algorithm\n");
159 parse_error = 1;
160 }
161 break;
162
163 case OPT_MODE_PACK:
164 case OPT_MODE_UNPACK:
165 mode = i;
166 break;
167 }
168 }
169
170 if (parse_error)
171 return PrintHelp();
172
173 switch(mode) {
174 case OPT_MODE_PACK:
175 return Pack(infile, outfile, algorithm, version);
176 case OPT_MODE_UNPACK:
177 return Unpack(infile, outfile);
178 default:
179 printf("Must specify a mode.\n");
180 return PrintHelp();
181 }
182}