security/nss/cmd/pp/pp.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/security/nss/cmd/pp/pp.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,187 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +/*
     1.9 + * Pretty-print some well-known BER or DER encoded data (e.g. certificates,
    1.10 + * keys, pkcs7)
    1.11 + */
    1.12 +
    1.13 +#include "secutil.h"
    1.14 +
    1.15 +#if defined(__sun) && !defined(SVR4)
    1.16 +extern int fprintf(FILE *, char *, ...);
    1.17 +#endif
    1.18 +
    1.19 +#include "plgetopt.h"
    1.20 +
    1.21 +#include "pk11func.h"
    1.22 +#include "nspr.h"
    1.23 +#include "nss.h"
    1.24 +
    1.25 +static void Usage(char *progName)
    1.26 +{
    1.27 +    fprintf(stderr,
    1.28 +	    "Usage:  %s [-t type] [-a] [-i input] [-o output] [-w] [-u]\n",
    1.29 +	    progName);
    1.30 +    fprintf(stderr, "Pretty prints a file containing ASN.1 data in DER or ascii format.\n");
    1.31 +    fprintf(stderr, "%-14s Specify input and display type: %s (sk),\n",
    1.32 +	    "-t type", SEC_CT_PRIVATE_KEY);
    1.33 +    fprintf(stderr, "%-14s %s (pk), %s (c), %s (cr),\n", "", SEC_CT_PUBLIC_KEY,
    1.34 +	    SEC_CT_CERTIFICATE, SEC_CT_CERTIFICATE_REQUEST);
    1.35 +    fprintf(stderr, "%-14s %s (ci), %s (p7), %s or %s (n).\n", "", SEC_CT_CERTIFICATE_ID,
    1.36 +            SEC_CT_PKCS7, SEC_CT_CRL, SEC_CT_NAME);
    1.37 +    fprintf(stderr, "%-14s (Use either the long type name or the shortcut.)\n", "", SEC_CT_CERTIFICATE_ID,
    1.38 +            SEC_CT_PKCS7, SEC_CT_CRL, SEC_CT_NAME);
    1.39 +    fprintf(stderr, "%-14s Input is in ascii encoded form (RFC1113)\n",
    1.40 +	    "-a");
    1.41 +    fprintf(stderr, "%-14s Define an input file to use (default is stdin)\n",
    1.42 +	    "-i input");
    1.43 +    fprintf(stderr, "%-14s Define an output file to use (default is stdout)\n",
    1.44 +	    "-o output");
    1.45 +    fprintf(stderr, "%-14s Don't wrap long output lines\n",
    1.46 +	    "-w");
    1.47 +    fprintf(stderr, "%-14s Use UTF-8 (default is to show non-ascii as .)\n",
    1.48 +	    "-u");
    1.49 +    exit(-1);
    1.50 +}
    1.51 +
    1.52 +int main(int argc, char **argv)
    1.53 +{
    1.54 +    int rv, ascii;
    1.55 +    char *progName;
    1.56 +    FILE *outFile;
    1.57 +    PRFileDesc *inFile;
    1.58 +    SECItem der, data;
    1.59 +    char *typeTag;
    1.60 +    PLOptState *optstate;
    1.61 +    PRBool wrap = PR_TRUE;
    1.62 +
    1.63 +    progName = strrchr(argv[0], '/');
    1.64 +    progName = progName ? progName+1 : argv[0];
    1.65 +
    1.66 +    ascii = 0;
    1.67 +    inFile = 0;
    1.68 +    outFile = 0;
    1.69 +    typeTag = 0;
    1.70 +    optstate = PL_CreateOptState(argc, argv, "at:i:o:uw");
    1.71 +    while ( PL_GetNextOpt(optstate) == PL_OPT_OK ) {
    1.72 +	switch (optstate->option) {
    1.73 +	  case '?':
    1.74 +	    Usage(progName);
    1.75 +	    break;
    1.76 +
    1.77 +	  case 'a':
    1.78 +	    ascii = 1;
    1.79 +	    break;
    1.80 +
    1.81 +	  case 'i':
    1.82 +	    inFile = PR_Open(optstate->value, PR_RDONLY, 0);
    1.83 +	    if (!inFile) {
    1.84 +		fprintf(stderr, "%s: unable to open \"%s\" for reading\n",
    1.85 +			progName, optstate->value);
    1.86 +		return -1;
    1.87 +	    }
    1.88 +	    break;
    1.89 +
    1.90 +	  case 'o':
    1.91 +	    outFile = fopen(optstate->value, "w");
    1.92 +	    if (!outFile) {
    1.93 +		fprintf(stderr, "%s: unable to open \"%s\" for writing\n",
    1.94 +			progName, optstate->value);
    1.95 +		return -1;
    1.96 +	    }
    1.97 +	    break;
    1.98 +
    1.99 +	  case 't':
   1.100 +	    typeTag = strdup(optstate->value);
   1.101 +	    break;
   1.102 +
   1.103 +	  case 'u':
   1.104 +	    SECU_EnableUtf8Display(PR_TRUE);
   1.105 +	    break;
   1.106 +
   1.107 +	  case 'w':
   1.108 +	    wrap = PR_FALSE;
   1.109 +	    break;
   1.110 +	}
   1.111 +    }
   1.112 +    PL_DestroyOptState(optstate);
   1.113 +    if (!typeTag) Usage(progName);
   1.114 +
   1.115 +    if (!inFile) inFile = PR_STDIN;
   1.116 +    if (!outFile) outFile = stdout;
   1.117 +
   1.118 +    PR_Init(PR_SYSTEM_THREAD, PR_PRIORITY_NORMAL, 1);
   1.119 +    rv = NSS_NoDB_Init(NULL);
   1.120 +    if (rv != SECSuccess) {
   1.121 +	fprintf(stderr, "%s: NSS_NoDB_Init failed (%s)\n",
   1.122 +		progName, SECU_Strerror(PORT_GetError()));
   1.123 +	exit(1);
   1.124 +    }
   1.125 +    SECU_RegisterDynamicOids();
   1.126 +
   1.127 +    rv = SECU_ReadDERFromFile(&der, inFile, ascii, PR_FALSE);
   1.128 +    if (rv != SECSuccess) {
   1.129 +	fprintf(stderr, "%s: SECU_ReadDERFromFile failed\n", progName);
   1.130 +	exit(1);
   1.131 +    }
   1.132 +
   1.133 +    /* Data is untyped, using the specified type */
   1.134 +    data.data = der.data;
   1.135 +    data.len = der.len;
   1.136 +
   1.137 +    SECU_EnableWrap(wrap);
   1.138 +
   1.139 +    /* Pretty print it */
   1.140 +    if (PORT_Strcmp(typeTag, SEC_CT_CERTIFICATE) == 0 ||
   1.141 +        PORT_Strcmp(typeTag, "c") == 0) {
   1.142 +	rv = SECU_PrintSignedData(outFile, &data, "Certificate", 0,
   1.143 +			     SECU_PrintCertificate);
   1.144 +    } else if (PORT_Strcmp(typeTag, SEC_CT_CERTIFICATE_ID) == 0 ||
   1.145 +               PORT_Strcmp(typeTag, "ci") == 0) {
   1.146 +        rv = SECU_PrintSignedContent(outFile, &data, 0, 0,
   1.147 +                                     SECU_PrintDumpDerIssuerAndSerial);
   1.148 +    } else if (PORT_Strcmp(typeTag, SEC_CT_CERTIFICATE_REQUEST) == 0 ||
   1.149 +               PORT_Strcmp(typeTag, "cr") == 0) {
   1.150 +	rv = SECU_PrintSignedData(outFile, &data, "Certificate Request", 0,
   1.151 +			     SECU_PrintCertificateRequest);
   1.152 +    } else if (PORT_Strcmp(typeTag, SEC_CT_CRL) == 0) {
   1.153 +	rv = SECU_PrintSignedData (outFile, &data, "CRL", 0, SECU_PrintCrl);
   1.154 +#ifdef HAVE_EPV_TEMPLATE
   1.155 +    } else if (PORT_Strcmp(typeTag, SEC_CT_PRIVATE_KEY) == 0 ||
   1.156 +               PORT_Strcmp(typeTag, "sk") == 0) {
   1.157 +	rv = SECU_PrintPrivateKey(outFile, &data, "Private Key", 0);
   1.158 +#endif
   1.159 +    } else if (PORT_Strcmp(typeTag, SEC_CT_PUBLIC_KEY) == 0 ||
   1.160 +               PORT_Strcmp (typeTag, "pk") == 0) {
   1.161 +	rv = SECU_PrintSubjectPublicKeyInfo(outFile, &data, "Public Key", 0);
   1.162 +    } else if (PORT_Strcmp(typeTag, SEC_CT_PKCS7) == 0 ||
   1.163 +               PORT_Strcmp (typeTag, "p7") == 0) {
   1.164 +	rv = SECU_PrintPKCS7ContentInfo(outFile, &data,
   1.165 +					"PKCS #7 Content Info", 0);
   1.166 +    } else if (PORT_Strcmp(typeTag, SEC_CT_NAME) == 0 ||
   1.167 +               PORT_Strcmp (typeTag, "n") == 0) {
   1.168 +	rv = SECU_PrintDERName(outFile, &data, "Name", 0);
   1.169 +    } else {
   1.170 +	fprintf(stderr, "%s: don't know how to print out '%s' files\n",
   1.171 +		progName, typeTag);
   1.172 +	SECU_PrintAny(outFile, &data, "File contains", 0);
   1.173 +	return -1;
   1.174 +    }
   1.175 +
   1.176 +    if (inFile != PR_STDIN)
   1.177 +	PR_Close(inFile);
   1.178 +    PORT_Free(der.data);
   1.179 +    if (rv) {
   1.180 +	fprintf(stderr, "%s: problem converting data (%s)\n",
   1.181 +		progName, SECU_Strerror(PORT_GetError()));
   1.182 +    }
   1.183 +    if (NSS_Shutdown() != SECSuccess) {
   1.184 +	fprintf(stderr, "%s: NSS_Shutdown failed (%s)\n",
   1.185 +		progName, SECU_Strerror(PORT_GetError()));
   1.186 +	rv = SECFailure;
   1.187 +    }
   1.188 +    PR_Cleanup();
   1.189 +    return rv;
   1.190 +}

mercurial