~ubuntu-branches/ubuntu/precise/nss/precise-security

« back to all changes in this revision

Viewing changes to nss/cmd/libpkix/sample_apps/dumpcert.c

  • Committer: Package Import Robot
  • Author(s): Marc Deslauriers
  • Date: 2013-11-14 14:58:07 UTC
  • mfrom: (1.1.19)
  • Revision ID: package-import@ubuntu.com-20131114145807-ay302kimn72ovt88
Tags: 3.15.3-0ubuntu0.12.04.1
* SECURITY UPDATE: New upstream release to fix multiple security issues
  and add TLSv1.2 support.
  - CVE-2013-1739
  - CVE-2013-1741
  - CVE-2013-5605
  - CVE-2013-5606
* Adjusted packaging for 3.15.3:
  - debian/patches/*: refreshed.
  - debian/patches/lower-dhe-priority.patch: removed, no longer needed,
    was a workaround for an old version of firefox.
  - debian/libnss3.symbols: added new symbols.
  - debian/rules: updated for new source layout.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* This Source Code Form is subject to the terms of the Mozilla Public
 
2
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 
3
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
4
/*
 
5
 * dumpcert.c
 
6
 *
 
7
 * dump certificate sample application
 
8
 *
 
9
 */
 
10
 
 
11
#include <stdio.h>
 
12
 
 
13
#include "pkix.h"
 
14
#include "testutil.h"
 
15
#include "prlong.h"
 
16
#include "plstr.h"
 
17
#include "prthread.h"
 
18
#include "plarena.h"
 
19
#include "seccomon.h"
 
20
#include "secdert.h"
 
21
#include "secasn1t.h"
 
22
#include "certt.h"
 
23
 
 
24
static void *plContext = NULL;
 
25
 
 
26
static 
 
27
void printUsage(void){
 
28
        (void) printf("\nUSAGE:\tdumpcert <certFile>\n");
 
29
        (void) printf("\tParses a certificate located at <certFile> "
 
30
                "and displays it.\n");
 
31
}
 
32
 
 
33
static 
 
34
void printFailure(char *msg){
 
35
        (void) printf("FAILURE: %s\n", msg);
 
36
}
 
37
 
 
38
static PKIX_PL_Cert *
 
39
createCert(char *inFileName)
 
40
{
 
41
        PKIX_PL_ByteArray *byteArray = NULL;
 
42
        PKIX_PL_Cert *cert = NULL;
 
43
        PKIX_Error *error = NULL;
 
44
        PRFileDesc *inFile = NULL;
 
45
        SECItem certDER;
 
46
        void *buf = NULL;
 
47
        PKIX_UInt32 len;
 
48
        SECStatus rv = SECFailure;
 
49
 
 
50
        certDER.data = NULL;
 
51
 
 
52
        inFile = PR_Open(inFileName, PR_RDONLY, 0);
 
53
 
 
54
        if (!inFile){
 
55
                printFailure("Unable to open cert file");
 
56
                goto cleanup;
 
57
        } else {
 
58
                rv = SECU_ReadDERFromFile(&certDER, inFile, PR_FALSE, PR_FALSE);
 
59
                if (!rv){
 
60
                        buf = (void *)certDER.data;
 
61
                        len = certDER.len;
 
62
 
 
63
                        error = PKIX_PL_ByteArray_Create
 
64
                                (buf, len, &byteArray, plContext);
 
65
 
 
66
                        if (error){
 
67
                                printFailure("PKIX_PL_ByteArray_Create failed");
 
68
                                goto cleanup;
 
69
                        }
 
70
 
 
71
                        error = PKIX_PL_Cert_Create
 
72
                                (byteArray, &cert, plContext);
 
73
 
 
74
                        if (error){
 
75
                                printFailure("PKIX_PL_Cert_Create failed");
 
76
                                goto cleanup;
 
77
                        }
 
78
                } else {
 
79
                        printFailure("Unable to read DER from cert file");
 
80
                        goto cleanup;
 
81
                }
 
82
        }
 
83
 
 
84
cleanup:
 
85
 
 
86
        if (inFile){
 
87
                PR_Close(inFile);
 
88
        }
 
89
 
 
90
        if (rv == SECSuccess){
 
91
                SECITEM_FreeItem(&certDER, PR_FALSE);
 
92
        }
 
93
 
 
94
        if (byteArray){
 
95
                PKIX_PL_Object_DecRef((PKIX_PL_Object *)(byteArray), plContext);
 
96
        }
 
97
 
 
98
        return (cert);
 
99
}
 
100
 
 
101
int dumpcert(int argc, char *argv[])
 
102
{
 
103
 
 
104
        PKIX_PL_String *string = NULL;
 
105
        PKIX_PL_Cert *cert = NULL;
 
106
        PKIX_Error *error = NULL;
 
107
        char *ascii = NULL;
 
108
        PKIX_UInt32 length = 0;
 
109
        PKIX_UInt32 j = 0;
 
110
        PKIX_Boolean useArenas = PKIX_FALSE;
 
111
        PKIX_UInt32 actualMinorVersion;
 
112
 
 
113
        PKIX_TEST_STD_VARS();
 
114
 
 
115
        if (argc == 1){
 
116
                printUsage();
 
117
                return (0);
 
118
        }
 
119
 
 
120
        useArenas = PKIX_TEST_ARENAS_ARG(argv[1]);
 
121
 
 
122
        PKIX_Initialize
 
123
                (PKIX_TRUE, /* nssInitNeeded */
 
124
                useArenas,
 
125
                PKIX_MAJOR_VERSION,
 
126
                PKIX_MINOR_VERSION,
 
127
                PKIX_MINOR_VERSION,
 
128
                &actualMinorVersion,
 
129
                &plContext);
 
130
 
 
131
        cert = createCert(argv[1+j]);
 
132
 
 
133
        if (cert){
 
134
 
 
135
                error = PKIX_PL_Object_ToString
 
136
                        ((PKIX_PL_Object *)cert, &string, plContext);
 
137
 
 
138
                if (error){
 
139
                        printFailure("Unable to get string representation "
 
140
                                    "of cert");
 
141
                        goto cleanup;
 
142
                }
 
143
 
 
144
                error = PKIX_PL_String_GetEncoded
 
145
                        (string,
 
146
                        PKIX_ESCASCII,
 
147
                        (void **)&ascii,
 
148
                        &length,
 
149
                        plContext);
 
150
 
 
151
                if (error || !ascii){
 
152
                        printFailure("Unable to get ASCII encoding of string");
 
153
                        goto cleanup;
 
154
                }
 
155
 
 
156
                (void) printf("OUTPUT:\n%s\n", ascii);
 
157
 
 
158
        } else {
 
159
                printFailure("Unable to create certificate");
 
160
                goto cleanup;
 
161
        }
 
162
 
 
163
cleanup:
 
164
 
 
165
        if (cert){
 
166
                PKIX_PL_Object_DecRef((PKIX_PL_Object *)(cert), plContext);
 
167
        }
 
168
 
 
169
        if (string){
 
170
                PKIX_PL_Object_DecRef((PKIX_PL_Object *)(string), plContext);
 
171
        }
 
172
 
 
173
        if (ascii){
 
174
                PKIX_PL_Free((PKIX_PL_Object *)(ascii), plContext);
 
175
        }
 
176
 
 
177
        PKIX_Shutdown(plContext);
 
178
 
 
179
        PKIX_TEST_RETURN();
 
180
 
 
181
        endTests("DUMPCERT");
 
182
 
 
183
        return (0);
 
184
}