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
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.
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 ***** */
40 * dump certificate sample application
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
);
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
;
79 SECStatus rv
= SECFailure
;
83 inFile
= PR_Open(inFileName
, PR_RDONLY
, 0);
86 printFailure("Unable to open cert file");
89 rv
= SECU_ReadDERFromFile(&certDER
, inFile
, PR_FALSE
);
91 buf
= (void *)certDER
.data
;
94 error
= PKIX_PL_ByteArray_Create
95 (buf
, len
, &byteArray
, plContext
);
98 printFailure("PKIX_PL_ByteArray_Create failed");
102 error
= PKIX_PL_Cert_Create
103 (byteArray
, &cert
, plContext
);
106 printFailure("PKIX_PL_Cert_Create failed");
110 printFailure("Unable to read DER from cert file");
121 if (rv
== SECSuccess
){
122 SECITEM_FreeItem(&certDER
, PR_FALSE
);
126 PKIX_PL_Object_DecRef((PKIX_PL_Object
*)(byteArray
), plContext
);
132 int main(int argc
, char *argv
[])
135 PKIX_PL_String
*string
= NULL
;
136 PKIX_PL_Cert
*cert
= NULL
;
137 PKIX_Error
*error
= NULL
;
139 PKIX_UInt32 length
= 0;
141 PKIX_Boolean useArenas
= PKIX_FALSE
;
142 PKIX_UInt32 actualMinorVersion
;
144 PKIX_TEST_STD_VARS();
151 useArenas
= PKIX_TEST_ARENAS_ARG(argv
[1]);
154 (PKIX_TRUE
, /* nssInitNeeded */
162 cert
= createCert(argv
[1+j
]);
166 error
= PKIX_PL_Object_ToString
167 ((PKIX_PL_Object
*)cert
, &string
, plContext
);
170 printFailure("Unable to get string representation "
175 error
= PKIX_PL_String_GetEncoded
182 if (error
|| !ascii
){
183 printFailure("Unable to get ASCII encoding of string");
187 (void) printf("OUTPUT:\n%s\n", ascii
);
190 printFailure("Unable to create certificate");
197 PKIX_PL_Object_DecRef((PKIX_PL_Object
*)(cert
), plContext
);
201 PKIX_PL_Object_DecRef((PKIX_PL_Object
*)(string
), plContext
);
205 PKIX_PL_Free((PKIX_PL_Object
*)(ascii
), plContext
);
208 PKIX_Shutdown(plContext
);
212 endTests("DUMPCERT");