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