Exemple #1
0
OSLock::OSLock(bool exceptionAllowed)
{
    int rc;

    lockData = NULL;
    if (!needThread) {
	return;
    }
#ifdef MAC
    if (!OSLock_attr_init) {
	rc = pthread_mutexattr_init(&OSLock_attr);
	if (rc < 0) {
	   if (exceptionAllowed) {
		throw PKCS11Exception(CKR_DEVICE_ERROR, "lock init failed");
	   } else {
		return;
	   }
	}
	OSLock_attr_init = 1;
    }
#endif
    lockData = new OSLockData;
    if (lockData) {
	rc = pthread_mutex_init(&lockData->mutex, &OSLock_attr);
	if (rc < 0) {
	    delete lockData;
	    lockData = NULL;
	}
    } 
    if (exceptionAllowed && !lockData) {
	throw PKCS11Exception(CKR_HOST_MEMORY, "lock allocation failed");
    }
}
CK_BBOOL StorageObject::readBBoolFromAttribute( const CK_ATTRIBUTE& a_Attribute ) {

    if( a_Attribute.ulValueLen != sizeof( CK_BBOOL ) ) {

        throw PKCS11Exception( CKR_ATTRIBUTE_VALUE_INVALID );
    }

    CK_BBOOL val = *(CK_BBOOL*)a_Attribute.pValue;

    if( ( val != 0x00 ) && ( val != 0x01 ) ) {

        throw PKCS11Exception( CKR_ATTRIBUTE_VALUE_INVALID );
    }

    return val;
}
void StorageObject::putU4ArrayInAttribute( Marshaller::u4Array* value,CK_ATTRIBUTE_PTR attribute)
{
    if( !attribute->pValue ) {

        if( !value ) {

            attribute->ulValueLen = 0;
        
        } else {

            attribute->ulValueLen = (value->GetLength() * 4);
        }

        return;
    }

    if( !value ) {

        attribute->ulValueLen = 0;
        
        return;
    }

    if( attribute->ulValueLen < ( value->GetLength( ) * 4 ) ) {

        attribute->ulValueLen = CK_UNAVAILABLE_INFORMATION;
        
        throw PKCS11Exception( CKR_BUFFER_TOO_SMALL );
    }

    attribute->ulValueLen = value->GetLength() * 4;
    
    memcpy((CK_BYTE_PTR)attribute->pValue,(u1*)value->GetBuffer(),attribute->ulValueLen);
}
void StorageObject::getAttribute( CK_ATTRIBUTE_PTR attribute ) {

    switch( attribute->type )
    {
        case CKA_CLASS:
            StorageObject::putULongInAttribute(m_Class,attribute);
            break;

        case CKA_PRIVATE:
            StorageObject::putBBoolInAttribute(m_Private,attribute);
            break;

        case CKA_TOKEN:
            StorageObject::putBBoolInAttribute(m_Token,attribute);
            break;

        case CKA_MODIFIABLE:
            StorageObject::putBBoolInAttribute(m_Modifiable,attribute);
            break;

        case CKA_LABEL:
			StorageObject::putU1ArrayInAttribute(m_pLabel.get( ),attribute);
            break;

        default:
           attribute->ulValueLen = CK_UNAVAILABLE_INFORMATION;

           throw PKCS11Exception( CKR_ATTRIBUTE_TYPE_INVALID );
    }
}
Exemple #5
0
FileLog::FileLog(const char *filename )
{
    file = fopen(filename, "at");
    if( file == NULL ) {
        throw PKCS11Exception(CKR_GENERAL_ERROR, "Failed to open logfile");
    }
}
void KeyObject::setAttribute( const CK_ATTRIBUTE& a_Attribute, const bool& objCreation ) {

    if( !a_Attribute.ulValueLen )
    {
        return;
    }

    if( !objCreation ){

        switch(a_Attribute.type){

        case CKA_KEY_TYPE:
        case CKA_LOCAL:
        case CKA_MECHANISM_TYPE:
            throw PKCS11Exception( CKR_ATTRIBUTE_READ_ONLY );
        }
    }

    switch(a_Attribute.type){

    case CKA_KEY_TYPE:
        _keyType = StorageObject::readULongFromAttribute( a_Attribute );
        break;

    case CKA_ID:
        m_pID.reset( StorageObject::readU1ArrayFromAttribute( a_Attribute ) );
        break;

    case CKA_START_DATE:
        m_pStartDate.reset( StorageObject::readDateFromAttribute( a_Attribute ) );
        break;

    case CKA_END_DATE:
        m_pEndDate.reset( StorageObject::readDateFromAttribute( a_Attribute ) );
        break;

    case CKA_LOCAL:
        _local = StorageObject::readBBoolFromAttribute( a_Attribute );
        break;

    case CKA_DERIVE:
        _derive = StorageObject::readBBoolFromAttribute( a_Attribute );
        break;

    case CKA_MECHANISM_TYPE:
        _mechanismType = StorageObject::readULongFromAttribute( a_Attribute );
        break;

    case CKA_ALLOWED_MECHANISMS:
        m_pAllowedMechanism.reset( new Marshaller::u4Array( a_Attribute.ulValueLen / 4 ) );
        memcpy( (unsigned char*)m_pAllowedMechanism->GetBuffer( ), (CK_BYTE_PTR)a_Attribute.pValue, a_Attribute.ulValueLen );
        break;

    default:
        StorageObject::setAttribute( a_Attribute, objCreation );

    }
}
Marshaller::u1Array* StorageObject::readDateFromAttribute( const CK_ATTRIBUTE& a_Attribute ) {

    if( a_Attribute.ulValueLen != 8 ) {

        throw PKCS11Exception( CKR_ATTRIBUTE_VALUE_INVALID );
    }

    return StorageObject::readU1ArrayFromAttribute( a_Attribute );
}
CK_ULONG StorageObject::readULongFromAttribute( const CK_ATTRIBUTE&  a_Attribute ) {

    if( a_Attribute.ulValueLen != sizeof( CK_ULONG ) ) {

        throw PKCS11Exception( CKR_ATTRIBUTE_VALUE_INVALID );
    }

    return *(CK_ULONG*)a_Attribute.pValue;
}
void StorageObject::setAttribute( const CK_ATTRIBUTE& a_attribute, const bool& a_objCreation) {

   if( !a_attribute.ulValueLen ) {

      return;
   }

    if( !a_objCreation ) {

        switch( a_attribute.type ) {

            case CKA_CLASS:
            case CKA_PRIVATE:
            case CKA_TOKEN:
            case CKA_MODIFIABLE:
                throw PKCS11Exception( CKR_ATTRIBUTE_READ_ONLY );
        }
    }

    switch( a_attribute.type ) {

        case CKA_CLASS:
            break;

        case CKA_PRIVATE:
            m_Private = StorageObject::readBBoolFromAttribute( a_attribute );
            break;

        case CKA_TOKEN:
            m_Token = StorageObject::readBBoolFromAttribute( a_attribute );
            break;

        case CKA_MODIFIABLE:
            m_Modifiable = StorageObject::readBBoolFromAttribute( a_attribute );
            break;

        case CKA_LABEL:
				m_pLabel.reset( StorageObject::readU1ArrayFromAttribute( a_attribute ) );
            break;

        default:
            throw PKCS11Exception( CKR_ATTRIBUTE_TYPE_INVALID );
    }
}
Exemple #10
0
OSLock::OSLock(bool exceptionAllowed)
{
    if (!needThread) {
	lockData = NULL;
	return;
    }
    lockData = new OSLockData;
    if (lockData) {
	InitializeCriticalSection(&lockData->mutex);
    } 
    if (exceptionAllowed && !lockData) {
	throw PKCS11Exception(CKR_HOST_MEMORY, "lock allocation failed");
    }
}
void Pkcs11ObjectKeyPublic::setAttribute( const CK_ATTRIBUTE& a_Attribute, const bool& objCreation)
{
    //if( 0 == a_Attribute.ulValueLen )
    //{
    //    return;
    //}

    if(objCreation == CK_FALSE){
        switch(a_Attribute.type){
        case CKA_ENCRYPT:
        case CKA_TRUSTED:
        case CKA_VERIFY:
        case CKA_VERIFY_RECOVER:
        case CKA_WRAP:
            if(*(CK_BBOOL*)a_Attribute.pValue == CK_TRUE){
                throw PKCS11Exception( CKR_ATTRIBUTE_READ_ONLY );
            }
            break;
        }
    }

    switch(a_Attribute.type){

    case CKA_ENCRYPT:
        _encrypt = StorageObject::readBBoolFromAttribute( a_Attribute );
        break;

    case CKA_VERIFY:
        _verify = StorageObject::readBBoolFromAttribute( a_Attribute );
        break;

    case CKA_VERIFY_RECOVER:
        _verifyRecover = StorageObject::readBBoolFromAttribute( a_Attribute );
        break;

    case CKA_WRAP:
        _wrap = StorageObject::readBBoolFromAttribute( a_Attribute );
        break;

    case CKA_SUBJECT:
        m_pSubject.reset( StorageObject::readU1ArrayFromAttribute( a_Attribute ) );
        break;

    default:
        KeyObject::setAttribute(a_Attribute,objCreation);
    }
}
void StorageObject::putULongInAttribute( const CK_ULONG& value, CK_ATTRIBUTE_PTR attribute ) {

    if( !attribute->pValue ) {

        attribute->ulValueLen = sizeof( CK_ULONG );

        return;
    }

    if( attribute->ulValueLen < sizeof( CK_ULONG ) ) {

        attribute->ulValueLen = CK_UNAVAILABLE_INFORMATION;
        
		throw PKCS11Exception( CKR_BUFFER_TOO_SMALL );
    }

    attribute->ulValueLen = sizeof( CK_ULONG );

    *(CK_ULONG*)attribute->pValue = value;
}