Skip to content
This repository was archived by the owner on Apr 25, 2025. It is now read-only.

Commit 8914810

Browse files
committed
[FAB-7763] Tool to import keys into HSM test fixture
Change-Id: I4b47466ab5c7842138292d0a7ea6176f3fd043b3 Signed-off-by: gbolo <george.bolo@gmail.com>
1 parent b3bc86f commit 8914810

File tree

9 files changed

+859
-1
lines changed

9 files changed

+859
-1
lines changed

scripts/_go/src/pkcs11helper/Gopkg.lock

Lines changed: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#
2+
# Copyright SecureKey Technologies Inc. All Rights Reserved.
3+
#
4+
# SPDX-License-Identifier: Apache-2.0
5+
#
6+
7+
# Gopkg.toml example
8+
#
9+
# Refer to https://github.com/golang/dep/blob/master/docs/Gopkg.toml.md
10+
# for detailed Gopkg.toml documentation.
11+
#
12+
# required = ["github.com/user/thing/cmd/thing"]
13+
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"]
14+
#
15+
# [[constraint]]
16+
# name = "github.com/user/project"
17+
# version = "1.0.0"
18+
#
19+
# [[constraint]]
20+
# name = "github.com/user/project2"
21+
# branch = "dev"
22+
# source = "github.com/myfork/project2"
23+
#
24+
# [[override]]
25+
# name = "github.com/x/y"
26+
# version = "2.4.0"
27+
28+
29+
[[constraint]]
30+
branch = "master"
31+
name = "github.com/miekg/pkcs11"
32+
33+
[[constraint]]
34+
branch = "master"
35+
name = "github.com/olekukonko/tablewriter"
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#
2+
# Copyright SecureKey Technologies Inc. All Rights Reserved.
3+
#
4+
# SPDX-License-Identifier: Apache-2.0
5+
#
6+
7+
/pkcs11helper
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# pkcs11 helper tool
2+
3+
Meant to help import keys for use in fabric.
4+
5+
```
6+
# prepare slot
7+
softhsm2-util --init-token --label ForFabric --pin 98765432 --free --so-pin 1234
8+
9+
# tool options
10+
./pkcs11helper -help
11+
Usage of ./pkcs11helper:
12+
-action string
13+
list,import (default "list")
14+
-keyFile string
15+
path to pem encoded EC private key you want to import (default "testdata/key.ec.pem")
16+
-lib string
17+
Location of pkcs11 library (Defaults to a list of possible paths to libsofthsm2.so)
18+
-pin string
19+
Slot PIN (default "98765432")
20+
-slot string
21+
Slot Label (default "ForFabric")
22+
23+
# import ec key
24+
./pkcs11helper -action import -keyFile testdata/key.ec.pem
25+
PKCS11 provider found specified slot label: ForFabric (slot: 0, index: 0)
26+
Object was imported with CKA_LABEL:BCPUB1 CKA_ID:018f389d200e48536367f05b99122f355ba33572009bd2b8b521cdbbb717a5b5
27+
Object was imported with CKA_LABEL:BCPRV1 CKA_ID:018f389d200e48536367f05b99122f355ba33572009bd2b8b521cdbbb717a5b5
28+
29+
# list objects
30+
./pkcs11helper -action list
31+
PKCS11 provider found specified slot label: ForFabric (slot: 0, index: 0)
32+
+-------+-----------------+-----------+------------------------------------------------------------------+
33+
| COUNT | CKA CLASS | CKA LABEL | CKA ID |
34+
+-------+-----------------+-----------+------------------------------------------------------------------+
35+
| 001 | CKO_PUBLIC_KEY | BCPUB1 | 018f389d200e48536367f05b99122f355ba33572009bd2b8b521cdbbb717a5b5 |
36+
| 002 | CKO_PRIVATE_KEY | BCPRV1 | 018f389d200e48536367f05b99122f355ba33572009bd2b8b521cdbbb717a5b5 |
37+
+-------+-----------------+-----------+------------------------------------------------------------------+
38+
Total objects found (max 50): 2
39+
```
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/*
2+
Copyright SecureKey Technologies Inc. All Rights Reserved.
3+
4+
SPDX-License-Identifier: Apache-2.0
5+
*/
6+
package main
7+
8+
import (
9+
"flag"
10+
"fmt"
11+
"os"
12+
"strings"
13+
14+
pw "pkcs11helper/pkg/pkcs11wrapper"
15+
"github.com/miekg/pkcs11"
16+
)
17+
18+
var (
19+
defaultPkcs11LibPaths = []string{
20+
"/usr/lib/softhsm/libsofthsm2.so",
21+
"/usr/lib/x86_64-linux-gnu/softhsm/libsofthsm2.so",
22+
"/usr/lib/s390x-linux-gnu/softhsm/libsofthsm2.so",
23+
"/usr/lib/powerpc64le-linux-gnu/softhsm/libsofthsm2.so",
24+
"/usr/local/Cellar/softhsm/2.1.0/lib/softhsm/libsofthsm2.so",
25+
}
26+
)
27+
28+
func main() {
29+
30+
// get flags
31+
pkcs11Library := flag.String("lib", "", "Location of pkcs11 library (Defaults to a list of possible paths to libsofthsm2.so)")
32+
slotLabel := flag.String("slot", "ForFabric", "Slot Label")
33+
slotPin := flag.String("pin", "98765432", "Slot PIN")
34+
action := flag.String("action", "list", "list,import")
35+
keyFile := flag.String("keyFile", "testdata/key.ec.pem", "path to pem encoded EC private key you want to import")
36+
37+
flag.Parse()
38+
39+
// initialize pkcs11
40+
var p11Lib string
41+
var err error
42+
43+
if *pkcs11Library == "" {
44+
// if no lib is specified, just try to find libsofthsm2.so
45+
p11Lib, err = searchForLib(strings.Join(defaultPkcs11LibPaths,","))
46+
exitWhenError(err)
47+
} else {
48+
p11Lib, err = searchForLib(*pkcs11Library)
49+
exitWhenError(err)
50+
}
51+
52+
p11w := pw.Pkcs11Wrapper{
53+
Library: pw.Pkcs11Library{
54+
Path: p11Lib,
55+
},
56+
SlotLabel: *slotLabel,
57+
SlotPin: *slotPin,
58+
}
59+
60+
err = p11w.InitContext()
61+
exitWhenError(err)
62+
63+
err = p11w.InitSession()
64+
exitWhenError(err)
65+
66+
err = p11w.Login()
67+
exitWhenError(err)
68+
69+
// defer cleanup
70+
defer p11w.CloseContext()
71+
72+
// complete actions
73+
switch *action {
74+
75+
case "import":
76+
err = p11w.ImportECKeyFromFile(*keyFile)
77+
exitWhenError(err)
78+
79+
default:
80+
p11w.ListObjects(
81+
[]*pkcs11.Attribute{},
82+
50,
83+
)
84+
85+
}
86+
87+
}
88+
89+
// exit properly
90+
func exitWhenError(err error) {
91+
if err != nil {
92+
fmt.Println("Error:", err)
93+
os.Exit(1)
94+
}
95+
}
96+
97+
// return the first path that is found
98+
func searchForLib(paths string) (firstFound string, err error) {
99+
100+
libPaths := strings.Split(paths, ",")
101+
for _, path := range libPaths {
102+
if _, err = os.Stat(strings.TrimSpace(path)); !os.IsNotExist(err) {
103+
firstFound = strings.TrimSpace(path)
104+
break
105+
}
106+
}
107+
108+
if firstFound == "" {
109+
err = fmt.Errorf("no suitable paths for pkcs11 library found: %s", paths)
110+
}
111+
112+
return
113+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
-----BEGIN PRIVATE KEY-----
2+
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgXa3mln4anewXtqrM
3+
hMw6mfZhslkRa/j9P790ToKjlsihRANCAARnxLhXvU4EmnIwhVl3Bh0VcByQi2um
4+
9KsJ/QdCDjRZb1dKg447voj5SZ8SSZOUglc/v8DJFFJFTfygjwi+27gz
5+
-----END PRIVATE KEY-----

0 commit comments

Comments
 (0)