Proposal: Supporting URI SAN in Certificate Authentication

Поиск
Список
Период
Сортировка
Искать

Proposal: Supporting URI SAN in Certificate Authentication

От:
olivier cano <kindermoumoute@gmail.com>
Дата:
Hello PostgreSQL Hackers,

I’d like to open the discussion about adding support for URI Subject Alternative Names (URI SAN) in PostgreSQL certificate authentication. Today, PostgreSQL only supports extracting identity from the certificate Subject (CN or full DN). This limits interoperability with modern workload identity systems that rely on URI-based identities:
* Cockroach Labs added URI SAN support for SPIFFE/SPIRE: https://www.cockroachlabs.com/blog/zero-trust-database-authentication-spiffe-spire
* The IETF WIMSE Working Group is standardizing URI-based workload identities: https://datatracker.ietf.org/group/wimse/about

Proposal: Allow certificate authentication to use URI SAN entries as the client identity (e.g. via a clientname=uri option in pg_hba.conf), in addition to the existing CN/DN options.

Questions:
* Is there interest in this feature from the community?
* Are there known objections or prior discussions around using SAN (and specifically URI SAN) for identity in PostgreSQL auth?
* How should multiple URI SAN entries be handled (first match, require uniqueness, mapping rules, etc.)?

Thanks,
Olivier Cano

Re: Proposal: Supporting URI SAN in Certificate Authentication

От:
David Steele <david@pgbackrest.org>
Дата:
On 3/27/26 20:20, olivier cano wrote:
> 
> Proposal: Allow certificate authentication to use URI SAN entries as the 
> client identity (e.g. via a clientname=uri option in pg_hba.conf), in 
> addition to the existing CN/DN options.
> 
> Questions:
> * Is there interest in this feature from the community?
This was recently requested for the pgBackRest project [1] so there does 
seem to be some interest from users, at least.

The user decided to fallback to using CNs since Postgres support does 
not seem like it will happen soon and we are unlikely to add it in 
pgBackRest until it is supported by Postgres.

Regards,
-David

[1] https://github.com/pgbackrest/pgbackrest/issues/2765


Re: Proposal: Supporting URI SAN in Certificate Authentication

От:
Jacob Champion <jacob.champion@enterprisedb.com>
Дата:
On Wed, Aug 26, 2026 at 11:07 AM Florin Irion  wrote:
> > * How should multiple URI SAN entries be handled (first match, require uniqueness, mapping rules, etc.)?
>
> The key thing is that the client must tell PostgreSQL which user they want to connect as. Then PostgreSQL looks through all the mappings it found and checks if any of the SANs map to that requested user. If exactly one SAN matches the user, we allow the connection. If none match, we reject. If many SANs map to different users but the client already said which one they want, that is fine, we just use the right one.

I interpreted the question about "first match" to mean: what if many
SANs in the cert map to the *same* database user?

(IMO an implementation of this that ignored pg_ident mapping would be
vetoable without a really, _really_ good reason, so I'm not sure that
branch of the discussion needs to consume much energy.)

> I think this is better than trying to pick the first match automatically, because that can be confusing.

I don't understand what you mean here, in the context of our startup
handshake. We don't allow you to omit the database username from the
startup packet, so there couldn't be any guesswork around which DB
role a client meant to connect under.

> The pg_ident.conf rules let you organize this however makes sense for your setup.

Sort of. We're currently pretty bad at allowing you to say "or" in the
HBA. So without more design work, I don't think there's going to be a
way to dynamically switch the `clientname` setting from CN to DN to
URI (to email, to other SAN...); you'd have to choose one and stick to
it for a given HBA line.

--Jacob


Re: Proposal: Supporting URI SAN in Certificate Authentication

От:
Florin Irion <irionr@gmail.com>
Дата:
Thanks Jacob,

Il giorno gio 27 ago 2026 alle ore 00:12 Jacob Champion <jacob.champion@enterprisedb.com> ha scritto:
I interpreted the question about "first match" to mean: what if many
SANs in the cert map to the *same* database user?

