2 * This is (now) gnutls_pkcs12_simple_parse() from GnuTLS 3.1, although
3 * it was actually taken from parse_pkcs12() in GnuTLS 2.12.x (where it
4 * was under LGPLv2.1) and modified locally. The modifications were
5 * accepted back into GnuTLS in commit 9a43e8fa. Further modifications
6 * by Nikos Mavrogiannopoulos are included here under LGPLv2.1 with his
10 #define opaque unsigned char
11 #define gnutls_assert() do {} while(0)
12 #define gnutls_assert_val(x) (x)
15 * Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
16 * Free Software Foundation, Inc.
18 * Author: Nikos Mavrogiannopoulos
20 * This file WAS part of GnuTLS.
22 * The GnuTLS is free software; you can redistribute it and/or
23 * modify it under the terms of the GNU Lesser General Public License
24 * as published by the Free Software Foundation; either version 2.1 of
25 * the License, or (at your option) any later version.
27 * This library is distributed in the hope that it will be useful, but
28 * WITHOUT ANY WARRANTY; without even the implied warranty of
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
30 * Lesser General Public License for more details.
32 * You should have received a copy of the GNU Lesser General Public
33 * License along with this library; if not, write to the Free Software
34 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
40 /* Checks if the extra_certs contain certificates that may form a chain
41 * with the first certificate in chain (it is expected that chain_len==1)
42 * and appends those in the chain.
44 static int make_chain(gnutls_x509_crt_t **chain, unsigned int *chain_len,
45 gnutls_x509_crt_t **extra_certs, unsigned int *extra_certs_len)
50 return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
53 while(i<*extra_certs_len)
55 /* if it is an issuer but not a self-signed one */
56 if (gnutls_x509_crt_check_issuer((*chain)[*chain_len - 1], (*extra_certs)[i]) != 0 &&
57 gnutls_x509_crt_check_issuer((*extra_certs)[i], (*extra_certs)[i]) == 0)
59 *chain = gnutls_realloc (*chain, sizeof((*chain)[0]) *
64 return GNUTLS_E_MEMORY_ERROR;
66 (*chain)[*chain_len - 1] = (*extra_certs)[i];
68 (*extra_certs)[i] = (*extra_certs)[*extra_certs_len-1];
80 * gnutls_pkcs12_simple_parse:
81 * @p12: the PKCS#12 blob.
82 * @password: optional password used to decrypt PKCS#12 blob, bags and keys.
83 * @key: a structure to store the parsed private key.
84 * @chain: the corresponding to key certificate chain
85 * @chain_len: will be updated with the number of additional
86 * @extra_certs: optional pointer to receive an array of additional
87 * certificates found in the PKCS#12 blob.
88 * @extra_certs_len: will be updated with the number of additional
90 * @crl: an optional structure to store the parsed CRL.
91 * @flags: should be zero
93 * This function parses a PKCS#12 blob in @p12blob and extracts the
94 * private key, the corresponding certificate chain, and any additional
95 * certificates and a CRL.
97 * The @extra_certs_ret and @extra_certs_ret_len parameters are optional
98 * and both may be set to %NULL. If either is non-%NULL, then both must
101 * MAC:ed PKCS#12 files are supported. Encrypted PKCS#12 bags are
102 * supported. Encrypted PKCS#8 private keys are supported. However,
103 * only password based security, and the same password for all
104 * operations, are supported.
106 * The private keys may be RSA PKCS#1 or DSA private keys encoded in
109 * PKCS#12 file may contain many keys and/or certificates, and there
110 * is no way to identify which key/certificate pair you want. You
111 * should make sure the PKCS#12 file only contain one key/certificate
112 * pair and/or one CRL.
114 * It is believed that the limitations of this function is acceptable
115 * for most usage, and that any more flexibility would introduce
116 * complexity that would make it harder to use this functionality at
119 * If the provided structure has encrypted fields but no password
120 * is provided then this function returns %GNUTLS_E_DECRYPTION_FAILED.
122 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
123 * negative error value.
128 gnutls_pkcs12_simple_parse (gnutls_pkcs12_t p12,
129 const char *password,
130 gnutls_x509_privkey_t * key,
131 gnutls_x509_crt_t ** chain,
132 unsigned int * chain_len,
133 gnutls_x509_crt_t ** extra_certs,
134 unsigned int * extra_certs_len,
135 gnutls_x509_crl_t * crl,
138 gnutls_pkcs12_bag_t bag = NULL;
139 gnutls_x509_crt_t *_extra_certs = NULL;
140 unsigned int _extra_certs_len = 0;
141 gnutls_x509_crt_t *_chain = NULL;
142 unsigned int _chain_len = 0;
145 size_t cert_id_size = 0;
146 size_t key_id_size = 0;
156 /* find the first private key */
162 ret = gnutls_pkcs12_bag_init (&bag);
170 ret = gnutls_pkcs12_get_bag (p12, idx, bag);
171 if (ret == GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE)
179 ret = gnutls_pkcs12_bag_get_type (bag, 0);
186 if (ret == GNUTLS_BAG_ENCRYPTED)
188 if (password == NULL)
190 ret = gnutls_assert_val(GNUTLS_E_DECRYPTION_FAILED);
194 ret = gnutls_pkcs12_bag_decrypt (bag, password);
202 elements_in_bag = gnutls_pkcs12_bag_get_count (bag);
203 if (elements_in_bag < 0)
209 for (i = 0; i < elements_in_bag; i++)
214 type = gnutls_pkcs12_bag_get_type (bag, i);
221 ret = gnutls_pkcs12_bag_get_data (bag, i, &data);
230 case GNUTLS_BAG_PKCS8_ENCRYPTED_KEY:
231 if (password == NULL)
233 ret = gnutls_assert_val(GNUTLS_E_DECRYPTION_FAILED);
237 case GNUTLS_BAG_PKCS8_KEY:
238 if (*key != NULL) /* too simple to continue */
244 ret = gnutls_x509_privkey_init (key);
251 ret = gnutls_x509_privkey_import_pkcs8
252 (*key, &data, GNUTLS_X509_FMT_DER, password,
253 type == GNUTLS_BAG_PKCS8_KEY ? GNUTLS_PKCS_PLAIN : 0);
257 gnutls_x509_privkey_deinit (*key);
261 key_id_size = sizeof (key_id);
263 gnutls_x509_privkey_get_key_id (*key, 0, key_id,
268 gnutls_x509_privkey_deinit (*key);
272 privkey_ok = 1; /* break */
280 gnutls_pkcs12_bag_deinit (bag);
282 if (privkey_ok != 0) /* private key was found */
286 if (privkey_ok == 0) /* no private key */
289 return GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE;
292 /* now find the corresponding certificate
301 ret = gnutls_pkcs12_bag_init (&bag);
309 ret = gnutls_pkcs12_get_bag (p12, idx, bag);
310 if (ret == GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE)
318 ret = gnutls_pkcs12_bag_get_type (bag, 0);
325 if (ret == GNUTLS_BAG_ENCRYPTED)
327 ret = gnutls_pkcs12_bag_decrypt (bag, password);
335 elements_in_bag = gnutls_pkcs12_bag_get_count (bag);
336 if (elements_in_bag < 0)
342 for (i = 0; i < elements_in_bag; i++)
346 gnutls_x509_crt_t this_cert;
348 type = gnutls_pkcs12_bag_get_type (bag, i);
355 ret = gnutls_pkcs12_bag_get_data (bag, i, &data);
364 case GNUTLS_BAG_CERTIFICATE:
365 ret = gnutls_x509_crt_init (&this_cert);
373 gnutls_x509_crt_import (this_cert, &data, GNUTLS_X509_FMT_DER);
377 gnutls_x509_crt_deinit (this_cert);
381 /* check if the key id match */
382 cert_id_size = sizeof (cert_id);
384 gnutls_x509_crt_get_key_id (this_cert, 0, cert_id, &cert_id_size);
388 gnutls_x509_crt_deinit (this_cert);
392 if (memcmp (cert_id, key_id, cert_id_size) != 0)
393 { /* they don't match - skip the certificate */
396 _extra_certs = gnutls_realloc (_extra_certs,
397 sizeof(_extra_certs[0]) *
402 ret = GNUTLS_E_MEMORY_ERROR;
405 _extra_certs[_extra_certs_len - 1] = this_cert;
410 gnutls_x509_crt_deinit (this_cert);
417 _chain = gnutls_malloc (sizeof(_chain[0]) * (++_chain_len));
421 ret = GNUTLS_E_MEMORY_ERROR;
424 _chain[_chain_len - 1] = this_cert;
429 gnutls_x509_crt_deinit (this_cert);
435 if (crl == NULL || *crl != NULL)
441 ret = gnutls_x509_crl_init (crl);
448 ret = gnutls_x509_crl_import (*crl, &data, GNUTLS_X509_FMT_DER);
452 gnutls_x509_crl_deinit (*crl);
457 case GNUTLS_BAG_ENCRYPTED:
458 /* XXX Bother to recurse one level down? Unlikely to
459 use the same password anyway. */
460 case GNUTLS_BAG_EMPTY:
467 gnutls_pkcs12_bag_deinit (bag);
472 ret = GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE;
476 ret = make_chain(&_chain, &_chain_len, &_extra_certs, &_extra_certs_len);
487 gnutls_pkcs12_bag_deinit (bag);
492 gnutls_x509_privkey_deinit(*key);
493 if (_extra_certs_len && _extra_certs != NULL)
496 for (i = 0; i < _extra_certs_len; i++)
497 gnutls_x509_crt_deinit(_extra_certs[i]);
498 gnutls_free(_extra_certs);
500 if (_chain_len && chain != NULL)
503 for (i = 0; i < _chain_len; i++)
504 gnutls_x509_crt_deinit(_chain[i]);
512 *extra_certs = _extra_certs;
513 *extra_certs_len = _extra_certs_len;
517 *chain_len = _chain_len;