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 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*) 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*) 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 posix_free((
void*) b_i);
377 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";
416 PRINT_ERROR(
"Cannot create certificate directory");
417 posix_free((
void*) *certpath);
425 # if !CFG_TLS_CRLS_DISABLE
437 static int tls_check_crl_age(
void)
450 if((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";
549 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 = 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*) posix_malloc(++len);
604 strcpy(pathname, tls_crlpath);
605 strcat(pathname, filename);
616 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*) 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, POSIX_O_WRONLY | POSIX_O_CREAT,
719 POSIX_S_IRUSR | 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 = posix_symlink(pn_der, *pn_pem);
760 PRINT_ERROR(
"Conversion of CRL to PEM format failed");
761 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 = posix_snprintf(name_symlink, 13,
"/%08lx.r%u", issuer_hash, i);
811 PRINT_ERROR(
"Creating of symlink failed, name not valid");
816 len = strlen(tls_crlpath);
817 len += strlen(name_symlink);
818 pn_symlink = (
char*) posix_malloc(++len);
819 if(NULL == pn_symlink) { abort = 1; }
822 strcpy(pn_symlink, tls_crlpath);
823 strcat(pn_symlink, name_symlink);
824 res = posix_symlink(pn_pem, pn_symlink);
833 if(!(-1 == res && POSIX_EEXIST == posix_errno))
840 posix_free((
void*) pn_symlink);
848 PRINT_ERROR(
"Creating of symlink failed, too many duplicates");
862 static int tls_crl_get_lock(
int* fd)
865 const char crl_lockfile[] =
".crl_lock";
866 const char* lockpathname = NULL;
870 if(NULL != lockpathname)
880 POSIX_O_WRONLY | POSIX_O_CREAT,
881 POSIX_S_IRUSR | POSIX_S_IWUSR);
884 PRINT_ERROR(
"Cannot open mutex file for CRL directory");
890 PRINT_ERROR(
"Cannot lock mutex file for CRL directory");
901 posix_free((
void*) lockpathname);
913 static void tls_crl_release_lock(
int* fd)
931 static int tls_update_crls(STACK_OF(X509)* certificate_chain)
936 int crls_required = 0;
942 STACK_OF(DIST_POINT)* dp = NULL;
945 const size_t buflen = 256;
955 if(tls_crl_get_lock(&lockfd)) { res = -2; }
959 printf(
"%s: %s-----------------------------------------------------\n",
961 printf(
"%s: %sAutomatic CRL maintenance\n", CFG_NAME,
MAIN_ERR_PREFIX);
963 if(NULL != tls_crlpath)
967 if(!posix_mkdir(tls_crlpath, (posix_mode_t) POSIX_S_IRWXU))
976 PRINT_ERROR(
"CRL update failed, directory not available");
981 ccl = sk_X509_num(certificate_chain);
982 for(cci = 0; cci < ccl; ++cci)
985 cc = sk_X509_value(certificate_chain, cci);
988 PRINT_ERROR(
"Error while parsing certificate chain");
991 rv = tls_cert_check_root(cc, &is_root, buflen, b_i, NULL);
998 printf(
"%s: %sSelf signed server certificate:\n",
1003 printf(
"%s: %sServer certificate:\n",
1011 printf(
"%s: %sRoot certificate:\n",
1016 printf(
"%s: %sIntermediate certificate:\n",
1026 printf(
"%s: %s Always trusted if installed"
1027 " (Must be uninstalled for revocation)\n",
1033 dp = X509_get_ext_d2i(cc, NID_crl_distribution_points, NULL,
1042 num2 = sk_DIST_POINT_num(dp);
1043 for(i2 = 0; i2 < num2; ++i2)
1045 dpi = sk_DIST_POINT_value(dp, i2);
1046 if(NULL != dpi->distpoint)
1048 num = sk_GENERAL_NAME_num(
1049 dpi->distpoint->name.fullname);
1050 for(i = 0; i < num; ++i)
1052 ani = sk_GENERAL_NAME_value(
1053 dpi->distpoint->name.fullname, i);
1054 if(GEN_URI == ani->type)
1057 ani->d.uniformResourceIdentifier->data;
1059 "CRL distribution point %d: %s\n",
1064 res = tls_download_crl(cci, p, &crl_pn_der);
1068 res = tls_convert_crl_to_pem(crl_pn_der,
1073 res = tls_create_hash_symlink(crl_pn_pem,
1084 posix_free((
void*) crl_pn_pem);
1085 posix_free((
void*) crl_pn_der);
1090 if(crl_valid) {
break; }
1098 printf(
"%s: %s-----------------------------------------------------\n",
1101 tls_crl_release_lock(&lockfd);
1103 if(!res && !crls_required) { res = 1; }
1107 if(NULL != dp) { CRL_DIST_POINTS_free(dp); }
1142 static int tls_chain_check_sigalgs(STACK_OF(X509)* certificate_chain,
1153 EVP_PKEY* pkey = NULL;
1154 # if !CFG_USE_OPENSSL_API_3
1155 RSA* rsa_key = NULL;
1166 ccl = sk_X509_num(certificate_chain);
1167 for(cci = 0; cci < ccl; ++cci)
1170 cc = sk_X509_value(certificate_chain, cci);
1173 PRINT_ERROR(
"Error while parsing certificate chain");
1177 rv = tls_cert_check_root(cc, &is_root, 0, NULL, NULL);
1180 if(!cci) { s =
"(Server) "; }
1181 if(is_root) { s =
"(Root) "; }
1182 printf(
"%s: %sCertificate level %d %sin chain:\n",
1185 pkey = X509_extract_key(cc);
1188 PRINT_ERROR(
"Extraction of public key from X.509 certificate"
1192 # if CFG_USE_OPENSSL_API_1_1 || CFG_USE_LIBRESSL_API_3_5
1193 if(EVP_PKEY_RSA != EVP_PKEY_base_id(pkey))
1195 if(EVP_PKEY_RSA != pkey->type)
1201 PRINT_ERROR(
"Certificate doesn't use RSA key (skipped)");
1204 PRINT_ERROR(
"Certificate doesn't use RSA key (not supported)");
1207 # if !CFG_USE_OPENSSL_API_3
1208 rsa_key = EVP_PKEY_get1_RSA(pkey);
1211 PRINT_ERROR(
"Extraction of key from certificate failed");
1215 # if CFG_USE_OPENSSL_API_3
1216 rsa_mod_size = EVP_PKEY_bits(pkey);
1217 if(0 >= rsa_mod_size)
1223 rsa_mod_size = RSA_size(rsa_key);
1224 if(0 >= rsa_mod_size || 8192 < rsa_mod_size)
1231 printf(
"%s: %s RSA key modulus size: %d bit\n",
1233 if(1024 > rsa_mod_size)
1235 PRINT_ERROR(
"RSA key modulus size of certificate rejected");
1238 else if(3072 > rsa_mod_size)
1241 printf(
"%s: %s Warning: RSA key modulus should be "
1248 # if CFG_USE_OPENSSL_API_3
1249 len = i2d_PUBKEY(pkey, &buf);
1251 len = i2d_RSA_PUBKEY(rsa_key, &buf);
1255 PRINT_ERROR(
"Extraction of key fingerprint failed");
1261 rv2 = posix_snprintf(prefix, 80,
"%s: %s ",
1277 # if !CFG_USE_OPENSSL_API_3
1280 EVP_PKEY_free(pkey);
1282 # if CFG_USE_OPENSSL_API_1_1 || CFG_USE_LIBRESSL_API_3_5
1283 alg = X509_get_signature_nid(cc);
1285 alg = OBJ_obj2nid(cc->sig_alg->algorithm);
1287 if(NID_undef == alg)
1289 PRINT_ERROR(
"Extraction of hash algorithm failed");
1292 md_s = OBJ_nid2sn(alg);
1295 printf(
"%s: %s Self signature is not used\n",
1300 printf(
"%s: %s Signature algorithm: %s\n",
1305 case NID_md2WithRSAEncryption:
1306 case NID_md4WithRSAEncryption:
1307 case NID_md5WithRSAEncryption:
1310 PRINT_ERROR(
"Hash algorithm of signature rejected");
1313 case NID_sha1WithRSAEncryption:
1314 case NID_sha224WithRSAEncryption:
1317 printf(
"%s: %s Warning: At least SHA-256 should be "
1319 # if CFG_USE_OPENSSL_API_1_1
1333 case NID_sha256WithRSAEncryption:
1334 case NID_sha384WithRSAEncryption:
1335 case NID_sha512WithRSAEncryption:
1343 PRINT_ERROR(
" Unexpected hash algorithm rejected");
1351 if(!md_ok) {
break; }
1354 if(cci == ccl) { res = 0; }
1373 static int tls_subject_check(
const char* cn,
const char* subject)
1376 # if CFG_USE_TLS_WILDCARD_SUBJECT
1381 if(
'*' == subject[0])
1386 if(
'.' == subject[1])
1388 subject = &subject[2];
1389 p = strchr(cn, (
int)
'.');
1401 res = posix_strcasecmp_l(cn, subject, tls_loc_ctype_posix);
1403 if(0 < res) { res = -res; }
1406 if(res) { printf(
" (doesn't match hostname)\n"); }
1407 # if CFG_USE_TLS_WILDCARD_SUBJECT
1408 else if(wildcard) { printf(
" (matches hostname using wildcard)\n"); }
1410 else { printf(
" (matches hostname)\n"); }
1435 static int tls_create_context(
int weak)
1438 long int mode = SSL_MODE_ENABLE_PARTIAL_WRITE
1439 | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER;
1440 long int options = SSL_OP_NO_TICKET | SSL_OP_NO_COMPRESSION
1441 | SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3;
1442 unsigned long int parameters = X509_V_FLAG_X509_STRICT;
1443 X509_VERIFY_PARAM* para = NULL;
1446 const char* cpp = NULL;
1447 # if !CFG_TLS_CRLS_DISABLE
1448 X509_LOOKUP* look = NULL;
1453 parameters |= X509_V_FLAG_CRL_CHECK
1454 | X509_V_FLAG_CRL_CHECK_ALL
1455 | X509_V_FLAG_EXTENDED_CRL_SUPPORT
1456 | X509_V_FLAG_USE_DELTAS;
1464 options |= SSL_OP_NO_TLSv1;
1465 # ifdef SSL_OP_NO_TLSv1_1
1466 options |= SSL_OP_NO_TLSv1_1;
1471 tls_ctx = SSL_CTX_new(SSLv23_client_method());
1479 # if CFG_USE_OPENSSL_API_1_1
1481 SSL_CTX_set_security_level(tls_ctx, 0);
1483 f = SSL_CTX_set_mode(tls_ctx, mode);
1484 if((f & mode) != mode)
1492 f = SSL_CTX_set_options(tls_ctx, options);
1493 if((f & options) != options)
1499 # if CFG_USE_OPENSSL_API_1_1 && (OPENSSL_VERSION_NUMBER >= 0x10101000L)
1506 PRINT_ERROR(
"No usable TLSv1.3 ciphers available, "
1507 "check your OpenSSL configuration");
1517 PRINT_ERROR(
"No usable ciphers with EDH key agreement available, "
1518 "check your OpenSSL configuration");
1524 SSL_CTX_set_verify(tls_ctx, SSL_VERIFY_NONE, NULL);
1525 para = X509_VERIFY_PARAM_new();
1528 PRINT_ERROR(
"Memory allocation for X.509 parameters failed ");
1533 rv = X509_VERIFY_PARAM_set_flags(para, parameters);
1541 rv = X509_VERIFY_PARAM_set_purpose(para, X509_PURPOSE_SSL_SERVER);
1544 PRINT_ERROR(
"Cannot set X.509 verification purpose");
1550 rv = SSL_CTX_set1_param(tls_ctx, para);
1553 PRINT_ERROR(
"Cannot store X.509 parameters in TLS context");
1557 X509_VERIFY_PARAM_free(para);
1563 # if CFG_USE_OPENSSL_API_1_1 && (OPENSSL_VERSION_NUMBER >= 0x10101000L)
1564 # if CFG_USE_OPENSSL_API_3
1570 printf(
"%s: %sWorkaround for OpenSSL 1.1.1: "
1577 PRINT_ERROR(
"Cannot store DHE groups in TLS context");
1581 # if CFG_USE_LIBRESSL && (LIBRESSL_VERSION_NUMBER >= 0x30101000L)
1584 printf(
"%s: %sWorkaround for LibreSSL: "
1587 rv = SSL_CTX_set1_groups_list(tls_ctx,
"X25519:P-256");
1590 PRINT_ERROR(
"Cannot store DHE groups in TLS context");
1597 # if CFG_USE_OPENSSL_API_1_1
1601 cctx = SSL_CONF_CTX_new();
1604 PRINT_ERROR(
"Cannot create TLS configuration context");
1610 SSL_CONF_CTX_set_flags(cctx, SSL_CONF_FLAG_FILE | SSL_CONF_FLAG_CLIENT
1611 | SSL_CONF_FLAG_SHOW_ERRORS);
1613 SSL_CONF_CTX_set_ssl_ctx(cctx, tls_ctx);
1615 # if CFG_USE_OPENSSL_API_1_1 && (OPENSSL_VERSION_NUMBER >= 0x10101000L)
1623 rv = SSL_CONF_cmd(cctx,
"SignatureAlgorithms",
TLS_SIGALGS);
1627 PRINT_ERROR(
"Cannot store TLS signature algorithms");
1633 rv = SSL_CONF_CTX_finish(cctx);
1636 PRINT_ERROR(
"Cannot store configuration context");
1647 if(!res) { res = tls_get_certpath(&cpp); }
1651 printf(
"%s: %sLocation of X.509 root certificates: %s/\n",
1653 # if CFG_USE_OPENSSL_API_3
1654 rv = SSL_CTX_load_verify_dir(tls_ctx, tls_certpath);
1656 rv = SSL_CTX_load_verify_locations(tls_ctx, NULL, tls_certpath);
1660 PRINT_ERROR(
"Setup of certificate verification failed");
1670 rv = SSL_CTX_set_default_verify_paths(tls_ctx);
1673 PRINT_ERROR(
"Setup of certificate verification failed");
1679 # if !CFG_TLS_CRLS_DISABLE
1683 res = tls_get_crlpath(&cpp);
1687 printf(
"%s: %sLocation of X.509 CRLs: %s/\n",
1692 look = X509_STORE_add_lookup(SSL_CTX_get_cert_store(tls_ctx),
1693 X509_LOOKUP_hash_dir());
1696 PRINT_ERROR(
"Cannot add CRL lookup method to TLS context");
1702 rv = X509_LOOKUP_add_dir(look, tls_crlpath, X509_FILETYPE_PEM);
1712 printf(
"%s: %sWarning: X.509 certificate revocation checks disabled "
1719 printf(
"%s: %sWarning: X.509 certificate revocation checks disabled "
1732 static void tls_destroy_context(
void)
1734 # if CFG_USE_OPENSSL_API_1_1
1736 SSL_CONF_CTX_free(cctx);
1741 SSL_CTX_free(tls_ctx);
1745 posix_free((
void*) tls_certpath);
1746 tls_certpath = NULL;
1747 # if !CFG_TLS_CRLS_DISABLE
1748 posix_free((
void*) tls_crlpath);
1767 tls_loc_ctype_posix = posix_newlocale(POSIX_LC_CTYPE_MASK,
"POSIX",
1768 (posix_locale_t) 0);
1769 if((posix_locale_t) 0 == tls_loc_ctype_posix)
1781 # if !CFG_USE_OPENSSL_API_1_1
1783 SSL_load_error_strings();
1786 # if OPENSSL_VERSION_NUMBER == 0x1000000FL
1793 OpenSSL_add_all_algorithms();
1801 "check your OpenSSL configuration");
1815 if((posix_locale_t) 0 != tls_loc_ctype_posix)
1817 posix_freelocale(tls_loc_ctype_posix);
1821 # if !CFG_USE_OPENSSL_API_1_1
1863 # if CFG_USE_OPENSSL_API_1_1
1864 unsigned long int version = OpenSSL_version_num();
1866 # if CFG_USE_LIBRESSL && defined(LIBRESSL_VERSION_NUMBER)
1868 unsigned long int version = LIBRESSL_VERSION_NUMBER;
1870 unsigned long int version = SSLeay();
1873 unsigned int major = (
unsigned int) ((version & 0xF0000000UL) >> 28);
1874 unsigned int minor = (
unsigned int) ((version & 0x0FF00000UL) >> 20);
1875 unsigned int fix = (
unsigned int) ((version & 0x000FF000UL) >> 12);
1876 unsigned int patch = (
unsigned int) ((version & 0x00000FF0UL) >> 4);
1877 # if ! CFG_USE_LIBRESSL && ! CFG_USE_OPENSSL_API_3
1879 char patch_string[4] = { 0, 0, 0, 0 };
1886 patch_letter = (char) (0x60U + patch);
1888 else if(50U >= patch)
1891 patch_letter = (char) (0x60U + patch - 25U);
1892 patch_string[1] = patch_letter;
1896 else if(75U >= patch)
1899 patch_letter = (char) (0x60U + patch - 50U);
1900 patch_string[2] = patch_letter;
1902 patch_string[1] =
'z';
1911 patch_string[0] = patch_letter;
1916 # if CFG_USE_LIBRESSL && defined(LIBRESSL_VERSION_NUMBER)
1919 printf(
"%s: %sLibreSSL library version: %X.%X.%X (0x%08lX)\n",
1923 # if CFG_USE_OPENSSL_API_3
1924 printf(
"%s: %sOpenSSL library version: %X.%X.%X (0x%08lX)\n",
1927 printf(
"%s: %sOpenSSL library version: %X.%X.%X%s (0x%08lX)\n",
1933 # if CFG_USE_OPENSSL_API_1_1 && (OPENSSL_VERSION_NUMBER >= 0x10101000L) \
1934 || CFG_USE_LIBRESSL && (LIBRESSL_VERSION_NUMBER >= 0x30101000L)
1935 printf(
"%s: %sProtocol version TLSv1.3 available for negotiation\n",
1946 if(1U == major && 0U == minor)
1948 if( (1U == fix && 0xEU <= patch && 0xFU >= patch)
1949 || (2U == fix && 0x2U <= patch && 0x3U >= patch) )
1951 printf(
"%s: %sOpenSSL library possibly vulnerable to #OprahSSL!\n",
1961 if(1U == major && 0U == minor && 1U == fix && 7U > patch)
1963 printf(
"%s: %sOpenSSL library possibly vulnerable to Heartbleed!\n",
1972 if(1U == major && 0U == minor)
1974 if( (0U == fix && 0x0DU > patch) || (1U == fix && 8U > patch) )
1977 "OpenSSL library possibly vulnerable to MITM attacks!\n",
2006 const char* res = NULL;
2007 unsigned char buf[16];
2009 if(1 != posix_inet_pton(POSIX_AF_INET, sn, buf))
2012 if(1 != posix_inet_pton(POSIX_AF_INET6, sn, buf))
2077 int tls_open(
int sd,
void** co,
int weak,
const char* sni)
2083 unsigned long int error;
2084 const char* sp = NULL;
2085 const char* tlsv = NULL;
2086 const char* cp = NULL;
2087 const char* kx = NULL;
2089 # if CFG_USE_OPENSSL_API_1_1
2090 EVP_PKEY* key = NULL;
2093 # if CFG_USE_OPENSSL_API_1_1
2094 const char* dp = NULL;
2095 # if (OPENSSL_VERSION_NUMBER >= 0x10101000L)
2106 res = tls_create_context(weak);
2111 *ssl = SSL_new(tls_ctx);
2120 if(!res && NULL != sni)
2122 printf(
"%s: %sUsing SNI extension with: %s\n",
2124 rv2 = SSL_set_tlsext_host_name(*ssl, sni);
2127 PRINT_ERROR(
"Adding SNI extension from RFC 6066 failed");
2143 printf(
"%s: %sWarning: Offering weak protocol versions "
2144 "and cipher suites to server\n",
2152 rv = SSL_set_fd(*ssl, sd);
2155 PRINT_ERROR(
"Cannot assign connection object to socket");
2163 rv = SSL_connect(*ssl);
2166 PRINT_ERROR(
"Failed to establish TLS connection");
2168 while((error = ERR_get_error()))
2170 sp = ERR_reason_error_string(error);
2180 if(res) {
PRINT_ERROR(
"Reading negotiation result failed"); }
2183 printf(
"%s: %sUsing protocol: %s with cipher suite %s\n",
2185 # if CFG_USE_OPENSSL_API_1_1
2186 if(SSL_get_peer_signature_nid(*ssl, &id2))
2188 dp =
"rsaEncryption";
2189 # if (OPENSSL_VERSION_NUMBER >= 0x10101000L)
2190 if(SSL_get_peer_signature_type_nid(*ssl, &
id))
2192 dp = OBJ_nid2sn(
id);
2195 if(!strcmp(
"rsaEncryption", dp)) { dp =
"RSASSA-PKCS1-v1_5"; }
2196 printf(
"%s: %sPeer signature algorithm: %s+%s\n",
2203 if(!weak && strcmp(
"TLSv1", tlsv) && strcmp(
"TLSv1.1", tlsv))
2205 PRINT_ERROR(
"Signature algorithm negotiation failed");
2210 if(!weak && strcmp(
"TLSv1", tlsv) && strcmp(
"TLSv1.1", tlsv))
2212 printf(
"%s: %sWarning: Signature algorithm "
2213 "negotiation not possible with OpenSSL API 1.0\n",
2217 if(strcmp(
"TLSv1", tlsv) && strcmp(
"TLSv1.1", tlsv)
2218 && strcmp(
"TLSv1.2", tlsv))
2220 if(!strcmp(
"FFDHE", kx))
2222 printf(
"%s: %sKey exchange algorithm: %s\n",
2227 printf(
"%s: %sKey exchange algorithm: ECDHE (%s)\n",
2232 # if CFG_USE_OPENSSL_API_1_1
2236 rv = SSL_get_server_tmp_key(*ssl, &key);
2237 if(!rv) { res = -1; }
2240 if(EVP_PKEY_DH == EVP_PKEY_id(key))
2242 length = EVP_PKEY_bits(key);
2243 if(0 >= length) { res = -1; }
2246 printf(
"%s: %sFFDHE group size: %d bit\n",
2253 else if(3072 > length)
2256 printf(
"%s: %sWarning: FFDHE group size should be"
2257 " at least 3072 bit\n",
2275 # if CFG_USE_LIBRESSL && (LIBRESSL_VERSION_NUMBER >= 0x30101000L)
2277 if(strcmp(
"TLSv1.3", tlsv))
2280 printf(
"%s: %sWarning: FFDHE group size check not possible "
2282 # if CFG_USE_LIBRESSL && (LIBRESSL_VERSION_NUMBER >= 0x30101000L)
2315 if(NULL != co && NULL != *((SSL**) co))
2318 res = SSL_shutdown(*((SSL**) co));
2319 if(!res) { res = SSL_shutdown(*((SSL**) co)); }
2320 if(0 <= res && 1 != res)
2322 PRINT_ERROR(
"Error while closing TLS connection");
2327 SSL_free(*((SSL**) co));
2328 *((SSL**) co) = NULL;
2331 tls_destroy_context();
2365 const SSL_CIPHER* cp;
2366 # if CFG_USE_OPENSSL_API_1_1
2371 cp = SSL_get_current_cipher(*ssl);
2374 PRINT_ERROR(
"Reading negotiated protocol and cipher suite failed");
2385 if(NULL != pv) { *pv = SSL_get_version(*ssl); }
2386 if(NULL != cs) { *cs = SSL_CIPHER_get_name(cp); }
2389 # if CFG_USE_OPENSSL_API_1_1
2390 if(SSL_get_server_tmp_key(*ssl, &tmp_key))
2392 switch(EVP_PKEY_id(tmp_key))
2401 *kx = OBJ_nid2sn(EVP_PKEY_id(tmp_key));
2405 EVP_PKEY_free(tmp_key);
2471 SSL** ssl = (SSL**) co;
2472 STACK_OF(X509)* certificate_chain = NULL;
2473 X509* certificate = NULL;
2475 X509_NAME_ENTRY* common_name;
2476 ASN1_STRING* common_name_string;
2477 BIO* biop = BIO_new(BIO_s_mem());
2479 const char* sp = NULL;
2481 const char* p = NULL;
2483 GENERAL_NAMES* an = NULL;
2491 certificate_chain = SSL_get_peer_cert_chain(*ssl);
2492 if(NULL == certificate_chain)
2494 PRINT_ERROR(
"Extracting certificate chain failed");
2499 printf(
"%s: %sWarning: Using weak certificate chain checks\n",
2504 # if !CFG_TLS_CRLS_DISABLE
2507 if(!tls_crl_update_skip && tls_check_crl_age())
2509 res = tls_update_crls(certificate_chain);
2510 if(0 > res) {
PRINT_ERROR(
"Updating of CRLs failed"); }
2524 PRINT_ERROR(
"Verification of certificate aborted because "
2526 SSL_get_verify_result(*ssl);
2538 if(res) {
PRINT_ERROR(
"Reading protocol version failed"); }
2542 if(!strcmp(
"TLSv1", pv)) { proto = 0; }
2543 else if(!strcmp(
"TLSv1.1", pv)) { proto = 1; }
2544 else if(!strcmp(
"TLSv1.2", pv)) { proto = 2; }
2549 res = tls_chain_check_sigalgs(certificate_chain, proto);
2556 # if CFG_USE_OPENSSL_API_3
2557 certificate = SSL_get1_peer_certificate(*ssl);
2559 certificate = SSL_get_peer_certificate(*ssl);
2561 if(NULL == certificate)
2563 PRINT_ERROR(
"Server has presented no certificate");
2570 *cert = (
void*) certificate;
2572 rv = SSL_get_verify_result(*ssl);
2575 sp = X509_verify_cert_error_string(rv);
2577 posix_snprintf(b, 256,
2578 "%sAlert: Server X.509 certificate verification "
2581 # if !CFG_TLS_CRLS_DISABLE
2582 if(X509_V_ERR_UNABLE_TO_GET_CRL == rv
2583 || X509_V_ERR_CRL_HAS_EXPIRED == rv)
2587 tls_reset_crl_age();
2594 subject = X509_get_subject_name(certificate);
2597 PRINT_ERROR(
"Extraction of subject from X.509 certificate "
2604 an = X509_get_ext_d2i(certificate, NID_subject_alt_name,
2608 num = sk_GENERAL_NAME_num(an);
2609 for(i = 0; i < num; ++i)
2611 ani = sk_GENERAL_NAME_value(an, i);
2612 if(GEN_DNS == ani->type)
2614 # if CFG_USE_OPENSSL_API_1_1
2615 p = (
const char*) ASN1_STRING_get0_data(ani->d.dNSName);
2618 p = (
const char*) ASN1_STRING_data(ani->d.dNSName);
2620 printf(
"%s: %s Subject Alternative Name: %s",
2622 if(!tls_subject_check(cn, p)) { match = 1;
break; }
2625 GENERAL_NAMES_free(an);
2632 i = X509_NAME_get_index_by_NID(subject, NID_commonName, i);
2633 if(-1 == i) {
break; }
2634 common_name = X509_NAME_get_entry(subject, i);
2635 common_name_string = X509_NAME_ENTRY_get_data(common_name);
2636 # if CFG_USE_OPENSSL_API_1_1
2637 p = (
const char*) ASN1_STRING_get0_data(common_name_string);
2640 p = (
const char*) ASN1_STRING_data(common_name_string);
2642 printf(
"%s: %s Subject Name: %s",
2644 if(!tls_subject_check(cn, p)) { match = 1;
break; }
2649 posix_snprintf(b, 256,
2650 "%sAlert: Server X.509 certificate "
2651 "verification failed "
2652 "(Subject doesn't match hostname %s)",
2659 # if !CFG_TLS_CRLS_DISABLE
2662 printf(
"%s: %sServer certificate verification "
2667 printf(
"%s: %sServer certificate verification "
2668 "succesful (revocation checks were skipped)\n",
2672 printf(
"%s: %sServer certificate verification successful "
2673 "(revocation checks were skipped)\n",
2684 if(NULL != certificate) { X509_free(certificate); }
2685 if(NULL != biop) { BIO_free(biop); }
2710 BIO* biop = BIO_new(BIO_s_mem());
2716 certificate = (X509*) cert;
2717 rv = X509_print(biop, certificate);
2720 len = (size_t) BIO_get_mem_data(biop, &p);
2721 if(NULL != p && len)
2723 buf = (
char*) posix_malloc(len + (
size_t) 1);
2726 memcpy((
void*) buf, (
void*) p, len);
2735 if(NULL != biop) { BIO_free(biop); }
2736 if(res) { posix_free((
void*) buf); }
2754 posix_ssize_t
tls_send(
void* co,
const void* buf,
size_t len)
2756 posix_ssize_t res = -1;
2760 if(POSIX_INT_MAX < len)
2766 res = (posix_ssize_t) SSL_write((SSL*) co, buf, num);
2767 if(!res) { res = -1; }
2791 posix_ssize_t
tls_recv(
void* co,
void* buf,
size_t len,
int peek)
2793 posix_ssize_t res = -1;
2797 if(POSIX_INT_MAX < len)
2806 # if CFG_USE_OPENSSL_API_1_1 && !CFG_USE_OPENSSL_API_3
2808 "Flag 'peek' not supported with OpenSSL 1.1.x");
2810 res = (posix_ssize_t) SSL_peek((SSL*) co, buf, num);
2813 else { res = (posix_ssize_t) SSL_read((SSL*) co, buf, num); }
2814 if(!res) { res = -1; }
2842 # if !CFG_TLS_CRLS_DISABLE
2845 res = tls_check_crl_age();
2871 # if !CFG_TLS_CRLS_DISABLE
2873 tls_crl_update_skip = crl_upd_disable;