"Field Information" context menu item that will bring up a web page reference to...
[obnox/wireshark/wip.git] / asn1 / cms / packet-cms-template.c
1 /* packet-cms.c
2  * Routines for RFC2630 Cryptographic Message Syntax packet dissection
3  *   Ronnie Sahlberg 2004
4  *
5  * $Id$
6  *
7  * Ethereal - Network traffic analyzer
8  * By Gerald Combs <gerald@ethereal.com>
9  * Copyright 1998 Gerald Combs
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24  */
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include <glib.h>
31 #include <epan/packet.h>
32 #include <epan/conversation.h>
33
34 #include <stdio.h>
35 #include <string.h>
36
37 #include "packet-ber.h"
38 #include "packet-cms.h"
39 #include "packet-x509af.h"
40 #include "packet-x509if.h"
41
42 #include <epan/sha1.h>
43 #include <epan/crypt-md5.h>
44
45 #define PNAME  "Cryptographic Message Syntax"
46 #define PSNAME "CMS"
47 #define PFNAME "cms"
48
49 /* Initialize the protocol and registered fields */
50 int proto_cms = -1;
51 static int hf_cms_ci_contentType = -1;
52 #include "packet-cms-hf.c"
53
54 /* Initialize the subtree pointers */
55 #include "packet-cms-ett.c"
56
57 static int dissect_cms_OCTET_STRING(gboolean implicit_tag _U_, tvbuff_t *tvb, int offset, packet_info *pinfo _U_, proto_tree *tree, int hf_index _U_) ; /* XXX kill a compiler warning until asn2eth stops generating these silly wrappers */
58
59
60 static const char *object_identifier_id;
61 static tvbuff_t *content_tvb = NULL;
62
63 static proto_tree *top_tree=NULL;
64
65 #define HASH_SHA1 "1.3.14.3.2.26"
66 #define SHA1_BUFFER_SIZE  20
67
68 #define HASH_MD5 "1.2.840.113549.2.5"
69 #define MD5_BUFFER_SIZE  16
70
71
72 /* SHA-2 variants */
73 #define HASH_SHA224 "2.16.840.1.101.3.4.2.4"
74 #define SHA224_BUFFER_SIZE  32 /* actually 28 */
75 #define HASH_SHA256 "2.16.840.1.101.3.4.2.1"
76 #define SHA256_BUFFER_SIZE  32
77
78 unsigned char digest_buf[MAX(SHA1_BUFFER_SIZE, MD5_BUFFER_SIZE)];
79
80 static void
81 cms_verify_msg_digest(proto_item *pi, tvbuff_t *content, const char *alg, tvbuff_t *tvb, int offset)
82 {
83   sha1_context sha1_ctx;
84   md5_state_t md5_ctx;
85   int i= 0, buffer_size = 0;
86
87   /* we only support two algorithms at the moment  - if we do add SHA2
88      we should add a registration process to use a registration process */
89
90   if(strcmp(alg, HASH_SHA1) == 0) {
91
92     sha1_starts(&sha1_ctx);
93
94     sha1_update(&sha1_ctx, tvb_get_ptr(content, 0, tvb_length(content)), 
95                 tvb_length(content));
96
97     sha1_finish(&sha1_ctx, digest_buf);
98
99     buffer_size = SHA1_BUFFER_SIZE;
100
101   } else if(strcmp(alg, HASH_MD5) == 0) {
102
103     md5_init(&md5_ctx);
104
105     md5_append(&md5_ctx, tvb_get_ptr(content, 0, tvb_length(content)), 
106                tvb_length(content));
107     
108     md5_finish(&md5_ctx, digest_buf);
109
110     buffer_size = MD5_BUFFER_SIZE;
111   }
112
113   if(buffer_size) {
114     /* compare our computed hash with what we have received */  
115
116     if(tvb_bytes_exist(tvb, offset, buffer_size) &&
117        (memcmp(tvb_get_ptr(tvb, offset, buffer_size), digest_buf, buffer_size) != 0)) { 
118       proto_item_append_text(pi, " [incorrect, should be ");
119       for(i = 0; i < buffer_size; i++)
120         proto_item_append_text(pi, "%02X", digest_buf[i]);
121
122       proto_item_append_text(pi, "]");
123     }
124     else
125       proto_item_append_text(pi, " [correct]");
126   } else {
127     proto_item_append_text(pi, " [unable to verify]");
128   }
129
130 }
131
132 #include "packet-cms-fn.c"
133
134 /*--- proto_register_cms ----------------------------------------------*/
135 void proto_register_cms(void) {
136
137   /* List of fields */
138   static hf_register_info hf[] = {
139     { &hf_cms_ci_contentType,
140       { "contentType", "cms.contentInfo.contentType",
141         FT_OID, BASE_NONE, NULL, 0,
142         "ContentType", HFILL }},
143 #include "packet-cms-hfarr.c"
144   };
145
146   /* List of subtrees */
147   static gint *ett[] = {
148 #include "packet-cms-ettarr.c"
149   };
150
151   /* Register protocol */
152   proto_cms = proto_register_protocol(PNAME, PSNAME, PFNAME);
153
154   /* Register fields and subtrees */
155   proto_register_field_array(proto_cms, hf, array_length(hf));
156   proto_register_subtree_array(ett, array_length(ett));
157
158 }
159
160
161 /*--- proto_reg_handoff_cms -------------------------------------------*/
162 void proto_reg_handoff_cms(void) {
163 #include "packet-cms-dis-tab.c"
164 }
165