diff --git a/doc/examples/cryptoki-demo.cxx b/doc/examples/cryptoki-demo.cxx index 26760f7..cead680 100644 --- a/doc/examples/cryptoki-demo.cxx +++ b/doc/examples/cryptoki-demo.cxx @@ -2,20 +2,16 @@ #include #include -template std::vector toVector(TYPE in[NUM]) { - return std::vector(in, in+NUM); -} -#define VECTOR(ARRAY) toVector(ARRAY) - template void print(const std::vector& v) { - for (typename std::vector::const_iterator it(v.begin()); it!=v.end(); ++it) + for (typename std::vector::const_iterator it(v.begin()); + it!=v.end(); ++it) std::cout<<"Item: "<<*it< v(toVector<4>((int[]){1,2,3,4})); - std::vector v(VECTOR(((int[]){1,2,3,4}))); - print(v); +// //std::vector v(toVector<4>((int[]){1,2,3,4})); +// std::vector v(VECTOR(((int[]){1,2,3,4}))); +// print(v); cryptoki::Init init(argc==2?argv[1]:"onepin-opensc-pkcs11.so"); cryptoki::Info inf(init.info()); std::cout<<"##################### INFO #####################"< #include #include +#include #include // for inline implementations only #include +#include // malloc/free //! C++ Wrapper around Cryptoki API namespace cryptoki { @@ -29,7 +31,14 @@ namespace cryptoki { #endif #define UNDEF_CRYPTOKI_FN_LOG #endif + + template std::vector toVector(TYPE in[NUM]) { + return std::vector(in, in+NUM); + } + #define CRYPTOKI_TO_VECTOR(ARRAY) \ + toVector(ARRAY) + //============================================================================ class exception: public std::exception { public: @@ -64,7 +73,17 @@ namespace cryptoki { typedef std::set MechanismList; typedef std::vector AttributeTypeList; - typedef std::vector AttributeList; + + struct Attribute { + Attribute(CK_ATTRIBUTE& attr): + type(attr.type), value((char*)attr.pValue, attr.ulValueLen) { + free(attr.pValue); + attr.pValue = 0; + } + CK_ATTRIBUTE_TYPE type; + std::string value; + }; + typedef std::map AttributeList; // //! Map Attribute Class to type // /*! @todo to be completed ... */ @@ -1216,14 +1235,32 @@ namespace cryptoki { AttributeList getattributevalue(const AttributeTypeList& attributes) { AttributeList res; - for (AttributeTypeList::const_iterator it(attributes.begin()); - it!=attributes.end(); ++it) - res.push_back((CK_ATTRIBUTE){*it, 0, 0}); - //! calls @c C_GetAttributeValue -// return check(_session._slot._init->_fn->C_GetAttributeValue(_session, _object, -// CK_ATTRIBUTE_PTR, CK_ULONG), -// CRYPTOKI_FN_LOG("C_GetAttributeValue")); - return 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()), + 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()), + CRYPTOKI_FN_LOG("C_GetAttributeValue")); + } + for (AttributeTypeList::size_type i(0); i0) + res.insert(std::make_pair(attrs[i].type, Attribute(attrs[i]))); + return res; + } catch (...) { + for (AttributeTypeList::size_type i(0); i