find objects

master
Marc Wäckerlin 15 years ago
parent fb85f0b686
commit 1f007bf395
  1. 32
      src/cryptoki.cxx
  2. 111
      src/cryptoki.hxx

@ -205,4 +205,36 @@ namespace cryptoki {
return res; 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;
}
}
} }

@ -70,20 +70,29 @@ namespace cryptoki {
class Slot; class Slot;
typedef std::vector<Slot> SlotList; typedef std::vector<Slot> SlotList;
class Object;
typedef std::vector<Object> ObjectList;
typedef std::set<CK_MECHANISM_TYPE> MechanismList; typedef std::set<CK_MECHANISM_TYPE> MechanismList;
typedef std::vector<CK_ATTRIBUTE_TYPE> AttributeTypeList; typedef std::vector<CK_ATTRIBUTE_TYPE> AttributeTypeList;
struct Attribute { struct Attribute {
Attribute(CK_ATTRIBUTE_TYPE t): type(t) {}
Attribute(CK_ATTRIBUTE& attr): Attribute(CK_ATTRIBUTE& attr):
type(attr.type), value((char*)attr.pValue, attr.ulValueLen) { type(attr.type), value((char*)attr.pValue, attr.ulValueLen) {
free(attr.pValue); free(attr.pValue);
attr.pValue = 0; attr.pValue = 0;
} }
Attribute& operator=(const std::string& v) {
value = v;
return *this;
}
CK_ATTRIBUTE_TYPE type; CK_ATTRIBUTE_TYPE type;
std::string value; std::string value;
}; };
typedef std::map<CK_ATTRIBUTE_TYPE, Attribute> AttributeList; typedef std::map<CK_ATTRIBUTE_TYPE, Attribute> AttributeMap;
typedef std::vector<Attribute> AttributeList;
// //! Map Attribute Class to type // //! Map Attribute Class to type
// /*! @todo to be completed ... */ // /*! @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 /*! @name C Like Error Handling
You are strongly recommended not to disable exception You are strongly recommended not to disable exception
@ -889,6 +908,16 @@ namespace cryptoki {
} }
@endcode */ @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: /*! @todo Not implemented:
@code @code
bool generaterandom() { bool generaterandom() {
@ -1082,12 +1111,12 @@ namespace cryptoki {
friend class Session; friend class Session;
CK_OBJECT_HANDLE _object; CK_OBJECT_HANDLE _object;
Session& _session; Session* _session;
CK_RV _res; CK_RV _res;
bool check(CK_RV result, const std::string& context="") { bool check(CK_RV result, const std::string& context="") {
_res = result; _res = result;
if (_session._slot._init->_exc && !*this) if (_session->_slot._init->_exc && !*this)
if (context.size()) if (context.size())
throw access_error(context+": "+error()); throw access_error(context+": "+error());
else else
@ -1095,14 +1124,22 @@ namespace cryptoki {
return _res==CKR_OK; return _res==CKR_OK;
} }
Object(); //! forbidden Object() {
}
Object(Session& session): _session(session), _res(CKR_OK) { Object(Session& session, CK_OBJECT_HANDLE obj):
//! @todo _object = ??? _session(&session), _object(obj), _res(CKR_OK) {
} }
public: public:
Object& operator=(Object& o) {
_object = o._object;
_session = o._session;
_res = o._res;
return *this;
}
/*! @name C Like Error Handling /*! @name C Like Error Handling
You are strongly recommended not to disable exception You are strongly recommended not to disable exception
@ -1118,7 +1155,7 @@ namespace cryptoki {
/*! @return error text of last cryptoki call */ /*! @return error text of last cryptoki call */
std::string error() { std::string error() {
return _session._slot._init->error(_res); return _session->_slot._init->error(_res);
} }
//@} //@}
@ -1133,7 +1170,7 @@ namespace cryptoki {
@code @code
bool copyobject() { bool copyobject() {
//! calls @c C_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), CK_ATTRIBUTE_PTR, CK_ULONG, CK_OBJECT_HANDLE_PTR),
CRYPTOKI_FN_LOG("C_CopyObject")); CRYPTOKI_FN_LOG("C_CopyObject"));
} }
@ -1143,7 +1180,7 @@ namespace cryptoki {
@code @code
bool createobject() { bool createobject() {
//! calls @c C_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), CK_OBJECT_HANDLE_PTR),
CRYPTOKI_FN_LOG("C_CreateObject")); CRYPTOKI_FN_LOG("C_CreateObject"));
} }
@ -1155,8 +1192,8 @@ namespace cryptoki {
type, param.begin().operator->(), param.size() type, param.begin().operator->(), param.size()
}; };
//! calls @c C_DecryptInit //! calls @c C_DecryptInit
return check(_session._slot._init->_fn->C_DecryptInit return check(_session->_slot._init->_fn->C_DecryptInit
(_session, &mech, key._object), (_session->_session, &mech, key._object),
CRYPTOKI_FN_LOG("C_DecryptInit")); CRYPTOKI_FN_LOG("C_DecryptInit"));
} }
@ -1165,7 +1202,7 @@ namespace cryptoki {
@code @code
bool derivekey() { bool derivekey() {
//! calls @c C_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), CK_ATTRIBUTE_PTR, CK_ULONG, CK_OBJECT_HANDLE_PTR),
CRYPTOKI_FN_LOG("C_DeriveKey")); CRYPTOKI_FN_LOG("C_DeriveKey"));
} }
@ -1175,7 +1212,7 @@ namespace cryptoki {
@code @code
bool destroyobject() { bool destroyobject() {
//! calls @c C_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")); CRYPTOKI_FN_LOG("C_DestroyObject"));
} }
@endcode */ @endcode */
@ -1185,7 +1222,7 @@ namespace cryptoki {
@code @code
bool digestkey() { bool digestkey() {
//! calls @c C_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")); CRYPTOKI_FN_LOG("C_DigestKey"));
} }
@endcode */ @endcode */
@ -1195,27 +1232,17 @@ namespace cryptoki {
@code @code
bool encryptinit() { bool encryptinit() {
//! calls @c C_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")); CRYPTOKI_FN_LOG("C_EncryptInit"));
} }
@endcode */ @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: /*! @todo Not implemented:
@code @code
bool generatekey() { bool generatekey() {
//! calls @c C_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), CK_ULONG, CK_OBJECT_HANDLE_PTR),
CRYPTOKI_FN_LOG("C_GenerateKey")); CRYPTOKI_FN_LOG("C_GenerateKey"));
} }
@ -1226,29 +1253,29 @@ namespace cryptoki {
@code @code
bool generatekeypair() { bool generatekeypair() {
//! calls @c C_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_ULONG, CK_ATTRIBUTE_PTR, CK_ULONG,
CK_OBJECT_HANDLE_PTR, CK_OBJECT_HANDLE_PTR), CK_OBJECT_HANDLE_PTR, CK_OBJECT_HANDLE_PTR),
CRYPTOKI_FN_LOG("C_GenerateKeyPair")); CRYPTOKI_FN_LOG("C_GenerateKeyPair"));
} }
@endcode */ @endcode */
AttributeList getattributevalue(const AttributeTypeList& attributes) { AttributeMap getattributevalue(const AttributeTypeList& attributes) {
AttributeList res; AttributeMap res;
CK_ATTRIBUTE* attrs(new CK_ATTRIBUTE[attributes.size()]); CK_ATTRIBUTE* attrs(new CK_ATTRIBUTE[attributes.size()]);
AttributeTypeList::const_iterator it(attributes.begin()); AttributeTypeList::const_iterator it(attributes.begin());
for (AttributeTypeList::size_type i(0); it!=attributes.end(); ++it, ++i) for (AttributeTypeList::size_type i(0); it!=attributes.end(); ++it, ++i)
attrs[i] = (CK_ATTRIBUTE){*it, 0, 0}; attrs[i] = (CK_ATTRIBUTE){*it, 0, 0};
try { try {
//! calls @c C_GetAttributeValue //! calls @c C_GetAttributeValue
if (check(_session._slot._init->_fn->C_GetAttributeValue if (check(_session->_slot._init->_fn->C_GetAttributeValue
(_session, _object, attrs, attributes.size()), (_session->_session, _object, attrs, attributes.size()),
CRYPTOKI_FN_LOG("C_GetAttributeValue"))) { CRYPTOKI_FN_LOG("C_GetAttributeValue"))) {
for (AttributeTypeList::size_type i(0); i<attributes.size(); ++i) for (AttributeTypeList::size_type i(0); i<attributes.size(); ++i)
if (attrs[i].ulValueLen>0) if (attrs[i].ulValueLen>0)
attrs[i].pValue = malloc(attrs[i].ulValueLen); attrs[i].pValue = malloc(attrs[i].ulValueLen);
check(_session._slot._init->_fn->C_GetAttributeValue check(_session->_slot._init->_fn->C_GetAttributeValue
(_session, _object, attrs, attributes.size()), (_session->_session, _object, attrs, attributes.size()),
CRYPTOKI_FN_LOG("C_GetAttributeValue")); CRYPTOKI_FN_LOG("C_GetAttributeValue"));
} }
for (AttributeTypeList::size_type i(0); i<attributes.size(); ++i) for (AttributeTypeList::size_type i(0); i<attributes.size(); ++i)
@ -1267,7 +1294,7 @@ namespace cryptoki {
@code @code
bool getobjectsize() { bool getobjectsize() {
//! calls @c C_GetObjectSize //! calls @c C_GetObjectSize
return check(_session._slot._init->_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")); CRYPTOKI_FN_LOG("C_GetObjectSize"));
} }
@endcode */ @endcode */
@ -1277,7 +1304,7 @@ namespace cryptoki {
@code @code
bool setattributevalue() { bool setattributevalue() {
//! calls @c C_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), CK_ATTRIBUTE_PTR, CK_ULONG),
CRYPTOKI_FN_LOG("C_SetAttributeValue")); CRYPTOKI_FN_LOG("C_SetAttributeValue"));
} }
@ -1287,7 +1314,7 @@ namespace cryptoki {
@code @code
bool setoperationstate() { bool setoperationstate() {
//! calls @c C_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), CK_OBJECT_HANDLE, CK_OBJECT_HANDLE),
CRYPTOKI_FN_LOG("C_SetOperationState")); CRYPTOKI_FN_LOG("C_SetOperationState"));
} }
@ -1297,7 +1324,7 @@ namespace cryptoki {
@code @code
bool signinit() { bool signinit() {
//! calls @c C_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")); CRYPTOKI_FN_LOG("C_SignInit"));
} }
@endcode */ @endcode */
@ -1307,7 +1334,7 @@ namespace cryptoki {
@code @code
bool signrecoverinit() { bool signrecoverinit() {
//! calls @c C_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")); CRYPTOKI_FN_LOG("C_SignRecoverInit"));
} }
@endcode */ @endcode */
@ -1316,7 +1343,7 @@ namespace cryptoki {
@code @code
bool unwrapkey() { bool unwrapkey() {
//! calls @c C_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_BYTE_PTR, CK_ULONG, CK_ATTRIBUTE_PTR, CK_ULONG,
CK_OBJECT_HANDLE_PTR), CK_OBJECT_HANDLE_PTR),
CRYPTOKI_FN_LOG("C_UnwrapKey")); CRYPTOKI_FN_LOG("C_UnwrapKey"));
@ -1327,7 +1354,7 @@ namespace cryptoki {
@code @code
bool verifyinit() { bool verifyinit() {
//! calls @c C_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")); CRYPTOKI_FN_LOG("C_VerifyInit"));
} }
@endcode */ @endcode */
@ -1337,7 +1364,7 @@ namespace cryptoki {
@code @code
bool verifyrecoverinit() { bool verifyrecoverinit() {
//! calls @c C_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")); CRYPTOKI_FN_LOG("C_VerifyRecoverInit"));
} }
@endcode */ @endcode */
@ -1347,7 +1374,7 @@ namespace cryptoki {
@code @code
bool wrapkey() { bool wrapkey() {
//! calls @c C_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), CK_OBJECT_HANDLE, CK_BYTE_PTR, CK_ULONG_PTR),
CRYPTOKI_FN_LOG("C_WrapKey")); CRYPTOKI_FN_LOG("C_WrapKey"));
} }

Loading…
Cancel
Save