diff --git a/src/cryptoki.hxx b/src/cryptoki.hxx index 9f2d02e..3e52a8d 100644 --- a/src/cryptoki.hxx +++ b/src/cryptoki.hxx @@ -19,6 +19,7 @@ #include // malloc/free #include // memset #include +#include #include // debug @@ -217,15 +218,31 @@ namespace cryptoki { }; typedef std::map AttributeMap; typedef std::vector AttributeList; - + +// class Class { +// public: +// CK_OBJECT_CLASS class; +// }; + + //class // //! Map Attribute Class to type // /*! @todo to be completed ... */ +// #define CRYPTOKI_DECLARE_ATTR(ATTR_ID, TYPE) \ +// template<> class AttributeType { \ +// public: typedef TYPE Type; \ +// } // template class AttributeType {}; -// template<> class AttributeType { -// public: typedef CK_OBJECT_CLASS Type; -// public: typedef Type Param; -// }; +// CRYPTOKI_DECLARE_ATTR(CKA_CLASS, CK_OBJECT_CLASS); +// CRYPTOKI_DECLARE_ATTR(CKA_HW_FEATURE_TYPE, CK_HW_FEATURE); +// CRYPTOKI_DECLARE_ATTR(CKA_VALUE, FixString<16>); +// CRYPTOKI_DECLARE_ATTR(CKA_RESET_ON_INIT, CK_BBOOL); +// CRYPTOKI_DECLARE_ATTR(CKA_HAS_RESET, CK_BBOOL); +// // CRYPTOKI_DECLARE_ATTR(CKA_VALUE, ); - byte array +// // CRYPTOKI_DECLARE_ATTR(, ); +// // CRYPTOKI_DECLARE_ATTR(, ); +// // CRYPTOKI_DECLARE_ATTR(, ); +// // CRYPTOKI_DECLARE_ATTR(, ); // template<> class AttributeType { // public: typedef CK_KEY_TYPE Type; // public: typedef Type Param; @@ -238,10 +255,7 @@ namespace cryptoki { // public: typedef CKA_BYTE Type; // public: typedef std::string Param; // }; -// template<> class AttributeType<> { -// public: typedef Type; -// public: typedef Type Param; -// }; +// #undef CRYPTOKI_DECLARE_ATTR template class FixString: public std::string { @@ -582,15 +596,6 @@ namespace cryptoki { if (!std::uncaught_exception()) throw; } } - - /*! @todo Not implemented: - @code - bool finalize() { - //! calls @c C_Finalize - return check(_slot._init->_fn->C_Finalize(CK_VOID_PTR), - CRYPTOKI_FN_LOG("C_Finalize")); - } - /*! @name C Like Error Handling @@ -785,6 +790,7 @@ namespace cryptoki { class Session { private: + friend class Login; friend class Object; Slot& _slot; @@ -819,6 +825,11 @@ namespace cryptoki { //! Closes actual session ~Session() { + try { + _login.reset(); + } catch (...) { + if (!std::uncaught_exception()) throw; + } try { //! calls @c C_CloseSession check(_slot._init->_fn->C_CloseSession(_session), @@ -1098,25 +1109,46 @@ namespace cryptoki { } @endcode */ - /*! @todo Not implemented: - @code - bool login() { - //! calls @c C_Login - return check(_slot._init->_fn->C_Login(_session, CK_USER_TYPE, CK_CHAR_PTR, CK_ULONG), - CRYPTOKI_FN_LOG("C_Login")); - } - @endcode */ + class Login { + + public: + + Login(Session& session, + const std::string& pin, + CK_USER_TYPE userType=CKU_USER): _session(session) { + //! calls @c C_Login + _session.check(_session._slot._init->_fn->C_Login + (_session._session, userType, + (CK_CHAR*)pin.c_str(), + pin.size()), + CRYPTOKI_FN_LOG("C_Login")); + } - /*! @todo Not implemented: - @code - bool logout() { - //! calls @c C_Logout - return check(_slot._init->_fn->C_Logout(_session), - CRYPTOKI_FN_LOG("C_Logout")); - } - @endcode */ + ~Login() { + try { + //! calls @c C_Logout + _session.check(_session._slot._init->_fn->C_Logout + (_session._session), + CRYPTOKI_FN_LOG("C_Logout")); + } catch (...) { + if (!std::uncaught_exception()) throw; + } + } + + private: + Session& _session; + + }; + void login(const std::string& pin, CK_USER_TYPE userType=CKU_USER) { + _login = std::auto_ptr(new Login(*this, pin, userType)); + } + void logout() { + _login.reset(); + } + std::auto_ptr _login; + /*! @todo Not implemented: @code bool seedrandom() {