Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 54 additions & 1 deletion scanner/plugins/opensslconf/opensslconf.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func NewOpenSSLConfPlugin() (plugins.Plugin, error) { return &Plugin{}, nil }
func (*Plugin) GetName() string { return "OpenSSL Config Plugin" }

func (*Plugin) GetExplanation() string {
return "Scans for OpenSSL configuration files (openssl.cnf) and adds selected TLS settings (Min/MaxProtocol, CipherString, Options, CAfile/CApath, default_md) to the CBOM as component properties."
return "Scans for OpenSSL configuration files (openssl.cnf) and adds selected TLS settings (Min/MaxProtocol, CipherString, Groups/Curves, Options, CAfile/CApath, default_md) to the CBOM as component properties. Groups/Curves are matched case-insensitively (Groups preferred when both are set in the same section); a match in an earlier preferred section wins over a later section even when the later section's directive name would otherwise be preferred."
}

func (*Plugin) GetType() plugins.PluginType { return plugins.PluginTypeAppend }
Expand Down Expand Up @@ -343,9 +343,62 @@ func extractRelevantProperties(cfg map[string]map[string]string) []cdx.Property
properties = append(properties, cdx.Property{Name: "theia:openssl:" + k, Value: v})
}
}
// Groups (TLS 1.3 key-exchange groups, e.g. post-quantum/hybrid groups such
// as X25519MLKEM768) is read from the "Groups" directive, or from its
// legacy alias "Curves" -- OpenSSL itself matches directive names
// case-insensitively, so both names are matched case-insensitively here
// too, with "Groups" preferred when a config sets both in the same
// section. Section priority still takes precedence over name priority:
// a match in an earlier preferred section wins even when a later section
// has the otherwise-preferred name (see
// Test_parseOpenSSLConf_SectionPriorityOverNamePriority).
if v, ok := getFirstKeyCI(cfg, preferredSections, "Groups", "Curves"); ok && v != "" {
properties = append(properties, cdx.Property{Name: "theia:openssl:Groups", Value: v})
}
return properties
}

// getFirstKeyCI is like getFirstKey but matches each of names case-insensitively,
// trying them in priority order (first name wins if multiple are present in the
// same section). Used for directives with a case-insensitive alias (Groups/Curves).
func getFirstKeyCI(cfg map[string]map[string]string, sections []string, names ...string) (string, bool) {
for _, s := range sections {
if sec, ok := cfg[s]; ok {
if v, ok2 := lookupKeyCI(sec, names); ok2 {
return v, true
}
}
}
secNames := make([]string, 0, len(cfg))
for s := range cfg {
secNames = append(secNames, s)
}
sort.Strings(secNames)
for _, s := range secNames {
if v, ok := lookupKeyCI(cfg[s], names); ok {
return v, true
}
}
return "", false
}

func lookupKeyCI(section map[string]string, names []string) (string, bool) {
for _, name := range names {
lname := strings.ToLower(name)
var matched []string
for k := range section {
if strings.ToLower(k) == lname {
matched = append(matched, k)
}
}
if len(matched) > 0 {
sort.Strings(matched) // deterministic if the same name appears with different casing
return section[matched[0]], true
}
}
return "", false
}

