PORT_Assert(att->attrib.ulValueLen == sizeof(CK_KEY_TYPE)); if (att->attrib.ulValueLen != sizeof(CK_KEY_TYPE)) { pk11_FreeAttribute(att); pk11_FreeObject(key); return CKR_ATTRIBUTE_VALUE_INVALID; } PORT_Memcpy(keyTypePtr, att->attrib.pValue, sizeof(CK_KEY_TYPE));
pk11_InitGeneric(PK11Session *session,PK11SessionContext **contextPtr,		 PK11ContextType ctype,PK11Object **keyPtr,		 CK_OBJECT_HANDLE hKey, CK_KEY_TYPE *keyTypePtr,		 CK_OBJECT_CLASS pubKeyType, CK_ATTRIBUTE_TYPE operation){    PK11Object *key = NULL;    PK11Attribute *att;    PK11SessionContext *context;    /* We can only init if there is not current context active */    if (pk11_ReturnContextByType(session,ctype) != NULL) {	return CKR_OPERATION_ACTIVE;    }    /* find the key */    if (keyPtr) {	unsigned int size;        key = pk11_ObjectFromHandle(hKey,session);        if (key == NULL) {	    return CKR_KEY_HANDLE_INVALID;    	}	/* make sure it's a valid  key for this operation */	if (((key->objclass != CKO_SECRET_KEY) && (key->objclass != pubKeyType))					|| !pk11_isTrue(key,operation)) {	    pk11_FreeObject(key);	    return CKR_KEY_TYPE_INCONSISTENT;	}	/* get the key type */	att = pk11_FindAttribute(key,CKA_KEY_TYPE);	PORT_Assert(att != NULL);	size = sizeof(CK_KEY_TYPE);	if (att->attrib.ulValueLen < size) {	    CK_KEY_TYPE kt;	    int i;	    int shift;#ifdef IS_LITTLE_ENDIAN	    shift = 0;#else	    shift = (att->attrib.ulValueLen - 1) * 8;#endif	    kt = 0;	    for (i=att->attrib.ulValueLen - 1; i>=0; i--) {		kt |= ((unsigned char *)att->attrib.pValue)[i] << shift;#ifdef IS_LITTLE_ENDIAN		shift += 8;#else		shift -= 8;#endif	    }	    *keyTypePtr = kt;	} else { 	    *keyTypePtr = *((CK_KEY_TYPE *)att->attrib.pValue);	}	pk11_FreeAttribute(att);	*keyPtr = key;    }    /* allocate the context structure */    context = (PK11SessionContext *)PORT_Alloc(sizeof(PK11SessionContext));    if (context == NULL) {	if (key) pk11_FreeObject(key);	return CKR_HOST_MEMORY;    }    context->type = ctype;    context->multi = PR_TRUE;    context->cipherInfo = NULL;    context->hashInfo = NULL;    context->doPad = PR_FALSE;    context->padDataLength = 0;    context->key = key;    context->blockSize = 0;    *contextPtr = context;    return CKR_OK;}