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
4 changes: 2 additions & 2 deletions docs/reference/tls.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ Default: defined by Go version
The elliptic curves that will be used in an ECDHE handshake, in preference
order.

Valid values: `p256`, `p384`, `p521`, `X25519`.
Valid values: `p256`, `p384`, `p521`, `X25519`, `X25519MLKEM768`.

## Client

Expand Down Expand Up @@ -151,7 +151,7 @@ Default: defined by Go version
The elliptic curves that will be used in an ECDHE handshake, in preference
order.

Valid values: `p256`, `p384`, `p521`, `X25519`.
Valid values: `p256`, `p384`, `p521`, `X25519`, `X25519MLKEM768`.

---

Expand Down
9 changes: 5 additions & 4 deletions framework/config/tls/general.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,11 @@
}

var strCurvesMap = map[string]tls.CurveID{
"p256": tls.CurveP256,
"p384": tls.CurveP384,
"p521": tls.CurveP521,
"X25519": tls.X25519,
"p256": tls.CurveP256,

Check failure on line 63 in framework/config/tls/general.go

View workflow job for this annotation

GitHub Actions / Lint

File is not properly formatted (goimports)
"p384": tls.CurveP384,
"p521": tls.CurveP521,
"X25519": tls.X25519,
"X25519MLKEM768": tls.X25519MLKEM768,
}

// TLSversionsDirective parses directive with arguments that specify
Expand Down
109 changes: 109 additions & 0 deletions framework/config/tls/general_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
Maddy Mail Server - Composable all-in-one email server.
Copyright © 2019-2020 Max Mazurov <fox.cpp@disroot.org>, Maddy Mail Server contributors

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package tls

import (
"crypto/tls"
"reflect"
"strings"
"testing"

"github.com/foxcpp/maddy/framework/config"
)

func parseCurves(t *testing.T, args ...string) []tls.CurveID {
t.Helper()

v, err := TLSCurvesDirective(nil, config.Node{Name: "curves", Args: args})
if err != nil {
t.Fatalf("TLSCurvesDirective(%v) failed: %v", args, err)
}
curves, ok := v.([]tls.CurveID)
if !ok {
t.Fatalf("TLSCurvesDirective(%v) returned %T, want []tls.CurveID", args, v)
}
return curves
}

func TestTLSCurvesDirectiveNames(t *testing.T) {
cases := []struct {
name string
want tls.CurveID
}{
{"p256", tls.CurveP256},
{"p384", tls.CurveP384},
{"p521", tls.CurveP521},
{"X25519", tls.X25519},
{"X25519MLKEM768", tls.X25519MLKEM768},
}
for _, tc := range cases {
got := parseCurves(t, tc.name)
if !reflect.DeepEqual(got, []tls.CurveID{tc.want}) {
t.Errorf("curves %s: got %v, want [%v]", tc.name, got, tc.want)
}
}
}

func TestTLSCurvesDirectiveMLKEMHybrid(t *testing.T) {
// A user who pins curves explicitly replaces whatever CurvePreferences
// crypto/tls would have used by default. Where those defaults include
// the X25519MLKEM768 hybrid group — as the standard library defaults
// do on Go 1.24 and later, subject to GODEBUG settings — a pinned list
// without the hybrid silently drops it. The directive must offer a way
// to keep the hybrid in the pinned list, in the specified order.
got := parseCurves(t, "X25519MLKEM768", "X25519")
want := []tls.CurveID{tls.X25519MLKEM768, tls.X25519}
if !reflect.DeepEqual(got, want) {
t.Errorf("got %v, want %v", got, want)
}
}

func TestTLSCurvesDirectiveDuplicates(t *testing.T) {
// Duplicate names are kept as specified: the directive does not
// deduplicate, so the resulting list mirrors the configuration.
got := parseCurves(t, "X25519", "X25519")
want := []tls.CurveID{tls.X25519, tls.X25519}
if !reflect.DeepEqual(got, want) {
t.Errorf("got %v, want %v", got, want)
}
}

func TestTLSCurvesDirectiveUnknown(t *testing.T) {
for _, args := range [][]string{
{"p999"},
{"X25519", "bogus"},
{"x25519mlkem768"}, // names are matched exactly, as with X25519
{""}, // an empty name is not a curve either
} {
_, err := TLSCurvesDirective(nil, config.Node{Name: "curves", Args: args})
if err == nil {
t.Errorf("curves %v: expected error, got none", args)
continue
}
if !strings.Contains(err.Error(), "unknown curve") {
t.Errorf("curves %v: error %q does not mention %q", args, err, "unknown curve")
}
}
}

func TestTLSCurvesDirectiveNoArgs(t *testing.T) {
if _, err := TLSCurvesDirective(nil, config.Node{Name: "curves"}); err == nil {
t.Error("expected error for curves directive without arguments, got none")
}
}
Loading