Skip to content

Commit dbbb3db

Browse files
authored
hide pkcs11 behind a build tag so that token sdk is pure go by default (#699)
Signed-off-by: Arne Rutjes <[email protected]>
1 parent 9655e82 commit dbbb3db

File tree

10 files changed

+83
-20
lines changed

10 files changed

+83
-20
lines changed

ci/scripts/setup_softhsm.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ if [[ "$OSTYPE" == "darwin"* ]]; then
2020
# softhsm2-util --delete-token --token "ForFSC" || true
2121

2222
echo "Initializing tokens..."
23-
softhsm2-util --init-token --slot 13 --label "ForFSC" --so-pin 1234 --pin 98765432
23+
softhsm2-util --init-token --free --label "ForFSC" --so-pin 1234 --pin 98765432
2424

2525
else
2626

docs/core-token.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,19 +102,21 @@ token:
102102
path: /path/to/issuer-wallet
103103
# additional options that can be used to instantiated the wallet.
104104
# options are driver dependent. With `fabtoken` and `dlog` drivers,
105-
# the following options apply
105+
# the following options apply.
106106
opts:
107107
BCCSP:
108108
Default: SW
109+
SW:
110+
Hash: SHA2
111+
Security: 256
112+
# The following only needs to be defined if the BCCSP Default is set to PKCS11.
113+
# NOTE: in order to use pkcs11, you have to build the application with "go build -tags pkcs11"
109114
PKCS11:
110115
Hash: SHA2
111116
Label: null
112117
Library: null
113118
Pin: null
114119
Security: 256
115-
SW:
116-
Hash: SHA2
117-
Security: 256
118120
# auditor wallets
119121
auditors:
120122
- id: auditor # the unique identifier of this wallet. Here is an example of use: `ttx.GetAuditorWallet(context, "auditor)`

docs/deployment/deployment.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,10 @@ func main() {
5656
return nil
5757
})
5858
}
59-
```
59+
```
60+
61+
## HSM Support
62+
63+
In order to use a hardware HSM for x.509 identities, you have to build the application with
64+
`CGO_ENABLED=1 go build -tags pkcs11` and configure the PKCS11 settings in the configuration
65+
file (see [core-token.md](../core-token.md)).

fungible.mk

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ integration-tests-dloghsm-fabric: install-softhsm
6363
@echo "Setup SoftHSM"
6464
@./ci/scripts/setup_softhsm.sh
6565
@echo "Start Integration Test"
66-
cd ./integration/token/fungible/dloghsm; export FAB_BINS=$(FAB_BINS); ginkgo $(GINKGO_TEST_OPTS) --label-filter="$(TEST_FILTER)" .
66+
cd ./integration/token/fungible/dloghsm; export FAB_BINS=$(FAB_BINS); ginkgo $(GINKGO_TEST_OPTS) --tags pkcs11 --label-filter="$(TEST_FILTER)" .
6767

6868
.PHONY: integration-tests-fabtoken-fabric-t1
6969
integration-tests-fabtoken-fabric-t1:
@@ -87,7 +87,7 @@ integration-tests-fabtoken-fabric-t5:
8787

8888
.PHONY: integration-tests-fabtoken-fabric
8989
integration-tests-fabtoken-fabric:
90-
cd ./integration/token/fungible/fabtoken; export FAB_BINS=$(FAB_BINS); ginkgo $(GINKGO_TEST_OPTS) --label-filter="$(TEST_FILTER)" .
90+
cd ./integration/token/fungible/fabtoken; export FAB_BINS=$(FAB_BINS); ginkgo $(GINKGO_TEST_OPTS) --tags pkcs11 --label-filter="$(TEST_FILTER)" .
9191

