-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* PKCS#11 untested * PKCS#11 test * PKCS#11 doc * missing docs * limit PKCS#11 handle parsing to 32 bit * Ed25519 support for PKCS#11 backend * go-pkcs11 v0.2.1 * chore: remove windows builds from goreleaser --------- Co-authored-by: GImbrailo <[email protected]>
- Loading branch information
Showing
13 changed files
with
520 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
--- | ||
id: pkcs11 | ||
title: PKCS#11 | ||
--- | ||
|
||
# PKCS#11 Vault | ||
|
||
## Configuration | ||
|
||
||||| | ||
|--- |--- |--- |--- | | ||
|Name|Type|Required|Description| | ||
|library_path|string|✅|Library Path| | ||
|pin|string|✅|User PIN| | ||
|slot|string||Slot ID| | ||
|label|string||Limit key search to the specified label (use in case of multiple key pairs in the same token)| | ||
|object_ih|hex||Limit key search to the specified object ID (use in case of multiple key pairs in the same token)| | ||
|
||
**Note**: If the token contains multiple key pairs, every pair must have unique label or ID shared between private and public parts. | ||
|
||
### Example | ||
|
||
```yaml | ||
library_path: /opt/homebrew/lib/softhsm/libsofthsm2.so | ||
pin: 1234 | ||
slot: 0x4d0b85a2 | ||
label: TestKey | ||
``` | ||
## Environment variables | ||
* `PKCS11_PATH` | ||
* `PKCS11_PIN` | ||
* `PKCS11_SLOT` | ||
* `PKCS11_LABEL` | ||
* `PKCS11_OBJECT_ID` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package pkcs11 | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
"runtime" | ||
"testing" | ||
|
||
"github.com/ecadlabs/gotez/v2/crypt" | ||
"github.com/ecadlabs/signatory/pkg/vault" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
const ( | ||
libSoftHSMPathUnix = "/usr/lib/softhsm/libsofthsm2.so" | ||
libSoftHSMPathMac = "/opt/homebrew/lib/softhsm/libsofthsm2.so" | ||
|
||
userPIN = "1234" | ||
soPIN = "1234" | ||
keyLabel = "TestKey" | ||
tokenLabel = "TestToken" | ||
) | ||
|
||
func TestPKCS11Vault(t *testing.T) { | ||
var path string | ||
if runtime.GOOS == "darwin" { | ||
path = libSoftHSMPathMac | ||
} else { | ||
path = libSoftHSMPathUnix | ||
} | ||
|
||
if _, err := os.Stat(path); err != nil { | ||
if errors.Is(err, os.ErrNotExist) { | ||
t.Skipf("libsofthsm2 not installed, skipping testing") | ||
} | ||
t.Fatal(err) | ||
} | ||
|
||
if _, err := exec.LookPath("pkcs11-tool"); err != nil { | ||
if errors.Is(err, exec.ErrNotFound) { | ||
t.Skipf("pkcs11-tool not installed, skipping testing") | ||
} | ||
t.Fatal(err) | ||
} | ||
|
||
configPath := filepath.Join(t.TempDir(), "softhsm.conf") | ||
tokensPath := t.TempDir() | ||
|
||
fd, err := os.OpenFile(configPath, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644) | ||
require.NoError(t, err) | ||
fmt.Fprintf(fd, "directories.tokendir = %s\n", tokensPath) | ||
fd.Close() | ||
|
||
t.Setenv("SOFTHSM2_CONF", configPath) | ||
|
||
out, err := exec.Command("pkcs11-tool", "--verbose", "--module", path, "--init-token", "--label", tokenLabel, "--so-pin", soPIN).CombinedOutput() | ||
if err != nil { | ||
t.Fatal(string(out)) | ||
} | ||
|
||
out, err = exec.Command("pkcs11-tool", "--verbose", "--module", path, "--login", "--so-pin", soPIN, "--init-pin", "--pin", userPIN).CombinedOutput() | ||
if err != nil { | ||
t.Fatal(string(out)) | ||
} | ||
|
||
out, err = exec.Command("pkcs11-tool", "--verbose", "--module", path, "--login", "--pin", userPIN, "--keypairgen", "--key-type", "EC:prime256v1", "--usage-sign", "--label", keyLabel).CombinedOutput() | ||
if err != nil { | ||
t.Fatal(string(out)) | ||
} | ||
|
||
v, err := New(context.Background(), &Config{ | ||
LibraryPath: path, | ||
Pin: userPIN, | ||
Label: keyLabel, | ||
}) | ||
require.NoError(t, err) | ||
t.Cleanup(func() { v.Close() }) | ||
|
||
require.NoError(t, v.Unlock(context.Background())) | ||
|
||
keys, err := vault.Collect(v.ListPublicKeys(context.Background())) | ||
require.NoError(t, err) | ||
require.Len(t, keys, 1) | ||
k0 := keys[0].PublicKey().(*crypt.ECDSAPublicKey) | ||
|
||
key, err := v.GetPublicKey(context.Background(), keys[0].ID()) | ||
require.NoError(t, err) | ||
k1 := key.PublicKey().(*crypt.ECDSAPublicKey) | ||
require.Equal(t, k0, k1) | ||
} |
Oops, something went wrong.