2 * Routines for RFC5652 Cryptographic Message Syntax packet dissection
8 * Wireshark - Network traffic analyzer
9 * By Gerald Combs <gerald@wireshark.org>
10 * Copyright 1998 Gerald Combs
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.
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.
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.
32 #include <epan/packet.h>
33 #include <epan/oids.h>
34 #include <epan/asn1.h>
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"
46 #include <epan/crypt/sha1.h>
47 #include <epan/crypt/md5.h>
49 #define PNAME "Cryptographic Message Syntax"
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"
58 /* Initialize the subtree pointers */
59 #include "packet-cms-ett.c"
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 */
64 static const char *object_identifier_id;
65 static tvbuff_t *content_tvb = NULL;
67 static proto_tree *top_tree=NULL;
68 static proto_tree *cap_tree=NULL;
70 #define HASH_SHA1 "1.3.14.3.2.26"
71 #define SHA1_BUFFER_SIZE 20
73 #define HASH_MD5 "1.2.840.113549.2.5"
74 #define MD5_BUFFER_SIZE 16
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
83 unsigned char digest_buf[MAX(SHA1_BUFFER_SIZE, MD5_BUFFER_SIZE)];
86 cms_verify_msg_digest(proto_item *pi, tvbuff_t *content, const char *alg, tvbuff_t *tvb, int offset)
88 sha1_context sha1_ctx;
90 int i= 0, buffer_size = 0;
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 */
95 if(strcmp(alg, HASH_SHA1) == 0) {
97 sha1_starts(&sha1_ctx);
99 sha1_update(&sha1_ctx, tvb_get_ptr(content, 0, tvb_length(content)),
100 tvb_length(content));
102 sha1_finish(&sha1_ctx, digest_buf);
104 buffer_size = SHA1_BUFFER_SIZE;
106 } else if(strcmp(alg, HASH_MD5) == 0) {
110 md5_append(&md5_ctx, tvb_get_ptr(content, 0, tvb_length(content)),
111 tvb_length(content));
113 md5_finish(&md5_ctx, digest_buf);
115 buffer_size = MD5_BUFFER_SIZE;
119 /* compare our computed hash with what we have received */
121 if(tvb_bytes_exist(tvb, offset, buffer_size) &&
122 (tvb_memeql(tvb, offset, 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]);
127 proto_item_append_text(pi, "]");
130 proto_item_append_text(pi, " [correct]");
132 proto_item_append_text(pi, " [unable to verify]");
137 #include "packet-cms-fn.c"
139 /*--- proto_register_cms ----------------------------------------------*/
140 void proto_register_cms(void) {
143 static hf_register_info hf[] = {
144 { &hf_cms_ci_contentType,
145 { "contentType", "cms.contentInfo.contentType",
146 FT_OID, BASE_NONE, NULL, 0,
148 #include "packet-cms-hfarr.c"
151 /* List of subtrees */
152 static gint *ett[] = {
153 #include "packet-cms-ettarr.c"
156 /* Register protocol */
157 proto_cms = proto_register_protocol(PNAME, PSNAME, PFNAME);
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));
163 register_ber_syntax_dissector("ContentInfo", proto_cms, dissect_ContentInfo_PDU);
164 register_ber_syntax_dissector("SignedData", proto_cms, dissect_SignedData_PDU);
165 register_ber_oid_syntax(".p7s", NULL, "ContentInfo");
166 register_ber_oid_syntax(".p7m", NULL, "ContentInfo");
167 register_ber_oid_syntax(".p7c", NULL, "ContentInfo");
173 /*--- proto_reg_handoff_cms -------------------------------------------*/
174 void proto_reg_handoff_cms(void) {
175 #include "packet-cms-dis-tab.c"
177 oid_add_from_string("id-data","1.2.840.113549.1.7.1");
178 oid_add_from_string("id-alg-des-ede3-cbc","1.2.840.113549.3.7");
179 oid_add_from_string("id-alg-des-cbc","1.3.14.3.2.7");