PK11SlotInfo* internalslot = PK11_GetInternalKeySlot();  if (cert && cert->slot != internalslot) {  rv = PK11_ImportCert(internalslot, cert, CK_INVALID_HANDLE, NULL, PR_FALSE);  PK11_FreeSlot(internalslot);  if (rv != SECSuccess ) { return SECFailure; } } else { PK11_FreeSlot(internalslot); }
CERT_SaveSMimeProfile(CERTCertificate *cert, SECItem *emailProfile,		      SECItem *profileTime){    int64 oldtime;    int64 newtime;    SECStatus rv = SECFailure;    PRBool saveit;    char *emailAddr;    SECItem oldprof, oldproftime;    SECItem *oldProfile = NULL;    SECItem *oldProfileTime = NULL;    PK11SlotInfo *slot = NULL;    NSSCertificate *c;    NSSCryptoContext *cc;    nssSMIMEProfile *stanProfile = NULL;    PRBool freeOldProfile = PR_FALSE;        emailAddr = cert->emailAddr;        if ( emailAddr == NULL ) {	goto loser;    }    c = STAN_GetNSSCertificate(cert);    if (!c) return SECFailure;    cc = c->object.cryptoContext;    if (cc != NULL) {	stanProfile = nssCryptoContext_FindSMIMEProfileForCertificate(cc, c);	if (stanProfile) {	    PORT_Assert(stanProfile->profileData);	    SECITEM_FROM_NSSITEM(&oldprof, stanProfile->profileData);	    oldProfile = &oldprof;	    SECITEM_FROM_NSSITEM(&oldproftime, stanProfile->profileTime);	    oldProfileTime = &oldproftime;	}    } else {	oldProfile = PK11_FindSMimeProfile(&slot, emailAddr, &cert->derSubject, 							&oldProfileTime); 	freeOldProfile = PR_TRUE;    }    saveit = PR_FALSE;        /* both profileTime and emailProfile have to exist or not exist */    if ( emailProfile == NULL ) {	profileTime = NULL;    } else if ( profileTime == NULL ) {	emailProfile = NULL;    }       if ( oldProfileTime == NULL ) {	saveit = PR_TRUE;    } else {	/* there was already a profile for this email addr */	if ( profileTime ) {	    /* we have an old and new profile - save whichever is more recent*/	    if ( oldProfileTime->len == 0 ) {		/* always replace if old entry doesn't have a time */		oldtime = LL_MININT;	    } else {		rv = DER_UTCTimeToTime(&oldtime, oldProfileTime);		if ( rv != SECSuccess ) {		    goto loser;		}	    }	    	    rv = DER_UTCTimeToTime(&newtime, profileTime);	    if ( rv != SECSuccess ) {		goto loser;	    }		    if ( LL_CMP(newtime, >, oldtime ) ) {		/* this is a newer profile, save it and cert */		saveit = PR_TRUE;	    }	} else {	    saveit = PR_TRUE;	}    }    if (saveit) {	if (cc) {	    if (stanProfile) {		/* stanProfile is already stored in the crypto context,		 * overwrite the data		 */		NSSArena *arena = stanProfile->object.arena;		stanProfile->profileTime = nssItem_Create(arena, 		                                          NULL,		                                          profileTime->len,		                                          profileTime->data);		stanProfile->profileData = nssItem_Create(arena, 		                                          NULL,		                                          emailProfile->len,		                                          emailProfile->data);	    } else if (profileTime && emailProfile) {		PRStatus nssrv;		NSSDER subject;		NSSItem profTime, profData;		NSSItem *pprofTime, *pprofData;		NSSITEM_FROM_SECITEM(&subject, &cert->derSubject);		if (profileTime) {		    NSSITEM_FROM_SECITEM(&profTime, profileTime);		    pprofTime = &profTime;		} else {		    pprofTime = NULL;		}		if (emailProfile) {		    NSSITEM_FROM_SECITEM(&profData, emailProfile);		    pprofData = &profData;		} else {		    pprofData = NULL;		}		stanProfile = nssSMIMEProfile_Create(c, pprofTime, pprofData);		if (!stanProfile) goto loser;		nssrv = nssCryptoContext_ImportSMIMEProfile(cc, stanProfile);		rv = (nssrv == PR_SUCCESS) ? SECSuccess : SECFailure;	    }	} else {	    rv = PK11_SaveSMimeProfile(slot, emailAddr, &cert->derSubject, 						emailProfile, profileTime);	}    } else {	rv = SECSuccess;    }loser:    if (oldProfile && freeOldProfile) {    	SECITEM_FreeItem(oldProfile,PR_TRUE);    }    if (oldProfileTime && freeOldProfile) {    	SECITEM_FreeItem(oldProfileTime,PR_TRUE);    }    if (stanProfile) {	nssSMIMEProfile_Destroy(stanProfile);    }    if (slot) {	PK11_FreeSlot(slot);    }        return(rv);}