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
// Copyright (c) 2019 Reyk Floeter <contact@reykfloeter.com> // // Permission to use, copy, modify, and distribute this software for any // purpose with or without fee is hereby granted, provided that the above // copyright notice and this permission notice appear in all copies. // // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES // WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF // MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR // ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES // WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN // ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF // OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. // // The documentation is based on libtls man pages of the LibreSSL project, // provided under the same ISC-style license (see https://www.libressl.org/): // // Copyright (c) 2015, 2016 Bob Beck <beck@openbsd.org> // Copyright (c) 2016 Brent Cook <bcook@openbsd.org> // Copyright (c) 2017 Claudio Jeker <claudio@openbsd.org> // Copyright (c) 2015 Doug Hogan <doug@openbsd.org> // Copyright (c) 2017 Ingo Schwarze <schwarze@openbsd.org> // Copyright (c) 2014, 2015, 2016, 2017, 2018 Joel Sing <jsing@openbsd.org> // Copyright (c) 2015 Reyk Floeter <reyk@openbsd.org> // Copyright (c) 2014, 2015 Ted Unangst <tedu@openbsd.org> // // Permission to use, copy, modify, and distribute this software for any // purpose with or without fee is hereby granted, provided that the above // copyright notice and this permission notice appear in all copies. // // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES // WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF // MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR // ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES // WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN // ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF // OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. //! [`Tls`] clients and servers are configured with the [`TlsConfig`] //! configuration context and its helper funtions. //! //! # Example //! //! ``` //! use libtls::{config::{self, TlsConfig}, error}; //! //! fn tls_server_config() -> error::Result<TlsConfig> { //! let mut tls_config = TlsConfig::new()?; //! //! let valid_cert = include_bytes!("../tests/eccert.crt"); //! let valid_key = include_bytes!("../tests/eccert.key"); //! //! // Sets the key pair and wipes the private key file from memory afterwards //! let res = tls_config.set_keypair_mem(valid_cert, valid_key); //! config::unload_file(valid_key.to_vec()); //! res?; //! //! // The following examples are all set by default and it is not //! // not required to set them. //! tls_config.set_ciphers("secure")?; //! tls_config.set_protocols(libtls::TLS_PROTOCOLS_DEFAULT)?; //! tls_config.prefer_ciphers_server(); //! tls_config.verify(); //! //! Ok(tls_config) //! } //! # fn main() { //! # let tls_config = tls_server_config().unwrap(); //! # } //! ``` //! //! [`Tls`]: ../tls/struct.Tls.html //! [`TlsConfig`]: struct.TlsConfig.html use crate::{error, tls::Tls, util::*}; use std::{ collections::HashMap, ffi::{CStr, CString}, io, marker::{Send, Sync}, os::unix::io::RawFd, path::{Path, PathBuf}, }; /// The TLS configuration context for [`Tls`] connections. /// /// Before a [`Tls`] connection is created, a configuration must be created. /// Several methods exist to change the options of the configuration; see /// [`set_protocols`], [`ocsp_require_stapling`], [`verify`]. /// /// [`Tls`]: ../tls/struct.Tls.html /// [`ocsp_require_stapling`]: #method.ocsp_require_stapling /// [`set_protocols`]: #method.set_protocols /// [`tls_config_load_file`]: ../fn.tls_config_load_file.html /// [`verify`]: #method.verify #[derive(Debug)] pub struct TlsConfig(pub(crate) *mut libtls_sys::tls_config); impl TlsConfig { /// Create a new configuration. /// /// The `new` function allocates, initializes, and returns a new default /// configuration object that can be used for future [`Tls`] connections. /// /// # Errors /// /// Returns an [`io::Error`] on error or an out of memory condition. /// /// # Example /// /// ``` /// # use libtls::{config::TlsConfig, error::Result}; /// # fn tls_config() -> Result<()> { /// let config = TlsConfig::new()?; /// # Ok(()) /// # } /// # tls_config().unwrap(); /// ``` /// /// # See also /// /// [`tls_config_new(3)`](https://man.openbsd.org/tls_config_new.3) /// /// [`Tls`]: ../tls/struct.Tls.html /// [`io::Error`]: https://doc.rust-lang.org/std/io/struct.Error.html pub fn new() -> io::Result<Self> { let config = unsafe { libtls_sys::tls_config_new() }; if config.is_null() { Err(io::Error::last_os_error()) } else { Ok(TlsConfig(config)) } } /// Wrap a raw C `tls_config` object. /// /// # Safety /// /// This function assumes that the raw pointer is valid, and takes /// ownership of the libtls object. /// Do not call `tls_free` yourself, since the `drop` destructor will /// take care of it. /// /// # Panics /// /// Panics if `config` is a null pointer. pub unsafe fn from_sys(config: *mut libtls_sys::tls_config) -> Self { if config.is_null() { panic!(io::Error::last_os_error()) } TlsConfig(config) } /// Add additional files of a public and private key pair. /// /// The `add_keypair_file` method adds an additional public certificate, and /// private key from the specified files, used as an alternative certificate /// for Server Name Indication (server only). /// /// # Example /// /// ``` /// # use libtls::{config::TlsConfig, error::Result}; /// # fn tls_config() -> Result<()> { /// let mut config = TlsConfig::new()?; /// assert!(config.add_keypair_file("does_not_exist.crt", "does_not_exist.key").is_err()); /// # Ok(()) /// # } /// # tls_config().unwrap(); /// ``` /// /// # See also /// /// [`tls_config_add_keypair_file(3)`](https://man.openbsd.org/tls_config_add_keypair_file.3) pub fn add_keypair_file<P: AsRef<Path>>( &mut self, cert_file: P, key_file: P, ) -> error::Result<()> { call_file2( self, (cert_file, "cert file"), (key_file, "key file"), libtls_sys::tls_config_add_keypair_file, ) } /// Add an additional public and private key pair from memory. /// /// The `add_keypair_mem` method adds an additional public certificate, and /// private key from memory, used as an alternative certificate for Server /// Name Indication (server only). /// /// # Example /// /// ``` /// # use libtls::{config::TlsConfig, error::Result}; /// # fn tls_config() -> Result<()> { /// let mut config = TlsConfig::new()?; /// let valid_cert = include_bytes!("../tests/eccert.crt"); /// let valid_key = include_bytes!("../tests/eccert.key"); /// config.add_keypair_mem(valid_cert, valid_key)?; /// # Ok(()) /// # } /// # tls_config().unwrap(); /// ``` /// /// # See also /// /// [`tls_config_add_keypair_mem(3)`](https://man.openbsd.org/tls_config_add_keypair_mem.3) pub fn add_keypair_mem(&mut self, cert: &[u8], key: &[u8]) -> error::Result<()> { cvt(self, unsafe { libtls_sys::tls_config_add_keypair_mem( self.0, cert.as_ptr(), cert.len(), key.as_ptr(), key.len(), ) }) } /// Add additional files of a public and private key pair and an OCSP staple. /// /// The `add_keypair_ocsp_file` method adds an additional public certificate, /// private key, and DER-encoded OCSP staple from the specified files, used /// as an alternative certificate for Server Name Indication (server only). /// /// # See also /// /// [`add_keypair_file`](#method.add_keypair_file), /// [`tls_config_add_keypair_ocsp_file(3)`](https://man.openbsd.org/tls_config_add_keypair_ocsp_file.3) pub fn add_keypair_ocsp_file<P: AsRef<Path>>( &mut self, cert_file: P, key_file: P, ocsp_staple_file: P, ) -> error::Result<()> { call_file3( self, (cert_file, "cert file"), (key_file, "key file"), (ocsp_staple_file, "ocsp staple file"), libtls_sys::tls_config_add_keypair_ocsp_file, ) } /// Add an additional public and private key pair and OCSP staple from memory. /// /// The `add_keypair_ocsp_mem` method adds an additional public certificate, /// private key, and DER-encoded OCSP staple from memory, used as an /// alternative certificate for Server Name Indication (server only). /// /// # See also /// /// [`add_keypair_mem`](#method.add_keypair_mem), /// [`tls_config_add_keypair_ocsp_mem(3)`](https://man.openbsd.org/tls_config_add_keypair_ocsp_mem.3) pub fn add_keypair_ocsp_mem( &mut self, cert: &[u8], key: &[u8], ocsp_staple: &[u8], ) -> error::Result<()> { cvt(self, unsafe { libtls_sys::tls_config_add_keypair_ocsp_mem( self.0, cert.as_ptr(), cert.len(), key.as_ptr(), key.len(), ocsp_staple.as_ptr(), ocsp_staple.len(), ) }) } /// Set the ALPN protocols that are supported. /// /// The `set_alpn` method sets the ALPN protocols that are supported. The /// alpn string is a comma separated list of protocols, in order of /// preference. /// /// # Example /// /// ``` /// # use libtls::{config::TlsConfig, error::Result}; /// # fn tls_config() -> Result<()> { /// let mut config = TlsConfig::new()?; /// /// // The `h2` ALPN is used by HTTP/2: /// config.set_alpn("h2")?; /// # Ok(()) /// # } /// # tls_config().unwrap(); /// ``` /// /// # See also /// /// [`tls_config_set_alpn(3)`](https://man.openbsd.org/tls_config_set_alpn.3) pub fn set_alpn(&mut self, alpn: &str) -> error::Result<()> { call_string1(self, alpn, libtls_sys::tls_config_set_alpn) } /// Set the CA file. /// /// The `set_ca_file` method sets the filename used to load a file containing /// the root certificates. The default filename can be returned with the /// [`default_ca_cert_file`] function. /// /// # See also /// /// [`add_keypair_file`](#method.add_keypair_file.html), /// [`tls_config_set_ca_file(3)`](https://man.openbsd.org/tls_config_set_ca_file.3) /// /// [`default_ca_cert_file`]: fn.default_ca_cert_file.html pub fn set_ca_file<P: AsRef<Path>>(&mut self, ca_file: P) -> error::Result<()> { call_file1( self, (ca_file, "ca file"), libtls_sys::tls_config_set_ca_file, ) } /// Set the path that should be searched for the CA files. /// /// The `set_ca_path` method sets sets the path (directory) which should be /// searched for root certificates. /// /// # See also /// /// [`set_ca_file`](#method.set_ca_file.html), /// [`tls_config_set_ca_path(3)`](https://man.openbsd.org/tls_config_set_ca_path.3) pub fn set_ca_path<P: AsRef<Path>>(&mut self, ca_path: P) -> error::Result<()> { call_file1( self, (ca_path, "ca path"), libtls_sys::tls_config_set_ca_path, ) } /// Set the CA from memory. /// /// The `set_ca_mem` method directly sets the root certificates directly from memory. /// /// /// # See also /// /// [`add_keypair_mem`](#method.add_keypair_mem), /// [`tls_config_set_ca_mem(3)`](https://man.openbsd.org/tls_config_set_ca_mem.3) pub fn set_ca_mem(&mut self, ca: &[u8]) -> error::Result<()> { cvt(self, unsafe { libtls_sys::tls_config_set_ca_mem(self.0, ca.as_ptr(), ca.len()) }) } /// Set the CA file from memory. /// /// The `set_ca_mem` method sets the root certificates directly from memory. /// /// # See also /// /// [`add_keypair_mem`](#method.add_keypair_mem.html), /// [`tls_config_set_ca_mem(3)`](https://man.openbsd.org/tls_config_set_ca_mem.3) pub fn tls_config_set_ca_mem(&mut self, ca: &[u8]) -> error::Result<()> { cvt(self, unsafe { libtls_sys::tls_config_set_ca_mem(self.0, ca.as_ptr(), ca.len()) }) } /// Set the public certificate file. /// /// The `set_cert_file` method sets file from which the public certificate /// will be read. /// /// # See also /// /// [`add_keypair_file`](#method.add_keypair_file), /// [`tls_config_set_cert_file(3)`](https://man.openbsd.org/tls_config_set_cert_file.3) pub fn set_cert_file<P: AsRef<Path>>(&mut self, cert_file: P) -> error::Result<()> { call_file1( self, (cert_file, "cert file"), libtls_sys::tls_config_set_cert_file, ) } /// Set the public certificate from memory. /// /// The `set_cert_mem` method sets the public certificate directly from /// memory. /// /// # See also /// /// [`add_keypair_mem`](#method.add_keypair_mem), /// [`tls_config_set_cert_mem(3)`](https://man.openbsd.org/tls_config_set_cert_mem.3) pub fn set_cert_mem(&mut self, cert: &[u8]) -> error::Result<()> { cvt(self, unsafe { libtls_sys::tls_config_set_cert_mem(self.0, cert.as_ptr(), cert.len()) }) } /// Set the list of cipher that may be used. /// /// The `set_ciphers` method sets the list of ciphers that may be used. /// Lists of ciphers are specified by name, and the permitted names are: /// /// * `secure` (or alias `default`) /// * `compat` /// * `legacy` /// * `insecure` (or alias `all`) /// /// Alternatively, libssl cipher strings can be specified. See the CIPHERS /// section of [`openssl(1)`] for further information. /// /// # Example /// /// ``` /// # use libtls::{config::TlsConfig, error::Result}; /// # fn tls_config() -> Result<()> { /// let mut config = TlsConfig::new()?; /// /// // Only use `compat` if you run into problems with the `secure` default! /// config.set_ciphers("compat")?; /// # Ok(()) /// # } /// # tls_config().unwrap(); /// ``` /// /// # See also /// /// [`openssl(1)`], /// [`tls_config_set_ciphers(3)`](https://man.openbsd.org/tls_config_set_ciphers.3) /// /// [`openssl(1)`]: http://man.openbsd.org/openssl.1#CIPHERS pub fn set_ciphers(&mut self, ciphers: &str) -> error::Result<()> { call_string1(self, ciphers, libtls_sys::tls_config_set_ciphers) } /// Set the CRL file. /// /// The `set_crl_file` method sets the filename used to load a file /// containing the Certificate Revocation List (CRL). /// /// # See also /// /// [`add_keypair_file`](#method.add_keypair_file.html), /// [`tls_config_set_crl_file(3)`](https://man.openbsd.org/tls_config_set_crl_file.3) pub fn set_crl_file<P: AsRef<Path>>(&mut self, crl_file: P) -> error::Result<()> { call_file1( self, (crl_file, "crl file"), libtls_sys::tls_config_set_crl_file, ) } /// Set the CRL from memory. /// /// The `set_crl_mem` method sets the Certificate Revocation List (CRL) /// directly from memory. /// /// # See also /// /// [`add_keypair_mem`](#method.add_keypair_mem.html), /// [`tls_config_set_crl_mem(3)`](https://man.openbsd.org/tls_config_set_crl_mem.3) pub fn set_crl_mem(&mut self, crl: &[u8]) -> error::Result<()> { cvt(self, unsafe { libtls_sys::tls_config_set_crl_mem(self.0, crl.as_ptr(), crl.len()) }) } /// Set the parameters of an Diffie-Hellman Ephemeral (DHE) key exchange. /// /// The `set_dheparams` method specifies the parameters that will be used /// during Diffie-Hellman Ephemeral (DHE) key exchange. Possible values are /// `none`, `auto` and `legacy`. In `auto` mode, the key size for the /// ephemeral key is automatically selected based on the size of the private /// key being used for signing. In `legacy` mode, 1024 bit ephemeral keys /// are used. The default value is `none`, which disables DHE key exchange. /// /// # Example /// /// ``` /// # use libtls::{config::TlsConfig, error::Result}; /// # fn tls_config() -> Result<()> { /// let mut config = TlsConfig::new()?; /// config.set_dheparams("auto")?; /// # Ok(()) /// # } /// # tls_config().unwrap(); /// ``` /// /// # See also /// /// [`tls_config_set_dheparams(3)`](https://man.openbsd.org/tls_config_set_dheparams.3) pub fn set_dheparams(&mut self, dheparams: &str) -> error::Result<()> { call_string1(self, dheparams, libtls_sys::tls_config_set_dheparams) } /// The `set_ecdhecurve` method was replaced by [set_ecdhecurves](#method.set_ecdhecurves). #[deprecated( since = "2.6.1-LibreSSL", note = "Replaced by [set_ecdhecurves](#method.set_ecdhecurves)." )] pub fn set_ecdhecurve(&mut self, ecdhecurve: &str) -> error::Result<()> { call_string1(self, ecdhecurve, libtls_sys::tls_config_set_ecdhecurve) } /// Set the curves of an Elliptic Curve Diffie-Hellman Ephemeral (ECDHE) key exchange. /// /// The `set_ecdhecurves` method specifies the names of the elliptic curves /// that may be used during Elliptic Curve Diffie-Hellman Ephemeral (ECDHE) /// key exchange. This is a comma separated list, given in order of /// preference. The special value of "default" will use the default curves /// (currently X25519, P-256 and P-384). This function replaces /// [`set_ecdhecurve`], which is deprecated. /// /// # Example /// /// ``` /// # use libtls::{config::TlsConfig, error::Result}; /// # fn tls_config() -> Result<()> { /// let mut config = TlsConfig::new()?; /// config.set_ecdhecurves("X25519,P-384")?; /// # Ok(()) /// # } /// # tls_config().unwrap(); /// ``` /// /// # See also /// /// [`tls_config_set_ecdhecurves(3)`](https://man.openbsd.org/tls_config_set_ecdhecurves.3) /// /// [`set_ecdhecurve`]: #method.set_ecdhecurve pub fn set_ecdhecurves(&mut self, ecdhecurves: &str) -> error::Result<()> { call_string1(self, ecdhecurves, libtls_sys::tls_config_set_ecdhecurves) } /// Set the private key file. /// /// The `set_key_file` method sets the file from which the private key will /// be read. /// /// # See also /// /// [`add_keypair_file`](#method.add_keypair_file), /// [`tls_config_set_key_file(3)`](https://man.openbsd.org/tls_config_set_key_file.3) pub fn set_key_file<P: AsRef<Path>>(&mut self, key_file: P) -> error::Result<()> { call_file1( self, (key_file, "key file"), libtls_sys::tls_config_set_key_file, ) } /// Set the private key from memory. /// /// The `set_key_mem` method directly sets the private key from memory. /// /// # See also /// /// [`add_keypair_mem`](#method.add_keypair_mem), /// [`tls_config_set_key_mem(3)`](https://man.openbsd.org/tls_config_set_key_mem.3) pub fn set_key_mem(&mut self, key: &[u8]) -> error::Result<()> { cvt(self, unsafe { libtls_sys::tls_config_set_key_mem(self.0, key.as_ptr(), key.len()) }) } /// Set the files of the public and private key pair. /// /// The `set_keypair_file` method sets the files from which the public /// certificate, and private key will be read. /// /// # See also /// /// [`add_keypair_file`](#method.add_keypair_file), /// [`tls_config_set_keypair_file(3)`](https://man.openbsd.org/tls_config_set_keypair_file.3) pub fn set_keypair_file<P: AsRef<Path>>( &mut self, cert_file: P, key_file: P, ) -> error::Result<()> { call_file2( self, (cert_file, "cert file"), (key_file, "key file"), libtls_sys::tls_config_set_keypair_file, ) } /// Set the public and private key pair from memory. /// /// The `set_keypair_mem` method directly sets the public certificate, and /// private key from memory. /// /// # See also /// /// [`add_keypair_mem`](#method.add_keypair_mem), /// [`tls_config_set_keypair_mem(3)`](https://man.openbsd.org/tls_config_set_keypair_mem.3) pub fn set_keypair_mem(&mut self, cert: &[u8], key: &[u8]) -> error::Result<()> { cvt(self, unsafe { libtls_sys::tls_config_set_keypair_mem( self.0, cert.as_ptr(), cert.len(), key.as_ptr(), key.len(), ) }) } /// Set the files of a public and private key pair and an OCSP staple. /// /// The `set_keypair_ocsp_file` method sets the public certificate, /// private key, and DER-encoded OCSP staple from the specified files. /// /// # See also /// /// [`add_keypair_ocsp_file`](#method.add_keypair_ocsp_file), /// [`tls_config_set_keypair_ocsp_file(3)`](https://man.openbsd.org/tls_config_set_keypair_ocsp_file.3) pub fn set_keypair_ocsp_file<P: AsRef<Path>>( &mut self, cert_file: P, key_file: P, ocsp_staple_file: P, ) -> error::Result<()> { call_file3( self, (cert_file, "cert file"), (key_file, "key file"), (ocsp_staple_file, "ocsp staple file"), libtls_sys::tls_config_set_keypair_ocsp_file, ) } /// Set the public and private key pair and an OCSP staple from memory. /// /// The `set_keypair_ocsp_mem` method sets the public certificate, /// private key, and DER-encoded OCSP staple directly from memory. /// /// # See also /// /// [`add_keypair_ocsp_mem`](#method.add_keypair_ocsp_mem), /// [`tls_config_set_keypair_ocsp_mem(3)`](https://man.openbsd.org/tls_config_set_keypair_ocsp_mem.3) pub fn set_keypair_ocsp_mem( &mut self, cert: &[u8], key: &[u8], ocsp_staple: &[u8], ) -> error::Result<()> { cvt(self, unsafe { libtls_sys::tls_config_set_keypair_ocsp_mem( self.0, cert.as_ptr(), cert.len(), key.as_ptr(), key.len(), ocsp_staple.as_ptr(), ocsp_staple.len(), ) }) } /// Set the OCSP staple from memory. /// /// The `set_keypair_ocsp_mem` method sets a DER-encoded OCSP response to be /// stapled during the TLS handshake from memory. /// /// # See also /// /// [`add_keypair_mem`](#method.add_keypair_mem), /// [`tls_config_set_ocsp_staple_mem(3)`](https://man.openbsd.org/tls_config_set_ocsp_staple_mem.3) pub fn set_ocsp_staple_mem(&mut self, ocsp_staple: &[u8]) -> error::Result<()> { cvt(self, unsafe { libtls_sys::tls_config_set_ocsp_staple_mem( self.0, ocsp_staple.as_ptr(), ocsp_staple.len(), ) }) } /// Set the OCSP staple file. /// /// The `set_keypair_ocsp_mem` method sets a DER-encoded OCSP response to be /// stapled during the TLS handshake from the specified file. /// /// /// # See also /// /// [`add_keypair_file`](#method.add_keypair_file), /// [`tls_config_set_ocsp_staple_file(3)`](https://man.openbsd.org/tls_config_set_ocsp_staple_file.3) pub fn set_ocsp_staple_file<P: AsRef<Path>>( &mut self, ocsp_staple_file: P, ) -> error::Result<()> { call_file1( self, (ocsp_staple_file, "ocsp staple file"), libtls_sys::tls_config_set_ocsp_staple_file, ) } /// Set which versions of the TLS protocol may be used. /// /// The `set_protocols` method specifies which versions of the TLS protocol /// may be used. Possible values are the bitwise OR of: /// /// * [`TLS_PROTOCOL_TLSv1_0`] /// * [`TLS_PROTOCOL_TLSv1_1`] /// * [`TLS_PROTOCOL_TLSv1_2`] /// /// Additionally, the values [`TLS_PROTOCOL_TLSv1`] (TLSv1.0, TLSv1.1 and /// TLSv1.2), [`TLS_PROTOCOLS_ALL`] (all supported protocols) and /// [`TLS_PROTOCOLS_DEFAULT`] (TLSv1.2 only) may be used. /// /// # Example /// /// ``` /// # use libtls::{config::{self, TlsConfig}, error::Result}; /// # fn tls_config() -> Result<()> { /// let mut config = TlsConfig::new()?; /// let protocols = config::parse_protocols("tlsv1.1,tlsv1.2")?; /// config.set_protocols(protocols)?; /// # Ok(()) /// # } /// # tls_config().unwrap(); /// ``` /// /// # See also /// /// [`parse_protocols`](fn.parse_protocols.html), /// [`tls_config_set_protocols(3)`](https://man.openbsd.org/tls_config_set_protocols.3) /// /// [`TLS_PROTOCOLS_ALL`]: ../constant.TLS_PROTOCOLS_ALL.html /// [`TLS_PROTOCOLS_DEFAULT`]: ../constant.TLS_PROTOCOLS_DEFAULT.html /// [`TLS_PROTOCOL_TLSv1`]: ../constant.TLS_PROTOCOL_TLSv1.html /// [`TLS_PROTOCOL_TLSv1_0`]: ../constant.TLS_PROTOCOL_TLSv1_0.html /// [`TLS_PROTOCOL_TLSv1_1`]: ../constant.TLS_PROTOCOL_TLSv1_1.html /// [`TLS_PROTOCOL_TLSv1_2`]: ../constant.TLS_PROTOCOL_TLSv1_2.html pub fn set_protocols(&mut self, protocols: u32) -> error::Result<()> { call_arg1(self, protocols, libtls_sys::tls_config_set_protocols) } /// Set a file descriptor to manage data for TLS sessions. /// /// The `set_session_fd` method sets a file descriptor to be used to manage /// data for TLS sessions (client only). The given file descriptor must be a /// regular file and be owned by the current user, with permissions being /// restricted to only allow the owner to read and write the file (0600). If /// the file has a non-zero length, the client will attempt to read session /// data from this file and resume the previous TLS session with the server. /// Upon a successful handshake the file will be updated with current session /// data, if available. The caller is responsible for closing this file /// descriptor, after all [`TLS`] contexts that have been configured to use it /// have been [dropped]. /// /// # See also /// /// [`tls_config_set_session_fd(3)`](https://man.openbsd.org/tls_config_set_session_fd.3) /// /// [`Tls`]: ../tls/struct.Tls.html /// [dropped]: ../tls/struct.Tls.html#impl-Drop pub fn set_session_fd(&mut self, session_fd: RawFd) -> error::Result<()> { call_arg1(self, session_fd, libtls_sys::tls_config_set_session_fd) } /// Set the certificate verification depth. /// /// The `set_verify_depth` method limits the number of intermediate /// certificates that will be followed during certificate validation. /// /// # See also /// /// [`tls_config_set_verify_depth(3)`](https://man.openbsd.org/tls_config_set_verify_depth.3) pub fn set_verify_depth(&mut self, verify_depth: usize) -> error::Result<()> { call_arg1( self, verify_depth as i32, libtls_sys::tls_config_set_verify_depth, ) } /// Prefer ciphers in the client's cipher list. /// /// The `prefer_ciphers_client` method prefers ciphers in the client's cipher /// list when selecting a cipher suite (server only). This is considered to /// be less secure than preferring the server's list. /// /// # See also /// /// [`tls_config_prefer_ciphers_client(3)`](https://man.openbsd.org/tls_config_prefer_ciphers_client.3) pub fn prefer_ciphers_client(&mut self) { unsafe { libtls_sys::tls_config_prefer_ciphers_client(self.0) } } /// Prefer ciphers in the servers's cipher list. /// /// The `prefer_ciphers_server` method prefers ciphers in the server's cipher /// list when selecting a cipher suite (server only). This is considered to /// be more secure than preferring the client's list and is the default. /// /// # See also /// /// [`tls_config_prefer_ciphers_server(3)`](https://man.openbsd.org/tls_config_prefer_ciphers_server.3) pub fn prefer_ciphers_server(&mut self) { unsafe { libtls_sys::tls_config_prefer_ciphers_server(self.0) } } /// Disable certificate verification. /// /// The `insecure_noverifycert` method disables certificate verification and /// OCSP validation. /// /// # See also /// /// [`tls_config_insecure_noverifycert(3)`](https://man.openbsd.org/tls_config_insecure_noverifycert.3) pub fn insecure_noverifycert(&mut self) { unsafe { libtls_sys::tls_config_insecure_noverifycert(self.0) } } /// Disable server name verification. /// /// The `insecure_noverifyname` method disables server name verification /// (client only). /// /// # See also /// /// [`tls_config_insecure_noverifyname(3)`](https://man.openbsd.org/tls_config_insecure_noverifyname.3) pub fn insecure_noverifyname(&mut self) { unsafe { libtls_sys::tls_config_insecure_noverifyname(self.0) } } /// Disable certificate validity checking. /// /// The `insecure_noverifytime` method disables validity checking of /// certificates and OCSP validation. /// /// # See also /// /// [`tls_config_insecure_noverifytime(3)`](https://man.openbsd.org/tls_config_insecure_noverifytime.3) pub fn insecure_noverifytime(&mut self) { unsafe { libtls_sys::tls_config_insecure_noverifytime(self.0) } } /// Enable all certificate verification. /// /// The `verify` method reenables server name and certificate verification. /// /// # See also /// /// [`tls_config_verify(3)`](https://man.openbsd.org/tls_config_verify.3) pub fn verify(&mut self) { // reenables OCSP validation as well? unsafe { libtls_sys::tls_config_verify(self.0) } } /// Require OCSP stapling. /// /// The `ocsp_require_stapling` method requires that a valid stapled OCSP /// response be provided during the TLS handshake. /// /// # See also /// /// [`tls_config_ocsp_require_stapling(3)`](https://man.openbsd.org/tls_config_ocsp_require_stapling.3) pub fn ocsp_require_stapling(&mut self) { unsafe { libtls_sys::tls_config_ocsp_require_stapling(self.0) } } /// Enable client certificate verification. /// /// The `verify_client` method enables client certificate verification, /// requiring the client to send a certificate (server only). /// /// # See also /// /// [`tls_config_verify_client(3)`](https://man.openbsd.org/tls_config_verify_client.3) pub fn verify_client(&mut self) { unsafe { libtls_sys::tls_config_verify_client(self.0) } } /// Enable optional client certificate verification. /// /// The `verify_client_optional` method enables client certificate /// verification, without requiring the client to send a certificate (server /// only). /// /// # See also /// /// [`tls_config_verify_client_optional(3)`](https://man.openbsd.org/tls_config_verify_client_optional.3) pub fn verify_client_optional(&mut self) { unsafe { libtls_sys::tls_config_verify_client_optional(self.0) } } /// Securely clear secret keys. /// /// The `clear_keys` method clears any secret keys from memory. /// /// # See also /// /// [`tls_config_clear_keys(3)`](https://man.openbsd.org/tls_config_clear_keys.3) pub fn clear_keys(&mut self) { unsafe { libtls_sys::tls_config_clear_keys(self.0) } } /// Set the session identifier for TLS sessions. /// /// The `set_session_id` method sets the session identifier that will be used /// by the TLS server when sessions are enabled (server only). By default a /// random value is used. /// /// # Example /// /// ``` /// # use libtls::{config::TlsConfig, error::Result, *}; /// # use rand::{thread_rng, Rng}; /// # fn tls_config() -> Result<()> { /// let mut session_id = [0; TLS_MAX_SESSION_ID_LENGTH as usize]; /// thread_rng().fill(&mut session_id[..]); /// /// let mut config = TlsConfig::new()?; /// config.set_session_id(&session_id[..])?; /// # Ok(()) /// # } /// # tls_config().unwrap(); /// ``` /// /// # See also /// /// [`tls_config_set_session_id(3)`](https://man.openbsd.org/tls_config_set_session_id.3) pub fn set_session_id(&mut self, session_id: &[u8]) -> error::Result<()> { cvt(self, unsafe { libtls_sys::tls_config_set_session_id(self.0, session_id.as_ptr(), session_id.len()) }) } /// Set the lifetime for TLS sessions. /// /// The `set_session_lifetime` method sets the lifetime to be used for TLS /// sessions (server only). Session support is disabled if a lifetime of /// zero is specified, which is the default. /// /// # See also /// /// [`tls_config_set_session_lifetime(3)`](https://man.openbsd.org/tls_config_set_session_lifetime.3) pub fn set_session_lifetime(&mut self, lifetime: usize) -> error::Result<()> { call_arg1( self, lifetime as i32, libtls_sys::tls_config_set_session_lifetime, ) } /// Add a key for the encryption and authentication of TLS tickets. /// /// The `add_ticket_key` method adds a key used for the encryption and /// authentication of TLS tickets (server only). By default keys are /// generated and rotated automatically based on their lifetime. This /// function should only be used to synchronise ticket encryption key across /// multiple processes. Re-adding a known key will result in an error, /// unless it is the most recently added key. /// /// # Example /// /// ``` /// # use libtls::{config::TlsConfig, error::Result, *}; /// # use rand::{thread_rng, Rng}; /// # fn tls_config() -> Result<()> { /// let mut key = [0; TLS_TICKET_KEY_SIZE as usize]; /// thread_rng().fill(&mut key[..]); /// /// let mut config = TlsConfig::new()?; /// config.add_ticket_key(1, &mut key[..])?; /// # Ok(()) /// # } /// # tls_config().unwrap(); /// ``` /// /// # See also /// /// [`tls_config_add_ticket_key(3)`](https://man.openbsd.org/tls_config_add_ticket_key.3) pub fn add_ticket_key(&mut self, keyrev: u32, key: &mut [u8]) -> error::Result<()> { // XXX key should be const, consider changing this in the upstream API cvt(self, unsafe { libtls_sys::tls_config_add_ticket_key(self.0, keyrev, key.as_mut_ptr(), key.len()) }) } } impl error::LastError for TlsConfig { /// Returns the configuration last error. /// /// The `last_error` method returns an error if no error occurred with config /// at all, or if memory allocation failed while trying to assemble the /// string describing the most recent error related to config. /// /// # See also /// /// [`tls_config_error(3)`](https://man.openbsd.org/tls_config_error.3) fn last_error(&self) -> error::Result<String> { unsafe { cvt_no_error(libtls_sys::tls_config_error(self.0)) } } fn to_error<T>(errstr: String) -> error::Result<T> { Err(error::TlsError::ConfigError(errstr)) } } impl Drop for TlsConfig { /// Free the configuration object. This should only happen when no /// more [`Tls`] contexts are to be configured. /// /// # See also /// /// [`tls_config_free(3)`](https://man.openbsd.org/tls_config_free.3) /// /// [`Tls`]: ../tls/struct.Tls.html fn drop(&mut self) { unsafe { libtls_sys::tls_config_free(self.0) }; } } unsafe impl Send for TlsConfig {} unsafe impl Sync for TlsConfig {} /// Return path of the default CA file. /// /// The `default_ca_cert_file` utility function returns the path of the file that /// contains the default root certificates. /// /// # Example /// /// ``` /// # use libtls::config; /// let certs = config::default_ca_cert_file(); /// assert!(certs.is_absolute()); /// ``` /// /// # See also /// /// [`tls_default_ca_cert_file(3)`](https://man.openbsd.org/tls_default_ca_cert_file.3) pub fn default_ca_cert_file() -> PathBuf { unsafe { CStr::from_ptr(libtls_sys::tls_default_ca_cert_file()) } .to_string_lossy() .into_owned() .into() } /// Parse protocol string. /// /// The `tls_config_parse_protocols` utility function parses a protocol /// string and returns the corresponding value via the protocols argument. /// This value can then be passed to the [`set_protocols`] method. /// The protocol string is a comma or colon separated list of keywords. /// Valid keywords are `tlsv1.0`, `tlsv1.1`, `tlsv1.2`, `all` (all supported /// protocols), `default` (an alias for secure), `legacy` (an alias for all) and /// `secure` (currently TLSv1.2 only). If a value has a negative prefix (in /// the form of a leading exclamation mark) then it is removed from the list /// of available protocols, rather than being added to it. /// /// # Example /// /// ``` /// # use libtls::{config::TlsConfig, error::Result, *}; /// # fn tls_config() -> Result<()> { /// // Parse a list of allowed protocols: /// let protocols = config::parse_protocols("tlsv1.1,tlsv1.2").unwrap(); /// assert_eq!(protocols, TLS_PROTOCOL_TLSv1_1|TLS_PROTOCOL_TLSv1_2); /// /// // The default is to use the `secure` protocols, currently TLSv1.2 only: /// let protocols = config::parse_protocols("default").unwrap(); /// assert_eq!(protocols, TLS_PROTOCOL_TLSv1_2); /// # Ok(()) /// # } /// # tls_config().unwrap(); /// ``` /// /// # See also /// /// [`tls_config_parse_protocols(3)`](https://man.openbsd.org/tls_config_parse_protocols.3) /// /// [`set_protocols`]: #method.set_protocols pub fn parse_protocols(protostr: &str) -> error::Result<u32> { let c_protostr = CString::new(protostr)?; let mut protocols: u32 = 0; let retval = unsafe { libtls_sys::tls_config_parse_protocols(&mut protocols, c_protostr.as_ptr()) }; if retval == -1 { Err(io::Error::new(io::ErrorKind::Other, "Invalid protocols string").into()) } else { Ok(protocols) } } /// Load a certificate or key file. /// /// The `load_file` function loads a certificate or key from disk into memory /// to be used with [`set_ca_mem`], [`set_cert_mem`], [`set_crl_mem`] or /// [`set_key_mem`]. A private key will be decrypted if the optional password /// argument is specified. /// /// # Example /// /// ``` /// # use libtls::{config, error::Result}; /// # fn tls_config() -> Result<()> { /// # let filename = file!(); /// let data = config::load_file(filename, None)?; /// config::unload_file(data); /// # Ok(()) /// # } /// # tls_config().unwrap(); /// ``` /// /// # See also /// /// [`unload_file`], /// [`tls_load_file(3)`](https://man.openbsd.org/tls_load_file.3) /// /// [`set_ca_mem`]: struct.TlsConfig.html#method.set_ca_mem /// [`set_cert_mem`]: struct.TlsConfig.html#method.set_cert_mem /// [`set_crl_mem`]: struct.TlsConfig.html#method.set_crl_mem /// [`set_key_mem`]: struct.TlsConfig.html#method.set_key_mem /// [`unload_file`]: fn.unload_file.html pub fn load_file<P: AsRef<Path>>(file: P, password: Option<&str>) -> error::Result<Vec<u8>> { let mut size = 0; let s_file = cvt_option( file.as_ref().to_str(), io::Error::new(io::ErrorKind::InvalidInput, "file"), )?; unsafe { let c_file = CString::new(s_file)?; let data = match password { Some(password) => { let c_password = CString::new(password)?; let raw = c_password.into_raw(); let data = libtls_sys::tls_load_file(c_file.as_ptr(), &mut size, raw); // Make sure that the raw pointer is not leaked let _ = CString::from_raw(raw); data } None => libtls_sys::tls_load_file(c_file.as_ptr(), &mut size, std::ptr::null_mut()), }; if data.is_null() { Err(io::Error::last_os_error().into()) } else { Ok(Vec::from_raw_parts(data, size, size)) } } } /// Securely unload file that was loaded into memory. /// /// The `unload_file` function unloads the memory that was returned from an /// earlier [`load_file`] call, ensuring that the memory contents is discarded. /// /// # See also /// /// [`tls_unload_file(3)`](https://man.openbsd.org/tls_unload_file.3) /// /// [`load_file`]: fn.load_file.html pub fn unload_file(mut data: Vec<u8>) { let ptr = data.as_mut_ptr(); let len = data.len(); std::mem::forget(data); unsafe { libtls_sys::tls_unload_file(ptr, len) } } #[derive(Debug, Clone)] enum KeyData { File(PathBuf), Path(PathBuf), KeyPairFiles(PathBuf, PathBuf, Option<PathBuf>), KeyPairMem(Vec<u8>, Vec<u8>, Option<Vec<u8>>), Mem(Vec<u8>), } /// `TlsConfigBuilder` for [`TlsConfig`]. /// /// # Example /// /// ``` /// # use libtls::{config::TlsConfigBuilder, error::Result}; /// # use libtls_sys::*; /// # fn tls_config() -> Result<()> { /// # let key = include_bytes!("../tests/eccert.key"); /// # let cert = include_bytes!("../tests/eccert.crt"); /// # let ticket_key1 = [0; TLS_TICKET_KEY_SIZE as usize]; /// # let ticket_key2 = [0; TLS_TICKET_KEY_SIZE as usize]; /// let mut config = TlsConfigBuilder::new() /// .keypair_mem(cert, key, None) /// .ticket_key(1, &ticket_key1) /// .ticket_key(2, &ticket_key2) /// .protocols(TLS_PROTOCOLS_ALL) /// .ecdhecurves("X25519") /// .alpn("h2") /// .build()?; /// # Ok(()) /// # } /// # tls_config().unwrap(); /// ``` /// /// [`TlsConfig`]: struct.TlsConfig.html #[derive(Default, Debug, Clone)] pub struct TlsConfigBuilder { alpn: Option<String>, ca: Option<KeyData>, ciphers: Option<String>, crl: Option<KeyData>, dheparams: Option<String>, ecdhecurves: Option<String>, keypairs: Vec<KeyData>, noverifycert: bool, noverifyname: bool, noverifytime: bool, protocols: Option<u32>, session_fd: Option<RawFd>, session_id: Option<Vec<u8>>, session_lifetime: Option<usize>, ticket_key: HashMap<u32, Vec<u8>>, verify: bool, verify_client: bool, verify_client_optional: bool, verify_depth: Option<usize>, } impl TlsConfigBuilder { /// Return new `TlsConfigBuilder`. /// /// # See also /// /// [`TlsConfig`](struct.TlsConfig.html) pub fn new() -> Self { Self { ..Default::default() } } /// Build new [`TlsConfig`] object. /// /// # See also /// /// [`TlsConfig`] /// /// [`TlsConfig`]: struct.TlsConfig.html pub fn build(&self) -> error::Result<TlsConfig> { let mut config = TlsConfig::new()?; // First add the keypairs and optional OCSP staples. self.build_keypairs(&mut config)?; // Now all the other settings if let Some(ref alpn) = self.alpn { config.set_alpn(alpn)?; } if let Some(ref ca) = self.ca { match ca { KeyData::Mem(mem) => config.set_ca_mem(mem)?, KeyData::File(file) => config.set_ca_file(file)?, KeyData::Path(path) => config.set_ca_path(path)?, _ => return Err(error::TlsError::NoError), }; } else if !default_ca_cert_file().exists() { // Try to use the default CA path as a fallback. config.set_ca_path("/etc/ssl/certs")?; }; if let Some(ref ciphers) = self.ciphers { config.set_ciphers(ciphers)?; } if let Some(ref crl) = self.crl { match crl { KeyData::Mem(mem) => config.set_ca_mem(mem)?, KeyData::File(file) => config.set_ca_file(file)?, _ => return Err(error::TlsError::NoError), }; } if let Some(ref dheparams) = self.dheparams { config.set_dheparams(dheparams)?; } if let Some(ref ecdhecurves) = self.ecdhecurves { config.set_ecdhecurves(ecdhecurves)?; } if let Some(protocols) = self.protocols { config.set_protocols(protocols)?; } if let Some(session_fd) = self.session_fd { config.set_session_fd(session_fd)?; } if let Some(ref session_id) = self.session_id { config.set_session_id(session_id)?; } if let Some(session_lifetime) = self.session_lifetime { config.set_session_lifetime(session_lifetime)?; } // Add ticket keys for (keyrev, key) in self.ticket_key.iter() { // The tls_ticket_key() API is "broken" as it requires the key // as mut and not const. config.add_ticket_key(*keyrev, key.clone().as_mut_slice())?; } // Order verify_* calls in a safe priority. if self.noverifycert { config.insecure_noverifycert(); } if self.noverifyname { config.insecure_noverifyname(); } if self.noverifytime { config.insecure_noverifytime(); } if let Some(verify_depth) = self.verify_depth { config.set_verify_depth(verify_depth)?; } if self.verify_client_optional { config.verify_client_optional(); } if self.verify_client { config.verify_client(); } if self.verify { config.verify(); } Ok(config) } fn build_keypairs(&self, config: &mut TlsConfig) -> error::Result<()> { for (i, kp) in self.keypairs.iter().enumerate() { match kp { KeyData::KeyPairMem(cert, key, ocsp) => { if let Some(ocsp) = ocsp { // Set the first keypair as the default. if i == 0 { config.set_keypair_ocsp_mem(cert, key, ocsp)?; } config.add_keypair_ocsp_mem(cert, key, ocsp)?; } else { if i == 0 { config.set_keypair_mem(cert, key)?; } config.add_keypair_mem(cert, key)?; } } KeyData::KeyPairFiles(cert, key, ocsp) => { if let Some(ocsp) = ocsp { if i == 0 { config.set_keypair_ocsp_file(cert, key, ocsp)?; } config.add_keypair_ocsp_file(cert, key, ocsp)?; } else { if i == 0 { config.set_keypair_file(cert, key)?; } config.add_keypair_file(cert, key)?; } } _ => return Err(error::TlsError::NoError), }; } Ok(()) } /// Build new [`TlsConfig`] object and return a configured [`Tls`] client. /// /// # See also /// /// [`Tls`], /// [`TlsConfig`] /// /// [`Tls`]: ../tls/struct.Tls.html /// [`TlsConfig`]: struct.TlsConfig.html pub fn client(&self) -> error::Result<Tls> { let mut client = Tls::client()?; client.configure(&self.build()?)?; Ok(client) } /// Build new [`TlsConfig`] object and return a configured [`Tls`] server. /// /// # See also /// /// [`Tls`], /// [`TlsConfig`] /// /// [`Tls`]: ../tls/struct.Tls.html /// [`TlsConfig`]: struct.TlsConfig.html pub fn server(&self) -> error::Result<Tls> { let mut server = Tls::server()?; server.configure(&self.build()?)?; Ok(server) } /// Set the ALPN protocols that are supported. /// /// # See also /// /// [`TlsConfig::set_alpn`](struct.TlsConfig.html#method.set_alpn) pub fn alpn(&'_ mut self, alpn: &str) -> &'_ mut Self { self.alpn = Some(alpn.to_owned()); self } /// Set the CA file. /// /// # See also /// /// [`TlsConfig::set_ca_file`](struct.TlsConfig.html#method.set_ca_file) pub fn ca_file<P: AsRef<Path>>(&'_ mut self, path: P) -> &'_ mut Self { self.ca = Some(KeyData::File(path.as_ref().to_owned())); self } /// Set the CA path. /// /// # See also /// /// [`TlsConfig::set_ca_path`](struct.TlsConfig.html#method.set_ca_path) pub fn ca_path<P: AsRef<Path>>(&'_ mut self, path: P) -> &'_ mut Self { self.ca = Some(KeyData::Path(path.as_ref().to_owned())); self } /// Set the CA from memory. /// /// # See also /// /// [`TlsConfig::set_ca_mem`](struct.TlsConfig.html#method.set_ca_mem) pub fn ca_mem(&'_ mut self, mem: &[u8]) -> &'_ mut Self { self.ca = Some(KeyData::Mem(mem.to_vec())); self } /// Set the list of cipher that may be used. /// /// # See also /// /// [`TlsConfig::set_ciphers`](struct.TlsConfig.html#method.set_ciphers) pub fn ciphers(&'_ mut self, ciphers: &str) -> &'_ mut Self { self.ciphers = Some(ciphers.to_owned()); self } /// Set the CRL file. /// /// # See also /// /// [`TlsConfig::set_crl_file`](struct.TlsConfig.html#method.set_crl_file) pub fn crl_file<P: AsRef<Path>>(&'_ mut self, path: P) -> &'_ mut Self { self.crl = Some(KeyData::File(path.as_ref().to_owned())); self } /// Set the CRL from memory. /// /// # See also /// /// [`TlsConfig::set_crl_mem`](struct.TlsConfig.html#method.set_crl_mem) pub fn crl_mem(&'_ mut self, mem: &[u8]) -> &'_ mut Self { self.crl = Some(KeyData::Mem(mem.to_vec())); self } /// Set the parameters of an Diffie-Hellman Ephemeral (DHE) key exchange. /// /// # See also /// /// [`TlsConfig::set_dheparams`](struct.TlsConfig.html#method.set_dheparams) pub fn dheparams(&'_ mut self, dheparams: &str) -> &'_ mut Self { self.dheparams = Some(dheparams.to_owned()); self } /// Set the curves of an Elliptic Curve Diffie-Hellman Ephemeral (ECDHE) key exchange. /// /// # See also /// /// [`TlsConfig::set_ecdhecurves`](struct.TlsConfig.html#method.set_ecdhecurves) pub fn ecdhecurves(&'_ mut self, ecdhecurves: &str) -> &'_ mut Self { self.ecdhecurves = Some(ecdhecurves.to_owned()); self } /// Add additional files of a public and private key pair and OCSP staple. /// /// # See also /// /// [`TlsConfig::add_keypair_file`](struct.TlsConfig.html#method.add_keypair_ocsp_file), /// [`TlsConfig::set_keypair_file`](struct.TlsConfig.html#method.set_keypair_ocsp_file) pub fn keypair_file<P: AsRef<Path>>( &'_ mut self, cert: P, key: P, ocsp_staple: Option<P>, ) -> &'_ mut Self { let certdata = cert.as_ref().to_owned(); let keydata = key.as_ref().to_owned(); let ocspdata = match ocsp_staple { Some(path) => Some(path.as_ref().to_owned()), None => None, }; self.keypairs .push(KeyData::KeyPairFiles(certdata, keydata, ocspdata)); self } /// Add an additional public and private key pair and OCSP staple from memory. /// /// # See also /// /// [`TlsConfig::set_keypair_mem`](struct.TlsConfig.html#method.add_keypair_ocsp_mem), /// [`TlsConfig::set_keypair_mem`](struct.TlsConfig.html#method.set_keypair_ocsp_mem) pub fn keypair_mem( &'_ mut self, cert: &[u8], key: &[u8], ocsp_staple: Option<&[u8]>, ) -> &'_ mut Self { let certdata = cert.to_vec(); let keydata = key.to_vec(); let ocspdata = match ocsp_staple { Some(mem) => Some(mem.to_vec()), None => None, }; self.keypairs .push(KeyData::KeyPairMem(certdata, keydata, ocspdata)); self } /// Disable certificate verification. /// /// # See also /// /// [`TlsConfig::insecure_noverifycert`](struct.TlsConfig.html#method.insecure_noverifycert) pub fn noverifycert(&'_ mut self) -> &'_ mut Self { self.noverifycert = true; self } /// Disable server name verification. /// /// # See also /// /// [`TlsConfig::insecure_noverifyname`](struct.TlsConfig.html#method.insecure_noverifyname) pub fn noverifyname(&'_ mut self) -> &'_ mut Self { self.noverifyname = true; self } /// Disable certificate validity checking. /// /// # See also /// /// [`TlsConfig::insecure_noverifytime`](struct.TlsConfig.html#method.insecure_noverifytime) pub fn noverifytime(&'_ mut self) -> &'_ mut Self { self.noverifytime = true; self } /// Set which versions of the TLS protocol may be used. /// /// # See also /// /// [`TlsConfig::set_protocols`](struct.TlsConfig.html#method.set_protocols) pub fn protocols(&'_ mut self, protocols: u32) -> &'_ mut Self { self.protocols = Some(protocols); self } /// Set a file descriptor to manage data for TLS sessions. /// /// # See also /// /// [`TlsConfig::set_session_fd`](struct.TlsConfig.html#method.set_session_fd) pub fn session_fd(&'_ mut self, fd: RawFd) -> &'_ mut Self { self.session_fd = Some(fd); self } /// Set the session identifier for TLS sessions. /// /// # See also /// /// [`TlsConfig::set_session_id`](struct.TlsConfig.html#method.set_session_id) pub fn session_id(&'_ mut self, id: &[u8]) -> &'_ mut Self { self.session_id = Some(id.to_vec()); self } /// Set the lifetime for TLS sessions. /// /// # See also /// /// [`TlsConfig::set_session_lifetime`](struct.TlsConfig.html#method.set_session_lifetime) pub fn session_lifetime(&'_ mut self, lifetime: usize) -> &'_ mut Self { self.session_lifetime = Some(lifetime); self } /// # See also /// /// [`TlsConfig::add_ticket_key`](struct.TlsConfig.html#method.add_ticket_key) pub fn ticket_key(&'_ mut self, keyrev: u32, key: &[u8]) -> &'_ mut Self { self.ticket_key.insert(keyrev, key.to_vec()); self } /// Enable all certificate verification. /// /// # See also /// /// [`TlsConfig::verify`](struct.TlsConfig.html#method.verify) pub fn verify(&'_ mut self) -> &'_ mut Self { self.verify = true; self } /// Enable client certificate verification. /// /// # See also /// /// [`TlsConfig::verify_client`](struct.TlsConfig.html#method.verify_client) pub fn verify_client(&'_ mut self) -> &'_ mut Self { self.verify_client = true; self } /// Enable optional client certificate verification. /// /// # See also /// /// [`TlsConfig::verify_client_optional`](struct.TlsConfig.html#method.verify_client_optional) pub fn verify_client_optional(&'_ mut self) -> &'_ mut Self { self.verify_client_optional = true; self } /// Set the certificate verification depth. /// /// # See also /// /// [`TlsConfig::verify_depth`](struct.TlsConfig.html#method.verify_depth) pub fn verify_depth(&'_ mut self, depth: usize) -> &'_ mut Self { self.verify_depth = Some(depth); self } }