-
Notifications
You must be signed in to change notification settings - Fork 15
/
attribute.h
93 lines (68 loc) · 2.47 KB
/
attribute.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#ifndef ST_ATTRIBUTE_H
#define ST_ATTRIBUTE_H
#include <memory>
#include <vector>
#include "pkcs11/pkcs11u.h"
#include "pkcs11/pkcs11.h"
struct attribute_t {
attribute_t() : attr_({0,0,0}) {}
attribute_t(const CK_ATTRIBUTE& other);
attribute_t(CK_ATTRIBUTE_TYPE type, CK_VOID_PTR value, CK_ULONG size);
attribute_t(CK_ATTRIBUTE_TYPE type, const std::string& string);
attribute_t(CK_ATTRIBUTE_TYPE type, const std::vector<char>& bytes);
template <typename T>
attribute_t(CK_ATTRIBUTE_TYPE type, const T& object) {
CK_ATTRIBUTE other;
other.type = type;
other.pValue = const_cast<T*>(&object);
other.ulValueLen = sizeof(object);
this->operator=(other);
}
bool operator==(const attribute_t& other) const;
bool operator!=(const attribute_t& other) const;
attribute_t& operator=(const CK_ATTRIBUTE& other);
void apply(CK_ATTRIBUTE& dst) const;
const CK_ATTRIBUTE* operator->() const {
return &attr_;
}
template<typename T>
inline T value() const {
return reinterpret_cast<T>(attr_.pValue);
}
template<typename T>
inline T to_value() const {
return (attr_.pValue)
? *(reinterpret_cast<T*>(attr_.pValue))
: 0;
}
inline CK_OBJECT_HANDLE to_handle() const {
return to_value<CK_OBJECT_HANDLE>();
}
inline CK_ULONG to_id() const {
return to_value<CK_ULONG>();
}
inline const std::string to_object_id() const {
return to_string();
}
inline const std::string to_string() const {
return (attr_.pValue)
? std::string(reinterpret_cast<char*>(attr_.pValue), attr_.ulValueLen)
: std::string("");
}
inline const std::vector<unsigned char> to_bytes() const {
return std::vector<unsigned char>(reinterpret_cast<unsigned char*>(attr_.pValue), reinterpret_cast<unsigned char*>(attr_.pValue) + attr_.ulValueLen);
}
inline bool to_bool() const {
return attr_.pValue && *(reinterpret_cast<CK_BBOOL*>(attr_.pValue)) == CK_TRUE;
}
inline CK_OBJECT_CLASS to_class() const {
if (attr_.ulValueLen != sizeof(CK_OBJECT_CLASS)) {
throw std::runtime_error("can't cast to CK_OBJECT_CLASS: invalid value length");
}
return to_value<CK_OBJECT_CLASS>();
}
private:
CK_ATTRIBUTE attr_;
std::shared_ptr<void> ptr_;
};
#endif