mirror of
https://github.com/caddyserver/caddy.git
synced 2026-11-21 23:39:43 +00:00
caddyhttp: expose negotiated TLS curve as placeholder and log field
Add {http.request.tls.curve} with the name of the negotiated key exchange and a numeric curve field to the tls object of access logs. Both are empty or omitted when CurveID is 0.
Closes #8065
This commit is contained in:
parent
7591cff1fb
commit
a187deaec3
5 changed files with 68 additions and 1 deletions
|
|
@ -96,6 +96,7 @@ func init() {
|
|||
// `{http.request.tls.proto_mutual}` | The negotiated next protocol was advertised by the server
|
||||
// `{http.request.tls.server_name}` | The server name requested by the client, if any
|
||||
// `{http.request.tls.ech}` | Whether ECH was offered by the client and accepted by the server
|
||||
// `{http.request.tls.curve}` | The negotiated key exchange mechanism (curve or group), if any
|
||||
// `{http.request.tls.client.fingerprint}` | The SHA256 checksum of the client certificate
|
||||
// `{http.request.tls.client.public_key}` | The public key of the client certificate.
|
||||
// `{http.request.tls.client.public_key_sha256}` | The SHA256 checksum of the client's public key.
|
||||
|
|
|
|||
|
|
@ -78,6 +78,9 @@ func (t LoggableTLSConnState) MarshalLogObject(enc zapcore.ObjectEncoder) error
|
|||
enc.AddString("proto", t.NegotiatedProtocol)
|
||||
enc.AddString("server_name", t.ServerName)
|
||||
enc.AddBool("ech", t.ECHAccepted)
|
||||
if t.CurveID != 0 {
|
||||
enc.AddUint16("curve", uint16(t.CurveID))
|
||||
}
|
||||
if len(t.PeerCertificates) > 0 {
|
||||
enc.AddString("client_common_name", t.PeerCertificates[0].Subject.CommonName)
|
||||
enc.AddString("client_serial", t.PeerCertificates[0].SerialNumber.String())
|
||||
|
|
|
|||
41
modules/caddyhttp/marshalers_test.go
Normal file
41
modules/caddyhttp/marshalers_test.go
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
// Copyright 2015 Matthew Holt and The Caddy Authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package caddyhttp
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"testing"
|
||||
|
||||
"go.uber.org/zap/zapcore"
|
||||
)
|
||||
|
||||
func TestLoggableTLSConnStateCurve(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
curve tls.CurveID
|
||||
expected any
|
||||
}{
|
||||
{curve: tls.X25519MLKEM768, expected: uint16(4588)},
|
||||
{curve: tls.X25519, expected: uint16(29)},
|
||||
{curve: 0, expected: nil},
|
||||
} {
|
||||
enc := zapcore.NewMapObjectEncoder()
|
||||
if err := LoggableTLSConnState(tls.ConnectionState{CurveID: tc.curve}).MarshalLogObject(enc); err != nil {
|
||||
t.Fatalf("curve %d: unexpected error: %v", tc.curve, err)
|
||||
}
|
||||
if actual := enc.Fields["curve"]; actual != tc.expected {
|
||||
t.Errorf("curve %d: expected %v (%T), got %v (%T)", tc.curve, tc.expected, tc.expected, actual, actual)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -622,6 +622,12 @@ func getTLSReplacement(state *tls.ConnectionState, key string) (any, bool) {
|
|||
return state.ServerName, true
|
||||
case "ech":
|
||||
return state.ECHAccepted, true
|
||||
case "curve":
|
||||
// CurveID is 0 when no curve-based key exchange was recorded
|
||||
if state.CurveID == 0 {
|
||||
return "", true
|
||||
}
|
||||
return state.CurveID.String(), true
|
||||
}
|
||||
return nil, false
|
||||
}
|
||||
|
|
|
|||
|
|
@ -47,7 +47,7 @@ func TestTLSPlaceholdersWithoutTLS(t *testing.T) {
|
|||
req = req.WithContext(context.WithValue(req.Context(), VarsCtxKey, map[string]any{}))
|
||||
repl := NewTestReplacer(req)
|
||||
for _, field := range []string{
|
||||
"version", "cipher_suite", "resumed", "proto", "proto_mutual", "server_name", "ech",
|
||||
"version", "cipher_suite", "resumed", "proto", "proto_mutual", "server_name", "ech", "curve",
|
||||
"client.fingerprint", "client.public_key", "client.public_key_sha256",
|
||||
"client.issuer", "client.serial", "client.subject", "client.certificate_pem", "client.certificate_der_base64",
|
||||
"client.san.dns_names", "client.san.emails", "client.san.ips", "client.san.uris",
|
||||
|
|
@ -66,6 +66,17 @@ func TestTLSPlaceholdersWithoutTLS(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestTLSCurvePlaceholderWithoutKeyExchange(t *testing.T) {
|
||||
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
||||
req.TLS = &tls.ConnectionState{Version: tls.VersionTLS12}
|
||||
req = req.WithContext(context.WithValue(req.Context(), VarsCtxKey, map[string]any{}))
|
||||
repl := NewTestReplacer(req)
|
||||
value, known := repl.Get("http.request.tls.curve")
|
||||
if !known || caddy.ToString(value) != "" {
|
||||
t.Fatalf("Get(%q) = %v, %v; want empty, known", "http.request.tls.curve", value, known)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnknownTLSPlaceholdersRemainLiteral(t *testing.T) {
|
||||
for _, state := range []*tls.ConnectionState{nil, {ServerName: "example.com"}} {
|
||||
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
||||
|
|
@ -123,6 +134,7 @@ eqp31wM9il1n+guTNyxJd+FzVAH+hCZE5K+tCgVDdVFUlDEHHbS/wqb2PSIoouLV
|
|||
HandshakeComplete: true,
|
||||
ServerName: "example.com",
|
||||
CipherSuite: tls.TLS_AES_256_GCM_SHA384,
|
||||
CurveID: tls.X25519MLKEM768,
|
||||
PeerCertificates: []*x509.Certificate{cert},
|
||||
NegotiatedProtocol: "h2",
|
||||
NegotiatedProtocolIsMutual: true,
|
||||
|
|
@ -252,6 +264,10 @@ eqp31wM9il1n+guTNyxJd+FzVAH+hCZE5K+tCgVDdVFUlDEHHbS/wqb2PSIoouLV
|
|||
get: "http.request.tls.cipher_suite",
|
||||
expect: "TLS_AES_256_GCM_SHA384",
|
||||
},
|
||||
{
|
||||
get: "http.request.tls.curve",
|
||||
expect: "X25519MLKEM768",
|
||||
},
|
||||
{
|
||||
get: "http.request.tls.proto",
|
||||
expect: "h2",
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue