security/nss/cmd/pp/pp.c

Wed, 31 Dec 2014 07:16:47 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:16:47 +0100
branch
TOR_BUG_9701
changeset 3
141e0f1194b1
permissions
-rw-r--r--

Revert simplistic fix pending revisit of Mozilla integration attempt.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 /*
michael@0 6 * Pretty-print some well-known BER or DER encoded data (e.g. certificates,
michael@0 7 * keys, pkcs7)
michael@0 8 */
michael@0 9
michael@0 10 #include "secutil.h"
michael@0 11
michael@0 12 #if defined(__sun) && !defined(SVR4)
michael@0 13 extern int fprintf(FILE *, char *, ...);
michael@0 14 #endif
michael@0 15
michael@0 16 #include "plgetopt.h"
michael@0 17
michael@0 18 #include "pk11func.h"
michael@0 19 #include "nspr.h"
michael@0 20 #include "nss.h"
michael@0 21
michael@0 22 static void Usage(char *progName)
michael@0 23 {
michael@0 24 fprintf(stderr,
michael@0 25 "Usage: %s [-t type] [-a] [-i input] [-o output] [-w] [-u]\n",
michael@0 26 progName);
michael@0 27 fprintf(stderr, "Pretty prints a file containing ASN.1 data in DER or ascii format.\n");
michael@0 28 fprintf(stderr, "%-14s Specify input and display type: %s (sk),\n",
michael@0 29 "-t type", SEC_CT_PRIVATE_KEY);
michael@0 30 fprintf(stderr, "%-14s %s (pk), %s (c), %s (cr),\n", "", SEC_CT_PUBLIC_KEY,
michael@0 31 SEC_CT_CERTIFICATE, SEC_CT_CERTIFICATE_REQUEST);
michael@0 32 fprintf(stderr, "%-14s %s (ci), %s (p7), %s or %s (n).\n", "", SEC_CT_CERTIFICATE_ID,
michael@0 33 SEC_CT_PKCS7, SEC_CT_CRL, SEC_CT_NAME);
michael@0 34 fprintf(stderr, "%-14s (Use either the long type name or the shortcut.)\n", "", SEC_CT_CERTIFICATE_ID,
michael@0 35 SEC_CT_PKCS7, SEC_CT_CRL, SEC_CT_NAME);
michael@0 36 fprintf(stderr, "%-14s Input is in ascii encoded form (RFC1113)\n",
michael@0 37 "-a");
michael@0 38 fprintf(stderr, "%-14s Define an input file to use (default is stdin)\n",
michael@0 39 "-i input");
michael@0 40 fprintf(stderr, "%-14s Define an output file to use (default is stdout)\n",
michael@0 41 "-o output");
michael@0 42 fprintf(stderr, "%-14s Don't wrap long output lines\n",
michael@0 43 "-w");
michael@0 44 fprintf(stderr, "%-14s Use UTF-8 (default is to show non-ascii as .)\n",
michael@0 45 "-u");
michael@0 46 exit(-1);
michael@0 47 }
michael@0 48
michael@0 49 int main(int argc, char **argv)
michael@0 50 {
michael@0 51 int rv, ascii;
michael@0 52 char *progName;
michael@0 53 FILE *outFile;
michael@0 54 PRFileDesc *inFile;
michael@0 55 SECItem der, data;
michael@0 56 char *typeTag;
michael@0 57 PLOptState *optstate;
michael@0 58 PRBool wrap = PR_TRUE;
michael@0 59
michael@0 60 progName = strrchr(argv[0], '/');
michael@0 61 progName = progName ? progName+1 : argv[0];
michael@0 62
michael@0 63 ascii = 0;
michael@0 64 inFile = 0;
michael@0 65 outFile = 0;
michael@0 66 typeTag = 0;
michael@0 67 optstate = PL_CreateOptState(argc, argv, "at:i:o:uw");
michael@0 68 while ( PL_GetNextOpt(optstate) == PL_OPT_OK ) {
michael@0 69 switch (optstate->option) {
michael@0 70 case '?':
michael@0 71 Usage(progName);
michael@0 72 break;
michael@0 73
michael@0 74 case 'a':
michael@0 75 ascii = 1;
michael@0 76 break;
michael@0 77
michael@0 78 case 'i':
michael@0 79 inFile = PR_Open(optstate->value, PR_RDONLY, 0);
michael@0 80 if (!inFile) {
michael@0 81 fprintf(stderr, "%s: unable to open \"%s\" for reading\n",
michael@0 82 progName, optstate->value);
michael@0 83 return -1;
michael@0 84 }
michael@0 85 break;
michael@0 86
michael@0 87 case 'o':
michael@0 88 outFile = fopen(optstate->value, "w");
michael@0 89 if (!outFile) {
michael@0 90 fprintf(stderr, "%s: unable to open \"%s\" for writing\n",
michael@0 91 progName, optstate->value);
michael@0 92 return -1;
michael@0 93 }
michael@0 94 break;
michael@0 95
michael@0 96 case 't':
michael@0 97 typeTag = strdup(optstate->value);
michael@0 98 break;
michael@0 99
michael@0 100 case 'u':
michael@0 101 SECU_EnableUtf8Display(PR_TRUE);
michael@0 102 break;
michael@0 103
michael@0 104 case 'w':
michael@0 105 wrap = PR_FALSE;
michael@0 106 break;
michael@0 107 }
michael@0 108 }
michael@0 109 PL_DestroyOptState(optstate);
michael@0 110 if (!typeTag) Usage(progName);
michael@0 111
michael@0 112 if (!inFile) inFile = PR_STDIN;
michael@0 113 if (!outFile) outFile = stdout;
michael@0 114
michael@0 115 PR_Init(PR_SYSTEM_THREAD, PR_PRIORITY_NORMAL, 1);
michael@0 116 rv = NSS_NoDB_Init(NULL);
michael@0 117 if (rv != SECSuccess) {
michael@0 118 fprintf(stderr, "%s: NSS_NoDB_Init failed (%s)\n",
michael@0 119 progName, SECU_Strerror(PORT_GetError()));
michael@0 120 exit(1);
michael@0 121 }
michael@0 122 SECU_RegisterDynamicOids();
michael@0 123
michael@0 124 rv = SECU_ReadDERFromFile(&der, inFile, ascii, PR_FALSE);
michael@0 125 if (rv != SECSuccess) {
michael@0 126 fprintf(stderr, "%s: SECU_ReadDERFromFile failed\n", progName);
michael@0 127 exit(1);
michael@0 128 }
michael@0 129
michael@0 130 /* Data is untyped, using the specified type */
michael@0 131 data.data = der.data;
michael@0 132 data.len = der.len;
michael@0 133
michael@0 134 SECU_EnableWrap(wrap);
michael@0 135
michael@0 136 /* Pretty print it */
michael@0 137 if (PORT_Strcmp(typeTag, SEC_CT_CERTIFICATE) == 0 ||
michael@0 138 PORT_Strcmp(typeTag, "c") == 0) {
michael@0 139 rv = SECU_PrintSignedData(outFile, &data, "Certificate", 0,
michael@0 140 SECU_PrintCertificate);
michael@0 141 } else if (PORT_Strcmp(typeTag, SEC_CT_CERTIFICATE_ID) == 0 ||
michael@0 142 PORT_Strcmp(typeTag, "ci") == 0) {
michael@0 143 rv = SECU_PrintSignedContent(outFile, &data, 0, 0,
michael@0 144 SECU_PrintDumpDerIssuerAndSerial);
michael@0 145 } else if (PORT_Strcmp(typeTag, SEC_CT_CERTIFICATE_REQUEST) == 0 ||
michael@0 146 PORT_Strcmp(typeTag, "cr") == 0) {
michael@0 147 rv = SECU_PrintSignedData(outFile, &data, "Certificate Request", 0,
michael@0 148 SECU_PrintCertificateRequest);
michael@0 149 } else if (PORT_Strcmp(typeTag, SEC_CT_CRL) == 0) {
michael@0 150 rv = SECU_PrintSignedData (outFile, &data, "CRL", 0, SECU_PrintCrl);
michael@0 151 #ifdef HAVE_EPV_TEMPLATE
michael@0 152 } else if (PORT_Strcmp(typeTag, SEC_CT_PRIVATE_KEY) == 0 ||
michael@0 153 PORT_Strcmp(typeTag, "sk") == 0) {
michael@0 154 rv = SECU_PrintPrivateKey(outFile, &data, "Private Key", 0);
michael@0 155 #endif
michael@0 156 } else if (PORT_Strcmp(typeTag, SEC_CT_PUBLIC_KEY) == 0 ||
michael@0 157 PORT_Strcmp (typeTag, "pk") == 0) {
michael@0 158 rv = SECU_PrintSubjectPublicKeyInfo(outFile, &data, "Public Key", 0);
michael@0 159 } else if (PORT_Strcmp(typeTag, SEC_CT_PKCS7) == 0 ||
michael@0 160 PORT_Strcmp (typeTag, "p7") == 0) {
michael@0 161 rv = SECU_PrintPKCS7ContentInfo(outFile, &data,
michael@0 162 "PKCS #7 Content Info", 0);
michael@0 163 } else if (PORT_Strcmp(typeTag, SEC_CT_NAME) == 0 ||
michael@0 164 PORT_Strcmp (typeTag, "n") == 0) {
michael@0 165 rv = SECU_PrintDERName(outFile, &data, "Name", 0);
michael@0 166 } else {
michael@0 167 fprintf(stderr, "%s: don't know how to print out '%s' files\n",
michael@0 168 progName, typeTag);
michael@0 169 SECU_PrintAny(outFile, &data, "File contains", 0);
michael@0 170 return -1;
michael@0 171 }
michael@0 172
michael@0 173 if (inFile != PR_STDIN)
michael@0 174 PR_Close(inFile);
michael@0 175 PORT_Free(der.data);
michael@0 176 if (rv) {
michael@0 177 fprintf(stderr, "%s: problem converting data (%s)\n",
michael@0 178 progName, SECU_Strerror(PORT_GetError()));
michael@0 179 }
michael@0 180 if (NSS_Shutdown() != SECSuccess) {
michael@0 181 fprintf(stderr, "%s: NSS_Shutdown failed (%s)\n",
michael@0 182 progName, SECU_Strerror(PORT_GetError()));
michael@0 183 rv = SECFailure;
michael@0 184 }
michael@0 185 PR_Cleanup();
michael@0 186 return rv;
michael@0 187 }

mercurial