-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypes.go
213 lines (188 loc) · 4.99 KB
/
types.go
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
204
205
206
207
208
209
210
211
212
213
package certex
/*
#include <stdlib.h>
#include <dlfcn.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include "./headers/cryptoki.h"
#include "./headers/pkcs11def.h"
#include "./headers/pkcs11t.h"
#include "./headers/PKICertexHSM.h"
*/
import "C"
import (
"fmt"
"sync"
"unsafe"
)
type arena []unsafe.Pointer
func (a *arena) Allocate(obj []byte) (C.CK_VOID_PTR, C.CK_ULONG) {
cobj := C.calloc(C.size_t(len(obj)), 1)
*a = append(*a, cobj)
C.memmove(cobj, unsafe.Pointer(&obj[0]), C.size_t(len(obj)))
return C.CK_VOID_PTR(cobj), C.CK_ULONG(len(obj))
}
func (a arena) Free() {
for _, p := range a {
C.free(p)
}
}
// Holds an attribute type/value combination.
type Attribute struct {
Type uint
Value []byte
}
// Hold options for returning a subset of objects from a slot.
//
// The returned object will match all provided parameters. For example, if
// Class=ClassPrivateKey and Label="foo", the returned object must be a
// private key with label "foo".
type Filter struct {
Class Class
Label string
}
// Holds global information about the module.
type Info struct {
// Manufacturer of the implementation. When multiple PKCS #11 devices are
// present this is used to differentiate devices.
Manufacturer string
// Version of the module.
Version Version
// Human readable description of the module.
Description string
}
// Holds a major and minor version.
type Version struct {
Major uint8
Minor uint8
}
type Cryptoki struct {
// Pointer to the dlopen handle. Kept around to dlfree
// when the Module is closed.
mod unsafe.Pointer
// List of C functions provided by the module.
fl C.CK_FUNCTION_LIST_PTR
mu sync.Mutex
// Version of the module, used for compatibility.
version C.CK_VERSION
// Holds global information about the module.
info Info
}
// Holds an mechanism type/value combination.
type Mechanism struct {
Mechanism uint
Parameter []byte
}
// Provides information about a particular mechanism.
type MechanismInfo struct {
MinKeySize uint
MaxKeySize uint
Flags uint
}
// Represents a single object stored within a slot. For example a key or
// certificate.
type Object struct {
p
o C.CK_OBJECT_HANDLE
c C.CK_OBJECT_CLASS
}
type p struct {
fl C.CK_FUNCTION_LIST_PTR
h C.CK_SESSION_HANDLE
}
// Returns the type of the object stored. For example, certificate, public
// key, or private key.
func (o Object) Class() Class {
return Class(int(o.c))
}
// Value returns an object's CKA_VALUE attribute, as bytes.
func (o Object) Value() ([]byte, error) {
return o.Attribute(CKA_VALUE)
}
// Attribute gets exactly one attribute from a PKCS#11 object, returning
// an error if the attribute is not found, or if multiple attributes are
// returned. On success, it will return the value of that attribute as a slice
// of bytes. For attributes not present (i.e. CKR_ATTRIBUTE_TYPE_INVALID),
// Attribute returns a nil slice and nil error.
func (o Object) Attribute(attributeType uint) ([]byte, error) {
attrs, err := o.GetAttributeValue([]*Attribute{NewAttribute(attributeType, nil)})
// The PKCS#11 spec states that C_GetAttributeValue may return
// CKR_ATTRIBUTE_TYPE_INVALID if an object simply does not posses a given
// attribute. We don't consider that an error, we just consider that
// equivalent to an empty value.
if err != nil {
return nil, err
}
if len(attrs) == 0 {
return nil, fmt.Errorf("Attribute not found")
}
if len(attrs) > 1 {
return nil, fmt.Errorf("too many attributes found")
}
return attrs[0].Value, nil
}
// SessionInfo provides information about a session.
type SessionInfo struct {
SlotID uint
State uint
Flags uint
DeviceError uint
}
// Represents a session to a slot.
//
// A session holds a listable set of objects, such as certificates and
// cryptographic keys.
type Slot struct {
p
rw bool
id uint32
}
// Holds the SlotID which for which an slot event (token insertion,
// removal, etc.) occurred.
type SlotEvent struct {
SlotID uint
}
type SlotOptions struct {
AdminPIN string
PIN string
Label string
}
// Holds information about the slot and underlying token.
type SlotInfo struct {
Label string
Model string
Serial string
Description string
}
// Holds configuration options for the slot session.
type Options struct {
PIN string
AdminPIN string
// Indicates that the slot should be opened with write capabilities,
// such as generating keys or importing certificates.
//
// By default, sessions can access objects and perform signing requests.
ReadWrite bool
}
// Obtains information about a particular token
type TokenInfo struct {
Label string
ManufacturerID string
Model string
SerialNumber string
Flags uint
MaxSessionCount uint
SessionCount uint
MaxRwSessionCount uint
RwSessionCount uint
MaxPinLen uint
MinPinLen uint
TotalPublicMemory uint
FreePublicMemory uint
TotalPrivateMemory uint
FreePrivateMemory uint
HardwareVersion Version
FirmwareVersion Version
TimeUTC string
}