We always HAVE_CONFIG_H so don't bother checking whether we have it or not.
[metze/wireshark/wip.git] / asn1 / ess / packet-ess-template.c
1 /* packet-ess.c
2  * Routines for RFC5035 Extended Security Services packet dissection
3  *   Ronnie Sahlberg 2004
4  *   Stig Bjorlykke 2010
5  *
6  * $Id$
7  *
8  * Wireshark - Network traffic analyzer
9  * By Gerald Combs <gerald@wireshark.org>
10  * Copyright 1998 Gerald Combs
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License
14  * as published by the Free Software Foundation; either version 2
15  * of the License, or (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25  */
26
27 #include "config.h"
28
29 #include <glib.h>
30 #include <string.h>
31
32 #include <epan/packet.h>
33 #include <epan/asn1.h>
34 #include <epan/prefs.h>
35 #include <epan/uat.h>
36
37 #include "packet-ber.h"
38 #include "packet-ess.h"
39 #include "packet-cms.h"
40 #include "packet-x509ce.h"
41 #include "packet-x509af.h"
42
43 #define PNAME  "Extended Security Services"
44 #define PSNAME "ESS"
45 #define PFNAME "ess"
46
47 typedef struct _ess_category_attributes_t {
48    char *oid;
49    guint lacv;
50    char *name;
51 } ess_category_attributes_t;
52
53 static ess_category_attributes_t *ess_category_attributes;
54 static guint num_ess_category_attributes;
55
56 /* Initialize the protocol and registered fields */
57 static int proto_ess = -1;
58 static int hf_ess_SecurityCategory_type_OID = -1;
59 static int hf_ess_Category_attribute = -1;
60
61 static gint ett_Category_attributes = -1;
62
63 #include "packet-ess-hf.c"
64
65 #include "packet-ess-val.h"
66
67 /* Initialize the subtree pointers */
68 #include "packet-ess-ett.c"
69
70 static const char *object_identifier_id;
71
72 UAT_CSTRING_CB_DEF(ess_category_attributes, oid, ess_category_attributes_t);
73 UAT_DEC_CB_DEF(ess_category_attributes, lacv, ess_category_attributes_t);
74 UAT_CSTRING_CB_DEF(ess_category_attributes, name, ess_category_attributes_t);
75
76 static void *
77 ess_copy_cb(void *dest, const void *orig, size_t len _U_)
78 {
79   ess_category_attributes_t *u = dest;
80   const ess_category_attributes_t *o = orig;
81
82   u->oid  = g_strdup(o->oid);
83   u->lacv = o->lacv;
84   u->name = g_strdup(o->name);
85
86   return dest;
87 }
88
89 static void
90 ess_free_cb(void *r)
91 {
92   ess_category_attributes_t *u = r;
93
94   g_free(u->oid);
95   g_free(u->name);
96 }
97
98 static void
99 ess_dissect_attribute (guint32 value, asn1_ctx_t *actx)
100 {
101   guint i;
102    
103   for (i = 0; i < num_ess_category_attributes; i++) {
104     ess_category_attributes_t *u = &(ess_category_attributes[i]);
105
106     if ((strcmp (u->oid, object_identifier_id) == 0) &&
107         (u->lacv == value))
108     {
109        proto_item_append_text (actx->created_item, " (%s)", u->name);
110        break;
111     }
112   }
113 }
114
115 static void
116 ess_dissect_attribute_flags (tvbuff_t *tvb, asn1_ctx_t *actx)
117 {
118   proto_tree *tree;
119   guint8 *value;
120   guint i;
121    
122   tree = proto_item_add_subtree (actx->created_item, ett_Category_attributes);
123   value = tvb_get_ephemeral_string (tvb, 0, tvb_length (tvb));
124   
125   for (i = 0; i < num_ess_category_attributes; i++) {
126     ess_category_attributes_t *u = &(ess_category_attributes[i]);
127
128     if ((strcmp (u->oid, object_identifier_id) == 0) &&
129         ((u->lacv / 8) < tvb_length (tvb)) &&
130         (value[u->lacv / 8] & (1 << (7 - (u->lacv % 8)))))
131     {
132        proto_tree_add_string_format (tree, hf_ess_Category_attribute, tvb,
133                                      u->lacv / 8, 1, u->name,
134                                      "%s (%d)", u->name, u->lacv);
135     }
136   }
137 }
138
139 #include "packet-ess-fn.c"
140
141 /*--- proto_register_ess ----------------------------------------------*/
142 void proto_register_ess(void) {
143
144   /* List of fields */
145   static hf_register_info hf[] = {
146     { &hf_ess_SecurityCategory_type_OID, 
147       { "type", "ess.type_OID", FT_STRING, BASE_NONE, NULL, 0,
148         "Type of Security Category", HFILL }},
149     { &hf_ess_Category_attribute, 
150       { "Attribute", "ess.attribute", FT_STRING, BASE_NONE, NULL, 0,
151         NULL, HFILL }},
152 #include "packet-ess-hfarr.c"
153   };
154
155   /* List of subtrees */
156   static gint *ett[] = {
157      &ett_Category_attributes,
158 #include "packet-ess-ettarr.c"
159   };
160   
161   static uat_field_t attributes_flds[] = {
162     UAT_FLD_CSTRING(ess_category_attributes,oid, "Tag Set", "Category Tag Set (Object Identifier)"),
163     UAT_FLD_DEC(ess_category_attributes,lacv, "Value", "Label And Cert Value"),
164     UAT_FLD_CSTRING(ess_category_attributes,name, "Name", "Category Name"),
165     UAT_END_FIELDS
166   };
167
168   uat_t *attributes_uat = uat_new("ESS Category Attributes",
169                                   sizeof(ess_category_attributes_t),
170                                   "ess_category_attributes",
171                                   TRUE,
172                                   (void*) &ess_category_attributes,
173                                   &num_ess_category_attributes,
174                                   UAT_AFFECTS_DISSECTION, /* affects dissection of packets, but not set of named fields */
175                                   "ChEssCategoryAttributes",
176                                   ess_copy_cb,
177                                   NULL,
178                                   ess_free_cb,
179                                   NULL,
180                                   attributes_flds);
181
182   static module_t *ess_module;
183
184   /* Register protocol */
185   proto_ess = proto_register_protocol(PNAME, PSNAME, PFNAME);
186
187   /* Register fields and subtrees */
188   proto_register_field_array(proto_ess, hf, array_length(hf));
189   proto_register_subtree_array(ett, array_length(ett));
190   
191   ess_module = prefs_register_protocol(proto_ess, NULL);
192
193   prefs_register_uat_preference(ess_module, "attributes_table",
194                                 "ESS Category Attributes",
195                                 "ESS category attributes translation table",
196                                 attributes_uat);
197
198 }
199
200
201 /*--- proto_reg_handoff_ess -------------------------------------------*/
202 void proto_reg_handoff_ess(void) {
203 #include "packet-ess-dis-tab.c"
204 }
205