~ubuntu-branches/ubuntu/precise/kompozer/precise

« back to all changes in this revision

Viewing changes to mozilla/security/nss/cmd/pp/pp.c

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Yarusso
  • Date: 2007-08-27 01:11:03 UTC
  • Revision ID: james.westby@ubuntu.com-20070827011103-2jgf4s6532gqu2ka
Tags: upstream-0.7.10
ImportĀ upstreamĀ versionĀ 0.7.10

Show diffs side-by-side

added added

removed removed

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