mirror of
https://github.com/aquasecurity/trivy.git
synced 2026-12-02 01:40:33 +00:00
feat(crypto): add check for small EC private keys (#11479)
This commit is contained in:
1 parent
60b6903279
commit
e2c05f7ae6
3 files changed
+156
-1
No files matched your search
@@ -121,10 +121,11 @@ Trivy runs built-in checks to find weak cryptographic assets and reports the fin
|
||||
| `CRYPTO-0002` | Small RSA key | key | the RSA key is shorter than 2048 bits | HIGH |
|
||||
| `CRYPTO-0003` | Weak elliptic curve | algorithm | the classical security level of an EC algorithm is below 112 bits | HIGH |
|
||||
| `CRYPTO-0004` | Expired certificate | certificate | `NotAfter` is before the time of the scan | MEDIUM |
|
||||
| `CRYPTO-0005` | Small EC private key | key | the public point of a P-256, P-384 or P-521 key equals k*G for k from 1 to 255, so the private key can be guessed | CRITICAL |
|
||||
|
||||
A key of unknown size or a curve without a security level gives no finding. Certificates in system trust stores are checked too, including old roots signed with SHA-1.
|
||||
|
||||
A small RSA key in a certificate gets the finding on its key asset. The EC finding points to the algorithm asset, which is reported once per file, so several keys on one curve in a file give one finding.
|
||||
A small RSA key or a small EC private key gets the finding on its key asset. The weak curve finding points to the algorithm asset, which is reported once per file, so several keys on one weak curve in a file give one finding.
|
||||
|
||||
A certificate signed with RSA-PSS gives no SHA-1 finding, because Trivy does not read the hash from the RSA-PSS parameters.
|
||||
|
||||
@@ -165,6 +166,7 @@ Every entry of `CryptoAssets` in JSON output has an `AssetID`, and a finding ref
|
||||
| `CRYPTO-0002` | `KeySize` |
|
||||
| `CRYPTO-0003` | `Curve`, `ClassicalSecurityLevel` |
|
||||
| `CRYPTO-0004` | `NotAfter` |
|
||||
| `CRYPTO-0005` | `Curve` |
|
||||
|
||||
Table output shows the findings without the inventory. Identical rows are merged and the asset name ends with their number, such as `x2`. `Total` counts every finding, and JSON output tells the assets apart.
|
||||
|
||||
|
||||
@@ -1,10 +1,15 @@
|
||||
package check_test
|
||||
|
||||
import (
|
||||
"crypto/ecdsa"
|
||||
"crypto/elliptic"
|
||||
"crypto/rand"
|
||||
"math/big"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/aquasecurity/trivy/pkg/crypto"
|
||||
"github.com/aquasecurity/trivy/pkg/crypto/check"
|
||||
@@ -88,6 +93,32 @@ func publicKey(filePath, algorithmOID string, size int, format ftypes.CryptoKeyF
|
||||
})
|
||||
}
|
||||
|
||||
func ecKey(t *testing.T, curve elliptic.Curve, k int, keyType ftypes.CryptoKeyType) ftypes.CryptoAsset {
|
||||
t.Helper()
|
||||
|
||||
var pub *ecdsa.PublicKey
|
||||
if k == 0 {
|
||||
priv, err := ecdsa.GenerateKey(curve, rand.Reader)
|
||||
require.NoError(t, err)
|
||||
pub = &priv.PublicKey
|
||||
} else {
|
||||
scalar := make([]byte, (curve.Params().BitSize+7)/8)
|
||||
big.NewInt(int64(k)).FillBytes(scalar)
|
||||
priv, err := ecdsa.ParseRawPrivateKey(curve, scalar)
|
||||
require.NoError(t, err)
|
||||
pub = &priv.PublicKey
|
||||
}
|
||||
|
||||
key, _, err := crypto.DescribeKey(pub, keyType)
|
||||
require.NoError(t, err)
|
||||
return withID(ftypes.CryptoAsset{
|
||||
CryptoAssetInfo: key,
|
||||
FilePath: "key.pem",
|
||||
Format: ftypes.CryptoKeyFormatPKIX,
|
||||
Layer: layer,
|
||||
})
|
||||
}
|
||||
|
||||
func algorithm(filePath, oid, parameters string, classicalLevel *int) ftypes.CryptoAsset {
|
||||
return withID(ftypes.CryptoAsset{
|
||||
CryptoAssetInfo: ftypes.CryptoAssetInfo{
|
||||
@@ -201,6 +232,54 @@ func TestEvaluate(t *testing.T) {
|
||||
name: "small key of another algorithm",
|
||||
assets: []ftypes.CryptoAsset{publicKey("key.pem", "1.2.840.10040.4.1", 1024, ftypes.CryptoKeyFormatPKIX)},
|
||||
},
|
||||
{
|
||||
name: "small P-256 public key",
|
||||
assets: []ftypes.CryptoAsset{ecKey(t, elliptic.P256(), 230, ftypes.CryptoKeyTypePublic)},
|
||||
want: []types.CryptoFinding{
|
||||
finding("CRYPTO-0005", "Small EC private key", "CRITICAL", ecKey(t, elliptic.P256(), 230, ftypes.CryptoKeyTypePublic),
|
||||
types.CryptoEvidence{Curve: "P-256"}),
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "small P-384 public key",
|
||||
assets: []ftypes.CryptoAsset{ecKey(t, elliptic.P384(), 230, ftypes.CryptoKeyTypePublic)},
|
||||
want: []types.CryptoFinding{
|
||||
finding("CRYPTO-0005", "Small EC private key", "CRITICAL", ecKey(t, elliptic.P384(), 230, ftypes.CryptoKeyTypePublic),
|
||||
types.CryptoEvidence{Curve: "P-384"}),
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "small P-384 private key",
|
||||
assets: []ftypes.CryptoAsset{ecKey(t, elliptic.P384(), 230, ftypes.CryptoKeyTypePrivate)},
|
||||
want: []types.CryptoFinding{
|
||||
finding("CRYPTO-0005", "Small EC private key", "CRITICAL", ecKey(t, elliptic.P384(), 230, ftypes.CryptoKeyTypePrivate),
|
||||
types.CryptoEvidence{Curve: "P-384"}),
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "small P-521 public key",
|
||||
assets: []ftypes.CryptoAsset{ecKey(t, elliptic.P521(), 230, ftypes.CryptoKeyTypePublic)},
|
||||
want: []types.CryptoFinding{
|
||||
finding("CRYPTO-0005", "Small EC private key", "CRITICAL", ecKey(t, elliptic.P521(), 230, ftypes.CryptoKeyTypePublic),
|
||||
types.CryptoEvidence{Curve: "P-521"}),
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "P-384 key at the limit",
|
||||
assets: []ftypes.CryptoAsset{ecKey(t, elliptic.P384(), 255, ftypes.CryptoKeyTypePublic)},
|
||||
want: []types.CryptoFinding{
|
||||
finding("CRYPTO-0005", "Small EC private key", "CRITICAL", ecKey(t, elliptic.P384(), 255, ftypes.CryptoKeyTypePublic),
|
||||
types.CryptoEvidence{Curve: "P-384"}),
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "P-384 key just above the limit",
|
||||
assets: []ftypes.CryptoAsset{ecKey(t, elliptic.P384(), 256, ftypes.CryptoKeyTypePublic)},
|
||||
},
|
||||
{
|
||||
name: "random P-384 key",
|
||||
assets: []ftypes.CryptoAsset{ecKey(t, elliptic.P384(), 0, ftypes.CryptoKeyTypePublic)},
|
||||
},
|
||||
{
|
||||
name: "EC strength below 112 bits",
|
||||
assets: []ftypes.CryptoAsset{algorithm("key.pem", oidECPublicKey, "curve=P-192", new(80))},
|
||||
@@ -321,6 +400,14 @@ func TestMessage(t *testing.T) {
|
||||
},
|
||||
want: "Certificate expired on 2024-03-01",
|
||||
},
|
||||
{
|
||||
name: "small EC private key",
|
||||
finding: types.CryptoFinding{
|
||||
RuleID: "CRYPTO-0005",
|
||||
Evidence: types.CryptoEvidence{Curve: "P-256"},
|
||||
},
|
||||
want: "Private key of this P-256 key is at most 255 and can be recovered",
|
||||
},
|
||||
{
|
||||
name: "unknown rule",
|
||||
finding: types.CryptoFinding{RuleID: "CRYPTO-9999"},
|
||||
|
||||
@@ -1,13 +1,18 @@
|
||||
package check
|
||||
|
||||
import (
|
||||
"crypto/ecdsa"
|
||||
"crypto/elliptic"
|
||||
"fmt"
|
||||
"math/big"
|
||||
"slices"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
dbTypes "github.com/aquasecurity/trivy-db/pkg/types"
|
||||
"github.com/aquasecurity/trivy/pkg/crypto"
|
||||
ftypes "github.com/aquasecurity/trivy/pkg/fanal/types"
|
||||
"github.com/aquasecurity/trivy/pkg/set"
|
||||
"github.com/aquasecurity/trivy/pkg/types"
|
||||
)
|
||||
|
||||
@@ -44,6 +49,14 @@ var rules = []rule{
|
||||
match: matchExpiredCertificate,
|
||||
message: expiredCertificateMessage,
|
||||
},
|
||||
{
|
||||
id: "CRYPTO-0005",
|
||||
title: "Small EC private key",
|
||||
severity: dbTypes.SeverityCritical,
|
||||
kind: ftypes.CryptoKindKey,
|
||||
match: matchSmallECPrivateKey,
|
||||
message: smallECPrivateKeyMessage,
|
||||
},
|
||||
}
|
||||
|
||||
const (
|
||||
@@ -110,6 +123,55 @@ func matchWeakCurve(asset ftypes.CryptoAsset, _ time.Time) (types.CryptoEvidence
|
||||
}, true
|
||||
}
|
||||
|
||||
// maxSmallScalar is the largest private key value that counts as small.
|
||||
// All 8 affected eMudhra keys used values up to 255.
|
||||
// See https://bugzilla.mozilla.org/show_bug.cgi?id=2083449#c5
|
||||
const maxSmallScalar = 255
|
||||
|
||||
var smallKeyCurves = []elliptic.Curve{
|
||||
elliptic.P256(),
|
||||
elliptic.P384(),
|
||||
elliptic.P521(),
|
||||
}
|
||||
|
||||
// smallKeyIdentities maps a curve name to the identities of the public keys
|
||||
// for the private keys from 1 to maxSmallScalar.
|
||||
var smallKeyIdentities = sync.OnceValue(func() map[string]set.Set[string] {
|
||||
byCurve := make(map[string]set.Set[string], len(smallKeyCurves))
|
||||
for _, curve := range smallKeyCurves {
|
||||
params := curve.Params()
|
||||
// The private key is a big-endian integer of (BitSize+7)/8 bytes.
|
||||
scalar := make([]byte, (params.BitSize+7)/8)
|
||||
|
||||
identities := set.New[string]()
|
||||
for k := 1; k <= maxSmallScalar; k++ {
|
||||
big.NewInt(int64(k)).FillBytes(scalar)
|
||||
priv, err := ecdsa.ParseRawPrivateKey(curve, scalar)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
key, _, err := crypto.DescribeKey(&priv.PublicKey, ftypes.CryptoKeyTypePublic)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
identities.Append(key.Identity.Value)
|
||||
}
|
||||
byCurve[params.Name] = identities
|
||||
}
|
||||
return byCurve
|
||||
})
|
||||
|
||||
func matchSmallECPrivateKey(asset ftypes.CryptoAsset, _ time.Time) (types.CryptoEvidence, bool) {
|
||||
if asset.Key == nil || asset.Key.Curve == "" || asset.Identity.Method != ftypes.CryptoMethodSPKISHA256 {
|
||||
return types.CryptoEvidence{}, false
|
||||
}
|
||||
identities, found := smallKeyIdentities()[asset.Key.Curve]
|
||||
if !found || !identities.Contains(asset.Identity.Value) {
|
||||
return types.CryptoEvidence{}, false
|
||||
}
|
||||
return types.CryptoEvidence{Curve: asset.Key.Curve}, true
|
||||
}
|
||||
|
||||
func matchExpiredCertificate(asset ftypes.CryptoAsset, now time.Time) (types.CryptoEvidence, bool) {
|
||||
if asset.Certificate == nil || asset.Certificate.NotAfter.IsZero() ||
|
||||
!asset.Certificate.NotAfter.Before(now) {
|
||||
@@ -144,6 +206,10 @@ func expiredCertificateMessage(e types.CryptoEvidence) string {
|
||||
return "Certificate expired on " + e.NotAfter.UTC().Format("2006-01-02")
|
||||
}
|
||||
|
||||
func smallECPrivateKeyMessage(e types.CryptoEvidence) string {
|
||||
return fmt.Sprintf("Private key of this %s key is at most %d and can be recovered", e.Curve, maxSmallScalar)
|
||||
}
|
||||
|
||||
// relatedAlgorithm returns the identity of the algorithm the asset has the given
|
||||
// relationship with.
|
||||
func relatedAlgorithm(asset ftypes.CryptoAsset, relationship ftypes.CryptoRelationshipType) (ftypes.CryptoIdentity, bool) {
|
||||
|
||||
Reference in new issue
Block a user