Import 3.0 beta 3 tarball
[mozilla-nss.git] / security / nss / lib / certhigh / certvfypkixprint.c
blob1d59310a9dde4542684b0db1add7d8209c218ff0
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 * nss_pkix_proxy.h
40 * PKIX - NSS proxy functions
43 #include "cert.h"
44 #include "pkix_pl_common.h"
46 #ifdef DEBUG
48 char *
49 pkix_Error2ASCII(PKIX_Error *error, void *plContext)
51 PKIX_UInt32 length;
52 char *asciiString = NULL;
53 PKIX_PL_String *pkixString = NULL;
54 PKIX_Error *errorResult = NULL;
56 errorResult = PKIX_PL_Object_ToString
57 ((PKIX_PL_Object*)error, &pkixString, plContext);
58 if (errorResult) goto cleanup;
60 errorResult = PKIX_PL_String_GetEncoded
61 (pkixString,
62 PKIX_ESCASCII,
63 (void **)&asciiString,
64 &length,
65 plContext);
67 cleanup:
69 if (pkixString){
70 if (PKIX_PL_Object_DecRef
71 ((PKIX_PL_Object*)pkixString, plContext)){
72 return (NULL);
76 if (errorResult){
77 PKIX_PL_Object_DecRef((PKIX_PL_Object*)errorResult, plContext);
78 return (NULL);
81 return (asciiString);
84 char *
85 pkix_Object2ASCII(PKIX_PL_Object *object)
87 PKIX_UInt32 length;
88 char *asciiString = NULL;
89 PKIX_PL_String *pkixString = NULL;
90 PKIX_Error *errorResult = NULL;
92 errorResult = PKIX_PL_Object_ToString
93 (object, &pkixString, NULL);
94 if (errorResult) goto cleanup;
96 errorResult = PKIX_PL_String_GetEncoded
97 (pkixString, PKIX_ESCASCII, (void **)&asciiString, &length, NULL);
99 cleanup:
101 if (pkixString){
102 if (PKIX_PL_Object_DecRef((PKIX_PL_Object*)pkixString, NULL)){
103 return (NULL);
107 if (errorResult){
108 return (NULL);
111 return (asciiString);
114 char *
115 pkix_Cert2ASCII(PKIX_PL_Cert *cert)
117 PKIX_PL_X500Name *issuer = NULL;
118 void *issuerAscii = NULL;
119 PKIX_PL_X500Name *subject = NULL;
120 void *subjectAscii = NULL;
121 void *asciiString = NULL;
122 PKIX_Error *errorResult = NULL;
123 PKIX_UInt32 numChars;
124 PKIX_UInt32 refCount = 0;
126 /* Issuer */
127 errorResult = PKIX_PL_Cert_GetIssuer(cert, &issuer, NULL);
128 if (errorResult) goto cleanup;
130 issuerAscii = pkix_Object2ASCII((PKIX_PL_Object*)issuer);
132 /* Subject */
133 errorResult = PKIX_PL_Cert_GetSubject(cert, &subject, NULL);
134 if (errorResult) goto cleanup;
136 if (subject){
137 subjectAscii = pkix_Object2ASCII((PKIX_PL_Object*)subject);
140 /* errorResult = PKIX_PL_Object_GetRefCount((PKIX_PL_Object*)cert, &refCount, NULL); */
141 if (errorResult) goto cleanup;
143 errorResult = PKIX_PL_Malloc(200, &asciiString, NULL);
144 if (errorResult) goto cleanup;
146 numChars =
147 PR_snprintf
148 (asciiString,
149 200,
150 "Ref: %d Issuer=%s\nSubject=%s\n",
151 refCount,
152 issuerAscii,
153 subjectAscii);
155 if (!numChars) goto cleanup;
157 cleanup:
159 if (issuer){
160 if (PKIX_PL_Object_DecRef((PKIX_PL_Object*)issuer, NULL)){
161 return (NULL);
165 if (subject){
166 if (PKIX_PL_Object_DecRef((PKIX_PL_Object*)subject, NULL)){
167 return (NULL);
171 if (PKIX_PL_Free((PKIX_PL_Object*)issuerAscii, NULL)){
172 return (NULL);
175 if (PKIX_PL_Free((PKIX_PL_Object*)subjectAscii, NULL)){
176 return (NULL);
179 if (errorResult){
180 return (NULL);
183 return (asciiString);
186 PKIX_Error *
187 cert_PrintCertChain(
188 PKIX_List *pkixCertChain,
189 void *plContext)
191 PKIX_PL_Cert *cert = NULL;
192 PKIX_UInt32 numCerts = 0, i = 0;
193 char *asciiResult = NULL;
195 PKIX_ENTER(CERTVFYPKIX, "cert_PrintCertChain");
197 PKIX_CHECK(
198 PKIX_List_GetLength(pkixCertChain, &numCerts, plContext),
199 PKIX_LISTGETLENGTHFAILED);
201 fprintf(stderr, "\n");
203 for (i = 0; i < numCerts; i++){
204 PKIX_CHECK
205 (PKIX_List_GetItem
206 (pkixCertChain, i, (PKIX_PL_Object**)&cert, plContext),
207 PKIX_LISTGETITEMFAILED);
209 asciiResult = pkix_Cert2ASCII(cert);
211 fprintf(stderr, "CERT[%d]:\n%s\n", i, asciiResult);
213 PKIX_PL_Free(asciiResult, plContext);
214 asciiResult = NULL;
216 PKIX_DECREF(cert);
219 cleanup:
220 PKIX_DECREF(cert);
222 PKIX_RETURN(CERTVFYPKIX);
225 void
226 cert_PrintCert(
227 PKIX_PL_Cert *pkixCert,
228 void *plContext)
230 char *asciiResult = NULL;
232 asciiResult = pkix_Cert2ASCII(pkixCert);
234 fprintf(stderr, "CERT[0]:\n%s\n", asciiResult);
236 PKIX_PL_Free(asciiResult, plContext);
239 #endif /* DEBUG */