Well that is mainly how the user configures the pg_ident and their SANs, We can't do much about it, no? 
And initially I was looking for 1 to 1 dbuser and SAN, but it should jsut be dbuser<->ANY SAN (once we find the first one that authenticates the user the client requests we're good).


> I think this is better than trying to pick the first match automatically, because that can be confusing.

I don't understand what you mean here, in the context of our startup
handshake. We don't allow you to omit the database username from the
startup packet, so there couldn't be any guesswork around which DB
role a client meant to connect under.

Correct, I probably just expressed it badly, I was trying to make a point that the dbuser is requested, so we just need to find a good SAN for it in order to authenticate it. And based on the previous point, not necessarily a unique dbuser<->SAN pair. 


> The pg_ident.conf rules let you organize this however makes sense for your setup.

Sort of. We're currently pretty bad at allowing you to say "or" in the
HBA. So without more design work, I don't think there's going to be a
way to dynamically switch the `clientname` setting from CN to DN to
URI (to email, to other SAN...); you'd have to choose one and stick to
it for a given HBA line.

Yes, a user can organize the rules in every way they like , but we can not use different `clientname` in pg_hba, that is still teh same as we have now with e.g. CN and DN, no? 
If we were to change this , it should not be part of this work IMO, no? Or is there something I'm missing? 

Cheers,
Florin


--
     Florin Irion       

Re: Proposal: Supporting URI SAN in Certificate Authentication

От:
Jacob Champion <jacob.champion@enterprisedb.com>
Дата:
On Thu, Aug 27, 2026 at 3:25 AM Florin Irion  wrote:
> Well that is mainly how the user configures the pg_ident and their SANs, We can't do much about it, no?
> And initially I was looking for 1 to 1 dbuser and SAN, but it should jsut be dbuser<->ANY SAN (once we find the first one that authenticates the user the client requests we're good).

Right. (I think that's what Olivier was trying to say in the OP.)

> Yes, a user can organize the rules in every way they like , but we can not use different `clientname` in pg_hba, that is still teh same as we have now with e.g. CN and DN, no?

It's the "same", I guess, but you can only ever have one Subject, and
CN is a subset of DN. So IMO, it doesn't really make sense to combine
them. But that's not the case for SANs.

> If we were to change this , it should not be part of this work IMO, no? Or is there something I'm missing?

I just want to make sure that expectations are set appropriately. If
the desired end goal is that you can authenticate using the standard
Subject match _or_ a number of Subject Alternative Names (which seems
like a very reasonable thing for people to want), v1 of this feature
is unlikely to deliver that without a lot more explicit planning and
design. That's okay with me as long as it's also okay with the people
asking for the feature.

Thanks,
--Jacob


Re: Proposal: Supporting URI SAN in Certificate Authentication

От:
Florin Irion <irionr@gmail.com>
Дата:
Вложения:
Hi

Here is the first version of the patch to support URI SAN in certificate
authentication (clientname=URI in pg_hba.conf). We can start a discussion
on the actual code now.The patch can also be reviewed on my “postgres”
project fork [1].

Together with Leonardo Cecchi, CloudNativePG (CNPG) maintainer, we also
tested the patch using the cnpg-i-spiffe plugin[2] that he wrote for the
occasion and the generated container image for CloudNativePG [3]. Everything
worked smoothly, and workloads can now authenticate natively with SPIFFE 
IDs.

Given that both CloudNativePG and SPIFFE/SPIRE are CNCF projects, we 
contacted
Agustín Martínez Fayó, a SPIRE maintainer, to verify the integration between
the two projects and to begin collaborating on the topic. We decided to
strictly allow only a single URI SAN per certificate. This follows the
X509-SVID spec, avoiding weird edge cases with user mapping, and making
connection logging very clear.

Cheers,
Florin

[1] - https://github.com/irionr/postgres/pull/7

[2] - https://github.com/leonardoce/cnpg-i-spiffe

[3] - 
https://github.com/leonardoce/postgres-trunk-containers/actions/runs/35070429177 

Re: Proposal: Supporting URI SAN in Certificate Authentication

От:
Jacob Champion <jacob.champion@enterprisedb.com>
Дата:
On Thu, Sep 17, 2026 at 3:10 AM Florin Irion  wrote:
> We decided to
> strictly allow only a single URI SAN per certificate. This follows the
> X509-SVID spec, avoiding weird edge cases with user mapping, and making
> connection logging very clear.

I'd like opinions from the crowd on the use of X509-SVID as a
governing spec here. I don't mind the 1:1 requirement for now; it
simplifies things. But if someone comes forward proposing multiple-URI
support, are we going to reject it since the original use case was
SPIFFE? Also, this patch doesn't seem to perform the other required
checks for X509-SVID validation [1], so it seems a little weird to
cite SPIFFE in the comments and tests and documentation.

Partial review follows (I have not read in detail):

> + * An empty subject means there is no DN to record, so leave
> + * peer_dn NULL.  The X.509-SVID specification allows this when a
> + * (critical) URI subjectAltName is present.

From a skim, it looks like other parts of the code assume either
peer_dn or peer_cn is set; they'll need to be changed too.

X.509-SVID isn't relevant in this particular part of the code, IMO.
Empty Subjects have been permissible in general for a very long time,
and criticality should generally be handled by the OpenSSL layer
rather than us.

The growing indentation in be_tls_open_server is, I think, a smell.
We've reached rule of three (or at least 2.5 depending on your view of
peer_dn and peer_cn) and I think this code is going to need to be
restructured a bit in order to absorb the new use case. I don't have
anything in particular in mind yet.

> The X.509-SVID specification
> +# requires the URI SAN to be critical when the subject is empty.

We can cite RFC 5280 for the criticality requirement (X509-SVID defers
to it anyway).

> +# Client certificate with an empty subject and a single, critical URI
> +# subjectAltName.  Unlike the standard client certificates, this needs
> +# an explicit "-subj /" when generating the CSR, since the config file
> +# alone cannot express an empty subject.

That's fun. Let's just pull the `-subj /` into a default-empty
variable that the new CSR can tweak. That should get rid of most of
the new copy-paste boilerplate, I think.

> For SPIFFE deployments, configure
> +       ssl_ca_file with a single trust domain's bundle;
> +      

Looks like this was cut off?

> + /*
> +  * The SPIFFE X.509-SVID specification requires an SVID to carry
> +  * exactly one URI SAN, so refuse both none and more than one: the
> +  * authenticated identity must be unambiguous.
> +  */
> + if (port->peer_uri_count != 1)

I think it's really important that we keep unedited LLMisms out of the
security code. (And elsewhere too.)

Like, definitely push back if I've misattributed this, but I imagine
that you wouldn't have willingly typed three lines of comments in
order to say "we require exactly one URI", and I don't really want to
read three lines of comments that say that. We need the
signal-to-noise ratio to be very high here.

> - if (peer_username == NULL ||
> -     strlen(peer_username) <= 0)
> + if (peer_username == NULL || strlen(peer_username) <= 0)

Drive-by cleanup is usually very welcome (thank you), but for security
patches I think it's good to keep unrelated code motion separate from
new implementation.

Thanks,
--Jacob

[1] https://spiffe.io/docs/latest/spiffe-specs/x509-svid/#5-validation


Re: Proposal: Supporting URI SAN in Certificate Authentication

От:
Florin Irion <irionr@gmail.com>
Дата:
Вложения:


On 18/09/2026 17:58, Jacob Champion wrote:
> On Thu, Sep 17, 2026 at 3:10 AM Florin Irion  wrote:
>> We decided to
>> strictly allow only a single URI SAN per certificate. This follows the
>> X509-SVID spec, avoiding weird edge cases with user mapping, and making
>> connection logging very clear.
> I'd like opinions from the crowd on the use of X509-SVID as a
> governing spec here. I don't mind the 1:1 requirement for now; it
> simplifies things. But if someone comes forward proposing multiple-URI
> support, are we going to reject it since the original use case was
> SPIFFE? Also, this patch doesn't seem to perform the other required
> checks for X509-SVID validation [1], so it seems a little weird to
> cite SPIFFE in the comments and tests and documentation.

The single-URI requirement is a design simplification, not a spec 
requirement.
SPIFFE motivated the feature but doesn't necessarily govern it. If someone
later proposes matching against multiple URI SANs, it can be evaluated 
on its
own merits.
v2 drops all X509-SVID citations from comments, tests, and docs, since 
we don't
implement the other X509-SVID validation checks.

> Partial review follows (I have not read in detail):
>
>> + * An empty subject means there is no DN to record, so leave
>> + * peer_dn NULL.  The X.509-SVID specification allows this when a
>> + * (critical) URI subjectAltName is present.
>  From a skim, it looks like other parts of the code assume either
> peer_dn or peer_cn is set; they'll need to be changed too.

With V2 I don't see any other places where we assume peer_dn or peer_cn 
is set.

> The growing indentation in be_tls_open_server is, I think, a smell.
> We've reached rule of three (or at least 2.5 depending on your view of
> peer_dn and peer_cn) and I think this code is going to need to be
> restructured a bit in order to absorb the new use case. I don't have
> anything in particular in mind yet.

I restructured the code adding be_tls_extract_peer_cert_names()

>> The X.509-SVID specification
>> +# requires the URI SAN to be critical when the subject is empty.
> We can cite RFC 5280 for the criticality requirement (X509-SVID defers
> to it anyway).

done.

>> +# Client certificate with an empty subject and a single, critical URI
>> +# subjectAltName.  Unlike the standard client certificates, this needs
>> +# an explicit "-subj /" when generating the CSR, since the config file
>> +# alone cannot express an empty subject.
> That's fun. Let's just pull the `-subj /` into a default-empty
> variable that the new CSR can tweak. That should get rid of most of
> the new copy-paste boilerplate, I think.

done.

>> For SPIFFE deployments, configure
>> +       ssl_ca_file with a single trust domain's bundle;
>> +      
> Looks like this was cut off?

Yup, fixed.

>> + /*
>> +  * The SPIFFE X.509-SVID specification requires an SVID to carry
>> +  * exactly one URI SAN, so refuse both none and more than one: the
>> +  * authenticated identity must be unambiguous.
>> +  */
>> + if (port->peer_uri_count != 1)
> I think it's really important that we keep unedited LLMisms out of the
> security code. (And elsewhere too.)
>
> Like, definitely push back if I've misattributed this, but I imagine
> that you wouldn't have willingly typed three lines of comments in
> order to say "we require exactly one URI", and I don't really want to
> read three lines of comments that say that. We need the
> signal-to-noise ratio to be very high here.

True.
I tend to be very succinct and sometimes I fear I don't give enough context,
but I agree here the LLM added way too much.
Fixed.

>> - if (peer_username == NULL ||
>> -     strlen(peer_username) <= 0)
>> + if (peer_username == NULL || strlen(peer_username) <= 0)
> Drive-by cleanup is usually very welcome (thank you), but for security
> patches I think it's good to keep unrelated code motion separate from
> new implementation.

Makes sense. Removed.

Thank you for checking this!
V2 attached.

Florin

-- 
_/**/__/**/__/**/__/**/__/* Florin Irion */__/**/__/**/__/**/__/**/__/**/_
/https://www.enterprisedb.com/

Re: Proposal: Supporting URI SAN in Certificate Authentication

От:
Agustín Martínez Fayó <amartinezfayo@gmail.com>
Дата:
On 22/09/2026 11:46, Florin Irion wrote:
> The single-URI requirement is a design simplification, not a spec requirement.
> SPIFFE motivated the feature but doesn't necessarily govern it. If someone
> later proposes matching against multiple URI SANs, it can be evaluated on its
> own merits.

Hi all,

Thank you Florin for the introduction. The SPIFFE community has wanted
native URI SAN authentication in PostgreSQL for a long time, since
integrations so far have typically had to go through a DNS SAN copied
into the CN, so I am glad to see this happening. I wanted to share a few
thoughts on the discussion so far, and some comments on v2.

I agree with treating the single URI as a design simplification rather
than something X509-SVID imposes. It keeps the existing model of one
identity string per certificate, the same as CN and DN, and it only
applies on lines that opt into clientname=URI, so I would not expect it
to break anyone. I also think strict first is the ordering that stays
compatible, since any-of-N matching could be added later as an opt-in
without affecting anyone who relied on the single-URI rule, whereas the
reverse would be a behavior change. For what it is worth, X509-SVID
requires a single URI SAN for the auditing and authorization reasons
Florin mentioned [1], so SPIFFE deployments would not be affected by a
later multi-URI option either way, since their certificates carry one
URI and would behave the same under both rules.

I also agree with not adding the other X509-SVID validation checks. As I
see it, PostgreSQL is a relying party consuming a client certificate
rather than an SVID validator. The leaf checks in section 5.2 [2] (cA
false, no keyCertSign or cRLSign, spiffe scheme, non-root path) are only
defeatable by someone holding a CA key, who could mint any identity
anyway, and the scheme and path are pinned in practice by the pg_ident
entry (for a literal entry or an anchored regex). The one SPIFFE
property I do not think PostgreSQL can provide today is binding the
trust domain in the ID to the CA that issued it, since ssl_ca_file is a
flat list. The Federation spec says bundles from different trust domains
must not be merged for that reason [3], so the caveat in v2 (one trust
domain per ssl_ca_file, federation unsupported) matches what the spec
expects.

Longer term, I think the SPIFFE community would welcome a SPIFFE-aware
trust mode in PostgreSQL, where each configured trust domain has its own
bundle and the trust domain in the ID selects which bundle validates the
chain, similar to Envoy's SPIFFE certificate validator [4]. That would
be a separate design with its own configuration questions, so I am not
proposing it for this patch. I only mention it because the URI SAN match
is the substrate such a mode would build on, and v2 already keeps that
door open by recording the URI as the identity. What I think would be
great to have in v1 is a short worked example in the documentation, a
pg_hba line with clientname=URI and a pg_ident entry mapping a SPIFFE ID
to a role, so that people looking for SPIFFE support can find the
feature.

I built v2, ran the SSL TAP suite, and tried a couple of things beyond
it. A few comments on the patch follow.

> +$node->connect_fails(
> +    "$uri_connstr user=ssltestuser sslcert=ssl/client-uri-multi.crt "
> +      . sslkey('client-uri-multi.key'),
> +    "certificate authorization fails when client certificate has multiple URI SANs",
> +    expected_stderr =>
> +      qr/certificate authentication failed for user "ssltestuser"/);

I think this does not exercise the count check. client-uri-multi.config
lists the non-matching URI first and the code keeps the first URI, so
without the check the connection is authenticated as
spiffe://.../not-the-user and then fails on the usermap, which produces
the same client error. I deleted the check and the suite still passes
286/286. I wonder if putting the matching URI first in the fixture, or
asserting the server log reason with log_like, would make the test fail
for the right cause.

> +        /* fail closed if the URI could not be converted */
> +        if (port->peer_uri_count > 0 && port->peer_uri == NULL)
> +            port->peer_uri_count = 0;

This seems to log a COMMERROR while the handshake still completes. I
signed a certificate with the suite's client CA whose single URI SAN
contains a null byte. On a clientname=CN line the log shows "URI subject
alternative name contains embedded null" and the connection is then
authenticated, with SELECT system_user returning cert:CN=ssltestuser. On
a clientname=URI line the same log line is followed by the single-URI
rejection message, which points at the wrong cause. Returning false
here, as the CN path does for an embedded null, would also reject such
certificates on CN and DN lines, which never looked at URI SANs before
this patch. I think that is a reasonable price for consistency, but the
alternative of failing only on URI lines with an accurate error message
would work too.

> +    if (X509_NAME_print_ex(bio, x509name, 0, XN_FLAG_RFC2253) == -1 ||
> +        BIO_get_mem_ptr(bio, &bio_buf) < 0)

I wonder whether relaxing this from <= 0 was needed. With OpenSSL 3.x
and 1.1.1, X509_NAME_print_ex returns 0 for an empty name and
BIO_get_mem_ptr still returns 1. If BIO_get_mem_ptr ever returned 0,
bio_buf would stay NULL for the dereference below, so the original check
seems safer to me.

> +       contain exactly one URI subject alternative name.  The comparison is
> +       case-sensitive and uses the exact URI as it appears in the
> +       certificate.

Minor. Since the URI is matched as an opaque string, I wonder if a
sentence advising that regular-expression maps anchor the full scheme
and authority would help avoid patterns that match a URI from an
unexpected authority.

> +       Note that PostgreSQL does not verify any relationship between a
> +       certificate's URI and the certificate authority that signed the
> +       certificate.  If ssl_ca_file contains certificates
> +       for more than one trust domain, any of them can issue a certificate
> +       for a URI in another trust domain.  For SPIFFE deployments, configure

Minor. This uses "trust domain" before SPIFFE is introduced. Maybe it
could say that any CA in the file can issue a certificate carrying any
URI, and keep the trust domain wording for the SPIFFE sentence that
follows.

Also minor, and outside the hunks. The cert method section ("The cn
(Common Name) attribute of the certificate will be compared") and the
clientcert=verify-full description ("enforces that the cn (Common Name)
in the certificate matches") still describe CN only, and the client
certificates section in runtime.sgml says the same thing in three
places. With three clientname values now, I think a pointer to
clientname from those places would help.

Everything else looks right to me from the SPIFFE side. The matched URI
is the recorded identity, an empty Subject with a critical URI SAN is
accepted and tested (the shape RFC 5280 prescribes when the only name is
in the SAN [5]), and the byte-for-byte comparison is documented. The
SPIFFE ID spec treats the scheme and trust domain as case-insensitive
when parsing [6], but also requires the trust domain to be
lowercase [7], and go-spiffe rejects a trust domain that is not
lowercase rather than normalizing it [8]. So I think treating the URI as
an opaque, case-sensitive string is consistent with the reference
implementation, and a certificate that differs only in case fails to
match rather than matching unexpectedly.

Happy to keep testing against SPIRE-issued SVIDs as the patch evolves.

Regards,
Agustín

[1] https://github.com/spiffe/spiffe/blob/main/standards/X509-SVID.md#2-spiffe-id
[2] https://github.com/spiffe/spiffe/blob/main/standards/X509-SVID.md#52-leaf-validation
[3] https://github.com/spiffe/spiffe/blob/main/standards/SPIFFE_Federation.md#42-managing-fetched-bundles
[4] https://www.envoyproxy.io/docs/envoy/latest/api-v3/extensions/transport_sockets/tls/v3/tls_spiffe_validator_config.proto
[5] https://www.rfc-editor.org/rfc/rfc5280#section-4.1.2.6
[6] https://github.com/spiffe/spiffe/blob/main/standards/SPIFFE-ID.md#24-spiffe-id-parsing
[7] https://github.com/spiffe/spiffe/blob/main/standards/SPIFFE-ID.md#21-trust-domain
[8] https://github.com/spiffe/go-spiffe/blob/main/spiffeid/trustdomain.go


Re: Proposal: Supporting URI SAN in Certificate Authentication

От:
Nico Williams <nico@cryptonector.com>
Дата:
On Wed, Aug 26, 2026 at 03:12:07PM -0700, Jacob Champion wrote:
> On Wed, Aug 26, 2026 at 11:07 AM Florin Irion  wrote:
> > > * How should multiple URI SAN entries be handled (first match,
> > > require uniqueness, mapping rules, etc.)?
> >
> > The key thing is that the client must tell PostgreSQL which user
> > they want to connect as. Then PostgreSQL looks through all the
> > mappings it found and checks if any of the SANs map to that
> > requested user. If exactly one SAN matches the user, we allow the
> > connection. If none match, we reject. If many SANs map to different
> > users but the client already said which one they want, that is fine,
> > we just use the right one.
> 
> I interpreted the question about "first match" to mean: what if many
> SANs in the cert map to the *same* database user?

A deterministic rule in that case is good enough, and 'first match' is a
perfectly good rule that also minimizes the amount of work to do, so
it's the rule I would prefer.

> (IMO an implementation of this that ignored pg_ident mapping would be
> vetoable without a really, _really_ good reason, so I'm not sure that
> branch of the discussion needs to consume much energy.)

(Sure, but IMO pg_ident and pg_hba ought to be _replaced_ with DDL and
metadata stored in pg_catalog tables.  This is a discussion that
deserves its own thread because once one starts down this path lots of
possibilities become available, such as radically changing the, uh,
metaschema for authorization.

But this is especially desirable given the SPIFFE motivation for this
work: the authorization database has to become significantly more
dynamic, both in that pg_ident entries need to be changed on the fly,
and in that one might want to be able to provide SQL functions to do the
work rather than table lookups.)

> > I think this is better than trying to pick the first match
> > automatically, because that can be confusing.
> 
> I don't understand what you mean here, in the context of our startup
> handshake. We don't allow you to omit the database username from the
> startup packet, so there couldn't be any guesswork around which DB
> role a client meant to connect under.

One could have a convention that some username (possibly the empty
string) is special and means 'derive the username from the
authentication credential'.  SSHv2 has this, for example, at least in
the GSS-API userauth case.

> > The pg_ident.conf rules let you organize this however makes sense for your setup.
> 
> Sort of. We're currently pretty bad at allowing you to say "or" in the
> HBA. So without more design work, I don't think there's going to be a
> way to dynamically switch the `clientname` setting from CN to DN to
> URI (to email, to other SAN...); you'd have to choose one and stick to
> it for a given HBA line.

+1.

Nico
-- 


Re: Proposal: Supporting URI SAN in Certificate Authentication

От:
Florin Irion <irionr@gmail.com>
Дата:
Вложения:

Thanks for the detailed review and the SPIFFE perspective.  v3 addresses all of it


On 26/09/2026 01:19, Agustín Martínez Fayó wrote:
>> +$node->connect_fails(
>> +    "$uri_connstr user=ssltestuser sslcert=ssl/client-uri-multi.crt "
>> +      . sslkey('client-uri-multi.key'),
>> +    "certificate authorization fails when client certificate has multiple URI SANs",
>> +    expected_stderr =>
>> +      qr/certificate authentication failed for user "ssltestuser"/);
> I think this does not exercise the count check. client-uri-multi.config
> lists the non-matching URI first and the code keeps the first URI, so
> without the check the connection is authenticated as
> spiffe://.../not-the-user and then fails on the usermap, which produces
> the same client error. I deleted the check and the suite still passes
> 286/286. I wonder if putting the matching URI first in the fixture, or
> asserting the server log reason with log_like, would make the test fail
> for the right cause.

Ok changed, now it lists the matching URI first and asserts the
server-side "exactly one URI SAN" message, so the test fails for the
right reason if the count check is removed.

>> +        /* fail closed if the URI could not be converted */
>> +        if (port->peer_uri_count > 0 && port->peer_uri == NULL)
>> +            port->peer_uri_count = 0;
> This seems to log a COMMERROR while the handshake still completes. I
> signed a certificate with the suite's client CA whose single URI SAN
> contains a null byte. On a clientname=CN line the log shows "URI subject
> alternative name contains embedded null" and the connection is then
> authenticated, with SELECT system_user returning cert:CN=ssltestuser. On
> a clientname=URI line the same log line is followed by the single-URI
> rejection message, which points at the wrong cause. Returning false
> here, as the CN path does for an embedded null, would also reject such
> certificates on CN and DN lines, which never looked at URI SANs before
> this patch. I think that is a reasonable price for consistency, but the
> alternative of failing only on URI lines with an accurate error message
> would work too.

The extraction now happens in CheckCertAuth(), only for
clientname=URI lines, so CN/DN lines never look at URI SANs.  A URI
that cannot be represented as a string logs the precise cause and
fails with its own error message, instead of the misleading
"exactly one URI SAN" message.

>> +    if (X509_NAME_print_ex(bio, x509name, 0, XN_FLAG_RFC2253) == -1 ||
>> +        BIO_get_mem_ptr(bio, &bio_buf) < 0)
> I wonder whether relaxing this from <= 0 was needed. With OpenSSL 3.x
> and 1.1.1, X509_NAME_print_ex returns 0 for an empty name and
> BIO_get_mem_ptr still returns 1. If BIO_get_mem_ptr ever returned 0,
> bio_buf would stay NULL for the dereference below, so the original check
> seems safer to me.

Done, BIO_get_mem_ptr() is back to <= 0.

>> +       contain exactly one URI subject alternative name.  The comparison is
>> +       case-sensitive and uses the exact URI as it appears in the
>> +       certificate.
> Minor. Since the URI is matched as an opaque string, I wonder if a
> sentence advising that regular-expression maps anchor the full scheme
> and authority would help avoid patterns that match a URI from an
> unexpected authority.
>> +       Note that PostgreSQL does not verify any relationship between a
>> +       certificate's URI and the certificate authority that signed the
>> +       certificate.  If ssl_ca_file contains certificates
>> +       for more than one trust domain, any of them can issue a certificate
>> +       for a URI in another trust domain.  For SPIFFE deployments, configure
> Minor. This uses "trust domain" before SPIFFE is introduced. Maybe it
> could say that any CA in the file can issue a certificate carrying any
> URI, and keep the trust domain wording for the SPIFFE sentence that
> follows.

Changed to advise anchoring regex maps to the scheme and authority,
introduce "trust domain" only in the SPIFFE sentence, reference
clientname from the CN-only descriptions, and include a worked
pg_hba.conf/pg_ident.conf example.

Cheers,
Florin

--
Florin Irion

www.enterprisedb.com





Re: Proposal: Supporting URI SAN in Certificate Authentication

От:
Agustín Martínez Fayó <amartinezfayo@gmail.com>
Дата:
On Tue, Oct 6, 2026 at 3:12 AM Florin Irion  wrote:
> Thanks for the detailed review and the SPIFFE perspective.  v3 addresses all of it

Thanks Florin. I applied v3 on current master, re-ran the SSL TAP suite
and the embedded-null certificate, and all the changes look good to me,
including the documentation. From the SPIFFE side, I think v3 is in
good shape.

Thank you again for working on this integration. I'm excited to see it
coming together and look forward to seeing it land.

Regards,
Agustín


FAQ