Skip to content

Commit

Permalink
fix: make PKCS#12 truststores deterministic
Browse files Browse the repository at this point in the history
Signed-off-by: Erik Godding Boye <[email protected]>
  • Loading branch information
erikgb committed Oct 12, 2024
1 parent 1756157 commit 0049e69
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 12 deletions.
11 changes: 8 additions & 3 deletions pkg/bundle/internal/truststore/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"crypto/sha256"
"encoding/hex"
"fmt"
"math/rand"

"github.com/pavlo-v-chernykh/keystore-go/v4"
"software.sslmate.com/src/go-pkcs12"
Expand Down Expand Up @@ -98,10 +99,14 @@ func (e pkcs12Encoder) Encode(trustBundle *util.CertPool) ([]byte, error) {
})
}

encoder := pkcs12.LegacyRC2
encoder := pkcs12.Passwordless

if e.password == "" {
encoder = pkcs12.Passwordless
if e.password != "" {
encoder = pkcs12.LegacyRC2.
// Short-circuiting the rand generator to make our PKCS#12 truststores deterministic.
// This should allow use of unconditional SSA requests from controller.
// See: https://cert-manager.io/docs/faq/#why-are-passwords-on-jks-or-pkcs12-files-not-helpful
WithRand(rand.New(rand.NewSource(1))) //#nosec G404
}

return encoder.EncodeTrustStoreEntries(entries, e.password)
Expand Down
11 changes: 2 additions & 9 deletions pkg/bundle/internal/truststore/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ import (

func Test_Encoder_Deterministic(t *testing.T) {
tests := map[string]struct {
encoder Encoder
expNonDeterministic bool
encoder Encoder
}{
"JKS default password": {
encoder: NewJKSEncoder(v1alpha1.DefaultJKSPassword),
Expand All @@ -46,8 +45,6 @@ func Test_Encoder_Deterministic(t *testing.T) {
},
"PKCS#12 custom password": {
encoder: NewPKCS12Encoder("my-password"),
// FIXME: We should try to make all encoders deterministic
expNonDeterministic: true,
},
}

Expand All @@ -72,11 +69,7 @@ func Test_Encoder_Deterministic(t *testing.T) {
t.Fatalf("didn't expect an error but got: %s", err)
}

if test.expNonDeterministic {
assert.NotEqual(t, store, store2, "expected encoder to be non-deterministic")
} else {
assert.Equal(t, store, store2, "expected encoder to be deterministic")
}
assert.Equal(t, store, store2, "expected encoder to be deterministic")
})
}
}
Expand Down

0 comments on commit 0049e69

Please sign in to comment.