Make the third argument to a UAT copy callback a size_t (not that any of
[obnox/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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
25  */
26
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <glib.h>
32 #include <string.h>
33
34 #include <epan/packet.h>
35 #include <epan/asn1.h>
36 #include <epan/prefs.h>
37 #include <epan/uat.h>
38
39 #include "packet-ber.h"
40 #include "packet-ess.h"
41 #include "packet-cms.h"
42 #include "packet-x509ce.h"
43 #include "packet-x509af.h"
44
45 #define PNAME  "Extended Security Services"
46 #define PSNAME "ESS"
47 #define PFNAME "ess"
48
49 typedef struct _ess_category_attributes_t {
50    char *oid;
51    guint lacv;
52    char *name;
53 } ess_category_attributes_t;
54
55 static ess_category_attributes_t *ess_category_attributes;
56 static guint num_ess_category_attributes;
57
58 /* Initialize the protocol and registered fields */
59 static int proto_ess = -1;
60 static int hf_ess_SecurityCategory_type_OID = -1;
61 static int hf_ess_Category_attribute = -1;
62
63 static gint ett_Category_attributes = -1;
64
65 #include "packet-ess-hf.c"
66
67 #include "packet-ess-val.h"
68
69 /* Initialize the subtree pointers */
70 #include "packet-ess-ett.c"
71
72 static const char *object_identifier_id;
73
74 UAT_CSTRING_CB_DEF(ess_category_attributes, oid, ess_category_attributes_t);
75 UAT_DEC_CB_DEF(ess_category_attributes, lacv, ess_category_attributes_t);
76 UAT_CSTRING_CB_DEF(ess_category_attributes, name, ess_category_attributes_t);
77
78 static void *
79 ess_copy_cb(void *dest, const void *orig, size_t len _U_)
80 {
81   ess_category_attributes_t *u = dest;
82   const ess_category_attributes_t *o = orig;
83
84   u->oid  = g_strdup(o->oid);
85   u->lacv = o->lacv;
86   u->name = g_strdup(o->name);
87
88   return dest;
89 }
90
91 static void
92 ess_free_cb(void *r)
93 {
94   ess_category_attributes_t *u = r;
95
96   g_free(u->oid);
97   g_free(u->name);
98 }
99
100 static void
101 ess_dissect_attribute (guint32 value, asn1_ctx_t *actx)
102 {
103   guint i;
104    
105   for (i = 0; i < num_ess_category_attributes; i++) {
106     ess_category_attributes_t *u = &(ess_category_attributes[i]);
107
108     if ((strcmp (u->oid, object_identifier_id) == 0) &&
109         (u->lacv == value))
110     {
111        proto_item_append_text (actx->created_item, " (%s)", u->name);
112        break;
113     }
114   }
115 }
116
117 static void
118 ess_dissect_attribute_flags (tvbuff_t *tvb, asn1_ctx_t *actx)
119 {
120   proto_tree *tree;
121   guint8 *value;
122   guint i;
123    
124   tree = proto_item_add_subtree (actx->created_item, ett_Category_attributes);
125   value = tvb_get_ephemeral_string (tvb, 0, tvb_length (tvb));
126   
127   for (i = 0; i < num_ess_category_attributes; i++) {
128     ess_category_attributes_t *u = &(ess_category_attributes[i]);
129
130     if ((strcmp (u->oid, object_identifier_id) == 0) &&
131         ((u->lacv / 8) < tvb_length (tvb)) &&
132         (value[u->lacv / 8] & (1 << (7 - (u->lacv % 8)))))
133     {
134        proto_tree_add_string_format (tree, hf_ess_Category_attribute, tvb,
135                                      u->lacv / 8, 1, u->name,
136                                      "%s (%d)", u->name, u->lacv);
137     }
138   }
139 }
140
141 #include "packet-ess-fn.c"
142
143 /*--- proto_register_ess ----------------------------------------------*/
144 void proto_register_ess(void) {
145
146   /* List of fields */
147   static hf_register_info hf[] = {
148     { &hf_ess_SecurityCategory_type_OID, 
149       { "type", "ess.type_OID", FT_STRING, BASE_NONE, NULL, 0,
150         "Type of Security Category", HFILL }},
151     { &hf_ess_Category_attribute, 
152       { "Attribute", "ess.attribute", FT_STRING, BASE_NONE, NULL, 0,
153         NULL, HFILL }},
154 #include "packet-ess-hfarr.c"
155   };
156
157   /* List of subtrees */
158   static gint *ett[] = {
159      &ett_Category_attributes,
160 #include "packet-ess-ettarr.c"
161   };
162   
163   static uat_field_t attributes_flds[] = {
164     UAT_FLD_CSTRING(ess_category_attributes,oid, "Tag Set", "Category Tag Set (Object Identifier)"),
165     UAT_FLD_DEC(ess_category_attributes,lacv, "Value", "Label And Cert Value"),
166     UAT_FLD_CSTRING(ess_category_attributes,name, "Name", "Category Name"),
167     UAT_END_FIELDS
168   };
169
170   uat_t *attributes_uat = uat_new("ESS Category Attributes",
171                                   sizeof(ess_category_attributes_t),
172                                   "ess_category_attributes",
173                                   TRUE,
174                                   (void*) &ess_category_attributes,
175                                   &num_ess_category_attributes,
176                                   UAT_CAT_PORTS,
177                                   "ChEssCategoryAttributes",
178                                   ess_copy_cb,
179                                   NULL,
180                                   ess_free_cb,
181                                   NULL,
182                                   attributes_flds);
183
184   static module_t *ess_module;
185
186   /* Register protocol */
187   proto_ess = proto_register_protocol(PNAME, PSNAME, PFNAME);
188
189   /* Register fields and subtrees */
190   proto_register_field_array(proto_ess, hf, array_length(hf));
191   proto_register_subtree_array(ett, array_length(ett));
192   
193   ess_module = prefs_register_protocol(proto_ess, NULL);
194
195   prefs_register_uat_preference(ess_module, "attributes_table",
196                                 "ESS Category Attributes",
197                                 "ESS category attributes translation table",
198                                 attributes_uat);
199
200 }
201
202
203 /*--- proto_reg_handoff_ess -------------------------------------------*/
204 void proto_reg_handoff_ess(void) {
205 #include "packet-ess-dis-tab.c"
206 }
207