Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
ex_x509.c

Nilorea Library n_x509 CA generation and per-host leaf minting test.

Nilorea Library n_x509 CA generation and per-host leaf minting test

Author
Castagnier Mickael
Version
1.0
/*
* Nilorea Library
* Copyright (C) 2005-2026 Castagnier Mickael
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "nilorea/n_log.h"
#include "nilorea/n_str.h"
#include "nilorea/n_x509.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <openssl/pem.h>
#include <openssl/x509.h>
#include <openssl/x509_vfy.h>
/* Parse the -V LOG_LEVEL verbosity argument. */
void process_args(int argc, char** argv) {
int opt = 0;
while ((opt = getopt(argc, argv, "hvV:")) != EOF) {
switch (opt) {
case 'V':
if (!strncmp("LOG_NULL", optarg, 8))
else if (!strncmp("LOG_NOTICE", optarg, 10))
else if (!strncmp("LOG_INFO", optarg, 8))
else if (!strncmp("LOG_ERR", optarg, 7))
else if (!strncmp("LOG_DEBUG", optarg, 9))
else {
fprintf(stderr, "Unknown log level %s\n", optarg);
exit(1);
}
break;
case 'v':
fprintf(stderr, "ex_x509\n");
exit(1);
case 'h':
default:
fprintf(stderr, "usage: %s [-V LOG_LEVEL]\n", argv[0]);
break;
}
}
}
/* Verify that leaf_pem chains to ca_pem. Returns 1 on success, 0 otherwise. */
static int verify_chain(const N_STR* ca_pem, const N_STR* leaf_pem) {
BIO* bca = BIO_new_mem_buf(ca_pem->data, (int)ca_pem->written);
BIO* bleaf = BIO_new_mem_buf(leaf_pem->data, (int)leaf_pem->written);
X509* ca = bca ? PEM_read_bio_X509(bca, NULL, NULL, NULL) : NULL;
X509* leaf = bleaf ? PEM_read_bio_X509(bleaf, NULL, NULL, NULL) : NULL;
X509_STORE* store = X509_STORE_new();
X509_STORE_CTX* ctx = X509_STORE_CTX_new();
int ok = 0;
if (ca && leaf && store && ctx) {
X509_STORE_add_cert(store, ca);
if (X509_STORE_CTX_init(ctx, store, leaf, NULL) == 1)
ok = (X509_verify_cert(ctx) == 1);
}
if (ctx)
X509_STORE_CTX_free(ctx);
if (store)
X509_STORE_free(store);
if (leaf)
X509_free(leaf);
if (ca)
X509_free(ca);
if (bleaf)
BIO_free(bleaf);
if (bca)
BIO_free(bca);
return ok;
}
int main(int argc, char** argv) {
N_STR* ca_cert = NULL;
N_STR* ca_key = NULL;
N_STR* key = NULL;
const char* hosts[] = {"example.com", "127.0.0.1"};
int failures = 0;
size_t i;
process_args(argc, argv);
if (n_x509_generate_ca("Nilorea Test CA", 3650, &ca_cert, &ca_key) != 0 || !ca_cert || !ca_key) {
n_log(LOG_ERR, "CA generation failed");
} else {
n_log(LOG_NOTICE, "generated CA: cert %zu bytes, key %zu bytes", (size_t)ca_cert->written, (size_t)ca_key->written);
for (i = 0; i < sizeof(hosts) / sizeof(hosts[0]); i++) {
N_STR* leaf_cert = NULL;
N_STR* leaf_key = NULL;
if (n_x509_mint_host_cert(hosts[i], ca_cert, ca_key, 825, &leaf_cert, &leaf_key) != 0 || !leaf_cert || !leaf_key) {
n_log(LOG_ERR, "minting leaf for %s failed", hosts[i]);
} else if (!verify_chain(ca_cert, leaf_cert)) {
n_log(LOG_ERR, "leaf for %s does not verify against the CA", hosts[i]);
} else {
n_log(LOG_NOTICE, "minted and verified leaf for %s (%zu bytes)", hosts[i], (size_t)leaf_cert->written);
}
free_nstr(&leaf_cert);
free_nstr(&leaf_key);
}
}
if (n_x509_keypair_pem(2048, &key) != 0 || !key) {
n_log(LOG_ERR, "standalone keypair generation failed");
} else {
n_log(LOG_NOTICE, "generated standalone key: %zu bytes", (size_t)key->written);
}
free_nstr(&ca_cert);
free_nstr(&ca_key);
if (failures) {
n_log(LOG_ERR, "ex_x509: %d failure(s)", failures);
return 1;
}
n_log(LOG_NOTICE, "ex_x509: all checks passed");
return 0;
}
static int failures
int main(void)
void process_args(int argc, char **argv)
Definition ex_common.c:48
char * key
static int verify_chain(const N_STR *ca_pem, const N_STR *leaf_pem)
Definition ex_x509.c:73
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
Definition n_log.h:89
#define LOG_DEBUG
debug-level messages
Definition n_log.h:84
#define LOG_ERR
error conditions
Definition n_log.h:76
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL )
Definition n_log.c:121
#define LOG_NOTICE
normal but significant condition
Definition n_log.h:80
#define LOG_NULL
no log output
Definition n_log.h:46
#define LOG_INFO
informational
Definition n_log.h:82
size_t written
number of meaningful bytes in data, excluding the null terminator; the size including the null termin...
Definition n_str.h:68
char * data
the string
Definition n_str.h:63
#define free_nstr(__ptr)
free a N_STR structure and set the pointer to NULL
Definition n_str.h:203
A box including a string and his lenght.
Definition n_str.h:61
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.
Definition n_x509.c:171
int n_x509_keypair_pem(int bits, N_STR **key_pem)
Generate an RSA private key and return it as a PEM string.
Definition n_x509.c:155
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.
Definition n_x509.c:229
Generic log system.
N_STR and string function declaration.
X.509 helpers: self-signed CA generation and per-host leaf minting.