diff --git a/src/cryptoki.cxx b/src/cryptoki.cxx index 1bf9424..aec4250 100644 --- a/src/cryptoki.cxx +++ b/src/cryptoki.cxx @@ -212,7 +212,9 @@ namespace cryptoki { CK_ATTRIBUTE* a(0); try { if (attrs.size()) { - //! @todo imlement attribute filtering + a = new CK_ATTRIBUTE[attrs.size()]; + for (AttributeList::size_type i(0); i_fn->C_FindObjectsInit @@ -237,4 +239,39 @@ namespace cryptoki { } } + //---------------------------------------------------------------------------- + Object Session::createCertificate(const std::string& derSubject, + const std::string& desValue) { + AttributeList attrs; + attrs.push_back(Attribute(CKA_CLASS) + .from(CKO_CERTIFICATE)); + attrs.push_back(Attribute(CKA_CERTIFICATE_TYPE) + .from(CKC_X_509)); + attrs.push_back(Attribute(CKA_SUBJECT, derSubject)); + attrs.push_back(Attribute(CKA_VALUE, desValue)); + return create(attrs); + } + + //---------------------------------------------------------------------------- + Object Session::create(const AttributeList& attrs) { + CK_ATTRIBUTE* a(0); + try { + if (attrs.size()) { + a = new CK_ATTRIBUTE[attrs.size()]; + for (AttributeList::size_type i(0); i_fn->C_CreateObject + (_session, a, attrs.size(), &object), + CRYPTOKI_FN_LOG("C_CreateObject")); + delete[] a; + return Object(*this, object); + } catch (...) { + delete[] a; + throw; + } + } + } diff --git a/src/cryptoki.hxx b/src/cryptoki.hxx index 3e52a8d..57aec45 100644 --- a/src/cryptoki.hxx +++ b/src/cryptoki.hxx @@ -36,31 +36,45 @@ namespace cryptoki { #endif #define UNDEF_CRYPTOKI_FN_LOG #endif - - inline std::string hex(const std::string& data) { - std::stringstream res; - for (std::string::const_iterator it(data.begin()); it!=data.end(); ++it) - res<?[\\]^_{|}~"); + ("!\"#%&'()*+,-./:;<=>?[\\]^_{|}~@"); static const std::string BLANK_CHARS (" "); static const std::string VALID_CHARS (LETTER_CHARS+NUMBER_CHARS+GRAFIC_CHARS+BLANK_CHARS); + + inline std::string hex(const std::string& data, + std::string::size_type len=20) { + std::stringstream res; + std::string::size_type pos(0); + for (std::string::const_iterator it(data.begin()); it!=data.end(); ++it) { + res<"; + else if (data.find_first_not_of(VALID_CHARS)(value.begin().operator->()); + a.ulValueLen = value.size(); + return a; + } std::string name() { switch (type) { case CKA_CLASS: return "CLASS"; @@ -213,6 +240,10 @@ namespace cryptoki { default: return readable(value); } } + template Attribute from(const TYPE& v) { + value = std::string((char*)&v, sizeof(TYPE)); + return *this; + } CK_ATTRIBUTE_TYPE type; std::string value; }; @@ -845,8 +876,13 @@ namespace cryptoki { Functions. They provide a higher level simpler access. */ //@{ + //! Get a list of matching objects. ObjectList find(const AttributeList& attrs=AttributeList()); + //! Create a new Certificate Object. + Object createCertificate(const std::string& derSubject, + const std::string& desValue); + //@} /*! @name C Like Error Handling @@ -874,12 +910,15 @@ namespace cryptoki { Direct access to the low level cryptoki API. Better use the comfort methods. */ //@{ + bool cancel() { //! calls @c C_CancelFunction return check(_slot._init->_fn->C_CancelFunction(_session), CRYPTOKI_FN_LOG("C_CancelFunction")); } + //! Create a new object. + Object create(const AttributeList& attrs); std::string decrypt(std::string in) { std::string res; @@ -1343,16 +1382,6 @@ namespace cryptoki { } @endcode */ - /*! @todo Not implemented: - @code - bool createobject() { - //! calls @c C_CreateObject - return check(_session->_slot._init->_fn->C_CreateObject(_session->_session, CK_ATTRIBUTE_PTR, CK_ULONG, - CK_OBJECT_HANDLE_PTR), - CRYPTOKI_FN_LOG("C_CreateObject")); - } - @endcode */ - bool decryptinit(CK_MECHANISM_TYPE type, std::string param, const Object& key) { CK_MECHANISM mech = { @@ -1722,7 +1751,6 @@ namespace cryptoki { //@} }; -#endif #ifdef UNDEF_CRYPTOKI_FN_LOG // cleanup if it was set in here #undef CRYPTOKI_FN_LOG @@ -1730,3 +1758,4 @@ namespace cryptoki { #endif } +#endif