|
| 1 | +/* |
| 2 | +Copyright IBM Corp. All Rights Reserved. |
| 3 | +
|
| 4 | +SPDX-License-Identifier: Apache-2.0 |
| 5 | +*/ |
| 6 | + |
| 7 | +package crypto |
| 8 | + |
| 9 | +import ( |
| 10 | + "fmt" |
| 11 | + "strings" |
| 12 | + "testing" |
| 13 | + |
| 14 | + "github.com/hyperledger/fabric-lib-go/bccsp" |
| 15 | + "github.com/stretchr/testify/assert" |
| 16 | + "github.com/stretchr/testify/require" |
| 17 | +) |
| 18 | + |
| 19 | +const testPIN = "s3cr3t-pin-9999" |
| 20 | + |
| 21 | +// PKCS11.String must redact the PIN and never emit it verbatim, whether the |
| 22 | +// *PKCS11 is logged directly or as a field of a *BCCSP. |
| 23 | +func TestPKCS11_String_RedactsPin(t *testing.T) { |
| 24 | + p := &PKCS11{Library: "/usr/lib/libpkcs11.so", Label: "tok", Pin: testPIN} |
| 25 | + |
| 26 | + s := p.String() |
| 27 | + assert.Contains(t, s, "[REDACTED]") |
| 28 | + assert.NotContains(t, s, testPIN) |
| 29 | + // Non-secret fields remain visible for debugging. |
| 30 | + assert.Contains(t, s, "/usr/lib/libpkcs11.so") |
| 31 | + assert.Contains(t, s, "tok") |
| 32 | + |
| 33 | + // %v / %s go through the Stringer as well. |
| 34 | + assert.NotContains(t, fmt.Sprintf("%v", p), testPIN) |
| 35 | + assert.NotContains(t, fmt.Sprintf("%s", p), testPIN) |
| 36 | +} |
| 37 | + |
| 38 | +// A nil *PKCS11 must format without panicking (SW-only configs have a nil PKCS11). |
| 39 | +func TestPKCS11_String_Nil(t *testing.T) { |
| 40 | + var p *PKCS11 |
| 41 | + assert.NotPanics(t, func() { _ = p.String() }) |
| 42 | + assert.Equal(t, "<nil>", p.String()) |
| 43 | + assert.NotPanics(t, func() { _ = fmt.Sprintf("%v", p) }) |
| 44 | +} |
| 45 | + |
| 46 | +// Formatting a *BCCSP (as setup.go and kmp.go do) must redact the nested PKCS11 PIN, |
| 47 | +// and must not panic when the PKCS11 field is nil. |
| 48 | +func TestBCCSP_Format_RedactsPin(t *testing.T) { |
| 49 | + t.Run("WithPKCS11", func(t *testing.T) { |
| 50 | + cfg := &BCCSP{Default: "PKCS11", PKCS11: &PKCS11{Label: "tok", Pin: testPIN}} |
| 51 | + out := fmt.Sprintf("%v", cfg) |
| 52 | + assert.Contains(t, out, "[REDACTED]") |
| 53 | + assert.NotContains(t, out, testPIN) |
| 54 | + }) |
| 55 | + t.Run("NilPKCS11", func(t *testing.T) { |
| 56 | + cfg := &BCCSP{Default: "SW", SW: &SoftwareProvider{Hash: "SHA2", Security: 256}} |
| 57 | + assert.NotPanics(t, func() { _ = fmt.Sprintf("%v", cfg) }) |
| 58 | + }) |
| 59 | + t.Run("NilBCCSP", func(t *testing.T) { |
| 60 | + var cfg *BCCSP |
| 61 | + assert.NotPanics(t, func() { _ = fmt.Sprintf("%v", cfg) }) |
| 62 | + }) |
| 63 | +} |
| 64 | + |
| 65 | +// getIdentityFactory must never mutate the caller's live config and must never panic, |
| 66 | +// across the nil, SW-only, and error paths. Regression test for the pointer-aliasing |
| 67 | +// redaction bug (Issue #2069) that overwrote the real PIN and dereferenced nil. |
| 68 | +func TestGetIdentityFactory_NoMutationNoPanic(t *testing.T) { |
| 69 | + conf := &Config{CryptoConfig: &CryptoConfig{SignatureHashFamily: bccsp.SHA2}} |
| 70 | + |
| 71 | + t.Run("NilBccspConfig", func(t *testing.T) { |
| 72 | + // Default SW provider; nil bccspConfig is a supported, common path. |
| 73 | + assert.NotPanics(t, func() { |
| 74 | + _, err := getIdentityFactory(conf, nil, nil) |
| 75 | + require.NoError(t, err) |
| 76 | + }) |
| 77 | + }) |
| 78 | + |
| 79 | + t.Run("SWOnlyConfigLeavesPinUntouched", func(t *testing.T) { |
| 80 | + bccspConfig := &BCCSP{ |
| 81 | + Default: "SW", |
| 82 | + SW: &SoftwareProvider{Hash: "SHA2", Security: 256}, |
| 83 | + // A PIN may be present even on the SW path; it must survive verbatim. |
| 84 | + PKCS11: &PKCS11{Label: "tok", Pin: testPIN}, |
| 85 | + } |
| 86 | + |
| 87 | + _, err := getIdentityFactory(conf, bccspConfig, nil) |
| 88 | + require.NoError(t, err) |
| 89 | + assert.Equal(t, testPIN, bccspConfig.PKCS11.Pin, "the caller's live PIN must not be mutated") |
| 90 | + }) |
| 91 | + |
| 92 | + t.Run("ErrorPathRedactsWithoutMutating", func(t *testing.T) { |
| 93 | + bccspConfig := &BCCSP{ |
| 94 | + Default: "does-not-exist", // forces GetBCCSPFromConf to return an error |
| 95 | + PKCS11: &PKCS11{Label: "tok", Pin: testPIN}, |
| 96 | + } |
| 97 | + |
| 98 | + _, err := getIdentityFactory(conf, bccspConfig, nil) |
| 99 | + require.Error(t, err) |
| 100 | + assert.True(t, strings.Contains(err.Error(), "[REDACTED]"), "error must show redaction marker") |
| 101 | + assert.False(t, strings.Contains(err.Error(), testPIN), "error must not leak the PIN") |
| 102 | + assert.Equal(t, testPIN, bccspConfig.PKCS11.Pin, "the caller's live PIN must not be mutated") |
| 103 | + }) |
| 104 | +} |
0 commit comments