Back to index

lightning-sunbird  0.9+nobinonly
derdump.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 #include "secutil.h"
00038 #include "nss.h"
00039 #include <errno.h>
00040 
00041 #if defined(XP_WIN) || (defined(__sun) && !defined(SVR4))
00042 #if !defined(WIN32)
00043 extern int fprintf(FILE *, char *, ...);
00044 #endif
00045 #endif
00046 #include "plgetopt.h"
00047 
00048 static void Usage(char *progName)
00049 {
00050     fprintf(stderr,
00051            "Usage: %s [-r] [-i input] [-o output]\n",
00052            progName);
00053     fprintf(stderr, "%-20s For formatted items, dump raw bytes as well\n",
00054            "-r");
00055     fprintf(stderr, "%-20s Define an input file to use (default is stdin)\n",
00056            "-i input");
00057     fprintf(stderr, "%-20s Define an output file to use (default is stdout)\n",
00058            "-o output");
00059     exit(-1);
00060 }
00061 
00062 int main(int argc, char **argv)
00063 {
00064     char *progName;
00065     FILE *outFile;
00066     PRFileDesc *inFile;
00067     SECItem der;
00068     SECStatus rv;
00069     int16 xp_error;
00070     PRBool raw = PR_FALSE;
00071     PLOptState *optstate;
00072     PLOptStatus status;
00073 
00074     progName = strrchr(argv[0], '/');
00075     progName = progName ? progName+1 : argv[0];
00076 
00077     /* Parse command line arguments */
00078     inFile = 0;
00079     outFile = 0;
00080     optstate = PL_CreateOptState(argc, argv, "i:o:r");
00081     while ((status = PL_GetNextOpt(optstate)) == PL_OPT_OK) {
00082        switch (optstate->option) {
00083          case 'i':
00084            inFile = PR_Open(optstate->value, PR_RDONLY, 0);
00085            if (!inFile) {
00086               fprintf(stderr, "%s: unable to open \"%s\" for reading\n",
00087                      progName, optstate->value);
00088               return -1;
00089            }
00090            break;
00091 
00092          case 'o':
00093            outFile = fopen(optstate->value, "w");
00094            if (!outFile) {
00095               fprintf(stderr, "%s: unable to open \"%s\" for writing\n",
00096                      progName, optstate->value);
00097               return -1;
00098            }
00099            break;
00100 
00101          case 'r':
00102            raw = PR_TRUE;
00103            break;
00104 
00105          default:
00106            Usage(progName);
00107            break;
00108        }
00109     }
00110        if (status == PL_OPT_BAD)
00111               Usage(progName);
00112 
00113     if (!inFile) inFile = PR_STDIN;
00114     if (!outFile) outFile = stdout;
00115 
00116     rv = NSS_NoDB_Init(NULL);      /* XXX */
00117     if (rv != SECSuccess) {
00118        SECU_PrintPRandOSError(progName);
00119        return -1;
00120     }
00121 
00122        rv = SECU_ReadDERFromFile(&der, inFile, PR_FALSE);
00123     if (rv == SECSuccess) {
00124        rv = DER_PrettyPrint(outFile, &der, raw);
00125        if (rv == SECSuccess)
00126            return 0;
00127     }
00128 
00129     xp_error = PORT_GetError();
00130     if (xp_error) {
00131        SECU_PrintError(progName, "error %d", xp_error);
00132     }
00133     if (errno) {
00134        SECU_PrintSystemError(progName, "errno=%d", errno);
00135     }
00136     return 1;
00137 }