Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
n_x509.c File Reference

X.509 helpers implementation: CA generation and per-host leaf minting. More...

#include "nilorea/n_x509.h"
#include "nilorea/n_common.h"
#include "nilorea/n_log.h"
#include <arpa/inet.h>
#include <string.h>
#include <openssl/bn.h>
#include <openssl/err.h>
#include <openssl/evp.h>
#include <openssl/pem.h>
#include <openssl/rsa.h>
#include <openssl/x509.h>
#include <openssl/x509v3.h>
Include dependency graph for n_x509.c:

Go to the source code of this file.

Macros

#define BN_RAND_BOTTOM_ANY   0
 BN_rand(): leave the least significant bit unconstrained.
#define BN_RAND_TOP_ANY   (-1)
 BN_rand(): leave the most significant bit unconstrained.
#define N_X509_EXT_VALUE(v)
 pre-1.1.0 X509V3_EXT_conf_nid() declares value as char*, it does not write to it
#define N_X509_INIT()
 populate the EVP algorithm table once, no-op from 1.1.0 on
#define N_X509_KEY_BITS   2048
 RSA modulus size in bits used for the CA and leaf keys.
#define N_X509_SAN_BUF   300
 buffer size for a subjectAltName value string
#define N_X509_SERIAL_BITS   64
 random serial number size in bits
#define X509_getm_notAfter(x)
 pre-1.1.0 spelling of the mutable notAfter accessor
#define X509_getm_notBefore(x)
 pre-1.1.0 spelling of the mutable notBefore accessor

Functions

static int add_ext (X509 *issuer, X509 *subject, int nid, const char *value)
static int bio_to_nstr (BIO *bio, N_STR **out)
static EVP_PKEY * gen_rsa (int bits)
static int host_is_ip (const char *host)
int n_x509_generate_ca (const char *cn, int days, N_STR **ca_cert_pem, N_STR **ca_key_pem)
 Generate a self-signed certificate authority (CA) keypair and cert.
static void n_x509_init_openssl (void)
int n_x509_keypair_pem (int bits, N_STR **key_pem)
 Generate an RSA private key and return it as a PEM string.
int n_x509_mint_host_cert (const char *host, const N_STR *ca_cert_pem, const N_STR *ca_key_pem, int days, N_STR **leaf_cert_pem, N_STR **leaf_key_pem)
 Mint a per-host leaf certificate signed by the given CA.
static int pkey_to_pem (EVP_PKEY *pkey, N_STR **out)
static int set_cn (X509_NAME *name, const char *cn)
static int set_random_serial (X509 *x)
static int x509_to_pem (X509 *x, N_STR **out)

Variables

static pthread_once_t n_x509_once = PTHREAD_ONCE_INIT

Detailed Description

X.509 helpers implementation: CA generation and per-host leaf minting.

Author
Castagnier Mickael
Version
1.0

Definition in file n_x509.c.

Macro Definition Documentation

◆ BN_RAND_BOTTOM_ANY

#define BN_RAND_BOTTOM_ANY   0

BN_rand(): leave the least significant bit unconstrained.

Definition at line 64 of file n_x509.c.

Referenced by set_random_serial().

◆ BN_RAND_TOP_ANY

#define BN_RAND_TOP_ANY   (-1)

BN_rand(): leave the most significant bit unconstrained.

Definition at line 60 of file n_x509.c.

Referenced by set_random_serial().

◆ N_X509_EXT_VALUE

#define N_X509_EXT_VALUE ( v)
Value:
((char*)(uintptr_t)(v))

pre-1.1.0 X509V3_EXT_conf_nid() declares value as char*, it does not write to it

Definition at line 75 of file n_x509.c.

Referenced by add_ext().

◆ N_X509_INIT

#define N_X509_INIT ( )
Value:
((void)pthread_once(&n_x509_once, n_x509_init_openssl))
static void n_x509_init_openssl(void)
Definition n_x509.c:92
static pthread_once_t n_x509_once
Definition n_x509.c:90

