C Collections

chttpserver · Section 3

chttpsvr_req_peer_cert_subject

chttpsvr_req_peer_cert_subject - give the subject of the verified client certificate

C Collections (libccollections, -lccollections)

#include <ccollections/chttpserver.h>
 
const char *chttpsvr_req_peer_cert_subject(const chttpsvr_req *req);

Returns the subject of the client certificate of the request as an
RFC 2253 distinguished name, which lists the attributes from the most
specific to the least specific, for example
 
    CN=alice,OU=staff,O=Example
 
Every byte above 0x7F, every control character and every character
that RFC 2253 reserves is escaped with a backslash.  The string is
therefore printable ASCII, and a name that a client crafted can't
pass for another one: An organization "Example, Inc." reads as
O=Example\, Inc.  Compare the subject as a whole string, because a
substring of a subject can come from any of its attributes.
 
You get the subject only when chttpsvr_req_peer_cert_verified(3)
reports true for req.  It is a property of the connection, so every
request on one keep-alive connection gets the same text, and a
resumed TLS session gets the subject that the handshake which set up
the session verified.

req  The request handle.

A NUL-terminated string that is valid for the lifetime of the
request.  Do not free it.
 
NULL in every case where chttpsvr_req_peer_cert_verified(3) reports
false (a plaintext connection, a server without a ca_bundle_path, a
client that presented no certificate, and a NULL req), and NULL as
well when an allocation fails.  Call chttpsvr_req_peer_cert_verified(3)
to tell these apart.

Like every other request accessor, call it only on the thread that
runs the handler or the middleware of req.

#include <ccollections/chttpserver.h>
#include <string.h>
 
static void handler(chttpsvr_req *req, chttpsvr_resp *resp, void *ctx)
{
    (void)ctx;
    const char *who = chttpsvr_req_peer_cert_subject(req);
    if (!who || strcmp(who, "CN=billing,O=Example") != 0) {
        chttpsvr_resp_set_status(resp, CHTTP_STATUS_FORBIDDEN);
        return;
    }
    chttpsvr_resp_write_str(resp, "ok");
}

chttpsvr_req_peer_cert_verified(3), chttpsvr_req_peer_cert_der(3), chttpsvr_req_peer_cert_sha256(3)