func getFirstKey(cfg map[string]map[string]string, sections []string, key string) (string, bool) {
for _, s := range sections {
if sec, ok := cfg[s]; ok {
Expand Down
67 changes: 67 additions & 0 deletions scanner/plugins/opensslconf/opensslconf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func Test_parseOpenSSLConf_and_extractRelevantProperties(t *testing.T) {
MinProtocol = TLSv1.2
MaxProtocol = TLSv1.3
CipherString = DEFAULT@SECLEVEL=2
Groups = X25519MLKEM768:X25519:P-256
Options = ServerPreference,PrioritizeChaCha

[ca_default]
Expand All @@ -54,12 +55,77 @@ CApath=/etc/ssl/certs
assert.Equal(t, "TLSv1.2", m["theia:openssl:MinProtocol"])
assert.Equal(t, "TLSv1.3", m["theia:openssl:MaxProtocol"])
assert.Equal(t, "DEFAULT@SECLEVEL=2", m["theia:openssl:CipherString"])
assert.Equal(t, "X25519MLKEM768:X25519:P-256", m["theia:openssl:Groups"])
assert.Equal(t, "ServerPreference,PrioritizeChaCha", m["theia:openssl:Options"])
assert.Equal(t, "/etc/ssl/certs/ca-bundle.crt", m["theia:openssl:CAfile"])
assert.Equal(t, "/etc/ssl/certs", m["theia:openssl:CApath"])
assert.Equal(t, "sha256", m["theia:openssl:default_md"])
}

// Test_parseOpenSSLConf_CurvesAliasCaseInsensitive covers cbomkit-theia#212:
// "Curves" is the legacy alias for "Groups", and OpenSSL matches directive
// names case-insensitively, so a config using either spelling/casing must
// still surface the post-quantum/hybrid group on the CBOM.
func Test_parseOpenSSLConf_CurvesAliasCaseInsensitive(t *testing.T) {
content := `
[system_default_sect]
curves = X25519MLKEM768
`
cfg, err := parseOpenSSLConf(strings.NewReader(content))
assert.NoError(t, err)
props := extractRelevantProperties(cfg)

m := map[string]string{}
for _, p := range props {
m[p.Name] = p.Value
}
assert.Equal(t, "X25519MLKEM768", m["theia:openssl:Groups"])
}

// Test_parseOpenSSLConf_GroupsPreferredOverCurves: when both directives are
// present (e.g. a config written for both OpenSSL 1.1 and 3.x readers),
// "Groups" wins deterministically.
func Test_parseOpenSSLConf_GroupsPreferredOverCurves(t *testing.T) {
content := `
[system_default_sect]
Groups = X25519MLKEM768
Curves = P-256
`
cfg, err := parseOpenSSLConf(strings.NewReader(content))
assert.NoError(t, err)
props := extractRelevantProperties(cfg)

m := map[string]string{}
for _, p := range props {
m[p.Name] = p.Value
}
assert.Equal(t, "X25519MLKEM768", m["theia:openssl:Groups"])
}

// Test_parseOpenSSLConf_SectionPriorityOverNamePriority: section order wins
// over name order across sections -- "Curves" in the earlier preferred
// section ("system_default_sect") is used even though "Groups" (the
// otherwise-preferred name) appears only in a later preferred section
// ("req").
func Test_parseOpenSSLConf_SectionPriorityOverNamePriority(t *testing.T) {
content := `
[system_default_sect]
Curves = X25519MLKEM768

[req]
Groups = P-256
`
cfg, err := parseOpenSSLConf(strings.NewReader(content))
assert.NoError(t, err)
props := extractRelevantProperties(cfg)

m := map[string]string{}
for _, p := range props {
m[p.Name] = p.Value
}
assert.Equal(t, "X25519MLKEM768", m["theia:openssl:Groups"])
}

func Test_UpdateBOM_adds_component(t *testing.T) {
fs := filesystem.NewPlainFilesystem("../../../testdata/openssl/dir")
bom := cdx.NewBOM()
Expand Down Expand Up @@ -91,6 +157,7 @@ func Test_UpdateBOM_adds_component(t *testing.T) {
assert.Equal(t, "TLSv1.2", props["theia:openssl:MinProtocol"])
assert.Equal(t, "TLSv1.3", props["theia:openssl:MaxProtocol"])
assert.Equal(t, "ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES256-GCM-SHA384", props["theia:openssl:CipherString"])
assert.Equal(t, "X25519MLKEM768:X25519:P-256", props["theia:openssl:Groups"])
assert.Equal(t, "ServerPreference,PrioritizeChaCha", props["theia:openssl:Options"])
assert.Equal(t, "/etc/ssl/certs/ca-bundle.crt", props["theia:openssl:CAfile"])
assert.Equal(t, "/etc/ssl/certs", props["theia:openssl:CApath"])
Expand Down
1 change: 1 addition & 0 deletions testdata/openssl/dir/openssl.cnf
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ MinProtocol = TLSv1.2
MaxProtocol = TLSv1.3
CipherString = ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES256-GCM-SHA384
Ciphersuites = TLS_AES_128_GCM_SHA256:TLS_CHACHA20_POLY1305_SHA256
Groups = X25519MLKEM768:X25519:P-256
Options = ServerPreference,PrioritizeChaCha

# Default CA section
Expand Down
Loading