cert->nickname = (char *)PORT_ArenaAlloc(cert->arena, PORT_Strlen(label)+1); if(cert->nickname == NULL) { return CKR_HOST_MEMORY; } PORT_Memcpy(cert->nickname, label, PORT_Strlen(label));
cert->nickname = PORT_ArenaStrdup(cert->arena, label);
pk11_handleCertObject(PK11Session *session,PK11Object *object){    PK11Attribute *attribute;    CK_CERTIFICATE_TYPE type;    SECItem derCert;    char *label;    CERTCertDBHandle *handle;    CERTCertificate *cert;    CK_RV crv;    /* certificates must have a type */    if ( !pk11_hasAttribute(object,CKA_CERTIFICATE_TYPE) ) {	return CKR_TEMPLATE_INCOMPLETE;    }    /* we can't store any certs private */    if (pk11_isTrue(object,CKA_PRIVATE)) {	return CKR_ATTRIBUTE_VALUE_INVALID;    }	    /* We only support X.509 Certs for now */    attribute = pk11_FindAttribute(object,CKA_CERTIFICATE_TYPE);    if (attribute == NULL) return CKR_TEMPLATE_INCOMPLETE;    type = *(CK_CERTIFICATE_TYPE *)attribute->attrib.pValue;    pk11_FreeAttribute(attribute);    if (type != CKC_X_509) {	return CKR_ATTRIBUTE_VALUE_INVALID;    }    /* X.509 Certificate */    /* make sure we have a cert */    if ( !pk11_hasAttribute(object,CKA_VALUE) ) {	return CKR_TEMPLATE_INCOMPLETE;    }    /* in PKCS #11, Subject is a required field */    if ( !pk11_hasAttribute(object,CKA_SUBJECT) ) {	return CKR_TEMPLATE_INCOMPLETE;    }    /*     * now parse the certificate     */    handle = CERT_GetDefaultCertDB();    /* get the nickname */    label = pk11_getString(object,CKA_LABEL);    object->label = label;    if (label == NULL) {	return CKR_HOST_MEMORY;    }      /* get the der cert */     attribute = pk11_FindAttribute(object,CKA_VALUE);    derCert.data = (unsigned char *)attribute->attrib.pValue;    derCert.len = attribute->attrib.ulValueLen ;        cert = CERT_NewTempCertificate(handle, &derCert, label, PR_FALSE, PR_TRUE);    pk11_FreeAttribute(attribute);    if (cert == NULL) {	return CKR_ATTRIBUTE_VALUE_INVALID;    }    /* add it to the object */    object->objectInfo = cert;    object->infoFree = (PK11Free) CERT_DestroyCertificate;        /* now just verify the required date fields */    crv = pk11_defaultAttribute(object, CKA_ID, NULL, 0);    if (crv != CKR_OK) { return crv; }    crv = pk11_defaultAttribute(object,CKA_ISSUER,				pk11_item_expand(&cert->derIssuer));    if (crv != CKR_OK) { return crv; }    crv = pk11_defaultAttribute(object,CKA_SERIAL_NUMBER,				pk11_item_expand(&cert->serialNumber));    if (crv != CKR_OK) { return crv; }    if (pk11_isTrue(object,CKA_TOKEN)) {	SECCertUsage *certUsage = NULL;	CERTCertTrust trust = { CERTDB_USER, CERTDB_USER, CERTDB_USER };	attribute = pk11_FindAttribute(object,CKA_NETSCAPE_TRUST);	if(attribute) {	    certUsage = (SECCertUsage*)attribute->attrib.pValue;	    pk11_FreeAttribute(attribute);	}	/* Temporary for PKCS 12 */	if(cert->nickname == NULL) {	    /* use the arena so we at least don't leak memory  */	    cert->nickname = (char *)PORT_ArenaAlloc(cert->arena,							PORT_Strlen(label)+1);	    if(cert->nickname == NULL) {		return CKR_HOST_MEMORY;	    }	    PORT_Memcpy(cert->nickname, label, PORT_Strlen(label));	}	/* only add certs that have a private key */	if (SECKEY_KeyForCertExists(SECKEY_GetDefaultKeyDB(),cert) 							!= SECSuccess) {	    return CKR_ATTRIBUTE_VALUE_INVALID;	}	if (!cert->isperm) {	    if (CERT_AddTempCertToPerm(cert, label, &trust) != SECSuccess) {		return CKR_HOST_MEMORY;	    }        } else {	    CERT_ChangeCertTrust(cert->dbhandle,cert,&trust);	}	if(certUsage) {	    if(CERT_ChangeCertTrustByUsage(CERT_GetDefaultCertDB(),				cert, *certUsage) != SECSuccess) {		return CKR_HOST_MEMORY;	    }	}	object->handle |= (PK11_TOKEN_MAGIC | PK11_TOKEN_TYPE_CERT);	object->inDB = PR_TRUE;    }    /* label has been adopted by object->label */    /*PORT_Free(label); */    return CKR_OK;}