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 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877
|
/* Portions of this file are subject to the following copyright(s). See
* the Net-SNMP's COPYING file for more details and other copyrights
* that may apply:
*/
/*
* Portions of this file are copyrighted by:
* Copyright Copyright 2003 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms specified in the COPYING file
* distributed with the Net-SNMP package.
*/
/*
* See the following web pages for useful documentation on this transport:
* http://www.net-snmp.org/wiki/index.php/TUT:Using_TLS
* http://www.net-snmp.org/wiki/index.php/Using_DTLS
*/
#include <net-snmp/net-snmp-config.h>
#ifdef HAVE_LIBSSL_DTLS
#include <net-snmp/net-snmp-features.h>
netsnmp_feature_require(cert_util);
netsnmp_feature_require(sockaddr_size);
#include <net-snmp/library/snmpIPBaseDomain.h>
#include <net-snmp/library/snmpDTLSUDPDomain.h>
#include <net-snmp/library/snmpUDPIPv6Domain.h>
#include <net-snmp/library/snmp_assert.h>
#include <stdio.h>
#include <sys/types.h>
#include <ctype.h>
#include <errno.h>
#if HAVE_STRING_H
#include <string.h>
#else
#include <strings.h>
#endif
#if HAVE_STDLIB_H
#include <stdlib.h>
#endif
#if HAVE_UNISTD_H
#include <unistd.h>
#endif
#if HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif
#if HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#if HAVE_ARPA_INET_H
#include <arpa/inet.h>
#endif
#if HAVE_NETDB_H
#include <netdb.h>
#endif
#if HAVE_SYS_UIO_H
#include <sys/uio.h>
#endif
#include "../memcheck.h"
#include <net-snmp/types.h>
#include <net-snmp/output_api.h>
#include <net-snmp/config_api.h>
#include <net-snmp/library/snmp_transport.h>
#include <net-snmp/library/system.h>
#include <net-snmp/library/tools.h>
#include <net-snmp/library/callback.h>
#include "openssl/bio.h"
#include "openssl/ssl.h"
#include "openssl/err.h"
#include "openssl/rand.h"
#include <net-snmp/library/snmpSocketBaseDomain.h>
#include <net-snmp/library/snmpTLSBaseDomain.h>
#include <net-snmp/library/snmpUDPDomain.h>
#include <net-snmp/library/cert_util.h>
#include <net-snmp/library/snmp_openssl.h>
#ifndef INADDR_NONE
#define INADDR_NONE -1
#endif
#define WE_ARE_SERVER 0
#define WE_ARE_CLIENT 1
oid netsnmpDTLSUDPDomain[] = { TRANSPORT_DOMAIN_DTLS_UDP_IP };
size_t netsnmpDTLSUDPDomain_len = OID_LENGTH(netsnmpDTLSUDPDomain);
static netsnmp_tdomain dtlsudpDomain;
#ifdef NETSNMP_TRANSPORT_UDPIPV6_DOMAIN
static int openssl_addr_index6 = 0;
#endif
/* this stores openssl credentials for each connection since openssl
can't do it for us at the moment; hopefully future versions will
change */
typedef struct bio_cache_s {
BIO *read_bio; /* OpenSSL will read its incoming SSL packets from here */
BIO *write_bio; /* OpenSSL will write its outgoing SSL packets to here */
netsnmp_sockaddr_storage sas;
u_int flags;
struct bio_cache_s *next;
int msgnum;
char *write_cache;
size_t write_cache_len;
_netsnmpTLSBaseData *tlsdata;
} bio_cache;
/** bio_cache flags */
#define NETSNMP_BIO_HAVE_COOKIE 0x0001 /* verified cookie */
#define NETSNMP_BIO_CONNECTED 0x0002 /* received decoded data */
#define NETSNMP_BIO_DISCONNECTED 0x0004 /* peer shutdown */
static bio_cache *biocache = NULL;
static int openssl_addr_index = 0;
static int netsnmp_dtls_verify_cookie(SSL *ssl,
SECOND_APPVERIFY_COOKIE_CB_ARG_QUALIFIER
unsigned char *cookie,
unsigned int cookie_len);
static int netsnmp_dtls_gen_cookie(SSL *ssl, unsigned char *cookie,
unsigned int *cookie_len);
/* this stores remote connections in a list to search through */
/* XXX: optimize for searching */
/* XXX: handle state issues for new connections to reduce DOS issues */
/* (TLS should do this, but openssl can't do more than one ctx per sock */
/* XXX: put a timer on the cache for expirary purposes */
static bio_cache *find_bio_cache(const netsnmp_sockaddr_storage *from_addr)
{
bio_cache *cachep = NULL;
for (cachep = biocache; cachep; cachep = cachep->next) {
if (cachep->sas.sa.sa_family != from_addr->sa.sa_family)
continue;
if ((from_addr->sa.sa_family == AF_INET) &&
((cachep->sas.sin.sin_addr.s_addr !=
from_addr->sin.sin_addr.s_addr) ||
(cachep->sas.sin.sin_port != from_addr->sin.sin_port)))
continue;
#ifdef NETSNMP_TRANSPORT_UDPIPV6_DOMAIN
else if ((from_addr->sa.sa_family == AF_INET6) &&
((cachep->sas.sin6.sin6_port != from_addr->sin6.sin6_port) ||
(cachep->sas.sin6.sin6_scope_id !=
from_addr->sin6.sin6_scope_id) ||
(memcmp(cachep->sas.sin6.sin6_addr.s6_addr,
from_addr->sin6.sin6_addr.s6_addr,
sizeof(from_addr->sin6.sin6_addr.s6_addr)) != 0)))
continue;
#endif
/* found an existing connection */
break;
}
return cachep;
}
/* removes a single cache entry and returns SUCCESS on finding and
removing it. */
static int remove_bio_cache(bio_cache *thiscache)
{
bio_cache *cachep = NULL, *prevcache = NULL;
cachep = biocache;
while (cachep) {
if (cachep == thiscache) {
/* remove it from the list */
if (NULL == prevcache) {
/* at the first cache in the list */
biocache = thiscache->next;
} else {
prevcache->next = thiscache->next;
}
return SNMPERR_SUCCESS;
}
prevcache = cachep;
cachep = cachep->next;
}
return SNMPERR_GENERR;
}
/* frees the contents of a bio_cache */
static void free_bio_cache(bio_cache *cachep)
{
/* These are freed by the SSL_free() call */
/*
BIO_free(cachep->read_bio);
BIO_free(cachep->write_bio);
*/
DEBUGMSGTL(("9:dtlsudp:bio_cache", "releasing %p\n", cachep));
SNMP_FREE(cachep->write_cache);
netsnmp_tlsbase_free_tlsdata(cachep->tlsdata);
}
static void remove_and_free_bio_cache(bio_cache *cachep)
{
/** no debug, remove_bio_cache does it */
remove_bio_cache(cachep);
free_bio_cache(cachep);
}
/* XXX: lots of malloc/state cleanup needed */
#define DIEHERE(msg) do { snmp_log(LOG_ERR, "%s\n", msg); return NULL; } while(0)
static bio_cache *
start_new_cached_connection(netsnmp_transport *t,
const netsnmp_sockaddr_storage *remote_addr,
int we_are_client)
{
bio_cache *cachep = NULL;
_netsnmpTLSBaseData *tlsdata;
DEBUGTRACETOK("9:dtlsudp");
/* RFC5953: section 5.3.1, step 1:
1) The snmpTlstmSessionOpens counter is incremented.
*/
if (we_are_client)
snmp_increment_statistic(STAT_TLSTM_SNMPTLSTMSESSIONOPENS);
if (!t->sock)
DIEHERE("no socket passed in to start_new_cached_connection\n");
if (!remote_addr)
DIEHERE("no remote_addr passed in to start_new_cached_connection\n");
cachep = SNMP_MALLOC_TYPEDEF(bio_cache);
if (!cachep)
return NULL;
/* allocate our TLS specific data */
if (NULL == (tlsdata = netsnmp_tlsbase_allocate_tlsdata(t, !we_are_client))) {
SNMP_FREE(cachep);
return NULL;
}
cachep->tlsdata = tlsdata;
/* RFC5953: section 5.3.1, step 1:
2) The client selects the appropriate certificate and cipher_suites
for the key agreement based on the tmSecurityName and the
tmRequestedSecurityLevel for the session. For sessions being
established as a result of a SNMP-TARGET-MIB based operation, the
certificate will potentially have been identified via the
snmpTlstmParamsTable mapping and the cipher_suites will have to
be taken from system-wide or implementation-specific
configuration. If no row in the snmpTlstmParamsTable exists then
implementations MAY choose to establish the connection using a
default client certificate available to the application.
Otherwise, the certificate and appropriate cipher_suites will
need to be passed to the openSession() ASI as supplemental
information or configured through an implementation-dependent
mechanism. It is also implementation-dependent and possibly
policy-dependent how tmRequestedSecurityLevel will be used to
influence the security capabilities provided by the (D)TLS
connection. However this is done, the security capabilities
provided by (D)TLS MUST be at least as high as the level of
security indicated by the tmRequestedSecurityLevel parameter.
The actual security level of the session is reported in the
tmStateReference cache as tmSecurityLevel. For (D)TLS to provide
strong authentication, each principal acting as a command
generator SHOULD have its own certificate.
*/
/* Implementation notes:
+ This Information is passed in via the transport and default
paremeters
*/
/* see if we have base configuration to copy in to this new one */
if (NULL != t->data && t->data_length == sizeof(_netsnmpTLSBaseData)) {
_netsnmpTLSBaseData *parentdata = t->data;
if (parentdata->our_identity)
tlsdata->our_identity = strdup(parentdata->our_identity);
if (parentdata->their_identity)
tlsdata->their_identity = strdup(parentdata->their_identity);
if (parentdata->their_fingerprint)
tlsdata->their_fingerprint = strdup(parentdata->their_fingerprint);
if (parentdata->trust_cert)
tlsdata->trust_cert = strdup(parentdata->trust_cert);
if (parentdata->their_hostname)
tlsdata->their_hostname = strdup(parentdata->their_hostname);
}
DEBUGMSGTL(("dtlsudp", "starting a new connection\n"));
cachep->next = biocache;
biocache = cachep;
if (remote_addr->sa.sa_family == AF_INET)
memcpy(&cachep->sas.sin, &remote_addr->sin, sizeof(remote_addr->sin));
#ifdef NETSNMP_TRANSPORT_UDPIPV6_DOMAIN
else if (remote_addr->sa.sa_family == AF_INET6)
memcpy(&cachep->sas.sin6, &remote_addr->sin6, sizeof(remote_addr->sin6));
#endif
else
DIEHERE("unknown address family");
/* create caching memory bios for OpenSSL to read and write to */
cachep->read_bio = BIO_new(BIO_s_mem()); /* openssl reads from */
if (!cachep->read_bio)
DIEHERE("failed to create the openssl read_bio");
cachep->write_bio = BIO_new(BIO_s_mem()); /* openssl writes to */
if (!cachep->write_bio) {
BIO_free(cachep->read_bio);
cachep->read_bio = NULL;
DIEHERE("failed to create the openssl write_bio");
}
BIO_set_mem_eof_return(cachep->read_bio, -1);
BIO_set_mem_eof_return(cachep->write_bio, -1);
if (we_are_client) {
/* we're the client */
DEBUGMSGTL(("dtlsudp",
"starting a new connection as a client to sock: %d\n",
t->sock));
tlsdata->ssl = SSL_new(sslctx_client_setup(DTLS_method(), tlsdata));
/* XXX: session setting 735 */
} else {
/* we're the server */
SSL_CTX *ctx = sslctx_server_setup(DTLS_method());
if (!ctx) {
BIO_free(cachep->read_bio);
BIO_free(cachep->write_bio);
cachep->read_bio = NULL;
cachep->write_bio = NULL;
DIEHERE("failed to create the SSL Context");
}
/* turn on cookie exchange */
/* Set DTLS cookie generation and verification callbacks */
SSL_CTX_set_cookie_generate_cb(ctx, netsnmp_dtls_gen_cookie);
SSL_CTX_set_cookie_verify_cb(ctx, netsnmp_dtls_verify_cookie);
tlsdata->ssl = SSL_new(ctx);
}
if (!tlsdata->ssl) {
BIO_free(cachep->read_bio);
BIO_free(cachep->write_bio);
cachep->read_bio = NULL;
cachep->write_bio = NULL;
DIEHERE("failed to create the SSL session structure");
}
SSL_set_mode(tlsdata->ssl, SSL_MODE_AUTO_RETRY);
/* set the bios that openssl should read from and write to */
/* (and we'll do the opposite) */
SSL_set_bio(tlsdata->ssl, cachep->read_bio, cachep->write_bio);
/* RFC5953: section 5.3.1, step 1:
3) Using the destTransportDomain and destTransportAddress values,
the client will initiate the (D)TLS handshake protocol to
establish session keys for message integrity and encryption.
If the attempt to establish a session is unsuccessful, then
snmpTlstmSessionOpenErrors is incremented, an error indication is
returned, and processing stops. If the session failed to open
because the presented server certificate was unknown or invalid
then the snmpTlstmSessionUnknownServerCertificate or
snmpTlstmSessionInvalidServerCertificates MUST be incremented and
a snmpTlstmServerCertificateUnknown or
snmpTlstmServerInvalidCertificate notification SHOULD be sent as
appropriate. Reasons for server certificate invalidation
includes, but is not limited to, cryptographic validation
failures and an unexpected presented certificate identity.
*/
/* Implementation notes:
+ Because we're working asynchronously the real "end" point of
opening a connection doesn't occur here as certificate
verification and other things needs to happen first in the
verify callback, etc. See the netsnmp_dtlsudp_recv()
function for the final processing.
*/
/* set the SSL notion of we_are_client/server */
if (we_are_client)
SSL_set_connect_state(tlsdata->ssl);
else {
/* XXX: we need to only create cache entries when cookies succeed */
SSL_set_options(tlsdata->ssl, SSL_OP_COOKIE_EXCHANGE);
SSL_set_ex_data(tlsdata->ssl, openssl_addr_index, cachep);
SSL_set_accept_state(tlsdata->ssl);
}
/* RFC5953: section 5.3.1, step 1:
6) The TLSTM-specific session identifier (tlstmSessionID) is set in
the tmSessionID of the tmStateReference passed to the TLS
Transport Model to indicate that the session has been established
successfully and to point to a specific (D)TLS connection for
future use. The tlstmSessionID is also stored in the LCD for
later lookup during processing of incoming messages
(Section 5.1.2).
*/
/* Implementation notes:
+ our sessionID is stored as the transport's data pointer member
*/
DEBUGMSGT(("9:dtlsudp:bio_cache:created", "%p\n", cachep));
return cachep;
}
static bio_cache *
find_or_create_bio_cache(netsnmp_transport *t,
const netsnmp_sockaddr_storage *from_addr,
int we_are_client)
{
bio_cache *cachep = find_bio_cache(from_addr);
if (NULL == cachep) {
/* none found; need to start a new context */
cachep = start_new_cached_connection(t, from_addr, we_are_client);
if (NULL == cachep) {
snmp_log(LOG_ERR, "failed to open a new dtls connection\n");
}
} else {
DEBUGMSGT(("9:dtlsudp:bio_cache:found", "%p\n", cachep));
}
return cachep;
}
static const netsnmp_indexed_addr_pair *
_extract_addr_pair(netsnmp_transport *t, const void *opaque, int olen)
{
if (opaque) {
switch (olen) {
case sizeof(netsnmp_tmStateReference): {
const netsnmp_tmStateReference *tmStateRef = opaque;
if (tmStateRef->have_addresses)
return &tmStateRef->addresses;
break;
}
default:
netsnmp_assert(0);
}
}
if (t && t->data) {
switch (t->data_length) {
case sizeof(netsnmp_indexed_addr_pair):
return t->data;
case sizeof(_netsnmpTLSBaseData): {
_netsnmpTLSBaseData *tlsdata = t->data;
return tlsdata->addr;
}
default:
netsnmp_assert(0);
}
}
return NULL;
}
static const struct sockaddr *
_find_remote_sockaddr(netsnmp_transport *t, const void *opaque, int olen,
int *socklen)
{
const netsnmp_indexed_addr_pair *addr_pair;
const struct sockaddr *sa = NULL;
addr_pair = _extract_addr_pair(t, opaque, olen);
if (NULL == addr_pair)
return NULL;
sa = &addr_pair->remote_addr.sa;
*socklen = netsnmp_sockaddr_size(sa);
return sa;
}
/*
* Reads data from our internal openssl outgoing BIO and sends any
* queued packets out the UDP port
*/
static int
_netsnmp_send_queued_dtls_pkts(netsnmp_transport *t, bio_cache *cachep)
{
int outsize, rc2;
void *outbuf;
DEBUGTRACETOK("9:dtlsudp");
/* for memory bios, we now read from openssl's write
buffer (ie, the packet to go out) and send it out
the udp port manually */
outsize = BIO_ctrl_pending(cachep->write_bio);
outbuf = malloc(outsize);
if (outsize > 0 && outbuf) {
int socksize;
void *sa;
DEBUGMSGTL(("dtlsudp", "have %d bytes to send\n", outsize));
outsize = BIO_read(cachep->write_bio, outbuf, outsize);
MAKE_MEM_DEFINED(outbuf, outsize);
sa = NETSNMP_REMOVE_CONST(struct sockaddr *,
_find_remote_sockaddr(t, NULL, 0, &socksize));
if (NULL == sa)
sa = &cachep->sas.sa;
socksize = netsnmp_sockaddr_size(sa);
rc2 = t->base_transport->f_send(t, outbuf, outsize, &sa, &socksize);
if (rc2 == -1) {
snmp_log(LOG_ERR, "failed to send a DTLS specific packet\n");
}
} else if (outsize == 0) {
DEBUGMSGTL(("9:dtlsudp", "have 0 bytes to send\n"));
} else {
DEBUGMSGTL(("9:dtlsudp", "buffer allocation failed\n"));
}
free(outbuf);
return outsize;
}
/*
* If we have any outgoing SNMP data queued that OpenSSL/DTLS couldn't send
* (likely due to DTLS control packets needing to go out first)
* then this function attempts to send them.
*/
/* returns SNMPERR_SUCCESS if we succeeded in getting the data out */
/* returns SNMPERR_GENERR if we still need more time */
static int
_netsnmp_bio_try_and_write_buffered(netsnmp_transport *t, bio_cache *cachep)
{
int rc;
_netsnmpTLSBaseData *tlsdata;
DEBUGTRACETOK("9:dtlsudp");
tlsdata = cachep->tlsdata;
/* make sure we have something to write */
if (!cachep->write_cache || cachep->write_cache_len == 0)
return SNMPERR_SUCCESS;
DEBUGMSGTL(("dtlsudp", "Trying to write %" NETSNMP_PRIz "d of buffered data\n",
cachep->write_cache_len));
/* try and write out the cached data */
rc = SSL_write(tlsdata->ssl, cachep->write_cache, cachep->write_cache_len);
while (rc == -1) {
int errnum = SSL_get_error(tlsdata->ssl, rc);
int bytesout;
/* don't treat want_read/write errors as real errors */
if (errnum != SSL_ERROR_WANT_READ &&
errnum != SSL_ERROR_WANT_WRITE) {
DEBUGMSGTL(("dtlsudp", "ssl_write error (of buffered data)\n"));
_openssl_log_error(rc, tlsdata->ssl, "SSL_write");
return SNMPERR_GENERR;
}
/* check to see if we have outgoing DTLS packets to send */
/* (SSL_write could have created DTLS control packets) */
bytesout = _netsnmp_send_queued_dtls_pkts(t, cachep);
/* If want_read/write but failed to actually send anything
then we need to wait for the other side, so quit */
if (bytesout <= 0) {
/* sending failed; must wait longer */
return SNMPERR_GENERR;
}
/* retry writing */
DEBUGMSGTL(("9:dtlsudp", "recalling ssl_write\n"));
rc = SSL_write(tlsdata->ssl, cachep->write_cache,
cachep->write_cache_len);
}
if (rc > 0)
cachep->msgnum++;
if (_netsnmp_send_queued_dtls_pkts(t, cachep) > 0) {
SNMP_FREE(cachep->write_cache);
cachep->write_cache_len = 0;
DEBUGMSGTL(("dtlsudp", " Write was successful\n"));
return SNMPERR_SUCCESS;
}
DEBUGMSGTL(("dtlsudp", " failed to send over UDP socket\n"));
return SNMPERR_GENERR;
}
static int
_netsnmp_add_buffered_data(bio_cache *cachep, const char *buf, size_t size)
{
if (cachep->write_cache && cachep->write_cache_len > 0) {
size_t newsize = cachep->write_cache_len + size;
char *newbuf = realloc(cachep->write_cache, newsize);
if (NULL == newbuf) {
/* ack! malloc failure */
/* XXX: free and close */
return SNMPERR_GENERR;
}
cachep->write_cache = newbuf;
/* write the new packet to the end */
memcpy(cachep->write_cache + cachep->write_cache_len,
buf, size);
cachep->write_cache_len = newsize;
} else {
cachep->write_cache = netsnmp_memdup(buf, size);
if (!cachep->write_cache) {
/* ack! malloc failure */
/* XXX: free and close */
return SNMPERR_GENERR;
}
cachep->write_cache_len = size;
}
return SNMPERR_SUCCESS;
}
static int
netsnmp_dtlsudp_recv(netsnmp_transport *t, void *buf, int size,
void **opaque, int *olength)
{
int rc = -1;
netsnmp_indexed_addr_pair *addr_pair = NULL;
netsnmp_tmStateReference *tmStateRef = NULL;
_netsnmpTLSBaseData *tlsdata;
bio_cache *cachep;
DEBUGTRACETOK("9:dtlsudp");
if (NULL == t || t->sock == 0)
return -1;
/* create a tmStateRef cache for slow fill-in */
tmStateRef = SNMP_MALLOC_TYPEDEF(netsnmp_tmStateReference);
if (tmStateRef == NULL) {
*opaque = NULL;
*olength = 0;
return -1;
}
/* Set the transportDomain */
memcpy(tmStateRef->transportDomain,
netsnmpDTLSUDPDomain, sizeof(netsnmpDTLSUDPDomain[0]) *
netsnmpDTLSUDPDomain_len);
tmStateRef->transportDomainLen = netsnmpDTLSUDPDomain_len;
addr_pair = &tmStateRef->addresses;
tmStateRef->have_addresses = 1;
while (rc < 0) {
void *opaque = NULL;
int olen;
rc = t->base_transport->f_recv(t, buf, size, &opaque, &olen);
if (rc > 0) {
if (olen > sizeof(*addr_pair))
snmp_log(LOG_ERR, "%s: from address length %d > %d\n",
NETSNMP_FUNCTION, olen, (int)sizeof(*addr_pair));
memcpy(addr_pair, opaque, SNMP_MIN(sizeof(*addr_pair), olen));
}
SNMP_FREE(opaque);
if (rc < 0 && errno != EINTR) {
break;
}
}
DEBUGMSGTL(("dtlsudp", "received %d raw bytes on way to dtls\n", rc));
if (rc < 0) {
DEBUGMSGTL(("dtlsudp", "recvfrom fd %d err %d (\"%s\")\n",
t->sock, errno, strerror(errno)));
SNMP_FREE(tmStateRef);
return -1;
}
/* now that we have the from address filled in, we can look up
the openssl context and have openssl read and process
appropriately */
/* RFC5953: section 5.1, step 1:
1) Determine the tlstmSessionID for the incoming message. The
tlstmSessionID MUST be a unique session identifier for this
(D)TLS connection. The contents and format of this identifier
are implementation-dependent as long as it is unique to the
session. A session identifier MUST NOT be reused until all
references to it are no longer in use. The tmSessionID is equal
to the tlstmSessionID discussed in Section 5.1.1. tmSessionID
refers to the session identifier when stored in the
tmStateReference and tlstmSessionID refers to the session
identifier when stored in the LCD. They MUST always be equal
when processing a given session's traffic.
If this is the first message received through this session and
the session does not have an assigned tlstmSessionID yet then the
snmpTlstmSessionAccepts counter is incremented and a
tlstmSessionID for the session is created. This will only happen
on the server side of a connection because a client would have
already assigned a tlstmSessionID during the openSession()
invocation. Implementations may have performed the procedures
described in Section 5.3.2 prior to this point or they may
perform them now, but the procedures described in Section 5.3.2
MUST be performed before continuing beyond this point.
*/
/* RFC5953: section 5.1, step 2:
2) Create a tmStateReference cache for the subsequent reference and
assign the following values within it:
tmTransportDomain = snmpTLSTCPDomain or snmpDTLSUDPDomain as
appropriate.
tmTransportAddress = The address the message originated from.
tmSecurityLevel = The derived tmSecurityLevel for the session,
as discussed in Section 3.1.2 and Section 5.3.
tmSecurityName = The derived tmSecurityName for the session as
discussed in Section 5.3. This value MUST be constant during
the lifetime of the session.
tmSessionID = The tlstmSessionID described in step 1 above.
*/
/* if we don't have a cachep for this connection then
we're receiving something new and are the server
side */
cachep =
find_or_create_bio_cache(t, &addr_pair->remote_addr, WE_ARE_SERVER);
if (NULL == cachep) {
snmp_increment_statistic(STAT_TLSTM_SNMPTLSTMSESSIONACCEPTS);
SNMP_FREE(tmStateRef);
return -1;
}
tlsdata = cachep->tlsdata;
if (NULL == tlsdata->ssl) {
/*
* this happens when the server starts but doesn't have an
* identity and a client connects...
*/
snmp_log(LOG_ERR,
"DTLSUDP: missing tlsdata!\n");
/*snmp_increment_statistic( XXX-rks ??? );*/
SNMP_FREE(tmStateRef);
return -1;
}
/* Implementation notes:
- we use the t->data memory pointer as the session ID
- the transport domain is already the correct type if we got here
- if we don't have a session yet (eg, no tmSessionID from the
specs) then we create one automatically here.
*/
/* write the received buffer to the memory-based input bio */
BIO_write(cachep->read_bio, buf, rc);
/* RFC5953: section 5.1, step 3:
3) The incomingMessage and incomingMessageLength are assigned values
from the (D)TLS processing.
*/
/* Implementation notes:
+ rc = incomingMessageLength
+ buf = IncomingMessage
*/
/* XXX: in Wes' other example we do a SSL_pending() call
too to ensure we're ready to read... it's possible
that buffered stuff in openssl won't be caught by the
net-snmp select loop because it's already been pulled
out; need to deal with this) */
rc = SSL_read(tlsdata->ssl, buf, size);
MAKE_MEM_DEFINED(&rc, sizeof(rc));
if (rc > 0)
MAKE_MEM_DEFINED(buf, rc);
/*
* moved netsnmp_openssl_null_checks to netsnmp_tlsbase_wrapup_recv.
* currently netsnmp_tlsbase_wrapup_recv is where we check for
* algorithm compliance, but we (sometimes) know the algorithms
* at this point, so we could bail earlier (here)...
*/
while (rc == -1) {
int errnum = SSL_get_error(tlsdata->ssl, rc);
int bytesout;
/* don't treat want_read/write errors as real errors */
if (errnum != SSL_ERROR_WANT_READ &&
errnum != SSL_ERROR_WANT_WRITE) {
_openssl_log_error(rc, tlsdata->ssl, "SSL_read");
break;
}
/* check to see if we have outgoing DTLS packets to send */
/* (SSL_read could have created DTLS control packets) */
bytesout = _netsnmp_send_queued_dtls_pkts(t, cachep);
/* If want_read/write but failed to actually send
anything then we need to wait for the other side,
so quit */
if (bytesout <= 0)
break;
/* retry reading */
DEBUGMSGTL(("9:dtlsudp", "recalling ssl_read\n"));
rc = SSL_read(tlsdata->ssl, buf, size);
MAKE_MEM_DEFINED(&rc, sizeof(rc));
if (rc > 0)
MAKE_MEM_DEFINED(buf, rc);
}
if (rc == -1) {
SNMP_FREE(tmStateRef);
DEBUGMSGTL(("9:dtlsudp", "no decoded data from dtls\n"));
if (SSL_get_error(tlsdata->ssl, rc) == SSL_ERROR_WANT_READ) {
DEBUGMSGTL(("9:dtlsudp", "ssl error want read\n"));
/* see if we have buffered write date to send out first */
if (cachep->write_cache) {
_netsnmp_bio_try_and_write_buffered(t, cachep);
/* XXX: check error or not here? */
/* (what would we do differently?) */
}
rc = -1; /* XXX: it's ok, but what's the right return? */
}
else
_openssl_log_error(rc, tlsdata->ssl, "SSL_read");
#if 0 /* to dump cache if we don't have a cookie, this is where to do it */
if (!(cachep->flags & NETSNMP_BIO_HAVE_COOKIE))
remove_and_free_bio_cache(cachep);
#endif
return rc;
}
DEBUGMSGTL(("dtlsudp", "received %d decoded bytes from dtls\n", rc));
if ((0 == rc) && (SSL_get_shutdown(tlsdata->ssl) & SSL_RECEIVED_SHUTDOWN)) {
DEBUGMSGTL(("dtlsudp", "peer disconnected\n"));
cachep->flags |= NETSNMP_BIO_DISCONNECTED;
remove_and_free_bio_cache(cachep);
SNMP_FREE(tmStateRef);
return rc;
}
cachep->flags |= NETSNMP_BIO_CONNECTED;
/* Until we've locally assured ourselves that all is well in
certificate-verification-land we need to be prepared to stop
here and ensure all our required checks have been done. */
if (0 == (tlsdata->flags & NETSNMP_TLSBASE_CERT_FP_VERIFIED)) {
int verifyresult;
if (tlsdata->flags & NETSNMP_TLSBASE_IS_CLIENT) {
/* verify that the server's certificate is the correct one */
/* RFC5953: section 5.3.1, step 1:
3) Using the destTransportDomain and
destTransportAddress values, the client will
initiate the (D)TLS handshake protocol to establish
session keys for message integrity and encryption.
If the attempt to establish a session is
unsuccessful, then snmpTlstmSessionOpenErrors is
incremented, an error indication is returned, and
processing stops. If the session failed to open
because the presented server certificate was
unknown or invalid then the
snmpTlstmSessionUnknownServerCertificate or
snmpTlstmSessionInvalidServerCertificates MUST be
incremented and a snmpTlstmServerCertificateUnknown
or snmpTlstmServerInvalidCertificate notification
SHOULD be sent as appropriate. Reasons for server
certificate invalidation includes, but is not
limited to, cryptographic validation failures and
an unexpected presented certificate identity.
*/
/* RFC5953: section 5.3.1, step 1:
4) The (D)TLS client MUST then verify that the (D)TLS
server's presented certificate is the expected
certificate. The (D)TLS client MUST NOT transmit
SNMP messages until the server certificate has been
authenticated, the client certificate has been
transmitted and the TLS connection has been fully
established.
If the connection is being established from
configuration based on SNMP-TARGET-MIB
configuration, then the snmpTlstmAddrTable
DESCRIPTION clause describes how the verification
is done (using either a certificate fingerprint, or
an identity authenticated via certification path
validation).
If the connection is being established for reasons
other than configuration found in the
SNMP-TARGET-MIB then configuration and procedures
outside the scope of this document should be
followed. Configuration mechanisms SHOULD be
similar in nature to those defined in the
snmpTlstmAddrTable to ensure consistency across
management configuration systems. For example, a
command-line tool for generating SNMP GETs might
support specifying either the server's certificate
fingerprint or the expected host name as a command
line argument.
*/
/* RFC5953: section 5.3.1, step 1:
5) (D)TLS provides assurance that the authenticated
identity has been signed by a trusted configured
certification authority. If verification of the
server's certificate fails in any way (for example
because of failures in cryptographic verification
or the presented identity did not match the
expected named entity) then the session
establishment MUST fail, the
snmpTlstmSessionInvalidServerCertificates object is
incremented. If the session can not be opened for
any reason at all, including cryptographic
verification failures and snmpTlstmCertToTSNTable
lookup failures, then the
snmpTlstmSessionOpenErrors counter is incremented
and processing stops.
*/
/* Implementation notes:
+ in the following function the server's certificate and
presented commonname or subjectAltName is checked
according to the rules in the snmpTlstmAddrTable.
*/
if ((verifyresult = netsnmp_tlsbase_verify_server_cert(tlsdata->ssl, tlsdata))
!= SNMPERR_SUCCESS) {
if (verifyresult == SNMPERR_TLS_NO_CERTIFICATE) {
/* assume we simply haven't received it yet and there
is more data to wait-for or send */
/* XXX: probably need to check for whether we should
send stuff from our end to continue the transaction
*/
SNMP_FREE(tmStateRef);
return -1;
} else {
/* XXX: free needed memory */
snmp_log(LOG_ERR,
"DTLSUDP: failed to verify ssl certificate (of the server)\n");
snmp_increment_statistic(STAT_TLSTM_SNMPTLSTMSESSIONUNKNOWNSERVERCERTIFICATE);
/* Step 5 says these are always incremented */
snmp_increment_statistic(STAT_TLSTM_SNMPTLSTMSESSIONINVALIDSERVERCERTIFICATES);
snmp_increment_statistic(STAT_TLSTM_SNMPTLSTMSESSIONOPENERRORS);
SNMP_FREE(tmStateRef);
return -1;
}
}
tlsdata->flags |= NETSNMP_TLSBASE_CERT_FP_VERIFIED;
DEBUGMSGTL(("dtlsudp", "Verified the server's certificate\n"));
} else {
#ifndef NETSNMP_NO_LISTEN_SUPPORT
/* verify that the client's certificate is the correct one */
if ((verifyresult = netsnmp_tlsbase_verify_client_cert(tlsdata->ssl, tlsdata))
!= SNMPERR_SUCCESS) {
if (verifyresult == SNMPERR_TLS_NO_CERTIFICATE) {
/* assume we simply haven't received it yet and there
is more data to wait-for or send */
/* XXX: probably need to check for whether we should
send stuff from our end to continue the transaction
*/
SNMP_FREE(tmStateRef);
return -1;
} else {
/* XXX: free needed memory */
snmp_log(LOG_ERR,
"DTLSUDP: failed to verify ssl certificate (of the client)\n");
snmp_increment_statistic(STAT_TLSTM_SNMPTLSTMSESSIONINVALIDCLIENTCERTIFICATES);
SNMP_FREE(tmStateRef);
return -1;
}
}
tlsdata->flags |= NETSNMP_TLSBASE_CERT_FP_VERIFIED;
DEBUGMSGTL(("dtlsudp", "Verified the client's certificate\n"));
#else /* NETSNMP_NO_LISTEN_SUPPORT */
return NULL;
#endif /* NETSNMP_NO_LISTEN_SUPPORT */
}
}
if (rc > 0)
cachep->msgnum++;
if (BIO_ctrl_pending(cachep->write_bio) > 0) {
_netsnmp_send_queued_dtls_pkts(t, cachep);
}
DEBUGIF ("9:dtlsudp") {
char *str =
t->base_transport->f_fmtaddr(t, addr_pair,
sizeof(netsnmp_indexed_addr_pair));
DEBUGMSGTL(("9:dtlsudp",
"recvfrom fd %d got %d bytes (from %s)\n",
t->sock, rc, str));
free(str);
}
/* see if we have buffered write date to send out first */
if (cachep->write_cache) {
if (SNMPERR_GENERR ==
_netsnmp_bio_try_and_write_buffered(t, cachep)) {
/* we still have data that can't get out in the buffer */
/* XXX: nothing to do here? */
}
}
if (netsnmp_tlsbase_wrapup_recv(tmStateRef, tlsdata, opaque, olength) !=
SNMPERR_SUCCESS)
return SNMPERR_GENERR;
/* RFC5953: section 5.1, step 4:
4) The TLS Transport Model passes the transportDomain,
transportAddress, incomingMessage, and incomingMessageLength to
the Dispatcher using the receiveMessage ASI:
statusInformation =
receiveMessage(
IN transportDomain -- snmpTLSTCPDomain or snmpDTLSUDPDomain,
IN transportAddress -- address for the received message
IN incomingMessage -- the whole SNMP message from (D)TLS
IN incomingMessageLength -- the length of the SNMP message
IN tmStateReference -- transport info
)
*/
/* Implementation notes: those parameters are all passed outward
using the functions arguments and the return code below (the length) */
return rc;
}
static int
netsnmp_dtlsudp_send(netsnmp_transport *t, const void *buf, int size,
void **opaque, int *olength)
{
int rc = -1;
const netsnmp_indexed_addr_pair *addr_pair = NULL;
bio_cache *cachep = NULL;
const netsnmp_tmStateReference *tmStateRef = NULL;
void *outbuf;
_netsnmpTLSBaseData *tlsdata = NULL;
int socksize;
void *sa;
DEBUGTRACETOK("9:dtlsudp");
DEBUGMSGTL(("dtlsudp", "sending %d bytes\n", size));
if (NULL == t || t->sock <= 0) {
snmp_increment_statistic(STAT_TLSTM_SNMPTLSTMSESSIONINVALIDCACHES);
snmp_log(LOG_ERR, "invalid netsnmp_dtlsudp_send usage\n");
return -1;
}
/* determine remote addresses */
addr_pair = _extract_addr_pair(t, opaque ? *opaque : NULL,
olength ? *olength : 0);
if (NULL == addr_pair) {
/* RFC5953: section 5.2, step 1:
1) If tmStateReference does not refer to a cache containing values
for tmTransportDomain, tmTransportAddress, tmSecurityName,
tmRequestedSecurityLevel, and tmSameSecurity, then increment the
snmpTlstmSessionInvalidCaches counter, discard the message, and
return the error indication in the statusInformation. Processing
of this message stops.
*/
snmp_increment_statistic(STAT_TLSTM_SNMPTLSTMSESSIONINVALIDCACHES);
snmp_log(LOG_ERR, "dtlsudp_send: can't get address to send to\n");
return -1;
}
/* RFC5953: section 5.2, step 2:
2) Extract the tmSessionID, tmTransportDomain, tmTransportAddress,
tmSecurityName, tmRequestedSecurityLevel, and tmSameSecurity
values from the tmStateReference. Note: The tmSessionID value
may be undefined if no session exists yet over which the message
can be sent.
*/
/* Implementation notes:
- we use the t->data memory pointer as the session ID
- the transport domain is already the correct type if we got here
- if we don't have a session yet (eg, no tmSessionID from the
specs) then we create one automatically here.
*/
if (opaque != NULL && *opaque != NULL &&
olength != NULL && *olength == sizeof(netsnmp_tmStateReference))
tmStateRef = *opaque;
/* RFC5953: section 5.2, step 3:
3) If tmSameSecurity is true and either tmSessionID is undefined or
refers to a session that is no longer open then increment the
snmpTlstmSessionNoSessions counter, discard the message and
return the error indication in the statusInformation. Processing
of this message stops.
*/
/* RFC5953: section 5.2, step 4:
4) If tmSameSecurity is false and tmSessionID refers to a session
that is no longer available then an implementation SHOULD open a
new session using the openSession() ASI (described in greater
detail in step 5b). Instead of opening a new session an
implementation MAY return a snmpTlstmSessionNoSessions error to
the calling module and stop processing of the message.
*/
/* Implementation Notes:
- We would never get here if the sessionID was different. We
tie packets directly to the transport object and it could
never be sent back over a different transport, which is what
the above text is trying to prevent.
- Auto-connections are handled higher in the Net-SNMP library stack
*/
/* RFC5953: section 5.2, step 5:
5) If tmSessionID is undefined, then use tmTransportDomain,
tmTransportAddress, tmSecurityName and tmRequestedSecurityLevel
to see if there is a corresponding entry in the LCD suitable to
send the message over.
5a) If there is a corresponding LCD entry, then this session
will be used to send the message.
5b) If there is no corresponding LCD entry, then open a session
using the openSession() ASI (discussed further in
Section 5.3.1). Implementations MAY wish to offer message
buffering to prevent redundant openSession() calls for the
same cache entry. If an error is returned from
openSession(), then discard the message, discard the
tmStateReference, increment the snmpTlstmSessionOpenErrors,
return an error indication to the calling module and stop
processing of the message.
*/
/* we're always a client if we're sending to something unknown yet */
if (NULL ==
(cachep = find_or_create_bio_cache(t, &addr_pair->remote_addr,
WE_ARE_CLIENT))) {
snmp_increment_statistic(STAT_TLSTM_SNMPTLSTMSESSIONOPENERRORS);
return -1;
}
tlsdata = cachep->tlsdata;
if (NULL == tlsdata || NULL == tlsdata->ssl) {
/** xxx mem leak? free created bio cache? */
snmp_increment_statistic(STAT_TLSTM_SNMPTLSTMSESSIONNOSESSIONS);
snmp_log(LOG_ERR, "bad tls data or ssl ptr in netsnmp_dtlsudp_send\n");
return -1;
}
if (!tlsdata->securityName && tmStateRef &&
tmStateRef->securityNameLen > 0) {
tlsdata->securityName = strdup(tmStateRef->securityName);
}
/* see if we have previous outgoing data to send */
if (cachep->write_cache) {
if (SNMPERR_GENERR == _netsnmp_bio_try_and_write_buffered(t, cachep)) {
/* we still have data that can't get out in the buffer */
DEBUGIF ("9:dtlsudp") {
char *str = t->base_transport->f_fmtaddr(t, addr_pair,
sizeof(netsnmp_indexed_addr_pair));
DEBUGMSGTL(("9:dtlsudp", "cached %d bytes for %s on fd %d\n",
size, str, t->sock));
free(str);
}
/* add the new data to the end of the existing cache */
if (_netsnmp_add_buffered_data(cachep, buf, size) !=
SNMPERR_SUCCESS) {
/* XXX: free and close */
}
return -1;
}
}
DEBUGIF ("9:dtlsudp") {
char *str = t->base_transport->f_fmtaddr(t, addr_pair,
sizeof(netsnmp_indexed_addr_pair));
DEBUGMSGTL(("9:dtlsudp", "send %d bytes from %p to %s on fd %d\n",
size, buf, str, t->sock));
free(str);
}
/* RFC5953: section 5.2, step 6:
6) Using either the session indicated by the tmSessionID if there
was one or the session resulting from a previous step (4 or 5),
pass the outgoingMessage to (D)TLS for encapsulation and
transmission.
*/
rc = SSL_write(tlsdata->ssl, buf, size);
while (rc == -1) {
int bytesout;
int errnum = SSL_get_error(tlsdata->ssl, rc);
/* don't treat want_read/write errors as real errors */
if (errnum != SSL_ERROR_WANT_READ &&
errnum != SSL_ERROR_WANT_WRITE) {
DEBUGMSGTL(("dtlsudp", "ssl_write error\n"));
_openssl_log_error(rc, tlsdata->ssl, "SSL_write");
break;
}
/* check to see if we have outgoing DTLS packets to send */
/* (SSL_read could have created DTLS control packets) */
bytesout = _netsnmp_send_queued_dtls_pkts(t, cachep);
/* If want_read/write but failed to actually send
anything then we need to wait for the other side,
so quit */
if (bytesout <= 0) {
/* We need more data written to or read from the socket
but we're failing to do so and need to wait till the
socket is ready again; unfortunately this means we need
to buffer the SNMP data temporarily in the mean time */
DEBUGMSGTL(("9:dtlsudp", "cached %d bytes for fd %d\n", size,
t->sock));
/* remember the packet */
if (_netsnmp_add_buffered_data(cachep, buf, size) !=
SNMPERR_SUCCESS) {
/* XXX: free and close */
return -1;
}
/* exit out of the loop until we get called again from
socket data */
break;
}
DEBUGMSGTL(("9:dtlsudp", "recalling ssl_write\n"));
rc = SSL_write(tlsdata->ssl, buf, size);
}
if (rc > 0)
cachep->msgnum++;
/* for memory bios, we now read from openssl's write buffer (ie,
the packet to go out) and send it out the udp port manually */
rc = BIO_ctrl_pending(cachep->write_bio);
if (rc <= 0) {
/* in theory an ok thing */
return 0;
}
outbuf = malloc(rc);
if (!outbuf)
return -1;
rc = BIO_read(cachep->write_bio, outbuf, rc);
MAKE_MEM_DEFINED(outbuf, rc);
socksize = netsnmp_sockaddr_size(&cachep->sas.sa);
sa = &cachep->sas.sa;
rc = t->base_transport->f_send(t, outbuf, rc, &sa, &socksize);
free(outbuf);
return rc;
}
static int
netsnmp_dtlsudp_close(netsnmp_transport *t)
{
/* XXX: issue a proper dtls closure notification(s) */
bio_cache *cachep = NULL;
_netsnmpTLSBaseData *tlsbase = NULL;
DEBUGTRACETOK("9:dtlsudp");
DEBUGMSGTL(("dtlsudp:close", "closing dtlsudp transport %p\n", t));
/* RFC5953: section 5.4, step 1:
1) Increment either the snmpTlstmSessionClientCloses or the
snmpTlstmSessionServerCloses counter as appropriate.
*/
snmp_increment_statistic(STAT_TLSTM_SNMPTLSTMSESSIONCLIENTCLOSES);
/* RFC5953: section 5.4, step 2:
2) Look up the session using the tmSessionID.
*/
/* Implementation notes:
+ Our session id is stored as the t->data pointer
*/
if (NULL != t->data && t->data_length == sizeof(_netsnmpTLSBaseData)) {
tlsbase = t->data;
if (tlsbase->addr)
cachep = find_bio_cache(&tlsbase->addr->remote_addr);
}
/* RFC5953: section 5.4, step 3:
3) If there is no open session associated with the tmSessionID, then
closeSession processing is completed.
*/
if (NULL == cachep)
return netsnmp_socketbase_close(t);
/* if we have any remaining packets to send, try to send them */
if (cachep->write_cache_len > 0) {
int i = 0;
char buf[8192];
int rc;
void *opaque = NULL;
int opaque_len = 0;
fd_set readfs;
NETSNMP_SELECT_TIMEVAL tv;
DEBUGMSGTL(("dtlsudp:close",
"%" NETSNMP_PRIz "d bytes remain in write_cache\n",
cachep->write_cache_len));
/*
* if negotiations have completed and we've received data, try and
* send any queued packets.
*/
if (1) {
/* make configurable:
- do this at all?
- retries
- timeout
*/
for (i = 0; i < 6 && cachep->write_cache_len != 0; ++i) {
/* first see if we can send out what we have */
_netsnmp_bio_try_and_write_buffered(t, cachep);
if (cachep->write_cache_len == 0)
break;
/* if we've failed that, we probably need to wait for packets */
FD_ZERO(&readfs);
FD_SET(t->sock, &readfs);
tv.tv_sec = 0;
tv.tv_usec = 50000;
rc = select(t->sock+1, &readfs, NULL, NULL, &tv);
if (rc > 0) {
/* junk recv for catching negotiations still in play */
opaque_len = 0;
rc = netsnmp_dtlsudp_recv(t, buf, sizeof(buf),
&opaque, &opaque_len);
DEBUGMSGTL(("dtlsudp:close",
"netsnmp_dtlsudp_recv() returned %d\n", rc));
SNMP_FREE(opaque);
}
} /* for loop */
}
/** dump anything that wasn't sent */
if (cachep->write_cache_len > 0) {
DEBUGMSGTL(("dtlsudp:close",
"dumping %" NETSNMP_PRIz "d bytes from write_cache\n",
cachep->write_cache_len));
SNMP_FREE(cachep->write_cache);
cachep->write_cache_len = 0;
}
}
/* RFC5953: section 5.4, step 4:
4) Have (D)TLS close the specified connection. This MUST include
sending a close_notify TLS Alert to inform the other side that
session cleanup may be performed.
*/
if (NULL != cachep->tlsdata && NULL != cachep->tlsdata->ssl) {
DEBUGMSGTL(("dtlsudp:close", "closing SSL socket\n"));
SSL_shutdown(cachep->tlsdata->ssl);
/* send the close_notify we maybe generated in step 4 */
if (BIO_ctrl_pending(cachep->write_bio) > 0)
_netsnmp_send_queued_dtls_pkts(t, cachep);
}
remove_and_free_bio_cache(cachep);
return netsnmp_socketbase_close(t);
}
static char *
netsnmp_dtlsudp_fmtaddr(netsnmp_transport *t, const void *data, int len,
const char *pfx,
char *(*fmt_base_addr)(const char *pfx,
netsnmp_transport *t,
const void *data, int len))
{
if (!data) {
data = t->data;
len = t->data_length;
}
switch (data ? len : 0) {
case sizeof(netsnmp_indexed_addr_pair):
return netsnmp_ipv4_fmtaddr(pfx, t, data, len);
case sizeof(netsnmp_tmStateReference): {
const netsnmp_tmStateReference *r = data;
const netsnmp_indexed_addr_pair *p = &r->addresses;
netsnmp_transport *bt = t->base_transport;
if (r->have_addresses) {
return fmt_base_addr("DTLSUDP", t, p, sizeof(*p));
} else if (bt && t->data_length == sizeof(_netsnmpTLSBaseData)) {
_netsnmpTLSBaseData *tlsdata = t->data;
netsnmp_indexed_addr_pair *tls_addr = tlsdata->addr;
return bt->f_fmtaddr(bt, tls_addr, sizeof(*tls_addr));
} else if (bt) {
return bt->f_fmtaddr(bt, t->data, t->data_length);
} else {
return strdup("DTLSUDP: unknown");
}
}
case sizeof(_netsnmpTLSBaseData): {
const _netsnmpTLSBaseData *b = data;
char *buf;
if (asprintf(&buf, "DTLSUDP: %s", b->addr_string) < 0)
buf = NULL;
return buf;
}
case 0:
return strdup("DTLSUDP: unknown");
default: {
char *buf;
if (asprintf(&buf, "DTLSUDP: len %d", len) < 0)
buf = NULL;
return buf;
}
}
}
static char *
netsnmp_dtlsudp4_fmtaddr(netsnmp_transport *t, const void *data, int len)
{
return netsnmp_dtlsudp_fmtaddr(t, data, len, "DTLSUDP",
netsnmp_ipv4_fmtaddr);
}
/*
* Open a DTLS-based transport for SNMP. Local is TRUE if addr is the local
* address to bind to (i.e. this is a server-type session); otherwise addr is
* the remote address to send things to.
*/
static netsnmp_transport *
_transport_common(netsnmp_transport *t, int local)
{
char *tmp = NULL;
int tmp_len;
DEBUGTRACETOK("9:dtlsudp");
if (NULL == t)
return NULL;
/** save base transport for clients; need in send/recv functions later */
if (t->data) { /* don't copy data */
tmp = t->data;
tmp_len = t->data_length;
t->data = NULL;
}
t->base_transport = netsnmp_transport_copy(t);
if (tmp) {
t->data = tmp;
t->data_length = tmp_len;
}
if (NULL != t->data &&
t->data_length == sizeof(netsnmp_indexed_addr_pair)) {
_netsnmpTLSBaseData *tlsdata =
netsnmp_tlsbase_allocate_tlsdata(t, local);
tlsdata->addr = t->data;
t->data = tlsdata;
t->data_length = sizeof(_netsnmpTLSBaseData);
}
/*
* Set Domain
*/
t->domain = netsnmpDTLSUDPDomain;
t->domain_length = netsnmpDTLSUDPDomain_len;
t->f_recv = netsnmp_dtlsudp_recv;
t->f_send = netsnmp_dtlsudp_send;
t->f_close = netsnmp_dtlsudp_close;
t->f_config = netsnmp_tlsbase_config;
t->f_setup_session = netsnmp_tlsbase_session_init;
t->f_accept = NULL;
t->f_fmtaddr = netsnmp_dtlsudp4_fmtaddr;
t->f_get_taddr = netsnmp_ipv4_get_taddr;
t->flags = NETSNMP_TRANSPORT_FLAG_TUNNELED;
return t;
}
netsnmp_transport *
netsnmp_dtlsudp_transport(const struct netsnmp_ep *ep, int local)
{
const struct sockaddr_in *addr = &ep->a.sin;
netsnmp_transport *t, *t2;
DEBUGTRACETOK("dtlsudp");
t = netsnmp_udp_transport(ep, local);
if (NULL == t)
return NULL;
t2 = _transport_common(t, local);
if (!t2) {
netsnmp_transport_free(t);
return NULL;
}
if (!local) {
/* dtls needs to bind the socket for SSL_write to work */
if (connect(t->sock, (const struct sockaddr *)addr, sizeof(*addr)) < 0)
snmp_log(LOG_ERR, "dtls: failed to connect\n");
}
return t2;
}
#ifdef NETSNMP_TRANSPORT_UDPIPV6_DOMAIN
static char *
netsnmp_dtlsudp6_fmtaddr(netsnmp_transport *t, const void *data, int len)
{
return netsnmp_dtlsudp_fmtaddr(t, data, len, "DTLSUDP6",
netsnmp_ipv6_fmtaddr);
}
/*
* Open a DTLS-based transport for SNMP. Local is TRUE if addr is the local
* address to bind to (i.e. this is a server-type session); otherwise addr is
* the remote address to send things to.
*/
netsnmp_transport *
netsnmp_dtlsudp6_transport(const struct netsnmp_ep *ep, int local)
{
const struct sockaddr_in6 *addr = &ep->a.sin6;
netsnmp_transport *t, *t2;
DEBUGTRACETOK("dtlsudp");
t = netsnmp_udp6_transport(ep, local);
if (NULL == t)
return NULL;
t2 = _transport_common(t, local);
if (!t2) {
netsnmp_transport_free(t);
return NULL;
}
if (!local) {
/* dtls needs to bind the socket for SSL_write to work */
if (connect(t->sock, (const struct sockaddr *)addr, sizeof(*addr)) < 0)
snmp_log(LOG_ERR, "dtls: failed to connect\n");
}
/* XXX: Potentially set sock opts here (SO_SNDBUF/SO_RCV_BUF) */
/* XXX: and buf size */
t2->f_fmtaddr = netsnmp_dtlsudp6_fmtaddr;
t2->f_get_taddr = netsnmp_ipv6_get_taddr;
return t2;
}
#endif
netsnmp_transport *
netsnmp_dtlsudp_create_tstring(const char *str, int isserver,
const char *default_target)
{
struct netsnmp_ep ep;
netsnmp_transport *t;
_netsnmpTLSBaseData *tlsdata;
char buf[SPRINT_MAX_LEN], *cp;
if (netsnmp_sockaddr_in3(&ep, str, default_target))
t = netsnmp_dtlsudp_transport(&ep, isserver);
#ifdef NETSNMP_TRANSPORT_UDPIPV6_DOMAIN
else if (netsnmp_sockaddr_in6_3(&ep, str, default_target))
t = netsnmp_dtlsudp6_transport(&ep, isserver);
#endif
else
return NULL;
/* see if we can extract the remote hostname */
if (!isserver && t && t->data && str) {
tlsdata = t->data;
/* search for a : */
if (NULL != (cp = strrchr(str, ':'))) {
sprintf(buf, "%.*s", (int) SNMP_MIN(cp - str, sizeof(buf) - 1),
str);
} else {
/* else the entire spec is a host name only */
strlcpy(buf, str, sizeof(buf));
}
tlsdata->their_hostname = strdup(buf);
}
return t;
}
netsnmp_transport *
netsnmp_dtlsudp_create_ostring(const void *o, size_t o_len, int local)
{
struct netsnmp_ep ep;
memset(&ep, 0, sizeof(ep));
if (netsnmp_ipv4_ostring_to_sockaddr(&ep.a.sin, o, o_len))
return netsnmp_dtlsudp_transport(&ep, local);
#ifdef NETSNMP_TRANSPORT_UDPIPV6_DOMAIN
else if (netsnmp_ipv6_ostring_to_sockaddr(&ep.a.sin6, o, o_len))
return netsnmp_dtlsudp6_transport(&ep, local);
#endif
else
return NULL;
}
void
netsnmp_dtlsudp_ctor(void)
{
static const char indexname[] = "_netsnmp_addr_info";
static const char *prefixes[] = { "dtlsudp", "dtls"
#ifdef NETSNMP_TRANSPORT_UDPIPV6_DOMAIN
, "dtlsudp6", "dtls6"
#endif
};
int i, num_prefixes = sizeof(prefixes) / sizeof(char *);
#ifdef NETSNMP_TRANSPORT_UDPIPV6_DOMAIN
static const char indexname6[] = "_netsnmp_addr_info6";
#endif
DEBUGMSGTL(("dtlsudp", "registering DTLS constructor\n"));
/* config settings */
#ifdef NETSNMP_TRANSPORT_UDPIPV6_DOMAIN
if (!openssl_addr_index6)
openssl_addr_index6 =
SSL_get_ex_new_index(0, NETSNMP_REMOVE_CONST(void *, indexname6),
NULL, NULL, NULL);
#endif
dtlsudpDomain.name = netsnmpDTLSUDPDomain;
dtlsudpDomain.name_length = netsnmpDTLSUDPDomain_len;
dtlsudpDomain.prefix = calloc(num_prefixes + 1, sizeof(char *));
for (i = 0; i < num_prefixes; ++ i)
dtlsudpDomain.prefix[i] = prefixes[i];
dtlsudpDomain.f_create_from_tstring_new = netsnmp_dtlsudp_create_tstring;
dtlsudpDomain.f_create_from_ostring = netsnmp_dtlsudp_create_ostring;
if (!openssl_addr_index)
openssl_addr_index =
SSL_get_ex_new_index(0, NETSNMP_REMOVE_CONST(void *, indexname),
NULL, NULL, NULL);
netsnmp_tdomain_register(&dtlsudpDomain);
}
/*
* Much of the code below was taken from the OpenSSL example code
* and is subject to the OpenSSL copyright.
*/
#define NETSNMP_COOKIE_SECRET_LENGTH 16
int cookie_initialized=0;
unsigned char cookie_secret[NETSNMP_COOKIE_SECRET_LENGTH];
int netsnmp_dtls_gen_cookie(SSL *ssl, unsigned char *cookie,
unsigned int *cookie_len)
{
unsigned char *buffer, result[EVP_MAX_MD_SIZE];
unsigned int length, resultlength;
bio_cache *cachep = NULL;
const netsnmp_sockaddr_storage *peer;
/* Initialize a random secret */
if (!cookie_initialized) {
if (!RAND_bytes(cookie_secret, NETSNMP_COOKIE_SECRET_LENGTH)) {
snmp_log(LOG_ERR, "dtls: error setting random cookie secret\n");
return 0;
}
MAKE_MEM_DEFINED(cookie_secret, NETSNMP_COOKIE_SECRET_LENGTH);
cookie_initialized = 1;
}
DEBUGMSGT(("dtlsudp:cookie", "generating cookie...\n"));
/* Read peer information */
cachep = SSL_get_ex_data(ssl, openssl_addr_index);
if (!cachep) {
snmp_log(LOG_ERR, "dtls: failed to get the peer address\n");
return 0;
}
peer = &cachep->sas;
/* Create buffer with peer's address and port */
length = 0;
switch (peer->sa.sa_family) {
case AF_INET:
length += sizeof(struct in_addr);
length += sizeof(peer->sin.sin_port);
break;
#ifdef NETSNMP_TRANSPORT_UDPIPV6_DOMAIN
case AF_INET6:
length += sizeof(struct in6_addr);
length += sizeof(peer->sin6.sin6_port);
break;
#endif
default:
snmp_log(LOG_ERR, "dtls generating cookie: unknown family: %d\n",
peer->sa.sa_family);
return 0;
}
buffer = malloc(length);
if (buffer == NULL) {
snmp_log(LOG_ERR,"dtls: out of memory\n");
return 0;
}
switch (peer->sa.sa_family) {
case AF_INET:
memcpy(buffer,
&peer->sin.sin_port,
sizeof(peer->sin.sin_port));
memcpy(buffer + sizeof(peer->sin.sin_port),
&peer->sin.sin_addr,
sizeof(struct in_addr));
break;
#ifdef NETSNMP_TRANSPORT_UDPIPV6_DOMAIN
case AF_INET6:
memcpy(buffer,
&peer->sin6.sin6_port,
sizeof(peer->sin6.sin6_port));
memcpy(buffer + sizeof(peer->sin6.sin6_port),
&peer->sin6.sin6_addr,
sizeof(struct in6_addr));
break;
#endif
default:
snmp_log(LOG_ERR, "dtls: unknown address family generating a cookie\n");
free(buffer);
return 0;
}
/* Calculate HMAC of buffer using the secret */
HMAC(EVP_sha1(), cookie_secret, NETSNMP_COOKIE_SECRET_LENGTH,
buffer, length, result, &resultlength);
free(buffer);
memcpy(cookie, result, resultlength);
*cookie_len = resultlength;
DEBUGMSGT(("9:dtlsudp:cookie", "generated %d byte cookie\n", *cookie_len));
return 1;
}
int netsnmp_dtls_verify_cookie(SSL *ssl,
SECOND_APPVERIFY_COOKIE_CB_ARG_QUALIFIER
unsigned char *cookie,
unsigned int cookie_len)
{
unsigned char *buffer, result[EVP_MAX_MD_SIZE];
unsigned int length, resultlength, rc;
bio_cache *cachep = NULL;
const netsnmp_sockaddr_storage *peer;
/* If secret isn't initialized yet, the cookie can't be valid */
if (!cookie_initialized)
return 0;
DEBUGMSGT(("9:dtlsudp:cookie", "verifying %d byte cookie\n", cookie_len));
cachep = SSL_get_ex_data(ssl, openssl_addr_index);
if (!cachep) {
snmp_log(LOG_ERR, "dtls: failed to get the peer address\n");
return 0;
}
peer = &cachep->sas;
/* Create buffer with peer's address and port */
length = 0;
switch (peer->sa.sa_family) {
case AF_INET:
length += sizeof(struct in_addr);
length += sizeof(peer->sin.sin_port);
break;
#ifdef NETSNMP_TRANSPORT_UDPIPV6_DOMAIN
case AF_INET6:
length += sizeof(struct in6_addr);
length += sizeof(peer->sin6.sin6_port);
break;
#endif
default:
snmp_log(LOG_ERR,
"dtls: unknown address family %d generating a cookie\n",
peer->sa.sa_family);
return 0;
}
buffer = malloc(length);
if (buffer == NULL) {
snmp_log(LOG_ERR, "dtls: unknown address family generating a cookie\n");
return 0;
}
switch (peer->sa.sa_family) {
case AF_INET:
memcpy(buffer,
&peer->sin.sin_port,
sizeof(peer->sin.sin_port));
memcpy(buffer + sizeof(peer->sin.sin_port),
&peer->sin.sin_addr,
sizeof(struct in_addr));
break;
#ifdef NETSNMP_TRANSPORT_UDPIPV6_DOMAIN
case AF_INET6:
memcpy(buffer,
&peer->sin6.sin6_port,
sizeof(peer->sin6.sin6_port));
memcpy(buffer + sizeof(peer->sin6.sin6_port),
&peer->sin6.sin6_addr,
sizeof(struct in6_addr));
break;
#endif
default:
snmp_log(LOG_ERR,
"dtls: unknown address family %d generating a cookie\n",
peer->sa.sa_family);
free(buffer);
return 0;
}
/* Calculate HMAC of buffer using the secret */
HMAC(EVP_sha1(), cookie_secret, NETSNMP_COOKIE_SECRET_LENGTH,
buffer, length, result, &resultlength);
free(buffer);
if (cookie_len != resultlength || memcmp(result, cookie, resultlength) != 0)
rc = 0;
else {
rc = 1;
cachep->flags |= NETSNMP_BIO_HAVE_COOKIE;
}
DEBUGMSGT(("dtlsudp:cookie", "verify cookie: %d\n", rc));
return rc;
}
#endif /* HAVE_LIBSSL_DTLS */
|