SSL_GET_PEER_CERTIFICATE(3ossl)

πŸ’– SSL_GET_PEER_CERTIFICATE πŸ’–

okay bestie, welcome to SSL_get_peer_certificate(3ossl) β€” it's an OpenSSL page, it lives in section 3ossl, and it's literally all about getting the X509 certificate of the peer. three functions, one job, one of them is lowkey retired. let's get into it 🫢


✨ NAME ✨

SSL_get_peer_certificate, SSL_get0_peer_certificate, SSL_get1_peer_certificate β€” get the X509 certificate of the peer. that's the whole vibe. πŸ’…


🌸 SYNOPSIS 🌸

#include <openssl/ssl.h>

X509 *SSL_get0_peer_certificate(const SSL *ssl);
X509 *SSL_get1_peer_certificate(const SSL *ssl);

and now for the one that's giving *legacy*… the following function has been deprecated since OpenSSL 3.0, and you can hide it entirely by defining OPENSSL_API_COMPAT with a suitable version value β€” peep openssl_user_macros(7) for that:

X509 *SSL_get_peer_certificate(const SSL *ssl);

πŸŽ€ DESCRIPTION πŸŽ€

these functions return a pointer to the X509 certificate that the peer presented. and if the peer did not present a certificate? you get NULL. no cert, no pointer, no drama. πŸ™ˆ


πŸ¦‹ NOTES πŸ¦‹

okay so the lore here matters, pay attention:

  • due to the protocol definition, a TLS/SSL server will always send a certificate, if present. iconic behavior honestly πŸ‘‘
  • a client will only send a certificate when it's explicitly requested to do so by the server β€” see SSL_CTX_set_verify(3)
  • if an anonymous cipher is used, no certificates are sent. periodt.
⚠️ the way that a certificate being returned does not indicate information about the verification state?? yeah. don't assume. use SSL_get_verify_result(3) to check the verification state, bestie. i'm serious.

now the refcount tea, because this is where people get humbled 😭:

  • the reference count of the X509 object returned by SSL_get1_peer_certificate() is incremented by one, so that it will not be destroyed when the session containing the peer certificate is freed. that means the X509 object must be explicitly freed using X509_free(). you own it. it's yours. clean up after yourself πŸ’–
  • the reference count of the X509 object returned by SSL_get0_peer_certificate() is not incremented, and must not be freed. hands off, it's not yours 🚫

and the lil plot twist: SSL_get_peer_certificate() is an alias of SSL_get1_peer_certificate(). same energy, older name.


πŸ’œ RETURN VALUES πŸ’œ

the following return values can occur:

  • NULL β€” no certificate was presented by the peer, or no connection was established. rough. 😭
  • Pointer to an X509 certificate β€” the return value points to the certificate presented by the peer. slay ✨

🌈 SEE ALSO 🌈

go read those, iykyk 🧚


⭐ HISTORY ⭐

  • SSL_get0_peer_certificate() and SSL_get1_peer_certificate() were added in 3.0.0.
  • SSL_get_peer_certificate() was deprecated in 3.0.0. rest in peace legend πŸ’€

Copyright 2000-2024 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>.


and that's the page!! this whole thing is from OpenSSL 3.5.9, dated 2026-09-29, and it's SSL_GET_PEER_CERTIFICATE(3ossl) β€” ty for coming to my ted talk, now go free your X509 objects πŸ«ΆπŸ’–βœ¨

πŸ“¦ where this page comes from