* limitations under the License
*
*
- * @file safe-buffer.h
+ * @file ckm-password.h
* @author Bartlomiej Grzelewski (b.grzelewski@samsung.com)
* @version 1.0
- * @brief Custom allocator for std
+ * @brief Password holder with auto-erase on cleanup
*/
#ifndef _SAFE_PASSWORD_H_
#define _SAFE_PASSWORD_H_
#include <ckm/ckm-raw-buffer.h>
-#include <boost/container/string.hpp>
+#include <string>
namespace CKM {
-typedef boost::container::basic_string<char, std::char_traits<char>, erase_on_dealloc<char>> Password;
+typedef std::basic_string<char, std::char_traits<char>, std_erase_on_dealloc<char>> Password;
} // namespace CKM
-#endif //_ERASE_ON_DEALLOC_H_
+#endif //_SAFE_PASSWORD_H_
* limitations under the License
*
*
- * @file safe-buffer.h
+ * @file ckm-raw-buffer.h
* @author Krzysztof Jackiewicz (k.jackiewicz@samsung.com)
* @version 1.0
* @brief Custom allocator for std
#ifndef _SAFE_BUFFER_H_
#define _SAFE_BUFFER_H_
-#include <boost/container/vector.hpp>
+#include <stddef.h>
+#include <string.h>
+#include <vector>
namespace CKM {
template <typename T>
-struct erase_on_dealloc {
- typedef T value_type;
+struct std_erase_on_dealloc
+{
+ // MJK: if re-factoring, remember not to inherit from the std::allocator !
+ // MJK: to be replaced with much shorter version once std::allocator_traits
+ // becomes supported in STL containers (i.e. list, vector and string)
+ typedef size_t size_type;
+ typedef ptrdiff_t difference_type;
+ typedef T* pointer;
+ typedef const T* const_pointer;
+ typedef T& reference;
+ typedef const T& const_reference;
+ typedef T value_type;
- erase_on_dealloc() noexcept {}
+ std_erase_on_dealloc() = default;
template <typename U>
- erase_on_dealloc (const erase_on_dealloc<U>&) noexcept {}
+ std_erase_on_dealloc(const std_erase_on_dealloc<U>&) {}
- T* allocate (std::size_t n)
- {
+ T* allocate(std::size_t n) {
return static_cast<T*>(::operator new(n*sizeof(T)));
}
- void deallocate (T* p, std::size_t n)
- {
+ void deallocate(T* ptr, std::size_t n) {
// clear the memory before deleting
- memset(p,0,n*sizeof(T));
- ::operator delete(p);
+ memset(ptr, 0 ,n * sizeof(T));
+ ::operator delete(ptr);
+ }
+
+ template<typename _Tp1>
+ struct rebind
+ {
+ typedef std_erase_on_dealloc<_Tp1> other;
+ };
+
+ void construct(pointer p, const T& val) {
+ new (p) T(val);
+ }
+
+ void destroy(pointer p) {
+ p->~T();
+ }
+
+ size_type max_size() const {
+ return size_type(-1);
}
};
template <typename T, typename U>
-constexpr bool operator== (const erase_on_dealloc<T>&, const erase_on_dealloc<U>&) noexcept
-{
+inline bool operator == (const std_erase_on_dealloc<T>&, const std_erase_on_dealloc<U>&) {
return true;
}
template <typename T, typename U>
-constexpr bool operator!= (const erase_on_dealloc<T>&, const erase_on_dealloc<U>&) noexcept
-{
- return false;
+inline bool operator != (const std_erase_on_dealloc<T>& a, const std_erase_on_dealloc<U>& b) {
+ return !(a == b);
}
+
/*
* TODO replace with:
*
*
* typedef SafeBuffer<unsigned char> RawBuffer
*
- * when gcc 4.7/4.8 is available. Also replace boost::vector with std::vector
- * in other parts of code
+ * when gcc 4.7/4.8 is available.
*/
template <typename T>
struct SafeBuffer {
- typedef boost::container::vector<T, erase_on_dealloc<T>> Type;
+ typedef std::vector<T, std_erase_on_dealloc<T>> Type;
};
// used to pass password and raw key data
} // namespace CKM
-#endif //_ERASE_ON_DEALLOC_H_
+#endif //_SAFE_BUFFER_H_
class IStream;
struct PolicySerializable : public Policy, ISerializable {
- PolicySerializable();
- explicit PolicySerializable(const Policy &);
- explicit PolicySerializable(IStream &);
- void Serialize(IStream &) const;
+ PolicySerializable() {};
+ explicit PolicySerializable(const Policy &policy) : Policy(policy) {}
+ explicit PolicySerializable(IStream &stream) {
+ Deserialization::Deserialize(stream, password);
+ Deserialization::Deserialize(stream, extractable);
+ }
+ void Serialize(IStream &stream) const {
+ Serialization::Serialize(stream, password);
+ Serialization::Serialize(stream, extractable);
+ }
};
} // namespace CKM