get users of oid_resolv to use the new oids, rollout packet-snmp.c
[metze/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  * Wireshark - Network traffic analyzer
8  * By Gerald Combs <gerald@wireshark.org>
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 #include <epan/oids.h>
34 #include <epan/asn1.h>
35
36 #include <stdio.h>
37 #include <string.h>
38
39 #include "packet-ber.h"
40 #include "packet-cms.h"
41 #include "packet-x509af.h"
42 #include "packet-x509if.h"
43 #include "packet-pkcs12.h"
44
45 #include <epan/crypt/crypt-sha1.h>
46 #include <epan/crypt/crypt-md5.h>
47
48 #define PNAME  "Cryptographic Message Syntax"
49 #define PSNAME "CMS"
50 #define PFNAME "cms"
51
52 /* Initialize the protocol and registered fields */
53 int proto_cms = -1;
54 static int hf_cms_ci_contentType = -1;
55 #include "packet-cms-hf.c"
56
57 /* Initialize the subtree pointers */
58 #include "packet-cms-ett.c"
59
60 static int dissect_cms_OCTET_STRING(gboolean implicit_tag _U_, tvbuff_t *tvb, int offset, asn1_ctx_t *actx, proto_tree *tree, int hf_index _U_) ; /* XXX kill a compiler warning until asn2wrs stops generating these silly wrappers */
61
62
63 static const char *object_identifier_id;
64 static tvbuff_t *content_tvb = NULL;
65
66 static proto_tree *top_tree=NULL;
67 static proto_tree *cap_tree=NULL;
68
69 #define HASH_SHA1 "1.3.14.3.2.26"
70 #define SHA1_BUFFER_SIZE  20
71
72 #define HASH_MD5 "1.2.840.113549.2.5"
73 #define MD5_BUFFER_SIZE  16
74
75
76 /* SHA-2 variants */
77 #define HASH_SHA224 "2.16.840.1.101.3.4.2.4"
78 #define SHA224_BUFFER_SIZE  32 /* actually 28 */
79 #define HASH_SHA256 "2.16.840.1.101.3.4.2.1"
80 #define SHA256_BUFFER_SIZE  32
81
82 unsigned char digest_buf[MAX(SHA1_BUFFER_SIZE, MD5_BUFFER_SIZE)];
83
84 static void
85 cms_verify_msg_digest(proto_item *pi, tvbuff_t *content, const char *alg, tvbuff_t *tvb, int offset)
86 {
87   sha1_context sha1_ctx;
88   md5_state_t md5_ctx;
89   int i= 0, buffer_size = 0;
90
91   /* we only support two algorithms at the moment  - if we do add SHA2
92      we should add a registration process to use a registration process */
93
94   if(strcmp(alg, HASH_SHA1) == 0) {
95
96     sha1_starts(&sha1_ctx);
97
98     sha1_update(&sha1_ctx, tvb_get_ptr(content, 0, tvb_length(content)),
99                 tvb_length(content));
100
101     sha1_finish(&sha1_ctx, digest_buf);
102
103     buffer_size = SHA1_BUFFER_SIZE;
104
105   } else if(strcmp(alg, HASH_MD5) == 0) {
106
107     md5_init(&md5_ctx);
108
109     md5_append(&md5_ctx, tvb_get_ptr(content, 0, tvb_length(content)),
110                tvb_length(content));
111
112     md5_finish(&md5_ctx, digest_buf);
113
114     buffer_size = MD5_BUFFER_SIZE;
115   }
116
117   if(buffer_size) {
118     /* compare our computed hash with what we have received */
119
120     if(tvb_bytes_exist(tvb, offset, buffer_size) &&
121        (memcmp(tvb_get_ptr(tvb, offset, buffer_size), digest_buf, buffer_size) != 0)) {
122       proto_item_append_text(pi, " [incorrect, should be ");
123       for(i = 0; i < buffer_size; i++)
124         proto_item_append_text(pi, "%02X", digest_buf[i]);
125
126       proto_item_append_text(pi, "]");
127     }
128     else
129       proto_item_append_text(pi, " [correct]");
130   } else {
131     proto_item_append_text(pi, " [unable to verify]");
132   }
133
134 }
135
136 #include "packet-cms-fn.c"
137
138 /*--- proto_register_cms ----------------------------------------------*/
139 void proto_register_cms(void) {
140
141   /* List of fields */
142   static hf_register_info hf[] = {
143     { &hf_cms_ci_contentType,
144       { "contentType", "cms.contentInfo.contentType",
145         FT_OID, BASE_NONE, NULL, 0,
146         "ContentType", HFILL }},
147 #include "packet-cms-hfarr.c"
148   };
149
150   /* List of subtrees */
151   static gint *ett[] = {
152 #include "packet-cms-ettarr.c"
153   };
154
155   /* Register protocol */
156   proto_cms = proto_register_protocol(PNAME, PSNAME, PFNAME);
157
158   /* Register fields and subtrees */
159   proto_register_field_array(proto_cms, hf, array_length(hf));
160   proto_register_subtree_array(ett, array_length(ett));
161
162   register_ber_syntax_dissector("ContentInfo", proto_cms, dissect_ContentInfo_PDU); 
163   register_ber_oid_syntax(".p7s", NULL, "ContentInfo");
164   register_ber_oid_syntax(".p7m", NULL, "ContentInfo");
165   register_ber_oid_syntax(".p7c", NULL, "ContentInfo");
166
167
168 }
169
170
171 /*--- proto_reg_handoff_cms -------------------------------------------*/
172 void proto_reg_handoff_cms(void) {
173 #include "packet-cms-dis-tab.c"
174
175   add_oid_str_name("1.2.840.113549.1.7.1", "id-data");
176   add_oid_str_name("1.2.840.113549.3.7", "id-alg-des-ede3-cbc");
177   add_oid_str_name("1.3.14.3.2.7", "id-alg-des-cbc");
178
179 }
180