PageRenderTime 48ms CodeModel.GetById 12ms app.highlight 29ms RepoModel.GetById 2ms app.codeStats 0ms

/security/nss/cmd/pp/pp.c

http://github.com/zpao/v8monkey
C | 193 lines | 131 code | 19 blank | 43 comment | 39 complexity | 36a33e019967825ae108c41b8b174228 MD5 | raw file
  1/* ***** BEGIN LICENSE BLOCK *****
  2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3 *
  4 * The contents of this file are subject to the Mozilla Public License Version
  5 * 1.1 (the "License"); you may not use this file except in compliance with
  6 * the License. You may obtain a copy of the License at
  7 * http://www.mozilla.org/MPL/
  8 *
  9 * Software distributed under the License is distributed on an "AS IS" basis,
 10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 11 * for the specific language governing rights and limitations under the
 12 * License.
 13 *
 14 * The Original Code is the Netscape security libraries.
 15 *
 16 * The Initial Developer of the Original Code is
 17 * Netscape Communications Corporation.
 18 * Portions created by the Initial Developer are Copyright (C) 1994-2000
 19 * the Initial Developer. All Rights Reserved.
 20 *
 21 * Contributor(s):
 22 *
 23 * Alternatively, the contents of this file may be used under the terms of
 24 * either the GNU General Public License Version 2 or later (the "GPL"), or
 25 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 26 * in which case the provisions of the GPL or the LGPL are applicable instead
 27 * of those above. If you wish to allow use of your version of this file only
 28 * under the terms of either the GPL or the LGPL, and not to allow others to
 29 * use your version of this file under the terms of the MPL, indicate your
 30 * decision by deleting the provisions above and replace them with the notice
 31 * and other provisions required by the GPL or the LGPL. If you do not delete
 32 * the provisions above, a recipient may use your version of this file under
 33 * the terms of any one of the MPL, the GPL or the LGPL.
 34 *
 35 * ***** END LICENSE BLOCK ***** */
 36
 37/*
 38 * Pretty-print some well-known BER or DER encoded data (e.g. certificates,
 39 * keys, pkcs7)
 40 *
 41 * $Id: pp.c,v 1.10 2010/09/03 19:25:02 nelson%bolyard.com Exp $
 42 */
 43
 44#include "secutil.h"
 45
 46#if defined(__sun) && !defined(SVR4)
 47extern int fprintf(FILE *, char *, ...);
 48#endif
 49
 50#include "plgetopt.h"
 51
 52#include "pk11func.h"
 53#include "nspr.h"
 54#include "nss.h"
 55
 56static void Usage(char *progName)
 57{
 58    fprintf(stderr,
 59	    "Usage:  %s -t type [-a] [-i input] [-o output]\n",
 60	    progName);
 61    fprintf(stderr, "%-20s Specify the input type (must be one of %s,\n",
 62	    "-t type", SEC_CT_PRIVATE_KEY);
 63    fprintf(stderr, "%-20s %s, %s, %s,\n", "", SEC_CT_PUBLIC_KEY,
 64	    SEC_CT_CERTIFICATE, SEC_CT_CERTIFICATE_REQUEST);
 65    fprintf(stderr, "%-20s %s, %s or %s)\n", "", SEC_CT_PKCS7, SEC_CT_CRL,
 66            SEC_CT_NAME);    
 67    fprintf(stderr, "%-20s Input is in ascii encoded form (RFC1113)\n",
 68	    "-a");
 69    fprintf(stderr, "%-20s Define an input file to use (default is stdin)\n",
 70	    "-i input");
 71    fprintf(stderr, "%-20s Define an output file to use (default is stdout)\n",
 72	    "-o output");
 73    exit(-1);
 74}
 75
 76int main(int argc, char **argv)
 77{
 78    int rv, ascii;
 79    char *progName;
 80    FILE *outFile;
 81    PRFileDesc *inFile;
 82    SECItem der, data;
 83    char *typeTag;
 84    PLOptState *optstate;
 85
 86    progName = strrchr(argv[0], '/');
 87    progName = progName ? progName+1 : argv[0];
 88
 89    ascii = 0;
 90    inFile = 0;
 91    outFile = 0;
 92    typeTag = 0;
 93    optstate = PL_CreateOptState(argc, argv, "at:i:o:");
 94    while ( PL_GetNextOpt(optstate) == PL_OPT_OK ) {
 95	switch (optstate->option) {
 96	  case '?':
 97	    Usage(progName);
 98	    break;
 99
100	  case 'a':
101	    ascii = 1;
102	    break;
103
104	  case 'i':
105	    inFile = PR_Open(optstate->value, PR_RDONLY, 0);
106	    if (!inFile) {
107		fprintf(stderr, "%s: unable to open \"%s\" for reading\n",
108			progName, optstate->value);
109		return -1;
110	    }
111	    break;
112
113	  case 'o':
114	    outFile = fopen(optstate->value, "w");
115	    if (!outFile) {
116		fprintf(stderr, "%s: unable to open \"%s\" for writing\n",
117			progName, optstate->value);
118		return -1;
119	    }
120	    break;
121
122	  case 't':
123	    typeTag = strdup(optstate->value);
124	    break;
125	}
126    }
127    PL_DestroyOptState(optstate);
128    if (!typeTag) Usage(progName);
129
130    if (!inFile) inFile = PR_STDIN;
131    if (!outFile) outFile = stdout;
132
133    PR_Init(PR_SYSTEM_THREAD, PR_PRIORITY_NORMAL, 1);
134    rv = NSS_NoDB_Init(NULL);
135    if (rv != SECSuccess) {
136	fprintf(stderr, "%s: NSS_NoDB_Init failed (%s)\n",
137		progName, SECU_Strerror(PORT_GetError()));
138	exit(1);
139    }
140    SECU_RegisterDynamicOids();
141
142    rv = SECU_ReadDERFromFile(&der, inFile, ascii);
143    if (rv != SECSuccess) {
144	fprintf(stderr, "%s: SECU_ReadDERFromFile failed\n", progName);
145	exit(1);
146    }
147
148    /* Data is untyped, using the specified type */
149    data.data = der.data;
150    data.len = der.len;
151
152    /* Pretty print it */
153    if (PORT_Strcmp(typeTag, SEC_CT_CERTIFICATE) == 0) {
154	rv = SECU_PrintSignedData(outFile, &data, "Certificate", 0,
155			     SECU_PrintCertificate);
156    } else if (PORT_Strcmp(typeTag, SEC_CT_CERTIFICATE_REQUEST) == 0) {
157	rv = SECU_PrintSignedData(outFile, &data, "Certificate Request", 0,
158			     SECU_PrintCertificateRequest);
159    } else if (PORT_Strcmp (typeTag, SEC_CT_CRL) == 0) {
160	rv = SECU_PrintSignedData (outFile, &data, "CRL", 0, SECU_PrintCrl);
161#ifdef HAVE_EPV_TEMPLATE
162    } else if (PORT_Strcmp(typeTag, SEC_CT_PRIVATE_KEY) == 0) {
163	rv = SECU_PrintPrivateKey(outFile, &data, "Private Key", 0);
164#endif
165    } else if (PORT_Strcmp(typeTag, SEC_CT_PUBLIC_KEY) == 0) {
166	rv = SECU_PrintSubjectPublicKeyInfo(outFile, &data, "Public Key", 0);
167    } else if (PORT_Strcmp(typeTag, SEC_CT_PKCS7) == 0) {
168	rv = SECU_PrintPKCS7ContentInfo(outFile, &data,
169					"PKCS #7 Content Info", 0);
170    } else if (PORT_Strcmp(typeTag, SEC_CT_NAME) == 0) {
171	rv = SECU_PrintDERName(outFile, &data, "Name", 0);
172    } else {
173	fprintf(stderr, "%s: don't know how to print out '%s' files\n",
174		progName, typeTag);
175	SECU_PrintAny(outFile, &data, "File contains", 0);
176	return -1;
177    }
178
179    if (inFile != PR_STDIN)
180	PR_Close(inFile);
181    PORT_Free(der.data);
182    if (rv) {
183	fprintf(stderr, "%s: problem converting data (%s)\n",
184		progName, SECU_Strerror(PORT_GetError()));
185    }
186    if (NSS_Shutdown() != SECSuccess) {
187	fprintf(stderr, "%s: NSS_Shutdown failed (%s)\n",
188		progName, SECU_Strerror(PORT_GetError()));
189	rv = SECFailure;
190    }
191    PR_Cleanup();
192    return rv;
193}