tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

derdump.c (3290B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 #include "secutil.h"
      6 #include "nss.h"
      7 #include <errno.h>
      8 
      9 #if defined(XP_WIN) || (defined(__sun) && !defined(SVR4))
     10 #if !defined(WIN32)
     11 extern int fprintf(FILE *, char *, ...);
     12 #endif
     13 #endif
     14 #include "plgetopt.h"
     15 
     16 static void
     17 Usage(char *progName)
     18 {
     19    fprintf(stderr,
     20            "Usage: %s [-r] [-i input] [-o output]\n",
     21            progName);
     22    fprintf(stderr, "%-20s For formatted items, dump raw bytes as well\n",
     23            "-r");
     24    fprintf(stderr, "%-20s Define an input file to use (default is stdin)\n",
     25            "-i input");
     26    fprintf(stderr, "%-20s Define an output file to use (default is stdout)\n",
     27            "-o output");
     28    exit(-1);
     29 }
     30 
     31 int
     32 main(int argc, char **argv)
     33 {
     34    char *progName;
     35    FILE *outFile;
     36    PRFileDesc *inFile;
     37    SECItem der = { siBuffer, NULL, 0 };
     38    SECStatus rv;
     39    PRInt16 xp_error;
     40    PRBool raw = PR_FALSE;
     41    PLOptState *optstate;
     42    PLOptStatus status;
     43    int retval = -1;
     44 
     45    progName = strrchr(argv[0], '/');
     46    progName = progName ? progName + 1 : argv[0];
     47 
     48    /* Parse command line arguments */
     49    inFile = 0;
     50    outFile = 0;
     51    optstate = PL_CreateOptState(argc, argv, "i:o:r");
     52    while ((status = PL_GetNextOpt(optstate)) == PL_OPT_OK) {
     53        switch (optstate->option) {
     54            case 'i':
     55                inFile = PR_Open(optstate->value, PR_RDONLY, 0);
     56                if (!inFile) {
     57                    fprintf(stderr, "%s: unable to open \"%s\" for reading\n",
     58                            progName, optstate->value);
     59                    goto cleanup;
     60                }
     61                break;
     62 
     63            case 'o':
     64                outFile = fopen(optstate->value, "w");
     65                if (!outFile) {
     66                    fprintf(stderr, "%s: unable to open \"%s\" for writing\n",
     67                            progName, optstate->value);
     68                    goto cleanup;
     69                }
     70                break;
     71 
     72            case 'r':
     73                raw = PR_TRUE;
     74                break;
     75 
     76            default:
     77                Usage(progName);
     78                break;
     79        }
     80    }
     81    if (status == PL_OPT_BAD)
     82        Usage(progName);
     83 
     84    if (!inFile)
     85        inFile = PR_STDIN;
     86    if (!outFile)
     87        outFile = stdout;
     88 
     89    rv = NSS_NoDB_Init(NULL);
     90    if (rv != SECSuccess) {
     91        SECU_PrintPRandOSError(progName);
     92        goto cleanup;
     93    }
     94 
     95    rv = SECU_ReadDERFromFile(&der, inFile, PR_FALSE, PR_FALSE);
     96    if (rv == SECSuccess) {
     97        rv = DER_PrettyPrint(outFile, &der, raw);
     98        if (rv == SECSuccess) {
     99            retval = 0;
    100            goto cleanup;
    101        }
    102    }
    103 
    104    xp_error = PORT_GetError();
    105    if (xp_error) {
    106        SECU_PrintError(progName, "error %d", xp_error);
    107    }
    108    if (errno) {
    109        SECU_PrintSystemError(progName, "errno=%d", errno);
    110    }
    111    retval = 1;
    112 
    113 cleanup:
    114    retval |= NSS_Shutdown();
    115    if (inFile) {
    116        PR_Close(inFile);
    117    }
    118    if (outFile) {
    119        fflush(outFile);
    120        fclose(outFile);
    121    }
    122    PL_DestroyOptState(optstate);
    123    if (der.data) {
    124        PORT_Free(der.data);
    125    }
    126 
    127    return retval;
    128 }