21#include <openssl/err.h>
22#include <openssl/ssl.h>
23#include <openssl/evp.h>
24#include <openssl/md5.h>
25#include <openssl/sha.h>
26#include <openssl/bio.h>
27#include <openssl/obj_mac.h>
28#include <openssl/x509.h>
29#include <openssl/x509v3.h>
30#include <openssl/x509_vfy.h>
44void __attribute__((format(printf, 5, 6)))
54 fp = open_memstream(&buffer, &length);
61 size_t fmt_len = strlen(fmt);
62 if (fmt[fmt_len - 1] ==
'\n') {
64 tmp_fmt[fmt_len - 1] =
'\0';
68 vfprintf(fp, fmt, ap);
70 ERR_print_errors_fp(fp);
74 ast_log(level,
file, line, function,
"%s\n", buffer);
81 const char *long_name)
87 ast_log(
LOG_ERROR,
"One or more of oid, short_name or long_name are NULL or empty\n");
91 nid = OBJ_sn2nid(short_name);
92 if (nid != NID_undef) {
97 nid = OBJ_create(oid, short_name, long_name);
98 if (nid == NID_undef) {
108 int nid,
const char *short_name)
114 nid = OBJ_sn2nid(short_name);
115 if (nid == NID_undef) {
120 const char *
tmp = OBJ_nid2sn(nid);
127 ex_idx = X509_get_ext_by_NID(cert, nid, -1);
132 ex = X509_get_ext(cert, ex_idx);
138 return X509_EXTENSION_get_data(ex);
143 EVP_PKEY *key =
NULL;
151 fp = fopen(filename,
"r");
168 X509_CRL *crl =
NULL;
175 fp = fopen(filename,
"r");
181 crl = PEM_read_X509_CRL(fp, &crl,
NULL,
NULL);
199 fp = fopen(filename,
"r");
205 cert = PEM_read_X509(fp, &cert,
NULL,
NULL);
223 bio = BIO_new_mem_buf(buffer, size);
239 EVP_PKEY *key =
NULL;
246 bio = BIO_new_mem_buf(buffer, size);
278 raw_key_len = BIO_get_mem_data(bio, &temp_ptr);
279 if (raw_key_len <= 0) {
288 memcpy(*buffer, temp_ptr, raw_key_len);
297 bio = BIO_new(BIO_s_mem());
299 if (!bio || (PEM_write_bio_PUBKEY(bio, key) <= 0)) {
308 unsigned char **buffer)
310 RAII_VAR(EVP_PKEY *, public_key, X509_get_pubkey(cert), EVP_PKEY_free);
324 bio = BIO_new(BIO_s_mem());
326 if (!bio || (PEM_write_bio_PrivateKey(bio, key,
NULL,
NULL, 0,
NULL,
NULL) <= 0)) {
374 X509_STORE_free(store->
certs);
379 if (store->untrusted_stack) {
380 sk_X509_free(store->untrusted_stack);
383 X509_STORE_free(store->
crls);
385 if (store->crl_stack) {
386 sk_X509_CRL_free(store->crl_stack);
398 store->
certs = X509_STORE_new();
411 store->untrusted_stack = sk_X509_new_null();
412 if (!store->untrusted_stack) {
418 store->
crls = X509_STORE_new();
424 store->crl_stack = sk_X509_CRL_new_null();
425 if (!store->crl_stack) {
448 rc = X509_STORE_add_cert(store, cert);
472 rc = X509_STORE_add_crl(store, crl);
487static int pem_file_cb(
const char *dir_name,
const char *filename,
void *obj)
490 char *filename_merged =
NULL;
494 if (
ast_asprintf(&filename_merged,
"%s/%s", dir_name, filename) < 0) {
498 if (lstat(filename_merged, &statbuf)) {
499 printf(
"Error reading path stats - %s: %s\n",
500 filename_merged, strerror(
errno));
506 if (!S_ISLNK(statbuf.st_mode)) {
583 STACK_OF(X509_OBJECT) *objs =
NULL;
609 objs = X509_STORE_get0_objects(
store->untrusted);
610 count = sk_X509_OBJECT_num(objs);
611 for (i = 0; i < count ; i++) {
612 X509_OBJECT *o = sk_X509_OBJECT_value(objs, i);
613 if (X509_OBJECT_get_type(o) == X509_LU_X509) {
614 X509 *
c = X509_OBJECT_get0_X509(o);
615 sk_X509_push(
store->untrusted_stack,
c);
626 STACK_OF(X509_OBJECT) *objs =
NULL;
652 objs = X509_STORE_get0_objects(
store->crls);
653 count = sk_X509_OBJECT_num(objs);
654 for (i = 0; i < count ; i++) {
655 X509_OBJECT *o = sk_X509_OBJECT_value(objs, i);
656 if (X509_OBJECT_get_type(o) == X509_LU_CRL) {
657 X509_CRL *
c = X509_OBJECT_get0_X509_CRL(o);
658 sk_X509_CRL_push(
store->crl_stack,
c);
667#if (OPENSSL_VERSION_NUMBER >= 0x10100000L)
668 STACK_OF(X509_OBJECT) *objs =
NULL;
670 int untrusted_count = 0;
678 objs = X509_STORE_get0_objects(
store->certs);
679 count = sk_X509_OBJECT_num(objs);
681 for (i = 0; i < count ; i++) {
682 X509_OBJECT *o = sk_X509_OBJECT_value(objs, i);
683 if (X509_OBJECT_get_type(o) == X509_LU_X509) {
684 X509 *
c = X509_OBJECT_get0_X509(o);
685 X509_NAME_oneline(X509_get_subject_name(
c), subj, 1024);
686 ast_cli(fd,
"Cert: %s\n", subj);
697 untrusted_count = sk_X509_num(
store->untrusted_stack);
698 for (i = 0; i < untrusted_count ; i++) {
699 X509 *
c = sk_X509_value(
store->untrusted_stack, i);
700 X509_NAME_oneline(X509_get_subject_name(
c), subj, 1024);
701 ast_cli(fd,
"Untrusted: %s\n", subj);
707 crl_count = sk_X509_CRL_num(
store->crl_stack);
708 for (i = 0; i < crl_count ; i++) {
709 X509_CRL *crl = sk_X509_CRL_value(
store->crl_stack, i);
710 X509_NAME_oneline(X509_CRL_get_issuer(crl), subj, 1024);
711 ast_cli(fd,
"CRL: %s\n", subj);
714 return count + untrusted_count + crl_count;
716 ast_cli(fd,
"This command is not supported until OpenSSL 1.1.0\n");
723 ASN1_STRING *notbefore;
724 ASN1_STRING *notafter;
727 reftime = time(
NULL);
729 notbefore = X509_get_notBefore(cert);
730 notafter = X509_get_notAfter(cert);
731 if (!notbefore || !notafter) {
732 ast_log(
LOG_ERROR,
"Either notbefore or notafter were not present in the cert\n");
736 return (X509_cmp_time(notbefore, &reftime) < 0 &&
737 X509_cmp_time(notafter, &reftime) > 0);
742 X509_STORE_CTX *verify_ctx =
NULL;
745 if (!(verify_ctx = X509_STORE_CTX_new())) {
750 if (X509_STORE_CTX_init(verify_ctx,
store->certs, cert,
store->untrusted_stack) != 1) {
751 X509_STORE_CTX_cleanup(verify_ctx);
752 X509_STORE_CTX_free(verify_ctx);
756 X509_STORE_CTX_set0_crls(verify_ctx,
store->crl_stack);
758 rc = X509_verify_cert(verify_ctx);
759 if (rc != 1 && err_msg !=
NULL) {
760 int err = X509_STORE_CTX_get_error(verify_ctx);
761 *err_msg = X509_verify_cert_error_string(err);
763 X509_STORE_CTX_cleanup(verify_ctx);
764 X509_STORE_CTX_free(verify_ctx);
769#define SECS_PER_DAY 86400
774 time_t rt = time(
NULL);
776 if (!ASN1_TIME_diff(&pday, &psec,
NULL, at)) {
791 char *search_buff =
NULL;
793 size_t search_len = 0;
802 unsigned long flags =
803 short_name ? XN_FLAG_FN_SN | XN_FLAG_SEP_MULTILINE : XN_FLAG_ONELINE;
804 FILE *fp = open_memstream(&buffer, &
len);
805 BIO *bio = fp ? BIO_new_fp(fp, BIO_CLOSE) :
NULL;
806 X509_NAME *subject = X509_get_subject_name(cert);
809 if (!fp || !bio || !subject) {
813 rc = X509_NAME_print_ex(bio, subject, 0, flags);
827 search_len = strlen(short_name) + 1;
829 if (rc != search_len) {
833 search_buff = buffer;
836 rtn =
ast_malloc(strlen(line) - search_len + 1);
838 strcpy(rtn, line + search_len);
Asterisk main include file. File version handling, generic pbx functions.
void ast_std_free(void *ptr)
#define ast_strdupa(s)
duplicate a string in memory from the stack
#define ast_asprintf(ret, fmt,...)
A wrapper for asprintf()
#define ast_malloc(len)
A wrapper for malloc()
#define ao2_ref(o, delta)
Reference/unreference an object and return the old refcount.
#define ao2_alloc(data_size, destructor_fn)
Standard Command Line Interface.
void ast_cli(int fd, const char *fmt,...)
X509 * crypto_load_cert_from_memory(const char *buffer, size_t size)
Load an X509 Cert from a NULL terminated buffer.
static EVP_PKEY * load_private_key_from_memory(const char *buffer, size_t size)
int crypto_load_crl_store(struct crypto_cert_store *store, const char *file, const char *path)
Load an X509 Store with certificate revocation lists.
static void crypto_cert_store_destructor(void *obj)
int crypto_is_cert_trusted(struct crypto_cert_store *store, X509 *cert, const char **err_msg)
Check if the cert is trusted.
static int pem_file_cb(const char *dir_name, const char *filename, void *obj)
static int _crypto_load_cert_store(X509_STORE *store, const char *file, const char *path)
static int crypto_load_store_from_crl_file(X509_STORE *store, const char *file)
void crypto_log_openssl(int level, char *file, int line, const char *function, const char *fmt,...)
Print a log message with any OpenSSL errors appended.
int crypto_get_raw_pubkey_from_cert(X509 *cert, unsigned char **buffer)
Retrieve RAW public key from cert.
int crypto_extract_raw_pubkey(EVP_PKEY *key, unsigned char **buffer)
Extract raw public key from EVP_PKEY.
time_t crypto_asn_time_as_time_t(ASN1_TIME *at)
Return a time_t for an ASN1_TIME.
X509_CRL * crypto_load_crl_from_file(const char *filename)
Load an X509 CRL from a PEM file.
EVP_PKEY * crypto_load_privkey_from_file(const char *filename)
Load a private key from a file.
int crypto_register_x509_extension(const char *oid, const char *short_name, const char *long_name)
Register a certificate extension to openssl.
int crypto_load_untrusted_cert_store(struct crypto_cert_store *store, const char *file, const char *path)
Load an X509 Store with untrusted certificates.
int crypto_extract_raw_privkey(EVP_PKEY *key, unsigned char **buffer)
Extract raw private key from EVP_PKEY.
int crypto_load(void)
Initialize the crypto utils.
char * crypto_get_cert_subject(X509 *cert, const char *short_name)
Returns the Subject (or component of Subject) from a certificate.
int crypto_is_cert_time_valid(X509 *cert, time_t reftime)
Check if the reftime is within the cert's valid dates.
static int crypto_load_store_from_cert_file(X509_STORE *store, const char *file)
struct crypto_cert_store * crypto_create_cert_store(void)
Create an empty X509 store.
static int dump_mem_bio(BIO *bio, unsigned char **buffer)
int crypto_has_private_key_from_memory(const char *buffer, size_t size)
Check if the supplied buffer has a private key.
int crypto_unload(void)
Clean up the crypto utils.
static int _crypto_load_crl_store(X509_STORE *store, const char *file, const char *path)
X509 * crypto_load_cert_from_file(const char *filename)
Load an X509 Cert from a file.
EVP_PKEY * crypto_load_private_key_from_memory(const char *buffer, size_t size)
Load a private key from memory.
int crypto_show_cli_store(struct crypto_cert_store *store, int fd)
Dump a cert store to the asterisk CLI.
ASN1_OCTET_STRING * crypto_get_cert_extension_data(X509 *cert, int nid, const char *short_name)
Return the data from a specific extension in a cert.
int crypto_load_cert_store(struct crypto_cert_store *store, const char *file, const char *path)
Load an X509 Store with either certificates or CRLs.
Generic File Format Support. Should be included by clients of the file handling routines....
int ast_file_read_dirs(const char *dir_name, ast_file_on_file on_file, void *obj, int max_depth)
Recursively iterate through files and directories up to max_depth.
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
Support for logging to various files, console and syslog Configuration in file logger....
Asterisk module definitions.
@ AST_MODULE_LOAD_SUCCESS
static force_inline int attribute_pure ast_strlen_zero(const char *s)
char * ast_read_line_from_buffer(char **buffer)
Read lines from a string buffer.
static int force_inline attribute_pure ast_begins_with(const char *str, const char *prefix)
Checks whether a string begins with another.
ao2 object wrapper for X509_STORE that provides locking and refcounting
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Vector container support.