Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | 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 | 2 * Use of this source code is governed by a BSD-style license that can be |
| 3 * found in the LICENSE file. | 3 * found in the LICENSE file. |
| 4 * | 4 * |
| 5 * Developer file-signing utility | 5 * Developer file-signing utility |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include <errno.h> | 8 #include <errno.h> |
| 9 #include <getopt.h> | 9 #include <getopt.h> |
| 10 #include <inttypes.h> /* For PRIu64 */ | 10 #include <inttypes.h> /* For PRIu64 */ |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 40 {"verify", 1, 0, OPT_MODE_VERIFY }, | 40 {"verify", 1, 0, OPT_MODE_VERIFY }, |
| 41 {"keyblock", 1, 0, OPT_KEYBLOCK }, | 41 {"keyblock", 1, 0, OPT_KEYBLOCK }, |
| 42 {"signprivate", 1, 0, OPT_SIGNPRIVATE }, | 42 {"signprivate", 1, 0, OPT_SIGNPRIVATE }, |
| 43 {"vblock", 1, 0, OPT_VBLOCK }, | 43 {"vblock", 1, 0, OPT_VBLOCK }, |
| 44 {"debug", 0, &opt_debug, 1 }, | 44 {"debug", 0, &opt_debug, 1 }, |
| 45 {NULL, 0, 0, 0} | 45 {NULL, 0, 0, 0} |
| 46 }; | 46 }; |
| 47 | 47 |
| 48 | 48 |
| 49 /* Print help and return error */ | 49 /* Print help and return error */ |
| 50 static int PrintHelp(char *progname) { | 50 static int PrintHelp(const char *progname) { |
| 51 fprintf(stderr, | 51 fprintf(stderr, |
| 52 "This program is used to sign and verify developer-mode files\n"); | 52 "This program is used to sign and verify developer-mode files\n"); |
| 53 fprintf(stderr, | 53 fprintf(stderr, |
| 54 "\n" | 54 "\n" |
| 55 "Usage: %s --sign <file> [PARAMETERS]\n" | 55 "Usage: %s --sign <file> [PARAMETERS]\n" |
| 56 "\n" | 56 "\n" |
| 57 " Required parameters:\n" | 57 " Required parameters:\n" |
| 58 " --keyblock <file> Key block in .keyblock format\n" | 58 " --keyblock <file> Key block in .keyblock format\n" |
| 59 " --signprivate <file>" | 59 " --signprivate <file>" |
| 60 " Private key to sign file data, in .vbprivk format\n" | 60 " Private key to sign file data, in .vbprivk format\n" |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 81 fprintf(stderr, "DEBUG: "); | 81 fprintf(stderr, "DEBUG: "); |
| 82 vfprintf(stderr, format, ap); | 82 vfprintf(stderr, format, ap); |
| 83 va_end(ap); | 83 va_end(ap); |
| 84 } | 84 } |
| 85 | 85 |
| 86 | 86 |
| 87 /* Sign a file. We'll reuse the same structs used to sign kernels, to avoid | 87 /* Sign a file. We'll reuse the same structs used to sign kernels, to avoid |
| 88 having to declare yet another one for just this purpose. */ | 88 having to declare yet another one for just this purpose. */ |
| 89 static int Sign(const char* filename, const char* keyblock_file, | 89 static int Sign(const char* filename, const char* keyblock_file, |
| 90 const char* signprivate_file, const char* outfile) { | 90 const char* signprivate_file, const char* outfile) { |
| 91 uint8_t *file_data; | 91 uint8_t* file_data; |
| 92 uint64_t file_size; | 92 uint64_t file_size; |
| 93 VbKeyBlockHeader* key_block; | 93 VbKeyBlockHeader* key_block; |
| 94 uint64_t key_block_size; | 94 uint64_t key_block_size; |
| 95 VbPrivateKey* signing_key; | 95 VbPrivateKey* signing_key; |
| 96 VbSignature* body_sig; | 96 VbSignature* body_sig; |
| 97 VbKernelPreambleHeader* preamble; | 97 VbKernelPreambleHeader* preamble; |
| 98 FILE* f; | 98 FILE* output_fp; |
| 99 uint64_t i; | |
| 100 | 99 |
| 101 /* Read the file that we're going to sign. */ | 100 /* Read the file that we're going to sign. */ |
| 102 file_data = ReadFile(filename, &file_size); | 101 file_data = ReadFile(filename, &file_size); |
| 103 if (!file_data) { | 102 if (!file_data) { |
| 104 error("Error reading file to sign.\n"); | 103 error("Error reading file to sign.\n"); |
| 105 return 1; | 104 return 1; |
| 106 } | 105 } |
| 107 | 106 |
| 108 /* Get the key block and read the private key corresponding to it. */ | 107 /* Get the key block and read the private key corresponding to it. */ |
| 109 key_block = (VbKeyBlockHeader*)ReadFile(keyblock_file, &key_block_size); | 108 key_block = (VbKeyBlockHeader*)ReadFile(keyblock_file, &key_block_size); |
| 110 if (!key_block) { | 109 if (!key_block) { |
| 111 error("Error reading key block.\n"); | 110 error("Error reading key block.\n"); |
| 112 return 1; | 111 return 1; |
| 113 } | 112 } |
| 114 signing_key = PrivateKeyRead(signprivate_file); | 113 signing_key = PrivateKeyRead(signprivate_file); |
| 115 if (!signing_key) { | 114 if (!signing_key) { |
| 116 error("Error reading signing key.\n"); | 115 error("Error reading signing key.\n"); |
| 117 return 1; | 116 return 1; |
| 118 } | 117 } |
| 119 | 118 |
| 120 /* Sign the file data */ | 119 /* Sign the file data */ |
| 121 body_sig = CalculateSignature(file_data, file_size, signing_key); | 120 body_sig = CalculateSignature(file_data, file_size, signing_key); |
| 122 if (!body_sig) { | 121 if (!body_sig) { |
| 123 error("Error calculating body signature\n"); | 122 error("Error calculating body signature\n"); |
| 124 return 1; | 123 return 1; |
| 125 } | 124 } |
| 126 | 125 |
| 127 /* Create preamble */ | 126 /* Create preamble */ |
| 128 preamble = CreateKernelPreamble(0UL, 0UL, 0UL, 0UL, | 127 preamble = CreateKernelPreamble((uint64_t)0, |
| 129 body_sig, 0UL, signing_key); | 128 (uint64_t)0, |
| 129 (uint64_t)0, | |
| 130 (uint64_t)0, | |
| 131 body_sig, | |
| 132 (uint64_t)0, | |
| 133 signing_key); | |
| 130 if (!preamble) { | 134 if (!preamble) { |
| 131 error("Error creating preamble.\n"); | 135 error("Error creating preamble.\n"); |
| 132 return 1; | 136 return 1; |
| 133 } | 137 } |
| 134 | 138 |
| 135 /* Write the output file */ | 139 /* Write the output file */ |
| 136 Debug("writing %s...\n", outfile); | 140 Debug("writing %s...\n", outfile); |
| 137 f = fopen(outfile, "wb"); | 141 output_fp = fopen(outfile, "wb"); |
| 138 if (!f) { | 142 if (!output_fp) { |
| 139 error("Can't open output file %s\n", outfile); | 143 error("Can't open output file %s\n", outfile); |
| 140 return 1; | 144 return 1; |
| 141 } | 145 } |
| 142 Debug("0x%" PRIx64 " bytes of key_block\n", key_block_size); | 146 Debug("0x%" PRIx64 " bytes of key_block\n", key_block_size); |
| 143 Debug("0x%" PRIx64 " bytes of preamble\n", preamble->preamble_size); | 147 Debug("0x%" PRIx64 " bytes of preamble\n", preamble->preamble_size); |
| 144 i = ((1 != fwrite(key_block, key_block_size, 1, f)) || | 148 if ((1 != fwrite(key_block, key_block_size, 1, output_fp)) || |
| 145 (1 != fwrite(preamble, preamble->preamble_size, 1, f))); | 149 (1 != fwrite(preamble, preamble->preamble_size, 1, output_fp))) { |
| 146 if (i) { | |
| 147 error("Can't write output file %s\n", outfile); | 150 error("Can't write output file %s\n", outfile); |
| 148 fclose(f); | 151 fclose(output_fp); |
| 149 unlink(outfile); | 152 unlink(outfile); |
| 150 return 1; | 153 return 1; |
| 151 } | 154 } |
| 152 fclose(f); | 155 fclose(output_fp); |
| 153 | 156 |
| 154 /* Done */ | 157 /* Done */ |
| 155 Free(preamble); | 158 Free(preamble); |
| 156 Free(body_sig); | 159 Free(body_sig); |
| 157 Free(signing_key); | 160 Free(signing_key); |
| 158 Free(key_block); | 161 Free(key_block); |
| 159 Free(file_data); | 162 Free(file_data); |
| 160 | 163 |
| 161 /* Success */ | 164 /* Success */ |
| 162 return 0; | 165 return 0; |
| 163 } | 166 } |
| 164 | 167 |
| 165 static int Verify(const char* filename, const char* vblock_file) { | 168 static int Verify(const char* filename, const char* vblock_file) { |
| 166 uint8_t *file_data; | 169 uint8_t* file_data; |
| 167 uint64_t file_size; | 170 uint64_t file_size; |
| 168 uint8_t *buf; | 171 uint8_t* buf; |
| 169 uint64_t buf_size; | 172 uint64_t buf_size; |
| 170 VbKeyBlockHeader* key_block; | 173 VbKeyBlockHeader* key_block; |
| 171 VbKernelPreambleHeader* preamble; | 174 VbKernelPreambleHeader* preamble; |
| 172 VbPublicKey* data_key; | 175 VbPublicKey* data_key; |
| 173 RSAPublicKey* rsa; | 176 RSAPublicKey* rsa; |
| 174 uint64_t now = 0; | 177 uint64_t current_buf_offset = 0; |
| 175 | 178 |
| 176 /* Read the file that we're going to verify. */ | 179 /* Read the file that we're going to verify. */ |
| 177 file_data = ReadFile(filename, &file_size); | 180 file_data = ReadFile(filename, &file_size); |
| 178 if (!file_data) { | 181 if (!file_data) { |
| 179 error("Error reading file to sign.\n"); | 182 error("Error reading file to sign.\n"); |
| 180 return 1; | 183 return 1; |
| 181 } | 184 } |
| 182 | 185 |
| 183 /* Read the vblock that we're going to use on it */ | 186 /* Read the vblock that we're going to use on it */ |
| 184 buf = ReadFile(vblock_file, &buf_size); | 187 buf = ReadFile(vblock_file, &buf_size); |
| 185 if (!buf) { | 188 if (!buf) { |
| 186 error("Error reading vblock_file.\n"); | 189 error("Error reading vblock_file.\n"); |
| 187 return 1; | 190 return 1; |
| 188 } | 191 } |
| 189 | 192 |
| 190 /* Find the key block */ | 193 /* Find the key block */ |
| 191 key_block = (VbKeyBlockHeader*)buf; | 194 key_block = (VbKeyBlockHeader*)buf; |
| 192 Debug("Keyblock is 0x%" PRIx64 " bytes\n", key_block->key_block_size); | 195 Debug("Keyblock is 0x%" PRIx64 " bytes\n", key_block->key_block_size); |
| 193 now += key_block->key_block_size; | 196 current_buf_offset += key_block->key_block_size; |
| 194 if (now > buf_size) { | 197 if (current_buf_offset > buf_size) { |
| 195 error("key_block_size advances past the end of the buffer\n"); | 198 error("key_block_size advances past the end of the buffer\n"); |
| 196 return 1; | 199 return 1; |
| 197 } | 200 } |
| 198 | 201 |
| 199 /* Find the preamble */ | 202 /* Find the preamble */ |
| 200 preamble = (VbKernelPreambleHeader*)(buf + now); | 203 preamble = (VbKernelPreambleHeader*)(buf + current_buf_offset); |
| 201 Debug("Preamble is 0x%" PRIx64 " bytes\n", preamble->preamble_size); | 204 Debug("Preamble is 0x%" PRIx64 " bytes\n", preamble->preamble_size); |
| 202 now += preamble->preamble_size; | 205 current_buf_offset += preamble->preamble_size; |
| 203 if (now > buf_size ) { | 206 if (current_buf_offset > buf_size ) { |
| 204 error("preamble_size advances past the end of the buffer\n"); | 207 error("preamble_size advances past the end of the buffer\n"); |
| 205 return 1; | 208 return 1; |
| 206 } | 209 } |
| 207 | 210 |
| 208 Debug("Now is at 0x%" PRIx64 " bytes\n", now); | 211 Debug("Current buf offset is at 0x%" PRIx64 " bytes\n", current_buf_offset); |
| 209 | 212 |
| 210 /* Check the keyblock */ | 213 /* Check the keyblock */ |
| 211 if (0 != KeyBlockVerify(key_block, file_size, NULL)) { | 214 if (0 != KeyBlockVerify(key_block, file_size, NULL)) { |
| 212 error("Error verifying key block.\n"); | 215 error("Error verifying key block.\n"); |
| 213 return 1; | 216 return 1; |
| 214 } | 217 } |
| 215 | 218 |
| 216 printf("Key block:\n"); | 219 printf("Key block:\n"); |
| 217 data_key = &key_block->data_key; | 220 data_key = &key_block->data_key; |
| 218 //HEY printf(" Signature: %s\n", sign_key ? "valid" : "ignored"); | |
| 219 printf(" Size: 0x%" PRIx64 "\n", key_block->key_block_size); | 221 printf(" Size: 0x%" PRIx64 "\n", key_block->key_block_size); |
| 220 printf(" Data key algorithm: %" PRIu64 " %s\n", data_key->algorithm, | 222 printf(" Data key algorithm: %" PRIu64 " %s\n", data_key->algorithm, |
| 221 (data_key->algorithm < kNumAlgorithms ? | 223 (data_key->algorithm < kNumAlgorithms ? |
| 222 algo_strings[data_key->algorithm] : "(invalid)")); | 224 algo_strings[data_key->algorithm] : "(invalid)")); |
| 223 printf(" Data key version: %" PRIu64 "\n", data_key->key_version); | 225 printf(" Data key version: %" PRIu64 "\n", data_key->key_version); |
| 224 printf(" Flags: %" PRIu64 "\n", key_block->key_block_flags); | 226 printf(" Flags: %" PRIu64 "\n", key_block->key_block_flags); |
| 225 | 227 |
| 226 | 228 |
| 227 /* Verify preamble */ | 229 /* Verify preamble */ |
| 228 rsa = PublicKeyToRSA(&key_block->data_key); | 230 rsa = PublicKeyToRSA(&key_block->data_key); |
| 229 if (!rsa) { | 231 if (!rsa) { |
| 230 error("Error parsing data key.\n"); | 232 error("Error parsing data key.\n"); |
| 231 return 1; | 233 return 1; |
| 232 } | 234 } |
| 233 if (0 != VerifyKernelPreamble( | 235 if (0 != VerifyKernelPreamble(preamble, file_size, rsa)) { |
| 234 preamble, file_size, rsa)) { | |
| 235 error("Error verifying preamble.\n"); | 236 error("Error verifying preamble.\n"); |
| 236 return 1; | 237 return 1; |
| 237 } | 238 } |
| 238 | 239 |
| 239 printf("Preamble:\n"); | 240 printf("Preamble:\n"); |
| 240 printf(" Size: 0x%" PRIx64 "\n", preamble->preamble_size); | 241 printf(" Size: 0x%" PRIx64 "\n", preamble->preamble_size); |
| 241 printf(" Header version: %" PRIu32 ".%" PRIu32"\n", | 242 printf(" Header version: %" PRIu32 ".%" PRIu32"\n", |
| 242 preamble->header_version_major, preamble->header_version_minor); | 243 preamble->header_version_major, preamble->header_version_minor); |
| 243 printf(" Kernel version: %" PRIu64 "\n", preamble->kernel_version); | 244 printf(" Kernel version: %" PRIu64 "\n", preamble->kernel_version); |
| 244 printf(" Body load address: 0x%" PRIx64 "\n", preamble->body_load_address); | 245 printf(" Body load address: 0x%" PRIx64 "\n", preamble->body_load_address); |
| 245 printf(" Body size: 0x%" PRIx64 "\n", | 246 printf(" Body size: 0x%" PRIx64 "\n", |
| 246 preamble->body_signature.data_size); | 247 preamble->body_signature.data_size); |
| 247 printf(" Bootloader address: 0x%" PRIx64 "\n", | 248 printf(" Bootloader address: 0x%" PRIx64 "\n", |
| 248 preamble->bootloader_address); | 249 preamble->bootloader_address); |
| 249 printf(" Bootloader size: 0x%" PRIx64 "\n", preamble->bootloader_size); | 250 printf(" Bootloader size: 0x%" PRIx64 "\n", preamble->bootloader_size); |
| 250 | 251 |
| 251 /* Verify body */ | 252 /* Verify body */ |
| 252 if (0 != VerifyData(file_data, file_size, &preamble->body_signature, | 253 if (0 != VerifyData(file_data, file_size, &preamble->body_signature, rsa)) { |
| 253 rsa)) { | |
| 254 error("Error verifying kernel body.\n"); | 254 error("Error verifying kernel body.\n"); |
| 255 return 1; | 255 return 1; |
| 256 } | 256 } |
| 257 printf("Body verification succeeded.\n"); | 257 printf("Body verification succeeded.\n"); |
| 258 | 258 |
| 259 // HEY | |
| 260 return 0; | 259 return 0; |
| 261 } | 260 } |
| 262 | 261 |
| 263 | 262 |
| 264 int main(int argc, char* argv[]) { | 263 int main(int argc, char* argv[]) { |
| 265 char* filename = NULL; | 264 char* filename = NULL; |
| 266 char* keyblock_file = NULL; | 265 char* keyblock_file = NULL; |
| 267 char* signprivate_file = NULL; | 266 char* signprivate_file = NULL; |
| 268 char* vblock_file = NULL; | 267 char* vblock_file = NULL; |
| 269 int mode = 0; | 268 int mode = 0; |
| 270 int parse_error = 0; | 269 int parse_error = 0; |
| 271 int i; | 270 int option_index; |
| 272 | 271 |
| 273 char *progname = strrchr(argv[0], '/'); | 272 char *progname = strrchr(argv[0], '/'); |
| 274 if (progname) | 273 if (progname) |
| 275 progname++; | 274 progname++; |
| 276 else | 275 else |
| 277 progname = argv[0]; | 276 progname = argv[0]; |
| 278 | 277 |
| 279 while (((i = getopt_long(argc, argv, ":", long_opts, NULL)) != -1) && | 278 while ((option_index = getopt_long(argc, argv, ":", long_opts, NULL)) != -1 && |
| 280 !parse_error) { | 279 !parse_error) { |
| 281 switch (i) { | 280 switch (option_index) { |
| 282 default: | 281 default: |
| 283 case '?': | 282 case '?': |
| 284 /* Unhandled option */ | 283 /* Unhandled option */ |
| 285 parse_error = 1; | 284 parse_error = 1; |
| 286 break; | 285 break; |
| 287 | 286 |
| 288 case 0: | 287 case 0: |
| 289 /* silently handled option */ | 288 /* silently handled option */ |
| 290 break; | 289 break; |
| 291 | 290 |
| 292 case OPT_MODE_SIGN: | 291 case OPT_MODE_SIGN: |
| 293 case OPT_MODE_VERIFY: | 292 case OPT_MODE_VERIFY: |
| 294 if (mode && (mode != i)) { | 293 if (mode && (mode != option_index)) { |
| 295 fprintf(stderr, "Only a single mode can be specified\n"); | 294 fprintf(stderr, "Only a single mode can be specified\n"); |
| 296 parse_error = 1; | 295 parse_error = 1; |
| 297 break; | 296 break; |
| 298 } | 297 } |
| 299 mode = i; | 298 mode = option_index; |
| 300 filename = optarg; | 299 filename = optarg; |
| 301 break; | 300 break; |
| 302 | 301 |
| 303 case OPT_KEYBLOCK: | 302 case OPT_KEYBLOCK: |
| 304 keyblock_file = optarg; | 303 keyblock_file = optarg; |
| 305 break; | 304 break; |
| 306 | 305 |
| 307 case OPT_SIGNPRIVATE: | 306 case OPT_SIGNPRIVATE: |
| 308 signprivate_file = optarg; | 307 signprivate_file = optarg; |
| 309 break; | 308 break; |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 331 return PrintHelp(progname); | 330 return PrintHelp(progname); |
| 332 } | 331 } |
| 333 return Verify(filename, vblock_file); | 332 return Verify(filename, vblock_file); |
| 334 | 333 |
| 335 default: | 334 default: |
| 336 fprintf(stderr, | 335 fprintf(stderr, |
| 337 "You must specify either --sign or --verify\n"); | 336 "You must specify either --sign or --verify\n"); |
| 338 return PrintHelp(progname); | 337 return PrintHelp(progname); |
| 339 } | 338 } |
| 340 | 339 |
| 341 return 1; | 340 /* NOTREACHED */ |
|
adlr
2010/08/11 19:14:26
btw, i'm fine w/ having a return 1 here, which may
| |
| 342 } | 341 } |
| OLD | NEW |