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.
23 * Alternatively, the contents of this file may be used under the terms of
24 * either the GNU General Public License Version 2 or later (the "GPL"), or
25 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26 * in which case the provisions of the GPL or the LGPL are applicable instead
27 * of those above. If you wish to allow use of your version of this file only
28 * under the terms of either the GPL or the LGPL, and not to allow others to
29 * use your version of this file under the terms of the MPL, indicate your
30 * decision by deleting the provisions above and replace them with the notice
31 * and other provisions required by the GPL or the LGPL. If you do not delete
32 * the provisions above, a recipient may use your version of this file under
33 * the terms of any one of the MPL, the GPL or the LGPL.
35 * ***** END LICENSE BLOCK ***** */
41 static int num_trav_certs
= 0;
42 static SECStatus
cert_trav_callback(CERTCertificate
*cert
, SECItem
*k
,
45 /*********************************************************************
50 ListCerts(char *key
, int list_certs
)
55 CERTCertDBHandle
* db
;
57 CERTCertificate
* cert
;
60 errlog
.arena
= PORT_NewArena(DER_DEFAULT_CHUNKSIZE
);
61 if ( errlog
.arena
== NULL
) {
68 ugly_list
= PORT_ZAlloc (16);
70 if (ugly_list
== NULL
) {
76 db
= CERT_GetDefaultCertDB();
78 if (list_certs
== 2) {
79 PR_fprintf(outputFD
, "\nS Certificates\n");
80 PR_fprintf(outputFD
, "- ------------\n");
82 PR_fprintf(outputFD
, "\nObject signing certificates\n");
83 PR_fprintf(outputFD
, "---------------------------------------\n");
88 /* Traverse non-internal DBs */
89 rv
= PK11_TraverseSlotCerts(cert_trav_callback
, (void * )&list_certs
,
93 PR_fprintf(outputFD
, "**Traverse of non-internal DBs failed**\n");
97 if (num_trav_certs
== 0) {
99 "You don't appear to have any object signing certificates.\n");
102 if (list_certs
== 2) {
103 PR_fprintf(outputFD
, "- ------------\n");
105 PR_fprintf(outputFD
, "---------------------------------------\n");
108 if (list_certs
== 1) {
110 "For a list including CA's, use \"%s -L\"\n", PROGRAM_NAME
);
113 if (list_certs
== 2) {
115 "Certificates that can be used to sign objects have *'s to "
120 /* Do an analysis of the given cert */
122 cert
= PK11_FindCertFromNickname(key
, NULL
/*wincx*/);
126 "\nThe certificate with nickname \"%s\" was found:\n",
128 PR_fprintf(outputFD
, "\tsubject name: %s\n", cert
->subjectName
);
129 PR_fprintf(outputFD
, "\tissuer name: %s\n", cert
->issuerName
);
131 PR_fprintf(outputFD
, "\n");
133 rv
= CERT_CertTimesValid (cert
);
134 if (rv
!= SECSuccess
) {
135 PR_fprintf(outputFD
, "**This certificate is expired**\n");
137 PR_fprintf(outputFD
, "This certificate is not expired.\n");
140 rv
= CERT_VerifyCert (db
, cert
, PR_TRUE
,
141 certUsageObjectSigner
, PR_Now(), NULL
, &errlog
);
143 if (rv
!= SECSuccess
) {
145 if (errlog
.count
> 0) {
147 "**Certificate validation failed for the "
148 "following reason(s):**\n");
150 PR_fprintf(outputFD
, "**Certificate validation failed**");
153 PR_fprintf(outputFD
, "This certificate is valid.\n");
155 displayVerifyLog(&errlog
);
161 "The certificate with nickname \"%s\" was NOT FOUND\n", key
);
165 if (errlog
.arena
!= NULL
) {
166 PORT_FreeArena(errlog
.arena
, PR_FALSE
);
176 /********************************************************************
178 * c e r t _ t r a v _ c a l l b a c k
181 cert_trav_callback(CERTCertificate
*cert
, SECItem
*k
, void *data
)
186 char *name
, *issuerCN
, *expires
;
187 CERTCertificate
* issuerCert
= NULL
;
190 list_certs
= *((int * )data
);
193 if (cert
->nickname
) {
194 name
= cert
->nickname
;
196 isSigningCert
= cert
->nsCertType
& NS_CERT_TYPE_OBJECT_SIGNING
;
197 issuerCert
= CERT_FindCertIssuer (cert
, PR_Now(), certUsageObjectSigner
);
198 issuerCN
= CERT_GetCommonName (&cert
->issuer
);
200 if (!isSigningCert
&& list_certs
== 1)
203 /* Add this name or email to list */
209 if (list_certs
== 2) {
210 PR_fprintf(outputFD
, "%s ", isSigningCert
? "*" : " ");
212 PR_fprintf(outputFD
, "%s\n", name
);
214 if (list_certs
== 1) {
215 if (issuerCert
== NULL
) {
217 "\t++ Error ++ Unable to find issuer certificate\n");
219 /*function was a success even if cert is bogus*/
221 if (issuerCN
== NULL
)
222 PR_fprintf(outputFD
, " Issued by: %s\n",
223 issuerCert
->nickname
);
226 " Issued by: %s (%s)\n", issuerCert
->nickname
,
229 expires
= DER_TimeChoiceDayToAscii(&cert
->validity
.notAfter
);
232 PR_fprintf(outputFD
, " Expires: %s\n", expires
);
234 rv
= CERT_CertTimesValid (cert
);
236 if (rv
!= SECSuccess
)
238 " ++ Error ++ THIS CERTIFICATE IS EXPIRED\n");
240 if (rv
== SECSuccess
) {
241 rv
= CERT_VerifyCertNow (cert
->dbhandle
, cert
,
242 PR_TRUE
, certUsageObjectSigner
, NULL
);
244 if (rv
!= SECSuccess
) {
245 rv
= PORT_GetError();
247 " ++ Error ++ THIS CERTIFICATE IS NOT VALID (%s)\n",
252 expires
= DER_TimeChoiceDayToAscii(&issuerCert
->validity
.notAfter
);
254 expires
= "(unknown)";
256 rv
= CERT_CertTimesValid (issuerCert
);
258 if (rv
!= SECSuccess
)
260 " ++ Error ++ ISSUER CERT \"%s\" EXPIRED ON %s\n",
261 issuerCert
->nickname
, expires
);
263 if (rv
== SECSuccess
) {
264 rv
= CERT_VerifyCertNow (issuerCert
->dbhandle
, issuerCert
,
265 PR_TRUE
, certUsageVerifyCA
, NULL
);
266 if (rv
!= SECSuccess
) {
267 rv
= PORT_GetError();
269 " ++ Error ++ ISSUER CERT \"%s\" IS NOT VALID (%s)\n",
270 issuerCert
->nickname
, secErrorString(rv
));