← Back to Pull requests

fix: make PKCS#12 truststores deterministic #455

Closed
erikgb closed this pull request without merging on Oct 24, 2024 16:13 UTC
View on GitHub
erikgb erikgb commented on Oct 12, 2024 16:18 UTC
Author

This PR might appear a bit controversial, since it short-circuits the random generator for our PKCS#12 truststores. 🤠 But there is a reason! My end goal is to greatly simplify the Bundle controller by removing the needsUpdate-logic and replacing it with well-tuned, but unconditional, SSA requests.

Diff: 50 lines (+40 / -10) across 3 files Reviewer: munnerz Review duration: 17275m
All Vouch review confidence checks passed
Residual risk 0.05 = Change Risk (0.29) × (1 − Review Confidence (0.84)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.29 · 50 lines diff across 3 files
0.29
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.73 · 5 substantive comment(s)
0.73
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 50%
84%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size diff_size
0.235
Real PR Conversation Timeline
10 items
cert-manager-prow[bot] cert-manager-prow[bot] commented on Oct 12, 2024 16:18 UTC
Bot

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please ask for approval from erikgb. For more information see the Kubernetes Code Review Process.

The full list of commands accepted by this bot can be found here.

<details open>
Needs approval from an approver in each of these files:

  • OWNERS
  • Approvers can indicate their approval by writing /approve in a comment
    Approvers can cancel approval by writing /approve cancel in a comment
    </details>
    <!-- META={"approvers":["erikgb"]} -->

    munnerz munnerz commented on code on Oct 12, 2024 18:01 UTC
    Code Review
    pkg/bundle/internal/truststore/types.go:1
    @@ -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

    Can this behaviour be made optional/tunable by the user/administrator? I imagine some environments may require use of a secure randomness source even if the underlying algorithm is considered insecure, though I'm not sure. What does other software in this space do?

    erikgb erikgb commented on code on Oct 12, 2024 19:16 UTC
    Code Review
    pkg/bundle/internal/truststore/types.go:1
    @@ -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

    Can this behaviour be made optional/tunable by the user/administrator?

    No, not without losing the opportunity to greatly simplify the controller (drop the needsUpdate-logic). Dropping a secure random source here is for sure controversial. But I think it could be worth it to simplify our code. Some additional arguments:

  • Our password-less PKCS#12 (the default) is deterministic.

  • We already stated (in our FAQ) that we support keystores/truststores for legacy reasons, and not because we consider them the recommended approach.

  • We already got a suggestion to include the truststore password just next to the truststore in the target configmap/secret. If we decide to implement anything in this direction, a requirement for a secure random source becomes even more meaningless.
  • What does other software in this space do?

    No idea. I am pretty sure that any software producing PKCS#12 truststores with a password attempts to use a secure random source. But a password-less PKCS#12 is the new standard for truststores in Java - after they abandoned JKS.

    SgtCoDFish SgtCoDFish commented on code on Oct 17, 2024 17:11 UTC
    Code Review
    pkg/bundle/internal/truststore/types.go:1
    @@ -21,6 +21,7 @@ import (
     	"crypto/sha256"
     	"encoding/hex"
     	"fmt"
    +	"math/rand"

    suggestion: Completely separate from James' concern (which may be a blocker). Sorry for the hastily written comment below!

    I think if we're going to do import math/rand it should be in a separate package entirely, so it's "quarantined". When we work on this file in the future we don't want to assume that rand means crypto/rand.

    I'd create internal/insecurerand/insecurerand.go, have it expose var Rand io.Reader = rand.New(rand.NewSource(1)).

    That was this file would import insecurerand.Rand and math/rand would be contained to one file only so it couldn't accidentally be used.

    That said, math/rand.Reader says it's not safe for concurrent use:

    Unlike the default Source used by top-level functions, this source is not safe for concurrent use by multiple goroutines.

    Might that be an issue if a user has multiple bundles?

    Further to that, is a seeded rand actually what we want? We want a deterministic io.Reader - could we create a bytes.Buffer with say N bytes all set to zero and use a reader on that instead? It looks like the use of rand doesn't actually need that many bytes: https://github.com/SSLMate/go-pkcs12/blob/v0.5.0/pkcs12.go#L705

    erikgb erikgb commented on code on Oct 17, 2024 18:23 UTC
    Code Review
    pkg/bundle/internal/truststore/types.go:1
    @@ -21,6 +21,7 @@ import (
     	"crypto/sha256"
     	"encoding/hex"
     	"fmt"
    +	"math/rand"

    Many good points here! Thanks Ash! I will update this PR - even if it's unlikely it will get merged.

    erikgb erikgb commented on code on Oct 17, 2024 18:34 UTC
    Code Review
    pkg/bundle/internal/truststore/types.go:1
    @@ -21,6 +21,7 @@ import (
     	"crypto/sha256"
     	"encoding/hex"
     	"fmt"
    +	"math/rand"

    I have updated the PR now addressing some of your comments.

    SgtCoDFish SgtCoDFish commented on Oct 18, 2024 16:50 UTC
    Contributor

    I've asked Venafi's security team if they have any input on this relating to auditing / compliance concerns - will report back if I hear anything!

    SgtCoDFish SgtCoDFish commented on Oct 19, 2024 08:37 UTC
    Contributor

    Feedback was that it's unlikely an auditor would know any context when they see something like this, and they're likely to just raise an issue that would create a bunch of extra work if they see this. They might _not_ see it depending on how they scan, but in any case it feels like deterministic PKCS#12 might be a non-starter unfortunately 🙃

    erikgb erikgb commented on Oct 24, 2024 16:13 UTC
    Author

    Thanks for all the input @SgtCoDFish @munnerz! I am going to close this PR now and shift my focus to how we can improve the current needsUpdate logic to be less error-prone. Handling data with randomness using SSA is not easy. 😅

    Reviewers
    Assignees
    erikgb erikgb
    Labels
    risk: low confidence: 84.3% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 29.4% (LOW) · Higher = larger blast radius
    29.4%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 84.3% · Lower = rushed or shallow review
    84.3%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 4.6% · Safe to merge
    4.6%