snmpv3.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /**
  2. * @file
  3. * Additional SNMPv3 functionality RFC3414 and RFC3826.
  4. */
  5. /*
  6. * Copyright (c) 2016 Elias Oenal.
  7. * All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without modification,
  10. * are permitted provided that the following conditions are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright notice,
  13. * this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright notice,
  15. * this list of conditions and the following disclaimer in the documentation
  16. * and/or other materials provided with the distribution.
  17. * 3. The name of the author may not be used to endorse or promote products
  18. * derived from this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  21. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  22. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  23. * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  24. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  25. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  27. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  28. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  29. * OF SUCH DAMAGE.
  30. *
  31. * Author: Elias Oenal <lwip@eliasoenal.com>
  32. */
  33. #include "snmpv3_priv.h"
  34. #include "lwip/apps/snmpv3.h"
  35. #include "lwip/sys.h"
  36. #include <string.h>
  37. #if LWIP_SNMP && LWIP_SNMP_V3
  38. #ifdef LWIP_SNMPV3_INCLUDE_ENGINE
  39. #include LWIP_SNMPV3_INCLUDE_ENGINE
  40. #endif
  41. #define SNMP_MAX_TIME_BOOT 2147483647UL
  42. /** Call this if engine has been changed. Has to reset boots, see below */
  43. void
  44. snmpv3_engine_id_changed(void)
  45. {
  46. snmpv3_set_engine_boots(0);
  47. }
  48. /** According to RFC3414 2.2.2.
  49. *
  50. * The number of times that the SNMP engine has
  51. * (re-)initialized itself since snmpEngineID
  52. * was last configured.
  53. */
  54. u32_t
  55. snmpv3_get_engine_boots_internal(void)
  56. {
  57. if (snmpv3_get_engine_boots() == 0 ||
  58. snmpv3_get_engine_boots() < SNMP_MAX_TIME_BOOT) {
  59. return snmpv3_get_engine_boots();
  60. }
  61. snmpv3_set_engine_boots(SNMP_MAX_TIME_BOOT);
  62. return snmpv3_get_engine_boots();
  63. }
  64. /** RFC3414 2.2.2.
  65. *
  66. * Once the timer reaches 2147483647 it gets reset to zero and the
  67. * engine boot ups get incremented.
  68. */
  69. u32_t
  70. snmpv3_get_engine_time_internal(void)
  71. {
  72. if (snmpv3_get_engine_time() >= SNMP_MAX_TIME_BOOT) {
  73. snmpv3_reset_engine_time();
  74. if (snmpv3_get_engine_boots() < SNMP_MAX_TIME_BOOT - 1) {
  75. snmpv3_set_engine_boots(snmpv3_get_engine_boots() + 1);
  76. } else {
  77. snmpv3_set_engine_boots(SNMP_MAX_TIME_BOOT);
  78. }
  79. }
  80. return snmpv3_get_engine_time();
  81. }
  82. #if LWIP_SNMP_V3_CRYPTO
  83. /* This function ignores the byte order suggestion in RFC3414
  84. * since it simply doesn't influence the effectiveness of an IV.
  85. *
  86. * Implementing RFC3826 priv param algorithm if LWIP_RAND is available.
  87. *
  88. * @todo: This is a potential thread safety issue.
  89. */
  90. err_t
  91. snmpv3_build_priv_param(u8_t* priv_param)
  92. {
  93. #ifdef LWIP_RAND /* Based on RFC3826 */
  94. static u8_t init;
  95. static u32_t priv1, priv2;
  96. /* Lazy initialisation */
  97. if (init == 0) {
  98. init = 1;
  99. priv1 = LWIP_RAND();
  100. priv2 = LWIP_RAND();
  101. }
  102. SMEMCPY(&priv_param[0], &priv1, sizeof(priv1));
  103. SMEMCPY(&priv_param[4], &priv2, sizeof(priv2));
  104. /* Emulate 64bit increment */
  105. priv1++;
  106. if (!priv1) { /* Overflow */
  107. priv2++;
  108. }
  109. #else /* Based on RFC3414 */
  110. static u32_t ctr;
  111. u32_t boots = LWIP_SNMPV3_GET_ENGINE_BOOTS();
  112. SMEMCPY(&priv_param[0], &boots, 4);
  113. SMEMCPY(&priv_param[4], &ctr, 4);
  114. ctr++;
  115. #endif
  116. return ERR_OK;
  117. }
  118. #endif /* LWIP_SNMP_V3_CRYPTO */
  119. #endif