|
|
|
@ -24,14 +24,53 @@ |
|
|
|
|
#include <memory> |
|
|
|
|
#include <cryptaux.hxx> |
|
|
|
|
|
|
|
|
|
#include <iostream> // debug |
|
|
|
|
|
|
|
|
|
/*! @defgroup gcryptoki C++ Wrapper around Cryptoki API */ |
|
|
|
|
//@{
|
|
|
|
|
/*! @defgroup cryptokilib Cryptoki C++ Library */ |
|
|
|
|
/*! @defgroup globaloperator Global Operator */ |
|
|
|
|
/*! @defgroup cryptokitypes Cryptoki C++ Types and Auxiliary */ |
|
|
|
|
/*! @defgroup cryptokiexceptions Cryptoki Exceptions */ |
|
|
|
|
|
|
|
|
|
#ifndef CRYPTOKI_FN_LOG |
|
|
|
|
#include <iostream> |
|
|
|
|
#if __GNUC__ >= 2 |
|
|
|
|
//! Cryptoki Error Message Formatting
|
|
|
|
|
/*! If you want to change cryptoki error formatting, just
|
|
|
|
|
redefine your own CRYPTOKY_FN_LOG macro before <code>#include |
|
|
|
|
<cryptoki.hxx></code>. |
|
|
|
|
#return std::String */ |
|
|
|
|
#define CRYPTOKI_FN_LOG(X) (std::string(X " failed in ") \ |
|
|
|
|
+std::string(__PRETTY_FUNCTION__)) |
|
|
|
|
#else |
|
|
|
|
#define CRYPTOKI_QUOTE(X) #X |
|
|
|
|
//! Cryptoki Error Message Formatting
|
|
|
|
|
/*! If you want to change cryptoki error formatting, just
|
|
|
|
|
redefine your own CRYPTOKY_FN_LOG macro before <code>#include |
|
|
|
|
<cryptoki.hxx></code>. |
|
|
|
|
#return std::String */ |
|
|
|
|
#define CRYPTOKI_FN_LOG(X) X " failed in \ |
|
|
|
|
" __FILE__ ":" CRYPTOKI_QUOTE(__LINE__) |
|
|
|
|
#endif |
|
|
|
|
#endif |
|
|
|
|
#ifndef CRYPTOKI_LOG |
|
|
|
|
#include <iostream> |
|
|
|
|
#if __GNUC__ >= 2 |
|
|
|
|
//! Cryptoki Logging
|
|
|
|
|
/*! If you want to change cryptoki logging mechanism, just
|
|
|
|
|
redefine your own CRYPTOKY_LOG macro before <code>#include |
|
|
|
|
<cryptoki.hxx></code>. Define it empty for no logging at |
|
|
|
|
all. By default logs to <code>std::clog</code>. */ |
|
|
|
|
#define CRYPTOKI_LOG(X) std::clog<<X<<" @ "<<__PRETTY_FUNCTION__<<std::endl |
|
|
|
|
#else |
|
|
|
|
//! Cryptoki Logging
|
|
|
|
|
/*! If you want to change cryptoki logging mechanism, just
|
|
|
|
|
redefine your own CRYPTOKY_LOG macro before <code>#include |
|
|
|
|
<cryptoki.hxx></code>. Define it empty for no logging at |
|
|
|
|
all. By default logs to <code>std::clog</code>. */ |
|
|
|
|
#define CRYPTOKI_LOG(X) std::clog<<X<<" @ "<<__FILE__<<__LINE__<<std::endl |
|
|
|
|
#endif |
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
//! @see gcryptoki
|
|
|
|
|
namespace cryptoki { |
|
|
|
|
|
|
|
|
@ -148,17 +187,6 @@ namespace cryptoki { |
|
|
|
|
T* _p; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
#ifndef CRYPTOKI_FN_LOG |
|
|
|
|
#if __GNUC__ >= 2 |
|
|
|
|
#define CRYPTOKI_FN_LOG(X) (std::string(X " failed in ") \ |
|
|
|
|
+std::string(__PRETTY_FUNCTION__)) |
|
|
|
|
#else |
|
|
|
|
#define CRYPTOKI_FN_LOG(X) X " failed in \ |
|
|
|
|
" __FILE__ ":" CRYPTOKI_QUOTE(__LINE__) |
|
|
|
|
#endif |
|
|
|
|
#define UNDEF_CRYPTOKI_FN_LOG |
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
//! @addtogroup cryptokitypes
|
|
|
|
|
//@{
|
|
|
|
|
|
|
|
|
@ -342,7 +370,7 @@ namespace cryptoki { |
|
|
|
|
default: return crypto::readable(value); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
template<typename TYPE> Attribute from(const TYPE& v) { |
|
|
|
|
template<typename TYPE> Attribute& from(const TYPE& v) { |
|
|
|
|
value = std::string((char*)&v, sizeof(TYPE)); |
|
|
|
|
return *this; |
|
|
|
|
} |
|
|
|
@ -803,6 +831,17 @@ namespace cryptoki { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public: |
|
|
|
|
|
|
|
|
|
//! Don't use without assignment! For standard containers only!
|
|
|
|
|
Slot(): _init(0) { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Slot& operator=(const Slot& o) { |
|
|
|
|
_init = o._init; |
|
|
|
|
_slot = o._slot; |
|
|
|
|
_res = o._res; |
|
|
|
|
return *this; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/*! @name C Like Error Handling
|
|
|
|
|
|
|
|
|
@ -1052,69 +1091,6 @@ namespace cryptoki { |
|
|
|
|
//! Create a new object.
|
|
|
|
|
Object create(const AttributeList& attrs); |
|
|
|
|
|
|
|
|
|
std::string decrypt(std::string in) { |
|
|
|
|
std::string res; |
|
|
|
|
res.resize(in.size()); |
|
|
|
|
CK_ULONG size(res.size()); //! @todo check if size is ok (seems so ...)
|
|
|
|
|
//! calls @c C_Decrypt
|
|
|
|
|
check(_slot._init->_fn->C_Decrypt |
|
|
|
|
(_session, |
|
|
|
|
(unsigned char*)in.begin().operator->(), in.size(), |
|
|
|
|
(unsigned char*)res.begin().operator->(), &size), |
|
|
|
|
CRYPTOKI_FN_LOG("C_Decrypt")); |
|
|
|
|
res.resize(size); |
|
|
|
|
return res; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
std::string decryptdigestupdate(std::string in) { |
|
|
|
|
std::string res; |
|
|
|
|
res.resize(in.size()); |
|
|
|
|
CK_ULONG size(res.size()); //! @todo check if size is ok
|
|
|
|
|
//! calls @c C_DecryptDigestUpdate
|
|
|
|
|
check(_slot._init->_fn->C_DecryptDigestUpdate |
|
|
|
|
(_session, |
|
|
|
|
(unsigned char*)in.begin().operator->(), in.size(), |
|
|
|
|
(unsigned char*)res.begin().operator->(), &size), |
|
|
|
|
CRYPTOKI_FN_LOG("C_DecryptDigestUpdate")); |
|
|
|
|
res.resize(size); |
|
|
|
|
return res; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
bool decryptfinal() { |
|
|
|
|
//! calls @c C_DecryptFinal
|
|
|
|
|
return check(_slot._init->_fn->C_DecryptFinal(_session, 0, 0), |
|
|
|
|
CRYPTOKI_FN_LOG("C_DecryptFinal")); |
|
|
|
|
//! @todo does this work?
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
std::string decryptupdate(std::string in) { |
|
|
|
|
std::string res; |
|
|
|
|
res.resize(in.size()); |
|
|
|
|
CK_ULONG size(res.size()); //! @todo check if size is ok
|
|
|
|
|
//! calls @c C_DecryptUpdate
|
|
|
|
|
check(_slot._init->_fn->C_DecryptUpdate |
|
|
|
|
(_session, |
|
|
|
|
(unsigned char*)in.begin().operator->(), in.size(), |
|
|
|
|
(unsigned char*)res.begin().operator->(), &size), |
|
|
|
|
CRYPTOKI_FN_LOG("C_DecryptUpdate")); |
|
|
|
|
res.resize(size); |
|
|
|
|
return res; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
std::string decryptverifyupdate(std::string in) { |
|
|
|
|
std::string res; |
|
|
|
|
res.resize(in.size()); |
|
|
|
|
CK_ULONG size(res.size()); //! @todo check if size is ok
|
|
|
|
|
//! calls @c C_DecryptVerifyUpdate
|
|
|
|
|
check(_slot._init->_fn->C_DecryptVerifyUpdate |
|
|
|
|
(_session, |
|
|
|
|
(unsigned char*)in.begin().operator->(), in.size(), |
|
|
|
|
(unsigned char*)res.begin().operator->(), &size), |
|
|
|
|
CRYPTOKI_FN_LOG("C_DecryptVerifyUpdate")); |
|
|
|
|
res.resize(size); |
|
|
|
|
return res; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
std::string digest(std::string in) { |
|
|
|
|
std::string res; |
|
|
|
|
res.resize(in.size()); |
|
|
|
@ -1170,43 +1146,6 @@ namespace cryptoki { |
|
|
|
|
} |
|
|
|
|
@endcode */ |
|
|
|
|
|
|
|
|
|
std::string encrypt(std::string in) { |
|
|
|
|
std::string res; |
|
|
|
|
res.resize(in.size()); |
|
|
|
|
CK_ULONG size(res.size()); //! @todo check if size is ok
|
|
|
|
|
//! calls @c C_Encrypt
|
|
|
|
|
check(_slot._init->_fn->C_Encrypt |
|
|
|
|
(_session, |
|
|
|
|
(unsigned char*)in.begin().operator->(), in.size(), |
|
|
|
|
(unsigned char*)res.begin().operator->(), &size), |
|
|
|
|
CRYPTOKI_FN_LOG("C_Encrypt")); |
|
|
|
|
res.resize(size); |
|
|
|
|
return res; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/*! @todo Not implemented:
|
|
|
|
|
@code |
|
|
|
|
bool encryptfinal() { |
|
|
|
|
//! calls @c C_EncryptFinal
|
|
|
|
|
return check(_slot._init->_fn->C_EncryptFinal(_session, CK_BYTE_PTR, CK_ULONG_PTR), |
|
|
|
|
CRYPTOKI_FN_LOG("C_EncryptFinal")); |
|
|
|
|
} |
|
|
|
|
@endcode */ |
|
|
|
|
|
|
|
|
|
std::string encryptupdate(std::string in) { |
|
|
|
|
std::string res; |
|
|
|
|
res.resize(in.size()); |
|
|
|
|
CK_ULONG size(res.size()); //! @todo check if size is ok
|
|
|
|
|
//! calls @c C_EncryptUpdate
|
|
|
|
|
check(_slot._init->_fn->C_EncryptUpdate |
|
|
|
|
(_session, |
|
|
|
|
(unsigned char*)in.begin().operator->(), in.size(), |
|
|
|
|
(unsigned char*)res.begin().operator->(), &size), |
|
|
|
|
CRYPTOKI_FN_LOG("C_EncryptUpdate")); |
|
|
|
|
res.resize(size); |
|
|
|
|
return res; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/*! @todo Not implemented:
|
|
|
|
|
@code |
|
|
|
|
bool findobjectsfinal() { |
|
|
|
@ -1478,6 +1417,23 @@ namespace cryptoki { |
|
|
|
|
return *this; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/*! @name Comfortable Access
|
|
|
|
|
|
|
|
|
|
Use these methods in favour of the Low Level Cryptoki |
|
|
|
|
Functions. They provide a higher level simpler access. */ |
|
|
|
|
//@{
|
|
|
|
|
|
|
|
|
|
std::string decrypt(std::string data, CK_MECHANISM_TYPE type, |
|
|
|
|
std::string param=std::string()) { |
|
|
|
|
CRYPTOKI_LOG("decryptinit"); |
|
|
|
|
decryptinit(type, param); |
|
|
|
|
CRYPTOKI_LOG("decrypt"); |
|
|
|
|
return decrypt(data); |
|
|
|
|
//! @todo don't call decryptfinal()?
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
//@}
|
|
|
|
|
|
|
|
|
|
/*! @name C Like Error Handling
|
|
|
|
|
|
|
|
|
|
You are strongly recommended not to disable exception |
|
|
|
@ -1513,18 +1469,90 @@ namespace cryptoki { |
|
|
|
|
CRYPTOKI_FN_LOG("C_CopyObject")); |
|
|
|
|
} |
|
|
|
|
@endcode */ |
|
|
|
|
|
|
|
|
|
//! Call this method on a key object only.
|
|
|
|
|
|
|
|
|
|
bool decryptinit(CK_MECHANISM_TYPE type, std::string param) { |
|
|
|
|
CK_MECHANISM mech = { |
|
|
|
|
type, param.begin().operator->(), param.size() |
|
|
|
|
type, param.size()?¶m[0]:0, param.size() |
|
|
|
|
}; |
|
|
|
|
CRYPTOKI_LOG("decryptinit: type="<<type<<"; mech=("<<mech.mechanism |
|
|
|
|
<<", "<<mech.pParameter<<", "<<mech.ulParameterLen<<')'); |
|
|
|
|
//! calls @c C_DecryptInit
|
|
|
|
|
return check(_session._slot._init->_fn->C_DecryptInit |
|
|
|
|
(_session._session, &mech, _object), |
|
|
|
|
CRYPTOKI_FN_LOG("C_DecryptInit")); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
//! requires decryptinit to be called before
|
|
|
|
|
std::string decrypt(std::string in) { |
|
|
|
|
std::string res; |
|
|
|
|
CK_ULONG size(0); // two calls, first to get minimum buffer length
|
|
|
|
|
CRYPTOKI_LOG("get size"); |
|
|
|
|
//! calls @c C_Decrypt
|
|
|
|
|
check(_session._slot._init->_fn->C_Decrypt |
|
|
|
|
(_session._session, |
|
|
|
|
(unsigned char*)&in[0], in.size(), 0, &size), |
|
|
|
|
CRYPTOKI_FN_LOG("C_Decrypt")); |
|
|
|
|
CRYPTOKI_LOG("maximum size is "<<size<<"Bytes"); |
|
|
|
|
res.resize(size, 0); |
|
|
|
|
check(_session._slot._init->_fn->C_Decrypt |
|
|
|
|
(_session._session, |
|
|
|
|
(unsigned char*)&in[0], in.size(), |
|
|
|
|
(unsigned char*)&res[0], &size), |
|
|
|
|
CRYPTOKI_FN_LOG("C_Decrypt")); |
|
|
|
|
CRYPTOKI_LOG("exact size is "<<size<<"Bytes"); |
|
|
|
|
res.resize(size); |
|
|
|
|
return res; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
std::string decryptdigestupdate(std::string in) { |
|
|
|
|
std::string res; |
|
|
|
|
res.resize(in.size()); |
|
|
|
|
CK_ULONG size(res.size()); //! @todo check if size is ok
|
|
|
|
|
//! calls @c C_DecryptDigestUpdate
|
|
|
|
|
check(_session._slot._init->_fn->C_DecryptDigestUpdate |
|
|
|
|
(_session._session, |
|
|
|
|
(unsigned char*)in.begin().operator->(), in.size(), |
|
|
|
|
(unsigned char*)res.begin().operator->(), &size), |
|
|
|
|
CRYPTOKI_FN_LOG("C_DecryptDigestUpdate")); |
|
|
|
|
res.resize(size); |
|
|
|
|
return res; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
bool decryptfinal() { |
|
|
|
|
//! calls @c C_DecryptFinal
|
|
|
|
|
return check(_session._slot._init->_fn->C_DecryptFinal |
|
|
|
|
(_session._session, 0, 0), |
|
|
|
|
CRYPTOKI_FN_LOG("C_DecryptFinal")); |
|
|
|
|
//! @todo does this work?
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
std::string decryptupdate(std::string in) { |
|
|
|
|
std::string res; |
|
|
|
|
res.resize(in.size()); |
|
|
|
|
CK_ULONG size(res.size()); //! @todo check if size is ok
|
|
|
|
|
//! calls @c C_DecryptUpdate
|
|
|
|
|
check(_session._slot._init->_fn->C_DecryptUpdate |
|
|
|
|
(_session._session, |
|
|
|
|
(unsigned char*)in.begin().operator->(), in.size(), |
|
|
|
|
(unsigned char*)res.begin().operator->(), &size), |
|
|
|
|
CRYPTOKI_FN_LOG("C_DecryptUpdate")); |
|
|
|
|
res.resize(size); |
|
|
|
|
return res; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
std::string decryptverifyupdate(std::string in) { |
|
|
|
|
std::string res; |
|
|
|
|
res.resize(in.size()); |
|
|
|
|
CK_ULONG size(res.size()); //! @todo check if size is ok
|
|
|
|
|
//! calls @c C_DecryptVerifyUpdate
|
|
|
|
|
check(_session._slot._init->_fn->C_DecryptVerifyUpdate |
|
|
|
|
(_session._session, |
|
|
|
|
(unsigned char*)in.begin().operator->(), in.size(), |
|
|
|
|
(unsigned char*)res.begin().operator->(), &size), |
|
|
|
|
CRYPTOKI_FN_LOG("C_DecryptVerifyUpdate")); |
|
|
|
|
res.resize(size); |
|
|
|
|
return res; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/*! @todo Not implemented:
|
|
|
|
|
@code |
|
|
|
@ -1563,6 +1591,42 @@ namespace cryptoki { |
|
|
|
|
} |
|
|
|
|
@endcode */ |
|
|
|
|
|
|
|
|
|
std::string encrypt(std::string in) { |
|
|
|
|
std::string res; |
|
|
|
|
res.resize(in.size()); |
|
|
|
|
CK_ULONG size(res.size()); //! @todo check if size is ok
|
|
|
|
|
//! calls @c C_Encrypt
|
|
|
|
|
check(_session._slot._init->_fn->C_Encrypt |
|
|
|
|
(_session._session, |
|
|
|
|
(unsigned char*)in.begin().operator->(), in.size(), |
|
|
|
|
(unsigned char*)res.begin().operator->(), &size), |
|
|
|
|
CRYPTOKI_FN_LOG("C_Encrypt")); |
|
|
|
|
res.resize(size); |
|
|
|
|
return res; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/*! @todo Not implemented:
|
|
|
|
|
@code |
|
|
|
|
bool encryptfinal() { |
|
|
|
|
//! calls @c C_EncryptFinal
|
|
|
|
|
return check(_session._slot._init->_fn->C_EncryptFinal(_session._session, CK_BYTE_PTR, CK_ULONG_PTR), |
|
|
|
|
CRYPTOKI_FN_LOG("C_EncryptFinal")); |
|
|
|
|
} |
|
|
|
|
@endcode */ |
|
|
|
|
|
|
|
|
|
std::string encryptupdate(std::string in) { |
|
|
|
|
std::string res; |
|
|
|
|
res.resize(in.size()); |
|
|
|
|
CK_ULONG size(res.size()); //! @todo check if size is ok
|
|
|
|
|
//! calls @c C_EncryptUpdate
|
|
|
|
|
check(_session._slot._init->_fn->C_EncryptUpdate |
|
|
|
|
(_session._session, |
|
|
|
|
(unsigned char*)in.begin().operator->(), in.size(), |
|
|
|
|
(unsigned char*)res.begin().operator->(), &size), |
|
|
|
|
CRYPTOKI_FN_LOG("C_EncryptUpdate")); |
|
|
|
|
res.resize(size); |
|
|
|
|
return res; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/*! @todo Not implemented:
|
|
|
|
|
@code |
|
|
|
@ -1882,12 +1946,23 @@ namespace cryptoki { |
|
|
|
|
|
|
|
|
|
}; |
|
|
|
|
//@}
|
|
|
|
|
|
|
|
|
|
#ifdef UNDEF_CRYPTOKI_FN_LOG // cleanup if it was set in here
|
|
|
|
|
#undef CRYPTOKI_FN_LOG |
|
|
|
|
#undef CRYPTOKI_QUOTE |
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
//! @groupadd globaloperator
|
|
|
|
|
//@{
|
|
|
|
|
inline cryptoki::AttributeList& operator<<(cryptoki::AttributeList& list, |
|
|
|
|
const cryptoki::Attribute& attr) { |
|
|
|
|
list.push_back(attr); |
|
|
|
|
return list; |
|
|
|
|
} |
|
|
|
|
inline cryptoki::AttributeList operator<<(const cryptoki::AttributeList& list, |
|
|
|
|
const cryptoki::Attribute& attr) { |
|
|
|
|
cryptoki::AttributeList res(list); |
|
|
|
|
res.push_back(attr); |
|
|
|
|
return res; |
|
|
|
|
} |
|
|
|
|
//@}
|
|
|
|
|
|
|
|
|
|
//@}
|
|
|
|
|
#endif |
|
|
|
|