Revert "TODO epan/dissectors/asn1/kerberos/packet-kerberos-template.c new GSS flags"
[wireshark-sm.git] / epan / dissectors / packet-ms-nns.c
blobc9b00d2fcad01274161db9f7a9ecc1e5061b81f8
1 /* packet-ms-nns.c
2 * Routines for .NET NegotiateStream Protocol (MS-NNS) dissection
3 * Copyright 2020, Uli Heilmeier <uh@heilmeier.eu>
5 * Wireshark - Network traffic analyzer
6 * By Gerald Combs <gerald@wieshark.org>
7 * Copyright 1998 Gerald Combs
9 * SPDX-License-Identifier: GPL-2.0-or-later
13 * Basic dissector for .NET NegotiateStream Protocol based on protocol reference found at
14 * https://winprotocoldoc.blob.core.windows.net/productionwindowsarchives/MS-NNS/%5bMS-NNS%5d.pdf
17 #include <config.h>
19 #include <epan/packet.h>
20 #include <epan/conversation.h>
22 /* Prototypes */
23 void proto_reg_handoff_nns(void);
24 void proto_register_nns(void);
26 static dissector_handle_t gssapi_handle;
29 #define MS_NNS_MESSAGE_HANDSHAKE_DONE 20
30 #define MS_NNS_MESSAGE_HANDSHAKE_ERROR 21
31 #define MS_NNS_MESSAGE_HANDSHAKE_PROGRESS 22
33 static const value_string nns_message_id_vals[] = {
34 { MS_NNS_MESSAGE_HANDSHAKE_DONE, "Handshake Done" },
35 { MS_NNS_MESSAGE_HANDSHAKE_ERROR, "Handshake Error" },
36 { MS_NNS_MESSAGE_HANDSHAKE_PROGRESS, "Handshake In Progress" },
37 { 0, NULL}
40 struct nns_session_state {
41 uint32_t handshake_done;
42 bool first_handshake_done;
45 static int proto_nns;
46 static int hf_nns_message_id;
47 static int hf_nns_major_version;
48 static int hf_nns_minor_version;
49 static int hf_nns_auth_payload_size;
50 static int hf_nns_auth_payload;
51 static int hf_nns_payload_size;
52 static int hf_nns_payload;
54 static int ett_nns;
55 static int ett_nns_payload;
57 #define MS_NNS_MIN_LENGTH 4
59 static int
60 dissect_nns(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data _U_)
62 proto_item *ti, *pti;
63 proto_tree *nns_tree, *payload_tree;
64 unsigned offset = 0;
65 uint32_t message_id;
66 uint32_t payload_size;
67 conversation_t *conversation;
68 tvbuff_t *nt_tvb;
69 int remaining;
70 struct nns_session_state *session_state;
72 if (tvb_reported_length(tvb) < MS_NNS_MIN_LENGTH)
73 return 0;
75 conversation = find_or_create_conversation(pinfo);
77 session_state = (struct nns_session_state *)conversation_get_proto_data(conversation, proto_nns);
78 if (!session_state) {
79 session_state = wmem_new0(wmem_file_scope(), struct nns_session_state);
80 conversation_add_proto_data(conversation, proto_nns, session_state);
83 col_set_str(pinfo->cinfo, COL_PROTOCOL, "MS-NNS");
84 col_clear(pinfo->cinfo, COL_INFO);
86 ti = proto_tree_add_item(tree, proto_nns, tvb, 0, -1, ENC_NA);
88 nns_tree = proto_item_add_subtree(ti, ett_nns);
90 /* As it is unknown if there is a one-way Handshake Done or a two-way Handshake Done we check the first frame
91 * after the first Handshake Done if it looks like a Handshake Done message (0x140100). */
93 if ( session_state->handshake_done && session_state->handshake_done < pinfo->num &&
94 !(session_state->first_handshake_done && tvb_get_ntoh24(tvb, offset) == 0x140100)) {
95 proto_tree_add_item_ret_uint(nns_tree, hf_nns_payload_size, tvb, offset, 4, ENC_LITTLE_ENDIAN, &payload_size);
96 offset += 4;
97 col_append_str(pinfo->cinfo, COL_INFO, "Data");
98 if ( payload_size > 0) {
99 remaining = tvb_reported_length_remaining(tvb, offset);
100 if ((uint32_t) remaining < payload_size) {
101 pinfo->desegment_offset = offset - 4;
102 pinfo->desegment_len = payload_size - remaining;
103 return (offset - 4);
105 proto_tree_add_item(nns_tree, hf_nns_payload, tvb, offset, payload_size, ENC_NA);
106 offset += payload_size;
107 session_state->first_handshake_done = false;
110 else {
111 proto_tree_add_item_ret_uint(nns_tree, hf_nns_message_id, tvb, offset, 1, ENC_NA, &message_id);
112 offset += 1;
113 col_append_sep_fstr(pinfo->cinfo, COL_INFO, ", ", "%s", val_to_str_const(message_id, nns_message_id_vals, "Unknown Record"));
114 proto_tree_add_item(nns_tree, hf_nns_major_version, tvb, offset, 1, ENC_NA);
115 offset += 1;
116 proto_tree_add_item(nns_tree, hf_nns_minor_version, tvb, offset, 1, ENC_NA);
117 offset += 1;
118 proto_tree_add_item_ret_uint(nns_tree, hf_nns_auth_payload_size, tvb, offset, 2, ENC_BIG_ENDIAN, &payload_size);
119 offset += 2;
120 if ( payload_size > 0) {
121 if ((uint32_t) tvb_reported_length_remaining(tvb, offset) < payload_size) {
122 pinfo->desegment_offset = offset - 5;
123 pinfo->desegment_len = payload_size;
124 return (offset - 5);
126 pti = proto_tree_add_item(nns_tree, hf_nns_auth_payload, tvb, offset, payload_size, ENC_NA);
127 if (message_id == MS_NNS_MESSAGE_HANDSHAKE_DONE || message_id == MS_NNS_MESSAGE_HANDSHAKE_PROGRESS) {
128 nt_tvb = tvb_new_subset_length(tvb, offset, payload_size);
129 payload_tree = proto_item_add_subtree(pti, ett_nns_payload);
130 call_dissector(gssapi_handle, nt_tvb, pinfo, payload_tree);
132 offset += payload_size;
134 if ( message_id == MS_NNS_MESSAGE_HANDSHAKE_DONE) {
135 session_state->handshake_done = pinfo->num;
136 session_state->first_handshake_done = (session_state->first_handshake_done ? false : true);
139 return offset;
142 void proto_register_nns(void)
144 static hf_register_info hf[] = {
145 { &hf_nns_message_id,
146 { "MessageID", "ms-nns.message_id",
147 FT_UINT8, BASE_DEC, VALS(nns_message_id_vals), 0x0,
148 NULL, HFILL }
150 { &hf_nns_major_version,
151 { "Major Version", "ms-nns.major_version",
152 FT_UINT8, BASE_DEC, NULL, 0x0,
153 NULL, HFILL }
155 { &hf_nns_minor_version,
156 { "Minor Version", "ms-nns.minor_version",
157 FT_UINT8, BASE_DEC, NULL, 0x0,
158 NULL, HFILL }
160 { &hf_nns_auth_payload_size,
161 { "Auth Payload Size", "ms-nns.auth_payload_size",
162 FT_UINT16, BASE_DEC, NULL, 0x0,
163 NULL, HFILL }
165 { &hf_nns_auth_payload,
166 { "Auth Payload", "ms-nns.known_encoding",
167 FT_BYTES, BASE_NONE, NULL, 0x0,
168 NULL, HFILL }
170 { &hf_nns_payload_size,
171 { "Payload Size", "ms-nns.payload_size",
172 FT_UINT32, BASE_DEC, NULL, 0x0,
173 NULL, HFILL }
175 { &hf_nns_payload,
176 { "Payload", "ms-nns.payload",
177 FT_BYTES, BASE_NONE, NULL, 0x0,
178 NULL, HFILL }
182 static int *ett[] = {
183 &ett_nns,
184 &ett_nns_payload
187 proto_nns = proto_register_protocol(".NET NegotiateStream Protocol", "MS-NNS", "ms-nns");
188 proto_register_field_array(proto_nns, hf, array_length(hf));
189 proto_register_subtree_array(ett, array_length(ett));
190 register_dissector("ms-nns", dissect_nns, proto_nns);
193 void proto_reg_handoff_nns(void)
195 gssapi_handle = find_dissector("gssapi");
199 * Editor modelines - https://www.wireshark.org/tools/modelines.html
201 * Local variables:
202 * c-basic-offset: 4
203 * tab-width: 8
204 * indent-tabs-mode: nil
205 * End:
207 * vi: set shiftwidth=4 tabstop=8 expandtab:
208 * :indentSize=4:tabSize=8:noTabs=true: