Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to obtain client certificate used to authenticate in my Rust Tonic service?

In my Rust Tonic setup, I have configured mTLS (mutual TLS) authentication between client and server.

Now, I would like to use the data stored in client certificate a) for authorizing access (interceptor) b) use in providing the service (ie. I want to see Hello Petr if Petr is connecting)

I could not find any examples about this, and traversing Tonic source code didn't help me, too.

like image 410
Petr Kozelka Avatar asked Oct 30 '25 17:10

Petr Kozelka


1 Answers

After asking on discord channel tokio/tonic, I got the solution (thanks Lucio Franco!) and also found that there actually is an example in tonic (yes my research was not good enough).

For anyone interested, this is the example code: https://github.com/hyperium/tonic/blob/master/examples/src/tls_client_auth/server.rs

Brief summary:

the request parameter, provided in each method generated for the service, contains parameter peer_certs() which returns all the user certificates.

These can be then iterated, and their bytes parsed. I used crate x509-parser which works great for me, but others might be probably used as well.

like image 85
Petr Kozelka Avatar answered Nov 03 '25 08:11

Petr Kozelka