aboutsummaryrefslogtreecommitdiffstats
path: root/epan/dissectors/packet-corosync-totemnet.c
blob: 640b4144f001ef97b430b25a4e141faac6aa2caf (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
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
/* packet-corosync-totemnet.c
 * Dissector routines for the lowest level(encryption/decryption) protocol used in Corosync cluster engine
 * Copyright 2009 2010 2014 Masatake YAMATO <yamato@redhat.com>
 * Copyright (c) 2010 2014 Red Hat, Inc.
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include "config.h"

#include <epan/packet.h>
#include <epan/prefs.h>
#include <wsutil/wsgcrypt.h>
#include <wsutil/sober128.h>

static dissector_handle_t corosync_totemsrp_handle;

/* This dissector deals packets defined in totemnet.c of corosync
   cluster engine. In the totemnet.c the packet is encrypted and decrypted
   with LibTomCrypt. This dissector tries decrypting the packet with
   sober128 and sha1 functions in wireshark. */

/*
 * Dissector body
 */

#define PORT_COROSYNC_TOTEMNET_RANGE "5404-5405" /* Not IANA registered */

/* Forward declaration we need below */
void proto_register_corosync_totemnet(void);
void proto_reg_handoff_corosync_totemnet(void);

/* Initialize the protocol and registered fields */
static int proto_corosync_totemnet = -1;

/* field of struct security_header */
static int hf_corosync_totemnet_security_header_hash_digest    = -1;
static int hf_corosync_totemnet_security_header_salt           = -1;
static int hf_corosync_totemnet_security_crypto_type           = -1;
static int hf_corosync_totemnet_security_crypto_key            = -1;

/* configurable parameters */
static gchar*  corosync_totemnet_private_keys      = NULL;
static gchar** corosync_totemnet_private_keys_list = NULL;

/* Initialize the subtree pointers */
static gint ett_corosync_totemnet_security_header              = -1;

#define SALT_SIZE          16

#define TOTEM_CRYPTO_SOBER 0
#define TOTEM_CRYPTO_NSS   1

static const value_string corosync_totemnet_crypto_type[] = {
  { TOTEM_CRYPTO_SOBER, "SOBER" },
  { TOTEM_CRYPTO_NSS,   "NSS"   },
  { 0, NULL }
};


static int
dissect_corosync_totemnet_security_header(tvbuff_t *tvb,
                                          packet_info *pinfo, proto_tree *parent_tree,
                                          gboolean check_crypt_type,
                                          const gchar* key)
{
  proto_item *item;
  proto_tree *tree;

  col_set_str(pinfo->cinfo, COL_PROTOCOL, "COROSYNC/TOTEMNET");
  col_clear(pinfo->cinfo, COL_INFO);

  if (parent_tree)
    {
      item = proto_tree_add_item(parent_tree, proto_corosync_totemnet, tvb, 0,
                                 -1, ENC_NA);
      tree = proto_item_add_subtree(item, ett_corosync_totemnet_security_header);

      proto_tree_add_item(tree,
                          hf_corosync_totemnet_security_header_hash_digest,
                          tvb, 0, HASH_SHA1_LENGTH, ENC_NA);
      proto_tree_add_item(tree,
                          hf_corosync_totemnet_security_header_salt,
                          tvb, HASH_SHA1_LENGTH, SALT_SIZE, ENC_NA);

      if (check_crypt_type)
        {
          int io_len = tvb_reported_length(tvb);
          proto_item * key_item;

          proto_tree_add_item(tree,
                              hf_corosync_totemnet_security_crypto_type,
                              tvb, io_len - 1, 1, ENC_BIG_ENDIAN);
          key_item = proto_tree_add_string(tree,
                                           hf_corosync_totemnet_security_crypto_key,
                                           tvb, 0, 0, key);
          PROTO_ITEM_SET_GENERATED(key_item);
        }
    }
  return HASH_SHA1_LENGTH + SALT_SIZE;
}

/* About totemnet.c of corosync cluster engine:
 *
 * dissect_corosynec_totemnet_with_decryption() is derived from
 * totemnet.c in corosync which is licensed under 3-clause BSD license.
 * However, to merge this dissector to wireshark official source tree,
 * corosync developers permit EXPLICITLY to reuse totemnet.c in GPL.
 *
 http://permalink.gmane.org/gmane.linux.redhat.cluster/19087
 ------------------------------------------------------------
  Steven Dake | 4 Jan 2011 22:02
  Re: [Openais] packet dissectors for totempg, cman, clvmd, rgmanager, cpg,

On 12/14/2010 08:04 AM, Masatake YAMATO wrote:
> Thank you for replying.
>
>> Masatake,
>>
>> Masatake YAMATO napsal(a):
>>> I'd like to your advice more detail seriously.
>>> I've been developing this code for three years.
>>> I don't want to make this code garbage.
>>>
>>>> Masatake,
>>>> I'm pretty sure that biggest problem of your code was that it was
>>>> licensed under BSD (three clause, same as Corosync has)
>>>> license. Wireshark is licensed under GPL and even I like BSD licenses
>>>> much more, I would recommend you to try to relicense code under GPL
>>>> and send them this code.
>>>>
>>>> Regards,
>>>>   Honza
>>> I got the similar comment from wireshark developer.
>>> Please, read the discussion:
>>>     https://bugs.wireshark.org/bugzilla/show_bug.cgi?id=3232
>>>
>>
>> I've read that thread long time before I've sent previous mail, so
>> thats reason why I think that Wireshark developers just feel MUCH more
>> comfortable with GPL and thats reason why they just ignoring it.
>
> I see.
>
>>> In my understanding there is no legal problem in putting 3-clause BSD
>>> code into GPL code.  Acutally wireshark includes some 3-clause BSD
>>> code:
>>>
nnn>>
>> Actually there is really not. BSD to GPL works without problem, but
>> many people just don't know it...
>
> ...it is too bad. I strongly believe FOSS developers should know the
> intent behind of the both licenses.
>
>>> epan/dissectors/packet-radiotap-defs.h:
>>> *//*-
>>>  * Copyright (c) 2003, 2004 David Young.  All rights reserved.
>>>  *
...
>>>  *
>>>  * Redistribution and use in source and binary forms, with or without
>>>  * modification, are permitted provided that the following conditions
>>>  * are met:
>>>  * 1. Redistributions of source code must retain the above copyright
>>>  *    notice, this list of conditions and the following disclaimer.
>>>  * 2. Redistributions in binary form must reproduce the above copyright
>>>  *    notice, this list of conditions and the following disclaimer in the
>>>  *    documentation and/or other materials provided with the distribution.
>>>  * 3. The name of David Young may not be used to endorse or promote
>>>  *    products derived from this software without specific prior
>>>  *    written permission.
>>>  *
>>>  * THIS SOFTWARE IS PROVIDED BY DAVID YOUNG ``AS IS'' AND ANY
>>>  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
>>>  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
>>>  * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL DAVID
>>>  * YOUNG BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
>>>  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
>>>  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
>>>  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
>>>  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
>>>  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
>>>  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
>>>  * OF SUCH DAMAGE.
>>>  *//*
>>> I'd like to separate the legal issue and preference. I think I
>>> understand the importance of preference of upstream
>>> developers. However, I'd like to clear the legal issue first.
>>>
>>
>> Legally it's ok. But as you said, developers preference are
>> different. And because you are trying to change THEIR code it's
>> sometimes better to play they rules.
>
> I see.
>
>>> I can image there are people who prefer to GPL as the license covering
>>> their software. But here I've taken some corosync code in my
>>> dissector. It is essential part of my dissector. And corosync is
>>
>> ^^^ This may be problem. Question is how big is that part and if it
>> can be possible to make exception there. Can you point that code?
>>
>> Steve, we were able to relicense HUGE portion of code in case of
>> libqb, are we able to make the same for Wireshark dissector?
>
> Could you see https://github.com/masatake/wireshark-plugin-rhcs/blob/master/src/packet-corosync-totemnet.c#L156
> I refer totemnet.c to write dissect_corosynec_totemnet_with_decryption() function.
>
>>> licensed in 3-clause BSD, as you know. I'd like to change the license
>>> to merge my code to upstream project. I cannot do it in this context.
>>> See https://bugs.wireshark.org/bugzilla/show_bug.cgi?id=3232#c13
>>> Thank you.
>>
>> Regards,
>>   Honza
>
> Masatake YAMATO

Masatake,

Red Hat is the author of the totemnet file and can provide that code
under GPL if you like.  We cannot modify the license for libtomcrypt as
we are not the authors.  Feel free to change the license for that
particular code you rewrote in the link

> Could you see
https://github.com/masatake/wireshark-plugin-rhcs/blob/master/src/packet-corosync-totemnet.c#L156

under a GPL license if it helps move things along.

Regards
-steveu
       */

static int
dissect_corosynec_totemnet_with_decryption(tvbuff_t *tvb,
                                           packet_info *pinfo, proto_tree *parent_tree,
                                           gboolean check_crypt_type,
                                           const gchar* key_for_trial)
{
  unsigned char  keys[48];
  sober128_prng  keygen_prng_state;
  sober128_prng  stream_prng_state;
  unsigned char *hmac_key       = &keys[32];
  unsigned char *cipher_key     = &keys[16];
  unsigned char *initial_vector = &keys[0];
  unsigned char  digest_comparison[HASH_SHA1_LENGTH];

  int            io_len;
  guint8        *io_base;

#define PRIVATE_KEY_LEN_MAX 256
  gchar          private_key[PRIVATE_KEY_LEN_MAX];
  gsize          private_key_len;
  unsigned char* hash_digest;
  unsigned char* salt;

  io_len = tvb_reported_length(tvb) - (check_crypt_type? 1: 0);
  if (io_len < HASH_SHA1_LENGTH + SALT_SIZE) {
    return 0;
  }

  io_base = (guint8 *)tvb_memdup(pinfo->pool, tvb, 0, io_len + (check_crypt_type? 1: 0));
  if (check_crypt_type &&
      ( io_base[io_len] != TOTEM_CRYPTO_SOBER )) {
    return 0;
  }

  hash_digest = io_base;
  salt        = io_base + HASH_SHA1_LENGTH;


  memset(private_key, 0, sizeof(private_key));

  private_key_len = (strlen(key_for_trial)+4) & 0xFC;
  g_strlcpy(private_key, key_for_trial, private_key_len);

  /*
   * Generate MAC, CIPHER, IV keys from private key
   */
  memset (keys, 0, sizeof(keys));
  sober128_start (&keygen_prng_state);
  sober128_add_entropy(private_key,
                                  (unsigned long)private_key_len, &keygen_prng_state);
  sober128_add_entropy (salt, SALT_SIZE, &keygen_prng_state);
  sober128_read (keys, sizeof (keys), &keygen_prng_state);

  /*
   * Setup stream cipher
   */
  sober128_start (&stream_prng_state);
  sober128_add_entropy (cipher_key, 16, &stream_prng_state);
  sober128_add_entropy (initial_vector, 16, &stream_prng_state);

  /*
   * Authenticate contents of message
   */
  if (ws_hmac_buffer(GCRY_MD_SHA1, digest_comparison, io_base + HASH_SHA1_LENGTH, io_len - HASH_SHA1_LENGTH, hmac_key, 16)) {
    return 0;
  }

  if (memcmp (digest_comparison, hash_digest, HASH_SHA1_LENGTH) != 0)
      return 0;

  /*
   * Decrypt the contents of the message with the cipher key
   */

  sober128_read (io_base + HASH_SHA1_LENGTH + SALT_SIZE,
                            io_len - (HASH_SHA1_LENGTH + SALT_SIZE),
                            &stream_prng_state);


  /*
   * Dissect the decrypted data
   */
  {
    tvbuff_t *decrypted_tvb;
    tvbuff_t *next_tvb;


    decrypted_tvb = tvb_new_real_data(io_base, io_len, io_len);

    tvb_set_child_real_data_tvbuff(tvb, decrypted_tvb);
    add_new_data_source(pinfo, decrypted_tvb, "Decrypted Data");


    dissect_corosync_totemnet_security_header(decrypted_tvb, pinfo, parent_tree,
                                              check_crypt_type, key_for_trial);

    next_tvb = tvb_new_subset_length_caplen(decrypted_tvb,
                              HASH_SHA1_LENGTH + SALT_SIZE,
                              io_len - (HASH_SHA1_LENGTH + SALT_SIZE),
                              io_len - (HASH_SHA1_LENGTH + SALT_SIZE));

    return call_dissector(corosync_totemsrp_handle, next_tvb, pinfo, parent_tree) + HASH_SHA1_LENGTH + SALT_SIZE;
  }
}

static int
dissect_corosynec_totemnet(tvbuff_t *tvb,
                           packet_info *pinfo, proto_tree *parent_tree,
                           void *data _U_)
{
  if (corosync_totemnet_private_keys_list)
    {
      static int last_key_index = -1;
      int key_index;

      static int last_check_crypt_type_index;
      int check_crypt_type_index = -1;
      gboolean check_crypt_type_list[] = {FALSE, TRUE};


      if (last_key_index != -1)
        {
          int r;

          r = dissect_corosynec_totemnet_with_decryption(tvb,
                                                         pinfo,
                                                         parent_tree,
                                                         check_crypt_type_list[last_check_crypt_type_index],
                                                         corosync_totemnet_private_keys_list[last_key_index]);
          if (r > 0)
            return r;
          else
            last_key_index = -1;
        }

      for (key_index = 0;
           corosync_totemnet_private_keys_list[key_index];
           key_index++)
        {
          for (check_crypt_type_index = 0;
               check_crypt_type_index < 2;
               check_crypt_type_index++)
            {
              int r;

              r = dissect_corosynec_totemnet_with_decryption(tvb,
                                                             pinfo,
                                                             parent_tree,
                                                             check_crypt_type_list[check_crypt_type_index],
                                                             corosync_totemnet_private_keys_list[key_index]);
              if (r > 0)
                {
                  last_key_index = key_index;
                  last_check_crypt_type_index = check_crypt_type_index;
                  return r;
                }
              else if (r < 0)
                break;

            }
        }
    }

  /* Not encrypted */
  return call_dissector(corosync_totemsrp_handle, tvb, pinfo, parent_tree);
}

static void
corosync_totemnet_shutdown(void)
{
  g_strfreev(corosync_totemnet_private_keys_list);
}

void
proto_register_corosync_totemnet(void)
{
  module_t *corosync_totemnet_module;

  static hf_register_info hf[] = {
    { &hf_corosync_totemnet_security_header_hash_digest,
      { "Hash digest", "corosync_totemnet.security_header_hash_digest",
        FT_BYTES, BASE_NONE, NULL, 0x0,
        NULL, HFILL }},
    { &hf_corosync_totemnet_security_header_salt,
      { "Salt", "corosync_totemnet.security_header_salt",
        FT_BYTES, BASE_NONE, NULL, 0x0,
        NULL, HFILL }},
    { &hf_corosync_totemnet_security_crypto_type,
      { "Cryptographic Type", "corosync_totemnet.security_crypto_type",
        FT_UINT8, BASE_DEC, VALS(corosync_totemnet_crypto_type), 0x0,
        NULL, HFILL }},
    { &hf_corosync_totemnet_security_crypto_key,
      { "Private Key for decryption", "corosync_totemnet.security_crypto_key",
        FT_STRING, BASE_NONE, NULL, 0x0, NULL, HFILL }},
  };

  static gint *ett_corosync_totemnet[] = {
    &ett_corosync_totemnet_security_header,
  };

  proto_corosync_totemnet = proto_register_protocol("Totemnet Layer of Corosync Cluster Engine",
                                                    "COROSYNC/TOTEMNET", "corosync_totemnet");
  proto_register_field_array(proto_corosync_totemnet, hf, array_length(hf));
  proto_register_subtree_array(ett_corosync_totemnet, array_length(ett_corosync_totemnet));

  corosync_totemnet_module = prefs_register_protocol(proto_corosync_totemnet,
                                                     proto_reg_handoff_corosync_totemnet);

  prefs_register_string_preference(corosync_totemnet_module, "private_keys", "Private keys",
                                   "Semicolon-separated  list of keys for decryption(e.g. key1;key2;..." ,
                                   (const gchar **)&corosync_totemnet_private_keys);

  register_shutdown_routine(corosync_totemnet_shutdown);
}

void
proto_reg_handoff_corosync_totemnet(void)
{
  static gboolean initialized = FALSE;
  static dissector_handle_t corosync_totemnet_handle;


  if (!initialized)
  {
    corosync_totemnet_handle = create_dissector_handle(dissect_corosynec_totemnet, proto_corosync_totemnet);
    corosync_totemsrp_handle = find_dissector_add_dependency("corosync_totemsrp", proto_corosync_totemnet);

    dissector_add_uint_range_with_preference("udp.port", PORT_COROSYNC_TOTEMNET_RANGE, corosync_totemnet_handle);
    initialized = TRUE;
  }

  g_strfreev(corosync_totemnet_private_keys_list);
  corosync_totemnet_private_keys_list = g_strsplit(corosync_totemnet_private_keys, ";", 0);
}

/*
 * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
 *
 * Local variables:
 * c-basic-offset: 4
 * tab-width: 8
 * indent-tabs-mode: nil
 * End:
 *
 * vi: set shiftwidth=4 tabstop=8 expandtab:
 * :indentSize=4:tabSize=8:noTabs=true:
 */