aboutsummaryrefslogtreecommitdiff
path: root/src/lib/protocols/roughtime.c
blob: 5ebd00b84eb8f9b9db4b9f39f90371d389b4a5cd (plain)
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
/*
 * roughtime.c
 *
 * Copyright (C) 2024 - ntop.org
 *
 * nDPI is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * nDPI is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with nDPI.  If not, see <http://www.gnu.org/licenses/>.
 *
 */


#include "ndpi_protocol_ids.h"

#define NDPI_CURRENT_PROTO NDPI_PROTOCOL_ROUGHTIME

#include "ndpi_api.h"
#include "ndpi_private.h"

static u_int32_t const valid_tags[] = {
  0x00444150 /* PAD */,
  0x00474953 /* SIG */,
  0x00524556 /* VER */,
  0x31545544 /* DUT1 */,
  0x434e4f4e /* NONC */,
  0x454c4544 /* DELE */,
  0x48544150 /* PATH */,
  0x49415444 /* DTAI */,
  0x49444152 /* RADI */,
  0x4b425550 /* PUBK */,
  0x5041454c /* LEAP */,
  0x5044494d /* MIDP */,
  0x50455253 /* SREP */,
  0x544e494d /* MINT */,
  0x544f4f52 /* ROOT */,
  0x54524543 /* CERT */,
  0x5458414d /* MAXT */,
  0x58444e49 /* INDX */,
  /*
   * It seems that some implementations are not following the specs
   * by using 0xFF instead of 0x00 as ASCII NUL.
   */
  0xFF444150 /* PAD */,
  0xFF474953 /* SIG */,
  0xFF524556 /* VER */,
  /*
   * Newer drafts may have the following additional tag.
   */
  0x7a7a7a7a /* ZZZZ */,
};

static void ndpi_int_roughtime_add_connection(struct ndpi_detection_module_struct * const ndpi_struct,
                                              struct ndpi_flow_struct * const flow)
{
  NDPI_LOG_INFO(ndpi_struct, "found roughtime\n");
  ndpi_set_detected_protocol(ndpi_struct, flow,
                             NDPI_PROTOCOL_ROUGHTIME,
                             NDPI_PROTOCOL_UNKNOWN,
                             NDPI_CONFIDENCE_DPI);
}

static void ndpi_search_roughtime(struct ndpi_detection_module_struct *ndpi_struct,
                                  struct ndpi_flow_struct *flow)
{
  struct ndpi_packet_struct const * const packet = &ndpi_struct->packet;

  NDPI_LOG_INFO(ndpi_struct, "search roughtime\n");

  if (packet->payload_packet_len < 4)
  {
    NDPI_EXCLUDE_PROTO(ndpi_struct, flow);
    return;
  }

  u_int32_t number_of_tags = le32toh(get_u_int32_t(packet->payload, 0));
  size_t const minimum_length = 4 /* number of tags (N) */ +
                               (number_of_tags - 1) * 4 /* number of tag offsets (N-1) */ +
                               (number_of_tags * 4) /* tags itself (N) */;
  if (number_of_tags < 1 || packet->payload_packet_len < minimum_length ||
      number_of_tags > NDPI_ARRAY_LENGTH(valid_tags))
  {
    NDPI_EXCLUDE_PROTO(ndpi_struct, flow);
    return;
  }

  if (number_of_tags > 1) {
    u_int32_t tag_offset = le32toh(get_u_int32_t(packet->payload, 4 + (number_of_tags - 2) * 4));
    if (packet->payload_packet_len < 4 + (number_of_tags - 1) * 4 + tag_offset)
    {
      NDPI_EXCLUDE_PROTO(ndpi_struct, flow);
      return;
    }
  }

  size_t i;
  for (i = 0; i < number_of_tags; ++i)
  {
    u_int32_t tag = le32toh(get_u_int32_t(packet->payload, 4 + (number_of_tags - 1) * 4 + i * 4));

    size_t j;
    for (j = 0; j < NDPI_ARRAY_LENGTH(valid_tags); ++j)
    {
      if (tag == valid_tags[j])
      {
        break;
      }
    }
    if (j == NDPI_ARRAY_LENGTH(valid_tags))
    {
      NDPI_EXCLUDE_PROTO(ndpi_struct, flow);
      return;
    }
  }

  ndpi_int_roughtime_add_connection(ndpi_struct, flow);
}

void init_roughtime_dissector(struct ndpi_detection_module_struct *ndpi_struct,
                              u_int32_t *id)
{
  ndpi_set_bitmask_protocol_detection("Roughtime", ndpi_struct, *id,
    NDPI_PROTOCOL_ROUGHTIME,
    ndpi_search_roughtime,
    NDPI_SELECTION_BITMASK_PROTOCOL_V4_V6_TCP_OR_UDP_WITH_PAYLOAD_WITHOUT_RETRANSMISSION,
    SAVE_DETECTION_BITMASK_AS_UNKNOWN,
    ADD_TO_DETECTION_BITMASK
  );

  *id += 1;
}