-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontext.go
157 lines (147 loc) · 4.86 KB
/
context.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
package main
/*
#include "pkcs11go.h"
*/
import "C"
import (
"encoding/binary"
"fmt"
"github.com/niclabs/dtc/v3/utils"
)
// SignContext represents a structure which groups parameters that allow to sign
// a document.
type SignContext interface {
Init(metaBytes []byte) error
SignatureLength() int
Update(data []byte) error
Final() ([]byte, error)
Initialized() bool
}
// VerifyContext represents a structure which groups parameters that allow to verify a signature of
// a document.
type VerifyContext interface {
Init(metaBytes []byte) error
Length() int
Update(data []byte) error
Final([]byte) error
Initialized() bool
}
func NewSignContext(session *Session, mechanism *Mechanism, hKey C.CK_OBJECT_HANDLE) (context SignContext, err error) {
keyObject, err := session.GetObject(hKey)
if err != nil {
return nil, err
}
keyIDAttr := keyObject.FindAttribute(AttrTypeKeyHandler)
if keyIDAttr == nil {
return nil, NewError("NewSignContext", "object handle does not contain a key handler attribute", C.CKR_ARGUMENTS_BAD)
}
keyMetaAttr := keyObject.FindAttribute(AttrTypeKeyMeta)
if keyMetaAttr == nil {
return nil, NewError(" NewSignContext", "object handle does not contain any key metainfo attribute", C.CKR_ARGUMENTS_BAD)
}
dtc, err := session.GetDTC()
if err != nil {
return nil, err
}
switch mechanism.Type {
case C.CKM_RSA_PKCS, C.CKM_MD5_RSA_PKCS, C.CKM_SHA1_RSA_PKCS, C.CKM_SHA256_RSA_PKCS, C.CKM_SHA384_RSA_PKCS, C.CKM_SHA512_RSA_PKCS, C.CKM_SHA1_RSA_PKCS_PSS, C.CKM_SHA256_RSA_PKCS_PSS, C.CKM_SHA384_RSA_PKCS_PSS, C.CKM_SHA512_RSA_PKCS_PSS:
c := &SignContextRSA{
dtc: dtc,
randSrc: session.randSrc,
keyID: string(keyIDAttr.Value),
mechanism: mechanism,
data: make([]byte, 0),
}
if err := c.Init(keyMetaAttr.Value); err != nil {
return nil, err
}
context = c
case C.CKM_ECDSA, C.CKM_ECDSA_SHA1, C.CKM_ECDSA_SHA256, C.CKM_ECDSA_SHA384, C.CKM_ECDSA_SHA512:
// Get PK
pkAttr := keyObject.FindAttribute(C.CKA_EC_POINT)
if pkAttr == nil {
return nil, NewError("NewSignContext", "object handle does not contain any ec public key attribute", C.CKR_ARGUMENTS_BAD)
}
c := &ECDSASignContext{
dtc: dtc,
randSrc: session.randSrc,
keyID: string(keyIDAttr.Value),
mechanism: mechanism,
data: make([]byte, 0),
}
if err := c.Init(keyMetaAttr.Value); err != nil {
return nil, err
}
pk, err := utils.ASN1BytesToPubKey(c.keyMeta.Curve(), pkAttr.Value)
if err != nil {
return nil, NewError("NewSignContext", fmt.Sprintf("%s", err), C.CKR_ARGUMENTS_BAD)
}
c.pubKey = pk
context = c
default:
err = NewError("NewSignContext", "sign mechanism invalid", C.CKR_MECHANISM_INVALID)
}
return context, nil
}
func NewVerifyContext(session *Session, mechanism *Mechanism, hKey C.CK_OBJECT_HANDLE) (context VerifyContext, err error) {
keyObject, err := session.GetObject(hKey)
if err != nil {
return nil, err
}
keyIDAttr := keyObject.FindAttribute(AttrTypeKeyHandler)
if keyIDAttr == nil {
return nil, NewError("NewSignContext", "object handle does not contain a key handler attribute", C.CKR_ARGUMENTS_BAD)
}
keyMetaAttr := keyObject.FindAttribute(AttrTypeKeyMeta)
if keyMetaAttr == nil {
return nil, NewError(" NewSignContext", "object handle does not contain any key metainfo attribute", C.CKR_ARGUMENTS_BAD)
}
dtc, err := session.GetDTC()
if err != nil {
return nil, err
}
switch mechanism.Type {
case C.CKM_RSA_PKCS, C.CKM_MD5_RSA_PKCS, C.CKM_SHA1_RSA_PKCS, C.CKM_SHA256_RSA_PKCS, C.CKM_SHA384_RSA_PKCS, C.CKM_SHA512_RSA_PKCS, C.CKM_SHA1_RSA_PKCS_PSS, C.CKM_SHA256_RSA_PKCS_PSS, C.CKM_SHA384_RSA_PKCS_PSS, C.CKM_SHA512_RSA_PKCS_PSS:
c := &VerifyContextRSA{
dtc: dtc,
randSrc: session.randSrc,
keyID: string(keyIDAttr.Value),
mechanism: mechanism,
data: make([]byte, 0),
}
if err := c.Init(keyMetaAttr.Value); err != nil {
return nil, err
}
context = c
case C.CKM_ECDSA, C.CKM_ECDSA_SHA1, C.CKM_ECDSA_SHA256, C.CKM_ECDSA_SHA384, C.CKM_ECDSA_SHA512:
// Get PK
pkAttr := keyObject.FindAttribute(C.CKA_EC_POINT)
if pkAttr == nil {
return nil, NewError("NewVerifyContext", "object handle does not contain any ec public key attribute", C.CKR_ARGUMENTS_BAD)
}
c := &ECDSAVerifyContext{
dtc: dtc,
randSrc: session.randSrc,
keyID: string(keyIDAttr.Value),
mechanism: mechanism,
data: make([]byte, 0),
}
if err := c.Init(keyMetaAttr.Value); err != nil {
return nil, err
}
pk, err := utils.ASN1BytesToPubKey(c.keyMeta.Curve(), pkAttr.Value)
if err != nil {
return nil, NewError("NewVerifyContext", fmt.Sprintf("%s", err), C.CKR_ARGUMENTS_BAD)
}
c.pubKey = pk
context = c
default:
err = NewError("NewVerifyContext", "sign mechanism invalid", C.CKR_MECHANISM_INVALID)
}
return context, nil
}
func ulongToArr(n C.ulong) []byte {
arr := make([]byte, 8)
binary.LittleEndian.PutUint64(arr, uint64(n))
return arr
}