myMPD
Internal API documentation
Loading...
Searching...
No Matches
cert.c File Reference

Certificate handling. More...

#include "compile_time.h"
#include "src/lib/cert.h"
#include "src/lib/filehandler.h"
#include "src/lib/list.h"
#include "src/lib/log.h"
#include "src/lib/sds_extras.h"
#include <arpa/inet.h>
#include <errno.h>
#include <ifaddrs.h>
#include <netdb.h>
#include <netinet/in.h>
#include <openssl/bn.h>
#include <openssl/conf.h>
#include <openssl/ec.h>
#include <openssl/err.h>
#include <openssl/evp.h>
#include <openssl/opensslv.h>
#include <openssl/pem.h>
#include <openssl/rand.h>
#include <openssl/rsa.h>
#include <openssl/x509v3.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
Include dependency graph for cert.c:

Enumerations

enum  expire_check_rcs { CERT_EXPIRE_ERROR = -1 , CERT_EXPIRE_OK = 0 , CERT_EXPIRE_RENEW = 1 }
 

Functions

static sds print_asn1_time (sds buffer, const ASN1_TIME *time)
 
static sds print_x509_subject (sds buffer, X509 *cert)
 
static bool certificates_create (sds dir, sds custom_san)
 
static void push_san (struct t_list *san_list, const char *san)
 
static sds get_san (sds buffer)
 
static bool generate_set_random_serial (X509 *cert)
 
static X509_REQ * generate_request (EVP_PKEY *pkey)
 
static void add_extension (X509V3_CTX *ctx, X509 *cert, int nid, const char *value)
 
static X509 * sign_certificate_request (EVP_PKEY *ca_key, X509 *ca_cert, X509_REQ *req, sds san)
 
static EVP_PKEY * generate_keypair (int key_type, unsigned key_bits)
 
static X509 * generate_selfsigned_cert (EVP_PKEY *pkey)
 
static bool write_to_disk (sds key_file, EVP_PKEY *pkey, sds cert_file, X509 *cert)
 
static bool load_certificate (sds key_file, EVP_PKEY **key, sds cert_file, X509 **cert)
 
static bool create_ca_certificate (sds cakey_file, EVP_PKEY **ca_key, sds cacert_file, X509 **ca_cert)
 
static bool create_server_certificate (sds serverkey_file, EVP_PKEY **server_key, sds servercert_file, X509 **server_cert, sds custom_san, EVP_PKEY **ca_key, X509 **ca_cert)
 
static int check_expiration (X509 *cert, sds cert_file, int min_days, int max_days)
 
static bool certificates_cleanup (sds dir, const char *name)
 
bool certificates_check (sds workdir, sds ssl_san)
 
sds certificate_get_detail (sds cert_content)
 

Detailed Description

Certificate handling.

Enumeration Type Documentation

◆ expire_check_rcs

Expiration state of certificates

Enumerator
CERT_EXPIRE_ERROR 

Error condition.

CERT_EXPIRE_OK 

Certificate is valid.

CERT_EXPIRE_RENEW 

Certificate should be renewed.

Function Documentation

◆ add_extension()

static void add_extension ( X509V3_CTX *  ctx,
X509 *  cert,
int  nid,
const char *  value 
)
static

Adds X509V3 extension to the certificate

Parameters
ctxpointer to already creates X509V3_CTX struct
certpointer to X509 struct to add the extension
nidX509V3 extension to set
valuevalue for the extension nid

◆ certificate_get_detail()

sds certificate_get_detail ( sds  cert_content)

Returns the certificate details

Parameters
cert_contentthe certificate as pem encoded sds
Returns
certificate details as newly allocated sds

◆ certificates_check()

bool certificates_check ( sds  workdir,
sds  ssl_san 
)

Public functions Creates the ssl directory, ca and cert and renews before expired

Parameters
workdirmyMPD working directory
ssl_sanAdditional subject alternative names
Returns
true on success else false

◆ certificates_cleanup()

static bool certificates_cleanup ( sds  dir,
const char *  name 
)
static

Deletes the key and certificate by name

Parameters
dirkey and certificate directory
namebasename
Returns
true on success else false

◆ certificates_create()

static bool certificates_create ( sds  dir,
sds  custom_san 
)
static

Creates the ca and cert and renews before expired

Parameters
dirkey and certificate directory
custom_sanAdditional subject alternative names
Returns
true on success else false

◆ check_expiration()

static int check_expiration ( X509 *  cert,
sds  cert_file,
int  min_days,
int  max_days 
)
static

