Debian Codegraph @

types.go

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 // Copyright 2013 Miek Gieben. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package pkcs11
6
7 /*
•8 #cgo pkg-config: nss
9 #include <stdlib.h>
10 #include <string.h>
11 #include "pkcs11go.h"
12
13 CK_ULONG Index(CK_ULONG_PTR array, CK_ULONG i)
14 {
15 return array[i];
16 }
17
18 static inline void putAttributePval(CK_ATTRIBUTE_PTR a, CK_VOID_PTR pValue)
19 {
20 a->pValue = pValue;
21 }
22
23 static inline void putMechanismParam(CK_MECHANISM_PTR m, CK_VOID_PTR pParameter)
24 {
25 m->pParameter = pParameter;
26 }
27 */
28 import "C"
29
30 import (
31 "fmt"
32 "time"
33 "unsafe"
34 )
35
36 type arena []unsafe.Pointer
37
38 func (a *arena) Allocate(obj []byte) (C.CK_VOID_PTR, C.CK_ULONG) {
39 cobj := C.calloc(C.size_t(len(obj)), 1)
40 *a = append(*a, cobj)
41 C.memmove(cobj, unsafe.Pointer(&obj[0]), C.size_t(len(obj)))
42 return C.CK_VOID_PTR(cobj), C.CK_ULONG(len(obj))
43 }
44
45 func (a arena) Free() {
46 for _, p := range a {
47 C.free(p)
48 }
49 }
50
51 // toList converts from a C style array to a []uint.
52 func toList(clist C.CK_ULONG_PTR, size C.CK_ULONG) []uint {
53 l := make([]uint, int(size))
54 for i := 0; i < len(l); i++ {
55 l[i] = uint(C.Index(clist, C.CK_ULONG(i)))
56 }
57 defer C.free(unsafe.Pointer(clist))
58 return l
59 }
60
61 // cBBool converts a bool to a CK_BBOOL.
62 func cBBool(x bool) C.CK_BBOOL {
63 if x {
64 return C.CK_BBOOL(C.CK_TRUE)
65 }
66 return C.CK_BBOOL(C.CK_FALSE)
67 }
68
69 func uintToBytes(x uint64) []byte {
70 ul := C.CK_ULONG(x)
71 return C.GoBytes(unsafe.Pointer(&ul), C.int(unsafe.Sizeof(ul)))
72 }
73
74 // Error represents an PKCS#11 error.
75 type Error uint
76
77 func (e Error) Error() string {
78 return fmt.Sprintf("pkcs11: 0x%X: %s", uint(e), strerror[uint(e)])
79 }
80
81 func toError(e C.CK_RV) error {
82 if e == C.CKR_OK {
83 return nil
84 }
85 return Error(e)
86 }
87
88 // SessionHandle is a Cryptoki-assigned value that identifies a session.
89 type SessionHandle uint
90
91 // ObjectHandle is a token-specific identifier for an object.
92 type ObjectHandle uint
93
94 // Version represents any version information from the library.
95 type Version struct {
96 Major byte
97 Minor byte
98 }
99
100 func toVersion(version C.CK_VERSION) Version {
101 return Version{byte(version.major), byte(version.minor)}
102 }
103
104 // SlotEvent holds the SlotID which for which an slot event (token insertion,
105 // removal, etc.) occurred.
106 type SlotEvent struct {
107 SlotID uint
108 }
109
110 // Info provides information about the library and hardware used.
111 type Info struct {
112 CryptokiVersion Version
113 ManufacturerID string
114 Flags uint
115 LibraryDescription string
116 LibraryVersion Version
117 }
118
119 // SlotInfo provides information about a slot.
120 type SlotInfo struct {
121 SlotDescription string // 64 bytes.
122 ManufacturerID string // 32 bytes.
123 Flags uint
124 HardwareVersion Version
125 FirmwareVersion Version
126 }
127
128 // TokenInfo provides information about a token.
129 type TokenInfo struct {
130 Label string
131 ManufacturerID string
132 Model string
133 SerialNumber string
134 Flags uint
135 MaxSessionCount uint
136 SessionCount uint
137 MaxRwSessionCount uint
138 RwSessionCount uint
139 MaxPinLen uint
140 MinPinLen uint
141 TotalPublicMemory uint
142 FreePublicMemory uint
143 TotalPrivateMemory uint
144 FreePrivateMemory uint
145 HardwareVersion Version
146 FirmwareVersion Version
147 UTCTime string
148 }
149
150 // SessionInfo provides information about a session.
151 type SessionInfo struct {
152 SlotID uint
153 State uint
154 Flags uint
155 DeviceError uint
156 }
157
158 // Attribute holds an attribute type/value combination.
159 type Attribute struct {
160 Type uint
161 Value []byte
162 }
163
164 // NewAttribute allocates a Attribute and returns a pointer to it.
165 // Note that this is merely a convenience function, as values returned
166 // from the HSM are not converted back to Go values, those are just raw
167 // byte slices.
168 func NewAttribute(typ uint, x interface{}) *Attribute {
169 // This function nicely transforms *to* an attribute, but there is
170 // no corresponding function that transform back *from* an attribute,
171 // which in PKCS#11 is just an byte array.
172 a := new(Attribute)
173 a.Type = typ
174 if x == nil {
175 return a
176 }
177 switch v := x.(type) {
178 case bool:
179 if v {
180 a.Value = []byte{1}
181 } else {
182 a.Value = []byte{0}
183 }
184 case int:
185 a.Value = uintToBytes(uint64(v))
186 case uint:
187 a.Value = uintToBytes(uint64(v))
188 case string:
189 a.Value = []byte(v)
190 case []byte:
191 a.Value = v
192 case time.Time: // for CKA_DATE
193 a.Value = cDate(v)
194 default:
195 panic("pkcs11: unhandled attribute type")
196 }
197 return a
198 }
199
200 // cAttribute returns the start address and the length of an attribute list.
201 func cAttributeList(a []*Attribute) (arena, C.CK_ATTRIBUTE_PTR, C.CK_ULONG) {
202 var arena arena
203 if len(a) == 0 {
204 return nil, nil, 0
205 }
206 pa := make([]C.CK_ATTRIBUTE, len(a))
207 for i, attr := range a {
208 pa[i]._type = C.CK_ATTRIBUTE_TYPE(attr.Type)
209 if len(attr.Value) != 0 {
210 buf, len := arena.Allocate(attr.Value)
211 // field is unaligned on windows so this has to call into C
212 C.putAttributePval(&pa[i], buf)
213 pa[i].ulValueLen = len
214 }
215 }
216 return arena, &pa[0], C.CK_ULONG(len(a))
217 }
218
219 func cDate(t time.Time) []byte {
220 b := make([]byte, 8)
221 year, month, day := t.Date()
222 y := fmt.Sprintf("%4d", year)
223 m := fmt.Sprintf("%02d", month)
224 d1 := fmt.Sprintf("%02d", day)
225 b[0], b[1], b[2], b[3] = y[0], y[1], y[2], y[3]
226 b[4], b[5] = m[0], m[1]
227 b[6], b[7] = d1[0], d1[1]
228 return b
229 }
230
231 // Mechanism holds an mechanism type/value combination.
232 type Mechanism struct {
233 Mechanism uint
234 Parameter []byte
235 generator interface{}
236 }
237
238 // NewMechanism returns a pointer to an initialized Mechanism.
239 func NewMechanism(mech uint, x interface{}) *Mechanism {
240 m := new(Mechanism)
241 m.Mechanism = mech
242 if x == nil {
243 return m
244 }
245
246 switch p := x.(type) {
247 case *GCMParams, *OAEPParams, *ECDH1DeriveParams:
248 // contains pointers; defer serialization until cMechanism
249 m.generator = p
250 case []byte:
251 m.Parameter = p
252 default:
253 panic("parameter must be one of type: []byte, *GCMParams, *OAEPParams, *ECDH1DeriveParams")
254 }
255
256 return m
257 }
258
259 func cMechanism(mechList []*Mechanism) (arena, *C.CK_MECHANISM) {
260 if len(mechList) != 1 {
261 panic("expected exactly one mechanism")
262 }
263 mech := mechList[0]
264 cmech := &C.CK_MECHANISM{mechanism: C.CK_MECHANISM_TYPE(mech.Mechanism)}
265 // params that contain pointers are allocated here
266 param := mech.Parameter
267 var arena arena
268 switch p := mech.generator.(type) {
269 case *GCMParams:
270 // uses its own arena because it has to outlive this function call (yuck)
271 param = cGCMParams(p)
272 case *OAEPParams:
273 param, arena = cOAEPParams(p, arena)
274 case *ECDH1DeriveParams:
275 param, arena = cECDH1DeriveParams(p, arena)
276 }
277 if len(param) != 0 {
278 buf, len := arena.Allocate(param)
279 // field is unaligned on windows so this has to call into C
280 C.putMechanismParam(cmech, buf)
281 cmech.ulParameterLen = len
282 }
283 return arena, cmech
284 }
285
286 // MechanismInfo provides information about a particular mechanism.
287 type MechanismInfo struct {
288 MinKeySize uint
289 MaxKeySize uint
290 Flags uint
291 }
292
293 // stubData is a persistent nonempty byte array used by cMessage.
294 var stubData = []byte{0}
295
296 // cMessage returns the pointer/length pair corresponding to data.
297 func cMessage(data []byte) (dataPtr C.CK_BYTE_PTR) {
298 l := len(data)
299 if l == 0 {
300 // &data[0] is forbidden in this case, so use a nontrivial array instead.
301 data = stubData
302 }
303 return C.CK_BYTE_PTR(unsafe.Pointer(&data[0]))
304 }