Skip to content

Commit ca4b8a5

Browse files
author
Eduardo
committed
at least it compiles...
1 parent 9659d0e commit ca4b8a5

29 files changed

+273
-317
lines changed

objects/application.go application.go

+10-13
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,19 @@
1-
package objects
1+
package main
22

33
/*
4-
#include "../criptoki/pkcs11go.h"
4+
#include "pkcs11go.h"
55
*/
66
import "C"
7-
import (
8-
"dtc/core"
9-
)
107

118
type Application struct {
129
Database TokenStorage
13-
DTC *core.DTC
10+
DTC *DTC
1411
Slots []*Slot
15-
Config *core.Config
12+
Config *Config
1613
}
1714

1815
func NewApplication() (app *Application, err error) {
19-
config, err := core.GetConfig()
16+
config, err := GetConfig()
2017
if err != nil {
2118
return
2219
}
@@ -30,7 +27,7 @@ func NewApplication() (app *Application, err error) {
3027

3128
for i, slotConf := range config.Criptoki.Slots {
3229
slot := &Slot{
33-
ID: CSlotID(i),
30+
ID: C.CK_SLOT_ID(i),
3431
Application: app,
3532
}
3633
var token *Token
@@ -42,7 +39,7 @@ func NewApplication() (app *Application, err error) {
4239
slot.InsertToken(token)
4340
slots[i] = slot
4441
}
45-
dtc, err := core.NewDTC(config.DTC)
42+
dtc, err := NewDTC(config.DTC)
4643
if err != nil {
4744
return
4845
}
@@ -55,7 +52,7 @@ func NewApplication() (app *Application, err error) {
5552
return
5653
}
5754

58-
func (app *Application) GetSessionSlot(handle CSessionHandle) (*Slot, error) {
55+
func (app *Application) GetSessionSlot(handle C.CK_SESSION_HANDLE) (*Slot, error) {
5956
for _, slot := range app.Slots {
6057
if slot.HasSession(handle) {
6158
return slot, nil
@@ -64,7 +61,7 @@ func (app *Application) GetSessionSlot(handle CSessionHandle) (*Slot, error) {
6461
return nil, NewError("Application.GetSessionSlot", "session not found", C.CKR_SESSION_HANDLE_INVALID)
6562
}
6663

67-
func (app *Application) GetSession(handle CSessionHandle) (*Session, error) {
64+
func (app *Application) GetSession(handle C.CK_SESSION_HANDLE) (*Session, error) {
6865
slot, err := app.GetSessionSlot(handle)
6966
if err != nil {
7067
return nil, err
@@ -76,7 +73,7 @@ func (app *Application) GetSession(handle CSessionHandle) (*Session, error) {
7673
return session, nil
7774
}
7875

79-
func (app *Application) GetSlot(id CSlotID) (*Slot, error) {
76+
func (app *Application) GetSlot(id C.CK_SLOT_ID) (*Slot, error) {
8077
if int(id) < len(app.Slots) {
8178
return nil, NewError("Application.GetSlot", "index out of bounds", C.CKR_SLOT_ID_INVALID)
8279
}

objects/attribute.go attribute.go

+12-12
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
package objects
1+
package main
22

33
/*
44
#include <stdlib.h>
55
#include <string.h>
6-
#include "../criptoki/pkcs11go.h"
6+
#include "pkcs11go.h"
77
*/
88
import "C"
99
import (
@@ -13,19 +13,19 @@ import (
1313

1414
// An attribute related to a crypto object.
1515
type Attribute struct {
16-
Type CAttrType
16+
Type C.CK_ATTRIBUTE_TYPE
1717
Value []byte
1818
}
1919

2020
// A map of attributes
21-
type Attributes map[CAttrType]*Attribute
21+
type Attributes map[C.CK_ATTRIBUTE_TYPE]*Attribute
2222

23-
func CToAttributes(pAttributes CAttrPointer, ulCount CULong) (Attributes, error) {
23+
func CToAttributes(pAttributes C.CK_ATTRIBUTE_PTR, ulCount C.CK_ULONG) (Attributes, error) {
2424
if ulCount <= 0 {
2525
return nil, NewError("CToAttributes", "cannot transform: ulcount is not greater than 0", C.CKR_BUFFER_TOO_SMALL)
2626
}
2727

28-
cAttrSlice := (*[1 << 30]CAttr)(unsafe.Pointer(pAttributes))[:ulCount:ulCount]
28+
cAttrSlice := (*[1 << 30]C.CK_ATTRIBUTE)(unsafe.Pointer(pAttributes))[:ulCount:ulCount]
2929

3030
attributes := make(Attributes, ulCount)
3131
for _, cAttr := range cAttrSlice {
@@ -61,7 +61,7 @@ func (attributes Attributes) SetIfUndefined(attr *Attribute) bool {
6161
return false
6262
}
6363

64-
func CToAttribute(cAttr CAttr) *Attribute {
64+
func CToAttribute(cAttr C.CK_ATTRIBUTE) *Attribute {
6565
attrType := cAttr._type
6666
attrVal := C.GoBytes(unsafe.Pointer(cAttr.pValue), C.int(cAttr.ulValueLen))
6767
return &Attribute{
@@ -70,14 +70,14 @@ func CToAttribute(cAttr CAttr) *Attribute {
7070
}
7171
}
7272

73-
func (attribute *Attribute) ToC(cDst CAttrPointer) error {
73+
func (attribute *Attribute) ToC(cDst C.CK_ATTRIBUTE_PTR) error {
7474
if cDst.pValue == nil {
75-
cDst.ulValueLen = CULong(len(attribute.Value))
75+
cDst.ulValueLen = C.CK_ULONG(len(attribute.Value))
7676
return nil
7777
}
78-
if cDst.ulValueLen >= CULong(len(attribute.Value)) {
78+
if cDst.ulValueLen >= C.CK_ULONG(len(attribute.Value)) {
7979
cValue := C.CBytes(attribute.Value)
80-
cValueLen := CULong(len(attribute.Value))
80+
cValueLen := C.CK_ULONG(len(attribute.Value))
8181
cDst._type = attribute.Type
8282
cDst.ulValueLen = cValueLen
8383
C.memcpy(unsafe.Pointer(cDst.pValue), unsafe.Pointer(cValue), cValueLen)
@@ -94,7 +94,7 @@ func (attribute *Attribute) Equals(attribute2 *Attribute) bool {
9494
bytes.Compare(attribute.Value, attribute2.Value) == 0
9595
}
9696

97-
func (attributes Attributes) GetAttributeByType(cAttr CAttrType) (*Attribute, error) {
97+
func (attributes Attributes) GetAttributeByType(cAttr C.CK_ATTRIBUTE_TYPE) (*Attribute, error) {
9898
attr, ok := attributes[cAttr]
9999
if ok {
100100
return attr, nil

build.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
cd criptoki && go build -o dtc.so -buildmode=c-shared -v
1+
go build -o dtc.so -buildmode=c-shared -v

core/config.go config.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package core
1+
package main
22

33
import (
44
"fmt"

objects/connection.go connection.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package objects
1+
package main
22

33
import (
44
"fmt"

criptoki/dtc.so

-1.58 MB
Binary file not shown.

criptoki/pkcs11.c

-75
This file was deleted.

objects/crypto_object.go crypto_object.go

+13-11
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,43 @@
1-
package objects
1+
package main
22

33
/*
4-
#include "../criptoki/pkcs11go.h"
4+
#include "pkcs11go.h"
55
*/
66
import "C"
77
import (
88
"bytes"
99
"unsafe"
1010
)
1111

12+
type CryptoObjectType int
13+
1214
const (
1315
SessionObject CryptoObjectType = iota
1416
TokenObject
1517
)
1618

1719
// A cryptoObject related to a token.
1820
type CryptoObject struct {
19-
Handle CObjectHandle
21+
Handle C.CK_OBJECT_HANDLE
2022
Type CryptoObjectType
2123
Attributes Attributes
2224
}
2325

2426
// A map of cryptoobjects
2527
type CryptoObjects []*CryptoObject
2628

27-
func CToCryptoObject(pAttributes CAttrPointer, ulCount CULong) (*CryptoObject, error) {
29+
func CToCryptoObject(pAttributes C.CK_ATTRIBUTE_PTR, ulCount C.CK_ULONG) (*CryptoObject, error) {
2830
attrSlice, err := CToAttributes(pAttributes, ulCount)
2931
if err != nil {
3032
return nil, err
3133
}
3234
var coType CryptoObjectType
33-
tokenAttr, ok := attrSlice[CToken]
35+
tokenAttr, ok := attrSlice[C.CKA_TOKEN]
3436
if !ok {
3537
return nil, NewError("CToCryptoObject", "Token attribute not found", C.CKR_ATTRIBUTE_VALUE_INVALID)
3638
}
37-
isToken := CBool(tokenAttr.Value[0])
38-
if isToken == CFalse {
39+
isToken := C.CK_BBOOL(tokenAttr.Value[0])
40+
if isToken == C.CK_FALSE {
3941
coType = SessionObject
4042
} else {
4143
coType = TokenObject
@@ -81,26 +83,26 @@ func (object *CryptoObject) Equals(object2 *CryptoObject) bool {
8183
func (object *CryptoObject) Match(attrs Attributes) bool {
8284
for _, theirAttr := range attrs {
8385
ourAttr, ok := object.Attributes[theirAttr.Type]
84-
if !ok || bytes.Compare(ourAttr.Value, theirAttr.Value) != 0 {
86+
if !ok || bytes.Compare(ourAttr.Value, ourAttr.Value) != 0 {
8587
return false
8688
}
8789
}
8890
return true
8991
}
9092

91-
func (object *CryptoObject) FindAttribute(attrType CAttrType) *Attribute {
93+
func (object *CryptoObject) FindAttribute(attrType C.CK_ATTRIBUTE_TYPE) *Attribute {
9294
if attr, ok := object.Attributes[attrType]; ok {
9395
return attr
9496
}
9597
return nil
9698
}
9799

98100
// https://stackoverflow.com/questions/28925179/cgo-how-to-pass-struct-array-from-c-to-go#28933938
99-
func (object *CryptoObject) CopyAttributes(pTemplate CAttrPointer, ulCount CULong) error {
101+
func (object *CryptoObject) CopyAttributes(pTemplate C.CK_ATTRIBUTE_PTR, ulCount C.CK_ULONG) error {
100102
if pTemplate == nil {
101103
return NewError("CryptoObject.CopyAttributes", "got NULL pointer", C.CKR_ARGUMENTS_BAD)
102104
}
103-
templateSlice := (*[1 << 30]CAttr)(unsafe.Pointer(pTemplate))[:ulCount:ulCount]
105+
templateSlice := (*[1 << 30]C.CK_ATTRIBUTE)(unsafe.Pointer(pTemplate))[:ulCount:ulCount]
104106

105107
for i := 0; i < len(templateSlice); i++ {
106108
src := object.FindAttribute(templateSlice[i]._type)

core/dtc.go dtc.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package core
1+
package main
22

33
import (
44
"dtc/network"

criptoki/dtc.h dtc.h

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* Code generated by cmd/cgo; DO NOT EDIT. */
22

3-
/* package dtc/criptoki */
3+
/* package dtc */
44

55

66
#line 1 "cgo-builtin-export-prolog"
@@ -21,6 +21,8 @@ typedef struct { const char *p; ptrdiff_t n; } _GoString_;
2121

2222
#line 3 "pkcs11.go"
2323

24+
#include <stdlib.h>
25+
#include <string.h>
2426
#include "pkcs11go.h"
2527

2628
#line 1 "cgo-generated-wrapper"
@@ -136,6 +138,8 @@ extern CK_RV C_VerifyUpdate(CK_SESSION_HANDLE p0, CK_BYTE_PTR p1, CK_ULONG p2);
136138

137139
extern CK_RV C_VerifyFinal(CK_SESSION_HANDLE p0, CK_BYTE_PTR p1, CK_ULONG p2);
138140

141+
extern CK_RV C_DigestInit(CK_SESSION_HANDLE p0, CK_MECHANISM_PTR p1);
142+
139143
extern CK_RV C_Digest(CK_SESSION_HANDLE p0, CK_BYTE_PTR p1, CK_ULONG p2, CK_BYTE_PTR p3, CK_ULONG_PTR p4);
140144

141145
extern CK_RV C_SeedRandom(CK_SESSION_HANDLE p0, CK_BYTE_PTR p1, CK_ULONG p2);
@@ -172,8 +176,6 @@ extern CK_RV C_DecryptUpdate(CK_SESSION_HANDLE p0, CK_BYTE_PTR p1, CK_ULONG p2,
172176

173177
extern CK_RV C_DecryptFinal(CK_SESSION_HANDLE p0, CK_BYTE_PTR p1, CK_ULONG_PTR p2);
174178

175-
extern CK_RV C_DigestInit(CK_SESSION_HANDLE p0, CK_MECHANISM_PTR p1);
176-
177179
extern CK_RV C_DigestUpdate(CK_SESSION_HANDLE p0, CK_BYTE_PTR p1, CK_ULONG p2);
178180

179181
extern CK_RV C_DigestKey(CK_SESSION_HANDLE p0, CK_OBJECT_HANDLE p1);

dtc.so

18.3 MB
Binary file not shown.

0 commit comments

Comments
 (0)