modules/libmar/tool/mar.c

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
michael@0 3 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 #include <stdio.h>
michael@0 8 #include <stdlib.h>
michael@0 9 #include <string.h>
michael@0 10 #include "mar.h"
michael@0 11 #include "mar_cmdline.h"
michael@0 12
michael@0 13 #ifdef XP_WIN
michael@0 14 #include <windows.h>
michael@0 15 #include <direct.h>
michael@0 16 #define chdir _chdir
michael@0 17 #else
michael@0 18 #include <unistd.h>
michael@0 19 #endif
michael@0 20
michael@0 21 #if !defined(NO_SIGN_VERIFY) && (!defined(XP_WIN) || defined(MAR_NSS))
michael@0 22 int NSSInitCryptoContext(const char *NSSConfigDir);
michael@0 23 #endif
michael@0 24
michael@0 25 int mar_repackage_and_sign(const char *NSSConfigDir,
michael@0 26 const char * const *certNames,
michael@0 27 uint32_t certCount,
michael@0 28 const char *src,
michael@0 29 const char * dest);
michael@0 30
michael@0 31 static void print_version() {
michael@0 32 printf("Version: %s\n", MOZ_APP_VERSION);
michael@0 33 printf("Default Channel ID: %s\n", MAR_CHANNEL_ID);
michael@0 34 }
michael@0 35
michael@0 36 static void print_usage() {
michael@0 37 printf("usage:\n");
michael@0 38 printf("Create a MAR file:\n");
michael@0 39 printf(" mar [-H MARChannelID] [-V ProductVersion] [-C workingDir] "
michael@0 40 "-c archive.mar [files...]\n");
michael@0 41
michael@0 42 printf("Extract a MAR file:\n");
michael@0 43 printf(" mar [-C workingDir] -x archive.mar\n");
michael@0 44 #ifndef NO_SIGN_VERIFY
michael@0 45 printf("Sign a MAR file:\n");
michael@0 46 printf(" mar [-C workingDir] -d NSSConfigDir -n certname -s "
michael@0 47 "archive.mar out_signed_archive.mar\n");
michael@0 48
michael@0 49 printf("Strip a MAR signature:\n");
michael@0 50 printf(" mar [-C workingDir] -r "
michael@0 51 "signed_input_archive.mar output_archive.mar\n");
michael@0 52
michael@0 53 printf("Extract a MAR signature:\n");
michael@0 54 printf(" mar [-C workingDir] -n(i) -X "
michael@0 55 "signed_input_archive.mar base_64_encoded_signature_file\n");
michael@0 56
michael@0 57 printf("Import a MAR signature:\n");
michael@0 58 printf(" mar [-C workingDir] -n(i) -I "
michael@0 59 "signed_input_archive.mar base_64_encoded_signature_file "
michael@0 60 "changed_signed_output.mar\n");
michael@0 61 printf("(i) is the index of the certificate to extract\n");
michael@0 62 #if defined(XP_MACOSX) || (defined(XP_WIN) && !defined(MAR_NSS))
michael@0 63 printf("Verify a MAR file:\n");
michael@0 64 printf(" mar [-C workingDir] -D DERFilePath -v signed_archive.mar\n");
michael@0 65 printf("At most %d signature certificate DER files are specified by "
michael@0 66 "-D0 DERFilePath1 -D1 DERFilePath2, ...\n", MAX_SIGNATURES);
michael@0 67 #else
michael@0 68 printf("Verify a MAR file:\n");
michael@0 69 printf(" mar [-C workingDir] -d NSSConfigDir -n certname "
michael@0 70 "-v signed_archive.mar\n");
michael@0 71 printf("At most %d signature certificate names are specified by "
michael@0 72 "-n0 certName -n1 certName2, ...\n", MAX_SIGNATURES);
michael@0 73 #endif
michael@0 74 printf("At most %d verification certificate names are specified by "
michael@0 75 "-n0 certName -n1 certName2, ...\n", MAX_SIGNATURES);
michael@0 76 #endif
michael@0 77 printf("Print information on a MAR file:\n");
michael@0 78 printf(" mar -t archive.mar\n");
michael@0 79
michael@0 80 printf("Print detailed information on a MAR file including signatures:\n");
michael@0 81 printf(" mar -T archive.mar\n");
michael@0 82
michael@0 83 printf("Refresh the product information block of a MAR file:\n");
michael@0 84 printf(" mar [-H MARChannelID] [-V ProductVersion] [-C workingDir] "
michael@0 85 "-i unsigned_archive_to_refresh.mar\n");
michael@0 86
michael@0 87 printf("Print executable version:\n");
michael@0 88 printf(" mar --version\n");
michael@0 89 printf("This program does not handle unicode file paths properly\n");
michael@0 90 }
michael@0 91
michael@0 92 static int mar_test_callback(MarFile *mar,
michael@0 93 const MarItem *item,
michael@0 94 void *unused) {
michael@0 95 printf("%u\t0%o\t%s\n", item->length, item->flags, item->name);
michael@0 96 return 0;
michael@0 97 }
michael@0 98
michael@0 99 static int mar_test(const char *path) {
michael@0 100 MarFile *mar;
michael@0 101
michael@0 102 mar = mar_open(path);
michael@0 103 if (!mar)
michael@0 104 return -1;
michael@0 105
michael@0 106 printf("SIZE\tMODE\tNAME\n");
michael@0 107 mar_enum_items(mar, mar_test_callback, NULL);
michael@0 108
michael@0 109 mar_close(mar);
michael@0 110 return 0;
michael@0 111 }
michael@0 112
michael@0 113 int main(int argc, char **argv) {
michael@0 114 char *NSSConfigDir = NULL;
michael@0 115 const char *certNames[MAX_SIGNATURES];
michael@0 116 char *MARChannelID = MAR_CHANNEL_ID;
michael@0 117 char *productVersion = MOZ_APP_VERSION;
michael@0 118 uint32_t i, k;
michael@0 119 int rv = -1;
michael@0 120 uint32_t certCount = 0;
michael@0 121 int32_t sigIndex = -1;
michael@0 122
michael@0 123 #if defined(XP_WIN) && !defined(MAR_NSS) && !defined(NO_SIGN_VERIFY)
michael@0 124 HANDLE certFile;
michael@0 125 uint8_t *certBuffers[MAX_SIGNATURES];
michael@0 126 #endif
michael@0 127 #if !defined(NO_SIGN_VERIFY) && ((!defined(MAR_NSS) && defined(XP_WIN)) || \
michael@0 128 defined(XP_MACOSX))
michael@0 129 char* DERFilePaths[MAX_SIGNATURES];
michael@0 130 uint32_t fileSizes[MAX_SIGNATURES];
michael@0 131 uint32_t read;
michael@0 132 #endif
michael@0 133
michael@0 134 memset(certNames, 0, sizeof(certNames));
michael@0 135 #if defined(XP_WIN) && !defined(MAR_NSS) && !defined(NO_SIGN_VERIFY)
michael@0 136 memset(certBuffers, 0, sizeof(certBuffers));
michael@0 137 #endif
michael@0 138 #if !defined(NO_SIGN_VERIFY) && ((!defined(MAR_NSS) && defined(XP_WIN)) || \
michael@0 139 defined(XP_MACOSX))
michael@0 140 memset(DERFilePaths, 0, sizeof(DERFilePaths));
michael@0 141 memset(fileSizes, 0, sizeof(fileSizes));
michael@0 142 #endif
michael@0 143
michael@0 144 if (argc > 1 && 0 == strcmp(argv[1], "--version")) {
michael@0 145 print_version();
michael@0 146 return 0;
michael@0 147 }
michael@0 148
michael@0 149 if (argc < 3) {
michael@0 150 print_usage();
michael@0 151 return -1;
michael@0 152 }
michael@0 153
michael@0 154 while (argc > 0) {
michael@0 155 if (argv[1][0] == '-' && (argv[1][1] == 'c' ||
michael@0 156 argv[1][1] == 't' || argv[1][1] == 'x' ||
michael@0 157 argv[1][1] == 'v' || argv[1][1] == 's' ||
michael@0 158 argv[1][1] == 'i' || argv[1][1] == 'T' ||
michael@0 159 argv[1][1] == 'r' || argv[1][1] == 'X' ||
michael@0 160 argv[1][1] == 'I')) {
michael@0 161 break;
michael@0 162 /* -C workingdirectory */
michael@0 163 } else if (argv[1][0] == '-' && argv[1][1] == 'C') {
michael@0 164 chdir(argv[2]);
michael@0 165 argv += 2;
michael@0 166 argc -= 2;
michael@0 167 }
michael@0 168 #if !defined(NO_SIGN_VERIFY) && ((!defined(MAR_NSS) && defined(XP_WIN)) || \
michael@0 169 defined(XP_MACOSX))
michael@0 170 /* -D DERFilePath, also matches -D[index] DERFilePath
michael@0 171 We allow an index for verifying to be symmetric
michael@0 172 with the import and export command line arguments. */
michael@0 173 else if (argv[1][0] == '-' &&
michael@0 174 argv[1][1] == 'D' &&
michael@0 175 (argv[1][2] == (char)('0' + certCount) || argv[1][2] == '\0')) {
michael@0 176 if (certCount >= MAX_SIGNATURES) {
michael@0 177 print_usage();
michael@0 178 return -1;
michael@0 179 }
michael@0 180 DERFilePaths[certCount++] = argv[2];
michael@0 181 argv += 2;
michael@0 182 argc -= 2;
michael@0 183 }
michael@0 184 #endif
michael@0 185 /* -d NSSConfigdir */
michael@0 186 else if (argv[1][0] == '-' && argv[1][1] == 'd') {
michael@0 187 NSSConfigDir = argv[2];
michael@0 188 argv += 2;
michael@0 189 argc -= 2;
michael@0 190 /* -n certName, also matches -n[index] certName
michael@0 191 We allow an index for verifying to be symmetric
michael@0 192 with the import and export command line arguments. */
michael@0 193 } else if (argv[1][0] == '-' &&
michael@0 194 argv[1][1] == 'n' &&
michael@0 195 (argv[1][2] == (char)('0' + certCount) ||
michael@0 196 argv[1][2] == '\0' ||
michael@0 197 !strcmp(argv[2], "-X") ||
michael@0 198 !strcmp(argv[2], "-I"))) {
michael@0 199 if (certCount >= MAX_SIGNATURES) {
michael@0 200 print_usage();
michael@0 201 return -1;
michael@0 202 }
michael@0 203 certNames[certCount++] = argv[2];
michael@0 204 if (strlen(argv[1]) > 2 &&
michael@0 205 (!strcmp(argv[2], "-X") || !strcmp(argv[2], "-I")) &&
michael@0 206 argv[1][2] >= '0' && argv[1][2] <= '9') {
michael@0 207 sigIndex = argv[1][2] - '0';
michael@0 208 argv++;
michael@0 209 argc--;
michael@0 210 } else {
michael@0 211 argv += 2;
michael@0 212 argc -= 2;
michael@0 213 }
michael@0 214 /* MAR channel ID */
michael@0 215 } else if (argv[1][0] == '-' && argv[1][1] == 'H') {
michael@0 216 MARChannelID = argv[2];
michael@0 217 argv += 2;
michael@0 218 argc -= 2;
michael@0 219 /* Product Version */
michael@0 220 } else if (argv[1][0] == '-' && argv[1][1] == 'V') {
michael@0 221 productVersion = argv[2];
michael@0 222 argv += 2;
michael@0 223 argc -= 2;
michael@0 224 }
michael@0 225 else {
michael@0 226 print_usage();
michael@0 227 return -1;
michael@0 228 }
michael@0 229 }
michael@0 230
michael@0 231 if (argv[1][0] != '-') {
michael@0 232 print_usage();
michael@0 233 return -1;
michael@0 234 }
michael@0 235
michael@0 236 switch (argv[1][1]) {
michael@0 237 case 'c': {
michael@0 238 struct ProductInformationBlock infoBlock;
michael@0 239 infoBlock.MARChannelID = MARChannelID;
michael@0 240 infoBlock.productVersion = productVersion;
michael@0 241 return mar_create(argv[2], argc - 3, argv + 3, &infoBlock);
michael@0 242 }
michael@0 243 case 'i': {
michael@0 244 struct ProductInformationBlock infoBlock;
michael@0 245 infoBlock.MARChannelID = MARChannelID;
michael@0 246 infoBlock.productVersion = productVersion;
michael@0 247 return refresh_product_info_block(argv[2], &infoBlock);
michael@0 248 }
michael@0 249 case 'T': {
michael@0 250 struct ProductInformationBlock infoBlock;
michael@0 251 uint32_t numSignatures, numAdditionalBlocks;
michael@0 252 int hasSignatureBlock, hasAdditionalBlock;
michael@0 253 if (!get_mar_file_info(argv[2],
michael@0 254 &hasSignatureBlock,
michael@0 255 &numSignatures,
michael@0 256 &hasAdditionalBlock,
michael@0 257 NULL, &numAdditionalBlocks)) {
michael@0 258 if (hasSignatureBlock) {
michael@0 259 printf("Signature block found with %d signature%s\n",
michael@0 260 numSignatures,
michael@0 261 numSignatures != 1 ? "s" : "");
michael@0 262 }
michael@0 263 if (hasAdditionalBlock) {
michael@0 264 printf("%d additional block%s found:\n",
michael@0 265 numAdditionalBlocks,
michael@0 266 numAdditionalBlocks != 1 ? "s" : "");
michael@0 267 }
michael@0 268
michael@0 269 rv = read_product_info_block(argv[2], &infoBlock);
michael@0 270 if (!rv) {
michael@0 271 printf(" - Product Information Block:\n");
michael@0 272 printf(" - MAR channel name: %s\n"
michael@0 273 " - Product version: %s\n",
michael@0 274 infoBlock.MARChannelID,
michael@0 275 infoBlock.productVersion);
michael@0 276 free((void *)infoBlock.MARChannelID);
michael@0 277 free((void *)infoBlock.productVersion);
michael@0 278 }
michael@0 279 }
michael@0 280 printf("\n");
michael@0 281 /* The fall through from 'T' to 't' is intentional */
michael@0 282 }
michael@0 283 case 't':
michael@0 284 return mar_test(argv[2]);
michael@0 285
michael@0 286 /* Extract a MAR file */
michael@0 287 case 'x':
michael@0 288 return mar_extract(argv[2]);
michael@0 289
michael@0 290 #ifndef NO_SIGN_VERIFY
michael@0 291 /* Extract a MAR signature */
michael@0 292 case 'X':
michael@0 293 if (sigIndex == -1) {
michael@0 294 fprintf(stderr, "ERROR: Signature index was not passed.\n");
michael@0 295 return -1;
michael@0 296 }
michael@0 297 if (sigIndex >= MAX_SIGNATURES || sigIndex < -1) {
michael@0 298 fprintf(stderr, "ERROR: Signature index is out of range: %d.\n",
michael@0 299 sigIndex);
michael@0 300 return -1;
michael@0 301 }
michael@0 302 return extract_signature(argv[2], sigIndex, argv[3]);
michael@0 303
michael@0 304 /* Import a MAR signature */
michael@0 305 case 'I':
michael@0 306 if (sigIndex == -1) {
michael@0 307 fprintf(stderr, "ERROR: signature index was not passed.\n");
michael@0 308 return -1;
michael@0 309 }
michael@0 310 if (sigIndex >= MAX_SIGNATURES || sigIndex < -1) {
michael@0 311 fprintf(stderr, "ERROR: Signature index is out of range: %d.\n",
michael@0 312 sigIndex);
michael@0 313 return -1;
michael@0 314 }
michael@0 315 if (argc < 5) {
michael@0 316 print_usage();
michael@0 317 return -1;
michael@0 318 }
michael@0 319 return import_signature(argv[2], sigIndex, argv[3], argv[4]);
michael@0 320
michael@0 321 case 'v':
michael@0 322
michael@0 323 #if defined(XP_WIN) && !defined(MAR_NSS)
michael@0 324 if (certCount == 0) {
michael@0 325 print_usage();
michael@0 326 return -1;
michael@0 327 }
michael@0 328
michael@0 329 for (k = 0; k < certCount; ++k) {
michael@0 330 /* If the mar program was built using CryptoAPI, then read in the buffer
michael@0 331 containing the cert from disk. */
michael@0 332 certFile = CreateFileA(DERFilePaths[k], GENERIC_READ,
michael@0 333 FILE_SHARE_READ |
michael@0 334 FILE_SHARE_WRITE |
michael@0 335 FILE_SHARE_DELETE,
michael@0 336 NULL,
michael@0 337 OPEN_EXISTING,
michael@0 338 0, NULL);
michael@0 339 if (INVALID_HANDLE_VALUE == certFile) {
michael@0 340 return -1;
michael@0 341 }
michael@0 342 fileSizes[k] = GetFileSize(certFile, NULL);
michael@0 343 certBuffers[k] = malloc(fileSizes[k]);
michael@0 344 if (!ReadFile(certFile, certBuffers[k], fileSizes[k], &read, NULL) ||
michael@0 345 fileSizes[k] != read) {
michael@0 346 CloseHandle(certFile);
michael@0 347 for (i = 0; i <= k; i++) {
michael@0 348 free(certBuffers[i]);
michael@0 349 }
michael@0 350 return -1;
michael@0 351 }
michael@0 352 CloseHandle(certFile);
michael@0 353 }
michael@0 354
michael@0 355 rv = mar_verify_signatures(argv[2], certBuffers, fileSizes,
michael@0 356 NULL, certCount);
michael@0 357 for (k = 0; k < certCount; ++k) {
michael@0 358 free(certBuffers[k]);
michael@0 359 }
michael@0 360 if (rv) {
michael@0 361 /* Determine if the source MAR file has the new fields for signing */
michael@0 362 int hasSignatureBlock;
michael@0 363 if (get_mar_file_info(argv[2], &hasSignatureBlock,
michael@0 364 NULL, NULL, NULL, NULL)) {
michael@0 365 fprintf(stderr, "ERROR: could not determine if MAR is old or new.\n");
michael@0 366 } else if (!hasSignatureBlock) {
michael@0 367 fprintf(stderr, "ERROR: The MAR file is in the old format so has"
michael@0 368 " no signature to verify.\n");
michael@0 369 }
michael@0 370 return -1;
michael@0 371 }
michael@0 372
michael@0 373 return 0;
michael@0 374
michael@0 375 #elif defined(XP_MACOSX)
michael@0 376 return mar_verify_signatures(argv[2], (const uint8_t* const*)DERFilePaths,
michael@0 377 0, NULL, certCount);
michael@0 378 #else
michael@0 379 if (!NSSConfigDir || certCount == 0) {
michael@0 380 print_usage();
michael@0 381 return -1;
michael@0 382 }
michael@0 383
michael@0 384 if (NSSInitCryptoContext(NSSConfigDir)) {
michael@0 385 fprintf(stderr, "ERROR: Could not initialize crypto library.\n");
michael@0 386 return -1;
michael@0 387 }
michael@0 388
michael@0 389 return mar_verify_signatures(argv[2], NULL, 0, certNames, certCount);
michael@0 390
michael@0 391 #endif /* defined(XP_WIN) && !defined(MAR_NSS) */
michael@0 392 case 's':
michael@0 393 if (!NSSConfigDir || certCount == 0 || argc < 4) {
michael@0 394 print_usage();
michael@0 395 return -1;
michael@0 396 }
michael@0 397 return mar_repackage_and_sign(NSSConfigDir, certNames, certCount,
michael@0 398 argv[2], argv[3]);
michael@0 399
michael@0 400 case 'r':
michael@0 401 return strip_signature_block(argv[2], argv[3]);
michael@0 402 #endif /* endif NO_SIGN_VERIFY disabled */
michael@0 403
michael@0 404 default:
michael@0 405 print_usage();
michael@0 406 return -1;
michael@0 407 }
michael@0 408
michael@0 409 return 0;
michael@0 410 }

mercurial