From 1f007bf39570f9b855285b9460c32a0740866bf3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20W=C3=A4ckerlin?= Date: Tue, 1 Sep 2009 09:11:11 +0000 Subject: [PATCH] find objects --- src/cryptoki.cxx | 32 ++++++++++++++ src/cryptoki.hxx | 111 +++++++++++++++++++++++++++++------------------ 2 files changed, 101 insertions(+), 42 deletions(-) diff --git a/src/cryptoki.cxx b/src/cryptoki.cxx index 0b8f1c4..1bf9424 100644 --- a/src/cryptoki.cxx +++ b/src/cryptoki.cxx @@ -204,5 +204,37 @@ namespace cryptoki { delete[] slots; return res; } + + //============================================================================ + + ObjectList Session::find(const AttributeList& attrs) { + ObjectList res; + CK_ATTRIBUTE* a(0); + try { + if (attrs.size()) { + //! @todo imlement attribute filtering + } + //! calls @c C_FindObjectsInit + if (check(_slot._init->_fn->C_FindObjectsInit + (_session, a, attrs.size()), + CRYPTOKI_FN_LOG("C_FindObjectsInit"))) { + CK_OBJECT_HANDLE obj; + //! calls @c C_FindObjects + for (CK_ULONG objs(0); + check(_slot._init->_fn->C_FindObjects + (_session, &obj, 1, &objs), + CRYPTOKI_FN_LOG("C_FindObjects")) && objs; + res.push_back(Object(*this, obj))); + } + //! calls @c C_FindObjectsFinal + check(_slot._init->_fn->C_FindObjectsFinal(_session), + CRYPTOKI_FN_LOG("C_FindObjectsFinal")); + delete[] a; + return res; + } catch (...) { + delete[] a; + throw; + } + } } diff --git a/src/cryptoki.hxx b/src/cryptoki.hxx index 67845da..17cfcdd 100644 --- a/src/cryptoki.hxx +++ b/src/cryptoki.hxx @@ -69,21 +69,30 @@ namespace cryptoki { class Slot; typedef std::vector SlotList; + + class Object; + typedef std::vector ObjectList; typedef std::set MechanismList; typedef std::vector AttributeTypeList; struct Attribute { + Attribute(CK_ATTRIBUTE_TYPE t): type(t) {} Attribute(CK_ATTRIBUTE& attr): type(attr.type), value((char*)attr.pValue, attr.ulValueLen) { free(attr.pValue); attr.pValue = 0; } + Attribute& operator=(const std::string& v) { + value = v; + return *this; + } CK_ATTRIBUTE_TYPE type; std::string value; }; - typedef std::map AttributeList; + typedef std::map AttributeMap; + typedef std::vector AttributeList; // //! Map Attribute Class to type // /*! @todo to be completed ... */ @@ -675,6 +684,16 @@ namespace cryptoki { } } + /*! @name Comfortable Access + + Use these methods in favour of the Low Level Cryptoki + Functions. They provide a higher level simpler access. */ + //@{ + + ObjectList find(const AttributeList& attrs=AttributeList()); + + //@} + /*! @name C Like Error Handling You are strongly recommended not to disable exception @@ -889,6 +908,16 @@ namespace cryptoki { } @endcode */ + /*! @todo Not implemented: + @code + bool findobjects() { + //! calls @c C_FindObjects + return check(_session._slot._init->_fn->C_FindObjects(_session, CK_OBJECT_HANDLE_PTR, CK_ULONG, + CK_ULONG_PTR), + CRYPTOKI_FN_LOG("C_FindObjects")); + } + @endcode */ + /*! @todo Not implemented: @code bool generaterandom() { @@ -1082,12 +1111,12 @@ namespace cryptoki { friend class Session; CK_OBJECT_HANDLE _object; - Session& _session; + Session* _session; CK_RV _res; bool check(CK_RV result, const std::string& context="") { _res = result; - if (_session._slot._init->_exc && !*this) + if (_session->_slot._init->_exc && !*this) if (context.size()) throw access_error(context+": "+error()); else @@ -1095,14 +1124,22 @@ namespace cryptoki { return _res==CKR_OK; } - Object(); //! forbidden + Object() { + } - Object(Session& session): _session(session), _res(CKR_OK) { - //! @todo _object = ??? + Object(Session& session, CK_OBJECT_HANDLE obj): + _session(&session), _object(obj), _res(CKR_OK) { } public: + Object& operator=(Object& o) { + _object = o._object; + _session = o._session; + _res = o._res; + return *this; + } + /*! @name C Like Error Handling You are strongly recommended not to disable exception @@ -1118,7 +1155,7 @@ namespace cryptoki { /*! @return error text of last cryptoki call */ std::string error() { - return _session._slot._init->error(_res); + return _session->_slot._init->error(_res); } //@} @@ -1133,7 +1170,7 @@ namespace cryptoki { @code bool copyobject() { //! calls @c C_CopyObject - return check(_session._slot._init->_fn->C_CopyObject(_session, CK_OBJECT_HANDLE, + return check(_session->_slot._init->_fn->C_CopyObject(_session->_session, CK_OBJECT_HANDLE, CK_ATTRIBUTE_PTR, CK_ULONG, CK_OBJECT_HANDLE_PTR), CRYPTOKI_FN_LOG("C_CopyObject")); } @@ -1143,7 +1180,7 @@ namespace cryptoki { @code bool createobject() { //! calls @c C_CreateObject - return check(_session._slot._init->_fn->C_CreateObject(_session, CK_ATTRIBUTE_PTR, CK_ULONG, + return check(_session->_slot._init->_fn->C_CreateObject(_session->_session, CK_ATTRIBUTE_PTR, CK_ULONG, CK_OBJECT_HANDLE_PTR), CRYPTOKI_FN_LOG("C_CreateObject")); } @@ -1155,8 +1192,8 @@ namespace cryptoki { type, param.begin().operator->(), param.size() }; //! calls @c C_DecryptInit - return check(_session._slot._init->_fn->C_DecryptInit - (_session, &mech, key._object), + return check(_session->_slot._init->_fn->C_DecryptInit + (_session->_session, &mech, key._object), CRYPTOKI_FN_LOG("C_DecryptInit")); } @@ -1165,7 +1202,7 @@ namespace cryptoki { @code bool derivekey() { //! calls @c C_DeriveKey - return check(_session._slot._init->_fn->C_DeriveKey(_session, CK_MECHANISM_PTR, CK_OBJECT_HANDLE, + return check(_session->_slot._init->_fn->C_DeriveKey(_session->_session, CK_MECHANISM_PTR, CK_OBJECT_HANDLE, CK_ATTRIBUTE_PTR, CK_ULONG, CK_OBJECT_HANDLE_PTR), CRYPTOKI_FN_LOG("C_DeriveKey")); } @@ -1175,7 +1212,7 @@ namespace cryptoki { @code bool destroyobject() { //! calls @c C_DestroyObject - return check(_session._slot._init->_fn->C_DestroyObject(_session, CK_OBJECT_HANDLE), + return check(_session->_slot._init->_fn->C_DestroyObject(_session->_session, CK_OBJECT_HANDLE), CRYPTOKI_FN_LOG("C_DestroyObject")); } @endcode */ @@ -1185,7 +1222,7 @@ namespace cryptoki { @code bool digestkey() { //! calls @c C_DigestKey - return check(_session._slot._init->_fn->C_DigestKey(_session, CK_OBJECT_HANDLE), + return check(_session->_slot._init->_fn->C_DigestKey(_session->_session, CK_OBJECT_HANDLE), CRYPTOKI_FN_LOG("C_DigestKey")); } @endcode */ @@ -1195,27 +1232,17 @@ namespace cryptoki { @code bool encryptinit() { //! calls @c C_EncryptInit - return check(_session._slot._init->_fn->C_EncryptInit(_session, CK_MECHANISM_PTR, CK_OBJECT_HANDLE), + return check(_session->_slot._init->_fn->C_EncryptInit(_session->_session, CK_MECHANISM_PTR, CK_OBJECT_HANDLE), CRYPTOKI_FN_LOG("C_EncryptInit")); } @endcode */ - /*! @todo Not implemented: - @code - bool findobjects() { - //! calls @c C_FindObjects - return check(_session._slot._init->_fn->C_FindObjects(_session, CK_OBJECT_HANDLE_PTR, CK_ULONG, - CK_ULONG_PTR), - CRYPTOKI_FN_LOG("C_FindObjects")); - } - @endcode */ - /*! @todo Not implemented: @code bool generatekey() { //! calls @c C_GenerateKey - return check(_session._slot._init->_fn->C_GenerateKey(_session, CK_MECHANISM_PTR, CK_ATTRIBUTE_PTR, + return check(_session->_slot._init->_fn->C_GenerateKey(_session->_session, CK_MECHANISM_PTR, CK_ATTRIBUTE_PTR, CK_ULONG, CK_OBJECT_HANDLE_PTR), CRYPTOKI_FN_LOG("C_GenerateKey")); } @@ -1226,29 +1253,29 @@ namespace cryptoki { @code bool generatekeypair() { //! calls @c C_GenerateKeyPair - return check(_session._slot._init->_fn->C_GenerateKeyPair(_session, CK_MECHANISM_PTR, CK_ATTRIBUTE_PTR, + return check(_session->_slot._init->_fn->C_GenerateKeyPair(_session->_session, CK_MECHANISM_PTR, CK_ATTRIBUTE_PTR, CK_ULONG, CK_ATTRIBUTE_PTR, CK_ULONG, CK_OBJECT_HANDLE_PTR, CK_OBJECT_HANDLE_PTR), CRYPTOKI_FN_LOG("C_GenerateKeyPair")); } @endcode */ - AttributeList getattributevalue(const AttributeTypeList& attributes) { - AttributeList res; + AttributeMap getattributevalue(const AttributeTypeList& attributes) { + AttributeMap res; CK_ATTRIBUTE* attrs(new CK_ATTRIBUTE[attributes.size()]); AttributeTypeList::const_iterator it(attributes.begin()); for (AttributeTypeList::size_type i(0); it!=attributes.end(); ++it, ++i) attrs[i] = (CK_ATTRIBUTE){*it, 0, 0}; try { //! calls @c C_GetAttributeValue - if (check(_session._slot._init->_fn->C_GetAttributeValue - (_session, _object, attrs, attributes.size()), + if (check(_session->_slot._init->_fn->C_GetAttributeValue + (_session->_session, _object, attrs, attributes.size()), CRYPTOKI_FN_LOG("C_GetAttributeValue"))) { for (AttributeTypeList::size_type i(0); i0) attrs[i].pValue = malloc(attrs[i].ulValueLen); - check(_session._slot._init->_fn->C_GetAttributeValue - (_session, _object, attrs, attributes.size()), + check(_session->_slot._init->_fn->C_GetAttributeValue + (_session->_session, _object, attrs, attributes.size()), CRYPTOKI_FN_LOG("C_GetAttributeValue")); } for (AttributeTypeList::size_type i(0); i_fn->C_GetObjectSize(_session, CK_OBJECT_HANDLE, CK_ULONG_PTR), + return check(_session->_slot._init->_fn->C_GetObjectSize(_session->_session, CK_OBJECT_HANDLE, CK_ULONG_PTR), CRYPTOKI_FN_LOG("C_GetObjectSize")); } @endcode */ @@ -1277,7 +1304,7 @@ namespace cryptoki { @code bool setattributevalue() { //! calls @c C_SetAttributeValue - return check(_session._slot._init->_fn->C_SetAttributeValue(_session, CK_OBJECT_HANDLE, + return check(_session->_slot._init->_fn->C_SetAttributeValue(_session->_session, CK_OBJECT_HANDLE, CK_ATTRIBUTE_PTR, CK_ULONG), CRYPTOKI_FN_LOG("C_SetAttributeValue")); } @@ -1287,7 +1314,7 @@ namespace cryptoki { @code bool setoperationstate() { //! calls @c C_SetOperationState - return check(_session._slot._init->_fn->C_SetOperationState(_session, CK_BYTE_PTR, CK_ULONG, + return check(_session->_slot._init->_fn->C_SetOperationState(_session->_session, CK_BYTE_PTR, CK_ULONG, CK_OBJECT_HANDLE, CK_OBJECT_HANDLE), CRYPTOKI_FN_LOG("C_SetOperationState")); } @@ -1297,7 +1324,7 @@ namespace cryptoki { @code bool signinit() { //! calls @c C_SignInit - return check(_session._slot._init->_fn->C_SignInit(_session, CK_MECHANISM_PTR, CK_OBJECT_HANDLE), + return check(_session->_slot._init->_fn->C_SignInit(_session->_session, CK_MECHANISM_PTR, CK_OBJECT_HANDLE), CRYPTOKI_FN_LOG("C_SignInit")); } @endcode */ @@ -1307,7 +1334,7 @@ namespace cryptoki { @code bool signrecoverinit() { //! calls @c C_SignRecoverInit - return check(_session._slot._init->_fn->C_SignRecoverInit(_session, CK_MECHANISM_PTR, CK_OBJECT_HANDLE), + return check(_session->_slot._init->_fn->C_SignRecoverInit(_session->_session, CK_MECHANISM_PTR, CK_OBJECT_HANDLE), CRYPTOKI_FN_LOG("C_SignRecoverInit")); } @endcode */ @@ -1316,7 +1343,7 @@ namespace cryptoki { @code bool unwrapkey() { //! calls @c C_UnwrapKey - return check(_session._slot._init->_fn->C_UnwrapKey(_session, CK_MECHANISM_PTR, CK_OBJECT_HANDLE, + return check(_session->_slot._init->_fn->C_UnwrapKey(_session->_session, CK_MECHANISM_PTR, CK_OBJECT_HANDLE, CK_BYTE_PTR, CK_ULONG, CK_ATTRIBUTE_PTR, CK_ULONG, CK_OBJECT_HANDLE_PTR), CRYPTOKI_FN_LOG("C_UnwrapKey")); @@ -1327,7 +1354,7 @@ namespace cryptoki { @code bool verifyinit() { //! calls @c C_VerifyInit - return check(_session._slot._init->_fn->C_VerifyInit(_session, CK_MECHANISM_PTR, CK_OBJECT_HANDLE), + return check(_session->_slot._init->_fn->C_VerifyInit(_session->_session, CK_MECHANISM_PTR, CK_OBJECT_HANDLE), CRYPTOKI_FN_LOG("C_VerifyInit")); } @endcode */ @@ -1337,7 +1364,7 @@ namespace cryptoki { @code bool verifyrecoverinit() { //! calls @c C_VerifyRecoverInit - return check(_session._slot._init->_fn->C_VerifyRecoverInit(_session, CK_MECHANISM_PTR, CK_OBJECT_HANDLE), + return check(_session->_slot._init->_fn->C_VerifyRecoverInit(_session->_session, CK_MECHANISM_PTR, CK_OBJECT_HANDLE), CRYPTOKI_FN_LOG("C_VerifyRecoverInit")); } @endcode */ @@ -1347,7 +1374,7 @@ namespace cryptoki { @code bool wrapkey() { //! calls @c C_WrapKey - return check(_session._slot._init->_fn->C_WrapKey(_session, CK_MECHANISM_PTR, CK_OBJECT_HANDLE, + return check(_session->_slot._init->_fn->C_WrapKey(_session->_session, CK_MECHANISM_PTR, CK_OBJECT_HANDLE, CK_OBJECT_HANDLE, CK_BYTE_PTR, CK_ULONG_PTR), CRYPTOKI_FN_LOG("C_WrapKey")); }