Randall Spangler | 32a6526 | 2011-06-27 10:49:11 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2011 The Chromium OS Authors. All rights reserved. |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 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 Spangler | d55c645 | 2010-06-10 12:43:51 -0700 | [diff] [blame] | 8 | #include <getopt.h> |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 9 | #include <inttypes.h> /* For PRIu64 */ |
Bill Richardson | abf0550 | 2010-07-01 10:22:06 -0700 | [diff] [blame] | 10 | #include <stdarg.h> |
Randall Spangler | d55c645 | 2010-06-10 12:43:51 -0700 | [diff] [blame] | 11 | #include <stdio.h> |
| 12 | #include <stdlib.h> |
Bill Richardson | abf0550 | 2010-07-01 10:22:06 -0700 | [diff] [blame] | 13 | #include <string.h> |
Randall Spangler | d55c645 | 2010-06-10 12:43:51 -0700 | [diff] [blame] | 14 | |
| 15 | #include "cryptolib.h" |
Bill Richardson | b84b81d | 2014-07-14 14:48:31 -0700 | [diff] [blame] | 16 | #include "futility.h" |
Randall Spangler | d55c645 | 2010-06-10 12:43:51 -0700 | [diff] [blame] | 17 | #include "host_common.h" |
Bill Richardson | 7829902 | 2014-06-20 14:33:00 -0700 | [diff] [blame] | 18 | #include "util_misc.h" |
Randall Spangler | d55c645 | 2010-06-10 12:43:51 -0700 | [diff] [blame] | 19 | #include "vboot_common.h" |
| 20 | |
Randall Spangler | d55c645 | 2010-06-10 12:43:51 -0700 | [diff] [blame] | 21 | /* Command line options */ |
| 22 | enum { |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 23 | OPT_INKEY = 1000, |
| 24 | OPT_KEY_VERSION, |
| 25 | OPT_ALGORITHM, |
| 26 | OPT_MODE_PACK, |
| 27 | OPT_MODE_UNPACK, |
| 28 | OPT_COPYTO, |
Randall Spangler | d55c645 | 2010-06-10 12:43:51 -0700 | [diff] [blame] | 29 | }; |
| 30 | |
Mike Frysinger | 7351ed7 | 2014-08-18 10:47:42 -0400 | [diff] [blame] | 31 | static const struct option long_opts[] = { |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 32 | {"key", 1, 0, OPT_INKEY}, |
| 33 | {"version", 1, 0, OPT_KEY_VERSION}, |
| 34 | {"algorithm", 1, 0, OPT_ALGORITHM}, |
| 35 | {"pack", 1, 0, OPT_MODE_PACK}, |
| 36 | {"unpack", 1, 0, OPT_MODE_UNPACK}, |
| 37 | {"copyto", 1, 0, OPT_COPYTO}, |
| 38 | {NULL, 0, 0, 0} |
Randall Spangler | d55c645 | 2010-06-10 12:43:51 -0700 | [diff] [blame] | 39 | }; |
| 40 | |
Bill Richardson | 779796f | 2014-09-23 11:47:40 -0700 | [diff] [blame] | 41 | static void print_help(const char *progname) |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 42 | { |
| 43 | int i; |
Randall Spangler | d55c645 | 2010-06-10 12:43:51 -0700 | [diff] [blame] | 44 | |
Bill Richardson | 779796f | 2014-09-23 11:47:40 -0700 | [diff] [blame] | 45 | printf("\n" |
| 46 | "Usage: " MYNAME " %s --pack <outfile> [PARAMETERS]\n" |
| 47 | "\n" |
| 48 | " Required parameters:\n" |
| 49 | " --key <infile> RSA key file (.keyb or .pem)\n" |
| 50 | " --version <number> Key version number " |
| 51 | "(required for .keyb,\n" |
| 52 | " ignored for .pem)\n" |
| 53 | " --algorithm <number> " |
| 54 | "Signing algorithm to use with key:\n", progname); |
Randall Spangler | d55c645 | 2010-06-10 12:43:51 -0700 | [diff] [blame] | 55 | |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 56 | for (i = 0; i < kNumAlgorithms; i++) { |
Bill Richardson | 779796f | 2014-09-23 11:47:40 -0700 | [diff] [blame] | 57 | printf(" %d = (%s)\n", |
| 58 | i, algo_strings[i]); |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 59 | } |
Randall Spangler | d55c645 | 2010-06-10 12:43:51 -0700 | [diff] [blame] | 60 | |
Bill Richardson | 779796f | 2014-09-23 11:47:40 -0700 | [diff] [blame] | 61 | printf("\nOR\n\n" |
| 62 | "Usage: " MYNAME " %s --unpack <infile>\n" |
| 63 | "\n" |
| 64 | " Optional parameters:\n" |
| 65 | " --copyto <file> " |
| 66 | "Write a copy of the key to this file.\n\n", progname); |
Randall Spangler | d55c645 | 2010-06-10 12:43:51 -0700 | [diff] [blame] | 67 | } |
| 68 | |
Bill Richardson | abf0550 | 2010-07-01 10:22:06 -0700 | [diff] [blame] | 69 | /* Pack a .keyb file into a .vbpubk, or a .pem into a .vbprivk */ |
Randall Spangler | d55c645 | 2010-06-10 12:43:51 -0700 | [diff] [blame] | 70 | static int Pack(const char *infile, const char *outfile, uint64_t algorithm, |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 71 | uint64_t version) |
| 72 | { |
| 73 | VbPublicKey *pubkey; |
| 74 | VbPrivateKey *privkey; |
Randall Spangler | d55c645 | 2010-06-10 12:43:51 -0700 | [diff] [blame] | 75 | |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 76 | if (!infile || !outfile) { |
| 77 | fprintf(stderr, "vbutil_key: Must specify --in and --out\n"); |
| 78 | return 1; |
| 79 | } |
Randall Spangler | d55c645 | 2010-06-10 12:43:51 -0700 | [diff] [blame] | 80 | |
Bill Richardson | 779796f | 2014-09-23 11:47:40 -0700 | [diff] [blame] | 81 | pubkey = PublicKeyReadKeyb(infile, algorithm, version); |
| 82 | if (pubkey) { |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 83 | if (0 != PublicKeyWrite(outfile, pubkey)) { |
| 84 | fprintf(stderr, "vbutil_key: Error writing key.\n"); |
| 85 | return 1; |
| 86 | } |
| 87 | free(pubkey); |
| 88 | return 0; |
| 89 | } |
Randall Spangler | d55c645 | 2010-06-10 12:43:51 -0700 | [diff] [blame] | 90 | |
Bill Richardson | 779796f | 2014-09-23 11:47:40 -0700 | [diff] [blame] | 91 | privkey = PrivateKeyReadPem(infile, algorithm); |
| 92 | if (privkey) { |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 93 | if (0 != PrivateKeyWrite(outfile, privkey)) { |
| 94 | fprintf(stderr, "vbutil_key: Error writing key.\n"); |
| 95 | return 1; |
| 96 | } |
| 97 | free(privkey); |
| 98 | return 0; |
| 99 | } |
Randall Spangler | d55c645 | 2010-06-10 12:43:51 -0700 | [diff] [blame] | 100 | |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 101 | VbExError("Unable to parse either .keyb or .pem from %s\n", infile); |
| 102 | return 1; |
Randall Spangler | d55c645 | 2010-06-10 12:43:51 -0700 | [diff] [blame] | 103 | } |
| 104 | |
Bill Richardson | abf0550 | 2010-07-01 10:22:06 -0700 | [diff] [blame] | 105 | /* Unpack a .vbpubk or .vbprivk */ |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 106 | static int Unpack(const char *infile, const char *outfile) |
| 107 | { |
| 108 | VbPublicKey *pubkey; |
| 109 | VbPrivateKey *privkey; |
Randall Spangler | d55c645 | 2010-06-10 12:43:51 -0700 | [diff] [blame] | 110 | |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 111 | if (!infile) { |
| 112 | fprintf(stderr, "Need file to unpack\n"); |
| 113 | return 1; |
| 114 | } |
Randall Spangler | d55c645 | 2010-06-10 12:43:51 -0700 | [diff] [blame] | 115 | |
Bill Richardson | 779796f | 2014-09-23 11:47:40 -0700 | [diff] [blame] | 116 | pubkey = PublicKeyRead(infile); |
| 117 | if (pubkey) { |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 118 | printf("Public Key file: %s\n", infile); |
| 119 | printf("Algorithm: %" PRIu64 " %s\n", pubkey->algorithm, |
| 120 | (pubkey->algorithm < kNumAlgorithms ? |
| 121 | algo_strings[pubkey->algorithm] : "(invalid)")); |
| 122 | printf("Key Version: %" PRIu64 "\n", pubkey->key_version); |
| 123 | printf("Key sha1sum: "); |
| 124 | PrintPubKeySha1Sum(pubkey); |
| 125 | printf("\n"); |
| 126 | if (outfile) { |
| 127 | if (0 != PublicKeyWrite(outfile, pubkey)) { |
| 128 | fprintf(stderr, |
| 129 | "vbutil_key: Error writing key copy\n"); |
| 130 | free(pubkey); |
| 131 | return 1; |
| 132 | } |
| 133 | } |
| 134 | free(pubkey); |
| 135 | return 0; |
| 136 | } |
Randall Spangler | d55c645 | 2010-06-10 12:43:51 -0700 | [diff] [blame] | 137 | |
Bill Richardson | 779796f | 2014-09-23 11:47:40 -0700 | [diff] [blame] | 138 | privkey = PrivateKeyRead(infile); |
| 139 | if (privkey) { |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 140 | printf("Private Key file: %s\n", infile); |
| 141 | printf("Algorithm: %" PRIu64 " %s\n", |
| 142 | privkey->algorithm, |
| 143 | (privkey->algorithm < |
| 144 | kNumAlgorithms ? algo_strings[privkey-> |
| 145 | algorithm] : |
| 146 | "(invalid)")); |
| 147 | if (outfile) { |
| 148 | if (0 != PrivateKeyWrite(outfile, privkey)) { |
| 149 | fprintf(stderr, |
| 150 | "vbutil_key: Error writing key copy\n"); |
| 151 | free(privkey); |
| 152 | return 1; |
| 153 | } |
| 154 | } |
| 155 | free(privkey); |
| 156 | return 0; |
| 157 | } |
Bill Richardson | abf0550 | 2010-07-01 10:22:06 -0700 | [diff] [blame] | 158 | |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 159 | VbExError("Unable to parse either .vbpubk or vbprivk from %s\n", |
| 160 | infile); |
| 161 | return 1; |
Randall Spangler | d55c645 | 2010-06-10 12:43:51 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 164 | static int do_vbutil_key(int argc, char *argv[]) |
| 165 | { |
Randall Spangler | d55c645 | 2010-06-10 12:43:51 -0700 | [diff] [blame] | 166 | |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 167 | char *infile = NULL; |
| 168 | char *outfile = NULL; |
| 169 | int mode = 0; |
| 170 | int parse_error = 0; |
| 171 | uint64_t version = 1; |
| 172 | uint64_t algorithm = kNumAlgorithms; |
| 173 | char *e; |
| 174 | int i; |
Randall Spangler | d55c645 | 2010-06-10 12:43:51 -0700 | [diff] [blame] | 175 | |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 176 | while ((i = getopt_long(argc, argv, "", long_opts, NULL)) != -1) { |
| 177 | switch (i) { |
| 178 | case '?': |
| 179 | /* Unhandled option */ |
| 180 | VbExError("Unknown option\n"); |
| 181 | parse_error = 1; |
| 182 | break; |
Bill Richardson | abf0550 | 2010-07-01 10:22:06 -0700 | [diff] [blame] | 183 | |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 184 | case OPT_INKEY: |
| 185 | infile = optarg; |
| 186 | break; |
Randall Spangler | d55c645 | 2010-06-10 12:43:51 -0700 | [diff] [blame] | 187 | |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 188 | case OPT_KEY_VERSION: |
| 189 | version = strtoul(optarg, &e, 0); |
| 190 | if (!*optarg || (e && *e)) { |
| 191 | VbExError("Invalid --version\n"); |
| 192 | parse_error = 1; |
| 193 | } |
| 194 | break; |
Randall Spangler | d55c645 | 2010-06-10 12:43:51 -0700 | [diff] [blame] | 195 | |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 196 | case OPT_ALGORITHM: |
| 197 | algorithm = strtoul(optarg, &e, 0); |
| 198 | if (!*optarg || (e && *e)) { |
| 199 | VbExError("Invalid --algorithm\n"); |
| 200 | parse_error = 1; |
| 201 | } |
| 202 | break; |
Randall Spangler | d55c645 | 2010-06-10 12:43:51 -0700 | [diff] [blame] | 203 | |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 204 | case OPT_MODE_PACK: |
| 205 | mode = i; |
| 206 | outfile = optarg; |
| 207 | break; |
Randall Spangler | d55c645 | 2010-06-10 12:43:51 -0700 | [diff] [blame] | 208 | |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 209 | case OPT_MODE_UNPACK: |
| 210 | mode = i; |
| 211 | infile = optarg; |
| 212 | break; |
Bill Richardson | abf0550 | 2010-07-01 10:22:06 -0700 | [diff] [blame] | 213 | |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 214 | case OPT_COPYTO: |
| 215 | outfile = optarg; |
| 216 | break; |
| 217 | } |
| 218 | } |
Bill Richardson | 8adcb43 | 2010-09-01 10:40:25 -0700 | [diff] [blame] | 219 | |
Bill Richardson | 779796f | 2014-09-23 11:47:40 -0700 | [diff] [blame] | 220 | if (parse_error) { |
| 221 | print_help(argv[0]); |
| 222 | return 1; |
| 223 | } |
Randall Spangler | d55c645 | 2010-06-10 12:43:51 -0700 | [diff] [blame] | 224 | |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 225 | switch (mode) { |
| 226 | case OPT_MODE_PACK: |
| 227 | return Pack(infile, outfile, algorithm, version); |
| 228 | case OPT_MODE_UNPACK: |
| 229 | return Unpack(infile, outfile); |
| 230 | default: |
| 231 | printf("Must specify a mode.\n"); |
Bill Richardson | 779796f | 2014-09-23 11:47:40 -0700 | [diff] [blame] | 232 | print_help(argv[0]); |
| 233 | return 1; |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 234 | } |
Randall Spangler | d55c645 | 2010-06-10 12:43:51 -0700 | [diff] [blame] | 235 | } |
Bill Richardson | b84b81d | 2014-07-14 14:48:31 -0700 | [diff] [blame] | 236 | |
| 237 | DECLARE_FUTIL_COMMAND(vbutil_key, do_vbutil_key, |
Bill Richardson | 1eae873 | 2015-02-05 12:36:15 -0800 | [diff] [blame] | 238 | VBOOT_VERSION_1_0, |
Bill Richardson | 779796f | 2014-09-23 11:47:40 -0700 | [diff] [blame] | 239 | "Wraps RSA keys with vboot headers", |
| 240 | print_help); |