populate the EVP algorithm table once, no-op from 1.1.0 on

Definition at line 98 of file n_x509.c.

Referenced by n_x509_generate_ca(), n_x509_keypair_pem(), and n_x509_mint_host_cert().

◆ N_X509_KEY_BITS

#define N_X509_KEY_BITS   2048

RSA modulus size in bits used for the CA and leaf keys.

Definition at line 105 of file n_x509.c.

Referenced by n_x509_generate_ca(), n_x509_keypair_pem(), and n_x509_mint_host_cert().

◆ N_X509_SAN_BUF

#define N_X509_SAN_BUF   300

buffer size for a subjectAltName value string

Definition at line 109 of file n_x509.c.

Referenced by n_x509_mint_host_cert().

◆ N_X509_SERIAL_BITS

#define N_X509_SERIAL_BITS   64

random serial number size in bits

Definition at line 107 of file n_x509.c.

Referenced by set_random_serial().

◆ X509_getm_notAfter

#define X509_getm_notAfter ( x)
Value:
X509_get_notAfter(x)

pre-1.1.0 spelling of the mutable notAfter accessor

Definition at line 72 of file n_x509.c.

Referenced by n_x509_generate_ca(), and n_x509_mint_host_cert().

◆ X509_getm_notBefore

#define X509_getm_notBefore ( x)
Value:
X509_get_notBefore(x)

pre-1.1.0 spelling of the mutable notBefore accessor

Definition at line 68 of file n_x509.c.

Referenced by n_x509_generate_ca(), and n_x509_mint_host_cert().

Function Documentation

◆ add_ext()

int add_ext ( X509 * issuer,
X509 * subject,
int nid,
const char * value )
static

Definition at line 164 of file n_x509.c.

References N_X509_EXT_VALUE.

Referenced by n_x509_generate_ca(), and n_x509_mint_host_cert().

Here is the caller graph for this function:

◆ bio_to_nstr()

int bio_to_nstr ( BIO * bio,
N_STR ** out )
static

Definition at line 113 of file n_x509.c.

References char_to_nstr_ex().

Referenced by pkey_to_pem(), and x509_to_pem().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ gen_rsa()

EVP_PKEY * gen_rsa ( int bits)
static

Definition at line 198 of file n_x509.c.

Referenced by n_x509_generate_ca(), n_x509_keypair_pem(), and n_x509_mint_host_cert().

Here is the caller graph for this function:

◆ host_is_ip()

int host_is_ip ( const char * host)
static

Definition at line 187 of file n_x509.c.

Referenced by n_x509_mint_host_cert().

Here is the caller graph for this function:

◆ n_x509_init_openssl()

void n_x509_init_openssl ( void )
static

Definition at line 92 of file n_x509.c.

◆ pkey_to_pem()

int pkey_to_pem ( EVP_PKEY * pkey,
N_STR ** out )
static

Definition at line 125 of file n_x509.c.

References bio_to_nstr().

Referenced by n_x509_generate_ca(), n_x509_keypair_pem(), and n_x509_mint_host_cert().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ set_cn()

int set_cn ( X509_NAME * name,
const char * cn )
static

Definition at line 180 of file n_x509.c.

Referenced by n_x509_generate_ca(), and n_x509_mint_host_cert().

Here is the caller graph for this function:

◆ set_random_serial()

int set_random_serial ( X509 * x)
static

Definition at line 149 of file n_x509.c.

References BN_RAND_BOTTOM_ANY, BN_RAND_TOP_ANY, and N_X509_SERIAL_BITS.

Referenced by n_x509_generate_ca(), and n_x509_mint_host_cert().

Here is the caller graph for this function:

◆ x509_to_pem()

int x509_to_pem ( X509 * x,
N_STR ** out )
static

Definition at line 137 of file n_x509.c.

References bio_to_nstr().

Referenced by n_x509_generate_ca(), and n_x509_mint_host_cert().

Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ n_x509_once

pthread_once_t n_x509_once = PTHREAD_ONCE_INIT
static

Definition at line 90 of file n_x509.c.