-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathengine_test.go
More file actions
133 lines (108 loc) · 2.6 KB
/
engine_test.go
File metadata and controls
133 lines (108 loc) · 2.6 KB
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
/*
Copyright NetFoundry Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package pkcs11
import (
"crypto"
"crypto/ecdsa"
"crypto/rand"
"crypto/rsa"
"encoding/asn1"
"github.com/openziti/identity/engines"
"github.com/stretchr/testify/assert"
"math/big"
"net/url"
_ "net/url"
"os"
"os/exec"
"testing"
)
const pin = "2171"
const softHsmEnvVar = "SOFTHSM2_LIB"
func init() {
_ = os.Setenv("SOFTHSM2_CONF", "softhsm2.conf")
}
func genTestData(pin string) error {
err := exec.Command(initScript, getPkcs11Lib(), pin).Run()
if err != nil {
return err
}
return nil
}
type ecdsaSig struct {
R, S *big.Int
}
func Test_softhsm2_keys(t *testing.T) {
pkcs11Lib := getPkcs11Lib()
eng, found := engines.GetEngine(EngineId)
if !found {
t.Logf("skipping %s: enfgine[%s] not found", t.Name(), EngineId)
t.SkipNow()
}
if _, err := os.Stat(pkcs11Lib); err != nil {
t.Logf("skipping %s: driver not found", t.Name())
t.SkipNow()
}
assert.NoError(t, genTestData(pin))
keys := map[string]string{
"prime256v1": "02",
"rsa:2048": "01",
}
for n, id := range keys {
t.Logf("testing key %s", n)
k, err := url.Parse("pkcs11:" + pkcs11Lib + "?pin=" + pin + "&id=" + id)
if err != nil {
t.Error(err)
}
key, err := eng.LoadKey(k)
if err != nil {
t.Error(err)
} else {
testSigner(key, t)
}
}
}
func testSigner(key crypto.PrivateKey, t *testing.T) {
privateKey, ok := key.(crypto.Signer)
if !ok {
t.Error("key is not a crypto.Signer")
}
pub := privateKey.Public()
bytes := make([]byte, 32)
_, _ = rand.Read(bytes)
sig, err := privateKey.Sign(rand.Reader, bytes, crypto.SHA256)
if err != nil {
t.Error(err)
}
switch pubKey := pub.(type) {
case *ecdsa.PublicKey:
var ecSig ecdsaSig
rest, err := asn1.Unmarshal(sig, &ecSig)
if err != nil {
t.Error(err)
}
if len(rest) != 0 {
t.Errorf("leftover bytes")
}
cool := ecdsa.Verify(pubKey, bytes, ecSig.R, ecSig.S)
if !cool {
t.Errorf("signature validation fail")
}
case *rsa.PublicKey:
err = rsa.VerifyPKCS1v15(pubKey, crypto.SHA256, bytes, sig)
if err != nil {
t.Error(err.Error())
}
default:
t.Errorf("bad pub key")
}
}