Skip to content

Add TLS certificate-based automatic client authentication - #14610

Merged
StavRLevi merged 4 commits into
redis:unstablefrom
StavRLevi:RED-176121
Dec 25, 2025
Merged

StavRLevi merged 4 commits into
redis:unstablefrom
StavRLevi:RED-176121

Conversation

@StavRLevi

Copy link
Copy Markdown
Collaborator

This PR implements support for automatic client authentication based on a field in the client's TLS certificate.
We adopt ValKey’s PR: valkey-io/valkey#1920

API Changes:

Add New configuration tls-auth-clients-user

  • Allowed values: off (default), CN.
  • off – disable TLS certificate–based auto-authentication.
  • CN – derive the ACL username from the Common Name (CN) field of the client certificate.

New INFO stat

  • acl_access_denied_tls_cert
  • Counts failed TLS certificate–based authentication attempts, i.e. TLS connections where a client certificate was presented, a username was derived from it, but no matching ACL user was found.

New ACL LOG reason

  • Reason string: "tls-cert"
  • Emitted when a client certificate’s Common Name fails to match any existing ACL user.

Implementation Details:

  • Added getCertFieldByName() utility to extract fields from peer certificates.

  • Added autoAuthenticateClientFromCert() to handle automatic login logic post-handshake.

  • Integrated automatic authentication into the TLSAccept function after handshake completion.

  • Updated test suite (tests/integration/tls.tcl) to validate the feature.

@StavRLevi
StavRLevi requested a review from ShooterIT December 8, 2025 08:46

@ShooterIT ShooterIT left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

interesting feature.

if some code is copied from valkey, we may add something at the begin of file, like https://github.com/redis/redis/blob/unstable/src/networking.c#L5

besides, we should add the authors of valkey commit to co-authors, right @sundb

Comment thread src/networking.c Outdated
sds username = connGetPeerUsername(conn);
if (username != NULL) {
user *u = ACLGetUserByName(username, sdslen(username));
if (u) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you notice this PR valkey-io/valkey#2364

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

now yes, thanks , the validation is necessary to ensure that a disabled user cannot authenticate. I’ve added the relevant changes as well as the tests

@ShooterIT ShooterIT added the release-notes indication that this issue needs to be mentioned in the release notes label Dec 10, 2025
Comment thread tests/unit/tls.tcl Outdated
assert {$after == $before + 1}
}

test {TLS: Disabled user cannot auto-authenticate via certificate} {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you write this test, maybe you can merge above two tests into one, since there are many duplicated codes,

foreach user_type {"non-existent" "disable"} {
    test {TLS: $user_type user cannot auto-authenticate via certificate} {
    }
}

@shahsb shahsb left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @StavRLevi for your efforts on this

Comment thread src/config.c

configEnum tls_client_auth_user_enum[] = {
{"CN", TLS_CLIENT_FIELD_CN},
{"off", TLS_CLIENT_FIELD_OFF},

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure the new option is clearly documented in redis.conf with examples for CA trust stores and cert validation modes (e.g., require exact match vs. wildcard subjects).

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agree, i can add examples

Comment thread src/config.c
{"CN", TLS_CLIENT_FIELD_CN},
{"off", TLS_CLIENT_FIELD_OFF},
{NULL, 0}
};

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: Add a tls-client-cert-require-subject or similar to enforce specific cert attributes. If not already, consider integrating with Redis's user/group system for role mapping based on cert CN.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it might be a good idea to follow up.. not for this PR. Current implementation covers the most common use case (CN mapping). Adding O, OU, EMAIL fields or wildcard matching could be a follow up enhancement..

Comment thread tests/unit/tls.tcl Outdated
# INFO stats should report that acl_access_denied_tls_cert increased by 1
set info_after [r INFO stats]
regexp {acl_access_denied_tls_cert:(\d+)} $info_after -> after
assert {$after == $before + 1}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add unit/integration tests for:

  • Successful auth with valid cert.
  • Rejection with invalid/expired/missing cert.
  • Fallback to password auth if cert fails.
  • Cluster mode compatibility (node-to-node cert auth?).

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, Successful auth with valid cert - is covered by Auto-authenticate using tls-auth-clients-user test.
Rejection with invalid/expired/missing cert - invalid/expired cert tests need certificate generation infrastructure (TLS-layer failures). Current coverage is solid for my opinion.
and for Fallback to password auth if cert fails. I added a test.

Comment thread src/config.c
{NULL, 0}
};

