Back to index

lightning-sunbird  0.9+nobinonly
pp.c
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is the Netscape security libraries.
00015  *
00016  * The Initial Developer of the Original Code is
00017  * Netscape Communications Corporation.
00018  * Portions created by the Initial Developer are Copyright (C) 1994-2000
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *
00023  * Alternatively, the contents of this file may be used under the terms of
00024  * either the GNU General Public License Version 2 or later (the "GPL"), or
00025  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00026  * in which case the provisions of the GPL or the LGPL are applicable instead
00027  * of those above. If you wish to allow use of your version of this file only
00028  * under the terms of either the GPL or the LGPL, and not to allow others to
00029  * use your version of this file under the terms of the MPL, indicate your
00030  * decision by deleting the provisions above and replace them with the notice
00031  * and other provisions required by the GPL or the LGPL. If you do not delete
00032  * the provisions above, a recipient may use your version of this file under
00033  * the terms of any one of the MPL, the GPL or the LGPL.
00034  *
00035  * ***** END LICENSE BLOCK ***** */
00036 
00037 /*
00038  * Pretty-print some well-known BER or DER encoded data (e.g. certificates,
00039  * keys, pkcs7)
00040  *
00041  * $Id: pp.c,v 1.7.28.1 2006/04/24 04:45:55 nelson%bolyard.com Exp $
00042  */
00043 
00044 #include "secutil.h"
00045 
00046 #if defined(__sun) && !defined(SVR4)
00047 extern int fprintf(FILE *, char *, ...);
00048 #endif
00049 
00050 #include "plgetopt.h"
00051 
00052 #include "pk11func.h"
00053 #include "nspr.h"
00054 #include "nss.h"
00055 
00056 static void Usage(char *progName)
00057 {
00058     fprintf(stderr,
00059            "Usage:  %s -t type [-a] [-i input] [-o output]\n",
00060            progName);
00061     fprintf(stderr, "%-20s Specify the input type (must be one of %s,\n",
00062            "-t type", SEC_CT_PRIVATE_KEY);
00063     fprintf(stderr, "%-20s %s, %s, %s,\n", "", SEC_CT_PUBLIC_KEY,
00064            SEC_CT_CERTIFICATE, SEC_CT_CERTIFICATE_REQUEST);
00065     fprintf(stderr, "%-20s %s or %s)\n", "", SEC_CT_PKCS7, SEC_CT_CRL);    
00066     fprintf(stderr, "%-20s Input is in ascii encoded form (RFC1113)\n",
00067            "-a");
00068     fprintf(stderr, "%-20s Define an input file to use (default is stdin)\n",
00069            "-i input");
00070     fprintf(stderr, "%-20s Define an output file to use (default is stdout)\n",
00071            "-o output");
00072     exit(-1);
00073 }
00074 
00075 int main(int argc, char **argv)
00076 {
00077     int rv, ascii;
00078     char *progName;
00079     FILE *outFile;
00080     PRFileDesc *inFile;
00081     SECItem der, data;
00082     char *typeTag;
00083     PLOptState *optstate;
00084 
00085     progName = strrchr(argv[0], '/');
00086     progName = progName ? progName+1 : argv[0];
00087 
00088     ascii = 0;
00089     inFile = 0;
00090     outFile = 0;
00091     typeTag = 0;
00092     optstate = PL_CreateOptState(argc, argv, "at:i:o:");
00093     while ( PL_GetNextOpt(optstate) == PL_OPT_OK ) {
00094        switch (optstate->option) {
00095          case '?':
00096            Usage(progName);
00097            break;
00098 
00099          case 'a':
00100            ascii = 1;
00101            break;
00102 
00103          case 'i':
00104            inFile = PR_Open(optstate->value, PR_RDONLY, 0);
00105            if (!inFile) {
00106               fprintf(stderr, "%s: unable to open \"%s\" for reading\n",
00107                      progName, optstate->value);
00108               return -1;
00109            }
00110            break;
00111 
00112          case 'o':
00113            outFile = fopen(optstate->value, "w");
00114            if (!outFile) {
00115               fprintf(stderr, "%s: unable to open \"%s\" for writing\n",
00116                      progName, optstate->value);
00117               return -1;
00118            }
00119            break;
00120 
00121          case 't':
00122            typeTag = strdup(optstate->value);
00123            break;
00124        }
00125     }
00126     PL_DestroyOptState(optstate);
00127     if (!typeTag) Usage(progName);
00128 
00129     if (!inFile) inFile = PR_STDIN;
00130     if (!outFile) outFile = stdout;
00131 
00132     PR_Init(PR_SYSTEM_THREAD, PR_PRIORITY_NORMAL, 1);
00133     rv = NSS_NoDB_Init(NULL);
00134     if (rv != SECSuccess) {
00135        fprintf(stderr, "%s: NSS_NoDB_Init failed (%s)\n",
00136               progName, SECU_Strerror(PORT_GetError()));
00137        exit(1);
00138     }
00139     SECU_RegisterDynamicOids();
00140 
00141     rv = SECU_ReadDERFromFile(&der, inFile, ascii);
00142     if (rv != SECSuccess) {
00143        fprintf(stderr, "%s: SECU_ReadDERFromFile failed\n", progName);
00144        exit(1);
00145     }
00146 
00147     /* Data is untyped, using the specified type */
00148     data.data = der.data;
00149     data.len = der.len;
00150 
00151     /* Pretty print it */
00152     if (PORT_Strcmp(typeTag, SEC_CT_CERTIFICATE) == 0) {
00153        rv = SECU_PrintSignedData(outFile, &data, "Certificate", 0,
00154                           SECU_PrintCertificate);
00155     } else if (PORT_Strcmp(typeTag, SEC_CT_CERTIFICATE_REQUEST) == 0) {
00156        rv = SECU_PrintSignedData(outFile, &data, "Certificate Request", 0,
00157                           SECU_PrintCertificateRequest);
00158     } else if (PORT_Strcmp (typeTag, SEC_CT_CRL) == 0) {
00159        rv = SECU_PrintSignedData (outFile, &data, "CRL", 0, SECU_PrintCrl);
00160 #ifdef HAVE_EPV_TEMPLATE
00161     } else if (PORT_Strcmp(typeTag, SEC_CT_PRIVATE_KEY) == 0) {
00162        rv = SECU_PrintPrivateKey(outFile, &data, "Private Key", 0);
00163 #endif
00164     } else if (PORT_Strcmp(typeTag, SEC_CT_PUBLIC_KEY) == 0) {
00165        rv = SECU_PrintPublicKey(outFile, &data, "Public Key", 0);
00166     } else if (PORT_Strcmp(typeTag, SEC_CT_PKCS7) == 0) {
00167        rv = SECU_PrintPKCS7ContentInfo(outFile, &data,
00168                                    "PKCS #7 Content Info", 0);
00169     } else {
00170        fprintf(stderr, "%s: don't know how to print out '%s' files\n",
00171               progName, typeTag);
00172        SECU_PrintAny(outFile, &data, "File contains", 0);
00173        return -1;
00174     }
00175 
00176     if (inFile != PR_STDIN)
00177        PR_Close(inFile);
00178     PORT_Free(der.data);
00179     if (rv) {
00180        fprintf(stderr, "%s: problem converting data (%s)\n",
00181               progName, SECU_Strerror(PORT_GetError()));
00182     }
00183     if (NSS_Shutdown() != SECSuccess) {
00184        fprintf(stderr, "%s: NSS_Shutdown failed (%s)\n",
00185               progName, SECU_Strerror(PORT_GetError()));
00186        rv = SECFailure;
00187     }
00188     PR_Cleanup();
00189     return rv;
00190 }