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