forked from namecoin/pkcs11mod
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypes.go
139 lines (117 loc) · 3.68 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
// Copyright 2018 Namecoin Developers LGPLv3+
package pkcs11mod
/*
#include "spec/pkcs11go.h"
void SetIndex(CK_ULONG_PTR array, CK_ULONG i, CK_ULONG val)
{
array[i] = val;
}
CK_ATTRIBUTE_PTR IndexAttributePtr(CK_ATTRIBUTE_PTR array, CK_ULONG i)
{
return &(array[i]);
}
// Copied verbatim from miekg/pkcs11 pkcs11.go
static inline CK_VOID_PTR getAttributePval(CK_ATTRIBUTE_PTR a)
{
return a->pValue;
}
*/
import "C"
import (
"fmt"
"unsafe"
"github.com/miekg/pkcs11"
)
// fromList converts from a []uint to a C style array.
func fromList(goList []uint, cList C.CK_ULONG_PTR, goSize uint) {
for i := 0; uint(i) < goSize; i++ {
C.SetIndex(cList, C.CK_ULONG(i), C.CK_ULONG(goList[i]))
}
}
// fromMechanismList converts from a []*pkcs11.Mechanism to a C style array of
// mechanism types.
func fromMechanismList(goList []*pkcs11.Mechanism, cList C.CK_ULONG_PTR, goSize uint) {
for i := 0; uint(i) < goSize; i++ {
C.SetIndex(cList, C.CK_ULONG(i), C.CK_ULONG(goList[i].Mechanism))
}
}
// fromObjectHandleList converts from a []pkcs11.ObjectHandle to a C style array.
func fromObjectHandleList(goList []pkcs11.ObjectHandle, cList C.CK_ULONG_PTR, goSize uint) {
for i := 0; uint(i) < goSize; i++ {
C.SetIndex(cList, C.CK_ULONG(i), C.CK_ULONG(goList[i]))
}
}
// fromCBBool converts a CK_BBOOL to a bool.
func fromCBBool(x C.CK_BBOOL) bool {
// Any nonzero value means true, and zero means false.
return x != C.CK_FALSE
}
func fromError(e error) C.CK_RV {
if e == nil {
return C.CKR_OK
}
return C.CK_RV(e.(pkcs11.Error))
}
// toTemplate converts from a C style array to a []*pkcs11.Attribute.
// It doesn't free the input array.
func toTemplate(clist C.CK_ATTRIBUTE_PTR, size C.CK_ULONG) []*pkcs11.Attribute {
l1 := make([]C.CK_ATTRIBUTE_PTR, int(size))
for i := 0; i < len(l1); i++ {
l1[i] = C.IndexAttributePtr(clist, C.CK_ULONG(i))
}
// defer C.free(unsafe.Pointer(clist)) // Removed compared to miekg implementation since it's not desired here
l2 := make([]*pkcs11.Attribute, int(size))
for i, c := range l1 {
x := new(pkcs11.Attribute)
x.Type = uint(c._type)
if int(c.ulValueLen) != -1 {
buf := unsafe.Pointer(C.getAttributePval(c))
x.Value = C.GoBytes(buf, C.int(c.ulValueLen))
//C.free(buf) // Removed compared to miekg implementation since it's not desired here
}
l2[i] = x
}
return l2
}
// toTemplate converts from a []*pkcs11.Attribute to a C style array that
// already contains a template as is passed to C_GetAttributeValue.
func fromTemplate(template []*pkcs11.Attribute, clist C.CK_ATTRIBUTE_PTR) error {
l1 := make([]C.CK_ATTRIBUTE_PTR, int(len(template)))
for i := 0; i < len(l1); i++ {
l1[i] = C.IndexAttributePtr(clist, C.CK_ULONG(i))
}
bufferTooSmall := false
for i, x := range template {
c := l1[i]
cLen := C.CK_ULONG(uint(len(x.Value)))
if C.getAttributePval(c) == nil {
c.ulValueLen = cLen
} else if c.ulValueLen >= cLen {
buf := unsafe.Pointer(C.getAttributePval(c))
// Adapted from solution 3 of https://stackoverflow.com/a/35675259
goBuf := (*[1 << 30]byte)(buf)
copy(goBuf[:], x.Value)
c.ulValueLen = cLen
} else {
c.ulValueLen = C.CK_UNAVAILABLE_INFORMATION
bufferTooSmall = true
}
}
if bufferTooSmall {
return pkcs11.Error(pkcs11.CKR_BUFFER_TOO_SMALL)
}
return nil
}
func BytesToBool(arg []byte) (bool, error) {
if len(arg) != 1 {
return false, fmt.Errorf("Invalid length: %d", len(arg))
}
return fromCBBool(*(*C.CK_BBOOL)(unsafe.Pointer(&arg[0]))), nil
}
func BytesToULong(arg []byte) (uint, error) {
// TODO: use cgo to get the actual size of ULong instead of guessing "at least 4"
if len(arg) < 4 {
return 0, fmt.Errorf("Invalid length: %d", len(arg))
}
return uint(*(*C.CK_ULONG)(unsafe.Pointer(&arg[0]))), nil
}