20 # include <openssl/crypto.h>
21 # include <openssl/asn1.h>
22 # include <openssl/bio.h>
23 # include <openssl/err.h>
24 # include <openssl/evp.h>
25 # include <openssl/objects.h>
26 # include <openssl/opensslv.h>
27 # include <openssl/rand.h>
28 # include <openssl/rsa.h>
29 # include <openssl/ssl.h>
30 # include <openssl/x509.h>
31 # include <openssl/x509v3.h>
32 # include <openssl/x509_vfy.h>
40 #include "fileutils.h"
101 #define MAIN_ERR_PREFIX "TLS: "
110 # define CFG_USE_TLS_WILDCARD_SUBJECT 1
116 # define TLS_CIPHERS_TLS13 "TLS_AES_256_GCM_SHA384" \
117 ":TLS_CHACHA20_POLY1305_SHA256" \
118 ":TLS_AES_128_GCM_SHA256" \
119 ":TLS_AES_128_CCM_SHA256"
153 # define TLS_CIPHERS_DEFAULT \
154 "!SSLv2:!RC2:!RC4:!DES:!3DES:!MD5:!kEECDH:!aNULL:!aDSS:!aECDSA" \
155 ":kEDH+HIGH@STRENGTH:-SSLv3:kEDH+HIGH:!AESCCM8"
172 # define TLS_CIPHERS_WEAK \
173 "!SSLv2:!RC2:!DES:!aNULL:!aDSS:!aECDSA" \
175 ":kEDH+HIGH:kEECDH+HIGH:-SSLv3:kEDH+HIGH:kEECDH+HIGH" \
176 ":-3DES:kEDH+HIGH:kEECDH+HIGH" \
178 ":AES256-GCM-SHA384:CAMELLIA256-SHA:AES128-SHA" \
201 # define TLS_SIGALGS_TLS13 \
202 "rsa_pss_rsae_sha512:rsa_pss_rsae_sha384:rsa_pss_rsae_sha256" \
204 ":ecdsa_secp521r1_sha512:ecdsa_secp256r1_sha256"
223 # define TLS_SIGALGS "RSA+SHA512:RSA+SHA384:RSA+SHA256"
226 # define TLS_ECDHE_GROUPS_TLS13 "X448:X25519:P-256"
234 # define TLS_FFDHE_GROUPS "ffdhe8192:ffdhe6144:ffdhe4096:ffdhe3072"
240 static api_posix_locale_t tls_loc_ctype_posix = 0;
241 static SSL_CTX* tls_ctx = NULL;
242 # if CFG_USE_OPENSSL_API_1_1
243 static SSL_CONF_CTX* cctx;
245 static const char* tls_certpath = NULL;
246 # if !CFG_TLS_CRLS_DISABLE
247 static const char* tls_crlpath = NULL;
248 static int tls_crl_update_skip = 0;
267 static int tls_cert_check_root(X509* cc,
int* is_root,
size_t bufsize,
268 char* buf_i,
char* buf_s)
271 BIO* biop = BIO_new(BIO_s_mem());
289 issuer = X509_get_issuer_name(cc);
292 PRINT_ERROR(
"Extraction of issuer from X.509 certificate failed");
297 X509_NAME_print_ex(biop, issuer, 0, XN_FLAG_ONELINE);
298 len = (size_t) BIO_get_mem_data(biop, &p);
299 if(!len) { res = -1; }
302 b_i = (
char*) api_posix_malloc(len + (
size_t) 1);
303 if(NULL == b_i) { res = -1; }
306 memcpy((
void*) b_i, (
void*) p, len);
316 if(1 != BIO_reset(biop)) { res = -1; }
319 subject = X509_get_subject_name(cc);
322 PRINT_ERROR(
"Extraction of subject from X.509 certificate "
328 X509_NAME_print_ex(biop, subject, 0, XN_FLAG_ONELINE);
329 len = (size_t) BIO_get_mem_data(biop, &p);
330 if(!len) { res = -1; }
333 b_s = (
char*) api_posix_malloc(len + (
size_t) 1);
334 if(NULL == b_s) { res = -1; }
337 memcpy((
void*) b_s, (
void*) p, len);
349 if(!strcmp(b_i, b_s))
362 if(bufsize - (
size_t) 1 < len) { len = bufsize - (size_t) 1; }
363 memcpy((
void*) buf_i, (
void*) b_i, len);
369 if(bufsize - (
size_t) 1 < len) { len = bufsize - (size_t) 1; }
370 memcpy((
void*) buf_s, (
void*) b_s, len);
376 api_posix_free((
void*) b_i);
377 api_posix_free((
void*) b_s);
378 if(NULL != biop) { BIO_free(biop); }
392 static int tls_get_certpath(
const char** certpath)
394 static const char certdir[] =
"certs";
409 res =
fu_create_path(*certpath, (api_posix_mode_t) API_POSIX_S_IRWXU);
416 PRINT_ERROR(
"Cannot create certificate directory");
417 api_posix_free((
void*) *certpath);
425 # if !CFG_TLS_CRLS_DISABLE
437 static int tls_check_crl_age(
void)
450 if((api_posix_time_t) 0 > ts) { res = -1; }
454 if(pts_last > pts_current)
456 PRINT_ERROR(
"Last CRL update timestamp is in the future");
461 age = pts_current - pts_last;
466 PRINT_ERROR(
"Negative CRL update interval was set to zero");
472 PRINT_ERROR(
"Too large CRL update interval was clamped");
476 if(interval < age) { res = 1; }
482 if(0 > res) {
PRINT_ERROR(
"CRL update interval check failed"); }
491 static int tls_set_crl_age(
void)
506 static int tls_reset_crl_age(
void)
524 static int tls_get_crlpath(
const char** crlpath)
526 static const char crldir[] =
"crls";
541 res =
fu_create_path(*crlpath, (api_posix_mode_t) API_POSIX_S_IRWXU);
549 api_posix_free((
void*) *crlpath);
572 static int tls_download_crl(
int cci,
const char* uri,
char** pn)
576 char filename[8] = {
'/',
'C',
'R',
'L',
'_',
'_',
'_', 0 };
577 char* pathname = NULL;
580 if(NULL == tls_crlpath)
582 PRINT_ERROR(
"Download of CRL failed, directory not available");
586 if(0 > cci || 999 < cci)
588 PRINT_ERROR(
"Download of CRL failed, invalid chain index");
592 rv = api_posix_snprintf(&filename[4], 4,
"%d", cci);
595 PRINT_ERROR(
"Download of CRL failed, filename invalid");
599 len = strlen(tls_crlpath);
600 len += strlen(filename);
601 pathname = (
char*) api_posix_malloc(++len);
604 strcpy(pathname, tls_crlpath);
605 strcat(pathname, filename);
616 api_posix_free((
void*) *pn);
634 static int tls_convert_crl_to_pem(
const char* pn_der,
char** pn_pem)
639 X509_CRL* crl = NULL;
642 char pem_cmp[] =
"-----BEGIN X509 CRL-----";
643 char pem_buf[] =
" ";
646 len = strlen(pn_der);
648 *pn_pem = (
char*) api_posix_malloc(++len);
651 PRINT_ERROR(
"Memory allocation failed for filename");
656 strcpy(*pn_pem, pn_der);
657 strcat(*pn_pem,
".pem");
664 biop = BIO_new(BIO_s_file());
667 rv = BIO_read_filename(biop, pn_der);
670 PRINT_ERROR(
"BIO error while configuring DER filename of CRL");
675 len = strlen(pem_cmp);
676 rv = BIO_read(biop, pem_buf, len);
679 if(!memcmp((
void*) pem_cmp, (
void*) pem_buf, len))
687 rv = BIO_seek(biop, 0);
694 crl = d2i_X509_CRL_bio(biop, NULL);
718 rv =
fu_open_file(*pn_pem, &fd, API_POSIX_O_WRONLY | API_POSIX_O_CREAT,
719 API_POSIX_S_IRUSR | API_POSIX_S_IWUSR);
723 biop = BIO_new(BIO_s_file());
726 rv = BIO_write_filename(biop, *pn_pem);
730 "BIO error while configuring PEM filename of CRL");
734 rv = PEM_write_bio_X509_CRL(biop, crl);
742 rv = BIO_get_fd(biop, NULL);
743 if(-1 != rv) { res =
fu_sync(rv, NULL); }
754 res = api_posix_symlink(pn_der, *pn_pem);
760 PRINT_ERROR(
"Conversion of CRL to PEM format failed");
761 api_posix_free((
void*) *pn_pem);
786 static int tls_create_hash_symlink(
const char* pn_pem, X509* cert)
790 unsigned long int issuer_hash;
792 char name_symlink[13];
793 char* pn_symlink = NULL;
797 if(NULL == tls_crlpath)
799 PRINT_ERROR(
"Creating of symlink failed, directory not available");
804 while(!abort && 9U >= i)
806 issuer_hash = X509_issuer_name_hash(cert);
808 rv = api_posix_snprintf(name_symlink, 13,
"/%08lx.r%u", issuer_hash,
812 PRINT_ERROR(
"Creating of symlink failed, name not valid");
817 len = strlen(tls_crlpath);
818 len += strlen(name_symlink);
819 pn_symlink = (
char*) api_posix_malloc(++len);
820 if(NULL == pn_symlink) { abort = 1; }
823 strcpy(pn_symlink, tls_crlpath);
824 strcat(pn_symlink, name_symlink);
825 res = api_posix_symlink(pn_pem, pn_symlink);
834 if(!(-1 == res && API_POSIX_EEXIST == api_posix_errno))
841 api_posix_free((
void*) pn_symlink);
849 PRINT_ERROR(
"Creating of symlink failed, too many duplicates");
863 static int tls_crl_get_lock(
int* fd)
866 const char crl_lockfile[] =
".crl_lock";
867 const char* lockpathname = NULL;
871 if(NULL != lockpathname)
873 rv =
fu_create_path(lockpathname, (api_posix_mode_t) API_POSIX_S_IRWXU);
881 API_POSIX_O_WRONLY | API_POSIX_O_CREAT,
882 API_POSIX_S_IRUSR | API_POSIX_S_IWUSR);
885 PRINT_ERROR(
"Cannot open mutex file for CRL directory");
891 PRINT_ERROR(
"Cannot lock mutex file for CRL directory");
902 api_posix_free((
void*) lockpathname);
914 static void tls_crl_release_lock(
int* fd)
932 static int tls_update_crls(STACK_OF(X509)* certificate_chain)
937 int crls_required = 0;
943 STACK_OF(DIST_POINT)* dp = NULL;
946 const size_t buflen = 256;
956 if(tls_crl_get_lock(&lockfd)) { res = -2; }
960 printf(
"%s: %s-----------------------------------------------------\n",
962 printf(
"%s: %sAutomatic CRL maintenance\n", CFG_NAME,
MAIN_ERR_PREFIX);
964 if(NULL != tls_crlpath)
968 if(!api_posix_mkdir(tls_crlpath,
969 (api_posix_mode_t) API_POSIX_S_IRWXU))
978 PRINT_ERROR(
"CRL update failed, directory not available");
983 ccl = sk_X509_num(certificate_chain);
984 for(cci = 0; cci < ccl; ++cci)
987 cc = sk_X509_value(certificate_chain, cci);
990 PRINT_ERROR(
"Error while parsing certificate chain");
993 rv = tls_cert_check_root(cc, &is_root, buflen, b_i, NULL);
1000 printf(
"%s: %sSelf signed server certificate:\n",
1005 printf(
"%s: %sServer certificate:\n",
1013 printf(
"%s: %sRoot certificate:\n",
1018 printf(
"%s: %sIntermediate certificate:\n",
1028 printf(
"%s: %s Always trusted if installed"
1029 " (Must be uninstalled for revocation)\n",
1035 dp = X509_get_ext_d2i(cc, NID_crl_distribution_points, NULL,
1044 num2 = sk_DIST_POINT_num(dp);
1045 for(i2 = 0; i2 < num2; ++i2)
1047 dpi = sk_DIST_POINT_value(dp, i2);
1048 if(NULL != dpi->distpoint)
1050 num = sk_GENERAL_NAME_num(
1051 dpi->distpoint->name.fullname);
1052 for(i = 0; i < num; ++i)
1054 ani = sk_GENERAL_NAME_value(
1055 dpi->distpoint->name.fullname, i);
1056 if(GEN_URI == ani->type)
1059 ani->d.uniformResourceIdentifier->data;
1061 "CRL distribution point %d: %s\n",
1066 res = tls_download_crl(cci, p, &crl_pn_der);
1070 res = tls_convert_crl_to_pem(crl_pn_der,
1075 res = tls_create_hash_symlink(crl_pn_pem,
1086 api_posix_free((
void*) crl_pn_pem);
1087 api_posix_free((
void*) crl_pn_der);
1092 if(crl_valid) {
break; }
1100 printf(
"%s: %s-----------------------------------------------------\n",
1103 tls_crl_release_lock(&lockfd);
1105 if(!res && !crls_required) { res = 1; }
1109 if(NULL != dp) { CRL_DIST_POINTS_free(dp); }
1144 static int tls_chain_check_sigalgs(STACK_OF(X509)* certificate_chain,
1152 api_posix_ssize_t rv2;
1155 EVP_PKEY* pkey = NULL;
1156 # if !CFG_USE_OPENSSL_API_3
1157 RSA* rsa_key = NULL;
1168 ccl = sk_X509_num(certificate_chain);
1169 for(cci = 0; cci < ccl; ++cci)
1172 cc = sk_X509_value(certificate_chain, cci);
1175 PRINT_ERROR(
"Error while parsing certificate chain");
1179 rv = tls_cert_check_root(cc, &is_root, 0, NULL, NULL);
1182 if(!cci) { s =
"(Server) "; }
1183 if(is_root) { s =
"(Root) "; }
1184 printf(
"%s: %sCertificate level %d %sin chain:\n",
1187 pkey = X509_extract_key(cc);
1190 PRINT_ERROR(
"Extraction of public key from X.509 certificate"
1194 # if CFG_USE_OPENSSL_API_1_1 || CFG_USE_LIBRESSL_API_3_5
1195 if(EVP_PKEY_RSA != EVP_PKEY_base_id(pkey))
1197 if(EVP_PKEY_RSA != pkey->type)
1203 PRINT_ERROR(
"Certificate doesn't use RSA key (skipped)");
1206 PRINT_ERROR(
"Certificate doesn't use RSA key (not supported)");
1209 # if !CFG_USE_OPENSSL_API_3
1210 rsa_key = EVP_PKEY_get1_RSA(pkey);
1213 PRINT_ERROR(
"Extraction of key from certificate failed");
1217 # if CFG_USE_OPENSSL_API_3
1218 rsa_mod_size = EVP_PKEY_bits(pkey);
1219 if(0 >= rsa_mod_size)
1225 rsa_mod_size = RSA_size(rsa_key);
1226 if(0 >= rsa_mod_size || 8192 < rsa_mod_size)
1233 printf(
"%s: %s RSA key modulus size: %d bit\n",
1235 if(1024 > rsa_mod_size)
1237 PRINT_ERROR(
"RSA key modulus size of certificate rejected");
1240 else if(3072 > rsa_mod_size)
1243 printf(
"%s: %s Warning: RSA key modulus should be "
1250 # if CFG_USE_OPENSSL_API_3
1251 len = i2d_PUBKEY(pkey, &buf);
1253 len = i2d_RSA_PUBKEY(rsa_key, &buf);
1257 PRINT_ERROR(
"Extraction of key fingerprint failed");
1263 rv2 = api_posix_snprintf(prefix, 80,
"%s: %s ",
1279 # if !CFG_USE_OPENSSL_API_3
1282 EVP_PKEY_free(pkey);
1284 # if CFG_USE_OPENSSL_API_1_1 || CFG_USE_LIBRESSL_API_3_5
1285 alg = X509_get_signature_nid(cc);
1287 alg = OBJ_obj2nid(cc->sig_alg->algorithm);
1289 if(NID_undef == alg)
1291 PRINT_ERROR(
"Extraction of hash algorithm failed");
1294 md_s = OBJ_nid2sn(alg);
1297 printf(
"%s: %s Self signature is not used\n",
1302 printf(
"%s: %s Signature algorithm: %s\n",
1307 case NID_md2WithRSAEncryption:
1308 case NID_md4WithRSAEncryption:
1309 case NID_md5WithRSAEncryption:
1312 PRINT_ERROR(
"Hash algorithm of signature rejected");
1315 case NID_sha1WithRSAEncryption:
1316 case NID_sha224WithRSAEncryption:
1319 printf(
"%s: %s Warning: At least SHA-256 should be "
1321 # if CFG_USE_OPENSSL_API_1_1
1335 case NID_sha256WithRSAEncryption:
1336 case NID_sha384WithRSAEncryption:
1337 case NID_sha512WithRSAEncryption:
1345 PRINT_ERROR(
" Unexpected hash algorithm rejected");
1353 if(!md_ok) {
break; }
1356 if(cci == ccl) { res = 0; }
1375 static int tls_subject_check(
const char* cn,
const char* subject)
1378 # if CFG_USE_TLS_WILDCARD_SUBJECT
1383 if(
'*' == subject[0])
1388 if(
'.' == subject[1])
1390 subject = &subject[2];
1391 p = strchr(cn, (
int)
'.');
1403 res = api_posix_strcasecmp_l(cn, subject, tls_loc_ctype_posix);
1405 if(0 < res) { res = -res; }
1408 if(res) { printf(
" (doesn't match hostname)\n"); }
1409 # if CFG_USE_TLS_WILDCARD_SUBJECT
1410 else if(wildcard) { printf(
" (matches hostname using wildcard)\n"); }
1412 else { printf(
" (matches hostname)\n"); }
1437 static int tls_create_context(
int weak)
1440 long int mode = SSL_MODE_ENABLE_PARTIAL_WRITE
1441 | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER;
1442 long int options = SSL_OP_NO_TICKET | SSL_OP_NO_COMPRESSION
1443 | SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3;
1444 unsigned long int parameters = X509_V_FLAG_X509_STRICT;
1445 X509_VERIFY_PARAM* para = NULL;
1448 const char* cpp = NULL;
1449 # if !CFG_TLS_CRLS_DISABLE
1450 X509_LOOKUP* look = NULL;
1455 parameters |= X509_V_FLAG_CRL_CHECK
1456 | X509_V_FLAG_CRL_CHECK_ALL
1457 | X509_V_FLAG_EXTENDED_CRL_SUPPORT
1458 | X509_V_FLAG_USE_DELTAS;
1466 options |= SSL_OP_NO_TLSv1;
1467 # ifdef SSL_OP_NO_TLSv1_1
1468 options |= SSL_OP_NO_TLSv1_1;
1473 tls_ctx = SSL_CTX_new(SSLv23_client_method());
1481 # if CFG_USE_OPENSSL_API_1_1
1483 SSL_CTX_set_security_level(tls_ctx, 0);
1485 f = SSL_CTX_set_mode(tls_ctx, mode);
1486 if((f & mode) != mode)
1494 f = SSL_CTX_set_options(tls_ctx, options);
1495 if((f & options) != options)
1501 # if CFG_USE_OPENSSL_API_1_1 && (OPENSSL_VERSION_NUMBER >= 0x10101000L)
1508 PRINT_ERROR(
"No usable TLSv1.3 ciphers available, "
1509 "check your OpenSSL configuration");
1519 PRINT_ERROR(
"No usable ciphers with EDH key agreement available, "
1520 "check your OpenSSL configuration");
1526 SSL_CTX_set_verify(tls_ctx, SSL_VERIFY_NONE, NULL);
1527 para = X509_VERIFY_PARAM_new();
1530 PRINT_ERROR(
"Memory allocation for X.509 parameters failed ");
1535 rv = X509_VERIFY_PARAM_set_flags(para, parameters);
1543 rv = X509_VERIFY_PARAM_set_purpose(para, X509_PURPOSE_SSL_SERVER);
1546 PRINT_ERROR(
"Cannot set X.509 verification purpose");
1552 rv = SSL_CTX_set1_param(tls_ctx, para);
1555 PRINT_ERROR(
"Cannot store X.509 parameters in TLS context");
1559 X509_VERIFY_PARAM_free(para);
1565 # if CFG_USE_OPENSSL_API_1_1 && (OPENSSL_VERSION_NUMBER >= 0x10101000L)
1566 # if CFG_USE_OPENSSL_API_3
1572 printf(
"%s: %sWorkaround for OpenSSL 1.1.1: "
1579 PRINT_ERROR(
"Cannot store DHE groups in TLS context");
1583 # if CFG_USE_LIBRESSL && (LIBRESSL_VERSION_NUMBER >= 0x30101000L)
1586 printf(
"%s: %sWorkaround for LibreSSL: "
1589 rv = SSL_CTX_set1_groups_list(tls_ctx,
"X25519:P-256");
1592 PRINT_ERROR(
"Cannot store DHE groups in TLS context");
1599 # if CFG_USE_OPENSSL_API_1_1
1603 cctx = SSL_CONF_CTX_new();
1606 PRINT_ERROR(
"Cannot create TLS configuration context");
1612 SSL_CONF_CTX_set_flags(cctx, SSL_CONF_FLAG_FILE | SSL_CONF_FLAG_CLIENT
1613 | SSL_CONF_FLAG_SHOW_ERRORS);
1615 SSL_CONF_CTX_set_ssl_ctx(cctx, tls_ctx);
1617 # if CFG_USE_OPENSSL_API_1_1 && (OPENSSL_VERSION_NUMBER >= 0x10101000L)
1625 rv = SSL_CONF_cmd(cctx,
"SignatureAlgorithms",
TLS_SIGALGS);
1629 PRINT_ERROR(
"Cannot store TLS signature algorithms");
1635 rv = SSL_CONF_CTX_finish(cctx);
1638 PRINT_ERROR(
"Cannot store configuration context");
1649 if(!res) { res = tls_get_certpath(&cpp); }
1653 printf(
"%s: %sLocation of X.509 root certificates: %s/\n",
1655 # if CFG_USE_OPENSSL_API_3
1656 rv = SSL_CTX_load_verify_dir(tls_ctx, tls_certpath);
1658 rv = SSL_CTX_load_verify_locations(tls_ctx, NULL, tls_certpath);
1662 PRINT_ERROR(
"Setup of certificate verification failed");
1672 rv = SSL_CTX_set_default_verify_paths(tls_ctx);
1675 PRINT_ERROR(
"Setup of certificate verification failed");
1681 # if !CFG_TLS_CRLS_DISABLE
1685 res = tls_get_crlpath(&cpp);
1689 printf(
"%s: %sLocation of X.509 CRLs: %s/\n",
1694 look = X509_STORE_add_lookup(SSL_CTX_get_cert_store(tls_ctx),
1695 X509_LOOKUP_hash_dir());
1698 PRINT_ERROR(
"Cannot add CRL lookup method to TLS context");
1704 rv = X509_LOOKUP_add_dir(look, tls_crlpath, X509_FILETYPE_PEM);
1714 printf(
"%s: %sWarning: X.509 certificate revocation checks disabled "
1721 printf(
"%s: %sWarning: X.509 certificate revocation checks disabled "
1734 static void tls_destroy_context(
void)
1736 # if CFG_USE_OPENSSL_API_1_1
1738 SSL_CONF_CTX_free(cctx);
1743 SSL_CTX_free(tls_ctx);
1747 api_posix_free((
void*) tls_certpath);
1748 tls_certpath = NULL;
1749 # if !CFG_TLS_CRLS_DISABLE
1750 api_posix_free((
void*) tls_crlpath);
1769 tls_loc_ctype_posix = api_posix_newlocale(API_POSIX_LC_CTYPE_MASK,
"POSIX",
1770 (api_posix_locale_t) 0);
1771 if((api_posix_locale_t) 0 == tls_loc_ctype_posix)
1783 # if !CFG_USE_OPENSSL_API_1_1
1785 SSL_load_error_strings();
1788 # if OPENSSL_VERSION_NUMBER == 0x1000000FL
1795 OpenSSL_add_all_algorithms();
1803 "check your OpenSSL configuration");
1817 if((api_posix_locale_t) 0 != tls_loc_ctype_posix)
1819 api_posix_freelocale(tls_loc_ctype_posix);
1823 # if !CFG_USE_OPENSSL_API_1_1
1867 # if CFG_USE_OPENSSL_API_1_1
1868 unsigned long int version = OpenSSL_version_num();
1870 # if CFG_USE_LIBRESSL && defined(LIBRESSL_VERSION_NUMBER)
1872 unsigned long int version = LIBRESSL_VERSION_NUMBER;
1874 unsigned long int version = SSLeay();
1877 unsigned int major = (
unsigned int) ((version & 0xF0000000UL) >> 28);
1878 unsigned int minor = (
unsigned int) ((version & 0x0FF00000UL) >> 20);
1879 unsigned int fix = (
unsigned int) ((version & 0x000FF000UL) >> 12);
1880 unsigned int patch = (
unsigned int) ((version & 0x00000FF0UL) >> 4);
1881 # if ! CFG_USE_LIBRESSL && ! CFG_USE_OPENSSL_API_3
1883 char patch_string[4] = { 0, 0, 0, 0 };
1890 patch_letter = (char) (0x60U + patch);
1892 else if(50U >= patch)
1895 patch_letter = (char) (0x60U + patch - 25U);
1896 patch_string[1] = patch_letter;
1900 else if(75U >= patch)
1903 patch_letter = (char) (0x60U + patch - 50U);
1904 patch_string[2] = patch_letter;
1906 patch_string[1] =
'z';
1915 patch_string[0] = patch_letter;
1920 # if CFG_USE_LIBRESSL && defined(LIBRESSL_VERSION_NUMBER)
1923 printf(
"%s: %sLibreSSL library version: %u.%u.%u (0x%08lX)\n",
1927 # if CFG_USE_OPENSSL_API_3
1928 printf(
"%s: %sOpenSSL library version: %u.%u.%u (0x%08lX)\n",
1931 printf(
"%s: %sOpenSSL library version: %u.%u.%u%s (0x%08lX)\n",
1937 # if CFG_USE_OPENSSL_API_1_1 && (OPENSSL_VERSION_NUMBER >= 0x10101000L) \
1938 || CFG_USE_LIBRESSL && (LIBRESSL_VERSION_NUMBER >= 0x30101000L)
1939 printf(
"%s: %sProtocol version TLSv1.3 available for negotiation\n",
1950 if(1U == major && 0U == minor)
1952 if( (1U == fix && 0xEU <= patch && 0xFU >= patch)
1953 || (2U == fix && 0x2U <= patch && 0x3U >= patch) )
1955 printf(
"%s: %sOpenSSL library possibly vulnerable to #OprahSSL!\n",
1965 if(1U == major && 0U == minor && 1U == fix && 7U > patch)
1967 printf(
"%s: %sOpenSSL library possibly vulnerable to Heartbleed!\n",
1976 if(1U == major && 0U == minor)
1978 if( (0U == fix && 0x0DU > patch) || (1U == fix && 8U > patch) )
1981 "OpenSSL library possibly vulnerable to MITM attacks!\n",
2010 const char* res = NULL;
2011 unsigned char buf[16];
2013 if(1 != api_posix_inet_pton(API_POSIX_AF_INET, sn, buf))
2016 if(1 != api_posix_inet_pton(API_POSIX_AF_INET6, sn, buf))
2081 int tls_open(
int sd,
void** co,
int weak,
const char* sni)
2087 unsigned long int error;
2088 const char* sp = NULL;
2089 const char* tlsv = NULL;
2090 const char* cp = NULL;
2091 const char* kx = NULL;
2093 # if CFG_USE_OPENSSL_API_1_1
2094 EVP_PKEY* key = NULL;
2097 # if CFG_USE_OPENSSL_API_1_1
2098 const char* dp = NULL;
2099 # if (OPENSSL_VERSION_NUMBER >= 0x10101000L)
2110 res = tls_create_context(weak);
2115 *ssl = SSL_new(tls_ctx);
2124 if(!res && NULL != sni)
2126 printf(
"%s: %sUsing SNI extension with: %s\n",
2128 rv2 = SSL_set_tlsext_host_name(*ssl, sni);
2131 PRINT_ERROR(
"Adding SNI extension from RFC 6066 failed");
2147 printf(
"%s: %sWarning: Offering weak protocol versions "
2148 "and cipher suites to server\n",
2156 rv = SSL_set_fd(*ssl, sd);
2159 PRINT_ERROR(
"Cannot assign connection object to socket");
2167 rv = SSL_connect(*ssl);
2170 PRINT_ERROR(
"Failed to establish TLS connection");
2172 while((error = ERR_get_error()))
2174 sp = ERR_reason_error_string(error);
2184 if(res) {
PRINT_ERROR(
"Reading negotiation result failed"); }
2187 printf(
"%s: %sUsing protocol: %s with cipher suite %s\n",
2189 # if CFG_USE_OPENSSL_API_1_1
2190 if(SSL_get_peer_signature_nid(*ssl, &id2))
2192 dp =
"rsaEncryption";
2193 # if (OPENSSL_VERSION_NUMBER >= 0x10101000L)
2194 if(SSL_get_peer_signature_type_nid(*ssl, &
id))
2196 dp = OBJ_nid2sn(
id);
2199 if(!strcmp(
"rsaEncryption", dp)) { dp =
"RSASSA-PKCS1-v1_5"; }
2200 printf(
"%s: %sPeer signature algorithm: %s+%s\n",
2207 if(!weak && strcmp(
"TLSv1", tlsv) && strcmp(
"TLSv1.1", tlsv))
2209 PRINT_ERROR(
"Signature algorithm negotiation failed");
2214 if(!weak && strcmp(
"TLSv1", tlsv) && strcmp(
"TLSv1.1", tlsv))
2216 printf(
"%s: %sWarning: Signature algorithm "
2217 "negotiation not possible with OpenSSL API 1.0\n",
2221 if(strcmp(
"TLSv1", tlsv) && strcmp(
"TLSv1.1", tlsv)
2222 && strcmp(
"TLSv1.2", tlsv))
2224 if(!strcmp(
"FFDHE", kx))
2226 printf(
"%s: %sKey exchange algorithm: %s\n",
2231 printf(
"%s: %sKey exchange algorithm: ECDHE (%s)\n",
2236 # if CFG_USE_OPENSSL_API_1_1
2240 rv = SSL_get_server_tmp_key(*ssl, &key);
2241 if(!rv) { res = -1; }
2244 if(EVP_PKEY_DH == EVP_PKEY_id(key))
2246 length = EVP_PKEY_bits(key);
2247 if(0 >= length) { res = -1; }
2250 printf(
"%s: %sFFDHE group size: %d bit\n",
2257 else if(3072 > length)
2260 printf(
"%s: %sWarning: FFDHE group size should be"
2261 " at least 3072 bit\n",
2279 # if CFG_USE_LIBRESSL && (LIBRESSL_VERSION_NUMBER >= 0x30101000L)
2281 if(strcmp(
"TLSv1.3", tlsv))
2284 printf(
"%s: %sWarning: FFDHE group size check not possible "
2286 # if CFG_USE_LIBRESSL && (LIBRESSL_VERSION_NUMBER >= 0x30101000L)
2319 if(NULL != co && NULL != *((SSL**) co))
2322 res = SSL_shutdown(*((SSL**) co));
2323 if(!res) { res = SSL_shutdown(*((SSL**) co)); }
2324 if(0 <= res && 1 != res)
2326 PRINT_ERROR(
"Error while closing TLS connection");
2331 SSL_free(*((SSL**) co));
2332 *((SSL**) co) = NULL;
2335 tls_destroy_context();
2369 const SSL_CIPHER* cp;
2370 # if CFG_USE_OPENSSL_API_1_1
2375 cp = SSL_get_current_cipher(*ssl);
2378 PRINT_ERROR(
"Reading negotiated protocol and cipher suite failed");
2389 if(NULL != pv) { *pv = SSL_get_version(*ssl); }
2390 if(NULL != cs) { *cs = SSL_CIPHER_get_name(cp); }
2393 # if CFG_USE_OPENSSL_API_1_1
2394 if(SSL_get_server_tmp_key(*ssl, &tmp_key))
2396 switch(EVP_PKEY_id(tmp_key))
2405 *kx = OBJ_nid2sn(EVP_PKEY_id(tmp_key));
2409 EVP_PKEY_free(tmp_key);
2475 SSL** ssl = (SSL**) co;
2476 STACK_OF(X509)* certificate_chain = NULL;
2477 X509* certificate = NULL;
2479 X509_NAME_ENTRY* common_name;
2480 ASN1_STRING* common_name_string;
2481 BIO* biop = BIO_new(BIO_s_mem());
2483 const char* sp = NULL;
2485 const char* p = NULL;
2487 GENERAL_NAMES* an = NULL;
2495 certificate_chain = SSL_get_peer_cert_chain(*ssl);
2496 if(NULL == certificate_chain)
2498 PRINT_ERROR(
"Extracting certificate chain failed");
2503 printf(
"%s: %sWarning: Using weak certificate chain checks\n",
2508 # if !CFG_TLS_CRLS_DISABLE
2511 if(!tls_crl_update_skip && tls_check_crl_age())
2513 res = tls_update_crls(certificate_chain);
2514 if(0 > res) {
PRINT_ERROR(
"Updating of CRLs failed"); }
2528 PRINT_ERROR(
"Verification of certificate aborted because "
2530 SSL_get_verify_result(*ssl);
2542 if(res) {
PRINT_ERROR(
"Reading protocol version failed"); }
2546 if(!strcmp(
"TLSv1", pv)) { proto = 0; }
2547 else if(!strcmp(
"TLSv1.1", pv)) { proto = 1; }
2548 else if(!strcmp(
"TLSv1.2", pv)) { proto = 2; }
2553 res = tls_chain_check_sigalgs(certificate_chain, proto);
2560 # if CFG_USE_OPENSSL_API_3
2561 certificate = SSL_get1_peer_certificate(*ssl);
2563 certificate = SSL_get_peer_certificate(*ssl);
2565 if(NULL == certificate)
2567 PRINT_ERROR(
"Server has presented no certificate");
2574 *cert = (
void*) certificate;
2576 rv = SSL_get_verify_result(*ssl);
2579 sp = X509_verify_cert_error_string(rv);
2581 api_posix_snprintf(b, 256,
2582 "%sAlert: Server X.509 certificate verification "
2585 # if !CFG_TLS_CRLS_DISABLE
2586 if(X509_V_ERR_UNABLE_TO_GET_CRL == rv
2587 || X509_V_ERR_CRL_HAS_EXPIRED == rv)
2591 tls_reset_crl_age();
2598 subject = X509_get_subject_name(certificate);
2601 PRINT_ERROR(
"Extraction of subject from X.509 certificate "
2608 an = X509_get_ext_d2i(certificate, NID_subject_alt_name,
2612 num = sk_GENERAL_NAME_num(an);
2613 for(i = 0; i < num; ++i)
2615 ani = sk_GENERAL_NAME_value(an, i);
2616 if(GEN_DNS == ani->type)
2618 # if CFG_USE_OPENSSL_API_1_1
2619 p = (
const char*) ASN1_STRING_get0_data(ani->d.dNSName);
2622 p = (
const char*) ASN1_STRING_data(ani->d.dNSName);
2624 printf(
"%s: %s Subject Alternative Name: %s",
2626 if(!tls_subject_check(cn, p)) { match = 1;
break; }
2629 GENERAL_NAMES_free(an);
2636 i = X509_NAME_get_index_by_NID(subject, NID_commonName, i);
2637 if(-1 == i) {
break; }
2638 common_name = X509_NAME_get_entry(subject, i);
2639 common_name_string = X509_NAME_ENTRY_get_data(common_name);
2640 # if CFG_USE_OPENSSL_API_1_1
2641 p = (
const char*) ASN1_STRING_get0_data(common_name_string);
2644 p = (
const char*) ASN1_STRING_data(common_name_string);
2646 printf(
"%s: %s Subject Name: %s",
2648 if(!tls_subject_check(cn, p)) { match = 1;
break; }
2653 api_posix_snprintf(b, 256,
2654 "%sAlert: Server X.509 certificate "
2655 "verification failed "
2656 "(Subject doesn't match hostname %s)",
2663 # if !CFG_TLS_CRLS_DISABLE
2666 printf(
"%s: %sServer certificate verification "
2671 printf(
"%s: %sServer certificate verification "
2672 "succesful (revocation checks were skipped)\n",
2676 printf(
"%s: %sServer certificate verification successful "
2677 "(revocation checks were skipped)\n",
2688 if(NULL != certificate) { X509_free(certificate); }
2689 if(NULL != biop) { BIO_free(biop); }
2714 BIO* biop = BIO_new(BIO_s_mem());
2720 certificate = (X509*) cert;
2721 rv = X509_print(biop, certificate);
2724 len = (size_t) BIO_get_mem_data(biop, &p);
2725 if(NULL != p && len)
2727 buf = (
char*) api_posix_malloc(len + (
size_t) 1);
2730 memcpy((
void*) buf, (
void*) p, len);
2739 if(NULL != biop) { BIO_free(biop); }
2740 if(res) { api_posix_free((
void*) buf); }
2758 api_posix_ssize_t
tls_send(
void* co,
const void* buf,
size_t len)
2760 api_posix_ssize_t res = -1;
2764 if(API_POSIX_INT_MAX < len)
2770 res = (api_posix_ssize_t) SSL_write((SSL*) co, buf, num);
2771 if(!res) { res = -1; }
2795 api_posix_ssize_t
tls_recv(
void* co,
void* buf,
size_t len,
int peek)
2797 api_posix_ssize_t res = -1;
2801 if(API_POSIX_INT_MAX < len)
2810 # if CFG_USE_OPENSSL_API_1_1 && !CFG_USE_OPENSSL_API_3
2812 "Flag 'peek' not supported with OpenSSL 1.1.x");
2814 res = (api_posix_ssize_t) SSL_peek((SSL*) co, buf, num);
2817 else { res = (api_posix_ssize_t) SSL_read((SSL*) co, buf, num); }
2818 if(!res) { res = -1; }
2846 # if !CFG_TLS_CRLS_DISABLE
2849 res = tls_check_crl_age();
2875 # if !CFG_TLS_CRLS_DISABLE
2877 tls_crl_update_skip = crl_upd_disable;
2879 (void) crl_upd_disable;