-
-
Notifications
You must be signed in to change notification settings - Fork 34
/
object.ts
203 lines (172 loc) · 5.72 KB
/
object.ts
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import assert from "assert";
import pkcs11 from "pkcs11js";
import * as graphene from "../src";
import config from "./config";
context("Object", () => {
let mod: graphene.Module;
let session: graphene.Session;
let modulus: Buffer;
let exponent: Buffer;
before(() => {
mod = graphene.Module.load(config.init.lib, config.init.libName);
mod.initialize();
const slot = mod.getSlots(config.controlValues.slot.slotIndex);
session = slot.open(2 | 4);
session.login(config.init.pin);
modulus = new Buffer(1024 / 8);
modulus[127] = 1;
exponent = Buffer.from([1, 0, 1]);
});
after(() => {
session.logout();
mod.finalize();
});
it("copy", () => {
const count = session.find().length;
const obj = session.create({
class: graphene.ObjectClass.PUBLIC_KEY,
keyType: graphene.KeyType.RSA,
wrap: true,
modulus,
publicExponent: exponent,
});
const copy = obj.copy({
label: "new label",
});
assert.equal(!copy, false);
const key = copy.toType<graphene.Key>();
assert.equal(key.label, "new label");
assert.equal(session.find().length, count + 2);
});
it("get attribute by name", () => {
const obj = session.create({
class: graphene.ObjectClass.PUBLIC_KEY,
label: "label",
keyType: graphene.KeyType.RSA,
wrap: true,
modulus,
publicExponent: exponent,
});
const wrap = obj.getAttribute("wrap");
assert.equal(wrap, true);
});
it("get attribute by template", () => {
const obj = session.create({
class: graphene.ObjectClass.PUBLIC_KEY,
label: "label",
keyType: graphene.KeyType.RSA,
wrap: true,
modulus,
publicExponent: exponent,
});
const attrs = obj.getAttribute({
wrap: null,
label: null,
});
assert.equal(attrs.wrap, true);
assert.equal(attrs.label, "label");
});
it("set attribute by name", () => {
const obj = session.create({
class: graphene.ObjectClass.PUBLIC_KEY,
label: "label",
keyType: graphene.KeyType.RSA,
wrap: true,
modulus,
publicExponent: exponent,
});
obj.setAttribute("label", "new label");
assert.equal(obj.getAttribute("label"), "new label");
});
it("set attribute by template", () => {
const obj = session.create({
class: graphene.ObjectClass.PUBLIC_KEY,
label: "label",
keyType: graphene.KeyType.RSA,
wrap: true,
modulus,
publicExponent: exponent,
});
obj.setAttribute({
label: "new label",
});
assert.equal(obj.getAttribute("label"), "new label");
});
it("destroy", () => {
const count = session.find().length;
const obj = session.create({
class: graphene.ObjectClass.PUBLIC_KEY,
label: "label",
keyType: graphene.KeyType.RSA,
wrap: true,
modulus,
publicExponent: exponent,
});
assert.equal(session.find().length, count + 1);
obj.destroy();
assert.equal(session.find().length, count);
});
it("size", () => {
const obj = session.create({
class: graphene.ObjectClass.PUBLIC_KEY,
label: "label",
keyType: graphene.KeyType.RSA,
wrap: true,
modulus,
publicExponent: exponent,
});
// SoftHSM doesn't return real size of object
assert.equal(obj.size > 0, true);
obj.destroy();
});
it("set function", () => {
const obj = session.create({
class: graphene.ObjectClass.DATA,
label: "data.set",
objectId: Buffer.from([1]),
token: false,
value: Buffer.from("Hello"),
});
let data = obj.toType<graphene.Data>();
assert.equal(data.value.toString(), "Hello");
assert.equal(data.token, false);
assert.equal(data.private, true);
assert.equal(data.modifiable, true);
assert.equal(data.objectId.toString("hex"), "01");
data.label = "data.new.label";
const objs = session.find({ label: "data.new.label" });
assert.equal(objs.length, 1);
data = objs.items(0).toType();
assert.equal(data.label, "data.new.label");
data.destroy();
});
context("custom attribute", () => {
let object: graphene.SessionObject;
const attrName = "label";
before(() => {
object = session.create({
class: graphene.ObjectClass.DATA,
label: "data.set",
objectId: Buffer.from("my custom id"),
token: false,
value: Buffer.from("Hello"),
});
// change default type of attribute
graphene.registerAttribute(attrName, pkcs11.CKA_LABEL, "buffer");
});
after(() => {
object.destroy();
// set default value for objectId
graphene.registerAttribute(attrName, pkcs11.CKA_LABEL, "string");
});
it("get value", () => {
const value = object.getAttribute(attrName);
assert.equal(Buffer.isBuffer(value), true);
});
it("set value", () => {
const newValue = "new value";
object.setAttribute(attrName, newValue);
assert.equal(object.getAttribute(attrName), newValue);
});
});
});