c25601251160cb8dfaefb2384396d1c9dcdd8858
[obnox/wireshark/wip.git] / epan / dissectors / packet-pkcs1.c
1 /* Do not modify this file.                                                   */
2 /* It is created automatically by the ASN.1 to Wireshark dissector compiler   */
3 /* .\packet-pkcs1.c                                                           */
4 /* ../../tools/asn2wrs.py -b -p pkcs1 -c pkcs1.cnf -s packet-pkcs1-template PKCS1.asn */
5
6 /* Input file: packet-pkcs1-template.c */
7
8 #line 1 "packet-pkcs1-template.c"
9 /* packet-pkcs1.c
10  * Routines for PKCS#1/RFC2313 packet dissection
11  *  Ronnie Sahlberg 2004
12  *
13  * $Id$
14  *
15  * Wireshark - Network traffic analyzer
16  * By Gerald Combs <gerald@wireshark.org>
17  * Copyright 1998 Gerald Combs
18  *
19  * This program is free software; you can redistribute it and/or
20  * modify it under the terms of the GNU General Public License
21  * as published by the Free Software Foundation; either version 2
22  * of the License, or (at your option) any later version.
23  *
24  * This program is distributed in the hope that it will be useful,
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27  * GNU General Public License for more details.
28  *
29  * You should have received a copy of the GNU General Public License
30  * along with this program; if not, write to the Free Software
31  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
32  */
33
34 #ifdef HAVE_CONFIG_H
35 # include "config.h"
36 #endif
37
38 #include <glib.h>
39 #include <epan/packet.h>
40 #include <epan/conversation.h>
41 #include <epan/asn1.h>
42
43 #include <stdio.h>
44 #include <string.h>
45
46 #include "packet-ber.h"
47 #include "packet-pkcs1.h"
48 #include "packet-x509af.h"
49
50 #define PNAME  "PKCS#1"
51 #define PSNAME "PKCS-1"
52 #define PFNAME "pkcs-1"
53
54 /* Initialize the protocol and registered fields */
55 static int proto_pkcs1 = -1;
56
57 /*--- Included file: packet-pkcs1-hf.c ---*/
58 #line 1 "packet-pkcs1-hf.c"
59 static int hf_pkcs1_modulus = -1;                 /* INTEGER */
60 static int hf_pkcs1_publicExponent = -1;          /* INTEGER */
61 static int hf_pkcs1_version = -1;                 /* Version */
62 static int hf_pkcs1_privateExponent = -1;         /* INTEGER */
63 static int hf_pkcs1_prime1 = -1;                  /* INTEGER */
64 static int hf_pkcs1_prime2 = -1;                  /* INTEGER */
65 static int hf_pkcs1_exponent1 = -1;               /* INTEGER */
66 static int hf_pkcs1_exponent2 = -1;               /* INTEGER */
67 static int hf_pkcs1_coefficient = -1;             /* INTEGER */
68 static int hf_pkcs1_digestAlgorithm = -1;         /* DigestAlgorithmIdentifier */
69 static int hf_pkcs1_digest = -1;                  /* Digest */
70
71 /*--- End of included file: packet-pkcs1-hf.c ---*/
72 #line 49 "packet-pkcs1-template.c"
73
74 /* Initialize the subtree pointers */
75
76 /*--- Included file: packet-pkcs1-ett.c ---*/
77 #line 1 "packet-pkcs1-ett.c"
78 static gint ett_pkcs1_RSAPublicKey = -1;
79 static gint ett_pkcs1_RSAPrivateKey = -1;
80 static gint ett_pkcs1_DigestInfo = -1;
81
82 /*--- End of included file: packet-pkcs1-ett.c ---*/
83 #line 52 "packet-pkcs1-template.c"
84
85
86 /*--- Included file: packet-pkcs1-fn.c ---*/
87 #line 1 "packet-pkcs1-fn.c"
88 /*--- Fields for imported types ---*/
89
90
91
92
93 static int
94 dissect_pkcs1_INTEGER(gboolean implicit_tag _U_, tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_, proto_tree *tree _U_, int hf_index _U_) {
95   offset = dissect_ber_integer(implicit_tag, actx, tree, tvb, offset, hf_index,
96                                   NULL);
97
98   return offset;
99 }
100 static int dissect_modulus(proto_tree *tree _U_, tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_) {
101   return dissect_pkcs1_INTEGER(FALSE, tvb, offset, actx, tree, hf_pkcs1_modulus);
102 }
103 static int dissect_publicExponent(proto_tree *tree _U_, tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_) {
104   return dissect_pkcs1_INTEGER(FALSE, tvb, offset, actx, tree, hf_pkcs1_publicExponent);
105 }
106 static int dissect_privateExponent(proto_tree *tree _U_, tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_) {
107   return dissect_pkcs1_INTEGER(FALSE, tvb, offset, actx, tree, hf_pkcs1_privateExponent);
108 }
109 static int dissect_prime1(proto_tree *tree _U_, tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_) {
110   return dissect_pkcs1_INTEGER(FALSE, tvb, offset, actx, tree, hf_pkcs1_prime1);
111 }
112 static int dissect_prime2(proto_tree *tree _U_, tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_) {
113   return dissect_pkcs1_INTEGER(FALSE, tvb, offset, actx, tree, hf_pkcs1_prime2);
114 }
115 static int dissect_exponent1(proto_tree *tree _U_, tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_) {
116   return dissect_pkcs1_INTEGER(FALSE, tvb, offset, actx, tree, hf_pkcs1_exponent1);
117 }
118 static int dissect_exponent2(proto_tree *tree _U_, tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_) {
119   return dissect_pkcs1_INTEGER(FALSE, tvb, offset, actx, tree, hf_pkcs1_exponent2);
120 }
121 static int dissect_coefficient(proto_tree *tree _U_, tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_) {
122   return dissect_pkcs1_INTEGER(FALSE, tvb, offset, actx, tree, hf_pkcs1_coefficient);
123 }
124
125
126 static const ber_old_sequence_t RSAPublicKey_sequence[] = {
127   { BER_CLASS_UNI, BER_UNI_TAG_INTEGER, BER_FLAGS_NOOWNTAG, dissect_modulus },
128   { BER_CLASS_UNI, BER_UNI_TAG_INTEGER, BER_FLAGS_NOOWNTAG, dissect_publicExponent },
129   { 0, 0, 0, NULL }
130 };
131
132 int
133 dissect_pkcs1_RSAPublicKey(gboolean implicit_tag _U_, tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_, proto_tree *tree _U_, int hf_index _U_) {
134   offset = dissect_ber_old_sequence(implicit_tag, actx, tree, tvb, offset,
135                                        RSAPublicKey_sequence, hf_index, ett_pkcs1_RSAPublicKey);
136
137   return offset;
138 }
139
140
141
142 static int
143 dissect_pkcs1_Version(gboolean implicit_tag _U_, tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_, proto_tree *tree _U_, int hf_index _U_) {
144   offset = dissect_ber_integer(implicit_tag, actx, tree, tvb, offset, hf_index,
145                                   NULL);
146
147   return offset;
148 }
149 static int dissect_version(proto_tree *tree _U_, tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_) {
150   return dissect_pkcs1_Version(FALSE, tvb, offset, actx, tree, hf_pkcs1_version);
151 }
152
153
154 static const ber_old_sequence_t RSAPrivateKey_sequence[] = {
155   { BER_CLASS_UNI, BER_UNI_TAG_INTEGER, BER_FLAGS_NOOWNTAG, dissect_version },
156   { BER_CLASS_UNI, BER_UNI_TAG_INTEGER, BER_FLAGS_NOOWNTAG, dissect_modulus },
157   { BER_CLASS_UNI, BER_UNI_TAG_INTEGER, BER_FLAGS_NOOWNTAG, dissect_publicExponent },
158   { BER_CLASS_UNI, BER_UNI_TAG_INTEGER, BER_FLAGS_NOOWNTAG, dissect_privateExponent },
159   { BER_CLASS_UNI, BER_UNI_TAG_INTEGER, BER_FLAGS_NOOWNTAG, dissect_prime1 },
160   { BER_CLASS_UNI, BER_UNI_TAG_INTEGER, BER_FLAGS_NOOWNTAG, dissect_prime2 },
161   { BER_CLASS_UNI, BER_UNI_TAG_INTEGER, BER_FLAGS_NOOWNTAG, dissect_exponent1 },
162   { BER_CLASS_UNI, BER_UNI_TAG_INTEGER, BER_FLAGS_NOOWNTAG, dissect_exponent2 },
163   { BER_CLASS_UNI, BER_UNI_TAG_INTEGER, BER_FLAGS_NOOWNTAG, dissect_coefficient },
164   { 0, 0, 0, NULL }
165 };
166
167 int
168 dissect_pkcs1_RSAPrivateKey(gboolean implicit_tag _U_, tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_, proto_tree *tree _U_, int hf_index _U_) {
169   offset = dissect_ber_old_sequence(implicit_tag, actx, tree, tvb, offset,
170                                        RSAPrivateKey_sequence, hf_index, ett_pkcs1_RSAPrivateKey);
171
172   return offset;
173 }
174
175
176
177 static int
178 dissect_pkcs1_DigestAlgorithmIdentifier(gboolean implicit_tag _U_, tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_, proto_tree *tree _U_, int hf_index _U_) {
179   offset = dissect_x509af_AlgorithmIdentifier(implicit_tag, tvb, offset, actx, tree, hf_index);
180
181   return offset;
182 }
183 static int dissect_digestAlgorithm(proto_tree *tree _U_, tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_) {
184   return dissect_pkcs1_DigestAlgorithmIdentifier(FALSE, tvb, offset, actx, tree, hf_pkcs1_digestAlgorithm);
185 }
186
187
188
189 static int
190 dissect_pkcs1_Digest(gboolean implicit_tag _U_, tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_, proto_tree *tree _U_, int hf_index _U_) {
191   offset = dissect_ber_octet_string(implicit_tag, actx, tree, tvb, offset, hf_index,
192                                        NULL);
193
194   return offset;
195 }
196 static int dissect_digest(proto_tree *tree _U_, tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_) {
197   return dissect_pkcs1_Digest(FALSE, tvb, offset, actx, tree, hf_pkcs1_digest);
198 }
199
200
201 static const ber_old_sequence_t DigestInfo_sequence[] = {
202   { BER_CLASS_UNI, BER_UNI_TAG_SEQUENCE, BER_FLAGS_NOOWNTAG, dissect_digestAlgorithm },
203   { BER_CLASS_UNI, BER_UNI_TAG_OCTETSTRING, BER_FLAGS_NOOWNTAG, dissect_digest },
204   { 0, 0, 0, NULL }
205 };
206
207 int
208 dissect_pkcs1_DigestInfo(gboolean implicit_tag _U_, tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_, proto_tree *tree _U_, int hf_index _U_) {
209   offset = dissect_ber_old_sequence(implicit_tag, actx, tree, tvb, offset,
210                                        DigestInfo_sequence, hf_index, ett_pkcs1_DigestInfo);
211
212   return offset;
213 }
214
215
216 /*--- End of included file: packet-pkcs1-fn.c ---*/
217 #line 54 "packet-pkcs1-template.c"
218
219 /*--- proto_register_pkcs1 ----------------------------------------------*/
220 void proto_register_pkcs1(void) {
221
222   /* List of fields */
223   static hf_register_info hf[] = {
224
225 /*--- Included file: packet-pkcs1-hfarr.c ---*/
226 #line 1 "packet-pkcs1-hfarr.c"
227     { &hf_pkcs1_modulus,
228       { "modulus", "pkcs1.modulus",
229         FT_INT32, BASE_DEC, NULL, 0,
230         "pkcs1.INTEGER", HFILL }},
231     { &hf_pkcs1_publicExponent,
232       { "publicExponent", "pkcs1.publicExponent",
233         FT_INT32, BASE_DEC, NULL, 0,
234         "pkcs1.INTEGER", HFILL }},
235     { &hf_pkcs1_version,
236       { "version", "pkcs1.version",
237         FT_INT32, BASE_DEC, VALS(x509af_Version_vals), 0,
238         "pkcs1.Version", HFILL }},
239     { &hf_pkcs1_privateExponent,
240       { "privateExponent", "pkcs1.privateExponent",
241         FT_INT32, BASE_DEC, NULL, 0,
242         "pkcs1.INTEGER", HFILL }},
243     { &hf_pkcs1_prime1,
244       { "prime1", "pkcs1.prime1",
245         FT_INT32, BASE_DEC, NULL, 0,
246         "pkcs1.INTEGER", HFILL }},
247     { &hf_pkcs1_prime2,
248       { "prime2", "pkcs1.prime2",
249         FT_INT32, BASE_DEC, NULL, 0,
250         "pkcs1.INTEGER", HFILL }},
251     { &hf_pkcs1_exponent1,
252       { "exponent1", "pkcs1.exponent1",
253         FT_INT32, BASE_DEC, NULL, 0,
254         "pkcs1.INTEGER", HFILL }},
255     { &hf_pkcs1_exponent2,
256       { "exponent2", "pkcs1.exponent2",
257         FT_INT32, BASE_DEC, NULL, 0,
258         "pkcs1.INTEGER", HFILL }},
259     { &hf_pkcs1_coefficient,
260       { "coefficient", "pkcs1.coefficient",
261         FT_INT32, BASE_DEC, NULL, 0,
262         "pkcs1.INTEGER", HFILL }},
263     { &hf_pkcs1_digestAlgorithm,
264       { "digestAlgorithm", "pkcs1.digestAlgorithm",
265         FT_NONE, BASE_NONE, NULL, 0,
266         "pkcs1.DigestAlgorithmIdentifier", HFILL }},
267     { &hf_pkcs1_digest,
268       { "digest", "pkcs1.digest",
269         FT_BYTES, BASE_HEX, NULL, 0,
270         "pkcs1.Digest", HFILL }},
271
272 /*--- End of included file: packet-pkcs1-hfarr.c ---*/
273 #line 61 "packet-pkcs1-template.c"
274   };
275
276   /* List of subtrees */
277   static gint *ett[] = {
278
279 /*--- Included file: packet-pkcs1-ettarr.c ---*/
280 #line 1 "packet-pkcs1-ettarr.c"
281     &ett_pkcs1_RSAPublicKey,
282     &ett_pkcs1_RSAPrivateKey,
283     &ett_pkcs1_DigestInfo,
284
285 /*--- End of included file: packet-pkcs1-ettarr.c ---*/
286 #line 66 "packet-pkcs1-template.c"
287   };
288
289   /* Register protocol */
290   proto_pkcs1 = proto_register_protocol(PNAME, PSNAME, PFNAME);
291
292   /* Register fields and subtrees */
293   proto_register_field_array(proto_pkcs1, hf, array_length(hf));
294   proto_register_subtree_array(ett, array_length(ett));
295
296 }
297
298
299 /*--- proto_reg_handoff_pkcs1 -------------------------------------------*/
300 void proto_reg_handoff_pkcs1(void) {
301         register_ber_oid_dissector("1.2.840.113549.2.2", dissect_ber_oid_NULL_callback, proto_pkcs1, "md2");
302         register_ber_oid_dissector("1.2.840.113549.2.4", dissect_ber_oid_NULL_callback, proto_pkcs1, "md4");
303         register_ber_oid_dissector("1.2.840.113549.2.5", dissect_ber_oid_NULL_callback, proto_pkcs1, "md5");
304
305         register_ber_oid_dissector("1.2.840.113549.1.1.1", dissect_ber_oid_NULL_callback, proto_pkcs1, "rsaEncryption");
306         register_ber_oid_dissector("1.2.840.113549.1.1.2", dissect_ber_oid_NULL_callback, proto_pkcs1, "md2WithRSAEncryption");
307         register_ber_oid_dissector("1.2.840.113549.1.1.3", dissect_ber_oid_NULL_callback, proto_pkcs1, "md4WithRSAEncryption");
308         register_ber_oid_dissector("1.2.840.113549.1.1.4", dissect_ber_oid_NULL_callback, proto_pkcs1, "md5WithRSAEncryption");
309
310
311         /* these two are not from RFC2313  but pulled in from
312            http://www.alvestrand.no/objectid/1.2.840.113549.1.1.html
313         */
314         register_ber_oid_dissector("1.2.840.113549.1.1.5", dissect_ber_oid_NULL_callback, proto_pkcs1, "shaWithRSAEncryption");
315         register_ber_oid_dissector("1.2.840.113549.1.1.6", dissect_ber_oid_NULL_callback, proto_pkcs1, "rsaOAEPEncryptionSET");
316 }
317