configEnum tls_client_auth_user_enum[] = {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Performance: Benchmark handshake latency with/without this feature under load (e.g., via redis-benchmark with --tls).

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, i think that the impact should be minimal (one cert parse per connection, not per command), but i can benchmark to confirm.

Comment thread src/acl.c
} else if (reason == ACL_DENIED_CHANNEL) {
server.acl_info.invalid_channel_accesses++;
} else if (reason == ACL_INVALID_TLS_CERT_AUTH) {
server.acl_info.acl_access_denied_tls_cert++;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Compatibility: Confirm no breakage for Redis clients without client certs (should still allow connections if not required).

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can see that: sds username = connGetPeerUsername(conn);
if (username != NULL) {
// auth logic
}
// If no cert or no CN, client stays as "default" user.. so already handled.. If no cert/CN, client remains as default user. Could add explicit test maybe

Comment thread src/tls.c

if (nid == -1) return 0;

X509_NAME *subject = X509_get_subject_name(cert);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Security Review: Audit for cert parsing vulns (e.g., buffer overflows in subject extraction). Consider fuzzing with libFuzzer.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We use standard OpenSSL APIs which are well tested... will try fuzzing as a hardening step

@ShooterIT ShooterIT left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, please trigger a daily CI run and make sure the TLS tests are green. I did not test it locally :)

Comment thread redis.conf Outdated
# Supported values: CN, off. Default: off.
#
# Matches certificate CN to Redis username (exact match only).
# Example: Cert CN=myapp → authenticates as user "myapp"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# Example: Cert CN=myapp → authenticates as user "myapp"
# Example: Cert CN=myapp -> authenticates as user "myapp"

let's use ASCII characters only

@sandeepkunusoth

sandeepkunusoth commented Dec 22, 2025 •

Copy link
Copy Markdown

