Import from 1.9a8 tarball
[mozilla-nss.git] / security / nss / cmd / libpkix / sample_apps / dumpcert.c
blob5338c5193aaa2c44a25dd3e02358b948413178c2
1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
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/
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.
14 * The Original Code is the Netscape security libraries.
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.
21 * Contributor(s):
22 * Sun Microsystems
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 * dumpcert.c
40 * dump certificate sample application
44 #include <stdio.h>
46 #include "pkix.h"
47 #include "testutil.h"
48 #include "prlong.h"
49 #include "plstr.h"
50 #include "prthread.h"
51 #include "plarena.h"
52 #include "seccomon.h"
53 #include "secdert.h"
54 #include "secasn1t.h"
55 #include "certt.h"
57 void *plContext = NULL;
59 void printUsage(void){
60 (void) printf("\nUSAGE:\tdumpcert <certFile>\n");
61 (void) printf("\tParses a certificate located at <certFile> "
62 "and displays it.\n");
65 void printFailure(char *msg){
66 (void) printf("FAILURE: %s\n", msg);
69 PKIX_PL_Cert *
70 createCert(char *inFileName)
72 PKIX_PL_ByteArray *byteArray = NULL;
73 PKIX_PL_Cert *cert = NULL;
74 PKIX_Error *error = NULL;
75 PRFileDesc *inFile = NULL;
76 SECItem certDER;
77 void *buf = NULL;
78 PKIX_UInt32 len;
79 SECStatus rv = SECFailure;
81 certDER.data = NULL;
83 inFile = PR_Open(inFileName, PR_RDONLY, 0);
85 if (!inFile){
86 printFailure("Unable to open cert file");
87 goto cleanup;
88 } else {
89 rv = SECU_ReadDERFromFile(&certDER, inFile, PR_FALSE);
90 if (!rv){
91 buf = (void *)certDER.data;
92 len = certDER.len;
94 error = PKIX_PL_ByteArray_Create
95 (buf, len, &byteArray, plContext);
97 if (error){
98 printFailure("PKIX_PL_ByteArray_Create failed");
99 goto cleanup;
102 error = PKIX_PL_Cert_Create
103 (byteArray, &cert, plContext);
105 if (error){
106 printFailure("PKIX_PL_Cert_Create failed");
107 goto cleanup;
109 } else {
110 printFailure("Unable to read DER from cert file");
111 goto cleanup;
115 cleanup:
117 if (inFile){
118 PR_Close(inFile);
121 if (rv == SECSuccess){
122 SECITEM_FreeItem(&certDER, PR_FALSE);
125 if (byteArray){
126 PKIX_PL_Object_DecRef((PKIX_PL_Object *)(byteArray), plContext);
129 return (cert);
132 int main(int argc, char *argv[])
135 PKIX_PL_String *string = NULL;
136 PKIX_PL_Cert *cert = NULL;
137 PKIX_Error *error = NULL;
138 char *ascii = NULL;
139 PKIX_UInt32 length = 0;
140 PKIX_UInt32 j = 0;
141 PKIX_Boolean useArenas = PKIX_FALSE;
142 PKIX_UInt32 actualMinorVersion;
144 PKIX_TEST_STD_VARS();
146 if (argc == 1){
147 printUsage();
148 return (0);
151 useArenas = PKIX_TEST_ARENAS_ARG(argv[1]);
153 PKIX_Initialize
154 (PKIX_TRUE, /* nssInitNeeded */
155 useArenas,
156 PKIX_MAJOR_VERSION,
157 PKIX_MINOR_VERSION,
158 PKIX_MINOR_VERSION,
159 &actualMinorVersion,
160 &plContext);
162 cert = createCert(argv[1+j]);
164 if (cert){
166 error = PKIX_PL_Object_ToString
167 ((PKIX_PL_Object *)cert, &string, plContext);
169 if (error){
170 printFailure("Unable to get string representation "
171 "of cert");
172 goto cleanup;
175 error = PKIX_PL_String_GetEncoded
176 (string,
177 PKIX_ESCASCII,
178 (void **)&ascii,
179 &length,
180 plContext);
182 if (error || !ascii){
183 printFailure("Unable to get ASCII encoding of string");
184 goto cleanup;
187 (void) printf("OUTPUT:\n%s\n", ascii);
189 } else {
190 printFailure("Unable to create certificate");
191 goto cleanup;
194 cleanup:
196 if (cert){
197 PKIX_PL_Object_DecRef((PKIX_PL_Object *)(cert), plContext);
200 if (string){
201 PKIX_PL_Object_DecRef((PKIX_PL_Object *)(string), plContext);
204 if (ascii){
205 PKIX_PL_Free((PKIX_PL_Object *)(ascii), plContext);
208 PKIX_Shutdown(plContext);
210 PKIX_TEST_RETURN();
212 endTests("DUMPCERT");
214 return (0);