feat(crypto): report the EC-P-192 algorithm for P-192 keys and certificates (#11431)

This commit is contained in:
Nikita Pivkin authored and GitHub committed 2026-10-29 12:26:56 +00:00
1 parent 4cdb2db63d
commit 6ec0da294f
13 files changed
+571 -18

No files matched your search

+4 -2
View File
@@ -49,7 +49,7 @@ The levels are estimates for an algorithm and its parameters, taken from NIST an
| Algorithm | Classical security level (bits) | NIST quantum security level |
| --- | --- | --- |
| RSA, key size 1024 / 2048 / 3072 / 4096 / 6144 / 8192 bits | 80 / 112 / 128 / 152 / 176 / 200 | 0 |
| EC, curve P-224 / P-256 / P-384 / P-521 | 112 / 128 / 192 / 256 | 0 |
| EC, curve P-192 / P-224 / P-256 / P-384 / P-521 | 80 / 112 / 128 / 192 / 256 | 0 |
| DSA, (L, N) of (1024, 160) / (2048, 224) / (2048, 256) / (3072, 256) | 80 / 112 / 112 / 128 | 0 |
| Ed25519 / Ed448 | 128 / 224 | 0 |
| ML-DSA-44 / ML-DSA-65 / ML-DSA-87 | - | 2 / 3 / 5 |
@@ -60,7 +60,7 @@ A dash means that the level is left out of the report.
The levels come from these publications:
- RSA with a key size of 2048 bits and above takes the approximate maximum strengths in Appendix D, Table 4 of [NIST SP 800-56B Rev. 2](https://csrc.nist.gov/pubs/sp/800/56/b/r2/final). RSA with a key size of 1024 bits takes the nominal maximum of 80 from Table 2 of [NIST SP 800-57 Part 1 Rev. 5](https://csrc.nist.gov/pubs/sp/800/57/pt1/r5/final), whose row for a 1024-bit modulus states at most 80 bits.
- EC takes the ECC strength ranges in Table 2 of NIST SP 800-57 Part 1 Rev. 5.
- EC takes the ECC strength ranges in Table 2 of NIST SP 800-57 Part 1 Rev. 5. The curve P-192 takes the nominal maximum of 80 from the Table 2 row that states at most 80 bits.
- DSA takes the FFC entries in Table 2 of NIST SP 800-57 Part 1 Rev. 5 and the DSA strengths in Section 3 of [NIST SP 800-131A Rev. 2](https://csrc.nist.gov/pubs/sp/800/131/a/r2/final). The (1024, 160) pair takes the nominal maximum of 80 from the Table 2 row that states at most 80 bits.
- Ed25519 and Ed448 take their strengths from [RFC 8032, Section 8.5](https://datatracker.ietf.org/doc/html/rfc8032#section-8.5).
- ML-DSA takes its categories from [FIPS 204](https://csrc.nist.gov/pubs/fips/204/final).
@@ -225,6 +225,8 @@ Files larger than 10 MB are skipped, because a file of that size with one of the
Some objects are recognized and skipped. Certificate signing requests, CRLs and PKCS#7 bundles are not part of the inventory. OpenSSH keys, JKS and JCEKS keystores and OpenPGP keyrings are not covered yet. A certificate or key that uses an EC key with explicit curve parameters instead of a named curve cannot be parsed and is skipped as well. Private keys of X25519, X448 and Ed448 are skipped, although their public keys are reported.
From certificates and keys on the curve P-192, only the `EC-P-192` algorithm is extracted.
Trivy does not decrypt PKCS#12 keystores and does not report their private keys, so it reads only the certificates stored without encryption. keytool, OpenSSL and .NET encrypt the certificates unless encryption is turned off explicitly, such as with `-certpbe NONE` in OpenSSL or `-J-Dkeystore.pkcs12.certProtectionAlgorithm=NONE` in keytool. OpenSSL encrypts them even with an empty password. The `cacerts` trust store that ships with JDK 18 and later has no password and is read in full.
The `cacerts` of JDK 17 and earlier is a JKS keystore and is skipped. A PKCS#12 keystore protected by a public-key signature instead of a password-based MAC is not read. Parts of a keystore encoded in BER rather than DER are skipped. Trivy reads at most 10000 bags from one keystore.
+35
View File
@@ -102,6 +102,37 @@
"oid": "1.2.840.10040.4.3"
}
},
{
"bom-ref": "crypto:algorithm:oid:1.2.840.10045.2.1:curve%3DP-192",
"type": "cryptographic-asset",
"name": "EC-P-192",
"evidence": {
"occurrences": [
{
"location": "etc/ssl/certs/p192.pem",
"additionalContext": "aquasecurity:trivy:LayerDiffID=sha256:8be4950119af3d5904781c15c5ba7e01951daf465a3567077b031a9c58d5275f"
},
{
"location": "etc/ssl/private/p192.key",
"additionalContext": "aquasecurity:trivy:LayerDiffID=sha256:8be4950119af3d5904781c15c5ba7e01951daf465a3567077b031a9c58d5275f"
},
{
"location": "etc/ssl/public/p192.pem",
"additionalContext": "aquasecurity:trivy:LayerDiffID=sha256:8be4950119af3d5904781c15c5ba7e01951daf465a3567077b031a9c58d5275f"
}
]
},
"cryptoProperties": {
"assetType": "algorithm",
"algorithmProperties": {
"primitive": "unknown",
"ellipticCurve": "nist/P-192",
"classicalSecurityLevel": 80,
"nistQuantumSecurityLevel": 0
},
"oid": "1.2.840.10045.2.1"
}
},
{
"bom-ref": "crypto:algorithm:oid:1.2.840.10045.2.1:curve%3DP-256",
"type": "cryptographic-asset",
@@ -1773,6 +1804,10 @@
"ref": "crypto:algorithm:oid:1.2.840.10040.4.3",
"dependsOn": []
},
{
"ref": "crypto:algorithm:oid:1.2.840.10045.2.1:curve%3DP-192",
"dependsOn": []
},
{
"ref": "crypto:algorithm:oid:1.2.840.10045.2.1:curve%3DP-256",
"dependsOn": []
+90
View File
@@ -107,6 +107,66 @@
"DiffID": "sha256:8be4950119af3d5904781c15c5ba7e01951daf465a3567077b031a9c58d5275f"
}
},
{
"AssetID": "b8ababf0115fb157",
"Kind": "algorithm",
"Identity": {
"Method": "oid",
"Value": "1.2.840.10045.2.1",
"Parameters": "curve=P-192"
},
"Name": "EC-P-192",
"Algorithm": {
"Primitive": "unknown",
"ClassicalSecurityLevel": 80,
"NISTQuantumSecurityLevel": 0
},
"FilePath": "etc/ssl/certs/p192.pem",
"Layer": {
"Digest": "sha256:e7d05081011beef8ab3c013bab7b9110aaa546f789473ec203c5a88b56800fc3",
"DiffID": "sha256:8be4950119af3d5904781c15c5ba7e01951daf465a3567077b031a9c58d5275f"
}
},
{
"AssetID": "ba912ff4a9c6b357",
"Kind": "algorithm",
"Identity": {
"Method": "oid",
"Value": "1.2.840.10045.2.1",
"Parameters": "curve=P-192"
},
"Name": "EC-P-192",
"Algorithm": {
"Primitive": "unknown",
"ClassicalSecurityLevel": 80,
"NISTQuantumSecurityLevel": 0
},
"FilePath": "etc/ssl/private/p192.key",
"Layer": {
"Digest": "sha256:e7d05081011beef8ab3c013bab7b9110aaa546f789473ec203c5a88b56800fc3",
"DiffID": "sha256:8be4950119af3d5904781c15c5ba7e01951daf465a3567077b031a9c58d5275f"
}
},
{
"AssetID": "362831e38706a08f",
"Kind": "algorithm",
"Identity": {
"Method": "oid",
"Value": "1.2.840.10045.2.1",
"Parameters": "curve=P-192"
},
"Name": "EC-P-192",
"Algorithm": {
"Primitive": "unknown",
"ClassicalSecurityLevel": 80,
"NISTQuantumSecurityLevel": 0
},
"FilePath": "etc/ssl/public/p192.pem",
"Layer": {
"Digest": "sha256:e7d05081011beef8ab3c013bab7b9110aaa546f789473ec203c5a88b56800fc3",
"DiffID": "sha256:8be4950119af3d5904781c15c5ba7e01951daf465a3567077b031a9c58d5275f"
}
},
{
"AssetID": "10bb9f843a9330d1",
"Kind": "algorithm",
@@ -2409,6 +2469,16 @@
"SignatureAlgorithmOID": "1.2.840.10045.4.1"
}
},
{
"RuleID": "CRYPTO-0003",
"Title": "Weak elliptic curve",
"Severity": "HIGH",
"AssetID": "b8ababf0115fb157",
"Evidence": {
"Curve": "P-192",
"ClassicalSecurityLevel": 80
}
},
{
"RuleID": "CRYPTO-0001",
"Title": "SHA-1 certificate signature",
@@ -2446,6 +2516,16 @@
"NotAfter": "2020-01-01T00:00:00Z"
}
},
{
"RuleID": "CRYPTO-0003",
"Title": "Weak elliptic curve",
"Severity": "HIGH",
"AssetID": "ba912ff4a9c6b357",
"Evidence": {
"Curve": "P-192",
"ClassicalSecurityLevel": 80
}
},
{
"RuleID": "CRYPTO-0002",
"Title": "Small RSA key",
@@ -2455,6 +2535,16 @@
"KeySize": 1024
}
},
{
"RuleID": "CRYPTO-0003",
"Title": "Weak elliptic curve",
"Severity": "HIGH",
"AssetID": "362831e38706a08f",
"Evidence": {
"Curve": "P-192",
"ClassicalSecurityLevel": 80
}
},
{
"RuleID": "CRYPTO-0001",
"Title": "SHA-1 certificate signature",
+3 -3
View File
@@ -203,13 +203,13 @@ func TestEvaluate(t *testing.T) {
},
{
name: "EC strength below 112 bits",
assets: []ftypes.CryptoAsset{algorithm("key.pem", oidECPublicKey, "curve=P-192", new(96))},
assets: []ftypes.CryptoAsset{algorithm("key.pem", oidECPublicKey, "curve=P-192", new(80))},
want: []types.CryptoFinding{
finding("CRYPTO-0003", "Weak elliptic curve", "HIGH",
algorithm("key.pem", oidECPublicKey, "curve=P-192", new(96)),
algorithm("key.pem", oidECPublicKey, "curve=P-192", new(80)),
types.CryptoEvidence{
Curve: "P-192",
ClassicalSecurityLevel: new(96),
ClassicalSecurityLevel: new(80),
}),
},
},
+5 -1
View File
@@ -26,6 +26,10 @@ func objectToAssets(obj object) ([]ftypes.CryptoAsset, error) {
return describeStandaloneKey(obj, ftypes.CryptoKeyTypePrivate)
case objectEncryptedPrivateKey:
return describeEncryptedPrivateKey(obj)
case objectECAlgorithm:
algorithm := crypto.DescribeAlgorithm(oidECPublicKey.String(), 0, 0, obj.curve)
// An algorithm has no format or encoding.
return []ftypes.CryptoAsset{{CryptoAssetInfo: algorithm}}, nil
default:
return nil, xerrors.Errorf("object kind %d has no description", obj.kind)
}
@@ -126,7 +130,7 @@ func describeStandaloneKey(obj object, keyType ftypes.CryptoKeyType) ([]ftypes.C
Format: obj.keyFormat,
Encoding: obj.encoding,
},
// The algorithm is derived and has no encoded form.
// An algorithm has no format or encoding.
{CryptoAssetInfo: keyAlgorithm},
}, nil
}
+9
View File
@@ -14,5 +14,14 @@ type PKCS1AdditionalPrime = pkcs1AdditionalPrime
// PKCS8PrivateKey exports pkcs8PrivateKey for testing.
type PKCS8PrivateKey = pkcs8PrivateKey
// ECPrivateKey exports ecPrivateKey for testing.
type ECPrivateKey = ecPrivateKey
// OIDRSAEncryption exports oidRSAEncryption for testing.
var OIDRSAEncryption = oidRSAEncryption
// OIDECPublicKey exports oidECPublicKey for testing.
var OIDECPublicKey = oidECPublicKey
// OIDNamedCurveP192 exports oidNamedCurveP192 for testing.
var OIDNamedCurveP192 = oidNamedCurveP192
+165 -6
View File
@@ -35,6 +35,9 @@ const (
objectPublicKey
// objectEncryptedPrivateKey identifies an opaque encrypted private key.
objectEncryptedPrivateKey
// objectECAlgorithm identifies a certificate or key on an EC curve crypto/x509 does
// not support. Only its algorithm is read.
objectECAlgorithm
)
// encryptionFormat identifies the container format of an encrypted private key.
@@ -69,6 +72,8 @@ type object struct {
encrypted []byte
// encryption identifies the encrypted private-key container format.
encryption encryptionFormat
// curve names the curve of objectECAlgorithm.
curve string
// encoding identifies the source encoding.
encoding ftypes.CryptoEncoding
// keystore identifies the keystore the object is stored in.
@@ -107,8 +112,48 @@ type pkcs8PrivateKey struct {
PrivateKey []byte
}
// ecPrivateKey is an EC private key in SEC 1, as defined in RFC 5915.
type ecPrivateKey struct {
Version int
PrivateKey []byte
NamedCurveOID asn1.ObjectIdentifier `asn1:"optional,explicit,tag:0"`
PublicKey asn1.BitString `asn1:"optional,explicit,tag:1"`
}
// rawCertificate is an X.509 certificate without the TBSCertificate fields after
// SubjectPublicKeyInfo. Only SubjectPublicKeyInfo is read. The other fields are not checked
// and stand in their places, so that a certificate without a signature fails to parse.
type rawCertificate struct {
TBSCertificate struct {
Version int `asn1:"optional,explicit,default:0,tag:0"`
SerialNumber asn1.RawValue
Signature asn1.RawValue
Issuer asn1.RawValue
Validity asn1.RawValue
Subject asn1.RawValue
SubjectPublicKeyInfo asn1.RawValue
}
SignatureAlgorithm asn1.RawValue
SignatureValue asn1.BitString
}
var oidRSAEncryption = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 1, 1}
// oidECPublicKey identifies an EC public key, defined by RFC 5480 section 2.1.1.
// See https://www.rfc-editor.org/rfc/rfc5480#section-2.1.1
var oidECPublicKey = asn1.ObjectIdentifier{1, 2, 840, 10045, 2, 1}
// oidNamedCurveP192 identifies the curve P-192, which RFC 5480 section 2.1.1.1 names secp192r1.
// See https://www.rfc-editor.org/rfc/rfc5480#section-2.1.1.1
var oidNamedCurveP192 = asn1.ObjectIdentifier{1, 2, 840, 10045, 3, 1, 1}
// p192Size is the length in bytes of a P-192 field element and of a P-192 private key.
const p192Size = 24
// p192Order is the order of the base point of P-192, given in SEC 2 section 2.2.2.
// See https://www.secg.org/sec2-v2.pdf
var p192Order, _ = new(big.Int).SetString("FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22831", 16)
var (
errNotCryptographic = errors.New("not cryptographic")
errUnsupportedCrypto = errors.New("unsupported cryptographic object")
@@ -289,7 +334,7 @@ func parsePEMObject(label string, der []byte) (object, error) {
case "RSA PRIVATE KEY":
return rsaPrivateKeyObject(der, ftypes.CryptoKeyFormatPKCS1)
case "EC PRIVATE KEY":
return privateKeyObject(der, ftypes.CryptoKeyFormatSEC1, stdx509.ParseECPrivateKey)
return ecPrivateKeyObject(der)
case "PUBLIC KEY":
return publicKeyObject(der)
case "ENCRYPTED PRIVATE KEY":
@@ -332,14 +377,122 @@ func privateKeyObject[K any](der []byte, format ftypes.CryptoKeyFormat, parseKey
}
// pkcs8PrivateKeyObject parses a private key in PKCS#8 and projects it to its public key.
// A key on P-192 is described by its algorithm alone.
func pkcs8PrivateKeyObject(der []byte) (object, error) {
var key pkcs8PrivateKey
if _, err := asn1.Unmarshal(der, &key); err == nil && key.Algo.Algorithm.Equal(oidRSAEncryption) {
return rsaPrivateKeyObject(key.PrivateKey, ftypes.CryptoKeyFormatPKCS8)
if _, err := asn1.Unmarshal(der, &key); err == nil {
switch {
case key.Algo.Algorithm.Equal(oidRSAEncryption):
return rsaPrivateKeyObject(key.PrivateKey, ftypes.CryptoKeyFormatPKCS8)
case key.Algo.Algorithm.Equal(oidECPublicKey) && isP192(key.Algo.Parameters.FullBytes):
// The curve is stored in the outer AlgorithmIdentifier, so the key may omit it.
if _, ok := parseP192PrivateKey(key.PrivateKey); !ok {
return object{}, errMalformedCrypto
}
return p192AlgorithmObject(), nil
}
}
return privateKeyObject(der, ftypes.CryptoKeyFormatPKCS8, stdx509.ParsePKCS8PrivateKey)
}
// ecPrivateKeyObject parses an EC private key in SEC 1 and projects it to its public key.
// A key on P-192 is described by its algorithm alone.
func ecPrivateKeyObject(der []byte) (object, error) {
obj, err := privateKeyObject(der, ftypes.CryptoKeyFormatSEC1, stdx509.ParseECPrivateKey)
if !errors.Is(err, errMalformedCrypto) {
return obj, err
}
// Bare SEC 1 must name the curve.
if key, ok := parseP192PrivateKey(der); ok && key.NamedCurveOID != nil {
return p192AlgorithmObject(), nil
}
return object{}, errMalformedCrypto
}
// parseP192PrivateKey parses a SEC 1 EC private key and reports whether it can be a P-192 key.
// The key must name P-192 or no curve, and be a valid scalar of at most 24 bytes. A key
// without a curve is accepted, because PKCS#8 names the curve in the algorithm identifier.
func parseP192PrivateKey(der []byte) (ecPrivateKey, bool) {
var key ecPrivateKey
rest, err := asn1.Unmarshal(der, &key)
if err != nil || len(rest) > 0 || key.Version != 1 {
return ecPrivateKey{}, false
}
if key.NamedCurveOID != nil && !key.NamedCurveOID.Equal(oidNamedCurveP192) {
return ecPrivateKey{}, false
}
if len(key.PrivateKey) == 0 || len(key.PrivateKey) > p192Size {
return ecPrivateKey{}, false
}
if d := new(big.Int).SetBytes(key.PrivateKey); d.Sign() == 0 || d.Cmp(p192Order) >= 0 {
return ecPrivateKey{}, false
}
return key, true
}
// isP192Certificate reports whether der is a certificate with a P-192 key, which crypto/x509
// rejects.
func isP192Certificate(der []byte) bool {
var cert rawCertificate
rest, err := asn1.Unmarshal(der, &cert)
if err != nil || len(rest) > 0 {
return false
}
return isP192PublicKey(cert.TBSCertificate.SubjectPublicKeyInfo.FullBytes)
}
// isP192PublicKey reports whether spki is a P-192 public key in SubjectPublicKeyInfo, which
// crypto/x509 does not decode.
func isP192PublicKey(spki []byte) bool {
var info struct {
Algorithm pkix.AlgorithmIdentifier
SubjectPublicKey asn1.BitString
}
rest, err := asn1.Unmarshal(spki, &info)
if err != nil || len(rest) > 0 {
return false
}
if !info.Algorithm.Algorithm.Equal(oidECPublicKey) || !isP192(info.Algorithm.Parameters.FullBytes) {
return false
}
return isP192Point(info.SubjectPublicKey)
}
// isP192 reports whether the parameters of an EC algorithm identifier name the curve P-192.
func isP192(parameters []byte) bool {
var curve asn1.ObjectIdentifier
rest, err := asn1.Unmarshal(parameters, &curve)
return err == nil && len(rest) == 0 && curve.Equal(oidNamedCurveP192)
}
// isP192Point reports whether point has the length and the leading byte of a P-192 point
// in the uncompressed or the compressed form of SEC 1 section 2.3.3. RFC 5480 allows both
// forms, so the compressed one is accepted although crypto/x509 reads only the uncompressed
// one for the other curves. Whether the point lies on the curve is not checked.
// See https://www.secg.org/sec1-v2.pdf
func isP192Point(point asn1.BitString) bool {
b := point.RightAlign()
if point.BitLength%8 != 0 || len(b) == 0 {
return false
}
switch b[0] {
case 4:
return len(b) == 1+2*p192Size
case 2, 3:
return len(b) == 1+p192Size
}
return false
}
// p192AlgorithmObject is the object of a certificate or key on P-192, which is described by
// its algorithm alone.
func p192AlgorithmObject() object {
return object{
kind: objectECAlgorithm,
curve: "P-192",
}
}
// rsaPrivateKeyObject reads an RSA private key in PKCS#1 and projects it to its public key.
// It checks the structure of the key but not its math, because crypto/x509 validates a key
// with arithmetic whose cost grows with the size of its values, and a crafted key picks
@@ -390,8 +543,8 @@ func parseDERObject(der []byte) (object, error) {
return obj, err
}
if privateKey, err := stdx509.ParseECPrivateKey(der); err == nil {
return privateKeyToObject(privateKey, ftypes.CryptoKeyFormatSEC1)
if obj, err := ecPrivateKeyObject(der); !errors.Is(err, errMalformedCrypto) {
return obj, err
}
if obj, err := publicKeyObject(der); err == nil {
@@ -425,6 +578,9 @@ func parseDERObject(der []byte) (object, error) {
func certificateObject(der []byte) (object, error) {
parsed, err := stdx509.ParseCertificate(der)
if err != nil {
if isP192Certificate(der) {
return p192AlgorithmObject(), nil
}
return object{}, errMalformedCrypto
}
@@ -475,7 +631,7 @@ func privateKeyToObject(privateKey any, format ftypes.CryptoKeyFormat) (object,
var decodedKeyAlgorithms = []asn1.ObjectIdentifier{
oidRSAEncryption,
{1, 2, 840, 10040, 4, 1}, // DSA
{1, 2, 840, 10045, 2, 1}, // EC
oidECPublicKey, // EC
{1, 3, 101, 110}, // X25519
{1, 3, 101, 112}, // Ed25519
{2, 16, 840, 1, 101, 3, 4, 3, 17}, // ML-DSA-44
@@ -495,6 +651,9 @@ func publicKeyObject(der []byte) (object, error) {
keyFormat: ftypes.CryptoKeyFormatPKIX,
}, nil
}
if isP192PublicKey(der) {
return p192AlgorithmObject(), nil
}
oid, err := crypto.ParseSubjectPublicKeyInfo(der)
if err != nil {
+253 -3
View File
@@ -95,6 +95,46 @@ wPyCQoe49bPiUWB8/x06gL27PLIqu2ZglEe/VC6SRV+0yHcrlalqxtq7s67IbH2k
-----END CERTIFICATE-----
`
// The P-192 fixtures are a self-signed certificate and its key in every form the parser
// reads. crypto/x509 does not support P-192, so they come from OpenSSL:
//
// docker run --rm alpine:3.22 sh -c '
// apk add -q openssl &&
// openssl req -x509 -newkey ec -pkeyopt ec_paramgen_curve:P-192 -nodes -keyout key.pem -subj /CN=p192 -days 36500 &&
// cat key.pem &&
// openssl ec -in key.pem &&
// openssl pkey -in key.pem -pubout'
const (
p192CertificatePEM = `-----BEGIN CERTIFICATE-----
MIIBVTCCAQugAwIBAgIUYOyOWGD1O9UuPc8l8IFxG8v67NEwCgYIKoZIzj0EAwIw
DzENMAsGA1UEAwwEcDE5MjAgFw0yNjEwMTYxMjQ5MTNaGA8yMTI2MDkyMjEyNDkx
M1owDzENMAsGA1UEAwwEcDE5MjBJMBMGByqGSM49AgEGCCqGSM49AwEBAzIABBYq
XHGsm8w7kRzKfRouJTjxdzufi3eLlJChrD0hWN8VQkeSZqmnFSTPxLLDWbnGN6NT
MFEwHQYDVR0OBBYEFPjzRjRj0sLkKoupN9gJiDZ1CLFbMB8GA1UdIwQYMBaAFPjz
RjRj0sLkKoupN9gJiDZ1CLFbMA8GA1UdEwEB/wQFMAMBAf8wCgYIKoZIzj0EAwID
OAAwNQIZAO/H03Gm8vN/34wxNd0vju0uSM+Shr7SzgIYIVZ+alM5dclVBAXpBhJX
GKh8LkK0kF4z
-----END CERTIFICATE-----
`
p192PKCS8PrivateKeyPEM = `-----BEGIN PRIVATE KEY-----
MG8CAQAwEwYHKoZIzj0CAQYIKoZIzj0DAQEEVTBTAgEBBBgQW7hyOKagpDHl65RX
R+R18SRA2nYcse6hNAMyAAQWKlxxrJvMO5Ecyn0aLiU48Xc7n4t3i5SQoaw9IVjf
FUJHkmappxUkz8Syw1m5xjc=
-----END PRIVATE KEY-----
`
p192SEC1PrivateKeyPEM = `-----BEGIN EC PRIVATE KEY-----
MF8CAQEEGBBbuHI4pqCkMeXrlFdH5HXxJEDadhyx7qAKBggqhkjOPQMBAaE0AzIA
BBYqXHGsm8w7kRzKfRouJTjxdzufi3eLlJChrD0hWN8VQkeSZqmnFSTPxLLDWbnG
Nw==
-----END EC PRIVATE KEY-----
`
p192PublicKeyPEM = `-----BEGIN PUBLIC KEY-----
MEkwEwYHKoZIzj0CAQYIKoZIzj0DAQEDMgAEFipccaybzDuRHMp9Gi4lOPF3O5+L
d4uUkKGsPSFY3xVCR5JmqacVJM/EssNZucY3
-----END PUBLIC KEY-----
`
)
// rsaPrivateKeyDER encodes a structurally valid 2048-bit RSA key in PKCS#1 after applying
// mutate to it. Its values are not consistent with each other.
func rsaPrivateKeyDER(t *testing.T, mutate func(*cryptox509.PKCS1PrivateKey)) []byte {
@@ -661,6 +701,25 @@ func TestParseAssets(t *testing.T) {
Primitive: ftypes.CryptoPrimitiveUnknown,
},
}
p192Algorithm := ftypes.CryptoAssetInfo{
Kind: ftypes.CryptoKindAlgorithm,
Identity: ftypes.CryptoIdentity{
Method: ftypes.CryptoMethodOID,
Value: "1.2.840.10045.2.1",
Parameters: "curve=P-192",
},
Name: "EC-P-192",
Algorithm: &ftypes.CryptoAlgorithm{
Primitive: ftypes.CryptoPrimitiveUnknown,
},
}
var p192Certificate struct {
TBSCertificate asn1.RawValue
}
_, err := asn1.Unmarshal(decodePEM(t, p192CertificatePEM), &p192Certificate)
require.NoError(t, err)
p192UnsignedCertificate, err := asn1.Marshal(p192Certificate)
require.NoError(t, err)
ecSignatureAlgorithm := ftypes.CryptoAssetInfo{
Kind: ftypes.CryptoKindAlgorithm,
Identity: ftypes.CryptoIdentity{
@@ -1124,6 +1183,137 @@ func TestParseAssets(t *testing.T) {
at(x25519Algorithm),
},
},
{
// crypto/x509 does not support P-192, so only the algorithm of the key is described.
name: "P-192 certificate PEM",
input: []byte(p192CertificatePEM),
want: []ftypes.CryptoAsset{at(p192Algorithm)},
},
{
name: "P-192 certificate DER",
input: decodePEM(t, p192CertificatePEM),
want: []ftypes.CryptoAsset{at(p192Algorithm)},
},
{
name: "P-192 public key",
input: []byte(p192PublicKeyPEM),
want: []ftypes.CryptoAsset{at(p192Algorithm)},
},
{
name: "P-192 PKCS#8 private key",
input: []byte(p192PKCS8PrivateKeyPEM),
want: []ftypes.CryptoAsset{at(p192Algorithm)},
},
{
name: "P-192 SEC 1 private key PEM",
input: []byte(p192SEC1PrivateKeyPEM),
want: []ftypes.CryptoAsset{at(p192Algorithm)},
},
{
name: "P-192 SEC 1 private key DER",
input: decodePEM(t, p192SEC1PrivateKeyPEM),
want: []ftypes.CryptoAsset{at(p192Algorithm)},
},
{
// The algorithm is reported once per file.
name: "P-192 certificate and keys",
input: []byte(p192CertificatePEM + p192PublicKeyPEM + p192PKCS8PrivateKeyPEM + p192SEC1PrivateKeyPEM),
want: []ftypes.CryptoAsset{at(p192Algorithm)},
},
{
name: "P-192 public key DER",
input: decodePEM(t, p192PublicKeyPEM),
want: []ftypes.CryptoAsset{at(p192Algorithm)},
},
{
name: "P-192 PKCS#8 private key DER",
input: decodePEM(t, p192PKCS8PrivateKeyPEM),
want: []ftypes.CryptoAsset{at(p192Algorithm)},
},
{
name: "P-192 public key with a compressed point",
input: pem.EncodeToMemory(&pem.Block{
Type: "PUBLIC KEY",
Bytes: p192PublicKeyDER(t, append([]byte{2}, make([]byte, 24)...)),
}),
want: []ftypes.CryptoAsset{at(p192Algorithm)},
},
{
name: "P-192 public key with a short point",
input: pem.EncodeToMemory(&pem.Block{
Type: "PUBLIC KEY",
Bytes: p192PublicKeyDER(t, []byte{4, 1, 2}),
}),
},
{
name: "P-192 certificate without a signature",
input: pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: p192UnsignedCertificate}),
},
{
name: "P-192 SEC 1 private key without a curve",
input: pem.EncodeToMemory(&pem.Block{
Type: "EC PRIVATE KEY",
Bytes: p192SEC1PrivateKeyDER(t, func(key *cryptox509.ECPrivateKey) {
key.NamedCurveOID = nil
}),
}),
},
{
name: "P-192 SEC 1 private key longer than 24 bytes",
input: pem.EncodeToMemory(&pem.Block{
Type: "EC PRIVATE KEY",
Bytes: p192SEC1PrivateKeyDER(t, func(key *cryptox509.ECPrivateKey) {
key.PrivateKey = make([]byte, 25)
}),
}),
},
{
name: "P-192 SEC 1 private key of zero",
input: pem.EncodeToMemory(&pem.Block{
Type: "EC PRIVATE KEY",
Bytes: p192SEC1PrivateKeyDER(t, func(key *cryptox509.ECPrivateKey) {
key.PrivateKey = make([]byte, 24)
}),
}),
},
{
name: "P-192 SEC 1 private key above the order of the curve",
input: pem.EncodeToMemory(&pem.Block{
Type: "EC PRIVATE KEY",
Bytes: p192SEC1PrivateKeyDER(t, func(key *cryptox509.ECPrivateKey) {
key.PrivateKey = bytes.Repeat([]byte{0xff}, 24)
}),
}),
},
{
name: "P-192 SEC 1 private key of version 2",
input: pem.EncodeToMemory(&pem.Block{
Type: "EC PRIVATE KEY",
Bytes: p192SEC1PrivateKeyDER(t, func(key *cryptox509.ECPrivateKey) {
key.Version = 2
}),
}),
},
{
// PKCS#8 names the curve in its algorithm, so the inner key may omit it.
name: "P-192 PKCS#8 private key without a curve in SEC 1",
input: pem.EncodeToMemory(&pem.Block{
Type: "PRIVATE KEY",
Bytes: p192PKCS8PrivateKeyDER(t, p192SEC1PrivateKeyDER(t, func(key *cryptox509.ECPrivateKey) {
key.NamedCurveOID = nil
})),
}),
want: []ftypes.CryptoAsset{at(p192Algorithm)},
},
{
name: "P-192 PKCS#8 private key with P-256 in SEC 1",
input: pem.EncodeToMemory(&pem.Block{
Type: "PRIVATE KEY",
Bytes: p192PKCS8PrivateKeyDER(t, p192SEC1PrivateKeyDER(t, func(key *cryptox509.ECPrivateKey) {
key.NamedCurveOID = asn1.ObjectIdentifier{1, 2, 840, 10045, 3, 1, 7}
})),
}),
},
{
// The key of a certificate and the same key on its own share an identity.
name: "Ed448 public key",
@@ -1757,13 +1947,73 @@ func newCertificate(t *testing.T, template *stdx509.Certificate, key stdcrypto.S
func parseCertificatePEM(t *testing.T, content string) *stdx509.Certificate {
t.Helper()
block, _ := pem.Decode([]byte(content))
require.NotNil(t, block)
certificate, err := stdx509.ParseCertificate(block.Bytes)
certificate, err := stdx509.ParseCertificate(decodePEM(t, content))
require.NoError(t, err)
return certificate
}
// decodePEM returns the DER of the first PEM block in content.
func decodePEM(t *testing.T, content string) []byte {
t.Helper()
block, _ := pem.Decode([]byte(content))
require.NotNil(t, block)
return block.Bytes
}
// p192PublicKeyDER encodes a SubjectPublicKeyInfo on P-192 with point as its public key.
func p192PublicKeyDER(t *testing.T, point []byte) []byte {
t.Helper()
der, err := asn1.Marshal(struct {
Algorithm pkix.AlgorithmIdentifier
SubjectPublicKey asn1.BitString
}{
Algorithm: p192AlgorithmIdentifier(t),
SubjectPublicKey: asn1.BitString{Bytes: point, BitLength: len(point) * 8},
})
require.NoError(t, err)
return der
}
// p192SEC1PrivateKeyDER encodes the P-192 key of the fixtures in SEC 1 after applying
// mutate to it.
func p192SEC1PrivateKeyDER(t *testing.T, mutate func(*cryptox509.ECPrivateKey)) []byte {
t.Helper()
var key cryptox509.ECPrivateKey
_, err := asn1.Unmarshal(decodePEM(t, p192SEC1PrivateKeyPEM), &key)
require.NoError(t, err)
mutate(&key)
der, err := asn1.Marshal(key)
require.NoError(t, err)
return der
}
// p192PKCS8PrivateKeyDER encodes a PKCS#8 private key on P-192 that holds sec1.
func p192PKCS8PrivateKeyDER(t *testing.T, sec1 []byte) []byte {
t.Helper()
der, err := asn1.Marshal(cryptox509.PKCS8PrivateKey{
Algo: p192AlgorithmIdentifier(t),
PrivateKey: sec1,
})
require.NoError(t, err)
return der
}
// p192AlgorithmIdentifier returns the algorithm identifier of an EC key on P-192.
func p192AlgorithmIdentifier(t *testing.T) pkix.AlgorithmIdentifier {
t.Helper()
parameters, err := asn1.Marshal(cryptox509.OIDNamedCurveP192)
require.NoError(t, err)
return pkix.AlgorithmIdentifier{
Algorithm: cryptox509.OIDECPublicKey,
Parameters: asn1.RawValue{FullBytes: parameters},
}
}
// marshalSPKI encodes a SubjectPublicKeyInfo with the given algorithm OID and public key bytes.
func marshalSPKI(t *testing.T, oid asn1.ObjectIdentifier, publicKey []byte) []byte {
t.Helper()
+2
View File
@@ -45,9 +45,11 @@ var (
}
// EC estimates are the ECC strength ranges in NIST SP 800-57 Part 1 Rev. 5, Table 2.
// P-192 takes the nominal maximum of 80 from the at-most-80 row.
ecStrengthBasis = strengthBasis{
nistCategory: new(0),
classicalStrengths: map[string]int{
"curve=P-192": 80,
"curve=P-224": 112,
"curve=P-256": 128,
"curve=P-384": 192,
+1
View File
@@ -39,6 +39,7 @@ func TestAssessStrength(t *testing.T) {
oid: "1.2.840.113549.1.1.1",
wantCategory: new(0),
},
{name: "EC-P-192", oid: "1.2.840.10045.2.1", curve: "P-192", wantClassical: new(80), wantCategory: new(0)},
{name: "EC-P-224", oid: "1.2.840.10045.2.1", curve: "P-224", wantClassical: new(112), wantCategory: new(0)},
{name: "EC-P-256", oid: "1.2.840.10045.2.1", curve: "P-256", wantClassical: new(128), wantCategory: new(0)},
{name: "EC-P-384", oid: "1.2.840.10045.2.1", curve: "P-384", wantClassical: new(192), wantCategory: new(0)},
+1 -1
View File
@@ -23,7 +23,7 @@ func init() {
}
const (
version = 4
version = 5
// maxFileSize bounds what is read into memory. The eligible extensions are shared with
// unrelated formats, such as Keynote presentations, which grow far beyond any file of
+1 -1
View File
@@ -1850,7 +1850,7 @@ func TestConvertCryptoFindings(t *testing.T) {
AssetID: "5d90a2c7e13f6b48",
Evidence: types.CryptoEvidence{
Curve: "P-192",
ClassicalSecurityLevel: new(96),
ClassicalSecurityLevel: new(80),
},
},
},
+2 -1
View File
@@ -13,10 +13,11 @@ import (
xslices "github.com/aquasecurity/trivy/pkg/x/slices"
)
// ellipticCurves maps the curve names of crypto/elliptic to the CycloneDX elliptic curve
// ellipticCurves maps the NIST curve names to the CycloneDX elliptic curve
// enumeration, which qualifies a curve with the body that standardized it. A curve absent
// from the map is left out, because the enumeration accepts nothing else.
var ellipticCurves = map[string]string{
"P-192": "nist/P-192",
"P-224": "nist/P-224",
"P-256": "nist/P-256",
"P-384": "nist/P-384",