2 * Routines for PKCS#12: Personal Information Exchange packet dissection
7 * Wireshark - Network traffic analyzer
8 * By Gerald Combs <gerald@wireshark.org>
9 * Copyright 1998 Gerald Combs
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
29 #include <epan/packet.h>
30 #include <epan/oids.h>
31 #include <epan/asn1.h>
32 #include <epan/prefs.h>
34 #include "packet-ber.h"
35 #include "packet-pkcs12.h"
36 #include "packet-x509af.h"
37 #include "packet-x509if.h"
38 #include "packet-cms.h"
40 #ifdef HAVE_SYS_TYPES_H
41 #include <sys/types.h>
44 #ifdef HAVE_SYS_TIME_H
49 #include <wsutil/wsgcrypt.h>
52 #define PNAME "PKCS#12: Personal Information Exchange"
53 #define PSNAME "PKCS12"
54 #define PFNAME "pkcs12"
56 #define PKCS12_PBE_ARCFOUR_SHA1_OID "1.2.840.113549.1.12.1.1"
57 #define PKCS12_PBE_3DES_SHA1_OID "1.2.840.113549.1.12.1.3"
58 #define PKCS12_PBE_RC2_40_SHA1_OID "1.2.840.113549.1.12.1.6"
60 void proto_register_pkcs12(void);
61 void proto_reg_handoff_pkcs12(void);
63 /* Initialize the protocol and registered fields */
64 static int proto_pkcs12 = -1;
66 static int hf_pkcs12_X509Certificate_PDU = -1;
67 static gint ett_decrypted_pbe = -1;
69 static const char *object_identifier_id = NULL;
70 static int iteration_count = 0;
71 static tvbuff_t *salt = NULL;
72 static const char *password = NULL;
73 static gboolean try_null_password = FALSE;
75 static void dissect_AuthenticatedSafe_OCTETSTRING_PDU(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);
76 static void dissect_SafeContents_OCTETSTRING_PDU(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);
77 static void dissect_PrivateKeyInfo_PDU(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);
79 #include "packet-pkcs12-hf.c"
81 /* Initialize the subtree pointers */
82 #include "packet-pkcs12-ett.c"
84 static void append_oid(proto_tree *tree, const char *oid)
86 const char *name = NULL;
88 name = oid_resolved_from_string(oid);
89 proto_item_append_text(tree, " (%s)", name ? name : oid);
95 generate_key_or_iv(unsigned int id, tvbuff_t *salt_tvb, unsigned int iter,
96 const char *pw, unsigned int req_keylen, char * keybuf)
101 gcry_mpi_t num_b1 = NULL;
103 char hash[20], buf_b[64], buf_i[128], *p;
112 salt_size = tvb_length(salt_tvb);
113 salt_p = tvb_get_ephemeral_string(salt_tvb, 0, salt_size);
125 /* Store salt and password in BUF_I */
127 for (i = 0; i < 64; i++)
128 *p++ = salt_p[i % salt_size];
131 for (i = j = 0; i < 64; i += 2)
135 if (++j > pwlen) /* Note, that we include the trailing zero */
143 err = gcry_md_open(&md, GCRY_MD_SHA1, 0);
144 if (gcry_err_code(err)) {
147 for (i = 0; i < 64; i++) {
148 unsigned char lid = id & 0xFF;
149 gcry_md_write (md, &lid, 1);
152 gcry_md_write(md, buf_i, pw ? 128 : 64);
155 memcpy (hash, gcry_md_read (md, 0), 20);
159 for (i = 1; i < iter; i++)
160 gcry_md_hash_buffer (GCRY_MD_SHA1, hash, hash, 20);
162 for (i = 0; i < 20 && cur_keylen < req_keylen; i++)
163 keybuf[cur_keylen++] = hash[i];
165 if (cur_keylen == req_keylen) {
166 gcry_mpi_release (num_b1);
167 return TRUE; /* ready */
170 /* need more bytes. */
171 for (i = 0; i < 64; i++)
172 buf_b[i] = hash[i % 20];
176 rc = gcry_mpi_scan (&num_b1, GCRYMPI_FMT_USG, buf_b, n, &n);
182 gcry_mpi_add_ui (num_b1, num_b1, 1);
184 for (i = 0; i < 128; i += 64) {
188 rc = gcry_mpi_scan (&num_ij, GCRYMPI_FMT_USG, buf_i + i, n, &n);
194 gcry_mpi_add (num_ij, num_ij, num_b1);
195 gcry_mpi_clear_highbit (num_ij, 64 * 8);
199 rc = gcry_mpi_print (GCRYMPI_FMT_USG, buf_i + i, n, &n, num_ij);
204 gcry_mpi_release (num_ij);
211 void PBE_reset_parameters(void)
217 int PBE_decrypt_data(const char *object_identifier_id_param, tvbuff_t *encrypted_tvb, asn1_ctx_t *actx, proto_item *item)
219 #ifdef HAVE_LIBGCRYPT
220 const char *encryption_algorithm;
221 gcry_cipher_hd_t cipher;
230 char *clear_data = NULL;
231 tvbuff_t *clear_tvb = NULL;
232 const gchar *oidname;
236 gboolean decrypt_ok = TRUE;
238 if(((password == NULL) || (*password == '\0')) && (try_null_password == FALSE)) {
239 /* we are not configured to decrypt */
243 encryption_algorithm = x509af_get_last_algorithm_id();
245 /* these are the only encryption schemes we understand for now */
246 if(!strcmp(encryption_algorithm, PKCS12_PBE_3DES_SHA1_OID)) {
249 algo = GCRY_CIPHER_3DES;
250 mode = GCRY_CIPHER_MODE_CBC;
251 } else if(!strcmp(encryption_algorithm, PKCS12_PBE_ARCFOUR_SHA1_OID)) {
254 algo = GCRY_CIPHER_ARCFOUR;
255 mode = GCRY_CIPHER_MODE_NONE;
256 } else if(!strcmp(encryption_algorithm, PKCS12_PBE_RC2_40_SHA1_OID)) {
259 algo = GCRY_CIPHER_RFC2268_40;
260 mode = GCRY_CIPHER_MODE_CBC;
262 /* we don't know how to decrypt this */
264 proto_item_append_text(item, " [Unsupported encryption algorithm]");
268 if((iteration_count == 0) || (salt == NULL)) {
269 proto_item_append_text(item, " [Insufficient parameters]");
273 /* allocate buffers */
274 key = (char *)ep_alloc(keylen);
276 if(!generate_key_or_iv(1 /*LEY */, salt, iteration_count, password, keylen, key))
281 iv = (char *)ep_alloc(ivlen);
283 if(!generate_key_or_iv(2 /* IV */, salt, iteration_count, password, ivlen, iv))
287 /* now try an internal function */
288 err = gcry_cipher_open(&cipher, algo, mode, 0);
289 if (gcry_err_code (err))
292 err = gcry_cipher_setkey (cipher, key, keylen);
293 if (gcry_err_code (err)) {
294 gcry_cipher_close (cipher);
299 err = gcry_cipher_setiv (cipher, iv, ivlen);
300 if (gcry_err_code (err)) {
301 gcry_cipher_close (cipher);
306 datalen = tvb_length(encrypted_tvb);
307 clear_data = (char *)g_malloc(datalen);
309 err = gcry_cipher_decrypt (cipher, clear_data, datalen, tvb_get_ephemeral_string(encrypted_tvb, 0, datalen), datalen);
310 if (gcry_err_code (err)) {
312 proto_item_append_text(item, " [Failed to decrypt with password preference]");
314 gcry_cipher_close (cipher);
319 gcry_cipher_close (cipher);
321 /* We don't know if we have successfully decrypted the data or not so we:
322 a) check the trailing bytes
323 b) see if we start with a sequence or a set (is this too constraining?
326 /* first the trailing bytes */
327 byte = clear_data[datalen-1];
331 for(i = (int)byte; i > 0 ; i--) {
332 if(clear_data[datalen - i] != byte) {
338 /* XXX: is this a failure? */
341 /* we assume the result is ASN.1 - check it is a SET or SEQUENCE */
342 byte = clear_data[0];
343 if((byte != 0x30) && (byte != 0x31)) { /* do we need more here? OCTET STRING? */
349 proto_item_append_text(item, " [Failed to decrypt with supplied password]");
354 proto_item_append_text(item, " [Decrypted successfully]");
356 tree = proto_item_add_subtree(item, ett_decrypted_pbe);
358 /* OK - so now clear_data contains the decrypted data */
360 clear_tvb = tvb_new_child_real_data(encrypted_tvb,(const guint8 *)clear_data, datalen, datalen);
361 tvb_set_free_cb(clear_tvb, g_free);
363 name = g_string_new("");
364 oidname = oid_resolved_from_string(object_identifier_id_param);
365 g_string_printf(name, "Decrypted %s", oidname ? oidname : object_identifier_id_param);
367 /* add it as a new source */
368 add_new_data_source(actx->pinfo, clear_tvb, name->str);
370 g_string_free(name, TRUE);
372 /* now try and decode it */
373 call_ber_oid_callback(object_identifier_id_param, clear_tvb, 0, actx->pinfo, tree);
377 /* we cannot decrypt */
383 #include "packet-pkcs12-fn.c"
385 static int strip_octet_string(tvbuff_t *tvb)
393 /* PKCS#7 encodes the content as OCTET STRING, whereas CMS is just any ANY */
394 /* if we use CMS (rather than PKCS#7) - which we are - we need to strip the OCTET STRING tag */
395 /* before proceeding */
397 offset = get_ber_identifier(tvb, 0, &ber_class, &pc, &tag);
398 offset = get_ber_length(tvb, offset, &len, &ind);
400 if((ber_class == BER_CLASS_UNI) && (tag == BER_UNI_TAG_OCTETSTRING))
407 static void dissect_AuthenticatedSafe_OCTETSTRING_PDU(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree) {
410 asn1_ctx_init(&asn1_ctx, ASN1_ENC_BER, TRUE, pinfo);
412 if((offset = strip_octet_string(tvb)) > 0)
413 dissect_pkcs12_AuthenticatedSafe(FALSE, tvb, offset, &asn1_ctx, tree, hf_pkcs12_AuthenticatedSafe_PDU);
415 proto_tree_add_text(tree, tvb, 0, 1, "BER Error: OCTET STRING expected");
418 static void dissect_SafeContents_OCTETSTRING_PDU(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
422 asn1_ctx_init(&asn1_ctx, ASN1_ENC_BER, TRUE, pinfo);
424 offset = strip_octet_string(tvb);
426 dissect_pkcs12_SafeContents(FALSE, tvb, offset, &asn1_ctx, tree, hf_pkcs12_SafeContents_PDU);
429 static void dissect_X509Certificate_OCTETSTRING_PDU(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
433 asn1_ctx_init(&asn1_ctx, ASN1_ENC_BER, TRUE, pinfo);
435 if((offset = strip_octet_string(tvb)) > 0)
436 dissect_x509af_Certificate(FALSE, tvb, offset, &asn1_ctx, tree, hf_pkcs12_X509Certificate_PDU);
438 proto_tree_add_text(tree, tvb, 0, 1, "BER Error: OCTET STRING expected");
441 /*--- proto_register_pkcs12 ----------------------------------------------*/
442 void proto_register_pkcs12(void) {
445 static hf_register_info hf[] = {
446 { &hf_pkcs12_X509Certificate_PDU,
447 { "X509Certificate", "pkcs12.X509Certificate",
448 FT_NONE, BASE_NONE, NULL, 0,
449 "pkcs12.X509Certificate", HFILL }},
450 #include "packet-pkcs12-hfarr.c"
453 /* List of subtrees */
454 static gint *ett[] = {
456 #include "packet-pkcs12-ettarr.c"
458 module_t *pkcs12_module;
460 /* Register protocol */
461 proto_pkcs12 = proto_register_protocol(PNAME, PSNAME, PFNAME);
463 /* Register fields and subtrees */
464 proto_register_field_array(proto_pkcs12, hf, array_length(hf));
465 proto_register_subtree_array(ett, array_length(ett));
467 /* Register preferences */
468 pkcs12_module = prefs_register_protocol(proto_pkcs12, NULL);
470 prefs_register_string_preference(pkcs12_module, "password",
471 "Password to decrypt the file with",
472 "The password to used to decrypt the encrypted elements within"
473 " the PKCS#12 file", &password);
475 prefs_register_bool_preference(pkcs12_module, "try_null_password",
476 "Try to decrypt with a empty password",
477 "Whether to try and decrypt the encrypted data within the"
478 " PKCS#12 with a NULL password", &try_null_password);
480 register_ber_syntax_dissector("PKCS#12", proto_pkcs12, dissect_PFX_PDU);
481 register_ber_oid_syntax(".p12", NULL, "PKCS#12");
482 register_ber_oid_syntax(".pfx", NULL, "PKCS#12");
486 /*--- proto_reg_handoff_pkcs12 -------------------------------------------*/
487 void proto_reg_handoff_pkcs12(void) {
488 #include "packet-pkcs12-dis-tab.c"
490 register_ber_oid_dissector("1.2.840.113549.1.9.22.1", dissect_X509Certificate_OCTETSTRING_PDU, proto_pkcs12, "x509Certificate");