source: S-port/trunk/Middlewares/Third_Party/LwIP/src/apps/snmp/snmpv3.c

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