MSWSP: use GuidPropertySet_find_guid() in parse_CFullPropSpec()
[wireshark-wip.git] / asn1 / cms / packet-cms-template.c
blobe4e93d12ea3860cf0184fe6f29ef66fce77ba119
1 /* packet-cms.c
2 * Routines for RFC5652 Cryptographic Message Syntax packet dissection
3 * Ronnie Sahlberg 2004
4 * Stig Bjorlykke 2010
6 * $Id$
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.
27 #include "config.h"
29 #include <glib.h>
31 #include <wsutil/sha1.h>
32 #include <wsutil/md5.h>
34 #include <epan/packet.h>
35 #include <epan/oids.h>
36 #include <epan/asn1.h>
38 #include <string.h>
40 #include "packet-ber.h"
41 #include "packet-cms.h"
42 #include "packet-x509af.h"
43 #include "packet-x509ce.h"
44 #include "packet-x509if.h"
45 #include "packet-x509sat.h"
46 #include "packet-pkcs12.h"
48 #define PNAME "Cryptographic Message Syntax"
49 #define PSNAME "CMS"
50 #define PFNAME "cms"
52 /* Initialize the protocol and registered fields */
53 static int proto_cms = -1;
54 static int hf_cms_ci_contentType = -1;
55 #include "packet-cms-hf.c"
57 /* Initialize the subtree pointers */
58 #include "packet-cms-ett.c"
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 */
63 static const char *object_identifier_id;
64 static tvbuff_t *content_tvb = NULL;
66 static proto_tree *top_tree=NULL;
67 static proto_tree *cap_tree=NULL;
69 #define HASH_SHA1 "1.3.14.3.2.26"
70 #define SHA1_BUFFER_SIZE 20
72 #define HASH_MD5 "1.2.840.113549.2.5"
73 #define MD5_BUFFER_SIZE 16
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
82 unsigned char digest_buf[MAX(SHA1_BUFFER_SIZE, MD5_BUFFER_SIZE)];
84 static void
85 cms_verify_msg_digest(proto_item *pi, tvbuff_t *content, const char *alg, tvbuff_t *tvb, int offset)
87 sha1_context sha1_ctx;
88 md5_state_t md5_ctx;
89 int i= 0, buffer_size = 0;
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 */
94 if(strcmp(alg, HASH_SHA1) == 0) {
96 sha1_starts(&sha1_ctx);
98 sha1_update(&sha1_ctx, tvb_get_ptr(content, 0, tvb_length(content)),
99 tvb_length(content));
101 sha1_finish(&sha1_ctx, digest_buf);
103 buffer_size = SHA1_BUFFER_SIZE;
105 } else if(strcmp(alg, HASH_MD5) == 0) {
107 md5_init(&md5_ctx);
109 md5_append(&md5_ctx, tvb_get_ptr(content, 0, tvb_length(content)),
110 tvb_length(content));
112 md5_finish(&md5_ctx, digest_buf);
114 buffer_size = MD5_BUFFER_SIZE;
117 if(buffer_size) {
118 /* compare our computed hash with what we have received */
120 if(tvb_bytes_exist(tvb, offset, buffer_size) &&
121 (tvb_memeql(tvb, offset, 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]);
126 proto_item_append_text(pi, "]");
128 else
129 proto_item_append_text(pi, " [correct]");
130 } else {
131 proto_item_append_text(pi, " [unable to verify]");
136 #include "packet-cms-fn.c"
138 /*--- proto_register_cms ----------------------------------------------*/
139 void proto_register_cms(void) {
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 NULL, HFILL }},
147 #include "packet-cms-hfarr.c"
150 /* List of subtrees */
151 static gint *ett[] = {
152 #include "packet-cms-ettarr.c"
155 /* Register protocol */
156 proto_cms = proto_register_protocol(PNAME, PSNAME, PFNAME);
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));
162 register_ber_syntax_dissector("ContentInfo", proto_cms, dissect_ContentInfo_PDU);
163 register_ber_syntax_dissector("SignedData", proto_cms, dissect_SignedData_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");
172 /*--- proto_reg_handoff_cms -------------------------------------------*/
173 void proto_reg_handoff_cms(void) {
174 #include "packet-cms-dis-tab.c"
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");