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