39 #if OPENSSL_VERSION_NUMBER <= 0x10100000 43 if (mode & CRYPTO_LOCK) {
64 num = CRYPTO_num_locks();
71 for (i = 0; i < num; i++) {
76 #if OPENSSL_VERSION_NUMBER <= 0x10100000 90 CRYPTO_set_locking_callback(NULL);
91 for (i = 0; i < CRYPTO_num_locks(); i++) {
108 if (!strcasecmp(name,
"md5"))
return EVP_md5();
109 if (!strcasecmp(name,
"sha1"))
return EVP_sha1();
110 if (!strcasecmp(name,
"sha-1"))
return EVP_sha1();
111 if (!strcasecmp(name,
"sha-256"))
return EVP_sha256();
112 if (!strcasecmp(name,
"sha-512"))
return EVP_sha512();
116 #if defined(_MSC_VER) || (defined(__SunOS_5_10) && defined(__SUNPRO_C)) 123 char *strsep(
char **stringp,
const char *delim)
127 if (!stringp || !*stringp || !**stringp)
131 while (**stringp && !strchr(delim, **stringp))
146 char *tmp = strdup(fp->
str);
151 while ((v = strsep(&p,
":")) && (i != (
MAX_FPLEN - 1))) {
152 sscanf(v,
"%02x", (uint32_t *) &fdata[i++]);
157 i = !memcmp(fdata, fp->
data, i);
164 char *tmp = strdup(str);
169 while ((v = strsep(&p,
":")) && (i != (
MAX_FPLEN - 1))) {
170 sscanf(v,
"%02x", (uint32_t *) &fp->
data[i++]);
185 if (X509_digest(x509, evp, fp->
data, &fp->
len) != 1 || fp->
len <= 0) {
190 for (i = 0, j = 0; i < fp->
len; ++i, j += 3){
191 sprintf((
char*)&fp->
str[j], (i == (fp->
len - 1)) ?
"%.2X" :
"%.2X:", fp->
data[i]);
193 *(&fp->
str[fp->
len * 3]) =
'\0';
213 if (!(bio = BIO_new(BIO_s_file()))) {
218 if (BIO_read_filename(bio, rsa) != 1) {
223 if (!(x509 = PEM_read_bio_X509(bio, NULL, 0, NULL))) {
248 static int mkcert(X509 **x509p, EVP_PKEY **pkeyp,
int bits,
int serial,
int days);
254 EVP_PKEY *pkey = NULL;
255 char *rsa = NULL, *pvt = NULL;
262 pem = strdup(prefix);
284 #ifdef CRYPTO_MEM_CHECK_ON 285 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
290 if (!
mkcert(&x509, &pkey, 4096, 0, 36500)) {
299 if ((fp = fopen(pem,
"w"))) {
300 PEM_write_PrivateKey(fp, pkey, NULL, NULL, 0, NULL, NULL);
301 PEM_write_X509(fp, x509);
306 if (pvt && (fp = fopen(pvt,
"w"))) {
307 PEM_write_PrivateKey(fp, pkey, NULL, NULL, 0, NULL, NULL);
311 if (rsa && (fp = fopen(rsa,
"w"))) {
312 PEM_write_X509(fp, x509);
320 #ifndef OPENSSL_NO_ENGINE 323 CRYPTO_cleanup_all_ex_data();
340 char *pem = NULL, *old_pem = NULL;
342 EVP_PKEY *pkey = NULL;
357 fp = fopen(pem,
"r");
363 pkey = PEM_read_PrivateKey(fp, NULL, NULL, NULL);
371 bits = EVP_PKEY_bits(pkey);
387 if (rename(pem, old_pem) != -1) {
400 static void callback(
int p,
int n,
void *arg)
412 static int mkcert(X509 **x509p, EVP_PKEY **pkeyp,
int bits,
int serial,
int days)
416 #if OPENSSL_VERSION_NUMBER < 0x30000000 419 X509_NAME *
name=NULL;
424 if (*pkeyp == NULL) {
425 if ((pk = EVP_PKEY_new()) == NULL) {
432 if (*x509p == NULL) {
433 if ((x = X509_new()) == NULL) {
440 #if OPENSSL_VERSION_NUMBER >= 0x30000000 444 ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, NULL);
446 if ((!ctx) || (EVP_PKEY_keygen_init(ctx) <= 0) || (EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, bits) <= 0)) {
452 if (EVP_PKEY_generate(ctx, &pk) <= 0) {
457 EVP_PKEY_CTX_free(ctx);
459 #elif OPENSSL_VERSION_NUMBER >= 0x10100000 462 static const BN_ULONG ULONG_RSA_F4 = RSA_F4;
463 BIGNUM* BN_value_RSA_F4 = BN_new();
464 if (!BN_value_RSA_F4) {
468 BN_set_word(BN_value_RSA_F4,ULONG_RSA_F4);
469 RSA_generate_key_ex(rsa, bits, BN_value_RSA_F4, NULL);
470 BN_free(BN_value_RSA_F4);
473 rsa = RSA_generate_key(bits, RSA_F4, NULL, NULL);
476 #if OPENSSL_VERSION_NUMBER < 0x30000000 477 if (!EVP_PKEY_assign_RSA(pk, rsa)) {
484 X509_set_version(x, 2);
485 ASN1_INTEGER_set(X509_get_serialNumber(x), serial);
486 X509_gmtime_adj(X509_get_notBefore(x), -(
long)60*60*24*7);
487 X509_gmtime_adj(X509_get_notAfter(x), (
long)60*60*24*days);
488 X509_set_pubkey(x, pk);
490 name = X509_get_subject_name(x);
496 X509_NAME_add_entry_by_txt(name,
"C", MBSTRING_ASC, (
unsigned char *)
"US", -1, -1, 0);
497 X509_NAME_add_entry_by_txt(name,
"CN", MBSTRING_ASC, (
unsigned char *)
"FreeSWITCH", -1, -1, 0);
503 X509_set_issuer_name(x, name);
505 #if OPENSSL_VERSION_NUMBER >= 0x30000000 506 if (!X509_sign(x, pk, EVP_sha256())) {
508 if (!X509_sign(x, pk, EVP_sha1())) {
518 ERR_print_errors_fp(stdout);
#define switch_core_new_memory_pool(p)
Create a new sub memory pool from the core's master pool.
static void switch_ssl_ssl_thread_id(CRYPTO_THREADID *id)
switch_status_t switch_mutex_destroy(switch_mutex_t *lock)
#define SWITCH_CHANNEL_LOG
switch_cache_db_handle_type_t type
#define switch_core_destroy_memory_pool(p)
Returns a subpool back to the main pool.
void switch_ssl_init_ssl_locks(void)
int switch_core_gen_certs(const char *prefix)
int switch_core_cert_expand_fingerprint(dtls_fingerprint_t *fp, const char *str)
static switch_mutex_t ** ssl_mutexes
switch_status_t switch_mutex_unlock(switch_mutex_t *lock)
#define SWITCH_MUTEX_NESTED
int switch_core_cert_verify(dtls_fingerprint_t *fp)
#define SWITCH_PATH_SEPARATOR
int switch_core_cert_gen_fingerprint(const char *prefix, dtls_fingerprint_t *fp)
switch_status_t switch_mutex_lock(switch_mutex_t *lock)
void switch_ssl_destroy_ssl_locks(void)
#define switch_safe_free(it)
Free a pointer and set it to NULL unless it already is NULL.
switch_status_t switch_mutex_init(switch_mutex_t **lock, unsigned int flags, switch_memory_pool_t *pool)
switch_byte_t switch_byte_t uint32_t switch_bitpack_mode_t mode
uint8_t data[MAX_FPLEN+1]
switch_bool_t switch_core_check_dtls_pem(const char *file)
int switch_core_cert_extract_fingerprint(X509 *x509, dtls_fingerprint_t *fp)
switch_directories SWITCH_GLOBAL_dirs
struct fspr_thread_mutex_t switch_mutex_t
switch_status_t switch_file_exists(const char *filename, switch_memory_pool_t *pool)
static switch_bool_t switch_is_file_path(const char *file)
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
const char * switch_stristr(const char *instr, const char *str)
struct fspr_pool_t switch_memory_pool_t
switch_thread_id_t switch_thread_self(void)
static void switch_ssl_ssl_lock_callback(int mode, int type, char *file, int line)
static switch_memory_pool_t * ssl_pool
static int mkcert(X509 **x509p, EVP_PKEY **pkeyp, int bits, int serial, int days)
SWITCH_BEGIN_EXTERN_C char * switch_mprintf(const char *zFormat,...)
static const EVP_MD * get_evp_by_name(const char *name)