Back to index

lightning-sunbird  0.9+nobinonly
cmpcert.c
Go to the documentation of this file.
00001 /*
00002  * NSS utility functions
00003  *
00004  * ***** BEGIN LICENSE BLOCK *****
00005  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00006  *
00007  * The contents of this file are subject to the Mozilla Public License Version
00008  * 1.1 (the "License"); you may not use this file except in compliance with
00009  * the License. You may obtain a copy of the License at
00010  * http://www.mozilla.org/MPL/
00011  *
00012  * Software distributed under the License is distributed on an "AS IS" basis,
00013  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00014  * for the specific language governing rights and limitations under the
00015  * License.
00016  *
00017  * The Original Code is the Netscape security libraries.
00018  *
00019  * The Initial Developer of the Original Code is
00020  * Netscape Communications Corporation.
00021  * Portions created by the Initial Developer are Copyright (C) 1994-2000
00022  * the Initial Developer. All Rights Reserved.
00023  *
00024  * Contributor(s):
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either the GNU General Public License Version 2 or later (the "GPL"), or
00028  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 /* $Id: cmpcert.c,v 1.4 2004/04/27 23:04:39 gerv%gerv.net Exp $ */
00040 
00041 #include <stdio.h>
00042 #include <string.h>
00043 #include "prerror.h"
00044 #include "secitem.h"
00045 #include "prnetdb.h"
00046 #include "cert.h"
00047 #include "nspr.h"
00048 #include "secder.h"
00049 #include "key.h"
00050 #include "nss.h"
00051 
00052 /*
00053  * Look to see if any of the signers in the cert chain for "cert" are found
00054  * in the list of caNames.  
00055  * Returns SECSuccess if so, SECFailure if not.
00056  */
00057 SECStatus
00058 NSS_CmpCertChainWCANames(CERTCertificate *cert, CERTDistNames *caNames)
00059 {
00060   SECItem *         caname;
00061   CERTCertificate * curcert;
00062   CERTCertificate * oldcert;
00063   PRInt32           contentlen;
00064   int               j;
00065   int               headerlen;
00066   int               depth;
00067   SECStatus         rv;
00068   SECItem           issuerName;
00069   SECItem           compatIssuerName;
00070 
00071   if (!cert || !caNames || !caNames->nnames || !caNames->names ||
00072       !caNames->names->data)
00073     return SECFailure;
00074   depth=0;
00075   curcert = CERT_DupCertificate(cert);
00076   
00077   while( curcert ) {
00078     issuerName = curcert->derIssuer;
00079     
00080     /* compute an alternate issuer name for compatibility with 2.0
00081      * enterprise server, which send the CA names without
00082      * the outer layer of DER hearder
00083      */
00084     rv = DER_Lengths(&issuerName, &headerlen, (uint32 *)&contentlen);
00085     if ( rv == SECSuccess ) {
00086       compatIssuerName.data = &issuerName.data[headerlen];
00087       compatIssuerName.len = issuerName.len - headerlen;
00088     } else {
00089       compatIssuerName.data = NULL;
00090       compatIssuerName.len = 0;
00091     }
00092     
00093     for (j = 0; j < caNames->nnames; j++) {
00094       caname = &caNames->names[j];
00095       if (SECITEM_CompareItem(&issuerName, caname) == SECEqual) {
00096        rv = SECSuccess;
00097        CERT_DestroyCertificate(curcert);
00098        goto done;
00099       } else if (SECITEM_CompareItem(&compatIssuerName, caname) == SECEqual) {
00100        rv = SECSuccess;
00101        CERT_DestroyCertificate(curcert);
00102        goto done;
00103       }
00104     }
00105     if ( ( depth <= 20 ) &&
00106         ( SECITEM_CompareItem(&curcert->derIssuer, &curcert->derSubject)
00107           != SECEqual ) ) {
00108       oldcert = curcert;
00109       curcert = CERT_FindCertByName(curcert->dbhandle,
00110                                 &curcert->derIssuer);
00111       CERT_DestroyCertificate(oldcert);
00112       depth++;
00113     } else {
00114       CERT_DestroyCertificate(curcert);
00115       curcert = NULL;
00116     }
00117   }
00118   rv = SECFailure;
00119   
00120 done:
00121   return rv;
00122 }
00123