X509_CHECK_PURPOSE(3SSL)

πŸ’– NAME πŸ’–

okay so bestie, this is the X509_CHECK_PURPOSE(3SSL) page from the OpenSSL manual, and it's literally the whole girl squad of functions that figure out what a certificate is actually for (and manage the purpose table while they're at it):

X509_check_purpose, X509_PURPOSE_get_count, X509_PURPOSE_get_unused_id, X509_PURPOSE_get_by_sname, X509_PURPOSE_get_by_id, X509_PURPOSE_add, X509_PURPOSE_cleanup, X509_PURPOSE_get0, X509_PURPOSE_get_id, X509_PURPOSE_get0_name, X509_PURPOSE_get0_sname, X509_PURPOSE_get_trust, X509_PURPOSE_set β€” functions related to checking the purpose of a certificate. πŸ’…


✨ SYNOPSIS ✨

#include <openssl/x509v3.h>

int X509_check_purpose(const X509 *x, int id, int ca);

int X509_PURPOSE_get_count(void);
int X509_PURPOSE_get_unused_id(OSSL_LIB_CTX *libctx);
int X509_PURPOSE_get_by_sname(const char *sname);
int X509_PURPOSE_get_by_id(int id);
int X509_PURPOSE_add(int id, int trust, int flags,
                     int (*ck) (const X509_PURPOSE *, const X509 *, int),
                     const char *name, const char *sname, void *arg);
void X509_PURPOSE_cleanup(void);

X509_PURPOSE *X509_PURPOSE_get0(int idx);
int X509_PURPOSE_get_id(const X509_PURPOSE *);
char *X509_PURPOSE_get0_name(const X509_PURPOSE *xp);
char *X509_PURPOSE_get0_sname(const X509_PURPOSE *xp);
int X509_PURPOSE_get_trust(const X509_PURPOSE *xp);
int X509_PURPOSE_set(int *p, int purpose);

πŸŽ€ DESCRIPTION πŸŽ€

X509_check_purpose()</p> is the main character here. It checks if certificate x was created with the purpose represented by id. πŸ¦‹ And if ca is nonzero? Then certificate x gets checked to determine if it's a possible CA β€” with various levels of certainty possibly returned (yes, various levels, we love a nuanced queen).

🚨 bestie pls note: the certificate x must be a complete certificate, otherwise the function returns an error. no half-finished certs in this house.

Below are the potential ID's that can be checked:

# define X509_PURPOSE_SSL_CLIENT        1
# define X509_PURPOSE_SSL_SERVER        2
# define X509_PURPOSE_NS_SSL_SERVER     3
# define X509_PURPOSE_SMIME_SIGN        4
# define X509_PURPOSE_SMIME_ENCRYPT     5
# define X509_PURPOSE_CRL_SIGN          6
# define X509_PURPOSE_ANY               7
# define X509_PURPOSE_OCSP_HELPER       8
# define X509_PURPOSE_TIMESTAMP_SIGN    9
# define X509_PURPOSE_CODE_SIGN        10

The checks performed take into account the X.509 extensions keyUsage, extendedKeyUsage, and basicConstraints. πŸ‘‘

And the moment you've all been waiting for: no actual check is performed if id is -1. she simply does not check. periodt.

X509_PURPOSE_get_count() returns the number of currently defined purposes. counting the girlies πŸ“Š