9292
.PHONY: integration-tests-dlog-orion
9393
integration-tests-dlog-orion:

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ require (
88
github.com/IBM/mathlib v0.0.3-0.20231011094432-44ee0eb539da
99
github.com/dgraph-io/badger/v3 v3.2103.2
1010
github.com/hashicorp/go-uuid v1.0.2
11-
github.com/hyperledger-labs/fabric-smart-client v0.3.1-0.20240724173042-088844238ec3
11+
github.com/hyperledger-labs/fabric-smart-client v0.3.1-0.20240805090211-ae3778fa9aac
1212
github.com/hyperledger-labs/fabric-token-sdk/txgen v0.0.0-00010101000000-000000000000
1313
github.com/hyperledger-labs/orion-sdk-go v0.2.10
1414
github.com/hyperledger-labs/orion-server v0.2.10

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -467,8 +467,8 @@ github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpO
467467
github.com/hudl/fargo v1.3.0/go.mod h1:y3CKSmjA+wD2gak7sUSXTAoopbhU08POFhmITJgmKTg=
468468
github.com/huin/goupnp v1.2.0 h1:uOKW26NG1hsSSbXIZ1IR7XP9Gjd1U8pnLaCMgntmkmY=
469469
github.com/huin/goupnp v1.2.0/go.mod h1:gnGPsThkYa7bFi/KWmEysQRf48l2dvR5bxr2OFckNX8=
470-
github.com/hyperledger-labs/fabric-smart-client v0.3.1-0.20240724173042-088844238ec3 h1:XqxKaspPPyty8w4LlIIKUcMEp2zi5QCvnia+2YVW6ME=
471-
github.com/hyperledger-labs/fabric-smart-client v0.3.1-0.20240724173042-088844238ec3/go.mod h1:o/L/+Apv/hCHFVIQNIiYJcHh1Sl0LGSc2kAWJAeFLB0=
470+
github.com/hyperledger-labs/fabric-smart-client v0.3.1-0.20240805090211-ae3778fa9aac h1:tarUHl0o/9a9q5IcZn6CLWL7Qv/en1SZD2dJ2oD/Xb4=
471+
github.com/hyperledger-labs/fabric-smart-client v0.3.1-0.20240805090211-ae3778fa9aac/go.mod h1:o/L/+Apv/hCHFVIQNIiYJcHh1Sl0LGSc2kAWJAeFLB0=
472472
github.com/hyperledger-labs/orion-sdk-go v0.2.10 h1:lFgWgxyvngIhWnIqymYGBmtmq9D6uC5d0uLG9cbyh5s=
473473
github.com/hyperledger-labs/orion-sdk-go v0.2.10/go.mod h1:iN2xZB964AqwVJwL+EnwPOs8z1EkMEbbIg/qYeC7gDY=
474474
github.com/hyperledger-labs/orion-server v0.2.10 h1:G4zbQEL5Egk0Oj+TwHCZWdTOLDBHOjaAEvYOT4G7ozw=

integration/nwo/token/generators/dlog/dlog_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222

2323
func TestDLogFabricCryptoMaterialGenerator_Setup(t *testing.T) {
2424
gomega.RegisterTestingT(t)
25-
buildServer := common.NewBuildServer()
25+
buildServer := common.NewBuildServer("-tags", "pkcs11")
2626
buildServer.Serve()
2727
defer buildServer.Shutdown(true)
2828

token/services/identity/msp/x509/msp/bccsp.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import (
1010
"encoding/hex"
1111
"path/filepath"
1212

13+
"github.com/hyperledger-labs/fabric-token-sdk/token/services/identity/msp/x509/msp/pkcs11"
1314
"github.com/hyperledger/fabric/bccsp"
14-
"github.com/hyperledger/fabric/bccsp/pkcs11"
1515
"github.com/hyperledger/fabric/bccsp/sw"
1616
"github.com/pkg/errors"
1717
)
@@ -42,14 +42,12 @@ func GetPKCS11BCCSP(conf *BCCSP) (bccsp.BCCSP, bccsp.KeyStore, error) {
4242
return nil, nil, errors.New("invalid BCCSP.PKCS11. missing configuration")
4343
}
4444

45-
p11Opts := *conf.PKCS11
45+
p11Opts := conf.PKCS11
4646
ks := sw.NewDummyKeyStore()
47-
mapper := skiMapper(p11Opts)
48-
csp, err := pkcs11.New(*ToPKCS11OptsOpts(&p11Opts), ks, pkcs11.WithKeyMapper(mapper))
49-
if err != nil {
50-
return nil, nil, errors.WithMessagef(err, "Failed initializing PKCS11 library with config [%+v]", p11Opts)
51-
}
52-
return csp, ks, nil
47+
opts := ToPKCS11OptsOpts(p11Opts)
48+
csp, err := pkcs11.NewProvider(*opts, ks, skiMapper(*p11Opts))
49+
50+
return csp, ks, err
5351
}
5452

5553
func skiMapper(p11Opts PKCS11) func([]byte) []byte {
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//go:build !pkcs11
2+
3+
/*
4+
Copyright IBM Corp. All Rights Reserved.
5+
6+
SPDX-License-Identifier: Apache-2.0
7+
*/
8+
9+
package pkcs11
10+
11+
import (
12+
"github.com/hyperledger/fabric/bccsp"
13+
)
14+
15+
type KeyIDMapping struct {
16+
SKI string `yaml:"SKI,omitempty"`
17+
ID string `yaml:"ID,omitempty"`
18+
}
19+
20+
type PKCS11Opts struct {
21+
// Default algorithms when not specified (Deprecated?)
22+
Security int `yaml:"Security"`
23+
Hash string `yaml:"Hash"`
24+
25+
// PKCS11 options
26+
Library string `yaml:"Library"`
27+
Label string `yaml:"Label"`
28+
Pin string `yaml:"Pin"`
29+
SoftwareVerify bool `yaml:"SoftwareVerify,omitempty"`
30+
Immutable bool `yaml:"Immutable,omitempty"`
31+
AltID string `yaml:"AltId,omitempty"`
32+
KeyIDs []KeyIDMapping `yaml:"KeyIds,omitempty" mapstructure:"KeyIds"`
33+
}
34+
35+
func NewProvider(opts any, ks bccsp.KeyStore, mapper func(ski []byte) []byte) (bccsp.BCCSP, error) {
36+
panic("pkcs11 not included in build. Use: go build -tags pkcs11")
37+
}
38+
39+
func ToPKCS11OptsOpts(o any) *PKCS11Opts {
40+
panic("pkcs11 not included in build. Use: go build -tags pkcs11")
41+
}
42+
43+
func FindPKCS11Lib() (lib, pin, label string, err error) {
44+
panic("pkcs11 not included in build. Use: go build -tags pkcs11")
45+
}

token/services/identity/msp/x509/msp/pkcs11/pkcs11.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//go:build pkcs11
2+
13
/*
24
Copyright IBM Corp. All Rights Reserved.
35
@@ -9,6 +11,7 @@ package pkcs11
911
import (
1012
"os"
1113

14+
"github.com/hyperledger/fabric/bccsp"
1215
"github.com/hyperledger/fabric/bccsp/pkcs11"
1316
"github.com/pkg/errors"
1417
)
@@ -25,6 +28,15 @@ type (
2528
KeyIDMapping = pkcs11.KeyIDMapping
2629
)
2730

31+
// NewProvider returns a pkcs11 provider
32+
func NewProvider(opts PKCS11Opts, ks bccsp.KeyStore, mapper func(ski []byte) []byte) (*pkcs11.Provider, error) {
33+
csp, err := pkcs11.New(opts, ks, pkcs11.WithKeyMapper(mapper))
34+
if err != nil {
35+
return nil, errors.WithMessagef(err, "Failed initializing PKCS11 library with config [%+v]", opts)
36+
}
37+
return csp, nil
38+
}
39+
2840
// FindPKCS11Lib attempts to find the PKCS11 library based on the given configuration
2941
func FindPKCS11Lib() (lib, pin, label string, err error) {
3042
if lib = os.Getenv("PKCS11_LIB"); lib == "" {

0 commit comments

Comments
 (0)