Skip to content

Commit 4916ac4

Browse files
author
Volodymyr Paprotski
committed
[FAB-1648] Vendor PKCS11 bindings
Bring in PKCS11 Golang bindings. Test the vendored files by loading softhsm if can be found Softhsm is being installed by https://gerrit.hyperledger.org/r/#/c/4359/ Change-Id: I848c66b778e131ff91819b11f8b53e03934374a1 Signed-off-by: Volodymyr Paprotski <[email protected]>
1 parent a0898e6 commit 4916ac4

File tree

14 files changed

+6007
-14
lines changed

14 files changed

+6007
-14
lines changed

bccsp/pkcs11/impl_test.go

+52-14
Original file line numberDiff line numberDiff line change
@@ -17,29 +17,24 @@ package pkcs11
1717

1818
import (
1919
"bytes"
20-
"os"
21-
"testing"
22-
2320
"crypto"
24-
"crypto/rsa"
25-
21+
"crypto/ecdsa"
22+
"crypto/elliptic"
2623
"crypto/rand"
24+
"crypto/rsa"
25+
"crypto/sha256"
26+
"crypto/sha512"
2727
"crypto/x509"
2828
"crypto/x509/pkix"
2929
"encoding/asn1"
30+
"fmt"
31+
"hash"
3032
"math/big"
3133
"net"
34+
"os"
35+
"testing"
3236
"time"
3337

34-
"crypto/ecdsa"
35-
"crypto/elliptic"
36-
"crypto/sha256"
37-
38-
"fmt"
39-
40-
"crypto/sha512"
41-
"hash"
42-
4338
"github.com/hyperledger/fabric/bccsp"
4439
"github.com/hyperledger/fabric/bccsp/signer"
4540
"github.com/hyperledger/fabric/bccsp/utils"
@@ -65,6 +60,17 @@ func TestMain(m *testing.M) {
6560
}
6661
currentKS = ks
6762

63+
lib, pin, label := findPKCS11Lib()
64+
if enablePKCS11tests {
65+
err := initPKCS11(lib, pin, label)
66+
if err != nil {
67+
fmt.Printf("Failed initializing PKCS11 library [%s]", err)
68+
os.Exit(-1)
69+
}
70+
} else {
71+
fmt.Printf("No PKCS11 library found, skipping PKCS11 tests")
72+
}
73+
6874
tests := []testConfig{
6975
{256, "SHA2"},
7076
{256, "SHA3"},
@@ -1896,3 +1902,35 @@ func getCryptoHashIndex(t *testing.T) crypto.Hash {
18961902

18971903
return crypto.SHA3_256
18981904
}
1905+
1906+
var enablePKCS11tests = false
1907+
1908+
func findPKCS11Lib() (lib, pin, label string) {
1909+
//FIXME: Till we workout the configuration piece, look for the libraries in the familiar places
1910+
lib = os.Getenv("PKCS11_LIB")
1911+
if lib == "" {
1912+
pin = "98765432"
1913+
label = "ForFabric"
1914+
possibilities := []string{
1915+
"/usr/lib/softhsm/libsofthsm2.so", //Debian
1916+
"/usr/lib/x86_64-linux-gnu/softhsm/libsofthsm2.so", //Ubuntu
1917+
"/usr/lib/s390x-linux-gnu/softhsm/libsofthsm2.so", //Ubuntu
1918+
"/usr/local/Cellar/softhsm/2.1.0/lib/softhsm/libsofthsm2.so", //MacOS
1919+
}
1920+
for _, path := range possibilities {
1921+
if _, err := os.Stat(path); !os.IsNotExist(err) {
1922+
lib = path
1923+
enablePKCS11tests = true
1924+
break
1925+
}
1926+
}
1927+
if lib == "" {
1928+
enablePKCS11tests = false
1929+
}
1930+
} else {
1931+
enablePKCS11tests = true
1932+
pin = os.Getenv("PKCS11_PIN")
1933+
label = os.Getenv("PKCS11_LABEL")
1934+
}
1935+
return lib, pin, label
1936+
}

bccsp/pkcs11/pkcs11.go

+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
Copyright IBM Corp. 2017 All Rights Reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
package pkcs11
17+
18+
import (
19+
"fmt"
20+
"github.com/miekg/pkcs11"
21+
)
22+
23+
var (
24+
ctx *pkcs11.Ctx
25+
sessions = make(chan pkcs11.SessionHandle, 2000)
26+
slot uint
27+
)
28+
29+
func initPKCS11(lib, pin, label string) error {
30+
return loadLib(lib, pin, label)
31+
}
32+
33+
func loadLib(lib, pin, label string) error {
34+
logger.Debugf("Loading pkcs11 library [%s]\n", lib)
35+
if lib == "" {
36+
return fmt.Errorf("No PKCS11 library default")
37+
}
38+
39+
ctx = pkcs11.New(lib)
40+
if ctx == nil {
41+
return fmt.Errorf("Instantiate failed [%s]", lib)
42+
}
43+
44+
ctx.Initialize()
45+
slots, err := ctx.GetSlotList(true)
46+
if err != nil {
47+
return err
48+
}
49+
found := false
50+
for _, s := range slots {
51+
info, err := ctx.GetTokenInfo(s)
52+
if err != nil {
53+
continue
54+
}
55+
if label == info.Label {
56+
found = true
57+
slot = s
58+
break
59+
}
60+
}
61+
if !found {
62+
return fmt.Errorf("Could not find token with label %s", label)
63+
}
64+
session := getSession()
65+
defer returnSession(session)
66+
67+
if pin == "" {
68+
return fmt.Errorf("No PIN set\n")
69+
}
70+
err = ctx.Login(session, pkcs11.CKU_USER, pin)
71+
if err != nil {
72+
return fmt.Errorf("Login failed [%s]\n", err)
73+
}
74+
75+
return nil
76+
}
77+
78+
func getSession() (session pkcs11.SessionHandle) {
79+
select {
80+
case session = <-sessions:
81+
logger.Debugf("Reusing existing pkcs11 session %x on slot %d\n", session, slot)
82+
83+
default:
84+
// create one
85+
var s pkcs11.SessionHandle
86+
var err error = nil
87+
for i := 0; i < 10; i++ {
88+
s, err = ctx.OpenSession(slot, pkcs11.CKF_SERIAL_SESSION|pkcs11.CKF_RW_SESSION)
89+
if err != nil {
90+
logger.Warningf("OpenSession failed, retrying [%s]\n", err)
91+
} else {
92+
break
93+
}
94+
}
95+
if err != nil {
96+
logger.Fatalf("OpenSession [%s]\n", err)
97+
}
98+
logger.Debugf("Created new pkcs11 session %x on slot %d\n", session, slot)
99+
session = s
100+
}
101+
return session
102+
}
103+
104+
func returnSession(session pkcs11.SessionHandle) {
105+
sessions <- session
106+
}

bccsp/pkcs11/pkcs11_test.go

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
Copyright IBM Corp. 2017 All Rights Reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
package pkcs11
17+
18+
import (
19+
"testing"
20+
)
21+
22+
func TestPKCS11GetSession(t *testing.T) {
23+
if !enablePKCS11tests {
24+
t.SkipNow()
25+
}
26+
27+
session := getSession()
28+
defer returnSession(session)
29+
}

vendor/github.com/miekg/pkcs11/LICENSE

+27
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/miekg/pkcs11/README.md

+64
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)