1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691
|
// OpenVPN -- An application to securely tunnel IP networks
// over a single port, with support for SSL/TLS-based
// session authentication and key exchange,
// packet encryption, packet authentication, and
// packet compression.
//
// Copyright (C) 2012- OpenVPN Inc.
//
// SPDX-License-Identifier: MPL-2.0 OR AGPL-3.0-only WITH openvpn3-openssl-exception
//
// Wrap the mbed TLS 2.3 SSL API as defined in <mbedtls/ssl.h>
// so that it can be used as the SSL layer by the OpenVPN core.
#ifndef OPENVPN_MBEDTLS_SSL_SSLCTX_H
#define OPENVPN_MBEDTLS_SSL_SSLCTX_H
#include <vector>
#include <string>
#include <sstream>
#include <cstring>
#include <memory>
#include <mbedtls/ssl.h>
#include <mbedtls/oid.h>
#include <mbedtls/sha1.h>
#include <mbedtls/debug.h>
#include <mbedtls/asn1.h>
#include <mbedtls/version.h>
#include <openvpn/common/size.hpp>
#include <openvpn/common/exception.hpp>
#include <openvpn/common/base64.hpp>
#include <openvpn/common/binprefix.hpp>
#include <openvpn/common/string.hpp>
#include <openvpn/frame/memq_stream.hpp>
#include <openvpn/pki/cclist.hpp>
#include <openvpn/pki/pkcs1.hpp>
#include <openvpn/ssl/sslconsts.hpp>
#include <openvpn/ssl/sslapi.hpp>
#include <openvpn/ssl/verify_x509_name.hpp>
#include <openvpn/ssl/iana_ciphers.hpp>
#include <openvpn/mbedtls/pki/x509cert.hpp>
#include <openvpn/mbedtls/pki/x509certinfo.hpp>
#include <openvpn/mbedtls/pki/x509crl.hpp>
#include <openvpn/mbedtls/pki/dh.hpp>
#include <openvpn/mbedtls/pki/pkctx.hpp>
#include <openvpn/mbedtls/util/error.hpp>
// An SSL Context is essentially a configuration that can be used
// to generate an arbitrary number of actual SSL connections objects.
// MbedTLSContext is an SSL Context implementation that uses the
// mbed TLS library as a backend.
namespace openvpn {
namespace mbedtls_ctx_private {
namespace {
#if MBEDTLS_VERSION_NUMBER < 0x03000000
/*
* This is a modified list from mbed TLS ssl_ciphersuites.c.
* We removed some SHA1 methods near the top of the list to
* avoid Chrome warnings about "obsolete cryptography".
* We also removed ECDSA, CCM, PSK, and CAMELLIA algs.
*
* With mbed TLS 3 or newer we trust the default list of
* algorithms in mbed TLS
*/
const int ciphersuites[] = // CONST GLOBAL
{
/* Selected AES-256 ephemeral suites */
MBEDTLS_TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
MBEDTLS_TLS_DHE_RSA_WITH_AES_256_GCM_SHA384,
MBEDTLS_TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384,
MBEDTLS_TLS_DHE_RSA_WITH_AES_256_CBC_SHA256,
MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384,
/* Selected AES-128 ephemeral suites */
MBEDTLS_TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
MBEDTLS_TLS_DHE_RSA_WITH_AES_128_GCM_SHA256,
MBEDTLS_TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256,
MBEDTLS_TLS_DHE_RSA_WITH_AES_128_CBC_SHA256,
MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256,
/* Selected remaining >= 128-bit ephemeral suites */
MBEDTLS_TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA,
MBEDTLS_TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA,
MBEDTLS_TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA,
/* Selected AES-256 suites */
MBEDTLS_TLS_RSA_WITH_AES_256_GCM_SHA384,
MBEDTLS_TLS_RSA_WITH_AES_256_CBC_SHA256,
MBEDTLS_TLS_RSA_WITH_AES_256_CBC_SHA,
MBEDTLS_TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384,
MBEDTLS_TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384,
MBEDTLS_TLS_ECDH_RSA_WITH_AES_256_CBC_SHA,
MBEDTLS_TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384,
MBEDTLS_TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384,
MBEDTLS_TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA,
/* Selected AES-128 suites */
MBEDTLS_TLS_RSA_WITH_AES_128_GCM_SHA256,
MBEDTLS_TLS_RSA_WITH_AES_128_CBC_SHA256,
MBEDTLS_TLS_RSA_WITH_AES_128_CBC_SHA,
MBEDTLS_TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256,
MBEDTLS_TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256,
MBEDTLS_TLS_ECDH_RSA_WITH_AES_128_CBC_SHA,
MBEDTLS_TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256,
MBEDTLS_TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256,
MBEDTLS_TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA,
/* Selected remaining >= 128-bit suites */
MBEDTLS_TLS_RSA_WITH_3DES_EDE_CBC_SHA,
MBEDTLS_TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA,
MBEDTLS_TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA,
0};
#endif
/*
* X509 cert profiles.
*/
#ifdef OPENVPN_ALLOW_INSECURE_CERTPROFILE
// This profile includes the broken MD5 alrogithm.
// We are going to ship support for this algorithm for a limited
// amount of time to allow our users to switch to something else
const mbedtls_x509_crt_profile crt_profile_insecure = // CONST GLOBAL
{
MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_MD5)
| MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA1)
| MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_RIPEMD160)
| MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA224)
| MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256)
| MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384)
| MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA512),
0xFFFFFFF, /* Any PK alg */
0xFFFFFFF, /* Any curve */
1024, /* Minimum size for RSA keys */
};
#endif
const mbedtls_x509_crt_profile crt_profile_legacy = // CONST GLOBAL
{
MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA1)
| MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_RIPEMD160)
| MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA224)
| MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256)
| MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384)
| MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA512),
0xFFFFFFF, /* Any PK alg */
0xFFFFFFF, /* Any curve */
1024, /* Minimum size for RSA keys */
};
const mbedtls_x509_crt_profile crt_profile_preferred = // CONST GLOBAL
{
MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256)
| MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384)
| MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA512),
0xFFFFFFF, /* Any PK alg */
0xFFFFFFF, /* Any curve */
2048, /* Minimum size for RSA keys */
};
} // namespace
} // namespace mbedtls_ctx_private
// Represents an SSL configuration that can be used
// to instantiate actual SSL sessions.
class MbedTLSContext : public SSLFactoryAPI
{
public:
typedef RCPtr<MbedTLSContext> Ptr;
enum
{
MAX_CIPHERTEXT_IN = 64 // maximum number of queued input ciphertext packets
};
// The data needed to construct a MbedTLSContext.
class Config : public SSLConfigAPI
{
friend class MbedTLSContext;
public:
typedef RCPtr<Config> Ptr;
Config()
: external_pki(nullptr),
ssl_debug_level(0),
flags(0),
ns_cert_type(NSCert::NONE),
tls_version_min(TLSVersion::Type::V1_2),
tls_cert_profile(TLSCertProfile::UNDEF),
local_cert_enabled(true)
{
}
SSLFactoryAPI::Ptr new_factory() override
{
return SSLFactoryAPI::Ptr(new MbedTLSContext(this));
}
void set_mode(const Mode &mode_arg) override
{
mode = mode_arg;
}
const Mode &get_mode() const override
{
return mode;
}
// if this callback is defined, no private key needs to be loaded
void set_external_pki_callback(ExternalPKIBase *external_pki_arg, const std::string &alias) override
{
external_pki = external_pki_arg;
external_pki_alias = alias;
}
void set_session_ticket_handler(TLSSessionTicketBase *session_ticket_handler_arg) override
{
// fixme -- this method should be implemented for server-side TLS session resumption tickets
throw MbedTLSException("set_session_ticket_handler not implemented");
}
void set_client_session_tickets(const bool v) override
{
// fixme -- this method should be implemented for client-side TLS session resumption tickets
throw MbedTLSException("set_client_session_tickets not implemented");
}
void enable_legacy_algorithms(const bool v) override
{
// We ignore the request to enable legacy as we do not have a runtime
// configuration for this
}
void set_sni_handler(SNI::HandlerBase *sni_handler) override
{
// fixme -- this method should be implemented on the server-side for SNI
throw MbedTLSException("set_sni_handler not implemented");
}
void set_sni_name(const std::string &sni_name_arg) override
{
// fixme -- this method should be implemented on the client-side for SNI
throw MbedTLSException("set_sni_name not implemented");
}
void set_cn_reject_handler(CommonNameReject *cn_reject_handler_arg) override
{
throw MbedTLSException("set_cn_reject_handler not implemented");
}
void set_private_key_password(const std::string &pwd) override
{
priv_key_pwd = pwd;
}
void load_ca(const std::string &ca_txt, bool strict) override
{
MbedTLSPKI::X509Cert::Ptr c = new MbedTLSPKI::X509Cert();
c->parse(ca_txt, "ca", strict);
ca_chain = c;
}
void load_crl(const std::string &crl_txt) override
{
MbedTLSPKI::X509CRL::Ptr c = new MbedTLSPKI::X509CRL();
c->parse(crl_txt);
crl_chain = c;
}
void load_cert(const std::string &cert_txt) override
{
MbedTLSPKI::X509Cert::Ptr c = new MbedTLSPKI::X509Cert();
c->parse(cert_txt, "cert", true);
crt_chain = c;
}
void load_cert(const std::string &cert_txt, const std::string &extra_certs_txt) override
{
MbedTLSPKI::X509Cert::Ptr c = new MbedTLSPKI::X509Cert();
c->parse(cert_txt, "cert", true);
if (!extra_certs_txt.empty())
c->parse(extra_certs_txt, "extra-certs", true);
crt_chain = c;
}
void load_private_key(const std::string &key_txt) override
{
MbedTLSPKI::PKContext::Ptr p = new MbedTLSPKI::PKContext();
auto *mbedrng = get_mbed_random_class();
p->parse(key_txt, "config", priv_key_pwd, *mbedrng);
priv_key = p;
}
void load_dh(const std::string &dh_txt) override
{
MbedTLSPKI::DH::Ptr mydh = new MbedTLSPKI::DH();
mydh->parse(dh_txt, "server-config");
dh = mydh;
}
std::string extract_ca() const override
{
if (!ca_chain)
return std::string();
return ca_chain->extract();
}
std::string extract_crl() const override
{
if (!crl_chain)
return std::string();
return crl_chain->extract();
}
std::string extract_cert() const override
{
if (!crt_chain)
return std::string();
return crt_chain->extract();
}
std::vector<std::string> extract_extra_certs() const override
{
if (!crt_chain)
return std::vector<std::string>();
return crt_chain->extract_extra_certs();
}
std::string extract_private_key() const override
{
if (!priv_key)
return std::string();
return priv_key->extract();
}
std::string extract_dh() const override
{
if (!dh)
return std::string();
return dh->extract();
}
PKType::Type private_key_type() const override
{
if (!priv_key)
return PKType::PK_NONE;
return priv_key->key_type();
}
size_t private_key_length() const override
{
if (!priv_key)
return 0;
return priv_key->key_length();
}
void set_frame(const Frame::Ptr &frame_arg) override
{
frame = frame_arg;
}
void set_debug_level(const int debug_level) override
{
ssl_debug_level = debug_level;
}
void set_flags(const unsigned int flags_arg) override
{
flags = flags_arg;
}
void set_ns_cert_type(const NSCert::Type ns_cert_type_arg) override
{
ns_cert_type = ns_cert_type_arg;
}
void set_remote_cert_tls(const KUParse::TLSWebType wt) override
{
KUParse::remote_cert_tls(wt, ku, eku);
}
void set_tls_remote(const std::string &tls_remote_arg) override
{
tls_remote = tls_remote_arg;
}
void set_tls_version_min(const TLSVersion::Type tvm) override
{
tls_version_min = tvm;
}
void set_tls_version_max(const TLSVersion::Type tvm) override
{
}
void set_tls_version_min_override(const std::string &override) override
{
TLSVersion::apply_override(tls_version_min, override);
}
void set_tls_cert_profile(const TLSCertProfile::Type type) override
{
tls_cert_profile = type;
}
virtual void set_tls_cipher_list(const std::string &override)
{
if (!override.empty())
tls_cipher_list = override;
}
virtual void set_tls_ciphersuite_list(const std::string &override)
{
// mbed TLS does not have TLS 1.3 support
}
virtual void set_tls_groups(const std::string &groups)
{
if (!groups.empty())
tls_groups = groups;
}
void set_tls_cert_profile_override(const std::string &override) override
{
TLSCertProfile::apply_override(tls_cert_profile, override);
}
void set_local_cert_enabled(const bool v) override
{
local_cert_enabled = v;
}
void set_x509_track(X509Track::ConfigSet x509_track_config_arg) override
{
x509_track_config = std::move(x509_track_config_arg);
}
void set_rng(const StrongRandomAPI::Ptr &rng_arg) override
{
rng = rng_arg;
}
std::string validate_cert(const std::string &cert_txt) const override
{
MbedTLSPKI::X509Cert::Ptr cert = new MbedTLSPKI::X509Cert(cert_txt, "validation cert", true);
return cert_txt; // fixme -- implement parse/re-render semantics
}
std::string validate_cert_list(const std::string &certs_txt) const override
{
MbedTLSPKI::X509Cert::Ptr cert = new MbedTLSPKI::X509Cert(certs_txt, "validation cert list", true);
return certs_txt; // fixme -- implement parse/re-render semantics
}
std::string validate_private_key(const std::string &key_txt) const override
{
auto *mbedrng = get_mbed_random_class();
MbedTLSPKI::PKContext::Ptr pkey = new MbedTLSPKI::PKContext(key_txt, "validation", "", *mbedrng);
return key_txt; // fixme -- implement parse/re-render semantics
}
std::string validate_dh(const std::string &dh_txt) const override
{
MbedTLSPKI::DH::Ptr dh = new MbedTLSPKI::DH(dh_txt, "validation");
return dh_txt; // fixme -- implement parse/re-render semantics
}
std::string validate_crl(const std::string &crl_txt) const override
{
MbedTLSPKI::X509CRL::Ptr crl = new MbedTLSPKI::X509CRL(crl_txt);
return crl_txt; // fixme -- implement parse/re-render semantics
}
void load(const OptionList &opt, const unsigned int lflags) override
{
// client/server
if (lflags & LF_PARSE_MODE)
mode = opt.exists("client") ? Mode(Mode::CLIENT) : Mode(Mode::SERVER);
// possibly disable peer cert verification
if ((lflags & LF_ALLOW_CLIENT_CERT_NOT_REQUIRED)
&& opt.exists("client-cert-not-required"))
flags |= SSLConst::NO_VERIFY_PEER;
// sni
{
const std::string name = opt.get_optional("sni", 1, 256);
if (!name.empty())
set_sni_name(name);
}
// ca
{
std::string ca_txt = opt.cat("ca");
if (lflags & LF_RELAY_MODE)
ca_txt += opt.cat("relay-extra-ca");
load_ca(ca_txt, true);
}
// CRL
{
const std::string crl_txt = opt.cat("crl-verify");
if (!crl_txt.empty())
load_crl(crl_txt);
}
// local cert/key
if (local_cert_enabled)
{
// cert/extra-certs
{
const std::string &cert_txt = opt.get("cert", 1, Option::MULTILINE);
const std::string ec_txt = opt.cat("extra-certs");
load_cert(cert_txt, ec_txt);
}
// private key
if (!external_pki)
{
const std::string &key_txt = opt.get("key", 1, Option::MULTILINE);
load_private_key(key_txt);
}
}
// DH
if (mode.is_server() && opt.exists("dh"))
{
const std::string &dh_txt = opt.get("dh", 1, Option::MULTILINE);
load_dh(dh_txt);
}
// relay mode
std::string relay_prefix;
if (lflags & LF_RELAY_MODE)
relay_prefix = "relay-";
// parse ns-cert-type
ns_cert_type = NSCert::ns_cert_type(opt, relay_prefix);
// parse remote-cert-x options
KUParse::remote_cert_tls(opt, relay_prefix, ku, eku);
KUParse::remote_cert_ku(opt, relay_prefix, ku);
KUParse::remote_cert_eku(opt, relay_prefix, eku);
// parse tls-remote
tls_remote = opt.get_optional(relay_prefix + "tls-remote", 1, 256);
// parse verify-x509-name
verify_x509_name.init(opt, relay_prefix);
// parse tls-version-min option
{
#if defined(MBEDTLS_SSL_MAJOR_VERSION_3) && defined(MBEDTLS_SSL_MINOR_VERSION_3)
const TLSVersion::Type maxver = TLSVersion::Type::V1_2;
#elif defined(MBEDTLS_SSL_MAJOR_VERSION_3) && defined(MBEDTLS_SSL_MINOR_VERSION_2)
const TLSVersion::Type maxver = TLSVersion::Type::V1_1;
#else
const TLSVersion::Type maxver = TLSVersion::Type::V1_0;
#endif
tls_version_min = TLSVersion::parse_tls_version_min(opt, relay_prefix, maxver);
}
// parse tls-cert-profile
tls_cert_profile = TLSCertProfile::parse_tls_cert_profile(opt, relay_prefix);
// Overrides for tls cipher suites
if (opt.exists("tls-cipher"))
tls_cipher_list = opt.get_optional("tls-cipher", 1, 256);
if (opt.exists("tls-groups"))
tls_groups = opt.get_optional("tls-groups", 1, 256);
// unsupported cert verification options
{
}
}
#ifdef OPENVPN_JSON_INTERNAL
SSLConfigAPI::Ptr json_override(const Json::Value &root, const bool load_cert_key) const override
{
throw MbedTLSException("json_override not implemented");
}
#endif
bool is_server() const
{
return mode.is_server();
}
private:
MbedTLSRandom *get_mbed_random_class() const
{
if (!rng)
{
throw MbedTLSException("RNG not initialised yet");
}
auto *mbedrng = dynamic_cast<MbedTLSRandom *>(rng.get());
if (!mbedrng)
{
throw MbedTLSException("RNG needs to be MbedTLSRandom");
}
return mbedrng;
}
const mbedtls_x509_crt_profile *select_crt_profile() const
{
switch (TLSCertProfile::default_if_undef(tls_cert_profile))
{
#ifdef OPENVPN_ALLOW_INSECURE_CERTPROFILE
case TLSCertProfile::INSECURE:
return &mbedtls_ctx_private::crt_profile_insecure;
#endif
case TLSCertProfile::LEGACY:
return &mbedtls_ctx_private::crt_profile_legacy;
case TLSCertProfile::PREFERRED:
return &mbedtls_ctx_private::crt_profile_preferred;
case TLSCertProfile::SUITEB:
return &mbedtls_x509_crt_profile_suiteb;
default:
throw MbedTLSException("select_crt_profile: unknown cert profile");
}
}
Mode mode;
protected:
MbedTLSPKI::X509Cert::Ptr crt_chain; // local cert chain (including client cert + extra certs)
MbedTLSPKI::X509Cert::Ptr ca_chain; // CA chain for remote verification
private:
MbedTLSPKI::X509CRL::Ptr crl_chain; // CRL chain for remote verification
MbedTLSPKI::PKContext::Ptr priv_key; // private key
std::string priv_key_pwd; // private key password
MbedTLSPKI::DH::Ptr dh; // diffie-hellman parameters (only needed in server mode)
ExternalPKIBase *external_pki;
std::string external_pki_alias;
Frame::Ptr frame;
int ssl_debug_level;
unsigned int flags; // defined in sslconsts.hpp
NSCert::Type ns_cert_type;
std::vector<unsigned int> ku; // if defined, peer cert X509 key usage must match one of these values
std::string eku; // if defined, peer cert X509 extended key usage must match this OID/string
std::string tls_remote;
VerifyX509Name verify_x509_name; // --verify-x509-name feature
TLSVersion::Type tls_version_min; // minimum TLS version that we will negotiate
TLSCertProfile::Type tls_cert_profile;
std::string tls_cipher_list;
std::string tls_groups;
X509Track::ConfigSet x509_track_config;
bool local_cert_enabled;
StrongRandomAPI::Ptr rng; // random data source
};
// Represents an actual SSL session.
// Normally instantiated by MbedTLSContext::ssl().
class SSL : public SSLAPI
{
// read/write callback errors
enum
{
// assumes that mbed TLS user-defined errors may start at -0x8000
CT_WOULD_BLOCK = -0x8000,
CT_INTERNAL_ERROR = -0x8001
};
friend class MbedTLSContext;
public:
typedef RCPtr<SSL> Ptr;
void start_handshake() override
{
mbedtls_ssl_handshake(ssl);
}
ssize_t write_cleartext_unbuffered(const void *data, const size_t size) override
{
const int status = mbedtls_ssl_write(ssl, (const unsigned char *)data, size);
if (status < 0)
{
if (status == CT_WOULD_BLOCK)
return SSLConst::SHOULD_RETRY;
else if (status == CT_INTERNAL_ERROR)
throw MbedTLSException("SSL write: internal error");
else
throw MbedTLSException("SSL write error", status);
}
else
return status;
}
ssize_t read_cleartext(void *data, const size_t capacity) override
{
if (!overflow)
{
const int status = mbedtls_ssl_read(ssl, (unsigned char *)data, capacity);
if (status < 0)
{
if (status == CT_WOULD_BLOCK)
return SSLConst::SHOULD_RETRY;
else if (status == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY)
return SSLConst::PEER_CLOSE_NOTIFY;
else if (status == CT_INTERNAL_ERROR)
throw MbedTLSException("SSL read: internal error");
else
throw MbedTLSException("SSL read error", status);
}
else
return status;
}
else
throw ssl_ciphertext_in_overflow();
}
bool read_cleartext_ready() const override
{
return !ct_in.empty() || mbedtls_ssl_get_bytes_avail(ssl);
}
void write_ciphertext(const BufferPtr &buf) override
{
if (ct_in.size() < MAX_CIPHERTEXT_IN)
ct_in.write_buf(buf);
else
overflow = true;
}
void write_ciphertext_unbuffered(const unsigned char *data, const size_t size) override
{
if (ct_in.size() < MAX_CIPHERTEXT_IN)
ct_in.write(data, size);
else
overflow = true;
}
bool read_ciphertext_ready() const override
{
return !ct_out.empty();
}
BufferPtr read_ciphertext() override
{
return ct_out.read_buf();
}
std::string ssl_handshake_details() const override
{
if (ssl)
{
const char *ver = mbedtls_ssl_get_version(ssl);
const char *cs = mbedtls_ssl_get_ciphersuite(ssl);
if (ver && cs)
return ver + std::string("/") + cs;
}
return "";
}
bool export_keying_material(const std::string &label, unsigned char *, size_t size) override
{
return false; // not implemented in our mbed TLS implementation
}
bool did_full_handshake() override
{
return false; // fixme -- not implemented
}
const AuthCert::Ptr &auth_cert() const override
{
return authcert;
}
void mark_no_cache() override
{
// fixme -- this method should be implemented for client-side TLS session resumption tickets
}
virtual ~SSL()
{
erase();
}
protected:
SSL(MbedTLSContext *ctx, const char *hostname)
{
clear();
try
{
const Config &c = *ctx->config;
int endpoint, status;
// set pointer back to parent
parent = ctx;
// set client/server mode
if (c.mode.is_server())
{
endpoint = MBEDTLS_SSL_IS_SERVER;
authcert.reset(new AuthCert());
}
else if (c.mode.is_client())
endpoint = MBEDTLS_SSL_IS_CLIENT;
else
throw MbedTLSException("unknown client/server mode");
// init SSL configuration object
sslconf = new mbedtls_ssl_config;
mbedtls_ssl_config_init(sslconf);
mbedtls_ssl_config_defaults(sslconf,
endpoint,
MBEDTLS_SSL_TRANSPORT_STREAM,
MBEDTLS_SSL_PRESET_DEFAULT);
// init X509 cert profile
mbedtls_ssl_conf_cert_profile(sslconf, c.select_crt_profile());
// init SSL object
ssl = new mbedtls_ssl_context;
mbedtls_ssl_init(ssl);
// set minimum TLS version
#if MBEDTLS_VERSION_NUMBER > 0x03000000
mbedtls_ssl_protocol_version version;
switch (c.tls_version_min)
{
default:
case TLSVersion::Type::V1_2:
version = MBEDTLS_SSL_VERSION_TLS1_2;
break;
case TLSVersion::Type::V1_3:
version = MBEDTLS_SSL_VERSION_TLS1_3;
break;
}
mbedtls_ssl_conf_min_tls_version(sslconf, version);
#else
int major;
int minor;
switch (c.tls_version_min)
{
#if defined(MBEDTLS_SSL_MAJOR_VERSION_3) && defined(MBEDTLS_SSL_MINOR_VERSION_1)
case TLSVersion::Type::V1_0:
major = MBEDTLS_SSL_MAJOR_VERSION_3;
minor = MBEDTLS_SSL_MINOR_VERSION_1;
break;
#endif
#if defined(MBEDTLS_SSL_MAJOR_VERSION_3) && defined(MBEDTLS_SSL_MINOR_VERSION_2)
case TLSVersion::Type::V1_1:
major = MBEDTLS_SSL_MAJOR_VERSION_3;
minor = MBEDTLS_SSL_MINOR_VERSION_2;
break;
#endif
#if defined(MBEDTLS_SSL_MAJOR_VERSION_3) && defined(MBEDTLS_SSL_MINOR_VERSION_3)
default:
case TLSVersion::Type::V1_2:
major = MBEDTLS_SSL_MAJOR_VERSION_3;
minor = MBEDTLS_SSL_MINOR_VERSION_3;
break;
#endif
}
mbedtls_ssl_conf_min_version(sslconf, major, minor);
#if 0
/* force TLS 1.2 as maximum version (debugging only, disable in production) */
/* This is basically is the same as tls-version-max that OpenVPN 2.x has but hardcoded */
mbedtls_ssl_conf_max_version(sslconf, MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3);
#endif
#endif
{
// peer must present a valid certificate unless SSLConst::NO_VERIFY_PEER.
// Presenting a valid certificate can be made optional by specifying
// SSL:Const::PEER_CERT_OPTIONAL
int authmode;
if (c.flags & SSLConst::NO_VERIFY_PEER)
authmode = MBEDTLS_SSL_VERIFY_NONE;
else if (c.flags & SSLConst::PEER_CERT_OPTIONAL)
throw MbedTLSException("Optional peer verification not supported");
else
authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
mbedtls_ssl_conf_authmode(sslconf, authmode);
}
// set verify callback
mbedtls_ssl_conf_verify(sslconf, c.mode.is_server() ? verify_callback_server : verify_callback_client, this);
// Notes on SSL resume/renegotiation:
// SSL resume on server side is controlled by ssl_set_session_cache.
// SSL renegotiation is disabled here via MBEDTLS_SSL_RENEGOTIATION_DISABLED
// and ssl_legacy_renegotiation defaults to
// MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION. To enable session tickets,
// MBEDTLS_SSL_SESSION_TICKETS (compile flag) must be defined
// in mbed TLS config.h.
mbedtls_ssl_conf_renegotiation(sslconf, MBEDTLS_SSL_RENEGOTIATION_DISABLED);
if (!c.tls_cipher_list.empty())
{
set_mbedtls_cipherlist(c.tls_cipher_list);
}
else
{
#if MBEDTLS_VERSION_NUMBER < 0x03000000
/* With newer versions we trust the default */
mbedtls_ssl_conf_ciphersuites(sslconf, mbedtls_ctx_private::ciphersuites);
#endif
}
if (!c.tls_groups.empty())
{
set_mbedtls_groups(c.tls_groups);
}
// set CA chain
if (c.ca_chain)
mbedtls_ssl_conf_ca_chain(sslconf,
c.ca_chain->get(),
c.crl_chain ? c.crl_chain->get() : nullptr);
else if (!(c.flags & SSLConst::NO_VERIFY_PEER))
throw MbedTLSException("CA chain not defined");
// Set hostname for SNI or if a CA chain is configured
// In pre-mbedtls-2.x the hostname for the CA chain was set in ssl_set_ca_chain().
// From mbedtls-2.x, the hostname must be set via mbedtls_ssl_set_hostname()
// https://tls.mbed.org/kb/how-to/upgrade-2.0
if (hostname && ((c.flags & SSLConst::ENABLE_CLIENT_SNI) || c.ca_chain))
{
if (mbedtls_ssl_set_hostname(ssl, hostname))
throw MbedTLSException("mbedtls_ssl_set_hostname failed");
}
// client cert+key
if (c.local_cert_enabled)
{
if (c.external_pki)
{
// set our own certificate, supporting chain (i.e. extra-certs), and external private key
if (c.crt_chain)
{
if (mbedtls_pk_get_type(&c.crt_chain.get()->get()->pk) == MBEDTLS_PK_RSA)
{
epki_ctx.epki_enable(ctx, epki_decrypt, epki_sign, epki_key_len);
mbedtls_ssl_conf_own_cert(sslconf, c.crt_chain->get(), epki_ctx.get());
}
else
{
throw MbedTLSException("cert has unsupported type for external pki support");
}
}
else
throw MbedTLSException("cert is undefined");
}
else
{
// set our own certificate, supporting chain (i.e. extra-certs), and private key
if (c.crt_chain && c.priv_key)
mbedtls_ssl_conf_own_cert(sslconf, c.crt_chain->get(), c.priv_key->get());
else
throw MbedTLSException("cert and/or private key is undefined");
}
}
// set DH
if (c.dh)
{
status = mbedtls_ssl_conf_dh_param_ctx(sslconf, c.dh->get());
if (status < 0)
throw MbedTLSException("error in ssl_set_dh_param_ctx", status);
}
// configure ciphertext buffers
ct_in.set_frame(c.frame);
ct_out.set_frame(c.frame);
// set BIO
mbedtls_ssl_set_bio(ssl, this, ct_write_func, ct_read_func, NULL);
// set RNG
if (c.rng)
{
rng = c.rng;
mbedtls_ssl_conf_rng(sslconf, rng_callback, this);
}
else
throw MbedTLSException("RNG not defined");
// set debug callback
if (c.ssl_debug_level)
mbedtls_ssl_conf_dbg(sslconf, dbg_callback, ctx);
/* OpenVPN 2.x disables cbc_record_splitting by default, therefore
* we have to do the same here to keep compatibility.
* If not disabled, this setting will trigger bad behaviours on
* TLS1.0 and possibly on other setups */
#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
mbedtls_ssl_conf_cbc_record_splitting(sslconf,
MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED);
#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
// Apply the configuration to the SSL connection object
if (mbedtls_ssl_setup(ssl, sslconf) < 0)
throw MbedTLSException("mbedtls_ssl_setup failed");
}
catch (...)
{
erase();
throw;
}
}
mbedtls_ssl_config *sslconf; // SSL configuration parameters for SSL connection object
std::unique_ptr<int[]> allowed_ciphers; //! Hold the array that is used for setting the allowed ciphers
// must have the same lifetime as sslconf
std::unique_ptr<mbedtls_ecp_group_id[]> groups; //! Hold the array that is used for setting the curves
MbedTLSContext *parent;
private:
void set_mbedtls_cipherlist(const std::string &cipher_list)
{
auto num_ciphers = std::count(cipher_list.begin(), cipher_list.end(), ':') + 1;
allowed_ciphers.reset(new int[num_ciphers + 1]);
std::stringstream cipher_list_ss(cipher_list);
std::string ciphersuite;
int i = 0;
while (std::getline(cipher_list_ss, ciphersuite, ':'))
{
const tls_cipher_name_pair *pair = tls_get_cipher_name_pair(ciphersuite);
if (pair && pair->iana_name != ciphersuite)
{
OVPN_LOG_INFO("mbed TLS -- Deprecated cipher suite name '"
<< pair->openssl_name << "' please use IANA name ' "
<< pair->iana_name << "'");
}
auto cipher_id = mbedtls_ssl_get_ciphersuite_id(ciphersuite.c_str());
if (cipher_id != 0)
{
allowed_ciphers[i] = cipher_id;
i++;
}
else
{
/* OpenVPN 2.x ignores silently ignores unknown cipher suites with
* mbed TLS. We warn about them in OpenVPN 3.x */
OVPN_LOG_INFO("mbed TLS -- warning ignoring unknown cipher suite '"
<< ciphersuite << "' in tls-cipher");
}
}
// Last element needs to be null
allowed_ciphers[i] = 0;
mbedtls_ssl_conf_ciphersuites(sslconf, allowed_ciphers.get());
}
void set_mbedtls_groups(const std::string &tls_groups)
{
auto num_groups = std::count(tls_groups.begin(), tls_groups.end(), ':') + 1;
/* add extra space for sentinel at the end */
groups.reset(new mbedtls_ecp_group_id[num_groups + 1]);
std::stringstream groups_ss(tls_groups);
std::string group;
int i = 0;
while (std::getline(groups_ss, group, ':'))
{
const mbedtls_ecp_curve_info *ci = mbedtls_ecp_curve_info_from_name(group.c_str());
if (ci)
{
groups[i] = ci->grp_id;
i++;
}
else
{
OVPN_LOG_INFO("mbed TLS -- warning ignoring unknown group '"
<< group << "' in tls-groups");
}
}
groups[i] = MBEDTLS_ECP_DP_NONE;
mbedtls_ssl_conf_curves(sslconf, groups.get());
}
// cleartext read callback
static int ct_read_func(void *arg, unsigned char *data, size_t length)
{
try
{
SSL *self = (SSL *)arg;
const size_t actual = self->ct_in.read(data, length);
return actual > 0 ? (int)actual : CT_WOULD_BLOCK;
}
catch (...)
{
return CT_INTERNAL_ERROR;
}
}
// cleartext write callback
static int ct_write_func(void *arg, const unsigned char *data, size_t length)
{
try
{
SSL *self = (SSL *)arg;
self->ct_out.write(data, length);
return (int)length;
}
catch (...)
{
return CT_INTERNAL_ERROR;
}
}
// RNG callback -- return random data to mbed TLS
static int rng_callback(void *arg, unsigned char *data, size_t len)
{
SSL *self = (SSL *)arg;
return self->rng->rand_bytes_noexcept(data, len)
? 0
: -1; // using -1 as a general-purpose mbed TLS error code
}
static void dbg_callback(void *arg, int level, const char *filename, int linenum, const char *text)
{
MbedTLSContext *self = (MbedTLSContext *)arg;
if (level <= self->config->ssl_debug_level)
OPENVPN_LOG_NTNL("mbed TLS[" << filename << ":" << linenum << " " << level << "]: "
<< text);
}
void clear()
{
parent = nullptr;
ssl = nullptr;
sslconf = nullptr;
overflow = false;
allowed_ciphers = nullptr;
}
void erase()
{
if (ssl)
{
mbedtls_ssl_free(ssl);
mbedtls_ssl_config_free(sslconf);
delete ssl;
delete sslconf;
}
clear();
}
mbedtls_ssl_context *ssl; // underlying SSL connection object
MbedTLSPKI::PKContext epki_ctx; // external PKI context
RandomAPI::Ptr rng; // random data source
MemQStream ct_in; // write ciphertext to here
MemQStream ct_out; // read ciphertext from here
AuthCert::Ptr authcert;
bool overflow;
};
/////// start of main class implementation
// create a new SSL instance
SSLAPI::Ptr ssl() override
{
return SSL::Ptr(new SSL(this, nullptr));
}
// Get the library context. This currently does not exist for mbed TLS
SSLLib::Ctx libctx() override
{
return nullptr;
}
// like ssl() above but verify hostname against cert CommonName and/or SubjectAltName
SSLAPI::Ptr ssl(const std::string *hostname, const std::string *cache_key) override
{
return SSL::Ptr(new SSL(this, hostname ? hostname->c_str() : nullptr));
}
const Mode &mode() const override
{
return config->mode;
}
virtual ~MbedTLSContext()
{
erase();
}
constexpr static bool support_key_material_export()
{
/* mbed TLS 2.18+ can support RFC5705 but the API is painful to use */
return false;
}
#ifdef UNIT_TEST
static void load_cert_info_into_authcert(AuthCert &authcert, const std::string &cert_txt)
{
const MbedTLSPKI::X509Cert cert(cert_txt, "MbedTLSContext::load_cert_info_into_authcert", true);
// save the Common Name
authcert.cn = MbedTLSPKI::x509_get_common_name(cert.get());
// save the issuer fingerprint
load_issuer_fingerprint_into_authcert(authcert, cert.get());
// save the leaf cert serial number
load_serial_number_into_authcert(authcert, cert.get());
authcert.defined_ = true;
}
#endif
protected:
MbedTLSContext(Config *config_arg)
: config(config_arg)
{
if (config->local_cert_enabled)
{
// Verify that cert is defined
if (!config->crt_chain)
throw MbedTLSException("cert is undefined");
}
}
private:
size_t key_len() const
{
return mbedtls_pk_get_bitlen(&config->crt_chain->get()->pk) / 8;
}
// ns-cert-type verification
bool ns_cert_type_defined() const
{
return config->ns_cert_type != NSCert::NONE;
}
bool verify_ns_cert_type(const mbedtls_x509_crt *cert) const
{
#if MBEDTLS_VERSION_NUMBER < 0x03000000
if (config->ns_cert_type == NSCert::SERVER)
return bool(cert->ns_cert_type & MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER);
else if (config->ns_cert_type == NSCert::CLIENT)
return bool(cert->ns_cert_type & MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT);
else
#endif
return false;
}
// remote-cert-ku verification
bool x509_cert_ku_defined() const
{
return config->ku.size() > 0;
}
bool verify_x509_cert_ku(const mbedtls_x509_crt *cert)
{
if (mbedtls_x509_crt_has_ext_type(cert, MBEDTLS_OID_X509_EXT_EXTENDED_KEY_USAGE))
{
for (std::vector<unsigned int>::const_iterator i = config->ku.begin(); i != config->ku.end(); ++i)
{
if (mbedtls_x509_crt_check_key_usage(cert, *i))
return true;
}
}
return false;
}
// remote-cert-eku verification
bool x509_cert_eku_defined() const
{
return !config->eku.empty();
}
bool verify_x509_cert_eku(mbedtls_x509_crt *cert)
{
if (mbedtls_x509_crt_has_ext_type(cert, MBEDTLS_OID_X509_EXT_EXTENDED_KEY_USAGE))
{
mbedtls_x509_sequence *oid_seq = &cert->ext_key_usage;
while (oid_seq != nullptr)
{
mbedtls_x509_buf *oid = &oid_seq->buf;
// first compare against description
{
const char *oid_str;
const int status = mbedtls_oid_get_extended_key_usage(oid, &oid_str);
if (status >= 0 && config->eku == oid_str)
return true;
}
// next compare against OID numeric string
{
char oid_num_str[256];
const int status = mbedtls_oid_get_numeric_string(oid_num_str, sizeof(oid_num_str), oid);
if (status >= 0 && config->eku == oid_num_str)
return true;
}
oid_seq = oid_seq->next;
}
}
return false;
}
static std::string status_string(const mbedtls_x509_crt *cert, const int depth, const uint32_t *flags)
{
std::ostringstream os;
std::string status_str = "OK";
if (*flags)
status_str = "FAIL -- " + MbedTLSException::mbedtls_verify_flags_errtext(*flags);
os << "VERIFY "
<< status_str
<< " : depth=" << depth
<< std::endl
<< cert_info(cert);
return os.str();
}
protected:
static int verify_callback_client(void *arg, mbedtls_x509_crt *cert, int depth, uint32_t *flags)
{
MbedTLSContext::SSL *ssl = (MbedTLSContext::SSL *)arg;
MbedTLSContext *self = ssl->parent;
bool fail = false;
// log status
if (self->config->flags & SSLConst::LOG_VERIFY_STATUS)
OVPN_LOG_INFO(status_string(cert, depth, flags));
// notify if connection is happening with an insecurely signed cert.
// mbed TLS 3.0 does not allow the weaker signatures by default and also does not give a
// proper accessor to these fields anymore
#if MBEDTLS_VERSION_NUMBER < 0x03000000
if (cert->sig_md == MBEDTLS_MD_MD5)
{
ssl->tls_warnings |= SSLAPI::TLS_WARN_SIG_MD5;
}
if (cert->sig_md == MBEDTLS_MD_SHA1)
{
ssl->tls_warnings |= SSLAPI::TLS_WARN_SIG_SHA1;
}
#endif
// leaf-cert verification
if (depth == 0)
{
// verify ns-cert-type
if (self->ns_cert_type_defined() && !self->verify_ns_cert_type(cert))
{
OVPN_LOG_INFO("VERIFY FAIL -- bad ns-cert-type in leaf certificate");
fail = true;
}
// verify X509 key usage
if (self->x509_cert_ku_defined() && !self->verify_x509_cert_ku(cert))
{
OVPN_LOG_INFO("VERIFY FAIL -- bad X509 key usage in leaf certificate");
fail = true;
}
// verify X509 extended key usage
if (self->x509_cert_eku_defined() && !self->verify_x509_cert_eku(cert))
{
OVPN_LOG_INFO("VERIFY FAIL -- bad X509 extended key usage in leaf certificate");
fail = true;
}
// verify tls-remote
if (!self->config->tls_remote.empty())
{
const std::string subject = TLSRemote::sanitize_x509_name(MbedTLSPKI::x509_get_subject(cert));
const std::string common_name = TLSRemote::sanitize_common_name(MbedTLSPKI::x509_get_common_name(cert));
TLSRemote::log(self->config->tls_remote, subject, common_name);
if (!TLSRemote::test(self->config->tls_remote, subject, common_name))
{
OVPN_LOG_INFO("VERIFY FAIL -- tls-remote match failed");
fail = true;
}
}
// verify-x509-name
const VerifyX509Name &verify_x509 = self->config->verify_x509_name;
if (verify_x509.get_mode() != VerifyX509Name::VERIFY_X509_NONE)
{
bool res = false;
switch (verify_x509.get_mode())
{
case VerifyX509Name::VERIFY_X509_SUBJECT_DN:
res = verify_x509.verify(MbedTLSPKI::x509_get_subject(cert, true));
break;
case VerifyX509Name::VERIFY_X509_SUBJECT_RDN:
case VerifyX509Name::VERIFY_X509_SUBJECT_RDN_PREFIX:
res = verify_x509.verify(MbedTLSPKI::x509_get_common_name(cert));
break;
default:
break;
}
if (!res)
{
OVPN_LOG_INFO("VERIFY FAIL -- verify-x509-name failed");
fail = true;
}
}
}
if (fail)
*flags |= MBEDTLS_X509_BADCERT_OTHER;
return 0;
}
static int verify_callback_server(void *arg, mbedtls_x509_crt *cert, int depth, uint32_t *flags)
{
MbedTLSContext::SSL *ssl = (MbedTLSContext::SSL *)arg;
MbedTLSContext *self = ssl->parent;
bool fail = false;
if (depth == 1) // issuer cert
{
// save the issuer cert fingerprint
if (ssl->authcert)
{
if (!load_issuer_fingerprint_into_authcert(*ssl->authcert, cert))
{
OVPN_LOG_INFO("VERIFY FAIL -- SHA1 calculation failed.");
fail = true;
}
}
}
else if (depth == 0) // leaf-cert
{
// verify ns-cert-type
if (self->ns_cert_type_defined() && !self->verify_ns_cert_type(cert))
{
OVPN_LOG_INFO("VERIFY FAIL -- bad ns-cert-type in leaf certificate");
fail = true;
}
// verify X509 key usage
if (self->x509_cert_ku_defined() && !self->verify_x509_cert_ku(cert))
{
OVPN_LOG_INFO("VERIFY FAIL -- bad X509 key usage in leaf certificate");
fail = true;
}
// verify X509 extended key usage
if (self->x509_cert_eku_defined() && !self->verify_x509_cert_eku(cert))
{
OVPN_LOG_INFO("VERIFY FAIL -- bad X509 extended key usage in leaf certificate");
fail = true;
}
if (ssl->authcert)
{
// save the Common Name
ssl->authcert->cn = MbedTLSPKI::x509_get_common_name(cert);
// save the leaf cert serial number
load_serial_number_into_authcert(*ssl->authcert, cert);
ssl->authcert->defined_ = true;
}
}
if (fail)
*flags |= MBEDTLS_X509_BADCERT_OTHER;
return 0;
}
Config::Ptr config;
private:
static std::string cert_info(const mbedtls_x509_crt *cert, const char *prefix = nullptr)
{
const size_t buf_size = 4096;
std::unique_ptr<char[]> buf(new char[buf_size]);
const int size = mbedtls_x509_crt_info(buf.get(), buf_size, prefix ? prefix : "", cert);
if (size >= 0)
return std::string(buf.get());
else
return "error rendering cert";
}
void erase()
{
}
static int epki_decrypt(void *arg,
#if MBEDTLS_VERSION_NUMBER < 0x03000000
int mode,
#endif
size_t *olen,
const unsigned char *input,
unsigned char *output,
size_t output_max_len)
{
OVPN_LOG_INFO("MbedTLSContext::epki_decrypt is unimplemented"
<< " output_max_len=" << output_max_len);
return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
}
static int epki_sign(void *arg,
int (*f_rng)(void *, unsigned char *, size_t),
void *p_rng,
#if MBEDTLS_VERSION_NUMBER < 0x03000000
int mode,
#endif
mbedtls_md_type_t md_alg,
unsigned int hashlen,
const unsigned char *hash,
unsigned char *sig)
{
MbedTLSContext *self = (MbedTLSContext *)arg;
try
{
#if MBEDTLS_VERSION_NUMBER < 0x03000000
if (mode == MBEDTLS_RSA_PRIVATE)
#else
if (true)
#endif
{
size_t digest_prefix_len = 0;
const unsigned char *digest_prefix = nullptr;
/* get signature type */
switch (md_alg)
{
case MBEDTLS_MD_NONE:
break;
case MBEDTLS_MD_MD5:
digest_prefix = PKCS1::DigestPrefix::MD5;
digest_prefix_len = sizeof(PKCS1::DigestPrefix::MD5);
break;
case MBEDTLS_MD_SHA1:
digest_prefix = PKCS1::DigestPrefix::SHA1;
digest_prefix_len = sizeof(PKCS1::DigestPrefix::SHA1);
break;
case MBEDTLS_MD_SHA256:
digest_prefix = PKCS1::DigestPrefix::SHA256;
digest_prefix_len = sizeof(PKCS1::DigestPrefix::SHA256);
break;
case MBEDTLS_MD_SHA384:
digest_prefix = PKCS1::DigestPrefix::SHA384;
digest_prefix_len = sizeof(PKCS1::DigestPrefix::SHA384);
break;
case MBEDTLS_MD_SHA512:
digest_prefix = PKCS1::DigestPrefix::SHA512;
digest_prefix_len = sizeof(PKCS1::DigestPrefix::SHA512);
break;
default:
OVPN_LOG_INFO("MbedTLSContext::epki_sign unrecognized hash_id"
#if MBEDTLS_VERSION_NUMBER < 0x03000000
<< "mode=" << mode
#endif
<< " md_alg=" << md_alg << " hashlen=" << hashlen);
return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
}
/* concatenate digest prefix with hash */
BufferAllocated from_buf(digest_prefix_len + hashlen, 0);
if (digest_prefix_len)
from_buf.write(digest_prefix, digest_prefix_len);
from_buf.write(hash, hashlen);
/* convert from_buf to base64 */
const std::string from_b64 = base64->encode(from_buf);
/* get signature */
std::string sig_b64;
const bool status = self->config->external_pki->sign(self->config->external_pki_alias, from_b64, sig_b64, "RSA_PKCS1_PADDING", "", "");
if (!status)
throw ssl_external_pki("MbedTLS: could not obtain signature");
/* decode base64 signature to binary */
const size_t len = self->key_len();
Buffer sigbuf(sig, len, false);
base64->decode(sigbuf, sig_b64);
/* verify length */
if (sigbuf.size() != len)
throw ssl_external_pki("mbed TLS: incorrect signature length");
/* success */
return 0;
}
else
{
OVPN_LOG_INFO("MbedTLSContext::epki_sign unrecognized parameters"
#if MBEDTLS_VERSION_NUMBER < 0x03000000
<< "mode=" << mode
#endif
<< " md_alg=" << md_alg << " hashlen=" << hashlen);
return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
}
}
catch (const std::exception &e)
{
OPENVPN_LOG("MbedTLSContext::epki_sign exception: " << e.what());
return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
}
}
static size_t epki_key_len(void *arg)
{
MbedTLSContext *self = (MbedTLSContext *)arg;
return self->key_len();
}
static void load_serial_number_into_authcert(AuthCert &authcert, const mbedtls_x509_crt *cert)
{
const mbedtls_x509_buf *s = &cert->serial;
if (s->len > 0 && s->len <= authcert.serial.size())
{
const size_t offset = authcert.serial.size() - s->len;
std::memset(authcert.serial.number(), 0, offset);
std::memcpy(authcert.serial.number() + offset, s->p, s->len);
}
}
static bool load_issuer_fingerprint_into_authcert(AuthCert &authcert, const mbedtls_x509_crt *cert)
{
const int SHA_DIGEST_LEN = 20;
static_assert(sizeof(AuthCert::issuer_fp) == SHA_DIGEST_LEN, "size inconsistency");
#if MBEDTLS_VERSION_NUMBER < 0x02070000
// mbed TLS 2.7.0 and newer deprecates mbedtls_sha1()
// in favour of mbedtls_sha1_ret().
// We support for older mbed TLS versions
// to be able to build on Debian 9 and Ubuntu 16.
mbedtls_sha1(cert->raw.p, cert->raw.len, authcert->issuer_fp);
#elif MBEDTLS_VERSION_NUMBER < 0x03000000
if (mbedtls_sha1_ret(cert->raw.p, cert->raw.len, authcert.issuer_fp))
return false;
#else
// mbedtls_sha1_ret is renamed to mbedtls_sha1 in 3.0
if (mbedtls_sha1(cert->raw.p, cert->raw.len, authcert.issuer_fp))
return false;
#endif
return true;
}
};
inline const std::string get_ssl_library_version()
{
unsigned int ver = mbedtls_version_get_number();
std::string version = "mbed TLS "
+ std::to_string((ver >> 24) & 0xff)
+ "." + std::to_string((ver >> 16) & 0xff)
+ "." + std::to_string((ver >> 8) & 0xff);
return version;
}
} // namespace openvpn
#endif
|