~ubuntu-branches/ubuntu/feisty/firefox/feisty-updates

« back to all changes in this revision

Viewing changes to security/nss-fips/cmd/pp/pp.c

  • Committer: Bazaar Package Importer
  • Author(s): Alexander Sack, Alexander Sack
  • Date: 2008-06-23 15:08:12 UTC
  • mfrom: (1.1.24 upstream)
  • Revision ID: james.westby@ubuntu.com-20080623150812-sxdwhn3dz9pmapvf
Tags: 2.0.0.15+0nobinonly-0ubuntu0.7.4
[ Alexander Sack ]
* New security/stability upstream release (v2.0.0.15)
  - see USN-619-1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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.7.28.1 2006/04/24 04:45:55 nelson%bolyard.com Exp $
 
42
 */
 
43
 
 
44
#include "secutil.h"
 
45
 
 
46
#if defined(__sun) && !defined(SVR4)
 
47
extern 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
 
 
56
static 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 or %s)\n", "", SEC_CT_PKCS7, SEC_CT_CRL);    
 
66
    fprintf(stderr, "%-20s Input is in ascii encoded form (RFC1113)\n",
 
67
            "-a");
 
68
    fprintf(stderr, "%-20s Define an input file to use (default is stdin)\n",
 
69
            "-i input");
 
70
    fprintf(stderr, "%-20s Define an output file to use (default is stdout)\n",
 
71
            "-o output");
 
72
    exit(-1);
 
73
}
 
74
 
 
75
int main(int argc, char **argv)
 
76
{
 
77
    int rv, ascii;
 
78
    char *progName;
 
79
    FILE *outFile;
 
80
    PRFileDesc *inFile;
 
81
    SECItem der, data;
 
82
    char *typeTag;
 
83
    PLOptState *optstate;
 
84
 
 
85
    progName = strrchr(argv[0], '/');
 
86
    progName = progName ? progName+1 : argv[0];
 
87
 
 
88
    ascii = 0;
 
89
    inFile = 0;
 
90
    outFile = 0;
 
91
    typeTag = 0;
 
92
    optstate = PL_CreateOptState(argc, argv, "at:i:o:");
 
93
    while ( PL_GetNextOpt(optstate) == PL_OPT_OK ) {
 
94
        switch (optstate->option) {
 
95
          case '?':
 
96
            Usage(progName);
 
97
            break;
 
98
 
 
99
          case 'a':
 
100
            ascii = 1;
 
101
            break;
 
102
 
 
103
          case 'i':
 
104
            inFile = PR_Open(optstate->value, PR_RDONLY, 0);
 
105
            if (!inFile) {
 
106
                fprintf(stderr, "%s: unable to open \"%s\" for reading\n",
 
107
                        progName, optstate->value);
 
108
                return -1;
 
109
            }
 
110
            break;
 
111
 
 
112
          case 'o':
 
113
            outFile = fopen(optstate->value, "w");
 
114
            if (!outFile) {
 
115
                fprintf(stderr, "%s: unable to open \"%s\" for writing\n",
 
116
                        progName, optstate->value);
 
117
                return -1;
 
118
            }
 
119
            break;
 
120
 
 
121
          case 't':
 
122
            typeTag = strdup(optstate->value);
 
123
            break;
 
124
        }
 
125
    }
 
126
    PL_DestroyOptState(optstate);
 
127
    if (!typeTag) Usage(progName);
 
128
 
 
129
    if (!inFile) inFile = PR_STDIN;
 
130
    if (!outFile) outFile = stdout;
 
131
 
 
132
    PR_Init(PR_SYSTEM_THREAD, PR_PRIORITY_NORMAL, 1);
 
133
    rv = NSS_NoDB_Init(NULL);
 
134
    if (rv != SECSuccess) {
 
135
        fprintf(stderr, "%s: NSS_NoDB_Init failed (%s)\n",
 
136
                progName, SECU_Strerror(PORT_GetError()));
 
137
        exit(1);
 
138
    }
 
139
    SECU_RegisterDynamicOids();
 
140
 
 
141
    rv = SECU_ReadDERFromFile(&der, inFile, ascii);
 
142
    if (rv != SECSuccess) {
 
143
        fprintf(stderr, "%s: SECU_ReadDERFromFile failed\n", progName);
 
144
        exit(1);
 
145
    }
 
146
 
 
147
    /* Data is untyped, using the specified type */
 
148
    data.data = der.data;
 
149
    data.len = der.len;
 
150
 
 
151
    /* Pretty print it */
 
152
    if (PORT_Strcmp(typeTag, SEC_CT_CERTIFICATE) == 0) {
 
153
        rv = SECU_PrintSignedData(outFile, &data, "Certificate", 0,
 
154
                             SECU_PrintCertificate);
 
155
    } else if (PORT_Strcmp(typeTag, SEC_CT_CERTIFICATE_REQUEST) == 0) {
 
156
        rv = SECU_PrintSignedData(outFile, &data, "Certificate Request", 0,
 
157
                             SECU_PrintCertificateRequest);
 
158
    } else if (PORT_Strcmp (typeTag, SEC_CT_CRL) == 0) {
 
159
        rv = SECU_PrintSignedData (outFile, &data, "CRL", 0, SECU_PrintCrl);
 
160
#ifdef HAVE_EPV_TEMPLATE
 
161
    } else if (PORT_Strcmp(typeTag, SEC_CT_PRIVATE_KEY) == 0) {
 
162
        rv = SECU_PrintPrivateKey(outFile, &data, "Private Key", 0);
 
163
#endif
 
164
    } else if (PORT_Strcmp(typeTag, SEC_CT_PUBLIC_KEY) == 0) {
 
165
        rv = SECU_PrintPublicKey(outFile, &data, "Public Key", 0);
 
166
    } else if (PORT_Strcmp(typeTag, SEC_CT_PKCS7) == 0) {
 
167
        rv = SECU_PrintPKCS7ContentInfo(outFile, &data,
 
168
                                        "PKCS #7 Content Info", 0);
 
169
    } else {
 
170
        fprintf(stderr, "%s: don't know how to print out '%s' files\n",
 
171
                progName, typeTag);
 
172
        SECU_PrintAny(outFile, &data, "File contains", 0);
 
173
        return -1;
 
174
    }
 
175
 
 
176
    if (inFile != PR_STDIN)
 
177
        PR_Close(inFile);
 
178
    PORT_Free(der.data);
 
179
    if (rv) {
 
180
        fprintf(stderr, "%s: problem converting data (%s)\n",
 
181
                progName, SECU_Strerror(PORT_GetError()));
 
182
    }
 
183
    if (NSS_Shutdown() != SECSuccess) {
 
184
        fprintf(stderr, "%s: NSS_Shutdown failed (%s)\n",
 
185
                progName, SECU_Strerror(PORT_GetError()));
 
186
        rv = SECFailure;
 
187
    }
 
188
    PR_Cleanup();
 
189
    return rv;
 
190
}