Skip to content

Commit 32c7dfa

Browse files
committed
hide pkcs11 behind a build tag so that token sdk is pure go by default
Signed-off-by: Arne Rutjes <[email protected]>
1 parent 6dfa071 commit 32c7dfa

File tree

5 files changed

+76
-13
lines changed

5 files changed

+76
-13
lines changed

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)).

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)