is this scenario handled here?

](valkey-io/valkey#2962)

@StavRLevi

Copy link
Copy Markdown
Collaborator Author

Hi @sandeepkunusoth
Correct me if I misunderstood, but the behavior you're describing is not caused by the new TLS certificate authentication feature. This is how nopass has always worked in Redis ACL, even before this feature was added.
There are 2 separate authentication flow:
Certificate Auto-Auth ( what was added in this pr, happens during TLS handshake)
AUTH Command.
These flows are completely independent. The AUTH command behavior was not changed by this feature. So in this case you described, the user auth will fail in this new certificate flow, but succeed in the existing auth no pass flow becouse of no pass.
I can see the case you concern about, but I believe this is a misconfiguration of the user.. the nopass flag means "AUTH works without a password" - it does not mean "cert-only". To achieve the behavior you want, the user should be configured with a password..
maybe we can think about a future enhancement like adding a flag 'certonly' : ACL SETUSER svc-cn-user on certonly +@ALL but it wont prevent misconfiguration and this flow you describe can still happend.

@sandeepkunusoth

sandeepkunusoth commented Dec 22, 2025 •

Copy link
Copy Markdown

Hi @sandeepkunusoth Correct me if I misunderstood, but the behavior you're describing is not caused by the new TLS certificate authentication feature. This is how nopass has always worked in Redis ACL, even before this feature was added. There are 2 separate authentication flow: Certificate Auto-Auth ( what was added in this pr, happens during TLS handshake) AUTH Command. These flows are completely independent. The AUTH command behavior was not changed by this feature. So in this case you described, the user auth will fail in this new certificate flow, but succeed in the existing auth no pass flow becouse of no pass. I can see the case you concern about, but I believe this is a misconfiguration of the user.. the nopass flag means "AUTH works without a password" - it does not mean "cert-only". To achieve the behavior you want, the user should be configured with a password.. maybe we can think about a future enhancement like adding a flag 'certonly' : ACL SETUSER svc-cn-user on certonly +@ALL but it wont prevent misconfiguration and this flow you describe can still happend.

If I want password auth for some users and TLS client-cert (CN) auth for others, there’s no clean way today to prevent a CN user from logging in without a client cert when tls-auth-clients optional, because a CN user needs nopass to be cert-only — but nopass also enables AUTH with no cert.

If I remove nopass from the CN user, then the client must provide both:

a client certificate, and AUTH

Is that the intended/expected behavior? Or is there a supported way to have cert-only users and password-only users safely in the same deployment without this gap?

This below scenario happens even with tls-auth-clients yes
I have also observed other case where user is passing mTLS client cert without having CN=username and when this user tries to login to username as CN, user is able to login. so there is no check if user is passing certificate with CN as username.

@StavRLevi

Copy link
Copy Markdown
Collaborator Author

@sandeepkunusoth
I think the confusion is here: "because a CN user needs nopass to be cert-only"
CN users only need to exist and not be disabled. They do not require nopass for certificate-based login.
If you have a user configured as CN and also require a password, your options are:
With cert : authenticate via CN flow (auto-auth, no password needed)
Without cert (if tls-auth-clients optional) : authenticate via AUTH flow (password required)
Also, if a user has nopass, anyone can AUTH as that user regardless of what certificate they have (or don't have). That's expected behavior for nopass - it means "no password required for AUTH".

@sandeepkunusoth

sandeepkunusoth commented Dec 24, 2025 •

Copy link
Copy Markdown

@sandeepkunusoth I think the confusion is here: "because a CN user needs nopass to be cert-only" CN users only need to exist and not be disabled. They do not require nopass for certificate-based login. If you have a user configured as CN and also require a password, your options are: With cert : authenticate via CN flow (auto-auth, no password needed) Without cert (if tls-auth-clients optional) : authenticate via AUTH flow (password required) Also, if a user has nopass, anyone can AUTH as that user regardless of what certificate they have (or don't have). That's expected behavior for nopass - it means "no password required for AUTH".

Hi, I understand that nopass is not required for certificate-based login.

What I was trying to convey is that a user should authenticate either via password or via certificate (using the certificate CN), but not both at the same time.

Password-based login would apply to non-certificate users.

Certificate-based login would apply to certificate users, where authentication is done using the certificate CN and no password is required.

In this current implementation, a non-certificate user could still connect via CN username by passing any valid mTLS certificate, even if the certificate CN does not match the username.

@StavRLevi

Copy link
Copy Markdown
Collaborator Author

@sandeepkunusoth I believe that the key point is how certificate auto-auth and AUTH are separated.

I think that the main thing is that for this current implementation, certificate is not representing identity.. so if we do want it we should be considered as a future enhancement ( maybe by a cert-only flag) rather than a bug in the current implementation.

When tls-auth-clients-user=CN is enabled, the server derives the username strictly from the certificate CN during the TLS handshake. A client with a certificate CN=bob cannot authenticate as admin via the certificate flow — CN mismatches are not accepted.
If a client later authenticates as another user via AUTH, that is the existing AUTH mechanism and is unchanged by this feature. Flags like nopass have always meant "AUTH succeeds without a password," not "certificate-only."

So the behavior where a user can authenticate without a certificate is due to nopass, not the TLS CN feature. @ShooterIT WDYT?

@ShooterIT

Copy link
Copy Markdown
Member

i think they are different things.
And i don't know why we set nopass, if so, this means clients can auth without password, it seems not a bug

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release-notes indication that this issue needs to be mentioned in the release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants