SSL_GET_PEER_CERT_CHAIN(3SSL)

πŸ’– SSL_GET_PEER_CERT_CHAIN πŸ’–

okay bestie so this is a page from the OpenSSL manual, specifically the <purple>SSL_GET_PEER_CERT_CHAIN</purple> section of the <purple>3SSL</purple> manual set. yes the title is literally screaming in all caps, that's just how manpages are, they're dramatic like that πŸ’…


✨ NAME ✨

SSL_get_peer_cert_chain, SSL_get0_verified_chain β€” get the X509 certificate chain of the peer πŸ¦‹

like, you wanna know who you're talking to? which certs they handed over? that's this. iykyk.


🌸 SYNOPSIS 🌸

#include <openssl/ssl.h>

STACK_OF(X509) *SSL_get_peer_cert_chain(const SSL *ssl);
STACK_OF(X509) *SSL_get0_verified_chain(const SSL *ssl);

two functions, one vibe: get the chain. slay.


πŸŽ€ DESCRIPTION πŸŽ€

SSL_get_peer_cert_chain()

so this one returns a pointer to a STACK_OF(X509) β€” that's the certificate chain, as in, the literal stack of certs that the peer sent you. and here's the tea on who gets what:

  • if you call it on the client side, the stack also contains the peer's certificate ✨
  • if you call it on the server side, the peer's certificate has to be grabbed separately using SSL_get_peer_certificate(3) πŸ‘€
  • if the peer straight up did not present a certificate, you get NULL back. no cert, no chain, no nothing 😭
NB: SSL_get_peer_cert_chain() returns the peer chain as sent by the peer. it only consists of the certificates the peer has sent β€” in the order the peer has sent them. it is not a verified chain. like, it's what they said, not what's been proven. periodt.

SSL_get0_verified_chain()

this one returns the verified certificate chain of the peer, including the peer's end entity certificate. iconic behavior honestly.

it must be called after a session has been successfully established. don't be jumping the gun bestie.

and if peer verification wasn't successful β€” which you can tell because SSL_get_verify_result() is not returning X509_V_OK β€” then the chain may be incomplete or invalid. so like, handle with care 🫢


🧚 NOTES 🧚

if the session is resumed, peers do not send certificates, so both of these functions return a NULL pointer. that's not a bug, that's just how resumed sessions work. you can call SSL_session_reused() to figure out whether a session is resumed, so you're not out here confused πŸ’€

IMPORTANT β€” pay attention bestie: the reference count of each certificate in the returned STACK_OF(X509) object is not incremented, and the returned stack may be invalidated by renegotiation. if your application wants to use any certificates in the returned chain indefinitely, you must bump the reference counts using X509_up_ref(), or grab a copy of the whole entire chain with X509_chain_up_ref(). do not skip this step. i am begging πŸ™ˆ

⭐ RETURN VALUES ⭐

here's what can come back at you:

  • NULL β€” no certificate was presented by the peer, or no connection was established, or the certificate chain is no longer available when a session is reused.
  • Pointer to a STACK_OF(X509) β€” the return value points to the certificate chain presented by the peer. πŸŽ‰

πŸ”₯ SEE ALSO πŸ”₯

go read those too, they're all connected, it's giving lore 🌈


Copyright 2000-2016 The OpenSSL Project Authors. All Rights Reserved.

Licensed under the Apache License 2.0 (the "License"). You may not use this file except in compliance with the License. You can obtain a copy in the file LICENSE in the source distribution or at <https://www.openssl.org/source/license.html>.


okay that's the whole page bestie, you're officially certified in peer cert chain retrieval, no cap πŸ’œ

OpenSSL 4.0.3 Β· dated 2026-09-29 Β· from the SSL_GET_PEER_CERT_CHAIN(3SSL) page β€” stay verified, stay iconic, mwah πŸ’–βœ¨πŸŽ€πŸ¦‹

πŸ“¦ where this page comes from