Checks the expiration date of a certificate

Parameters
certpointer to X509 struct
cert_filefilename of the certificate
min_days
max_days
Returns
CERT_EXPIRE_OK if cert is valid gt min_days and lt max_days CERT_EXPIRE_ERROR on error reading certificate CERT_EXPIRE_RENEW cert must be renewed

◆ create_ca_certificate()

static bool create_ca_certificate ( sds  cakey_file,
EVP_PKEY **  ca_key,
sds  cacert_file,
X509 **  ca_cert 
)
static

Creates a self-signed CA certificate

Parameters
cakey_filefilename to save the key
ca_keypointer to EVP_KEY struct to populate
cacert_filefilename to save the cert
ca_certpointer to X509 struct to populate
Returns
true on success else false

◆ create_server_certificate()

static bool create_server_certificate ( sds  serverkey_file,
EVP_PKEY **  server_key,
sds  servercert_file,
X509 **  server_cert,
sds  custom_san,
EVP_PKEY **  ca_key,
X509 **  ca_cert 
)
static

Creates the server certificate and signs it with the CA

Parameters
serverkey_filefilename to save the key
server_keypointer to EVP_KEY struct to populate
servercert_filefilename to save the cert
server_certpointer to X509 struct to populate
custom_sanSAN to append
ca_keyCA key for signing
ca_certCA cert for signing
Returns
true on success else false

◆ generate_keypair()

static EVP_PKEY * generate_keypair ( int  key_type,
unsigned  key_bits 
)
static

Generates a private/public key pair

Parameters
key_typekey type: EVP_PKEY_RSA or EVP_PKEY_EC
key_bitsnumber of bits for the key
Returns
newly allocated key or NULL on error

◆ generate_request()

static X509_REQ * generate_request ( EVP_PKEY *  pkey)
static

Generates a certificate signing request

Parameters
pkeypointer to private key
Returns
certificate signing request as X509_REQ struct

◆ generate_selfsigned_cert()

static X509 * generate_selfsigned_cert ( EVP_PKEY *  pkey)
static

Generates a self-signed x509 certificate.

Parameters
pkeypointer to the private key
Returns
newly allocated cert or NULL

◆ generate_set_random_serial()

static bool generate_set_random_serial ( X509 *  cert)
static

Generates a 20 byte random serial number and sets it in the certificate.

Parameters
certpointer to X509 struct to set the serial
Returns
true on success else false

◆ get_san()

static sds get_san ( sds  buffer)
static

Gets local hostnames and ips for subject alternative names

Parameters
buffersds string to populate
Returns
pointer to buffer

◆ load_certificate()

static bool load_certificate ( sds  key_file,
EVP_PKEY **  key,
sds  cert_file,
X509 **  cert 
)
static

Loads the key and cert from the filesystem

Parameters
key_filefilename for the key
keypointer to EVP_KEY struct to populate
cert_filefilename for the cert
certpointer to X509 struct to populate
Returns
true on success, else false

◆ print_asn1_time()

static sds print_asn1_time ( sds  buffer,
const ASN1_TIME *  time 
)
static

Private functions Prints an asn1 time struct to buffer

Parameters
bufferalready allocated sds string
timeasn1 time struct to print
Returns
pointer to buffer

◆ print_x509_subject()

static sds print_x509_subject ( sds  buffer,
X509 *  cert 
)
static

Prints x509 subject to buffer

Parameters
bufferalready allocated sds string
certx509 certificate
Returns
pointer to buffer

◆ push_san()

static void push_san ( struct t_list san_list,
const char *  san 
)
static

Adds a uniq string to the san list

Parameters
san_listpointer to the san list
sanstring to add

◆ sign_certificate_request()

static X509 * sign_certificate_request ( EVP_PKEY *  ca_key,
X509 *  ca_cert,
X509_REQ *  req,
sds  san 
)
static

Signs the certificate request

Parameters
ca_keyCA key for signing
ca_certCA cert for signing
reqpointer to the certificate signing request
sanSubject Alternative Name to set
Returns
on success a pointer to allocated X509 struct else NULL

◆ write_to_disk()

static bool write_to_disk ( sds  key_file,
EVP_PKEY *  pkey,
sds  cert_file,
X509 *  cert 
)
static

Writes the private key and cert to disc

Parameters
key_filefilename to save the key
pkeypointer to the private key
cert_filefilename to save the cert
certpointer to the cert
Returns
true on success else false