| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package pkcs11 |
| 6 |
|
| 7 |
|
| •8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 89 |
type SessionHandle uint |
| 90 |
|
| 91 |
|
| 92 |
type ObjectHandle uint |
| 93 |
|
| 94 |
|
| 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 |
|
| 105 |
|
| 106 |
type SlotEvent struct { |
| 107 |
SlotID uint |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
type Info struct { |
| 112 |
CryptokiVersion Version |
| 113 |
ManufacturerID string |
| 114 |
Flags uint |
| 115 |
LibraryDescription string |
| 116 |
LibraryVersion Version |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
type SlotInfo struct { |
| 121 |
SlotDescription string |
| 122 |
ManufacturerID string |
| 123 |
Flags uint |
| 124 |
HardwareVersion Version |
| 125 |
FirmwareVersion Version |
| 126 |
} |
| 127 |
|
| 128 |
|
| 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 |
|
| 151 |
type SessionInfo struct { |
| 152 |
SlotID uint |
| 153 |
State uint |
| 154 |
Flags uint |
| 155 |
DeviceError uint |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
type Attribute struct { |
| 160 |
Type uint |
| 161 |
Value []byte |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
func NewAttribute(typ uint, x interface{}) *Attribute { |
| 169 |
|
| 170 |
|
| 171 |
|
| 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: |
| 193 |
a.Value = cDate(v) |
| 194 |
default: |
| 195 |
panic("pkcs11: unhandled attribute type") |
| 196 |
} |
| 197 |
return a |
| 198 |
} |
| 199 |
|
| 200 |
|
| 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 |
|
| 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 |
|
| 232 |
type Mechanism struct { |
| 233 |
Mechanism uint |
| 234 |
Parameter []byte |
| 235 |
generator interface{} |
| 236 |
} |
| 237 |
|
| 238 |
|
| 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 |
|
| 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 |
|
| 266 |
param := mech.Parameter |
| 267 |
var arena arena |
| 268 |
switch p := mech.generator.(type) { |
| 269 |
case *GCMParams: |
| 270 |
|
| 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 |
|
| 280 |
C.putMechanismParam(cmech, buf) |
| 281 |
cmech.ulParameterLen = len |
| 282 |
} |
| 283 |
return arena, cmech |
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
type MechanismInfo struct { |
| 288 |
MinKeySize uint |
| 289 |
MaxKeySize uint |
| 290 |
Flags uint |
| 291 |
} |
| 292 |
|
| 293 |
|
| 294 |
var stubData = []byte{0} |
| 295 |
|
| 296 |
|
| 297 |
func cMessage(data []byte) (dataPtr C.CK_BYTE_PTR) { |
| 298 |
l := len(data) |
| 299 |
if l == 0 { |
| 300 |
|
| 301 |
data = stubData |
| 302 |
} |
| 303 |
return C.CK_BYTE_PTR(unsafe.Pointer(&data[0])) |
| 304 |
} |