tempCert = CERT_DecodeDERCertificate( &certList->certs[count], PR_FALSE, NULL);
tempCert = CERT_FindCertByDERCert(CERT_GetDefaultCertDB(), &certList->certs[count]);
SEC_PKCS12AddCert(SEC_PKCS12ExportContext *p12ctxt, SEC_PKCS12SafeInfo *safe, 		  void *nestedDest, CERTCertificate *cert, 		  CERTCertDBHandle *certDb, SECItem *keyId,		  PRBool includeCertChain){    sec_PKCS12CertBag *certBag;    sec_PKCS12SafeBag *safeBag;    void *mark;    SECStatus rv;    SECItem nick = {siBuffer, NULL,0};    if(!p12ctxt || !cert) {	return SECFailure;    }    mark = PORT_ArenaMark(p12ctxt->arena);    /* allocate the cert bag */    certBag = sec_PKCS12NewCertBag(p12ctxt->arena,     				   SEC_OID_PKCS9_X509_CERT);    if(!certBag) {	goto loser;    }    if(SECITEM_CopyItem(p12ctxt->arena, &certBag->value.x509Cert,     			&cert->derCert) != SECSuccess) {	PORT_SetError(SEC_ERROR_NO_MEMORY);	goto loser;    }    /* if the cert chain is to be included, we should only be exporting     * the cert from our internal database.     */    if(includeCertChain) {	CERTCertificateList *certList = CERT_CertChainFromCert(cert,							       certUsageSSLClient,							       PR_TRUE);	unsigned int count = 0;	if(!certList) {	    PORT_SetError(SEC_ERROR_NO_MEMORY);	    goto loser;	}	/* add cert chain */	for(count = 0; count < (unsigned int)certList->len; count++) {	    if(SECITEM_CompareItem(&certList->certs[count], &cert->derCert)	    			!= SECEqual) {	    	CERTCertificate *tempCert;		/* decode the certificate */	    	 tempCert = 			CERT_DecodeDERCertificate( &certList->certs[count], 					PR_FALSE, NULL);	    	if(!tempCert) {		    CERT_DestroyCertificateList(certList);		    goto loser;		}		/* add the certificate */	    	if(SEC_PKCS12AddCert(p12ctxt, safe, nestedDest, tempCert,				 certDb, NULL, PR_FALSE) != SECSuccess) {		    CERT_DestroyCertificate(tempCert);		    CERT_DestroyCertificateList(certList);		    goto loser;		}		CERT_DestroyCertificate(tempCert);	    }	}	CERT_DestroyCertificateList(certList);    }    /* if the certificate has a nickname, we will set the friendly name     * to that.     */    if(cert->nickname) {        if (cert->slot && !PK11_IsInternal(cert->slot)) {	  /*	   * The cert is coming off of an external token, 	   * let's strip the token name from the nickname	   * and only add what comes after the colon as the	   * nickname. -javi	   */	    char *delimit;	    	    delimit = PORT_Strchr(cert->nickname,':');	    if (delimit == NULL) {	        nick.data = (unsigned char *)cert->nickname;		nick.len = PORT_Strlen(cert->nickname);	    } else {	        delimit++;	        nick.data = (unsigned char *)PORT_ArenaStrdup(p12ctxt->arena,							      delimit);		nick.len = PORT_Strlen(delimit);	    }	} else {	    nick.data = (unsigned char *)cert->nickname;	    nick.len = PORT_Strlen(cert->nickname);	}    }    safeBag = sec_PKCS12CreateSafeBag(p12ctxt, SEC_OID_PKCS12_V1_CERT_BAG_ID,     				      certBag);    if(!safeBag) {	goto loser;    }    /* add the friendly name and keyId attributes, if necessary */    if(nick.data) {	if(sec_PKCS12AddAttributeToBag(p12ctxt, safeBag, 				       SEC_OID_PKCS9_FRIENDLY_NAME, &nick) 				       != SECSuccess) {	    goto loser;	}    }	       if(keyId) {	if(sec_PKCS12AddAttributeToBag(p12ctxt, safeBag, SEC_OID_PKCS9_LOCAL_KEY_ID,				       keyId) != SECSuccess) {	    goto loser;	}    }    /* append the cert safeBag */    if(nestedDest) {	rv = sec_pkcs12_append_bag_to_safe_contents(p12ctxt->arena, 					  (sec_PKCS12SafeContents*)nestedDest, 					   safeBag);    } else {	rv = sec_pkcs12_append_bag(p12ctxt, safe, safeBag);    }    if(rv != SECSuccess) {	goto loser;    }    PORT_ArenaUnmark(p12ctxt->arena, mark);    return SECSuccess;loser:    if(mark) {	PORT_ArenaRelease(p12ctxt->arena, mark);    }    return SECFailure;}