X509_PURPOSE_get_unused_id() returns the smallest purpose id not yet used, which is guaranteed to be unique and larger than X509_PURPOSE_MAX. The libctx parameter should be used to provide the library context. It is currently ignored as the purpose mapping table is global. (yes bestie, it's giving "we're accepting the parameter for the vibes" πŸ™ˆ)

X509_PURPOSE_get_by_sname() returns the index of the purpose with the given short name, or -1 if not found.

X509_PURPOSE_get_by_id() returns the index of the purpose with the given id, or -1 if not found.

X509_PURPOSE_add() adds or modifies a purpose entry identified by sname. Unless the id stays the same for an existing entry, id must be fresh β€” which you can achieve by using the result of X509_PURPOSE_get_unused_id(). ✨

The function also sets in the entry:

  • the trust id trust
  • the given flags
  • the purpose (long) name name
  • the short name sname
  • the purpose checking function ck of type int (*) (const X509_PURPOSE *, const X509 *, int)
  • and its user data arg, which may be retrieved via the X509_PURPOSE pointer

X509_PURPOSE_cleanup() removes all purposes that are not pre-defined. bye bye custom purposes 🧹

X509_PURPOSE_get0() returns an X509_PURPOSE pointer or NULL on error.

X509_PURPOSE_get_id() returns the id of the given X509_PURPOSE structure.

X509_PURPOSE_get0_name() returns the (long) name of the given X509_PURPOSE.

X509_PURPOSE_get0_sname() returns the short name of the given X509_PURPOSE.

X509_PURPOSE_get_trust() returns the trust id of the given X509_PURPOSE.

X509_PURPOSE_set() assigns the given purpose id to the location pointed at by p. This resets to the any purpose if purpose is X509_PURPOSE_DEFAULT_ANY. πŸ”


πŸ“ NOTES πŸ“

X509_check_purpose() fills in various internal fields of the certificate structure x. This is safe despite the x argument's type change to const X509 * as of OpenSSL 4.0. so don't let the const jump-scare you, she's fine πŸ’…


πŸ”₯ RETURN VALUES πŸ”₯

X509_check_purpose() returns the following values.

For non-CA checks:

  • -1 β€” an error condition has occurred 😭
  • 1 β€” the certificate was created to perform the purpose represented by id
  • 0 β€” the certificate was not created to perform the purpose represented by id

For CA checks, the below integers could be returned, with the following meanings:

  • -1 β€” an error condition has occurred
  • 0 β€” not a CA or does not have the purpose represented by id
  • 1 β€” is a CA.
  • 2 β€” Only possible in old versions of openSSL when basicConstraints are absent. New versions will not return this value. May be a CA
  • 3 β€” basicConstraints absent but self signed V1.
  • 4 β€” basicConstraints absent but keyUsage present and keyCertSign asserted.
  • 5 β€” legacy Netscape specific CA Flags present

X509_PURPOSE_get_count() returns the number of currently defined purposes.

X509_PURPOSE_get_unused_id() returns the smallest purpose id not yet used.

X509_PURPOSE_get_by_sname() returns the index of the purpose with the given short name, or -1 if not found.

X509_PURPOSE_get_by_id() returns the index of the purpose with the given id, or -1 if not found.

int X509_PURPOSE_add() returns 1 on success, 0 on error.

X509_PURPOSE_cleanup() does not return anything. (she's void-coded, silent, mysterious 🫢)

X509_PURPOSE_get0() returns an X509_PURPOSE pointer or NULL on error.

X509_PURPOSE_get_id() returns the id of the given X509_PURPOSE structure.

X509_PURPOSE_get0_name() returns the (long) name of the given X509_PURPOSE.

X509_PURPOSE_get0_sname() returns the short name of the given X509_PURPOSE.

X509_PURPOSE_get_trust() returns the trust id of the given X509_PURPOSE.

X509_PURPOSE_set() returns 1 on success, 0 on error.


πŸ’€ BUGS πŸ’€

ngl, this one's important. The X509_PURPOSE implementation so far is not thread-safe. There may be race conditions retrieving purpose information while X509_PURPOSE_add() or X509_PURPOSE_cleanup(void) is being called.

😭 so like, please don't add or clean up purposes while another thread is reading them. she is NOT built for that yet. protect your threads bestie.

🌸 HISTORY 🌸

  • X509_PURPOSE_get_unused_id() was added in OpensSL 3.5.
  • X509_check_purpose() had its X509 parameter converted to const in OpenSSL 4.0.0.

Copyright 2019-2026 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 whole page bestie 🫢 this was version 4.0.3, dated 2026-09-29, living its best life in section 3SSL as X509_CHECK_PURPOSE β€” ily, go check those purposes responsibly βœ¨πŸŽ€πŸ’–

πŸ“¦ where this page comes from