if ((keyTypeLen != sizeof(keyType)) && (keyTypeLen != 1)) { PORT_SetError(SEC_ERROR_BAD_DATABASE); return NULL;
if (keyTypeLen == 8) { keyTypeStorage = *(PRUint32 *) keyString; if (keyTypeStorage == 0) { keyString += sizeof(PRUint32); } keyTypeLen = 4;
pk11_FindSecretKeyAttribute(PK11TokenObject *object, CK_ATTRIBUTE_TYPE type){    NSSLOWKEYPrivateKey *key;    char *label;    unsigned char *keyString;    PK11Attribute *att;    int keyTypeLen;    CK_ULONG keyLen;    CK_KEY_TYPE keyType;    switch (type) {    case CKA_PRIVATE:    case CKA_SENSITIVE:    case CKA_ALWAYS_SENSITIVE:    case CKA_EXTRACTABLE:    case CKA_DERIVE:    case CKA_ENCRYPT:    case CKA_DECRYPT:    case CKA_SIGN:    case CKA_VERIFY:    case CKA_WRAP:    case CKA_UNWRAP:    case CKA_MODIFIABLE:	return (PK11Attribute *) &pk11_StaticTrueAttr;    case CKA_NEVER_EXTRACTABLE:	return (PK11Attribute *) &pk11_StaticFalseAttr;    case CKA_LABEL:        label = nsslowkey_FindKeyNicknameByPublicKey(object->obj.slot->keyDB,				&object->dbKey, object->obj.slot->password);	if (label == NULL) {	   return (PK11Attribute *)&pk11_StaticNullAttr;	}	att = pk11_NewTokenAttribute(type,label,PORT_Strlen(label), PR_TRUE);	PORT_Free(label);	return att;    case CKA_KEY_TYPE:    case CKA_VALUE_LEN:    case CKA_VALUE:	break;    default:	return NULL;    }    key = pk11_GetPrivateKey(object);    if (key == NULL) {	return NULL;    }    switch (type) {    case CKA_KEY_TYPE:	/* handle legacy databases. In legacy databases key_type was stored	 * in host order, with any leading zeros stripped off. Only key types	 * under 0x1f (AES) were stored. We assume that any values which are	 * either 1 byte long (big endian), or have byte[0] between 0 and 	 * 0x1f and bytes[1]-bytes[3] equal to '0' (little endian). All other	 * values are assumed to be from the new database, which is always 4	 * bytes in host order */	keyType=0;	keyString = key->u.rsa.coefficient.data;	keyTypeLen = key->u.rsa.coefficient.len;	/* only length of 1 or 4 are valid */	if ((keyTypeLen != sizeof(keyType)) && (keyTypeLen != 1)) {	    PORT_SetError(SEC_ERROR_BAD_DATABASE);	    return NULL;	}	if ((keyTypeLen == 1)  ||	    ((keyString[0] <= 0x1f) && (keyString[1] == 0) && 	     (keyString[2] == 0)    && (keyString[3] == 0))) {	    keyType = (CK_KEY_TYPE) keyString[0] ;	} else {	    keyType = *(CK_KEY_TYPE *) keyString;	    keyType = PR_ntohl(keyType);	}	return pk11_NewTokenAttribute(type,&keyType,sizeof(keyType),PR_TRUE);    case CKA_VALUE:	return pk11_NewTokenAttribute(type,key->u.rsa.privateExponent.data,				key->u.rsa.privateExponent.len, PR_FALSE);    case CKA_VALUE_LEN:	keyLen=key->u.rsa.privateExponent.len;	return pk11_NewTokenAttribute(type, &keyLen, sizeof(CK_ULONG), PR_TRUE);    }    return NULL;}