/src/gnutls/lib/x509/pkcs12.c
Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | /*  | 
2  |  |  * Copyright (C) 2003-2012 Free Software Foundation, Inc.  | 
3  |  |  * Copyright (C) 2012 Nikos Mavrogiannopoulos  | 
4  |  |  * Copyright (C) 2017 Red Hat, Inc.  | 
5  |  |  *  | 
6  |  |  * Author: Nikos Mavrogiannopoulos  | 
7  |  |  *  | 
8  |  |  * This file is part of GnuTLS.  | 
9  |  |  *  | 
10  |  |  * The GnuTLS is free software; you can redistribute it and/or  | 
11  |  |  * modify it under the terms of the GNU Lesser General Public License  | 
12  |  |  * as published by the Free Software Foundation; either version 2.1 of  | 
13  |  |  * the License, or (at your option) any later version.  | 
14  |  |  *  | 
15  |  |  * This library is distributed in the hope that it will be useful, but  | 
16  |  |  * WITHOUT ANY WARRANTY; without even the implied warranty of  | 
17  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU  | 
18  |  |  * Lesser General Public License for more details.  | 
19  |  |  *  | 
20  |  |  * You should have received a copy of the GNU Lesser General Public License  | 
21  |  |  * along with this program.  If not, see <https://www.gnu.org/licenses/>  | 
22  |  |  *  | 
23  |  |  */  | 
24  |  |  | 
25  |  | /* Functions that relate on PKCS12 packet parsing.  | 
26  |  |  */  | 
27  |  |  | 
28  |  | #include "gnutls_int.h"  | 
29  |  | #include <libtasn1.h>  | 
30  |  |  | 
31  |  | #include "datum.h"  | 
32  |  | #include "global.h"  | 
33  |  | #include "errors.h"  | 
34  |  | #include "num.h"  | 
35  |  | #include "common.h"  | 
36  |  | #include "x509_b64.h"  | 
37  |  | #include "x509_int.h"  | 
38  |  | #include "pkcs7_int.h"  | 
39  |  | #include "random.h"  | 
40  |  | #include "intprops.h"  | 
41  |  |  | 
42  | 0  | #define PBMAC1_OID "1.2.840.113549.1.5.14"  | 
43  |  |  | 
44  |  | /* Decodes the PKCS #12 auth_safe, and returns the allocated raw data,  | 
45  |  |  * which holds them. Returns an asn1_node of authenticatedSafe.  | 
46  |  |  */  | 
47  |  | static int _decode_pkcs12_auth_safe(asn1_node pkcs12, asn1_node *authen_safe,  | 
48  |  |             gnutls_datum_t *raw)  | 
49  | 0  | { | 
50  | 0  |   char oid[MAX_OID_SIZE];  | 
51  | 0  |   asn1_node c2 = NULL;  | 
52  | 0  |   gnutls_datum_t auth_safe = { NULL, 0 }; | 
53  | 0  |   int len, result;  | 
54  | 0  |   char error_str[ASN1_MAX_ERROR_DESCRIPTION_SIZE];  | 
55  |  | 
  | 
56  | 0  |   len = sizeof(oid) - 1;  | 
57  | 0  |   result = asn1_read_value(pkcs12, "authSafe.contentType", oid, &len);  | 
58  | 0  |   if (result != ASN1_SUCCESS) { | 
59  | 0  |     gnutls_assert();  | 
60  | 0  |     return _gnutls_asn2err(result);  | 
61  | 0  |   }  | 
62  |  |  | 
63  | 0  |   if (strcmp(oid, DATA_OID) != 0) { | 
64  | 0  |     gnutls_assert();  | 
65  | 0  |     _gnutls_debug_log("Unknown PKCS12 Content OID '%s'\n", oid); | 
66  | 0  |     return GNUTLS_E_UNKNOWN_PKCS_CONTENT_TYPE;  | 
67  | 0  |   }  | 
68  |  |  | 
69  |  |   /* Step 1. Read the content data  | 
70  |  |    */  | 
71  |  |  | 
72  | 0  |   result = _gnutls_x509_read_string(pkcs12, "authSafe.content",  | 
73  | 0  |             &auth_safe, ASN1_ETYPE_OCTET_STRING,  | 
74  | 0  |             1);  | 
75  | 0  |   if (result < 0) { | 
76  | 0  |     gnutls_assert();  | 
77  | 0  |     goto cleanup;  | 
78  | 0  |   }  | 
79  |  |  | 
80  |  |   /* Step 2. Extract the authenticatedSafe.  | 
81  |  |    */  | 
82  |  |  | 
83  | 0  |   if ((result = asn1_create_element(_gnutls_get_pkix(),  | 
84  | 0  |             "PKIX1.pkcs-12-AuthenticatedSafe",  | 
85  | 0  |             &c2)) != ASN1_SUCCESS) { | 
86  | 0  |     gnutls_assert();  | 
87  | 0  |     result = _gnutls_asn2err(result);  | 
88  | 0  |     goto cleanup;  | 
89  | 0  |   }  | 
90  |  |  | 
91  | 0  |   result = asn1_der_decoding(&c2, auth_safe.data, auth_safe.size,  | 
92  | 0  |            error_str);  | 
93  | 0  |   if (result != ASN1_SUCCESS) { | 
94  | 0  |     gnutls_assert();  | 
95  | 0  |     _gnutls_debug_log("DER error: %s\n", error_str); | 
96  | 0  |     result = _gnutls_asn2err(result);  | 
97  | 0  |     goto cleanup;  | 
98  | 0  |   }  | 
99  |  |  | 
100  | 0  |   if (raw == NULL) { | 
101  | 0  |     _gnutls_free_datum(&auth_safe);  | 
102  | 0  |   } else { | 
103  | 0  |     raw->data = auth_safe.data;  | 
104  | 0  |     raw->size = auth_safe.size;  | 
105  | 0  |   }  | 
106  |  | 
  | 
107  | 0  |   if (authen_safe)  | 
108  | 0  |     *authen_safe = c2;  | 
109  | 0  |   else  | 
110  | 0  |     asn1_delete_structure(&c2);  | 
111  |  | 
  | 
112  | 0  |   return 0;  | 
113  |  |  | 
114  | 0  | cleanup:  | 
115  | 0  |   if (c2)  | 
116  | 0  |     asn1_delete_structure(&c2);  | 
117  | 0  |   _gnutls_free_datum(&auth_safe);  | 
118  | 0  |   return result;  | 
119  | 0  | }  | 
120  |  |  | 
121  |  | static int pkcs12_reinit(gnutls_pkcs12_t pkcs12)  | 
122  | 0  | { | 
123  | 0  |   int result;  | 
124  |  | 
  | 
125  | 0  |   if (pkcs12->pkcs12)  | 
126  | 0  |     asn1_delete_structure(&pkcs12->pkcs12);  | 
127  |  | 
  | 
128  | 0  |   result = asn1_create_element(_gnutls_get_pkix(), "PKIX1.pkcs-12-PFX",  | 
129  | 0  |              &pkcs12->pkcs12);  | 
130  | 0  |   if (result != ASN1_SUCCESS) { | 
131  | 0  |     gnutls_assert();  | 
132  | 0  |     return _gnutls_asn2err(result);  | 
133  | 0  |   }  | 
134  |  |  | 
135  | 0  |   return 0;  | 
136  | 0  | }  | 
137  |  |  | 
138  |  | /**  | 
139  |  |  * gnutls_pkcs12_init:  | 
140  |  |  * @pkcs12: A pointer to the type to be initialized  | 
141  |  |  *  | 
142  |  |  * This function will initialize a PKCS12 type. PKCS12 structures  | 
143  |  |  * usually contain lists of X.509 Certificates and X.509 Certificate  | 
144  |  |  * revocation lists.  | 
145  |  |  *  | 
146  |  |  * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a  | 
147  |  |  *   negative error value.  | 
148  |  |  **/  | 
149  |  | int gnutls_pkcs12_init(gnutls_pkcs12_t *pkcs12)  | 
150  | 0  | { | 
151  | 0  |   *pkcs12 = gnutls_calloc(1, sizeof(gnutls_pkcs12_int));  | 
152  |  | 
  | 
153  | 0  |   if (*pkcs12) { | 
154  | 0  |     int result = pkcs12_reinit(*pkcs12);  | 
155  | 0  |     if (result < 0) { | 
156  | 0  |       gnutls_assert();  | 
157  | 0  |       gnutls_free(*pkcs12);  | 
158  | 0  |       return result;  | 
159  | 0  |     }  | 
160  | 0  |     return 0; /* success */  | 
161  | 0  |   }  | 
162  | 0  |   return GNUTLS_E_MEMORY_ERROR;  | 
163  | 0  | }  | 
164  |  |  | 
165  |  | /**  | 
166  |  |  * gnutls_pkcs12_deinit:  | 
167  |  |  * @pkcs12: The type to be initialized  | 
168  |  |  *  | 
169  |  |  * This function will deinitialize a PKCS12 type.  | 
170  |  |  **/  | 
171  |  | void gnutls_pkcs12_deinit(gnutls_pkcs12_t pkcs12)  | 
172  | 0  | { | 
173  | 0  |   if (!pkcs12)  | 
174  | 0  |     return;  | 
175  |  |  | 
176  | 0  |   if (pkcs12->pkcs12)  | 
177  | 0  |     asn1_delete_structure(&pkcs12->pkcs12);  | 
178  |  | 
  | 
179  | 0  |   gnutls_free(pkcs12);  | 
180  | 0  | }  | 
181  |  |  | 
182  |  | /**  | 
183  |  |  * gnutls_pkcs12_import:  | 
184  |  |  * @pkcs12: The data to store the parsed PKCS12.  | 
185  |  |  * @data: The DER or PEM encoded PKCS12.  | 
186  |  |  * @format: One of DER or PEM  | 
187  |  |  * @flags: an ORed sequence of gnutls_privkey_pkcs8_flags  | 
188  |  |  *  | 
189  |  |  * This function will convert the given DER or PEM encoded PKCS12  | 
190  |  |  * to the native gnutls_pkcs12_t format. The output will be stored in 'pkcs12'.  | 
191  |  |  *  | 
192  |  |  * If the PKCS12 is PEM encoded it should have a header of "PKCS12".  | 
193  |  |  *  | 
194  |  |  * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a  | 
195  |  |  *   negative error value.  | 
196  |  |  **/  | 
197  |  | int gnutls_pkcs12_import(gnutls_pkcs12_t pkcs12, const gnutls_datum_t *data,  | 
198  |  |        gnutls_x509_crt_fmt_t format, unsigned int flags)  | 
199  | 0  | { | 
200  | 0  |   int result = 0, need_free = 0;  | 
201  | 0  |   gnutls_datum_t _data;  | 
202  | 0  |   char error_str[ASN1_MAX_ERROR_DESCRIPTION_SIZE];  | 
203  |  | 
  | 
204  | 0  |   _data.data = data->data;  | 
205  | 0  |   _data.size = data->size;  | 
206  |  | 
  | 
207  | 0  |   if (pkcs12 == NULL) { | 
208  | 0  |     gnutls_assert();  | 
209  | 0  |     return GNUTLS_E_INVALID_REQUEST;  | 
210  | 0  |   }  | 
211  |  |  | 
212  |  |   /* If the PKCS12 is in PEM format then decode it  | 
213  |  |    */  | 
214  | 0  |   if (format == GNUTLS_X509_FMT_PEM) { | 
215  | 0  |     result = _gnutls_fbase64_decode(PEM_PKCS12, data->data,  | 
216  | 0  |             data->size, &_data);  | 
217  |  | 
  | 
218  | 0  |     if (result < 0) { | 
219  | 0  |       gnutls_assert();  | 
220  | 0  |       return result;  | 
221  | 0  |     }  | 
222  |  |  | 
223  | 0  |     need_free = 1;  | 
224  | 0  |   }  | 
225  |  |  | 
226  | 0  |   if (pkcs12->expanded) { | 
227  | 0  |     result = pkcs12_reinit(pkcs12);  | 
228  | 0  |     if (result < 0) { | 
229  | 0  |       gnutls_assert();  | 
230  | 0  |       goto cleanup;  | 
231  | 0  |     }  | 
232  | 0  |   }  | 
233  | 0  |   pkcs12->expanded = 1;  | 
234  |  | 
  | 
235  | 0  |   result = asn1_der_decoding(&pkcs12->pkcs12, _data.data, _data.size,  | 
236  | 0  |            error_str);  | 
237  | 0  |   if (result != ASN1_SUCCESS) { | 
238  | 0  |     result = _gnutls_asn2err(result);  | 
239  | 0  |     _gnutls_debug_log("DER error: %s\n", error_str); | 
240  | 0  |     gnutls_assert();  | 
241  | 0  |     goto cleanup;  | 
242  | 0  |   }  | 
243  |  |  | 
244  | 0  |   if (need_free)  | 
245  | 0  |     _gnutls_free_datum(&_data);  | 
246  |  | 
  | 
247  | 0  |   return 0;  | 
248  |  |  | 
249  | 0  | cleanup:  | 
250  | 0  |   if (need_free)  | 
251  | 0  |     _gnutls_free_datum(&_data);  | 
252  | 0  |   return result;  | 
253  | 0  | }  | 
254  |  |  | 
255  |  | /**  | 
256  |  |  * gnutls_pkcs12_export:  | 
257  |  |  * @pkcs12: A pkcs12 type  | 
258  |  |  * @format: the format of output params. One of PEM or DER.  | 
259  |  |  * @output_data: will contain a structure PEM or DER encoded  | 
260  |  |  * @output_data_size: holds the size of output_data (and will be  | 
261  |  |  *   replaced by the actual size of parameters)  | 
262  |  |  *  | 
263  |  |  * This function will export the pkcs12 structure to DER or PEM format.  | 
264  |  |  *  | 
265  |  |  * If the buffer provided is not long enough to hold the output, then  | 
266  |  |  * *output_data_size will be updated and GNUTLS_E_SHORT_MEMORY_BUFFER  | 
267  |  |  * will be returned.  | 
268  |  |  *  | 
269  |  |  * If the structure is PEM encoded, it will have a header  | 
270  |  |  * of "BEGIN PKCS12".  | 
271  |  |  *  | 
272  |  |  * Returns: In case of failure a negative error code will be  | 
273  |  |  *   returned, and 0 on success.  | 
274  |  |  **/  | 
275  |  | int gnutls_pkcs12_export(gnutls_pkcs12_t pkcs12, gnutls_x509_crt_fmt_t format,  | 
276  |  |        void *output_data, size_t *output_data_size)  | 
277  | 0  | { | 
278  | 0  |   if (pkcs12 == NULL) { | 
279  | 0  |     gnutls_assert();  | 
280  | 0  |     return GNUTLS_E_INVALID_REQUEST;  | 
281  | 0  |   }  | 
282  |  |  | 
283  | 0  |   return _gnutls_x509_export_int(pkcs12->pkcs12, format, PEM_PKCS12,  | 
284  | 0  |                output_data, output_data_size);  | 
285  | 0  | }  | 
286  |  |  | 
287  |  | /**  | 
288  |  |  * gnutls_pkcs12_export2:  | 
289  |  |  * @pkcs12: A pkcs12 type  | 
290  |  |  * @format: the format of output params. One of PEM or DER.  | 
291  |  |  * @out: will contain a structure PEM or DER encoded  | 
292  |  |  *  | 
293  |  |  * This function will export the pkcs12 structure to DER or PEM format.  | 
294  |  |  *  | 
295  |  |  * The output buffer is allocated using gnutls_malloc().  | 
296  |  |  *  | 
297  |  |  * If the structure is PEM encoded, it will have a header  | 
298  |  |  * of "BEGIN PKCS12".  | 
299  |  |  *  | 
300  |  |  * Returns: In case of failure a negative error code will be  | 
301  |  |  *   returned, and 0 on success.  | 
302  |  |  *  | 
303  |  |  * Since: 3.1.3  | 
304  |  |  **/  | 
305  |  | int gnutls_pkcs12_export2(gnutls_pkcs12_t pkcs12, gnutls_x509_crt_fmt_t format,  | 
306  |  |         gnutls_datum_t *out)  | 
307  | 0  | { | 
308  | 0  |   if (pkcs12 == NULL) { | 
309  | 0  |     gnutls_assert();  | 
310  | 0  |     return GNUTLS_E_INVALID_REQUEST;  | 
311  | 0  |   }  | 
312  |  |  | 
313  | 0  |   return _gnutls_x509_export_int2(pkcs12->pkcs12, format, PEM_PKCS12,  | 
314  | 0  |           out);  | 
315  | 0  | }  | 
316  |  |  | 
317  |  | static int oid2bag(const char *oid)  | 
318  | 0  | { | 
319  | 0  |   if (strcmp(oid, BAG_PKCS8_KEY) == 0)  | 
320  | 0  |     return GNUTLS_BAG_PKCS8_KEY;  | 
321  | 0  |   if (strcmp(oid, BAG_PKCS8_ENCRYPTED_KEY) == 0)  | 
322  | 0  |     return GNUTLS_BAG_PKCS8_ENCRYPTED_KEY;  | 
323  | 0  |   if (strcmp(oid, BAG_CERTIFICATE) == 0)  | 
324  | 0  |     return GNUTLS_BAG_CERTIFICATE;  | 
325  | 0  |   if (strcmp(oid, BAG_CRL) == 0)  | 
326  | 0  |     return GNUTLS_BAG_CRL;  | 
327  | 0  |   if (strcmp(oid, BAG_SECRET) == 0)  | 
328  | 0  |     return GNUTLS_BAG_SECRET;  | 
329  |  |  | 
330  | 0  |   return GNUTLS_BAG_UNKNOWN;  | 
331  | 0  | }  | 
332  |  |  | 
333  |  | static const char *bag_to_oid(int bag)  | 
334  | 0  | { | 
335  | 0  |   switch (bag) { | 
336  | 0  |   case GNUTLS_BAG_PKCS8_KEY:  | 
337  | 0  |     return BAG_PKCS8_KEY;  | 
338  | 0  |   case GNUTLS_BAG_PKCS8_ENCRYPTED_KEY:  | 
339  | 0  |     return BAG_PKCS8_ENCRYPTED_KEY;  | 
340  | 0  |   case GNUTLS_BAG_CERTIFICATE:  | 
341  | 0  |     return BAG_CERTIFICATE;  | 
342  | 0  |   case GNUTLS_BAG_CRL:  | 
343  | 0  |     return BAG_CRL;  | 
344  | 0  |   case GNUTLS_BAG_SECRET:  | 
345  | 0  |     return BAG_SECRET;  | 
346  | 0  |   }  | 
347  | 0  |   return NULL;  | 
348  | 0  | }  | 
349  |  |  | 
350  |  | /* Decodes the SafeContents, and puts the output in  | 
351  |  |  * the given bag.   | 
352  |  |  */  | 
353  |  | int _pkcs12_decode_safe_contents(const gnutls_datum_t *content,  | 
354  |  |          gnutls_pkcs12_bag_t bag)  | 
355  | 0  | { | 
356  | 0  |   char oid[MAX_OID_SIZE], root[MAX_NAME_SIZE];  | 
357  | 0  |   asn1_node c2 = NULL;  | 
358  | 0  |   int len, result;  | 
359  | 0  |   int bag_type;  | 
360  | 0  |   gnutls_datum_t attr_val;  | 
361  | 0  |   gnutls_datum_t t;  | 
362  | 0  |   int count = 0, attributes, j;  | 
363  | 0  |   unsigned i;  | 
364  |  |  | 
365  |  |   /* Step 1. Extract the SEQUENCE.  | 
366  |  |    */  | 
367  |  | 
  | 
368  | 0  |   if ((result = asn1_create_element(_gnutls_get_pkix(),  | 
369  | 0  |             "PKIX1.pkcs-12-SafeContents", &c2)) !=  | 
370  | 0  |       ASN1_SUCCESS) { | 
371  | 0  |     gnutls_assert();  | 
372  | 0  |     result = _gnutls_asn2err(result);  | 
373  | 0  |     goto cleanup;  | 
374  | 0  |   }  | 
375  |  |  | 
376  | 0  |   result = asn1_der_decoding(&c2, content->data, content->size, NULL);  | 
377  | 0  |   if (result != ASN1_SUCCESS) { | 
378  | 0  |     gnutls_assert();  | 
379  | 0  |     result = _gnutls_asn2err(result);  | 
380  | 0  |     goto cleanup;  | 
381  | 0  |   }  | 
382  |  |  | 
383  |  |   /* Count the number of bags  | 
384  |  |    */  | 
385  | 0  |   result = asn1_number_of_elements(c2, "", &count);  | 
386  | 0  |   if (result != ASN1_SUCCESS) { | 
387  | 0  |     gnutls_assert();  | 
388  | 0  |     result = _gnutls_asn2err(result);  | 
389  | 0  |     goto cleanup;  | 
390  | 0  |   }  | 
391  |  |  | 
392  | 0  |   bag->bag_elements = MIN(MAX_BAG_ELEMENTS, count);  | 
393  |  | 
  | 
394  | 0  |   for (i = 0; i < bag->bag_elements; i++) { | 
395  | 0  |     snprintf(root, sizeof(root), "?%u.bagId", i + 1);  | 
396  |  | 
  | 
397  | 0  |     len = sizeof(oid);  | 
398  | 0  |     result = asn1_read_value(c2, root, oid, &len);  | 
399  | 0  |     if (result != ASN1_SUCCESS) { | 
400  | 0  |       gnutls_assert();  | 
401  | 0  |       result = _gnutls_asn2err(result);  | 
402  | 0  |       goto cleanup;  | 
403  | 0  |     }  | 
404  |  |  | 
405  |  |     /* Read the Bag type  | 
406  |  |      */  | 
407  | 0  |     bag_type = oid2bag(oid);  | 
408  |  | 
  | 
409  | 0  |     if (bag_type < 0) { | 
410  | 0  |       gnutls_assert();  | 
411  | 0  |       goto cleanup;  | 
412  | 0  |     }  | 
413  |  |  | 
414  |  |     /* Read the Bag Value  | 
415  |  |      */  | 
416  |  |  | 
417  | 0  |     snprintf(root, sizeof(root), "?%u.bagValue", i + 1);  | 
418  |  | 
  | 
419  | 0  |     result = _gnutls_x509_read_value(c2, root,  | 
420  | 0  |              &bag->element[i].data);  | 
421  | 0  |     if (result < 0) { | 
422  | 0  |       gnutls_assert();  | 
423  | 0  |       goto cleanup;  | 
424  | 0  |     }  | 
425  |  |  | 
426  | 0  |     if (bag_type == GNUTLS_BAG_CERTIFICATE ||  | 
427  | 0  |         bag_type == GNUTLS_BAG_CRL ||  | 
428  | 0  |         bag_type == GNUTLS_BAG_SECRET) { | 
429  | 0  |       gnutls_datum_t tmp = bag->element[i].data;  | 
430  | 0  |       bag->element[i].data.data = NULL;  | 
431  | 0  |       bag->element[i].data.size = 0;  | 
432  |  | 
  | 
433  | 0  |       result = _pkcs12_decode_crt_bag(bag_type, &tmp,  | 
434  | 0  |               &bag->element[i].data);  | 
435  | 0  |       _gnutls_free_datum(&tmp);  | 
436  | 0  |       if (result < 0) { | 
437  | 0  |         gnutls_assert();  | 
438  | 0  |         goto cleanup;  | 
439  | 0  |       }  | 
440  | 0  |     }  | 
441  |  |  | 
442  |  |     /* read the bag attributes  | 
443  |  |      */  | 
444  | 0  |     snprintf(root, sizeof(root), "?%u.bagAttributes", i + 1);  | 
445  |  | 
  | 
446  | 0  |     result = asn1_number_of_elements(c2, root, &attributes);  | 
447  | 0  |     if (result != ASN1_SUCCESS &&  | 
448  | 0  |         result != ASN1_ELEMENT_NOT_FOUND) { | 
449  | 0  |       gnutls_assert();  | 
450  | 0  |       result = _gnutls_asn2err(result);  | 
451  | 0  |       goto cleanup;  | 
452  | 0  |     }  | 
453  |  |  | 
454  | 0  |     if (attributes < 0)  | 
455  | 0  |       attributes = 1;  | 
456  |  | 
  | 
457  | 0  |     if (result != ASN1_ELEMENT_NOT_FOUND)  | 
458  | 0  |       for (j = 0; j < attributes; j++) { | 
459  | 0  |         snprintf(root, sizeof(root),  | 
460  | 0  |            "?%u.bagAttributes.?%d", i + 1, j + 1);  | 
461  |  | 
  | 
462  | 0  |         result = _gnutls_x509_decode_and_read_attribute(  | 
463  | 0  |           c2, root, oid, sizeof(oid), &attr_val,  | 
464  | 0  |           1, 0);  | 
465  |  | 
  | 
466  | 0  |         if (result < 0) { | 
467  | 0  |           gnutls_assert();  | 
468  | 0  |           continue; /* continue in case we find some known attributes */  | 
469  | 0  |         }  | 
470  |  |  | 
471  | 0  |         if (strcmp(oid, KEY_ID_OID) == 0) { | 
472  | 0  |           result = _gnutls_x509_decode_string(  | 
473  | 0  |             ASN1_ETYPE_OCTET_STRING,  | 
474  | 0  |             attr_val.data, attr_val.size,  | 
475  | 0  |             &t, 1);  | 
476  |  | 
  | 
477  | 0  |           _gnutls_free_datum(&attr_val);  | 
478  | 0  |           if (result < 0) { | 
479  | 0  |             gnutls_assert();  | 
480  | 0  |             _gnutls_debug_log(  | 
481  | 0  |               "Error decoding PKCS12 Bag Attribute OID '%s'\n",  | 
482  | 0  |               oid);  | 
483  | 0  |             continue;  | 
484  | 0  |           }  | 
485  |  |  | 
486  | 0  |           _gnutls_free_datum(  | 
487  | 0  |             &bag->element[i].local_key_id);  | 
488  | 0  |           bag->element[i].local_key_id.data =  | 
489  | 0  |             t.data;  | 
490  | 0  |           bag->element[i].local_key_id.size =  | 
491  | 0  |             t.size;  | 
492  | 0  |         } else if (strcmp(oid, FRIENDLY_NAME_OID) ==  | 
493  | 0  |                0 &&  | 
494  | 0  |              bag->element[i].friendly_name ==  | 
495  | 0  |                NULL) { | 
496  | 0  |           result = _gnutls_x509_decode_string(  | 
497  | 0  |             ASN1_ETYPE_BMP_STRING,  | 
498  | 0  |             attr_val.data, attr_val.size,  | 
499  | 0  |             &t, 1);  | 
500  |  | 
  | 
501  | 0  |           _gnutls_free_datum(&attr_val);  | 
502  | 0  |           if (result < 0) { | 
503  | 0  |             gnutls_assert();  | 
504  | 0  |             _gnutls_debug_log(  | 
505  | 0  |               "Error decoding PKCS12 Bag Attribute OID '%s'\n",  | 
506  | 0  |               oid);  | 
507  | 0  |             continue;  | 
508  | 0  |           }  | 
509  |  |  | 
510  | 0  |           gnutls_free(  | 
511  | 0  |             bag->element[i].friendly_name);  | 
512  | 0  |           bag->element[i].friendly_name =  | 
513  | 0  |             (char *)t.data;  | 
514  | 0  |         } else { | 
515  | 0  |           _gnutls_free_datum(&attr_val);  | 
516  | 0  |           _gnutls_debug_log(  | 
517  | 0  |             "Unknown PKCS12 Bag Attribute OID '%s'\n",  | 
518  | 0  |             oid);  | 
519  | 0  |         }  | 
520  | 0  |       }  | 
521  |  | 
  | 
522  | 0  |     bag->element[i].type = bag_type;  | 
523  | 0  |   }  | 
524  |  |  | 
525  | 0  |   result = 0;  | 
526  |  | 
  | 
527  | 0  | cleanup:  | 
528  | 0  |   if (c2)  | 
529  | 0  |     asn1_delete_structure(&c2);  | 
530  | 0  |   return result;  | 
531  | 0  | }  | 
532  |  |  | 
533  |  | static int _parse_safe_contents(asn1_node sc, const char *sc_name,  | 
534  |  |         gnutls_pkcs12_bag_t bag)  | 
535  | 0  | { | 
536  | 0  |   gnutls_datum_t content = { NULL, 0 }; | 
537  | 0  |   int result;  | 
538  |  |  | 
539  |  |   /* Step 1. Extract the content.  | 
540  |  |    */  | 
541  |  | 
  | 
542  | 0  |   result = _gnutls_x509_read_string(sc, sc_name, &content,  | 
543  | 0  |             ASN1_ETYPE_OCTET_STRING, 1);  | 
544  | 0  |   if (result < 0) { | 
545  | 0  |     gnutls_assert();  | 
546  | 0  |     goto cleanup;  | 
547  | 0  |   }  | 
548  |  |  | 
549  | 0  |   result = _pkcs12_decode_safe_contents(&content, bag);  | 
550  | 0  |   if (result < 0) { | 
551  | 0  |     gnutls_assert();  | 
552  | 0  |     goto cleanup;  | 
553  | 0  |   }  | 
554  |  |  | 
555  | 0  |   _gnutls_free_datum(&content);  | 
556  |  | 
  | 
557  | 0  |   return 0;  | 
558  |  |  | 
559  | 0  | cleanup:  | 
560  | 0  |   _gnutls_free_datum(&content);  | 
561  | 0  |   return result;  | 
562  | 0  | }  | 
563  |  |  | 
564  |  | /**  | 
565  |  |  * gnutls_pkcs12_get_bag:  | 
566  |  |  * @pkcs12: A pkcs12 type  | 
567  |  |  * @indx: contains the index of the bag to extract  | 
568  |  |  * @bag: An initialized bag, where the contents of the bag will be copied  | 
569  |  |  *  | 
570  |  |  * This function will return a Bag from the PKCS12 structure.  | 
571  |  |  *  | 
572  |  |  * After the last Bag has been read  | 
573  |  |  * %GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE will be returned.  | 
574  |  |  *  | 
575  |  |  * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a  | 
576  |  |  *   negative error value.  | 
577  |  |  **/  | 
578  |  | int gnutls_pkcs12_get_bag(gnutls_pkcs12_t pkcs12, int indx,  | 
579  |  |         gnutls_pkcs12_bag_t bag)  | 
580  | 0  | { | 
581  | 0  |   asn1_node c2 = NULL;  | 
582  | 0  |   int result, len;  | 
583  | 0  |   char root2[MAX_NAME_SIZE];  | 
584  | 0  |   char oid[MAX_OID_SIZE];  | 
585  |  | 
  | 
586  | 0  |   if (pkcs12 == NULL) { | 
587  | 0  |     gnutls_assert();  | 
588  | 0  |     return GNUTLS_E_INVALID_REQUEST;  | 
589  | 0  |   }  | 
590  |  |  | 
591  |  |   /* Step 1. decode the data.  | 
592  |  |    */  | 
593  | 0  |   result = _decode_pkcs12_auth_safe(pkcs12->pkcs12, &c2, NULL);  | 
594  | 0  |   if (result < 0) { | 
595  | 0  |     gnutls_assert();  | 
596  | 0  |     return result;  | 
597  | 0  |   }  | 
598  |  |  | 
599  |  |   /* Step 2. Parse the AuthenticatedSafe  | 
600  |  |    */  | 
601  |  |  | 
602  | 0  |   snprintf(root2, sizeof(root2), "?%d.contentType", indx + 1);  | 
603  |  | 
  | 
604  | 0  |   len = sizeof(oid) - 1;  | 
605  | 0  |   result = asn1_read_value(c2, root2, oid, &len);  | 
606  |  | 
  | 
607  | 0  |   if (result == ASN1_ELEMENT_NOT_FOUND) { | 
608  | 0  |     result = GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE;  | 
609  | 0  |     goto cleanup;  | 
610  | 0  |   }  | 
611  |  |  | 
612  | 0  |   if (result != ASN1_SUCCESS) { | 
613  | 0  |     gnutls_assert();  | 
614  | 0  |     result = _gnutls_asn2err(result);  | 
615  | 0  |     goto cleanup;  | 
616  | 0  |   }  | 
617  |  |  | 
618  |  |   /* Not encrypted Bag  | 
619  |  |    */  | 
620  |  |  | 
621  | 0  |   snprintf(root2, sizeof(root2), "?%d.content", indx + 1);  | 
622  |  | 
  | 
623  | 0  |   if (strcmp(oid, DATA_OID) == 0) { | 
624  | 0  |     result = _parse_safe_contents(c2, root2, bag);  | 
625  | 0  |     goto cleanup;  | 
626  | 0  |   }  | 
627  |  |  | 
628  |  |   /* ENC_DATA_OID needs decryption */  | 
629  |  |  | 
630  | 0  |   result = _gnutls_x509_read_value(c2, root2, &bag->element[0].data);  | 
631  | 0  |   if (result < 0) { | 
632  | 0  |     gnutls_assert();  | 
633  | 0  |     goto cleanup;  | 
634  | 0  |   }  | 
635  |  |  | 
636  | 0  |   bag->element[0].type = GNUTLS_BAG_ENCRYPTED;  | 
637  | 0  |   bag->bag_elements = 1;  | 
638  |  | 
  | 
639  | 0  |   result = 0;  | 
640  |  | 
  | 
641  | 0  | cleanup:  | 
642  | 0  |   if (c2)  | 
643  | 0  |     asn1_delete_structure(&c2);  | 
644  | 0  |   return result;  | 
645  | 0  | }  | 
646  |  |  | 
647  |  | /* Creates an empty PFX structure for the PKCS12 structure.  | 
648  |  |  */  | 
649  |  | static int create_empty_pfx(asn1_node pkcs12)  | 
650  | 0  | { | 
651  | 0  |   uint8_t three = 3;  | 
652  | 0  |   int result;  | 
653  | 0  |   asn1_node c2 = NULL;  | 
654  |  |  | 
655  |  |   /* Use version 3  | 
656  |  |    */  | 
657  | 0  |   result = asn1_write_value(pkcs12, "version", &three, 1);  | 
658  | 0  |   if (result != ASN1_SUCCESS) { | 
659  | 0  |     gnutls_assert();  | 
660  | 0  |     result = _gnutls_asn2err(result);  | 
661  | 0  |     goto cleanup;  | 
662  | 0  |   }  | 
663  |  |  | 
664  |  |   /* Write the content type of the data  | 
665  |  |    */  | 
666  | 0  |   result = asn1_write_value(pkcs12, "authSafe.contentType", DATA_OID, 1);  | 
667  | 0  |   if (result != ASN1_SUCCESS) { | 
668  | 0  |     gnutls_assert();  | 
669  | 0  |     result = _gnutls_asn2err(result);  | 
670  | 0  |     goto cleanup;  | 
671  | 0  |   }  | 
672  |  |  | 
673  |  |   /* Check if the authenticatedSafe content is empty, and encode a  | 
674  |  |    * null one in that case.  | 
675  |  |    */  | 
676  |  |  | 
677  | 0  |   if ((result = asn1_create_element(_gnutls_get_pkix(),  | 
678  | 0  |             "PKIX1.pkcs-12-AuthenticatedSafe",  | 
679  | 0  |             &c2)) != ASN1_SUCCESS) { | 
680  | 0  |     gnutls_assert();  | 
681  | 0  |     result = _gnutls_asn2err(result);  | 
682  | 0  |     goto cleanup;  | 
683  | 0  |   }  | 
684  |  |  | 
685  | 0  |   result = _gnutls_x509_der_encode_and_copy(c2, "", pkcs12,  | 
686  | 0  |               "authSafe.content", 1);  | 
687  | 0  |   if (result < 0) { | 
688  | 0  |     gnutls_assert();  | 
689  | 0  |     goto cleanup;  | 
690  | 0  |   }  | 
691  | 0  |   asn1_delete_structure(&c2);  | 
692  |  | 
  | 
693  | 0  |   return 0;  | 
694  |  |  | 
695  | 0  | cleanup:  | 
696  | 0  |   asn1_delete_structure(&c2);  | 
697  | 0  |   return result;  | 
698  | 0  | }  | 
699  |  |  | 
700  |  | /**  | 
701  |  |  * gnutls_pkcs12_set_bag:  | 
702  |  |  * @pkcs12: should contain a gnutls_pkcs12_t type  | 
703  |  |  * @bag: An initialized bag  | 
704  |  |  *  | 
705  |  |  * This function will insert a Bag into the PKCS12 structure.  | 
706  |  |  *  | 
707  |  |  * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a  | 
708  |  |  *   negative error value.  | 
709  |  |  **/  | 
710  |  | int gnutls_pkcs12_set_bag(gnutls_pkcs12_t pkcs12, gnutls_pkcs12_bag_t bag)  | 
711  | 0  | { | 
712  | 0  |   asn1_node c2 = NULL;  | 
713  | 0  |   asn1_node safe_cont = NULL;  | 
714  | 0  |   int result;  | 
715  | 0  |   int enc = 0, dum = 1;  | 
716  | 0  |   char null;  | 
717  |  | 
  | 
718  | 0  |   if (pkcs12 == NULL) { | 
719  | 0  |     gnutls_assert();  | 
720  | 0  |     return GNUTLS_E_INVALID_REQUEST;  | 
721  | 0  |   }  | 
722  |  |  | 
723  |  |   /* Step 1. Check if the pkcs12 structure is empty. In that  | 
724  |  |    * case generate an empty PFX.  | 
725  |  |    */  | 
726  | 0  |   result = asn1_read_value(pkcs12->pkcs12, "authSafe.content", &null,  | 
727  | 0  |          &dum);  | 
728  | 0  |   if (result == ASN1_VALUE_NOT_FOUND) { | 
729  | 0  |     result = create_empty_pfx(pkcs12->pkcs12);  | 
730  | 0  |     if (result < 0) { | 
731  | 0  |       gnutls_assert();  | 
732  | 0  |       return result;  | 
733  | 0  |     }  | 
734  | 0  |   }  | 
735  |  |  | 
736  |  |   /* Step 2. decode the authenticatedSafe.  | 
737  |  |    */  | 
738  | 0  |   result = _decode_pkcs12_auth_safe(pkcs12->pkcs12, &c2, NULL);  | 
739  | 0  |   if (result < 0) { | 
740  | 0  |     gnutls_assert();  | 
741  | 0  |     return result;  | 
742  | 0  |   }  | 
743  |  |  | 
744  |  |   /* Step 3. Encode the bag elements into a SafeContents   | 
745  |  |    * structure.  | 
746  |  |    */  | 
747  | 0  |   result = _pkcs12_encode_safe_contents(bag, &safe_cont, &enc);  | 
748  | 0  |   if (result < 0) { | 
749  | 0  |     gnutls_assert();  | 
750  | 0  |     return result;  | 
751  | 0  |   }  | 
752  |  |  | 
753  |  |   /* Step 4. Insert the encoded SafeContents into the AuthenticatedSafe  | 
754  |  |    * structure.  | 
755  |  |    */  | 
756  | 0  |   result = asn1_write_value(c2, "", "NEW", 1);  | 
757  | 0  |   if (result != ASN1_SUCCESS) { | 
758  | 0  |     gnutls_assert();  | 
759  | 0  |     result = _gnutls_asn2err(result);  | 
760  | 0  |     goto cleanup;  | 
761  | 0  |   }  | 
762  |  |  | 
763  | 0  |   if (enc)  | 
764  | 0  |     result = asn1_write_value(c2, "?LAST.contentType", ENC_DATA_OID,  | 
765  | 0  |             1);  | 
766  | 0  |   else  | 
767  | 0  |     result = asn1_write_value(c2, "?LAST.contentType", DATA_OID, 1);  | 
768  | 0  |   if (result != ASN1_SUCCESS) { | 
769  | 0  |     gnutls_assert();  | 
770  | 0  |     result = _gnutls_asn2err(result);  | 
771  | 0  |     goto cleanup;  | 
772  | 0  |   }  | 
773  |  |  | 
774  | 0  |   if (enc) { | 
775  |  |     /* Encrypted packets are written directly.  | 
776  |  |      */  | 
777  | 0  |     result = asn1_write_value(c2, "?LAST.content",  | 
778  | 0  |             bag->element[0].data.data,  | 
779  | 0  |             bag->element[0].data.size);  | 
780  | 0  |     if (result != ASN1_SUCCESS) { | 
781  | 0  |       gnutls_assert();  | 
782  | 0  |       result = _gnutls_asn2err(result);  | 
783  | 0  |       goto cleanup;  | 
784  | 0  |     }  | 
785  | 0  |   } else { | 
786  | 0  |     result = _gnutls_x509_der_encode_and_copy(safe_cont, "", c2,  | 
787  | 0  |                 "?LAST.content", 1);  | 
788  | 0  |     if (result < 0) { | 
789  | 0  |       gnutls_assert();  | 
790  | 0  |       goto cleanup;  | 
791  | 0  |     }  | 
792  | 0  |   }  | 
793  |  |  | 
794  | 0  |   asn1_delete_structure(&safe_cont);  | 
795  |  |  | 
796  |  |   /* Step 5. Re-encode and copy the AuthenticatedSafe into the pkcs12  | 
797  |  |    * structure.  | 
798  |  |    */  | 
799  | 0  |   result = _gnutls_x509_der_encode_and_copy(c2, "", pkcs12->pkcs12,  | 
800  | 0  |               "authSafe.content", 1);  | 
801  | 0  |   if (result < 0) { | 
802  | 0  |     gnutls_assert();  | 
803  | 0  |     goto cleanup;  | 
804  | 0  |   }  | 
805  |  |  | 
806  | 0  |   asn1_delete_structure(&c2);  | 
807  |  | 
  | 
808  | 0  |   return 0;  | 
809  |  |  | 
810  | 0  | cleanup:  | 
811  | 0  |   asn1_delete_structure(&c2);  | 
812  | 0  |   asn1_delete_structure(&safe_cont);  | 
813  | 0  |   return result;  | 
814  | 0  | }  | 
815  |  |  | 
816  |  | #if ENABLE_GOST  | 
817  |  | /*  | 
818  |  |  * Russian differs from PKCS#12 here. It described proprietary way  | 
819  |  |  * to obtain MAC key instead of using standard mechanism.  | 
820  |  |  *  | 
821  |  |  * See https://wwwold.tc26.ru/standard/rs/%D0%A0%2050.1.112-2016.pdf  | 
822  |  |  * section 5.  | 
823  |  |  */  | 
824  |  | static int  | 
825  |  | _gnutls_pkcs12_gost_string_to_key(gnutls_mac_algorithm_t algo,  | 
826  |  |           const uint8_t *salt, unsigned int salt_size,  | 
827  |  |           unsigned int iter, const char *pass,  | 
828  |  |           unsigned int req_keylen, uint8_t *keybuf)  | 
829  | 0  | { | 
830  | 0  |   uint8_t temp[96];  | 
831  | 0  |   size_t temp_len = sizeof(temp);  | 
832  | 0  |   gnutls_datum_t key;  | 
833  | 0  |   gnutls_datum_t _salt;  | 
834  | 0  |   int ret;  | 
835  |  | 
  | 
836  | 0  |   if (iter == 0)  | 
837  | 0  |     return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);  | 
838  |  |  | 
839  | 0  |   key.data = (void *)pass;  | 
840  | 0  |   key.size = pass ? strlen(pass) : 0;  | 
841  |  | 
  | 
842  | 0  |   _salt.data = (void *)salt;  | 
843  | 0  |   _salt.size = salt_size;  | 
844  |  | 
  | 
845  | 0  |   ret = gnutls_pbkdf2(algo, &key, &_salt, iter, temp, temp_len);  | 
846  | 0  |   if (ret < 0)  | 
847  | 0  |     return gnutls_assert_val(ret);  | 
848  |  |  | 
849  | 0  |   memcpy(keybuf, temp + temp_len - req_keylen, req_keylen);  | 
850  |  | 
  | 
851  | 0  |   return 0;  | 
852  | 0  | }  | 
853  |  | #endif  | 
854  |  |  | 
855  |  | static int generate_mac_pbmac1(gnutls_mac_algorithm_t mac,  | 
856  |  |              const gnutls_datum_t *key,  | 
857  |  |              const struct pbkdf2_params *params,  | 
858  |  |              const gnutls_datum_t *data, asn1_node pkcs12)  | 
859  | 0  | { | 
860  | 0  |   uint8_t mac_output_data[MAX_HASH_SIZE];  | 
861  | 0  |   gnutls_datum_t mac_output;  | 
862  | 0  |   int result;  | 
863  |  | 
  | 
864  | 0  |   result = _gnutls_pbmac1(mac, key, params, data, mac_output_data);  | 
865  | 0  |   if (result < 0) { | 
866  | 0  |     gnutls_assert();  | 
867  | 0  |     return result;  | 
868  | 0  |   }  | 
869  |  |  | 
870  | 0  |   mac_output.data = mac_output_data;  | 
871  | 0  |   mac_output.size = params->key_size;  | 
872  |  | 
  | 
873  | 0  |   result = _gnutls_x509_write_value(pkcs12, "macData.mac.digest",  | 
874  | 0  |             &mac_output);  | 
875  | 0  |   if (result < 0) { | 
876  | 0  |     gnutls_assert();  | 
877  | 0  |     return result;  | 
878  | 0  |   }  | 
879  |  |  | 
880  | 0  |   result = asn1_write_value(  | 
881  | 0  |     pkcs12, "macData.mac.digestAlgorithm.algorithm", PBMAC1_OID, 1);  | 
882  | 0  |   if (result != ASN1_SUCCESS) { | 
883  | 0  |     gnutls_assert();  | 
884  | 0  |     return _gnutls_asn2err(result);  | 
885  | 0  |   }  | 
886  |  |  | 
887  | 0  |   result = _gnutls_write_pbmac1_params(  | 
888  | 0  |     pkcs12, params, mac, "macData.mac.digestAlgorithm.parameters");  | 
889  | 0  |   if (result < 0) { | 
890  | 0  |     gnutls_assert();  | 
891  | 0  |     return result;  | 
892  | 0  |   }  | 
893  |  |  | 
894  | 0  |   return 0;  | 
895  | 0  | }  | 
896  |  |  | 
897  |  | static int generate_mac_pkcs12(const mac_entry_st *me,  | 
898  |  |              const gnutls_datum_t *key,  | 
899  |  |              const gnutls_datum_t *salt, unsigned iter_count,  | 
900  |  |              const gnutls_datum_t *data, asn1_node pkcs12)  | 
901  | 0  | { | 
902  | 0  |   gnutls_hmac_hd_t hd;  | 
903  | 0  |   uint8_t mac_key_data[MAX_HASH_SIZE];  | 
904  | 0  |   size_t mac_key_size = _gnutls_mac_get_algo_len(me);  | 
905  | 0  |   uint8_t mac_data[MAX_HASH_SIZE];  | 
906  | 0  |   gnutls_datum_t mac;  | 
907  | 0  |   int result;  | 
908  |  | 
  | 
909  | 0  | #if ENABLE_GOST  | 
910  | 0  |   if (me->id == GNUTLS_MAC_GOSTR_94 ||  | 
911  | 0  |       me->id == GNUTLS_MAC_STREEBOG_256 ||  | 
912  | 0  |       me->id == GNUTLS_MAC_STREEBOG_512) { | 
913  | 0  |     mac_key_size = 32;  | 
914  | 0  |     result = _gnutls_pkcs12_gost_string_to_key(  | 
915  | 0  |       me->id, salt->data, salt->size, iter_count,  | 
916  | 0  |       (const char *)key->data, mac_key_size, mac_key_data);  | 
917  | 0  |   } else  | 
918  | 0  | #endif  | 
919  | 0  |     result = _gnutls_pkcs12_string_to_key(  | 
920  | 0  |       me, 3 /*MAC*/, salt->data, salt->size, iter_count,  | 
921  | 0  |       (const char *)key->data, mac_key_size, mac_key_data);  | 
922  | 0  |   if (result < 0) { | 
923  | 0  |     gnutls_assert();  | 
924  | 0  |     return result;  | 
925  | 0  |   }  | 
926  |  |  | 
927  |  |   /* MAC the data.  | 
928  |  |    */  | 
929  | 0  |   result = gnutls_hmac_init(&hd, me->id, mac_key_data, mac_key_size);  | 
930  | 0  |   if (result < 0) { | 
931  | 0  |     gnutls_assert();  | 
932  | 0  |     return result;  | 
933  | 0  |   }  | 
934  |  |  | 
935  | 0  |   gnutls_hmac(hd, data->data, data->size);  | 
936  |  | 
  | 
937  | 0  |   gnutls_hmac_deinit(hd, mac_data);  | 
938  |  | 
  | 
939  | 0  |   mac.data = mac_data;  | 
940  | 0  |   mac.size = _gnutls_mac_get_algo_len(me);  | 
941  |  | 
  | 
942  | 0  |   result = _gnutls_x509_write_value(pkcs12, "macData.mac.digest", &mac);  | 
943  | 0  |   if (result < 0) { | 
944  | 0  |     gnutls_assert();  | 
945  | 0  |     return result;  | 
946  | 0  |   }  | 
947  |  |  | 
948  | 0  |   result = asn1_write_value(  | 
949  | 0  |     pkcs12, "macData.mac.digestAlgorithm.algorithm", me->oid, 1);  | 
950  | 0  |   if (result != ASN1_SUCCESS) { | 
951  | 0  |     gnutls_assert();  | 
952  | 0  |     return _gnutls_asn2err(result);  | 
953  | 0  |   }  | 
954  |  |  | 
955  | 0  |   result = asn1_write_value(  | 
956  | 0  |     pkcs12, "macData.mac.digestAlgorithm.parameters", NULL, 0);  | 
957  | 0  |   if (result != ASN1_SUCCESS && result != ASN1_ELEMENT_NOT_FOUND) { | 
958  | 0  |     gnutls_assert();  | 
959  | 0  |     return _gnutls_asn2err(result);  | 
960  | 0  |   }  | 
961  |  |  | 
962  |  |   /* _gnutls_pkcs12_string_to_key is not a FIPS approved operation */  | 
963  | 0  |   _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_NOT_APPROVED);  | 
964  |  | 
  | 
965  | 0  |   return 0;  | 
966  | 0  | }  | 
967  |  |  | 
968  |  | /**  | 
969  |  |  * gnutls_pkcs12_generate_mac3:  | 
970  |  |  * @pkcs12: A pkcs12 type  | 
971  |  |  * @mac: the MAC algorithm to use  | 
972  |  |  * @pass: The password for the MAC  | 
973  |  |  * @flags: an ORed sequence of gnutls_pkcs12_flags_t  | 
974  |  |  *  | 
975  |  |  * This function will generate a MAC for the PKCS12 structure.  | 
976  |  |  *  | 
977  |  |  * If @flags contains %GNUTLS_PKCS12_USE_PBMAC1, it uses PBMAC1 key  | 
978  |  |  * derivation function instead of the PKCS#12 one.  | 
979  |  |  *  | 
980  |  |  * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a  | 
981  |  |  *   negative error value.  | 
982  |  |  **/  | 
983  |  | int gnutls_pkcs12_generate_mac3(gnutls_pkcs12_t pkcs12,  | 
984  |  |         gnutls_mac_algorithm_t mac, const char *pass,  | 
985  |  |         unsigned int flags)  | 
986  | 0  | { | 
987  | 0  |   uint8_t salt_data[8];  | 
988  | 0  |   gnutls_datum_t salt, key;  | 
989  | 0  |   const int iter_count = PKCS12_ITER_COUNT;  | 
990  | 0  |   int result;  | 
991  | 0  |   gnutls_datum_t data = { NULL, 0 }; | 
992  | 0  |   const mac_entry_st *me = mac_to_entry(mac);  | 
993  |  | 
  | 
994  | 0  |   if (pkcs12 == NULL || me == NULL)  | 
995  | 0  |     return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);  | 
996  |  |  | 
997  | 0  |   if (me->oid == NULL)  | 
998  | 0  |     return gnutls_assert_val(GNUTLS_E_UNIMPLEMENTED_FEATURE);  | 
999  |  |  | 
1000  |  |   /* Enable PBMAC1 by default in FIPS mode; otherwise the MAC  | 
1001  |  |    * calculation will be FIPS non-compliant.  | 
1002  |  |    */  | 
1003  | 0  |   if (_gnutls_fips_mode_enabled())  | 
1004  | 0  |     flags |= GNUTLS_PKCS12_USE_PBMAC1;  | 
1005  |  |  | 
1006  |  |   /* Generate the salt.  | 
1007  |  |    */  | 
1008  | 0  |   salt.data = salt_data;  | 
1009  | 0  |   salt.size = sizeof(salt_data);  | 
1010  |  | 
  | 
1011  | 0  |   result = gnutls_rnd(GNUTLS_RND_NONCE, salt.data, salt.size);  | 
1012  | 0  |   if (result < 0) { | 
1013  | 0  |     gnutls_assert();  | 
1014  | 0  |     return result;  | 
1015  | 0  |   }  | 
1016  |  |  | 
1017  |  |   /* Write the salt into the structure.  | 
1018  |  |    */  | 
1019  | 0  |   result = _gnutls_x509_write_value(pkcs12->pkcs12, "macData.macSalt",  | 
1020  | 0  |             &salt);  | 
1021  | 0  |   if (result < 0) { | 
1022  | 0  |     gnutls_assert();  | 
1023  | 0  |     goto cleanup;  | 
1024  | 0  |   }  | 
1025  |  |  | 
1026  |  |   /* Write the iteration count into the structure.  | 
1027  |  |    */  | 
1028  | 0  |   result = _gnutls_x509_write_uint32(pkcs12->pkcs12, "macData.iterations",  | 
1029  | 0  |              iter_count);  | 
1030  | 0  |   if (result < 0) { | 
1031  | 0  |     gnutls_assert();  | 
1032  | 0  |     goto cleanup;  | 
1033  | 0  |   }  | 
1034  |  |  | 
1035  |  |   /* Get the data to be MACed.  | 
1036  |  |    */  | 
1037  | 0  |   result = _decode_pkcs12_auth_safe(pkcs12->pkcs12, NULL, &data);  | 
1038  | 0  |   if (result < 0) { | 
1039  | 0  |     gnutls_assert();  | 
1040  | 0  |     goto cleanup;  | 
1041  | 0  |   }  | 
1042  |  |  | 
1043  | 0  |   key.data = (void *)pass;  | 
1044  | 0  |   key.size = strlen(pass);  | 
1045  |  | 
  | 
1046  | 0  |   if (flags & GNUTLS_PKCS12_USE_PBMAC1) { | 
1047  | 0  |     struct pbkdf2_params kdf_params;  | 
1048  |  | 
  | 
1049  | 0  |     memcpy(kdf_params.salt, salt.data, salt.size);  | 
1050  | 0  |     kdf_params.salt_size = salt.size;  | 
1051  | 0  |     kdf_params.iter_count = iter_count;  | 
1052  | 0  |     kdf_params.key_size = _gnutls_mac_get_algo_len(me);  | 
1053  | 0  |     kdf_params.mac = GNUTLS_MAC_SHA256;  | 
1054  |  | 
  | 
1055  | 0  |     result = generate_mac_pbmac1(me->id, &key, &kdf_params, &data,  | 
1056  | 0  |                pkcs12->pkcs12);  | 
1057  | 0  |   } else  | 
1058  | 0  |     result = generate_mac_pkcs12(me, &key, &salt, iter_count, &data,  | 
1059  | 0  |                pkcs12->pkcs12);  | 
1060  |  | 
  | 
1061  | 0  | cleanup:  | 
1062  | 0  |   if (result < 0)  | 
1063  | 0  |     _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);  | 
1064  | 0  |   _gnutls_free_datum(&data);  | 
1065  | 0  |   return result;  | 
1066  | 0  | }  | 
1067  |  |  | 
1068  |  | /**  | 
1069  |  |  * gnutls_pkcs12_generate_mac2:  | 
1070  |  |  * @pkcs12: A pkcs12 type  | 
1071  |  |  * @mac: the MAC algorithm to use  | 
1072  |  |  * @pass: The password for the MAC  | 
1073  |  |  *  | 
1074  |  |  * This function will generate a MAC for the PKCS12 structure.  | 
1075  |  |  *  | 
1076  |  |  * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a  | 
1077  |  |  *   negative error value.  | 
1078  |  |  **/  | 
1079  |  | int gnutls_pkcs12_generate_mac2(gnutls_pkcs12_t pkcs12,  | 
1080  |  |         gnutls_mac_algorithm_t mac, const char *pass)  | 
1081  | 0  | { | 
1082  | 0  |   return gnutls_pkcs12_generate_mac3(pkcs12, mac, pass, 0);  | 
1083  | 0  | }  | 
1084  |  |  | 
1085  |  | /**  | 
1086  |  |  * gnutls_pkcs12_generate_mac:  | 
1087  |  |  * @pkcs12: A pkcs12 type  | 
1088  |  |  * @pass: The password for the MAC  | 
1089  |  |  *  | 
1090  |  |  * This function will generate a MAC for the PKCS12 structure.  | 
1091  |  |  *  | 
1092  |  |  * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a  | 
1093  |  |  *   negative error value.  | 
1094  |  |  **/  | 
1095  |  | int gnutls_pkcs12_generate_mac(gnutls_pkcs12_t pkcs12, const char *pass)  | 
1096  | 0  | { | 
1097  | 0  |   return gnutls_pkcs12_generate_mac2(pkcs12, GNUTLS_MAC_SHA256, pass);  | 
1098  | 0  | }  | 
1099  |  |  | 
1100  |  | static int pkcs12_verify_mac_pbmac1(gnutls_pkcs12_t pkcs12, const char *pass)  | 
1101  | 0  | { | 
1102  | 0  |   int result;  | 
1103  | 0  |   int len;  | 
1104  | 0  |   gnutls_datum_t params = { NULL, 0 }, data = { NULL, 0 }; | 
1105  | 0  |   gnutls_datum_t key;  | 
1106  | 0  |   uint8_t mac_output[MAX_HASH_SIZE];  | 
1107  | 0  |   uint8_t mac_output_orig[MAX_HASH_SIZE];  | 
1108  | 0  |   struct pbkdf2_params kdf_params;  | 
1109  | 0  |   gnutls_mac_algorithm_t algo = GNUTLS_MAC_UNKNOWN;  | 
1110  | 0  |   const mac_entry_st *me;  | 
1111  |  | 
  | 
1112  | 0  |   result = _gnutls_x509_read_value(  | 
1113  | 0  |     pkcs12->pkcs12, "macData.mac.digestAlgorithm.parameters",  | 
1114  | 0  |     ¶ms);  | 
1115  | 0  |   if (result < 0) { | 
1116  | 0  |     return gnutls_assert_val(result);  | 
1117  | 0  |   }  | 
1118  |  |  | 
1119  | 0  |   memset(&kdf_params, 0, sizeof(kdf_params));  | 
1120  | 0  |   result = _gnutls_read_pbmac1_params(params.data, params.size,  | 
1121  | 0  |               &kdf_params, &algo);  | 
1122  | 0  |   if (result < 0) { | 
1123  | 0  |     gnutls_assert();  | 
1124  | 0  |     goto cleanup;  | 
1125  | 0  |   }  | 
1126  |  |  | 
1127  | 0  |   me = mac_to_entry(algo);  | 
1128  | 0  |   if (unlikely(me == NULL)) { | 
1129  | 0  |     gnutls_assert();  | 
1130  | 0  |     result = GNUTLS_E_UNKNOWN_HASH_ALGORITHM;  | 
1131  | 0  |     goto cleanup;  | 
1132  | 0  |   }  | 
1133  |  |  | 
1134  |  |   /* Get the data to be MACed  | 
1135  |  |    */  | 
1136  | 0  |   result = _decode_pkcs12_auth_safe(pkcs12->pkcs12, NULL, &data);  | 
1137  | 0  |   if (result < 0) { | 
1138  | 0  |     gnutls_assert();  | 
1139  | 0  |     goto cleanup;  | 
1140  | 0  |   }  | 
1141  |  |  | 
1142  | 0  |   key.data = (void *)pass;  | 
1143  | 0  |   key.size = strlen(pass);  | 
1144  |  | 
  | 
1145  | 0  |   result = _gnutls_pbmac1(me->id, &key, &kdf_params, &data, mac_output);  | 
1146  | 0  |   if (result < 0) { | 
1147  | 0  |     gnutls_assert();  | 
1148  | 0  |     goto cleanup;  | 
1149  | 0  |   }  | 
1150  |  |  | 
1151  | 0  |   len = sizeof(mac_output_orig);  | 
1152  | 0  |   result = asn1_read_value(pkcs12->pkcs12, "macData.mac.digest",  | 
1153  | 0  |          mac_output_orig, &len);  | 
1154  | 0  |   if (result != ASN1_SUCCESS) { | 
1155  | 0  |     gnutls_assert();  | 
1156  | 0  |     result = _gnutls_asn2err(result);  | 
1157  | 0  |     goto cleanup;  | 
1158  | 0  |   }  | 
1159  |  |  | 
1160  | 0  |   if ((unsigned)len != _gnutls_mac_get_algo_len(me) ||  | 
1161  | 0  |       memcmp(mac_output_orig, mac_output, len) != 0) { | 
1162  | 0  |     gnutls_assert();  | 
1163  | 0  |     result = GNUTLS_E_MAC_VERIFY_FAILED;  | 
1164  | 0  |     goto cleanup;  | 
1165  | 0  |   }  | 
1166  |  |  | 
1167  | 0  | cleanup:  | 
1168  | 0  |   _gnutls_free_datum(¶ms);  | 
1169  | 0  |   _gnutls_free_datum(&data);  | 
1170  | 0  |   return result;  | 
1171  | 0  | }  | 
1172  |  |  | 
1173  |  | static int pkcs12_verify_mac_pkcs12(gnutls_pkcs12_t pkcs12,  | 
1174  |  |             gnutls_mac_algorithm_t algo,  | 
1175  |  |             const char *pass)  | 
1176  | 0  | { | 
1177  | 0  |   const mac_entry_st *entry;  | 
1178  | 0  |   uint8_t key[MAX_HASH_SIZE];  | 
1179  | 0  |   uint8_t mac_output[MAX_HASH_SIZE];  | 
1180  | 0  |   uint8_t mac_output_orig[MAX_HASH_SIZE];  | 
1181  | 0  |   gnutls_datum_t tmp = { NULL, 0 }, salt = { NULL, 0 }; | 
1182  | 0  |   unsigned mac_len, key_len;  | 
1183  | 0  |   int len;  | 
1184  | 0  |   gnutls_hmac_hd_t td1;  | 
1185  | 0  |   unsigned iter_count;  | 
1186  | 0  | #if ENABLE_GOST  | 
1187  | 0  |   int gost_retry = 0;  | 
1188  | 0  | #endif  | 
1189  | 0  |   int result;  | 
1190  |  | 
  | 
1191  | 0  |   entry = mac_to_entry(algo);  | 
1192  | 0  |   if (unlikely(entry == NULL)) { | 
1193  | 0  |     return gnutls_assert_val(GNUTLS_E_UNKNOWN_HASH_ALGORITHM);  | 
1194  | 0  |   }  | 
1195  |  |  | 
1196  | 0  |   mac_len = _gnutls_mac_get_algo_len(entry);  | 
1197  | 0  |   key_len = mac_len;  | 
1198  |  |  | 
1199  |  |   /* Read the iterations from the structure.  | 
1200  |  |    */  | 
1201  | 0  |   result = _gnutls_x509_read_uint(pkcs12->pkcs12, "macData.iterations",  | 
1202  | 0  |           &iter_count);  | 
1203  | 0  |   if (result < 0) { | 
1204  | 0  |     iter_count = 1; /* the default */  | 
1205  | 0  |   }  | 
1206  |  |  | 
1207  |  |   /* Read the salt from the structure.  | 
1208  |  |    */  | 
1209  | 0  |   result = _gnutls_x509_read_null_value(pkcs12->pkcs12, "macData.macSalt",  | 
1210  | 0  |                 &salt);  | 
1211  | 0  |   if (result < 0) { | 
1212  | 0  |     gnutls_assert();  | 
1213  | 0  |     goto cleanup;  | 
1214  | 0  |   }  | 
1215  |  |  | 
1216  |  |   /* Generate the key.  | 
1217  |  |    */  | 
1218  | 0  |   result = _gnutls_pkcs12_string_to_key(entry, 3 /*MAC*/, salt.data,  | 
1219  | 0  |                 salt.size, iter_count, pass,  | 
1220  | 0  |                 key_len, key);  | 
1221  | 0  |   if (result < 0) { | 
1222  | 0  |     gnutls_assert();  | 
1223  | 0  |     goto cleanup;  | 
1224  | 0  |   }  | 
1225  |  |  | 
1226  |  |   /* Get the data to be MACed.  | 
1227  |  |    */  | 
1228  | 0  |   result = _decode_pkcs12_auth_safe(pkcs12->pkcs12, NULL, &tmp);  | 
1229  | 0  |   if (result < 0) { | 
1230  | 0  |     gnutls_assert();  | 
1231  | 0  |     goto cleanup;  | 
1232  | 0  |   }  | 
1233  | 0  | #if ENABLE_GOST  | 
1234  |  |   /* GOST PKCS#12 files use either PKCS#12 scheme or proprietary  | 
1235  |  |    * HMAC-based scheme to generate MAC key. */  | 
1236  | 0  | pkcs12_try_gost:  | 
1237  | 0  | #endif  | 
1238  |  |  | 
1239  |  |   /* MAC the data.  | 
1240  |  |    */  | 
1241  | 0  |   result = gnutls_hmac_init(&td1, entry->id, key, key_len);  | 
1242  | 0  |   if (result < 0) { | 
1243  | 0  |     gnutls_assert();  | 
1244  | 0  |     goto cleanup;  | 
1245  | 0  |   }  | 
1246  |  |  | 
1247  | 0  |   gnutls_hmac(td1, tmp.data, tmp.size);  | 
1248  |  | 
  | 
1249  | 0  |   gnutls_hmac_deinit(td1, mac_output);  | 
1250  |  | 
  | 
1251  | 0  |   len = sizeof(mac_output_orig);  | 
1252  | 0  |   result = asn1_read_value(pkcs12->pkcs12, "macData.mac.digest",  | 
1253  | 0  |          mac_output_orig, &len);  | 
1254  | 0  |   if (result != ASN1_SUCCESS) { | 
1255  | 0  |     gnutls_assert();  | 
1256  | 0  |     result = _gnutls_asn2err(result);  | 
1257  | 0  |     goto cleanup;  | 
1258  | 0  |   }  | 
1259  |  |  | 
1260  | 0  |   if ((unsigned)len != mac_len ||  | 
1261  | 0  |       memcmp(mac_output_orig, mac_output, len) != 0) { | 
1262  | 0  | #if ENABLE_GOST  | 
1263  |  |     /* It is possible that GOST files use proprietary  | 
1264  |  |      * key generation scheme */  | 
1265  | 0  |     if (!gost_retry && (algo == GNUTLS_MAC_GOSTR_94 ||  | 
1266  | 0  |             algo == GNUTLS_MAC_STREEBOG_256 ||  | 
1267  | 0  |             algo == GNUTLS_MAC_STREEBOG_512)) { | 
1268  | 0  |       gost_retry = 1;  | 
1269  | 0  |       key_len = 32;  | 
1270  | 0  |       result = _gnutls_pkcs12_gost_string_to_key(  | 
1271  | 0  |         algo, salt.data, salt.size, iter_count, pass,  | 
1272  | 0  |         key_len, key);  | 
1273  | 0  |       if (result < 0) { | 
1274  | 0  |         gnutls_assert();  | 
1275  | 0  |         goto cleanup;  | 
1276  | 0  |       }  | 
1277  |  |  | 
1278  | 0  |       goto pkcs12_try_gost;  | 
1279  | 0  |     }  | 
1280  | 0  | #endif  | 
1281  |  |  | 
1282  | 0  |     gnutls_assert();  | 
1283  | 0  |     result = GNUTLS_E_MAC_VERIFY_FAILED;  | 
1284  | 0  |     goto cleanup;  | 
1285  | 0  |   }  | 
1286  |  |  | 
1287  |  |   /* _gnutls_pkcs12_string_to_key is not a FIPS approved operation */  | 
1288  | 0  |   _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_NOT_APPROVED);  | 
1289  | 0  |   result = 0;  | 
1290  | 0  | cleanup:  | 
1291  | 0  |   _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);  | 
1292  | 0  |   _gnutls_free_datum(&tmp);  | 
1293  | 0  |   _gnutls_free_datum(&salt);  | 
1294  | 0  |   return result;  | 
1295  | 0  | }  | 
1296  |  |  | 
1297  |  | /**  | 
1298  |  |  * gnutls_pkcs12_verify_mac:  | 
1299  |  |  * @pkcs12: should contain a gnutls_pkcs12_t type  | 
1300  |  |  * @pass: The password for the MAC  | 
1301  |  |  *  | 
1302  |  |  * This function will verify the MAC for the PKCS12 structure.  | 
1303  |  |  *  | 
1304  |  |  * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a  | 
1305  |  |  *   negative error value.  | 
1306  |  |  **/  | 
1307  |  | int gnutls_pkcs12_verify_mac(gnutls_pkcs12_t pkcs12, const char *pass)  | 
1308  | 0  | { | 
1309  | 0  |   char oid[MAX_OID_SIZE];  | 
1310  | 0  |   int result;  | 
1311  | 0  |   int len;  | 
1312  |  | 
  | 
1313  | 0  |   if (pkcs12 == NULL) { | 
1314  | 0  |     gnutls_assert();  | 
1315  | 0  |     return GNUTLS_E_INVALID_REQUEST;  | 
1316  | 0  |   }  | 
1317  |  |  | 
1318  | 0  |   len = sizeof(oid);  | 
1319  | 0  |   result = asn1_read_value(pkcs12->pkcs12,  | 
1320  | 0  |          "macData.mac.digestAlgorithm.algorithm", oid,  | 
1321  | 0  |          &len);  | 
1322  | 0  |   if (result != ASN1_SUCCESS) { | 
1323  | 0  |     gnutls_assert();  | 
1324  | 0  |     return _gnutls_asn2err(result);  | 
1325  | 0  |   }  | 
1326  |  |  | 
1327  | 0  |   if (strcmp(oid, PBMAC1_OID) == 0) { | 
1328  | 0  |     return pkcs12_verify_mac_pbmac1(pkcs12, pass);  | 
1329  | 0  |   } else { | 
1330  | 0  |     gnutls_mac_algorithm_t algo;  | 
1331  |  | 
  | 
1332  | 0  |     algo = DIG_TO_MAC(gnutls_oid_to_digest(oid));  | 
1333  | 0  |     return pkcs12_verify_mac_pkcs12(pkcs12, algo, pass);  | 
1334  | 0  |   }  | 
1335  | 0  | }  | 
1336  |  |  | 
1337  |  | static int write_attributes(gnutls_pkcs12_bag_t bag, int elem, asn1_node c2,  | 
1338  |  |           const char *where)  | 
1339  | 0  | { | 
1340  | 0  |   int result;  | 
1341  | 0  |   char root[128];  | 
1342  |  |  | 
1343  |  |   /* If the bag attributes are empty, then write  | 
1344  |  |    * nothing to the attribute field.  | 
1345  |  |    */  | 
1346  | 0  |   if (bag->element[elem].friendly_name == NULL &&  | 
1347  | 0  |       bag->element[elem].local_key_id.data == NULL) { | 
1348  |  |     /* no attributes  | 
1349  |  |      */  | 
1350  | 0  |     result = asn1_write_value(c2, where, NULL, 0);  | 
1351  | 0  |     if (result != ASN1_SUCCESS) { | 
1352  | 0  |       gnutls_assert();  | 
1353  | 0  |       return _gnutls_asn2err(result);  | 
1354  | 0  |     }  | 
1355  |  |  | 
1356  | 0  |     return 0;  | 
1357  | 0  |   }  | 
1358  |  |  | 
1359  | 0  |   if (bag->element[elem].local_key_id.data != NULL) { | 
1360  |  |     /* Add a new Attribute  | 
1361  |  |      */  | 
1362  | 0  |     result = asn1_write_value(c2, where, "NEW", 1);  | 
1363  | 0  |     if (result != ASN1_SUCCESS) { | 
1364  | 0  |       gnutls_assert();  | 
1365  | 0  |       return _gnutls_asn2err(result);  | 
1366  | 0  |     }  | 
1367  |  |  | 
1368  | 0  |     _gnutls_str_cpy(root, sizeof(root), where);  | 
1369  | 0  |     _gnutls_str_cat(root, sizeof(root), ".?LAST");  | 
1370  |  | 
  | 
1371  | 0  |     result = _gnutls_x509_encode_and_write_attribute(  | 
1372  | 0  |       KEY_ID_OID, c2, root,  | 
1373  | 0  |       bag->element[elem].local_key_id.data,  | 
1374  | 0  |       bag->element[elem].local_key_id.size, 1);  | 
1375  | 0  |     if (result < 0) { | 
1376  | 0  |       gnutls_assert();  | 
1377  | 0  |       return result;  | 
1378  | 0  |     }  | 
1379  | 0  |   }  | 
1380  |  |  | 
1381  | 0  |   if (bag->element[elem].friendly_name != NULL) { | 
1382  | 0  |     uint8_t *name;  | 
1383  | 0  |     int size, i;  | 
1384  | 0  |     const char *p;  | 
1385  |  |  | 
1386  |  |     /* Add a new Attribute  | 
1387  |  |      */  | 
1388  | 0  |     result = asn1_write_value(c2, where, "NEW", 1);  | 
1389  | 0  |     if (result != ASN1_SUCCESS) { | 
1390  | 0  |       gnutls_assert();  | 
1391  | 0  |       return _gnutls_asn2err(result);  | 
1392  | 0  |     }  | 
1393  |  |  | 
1394  |  |     /* convert name to BMPString  | 
1395  |  |      */  | 
1396  | 0  |     size = strlen(bag->element[elem].friendly_name) * 2;  | 
1397  | 0  |     name = gnutls_malloc(size);  | 
1398  |  | 
  | 
1399  | 0  |     if (name == NULL) { | 
1400  | 0  |       gnutls_assert();  | 
1401  | 0  |       return GNUTLS_E_MEMORY_ERROR;  | 
1402  | 0  |     }  | 
1403  |  |  | 
1404  | 0  |     p = bag->element[elem].friendly_name;  | 
1405  | 0  |     for (i = 0; i < size; i += 2) { | 
1406  | 0  |       name[i] = 0;  | 
1407  | 0  |       name[i + 1] = *p;  | 
1408  | 0  |       p++;  | 
1409  | 0  |     }  | 
1410  |  | 
  | 
1411  | 0  |     _gnutls_str_cpy(root, sizeof(root), where);  | 
1412  | 0  |     _gnutls_str_cat(root, sizeof(root), ".?LAST");  | 
1413  |  | 
  | 
1414  | 0  |     result = _gnutls_x509_encode_and_write_attribute(  | 
1415  | 0  |       FRIENDLY_NAME_OID, c2, root, name, size, 1);  | 
1416  |  | 
  | 
1417  | 0  |     gnutls_free(name);  | 
1418  |  | 
  | 
1419  | 0  |     if (result < 0) { | 
1420  | 0  |       gnutls_assert();  | 
1421  | 0  |       return result;  | 
1422  | 0  |     }  | 
1423  | 0  |   }  | 
1424  |  |  | 
1425  | 0  |   return 0;  | 
1426  | 0  | }  | 
1427  |  |  | 
1428  |  | /* Encodes the bag into a SafeContents structure, and puts the output in  | 
1429  |  |  * the given datum. Enc is set to non-zero if the data are encrypted;  | 
1430  |  |  */  | 
1431  |  | int _pkcs12_encode_safe_contents(gnutls_pkcs12_bag_t bag, asn1_node *contents,  | 
1432  |  |          int *enc)  | 
1433  | 0  | { | 
1434  | 0  |   asn1_node c2 = NULL;  | 
1435  | 0  |   int result;  | 
1436  | 0  |   unsigned i;  | 
1437  | 0  |   const char *oid;  | 
1438  |  | 
  | 
1439  | 0  |   if (bag->element[0].type == GNUTLS_BAG_ENCRYPTED && enc) { | 
1440  | 0  |     *enc = 1;  | 
1441  | 0  |     return 0; /* ENCRYPTED BAG, do nothing. */  | 
1442  | 0  |   } else if (enc)  | 
1443  | 0  |     *enc = 0;  | 
1444  |  |  | 
1445  |  |   /* Step 1. Create the SEQUENCE.  | 
1446  |  |    */  | 
1447  |  |  | 
1448  | 0  |   if ((result = asn1_create_element(_gnutls_get_pkix(),  | 
1449  | 0  |             "PKIX1.pkcs-12-SafeContents", &c2)) !=  | 
1450  | 0  |       ASN1_SUCCESS) { | 
1451  | 0  |     gnutls_assert();  | 
1452  | 0  |     result = _gnutls_asn2err(result);  | 
1453  | 0  |     goto cleanup;  | 
1454  | 0  |   }  | 
1455  |  |  | 
1456  | 0  |   for (i = 0; i < bag->bag_elements; i++) { | 
1457  | 0  |     oid = bag_to_oid(bag->element[i].type);  | 
1458  | 0  |     if (oid == NULL) { | 
1459  | 0  |       gnutls_assert();  | 
1460  | 0  |       continue;  | 
1461  | 0  |     }  | 
1462  |  |  | 
1463  | 0  |     result = asn1_write_value(c2, "", "NEW", 1);  | 
1464  | 0  |     if (result != ASN1_SUCCESS) { | 
1465  | 0  |       gnutls_assert();  | 
1466  | 0  |       result = _gnutls_asn2err(result);  | 
1467  | 0  |       goto cleanup;  | 
1468  | 0  |     }  | 
1469  |  |  | 
1470  |  |     /* Copy the bag type.  | 
1471  |  |      */  | 
1472  | 0  |     result = asn1_write_value(c2, "?LAST.bagId", oid, 1);  | 
1473  | 0  |     if (result != ASN1_SUCCESS) { | 
1474  | 0  |       gnutls_assert();  | 
1475  | 0  |       result = _gnutls_asn2err(result);  | 
1476  | 0  |       goto cleanup;  | 
1477  | 0  |     }  | 
1478  |  |  | 
1479  |  |     /* Set empty attributes  | 
1480  |  |      */  | 
1481  | 0  |     result = write_attributes(bag, i, c2, "?LAST.bagAttributes");  | 
1482  | 0  |     if (result < 0) { | 
1483  | 0  |       gnutls_assert();  | 
1484  | 0  |       goto cleanup;  | 
1485  | 0  |     }  | 
1486  |  |  | 
1487  |  |     /* Copy the Bag Value  | 
1488  |  |      */  | 
1489  |  |  | 
1490  | 0  |     if (bag->element[i].type == GNUTLS_BAG_CERTIFICATE ||  | 
1491  | 0  |         bag->element[i].type == GNUTLS_BAG_SECRET ||  | 
1492  | 0  |         bag->element[i].type == GNUTLS_BAG_CRL) { | 
1493  | 0  |       gnutls_datum_t tmp;  | 
1494  |  |  | 
1495  |  |       /* in that case encode it to a CertBag or  | 
1496  |  |        * a CrlBag.  | 
1497  |  |        */  | 
1498  |  | 
  | 
1499  | 0  |       result = _pkcs12_encode_crt_bag(bag->element[i].type,  | 
1500  | 0  |               &bag->element[i].data,  | 
1501  | 0  |               &tmp);  | 
1502  |  | 
  | 
1503  | 0  |       if (result < 0) { | 
1504  | 0  |         gnutls_assert();  | 
1505  | 0  |         goto cleanup;  | 
1506  | 0  |       }  | 
1507  |  |  | 
1508  | 0  |       result = _gnutls_x509_write_value(c2, "?LAST.bagValue",  | 
1509  | 0  |                 &tmp);  | 
1510  |  | 
  | 
1511  | 0  |       _gnutls_free_datum(&tmp);  | 
1512  |  | 
  | 
1513  | 0  |     } else { | 
1514  | 0  |       result = _gnutls_x509_write_value(  | 
1515  | 0  |         c2, "?LAST.bagValue", &bag->element[i].data);  | 
1516  | 0  |     }  | 
1517  |  |  | 
1518  | 0  |     if (result < 0) { | 
1519  | 0  |       gnutls_assert();  | 
1520  | 0  |       goto cleanup;  | 
1521  | 0  |     }  | 
1522  | 0  |   }  | 
1523  |  |  | 
1524  |  |   /* Encode the data and copy them into the datum  | 
1525  |  |    */  | 
1526  | 0  |   *contents = c2;  | 
1527  |  | 
  | 
1528  | 0  |   return 0;  | 
1529  |  |  | 
1530  | 0  | cleanup:  | 
1531  | 0  |   if (c2)  | 
1532  | 0  |     asn1_delete_structure(&c2);  | 
1533  | 0  |   return result;  | 
1534  | 0  | }  | 
1535  |  |  | 
1536  |  | /* Checks if the extra_certs contain certificates that may form a chain  | 
1537  |  |  * with the first certificate in chain (it is expected that chain_len==1)  | 
1538  |  |  * and appends those in the chain.  | 
1539  |  |  */  | 
1540  |  | static int make_chain(gnutls_x509_crt_t **chain, unsigned int *chain_len,  | 
1541  |  |           gnutls_x509_crt_t **extra_certs,  | 
1542  |  |           unsigned int *extra_certs_len, unsigned int flags)  | 
1543  | 0  | { | 
1544  | 0  |   unsigned int i;  | 
1545  |  | 
  | 
1546  | 0  |   if (*chain_len != 1)  | 
1547  | 0  |     return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);  | 
1548  |  |  | 
1549  | 0  |   i = 0;  | 
1550  | 0  |   while (i < *extra_certs_len) { | 
1551  |  |     /* if it is an issuer but not a self-signed one */  | 
1552  | 0  |     if (gnutls_x509_crt_check_issuer((*chain)[*chain_len - 1],  | 
1553  | 0  |              (*extra_certs)[i]) != 0) { | 
1554  | 0  |       if (!(flags & GNUTLS_PKCS12_SP_INCLUDE_SELF_SIGNED) &&  | 
1555  | 0  |           gnutls_x509_crt_check_issuer(  | 
1556  | 0  |             (*extra_certs)[i], (*extra_certs)[i]) != 0)  | 
1557  | 0  |         goto skip;  | 
1558  |  |  | 
1559  | 0  |       if (unlikely(INT_ADD_OVERFLOW(*chain_len, 1))) { | 
1560  | 0  |         return gnutls_assert_val(GNUTLS_E_MEMORY_ERROR);  | 
1561  | 0  |       }  | 
1562  |  |  | 
1563  | 0  |       *chain = _gnutls_reallocarray_fast(  | 
1564  | 0  |         *chain, ++(*chain_len), sizeof((*chain)[0]));  | 
1565  | 0  |       if (*chain == NULL) { | 
1566  | 0  |         gnutls_assert();  | 
1567  | 0  |         return GNUTLS_E_MEMORY_ERROR;  | 
1568  | 0  |       }  | 
1569  | 0  |       (*chain)[*chain_len - 1] = (*extra_certs)[i];  | 
1570  |  | 
  | 
1571  | 0  |       (*extra_certs)[i] =  | 
1572  | 0  |         (*extra_certs)[*extra_certs_len - 1];  | 
1573  | 0  |       (*extra_certs_len)--;  | 
1574  |  | 
  | 
1575  | 0  |       i = 0;  | 
1576  | 0  |       continue;  | 
1577  | 0  |     }  | 
1578  |  |  | 
1579  | 0  |   skip:  | 
1580  | 0  |     i++;  | 
1581  | 0  |   }  | 
1582  | 0  |   return 0;  | 
1583  | 0  | }  | 
1584  |  |  | 
1585  |  | /**  | 
1586  |  |  * gnutls_pkcs12_simple_parse:  | 
1587  |  |  * @p12: A pkcs12 type  | 
1588  |  |  * @password: optional password used to decrypt the structure, bags and keys.  | 
1589  |  |  * @key: a structure to store the parsed private key.  | 
1590  |  |  * @chain: the corresponding to key certificate chain (may be %NULL)  | 
1591  |  |  * @chain_len: will be updated with the number of additional (may be %NULL)  | 
1592  |  |  * @extra_certs: optional pointer to receive an array of additional  | 
1593  |  |  *         certificates found in the PKCS12 structure (may be %NULL).  | 
1594  |  |  * @extra_certs_len: will be updated with the number of additional  | 
1595  |  |  *       certs (may be %NULL).  | 
1596  |  |  * @crl: an optional structure to store the parsed CRL (may be %NULL).  | 
1597  |  |  * @flags: should be zero or one of GNUTLS_PKCS12_SP_*  | 
1598  |  |  *  | 
1599  |  |  * This function parses a PKCS12 structure in @pkcs12 and extracts the  | 
1600  |  |  * private key, the corresponding certificate chain, any additional  | 
1601  |  |  * certificates and a CRL. The structures in @key, @chain @crl, and @extra_certs  | 
1602  |  |  * must not be initialized.  | 
1603  |  |  *  | 
1604  |  |  * The @extra_certs and @extra_certs_len parameters are optional  | 
1605  |  |  * and both may be set to %NULL. If either is non-%NULL, then both must  | 
1606  |  |  * be set. The value for @extra_certs is allocated  | 
1607  |  |  * using gnutls_malloc().  | 
1608  |  |  *   | 
1609  |  |  * Encrypted PKCS12 bags and PKCS8 private keys are supported, but  | 
1610  |  |  * only with password based security and the same password for all  | 
1611  |  |  * operations.  | 
1612  |  |  *  | 
1613  |  |  * Note that a PKCS12 structure may contain many keys and/or certificates,  | 
1614  |  |  * and there is no way to identify which key/certificate pair you want.  | 
1615  |  |  * For this reason this function is useful for PKCS12 files that contain   | 
1616  |  |  * only one key/certificate pair and/or one CRL.  | 
1617  |  |  *  | 
1618  |  |  * If the provided structure has encrypted fields but no password  | 
1619  |  |  * is provided then this function returns %GNUTLS_E_DECRYPTION_FAILED.  | 
1620  |  |  *  | 
1621  |  |  * Note that normally the chain constructed does not include self signed  | 
1622  |  |  * certificates, to comply with TLS' requirements. If, however, the flag   | 
1623  |  |  * %GNUTLS_PKCS12_SP_INCLUDE_SELF_SIGNED is specified then  | 
1624  |  |  * self signed certificates will be included in the chain.  | 
1625  |  |  *  | 
1626  |  |  * Prior to using this function the PKCS #12 structure integrity must  | 
1627  |  |  * be verified using gnutls_pkcs12_verify_mac().  | 
1628  |  |  *  | 
1629  |  |  * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a  | 
1630  |  |  *   negative error value.  | 
1631  |  |  *  | 
1632  |  |  * Since: 3.1.0  | 
1633  |  |  **/  | 
1634  |  | int gnutls_pkcs12_simple_parse(gnutls_pkcs12_t p12, const char *password,  | 
1635  |  |              gnutls_x509_privkey_t *key,  | 
1636  |  |              gnutls_x509_crt_t **chain,  | 
1637  |  |              unsigned int *chain_len,  | 
1638  |  |              gnutls_x509_crt_t **extra_certs,  | 
1639  |  |              unsigned int *extra_certs_len,  | 
1640  |  |              gnutls_x509_crl_t *crl, unsigned int flags)  | 
1641  | 0  | { | 
1642  | 0  |   gnutls_pkcs12_bag_t bag = NULL;  | 
1643  | 0  |   gnutls_x509_crt_t *_extra_certs = NULL;  | 
1644  | 0  |   unsigned int _extra_certs_len = 0;  | 
1645  | 0  |   gnutls_x509_crt_t *_chain = NULL;  | 
1646  | 0  |   unsigned int _chain_len = 0;  | 
1647  | 0  |   int idx = 0;  | 
1648  | 0  |   int ret;  | 
1649  | 0  |   size_t cert_id_size = 0;  | 
1650  | 0  |   size_t key_id_size = 0;  | 
1651  | 0  |   uint8_t cert_id[20];  | 
1652  | 0  |   uint8_t key_id[20];  | 
1653  | 0  |   int privkey_ok = 0;  | 
1654  | 0  |   unsigned int i;  | 
1655  | 0  |   int elements_in_bag;  | 
1656  |  | 
  | 
1657  | 0  |   *key = NULL;  | 
1658  |  | 
  | 
1659  | 0  |   if (crl)  | 
1660  | 0  |     *crl = NULL;  | 
1661  |  |  | 
1662  |  |   /* find the first private key */  | 
1663  | 0  |   for (;;) { | 
1664  | 0  |     ret = gnutls_pkcs12_bag_init(&bag);  | 
1665  | 0  |     if (ret < 0) { | 
1666  | 0  |       bag = NULL;  | 
1667  | 0  |       gnutls_assert();  | 
1668  | 0  |       goto done;  | 
1669  | 0  |     }  | 
1670  |  |  | 
1671  | 0  |     ret = gnutls_pkcs12_get_bag(p12, idx, bag);  | 
1672  | 0  |     if (ret == GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE) { | 
1673  | 0  |       gnutls_pkcs12_bag_deinit(bag);  | 
1674  | 0  |       bag = NULL;  | 
1675  | 0  |       break;  | 
1676  | 0  |     }  | 
1677  | 0  |     if (ret < 0) { | 
1678  | 0  |       gnutls_assert();  | 
1679  | 0  |       goto done;  | 
1680  | 0  |     }  | 
1681  |  |  | 
1682  | 0  |     ret = gnutls_pkcs12_bag_get_type(bag, 0);  | 
1683  | 0  |     if (ret < 0) { | 
1684  | 0  |       gnutls_assert();  | 
1685  | 0  |       goto done;  | 
1686  | 0  |     }  | 
1687  |  |  | 
1688  | 0  |     if (ret == GNUTLS_BAG_ENCRYPTED) { | 
1689  | 0  |       if (password == NULL) { | 
1690  | 0  |         ret = gnutls_assert_val(  | 
1691  | 0  |           GNUTLS_E_DECRYPTION_FAILED);  | 
1692  | 0  |         goto done;  | 
1693  | 0  |       }  | 
1694  |  |  | 
1695  | 0  |       ret = gnutls_pkcs12_bag_decrypt(bag, password);  | 
1696  | 0  |       if (ret < 0) { | 
1697  | 0  |         gnutls_assert();  | 
1698  | 0  |         goto done;  | 
1699  | 0  |       }  | 
1700  | 0  |     }  | 
1701  |  |  | 
1702  | 0  |     elements_in_bag = gnutls_pkcs12_bag_get_count(bag);  | 
1703  | 0  |     if (elements_in_bag < 0) { | 
1704  | 0  |       gnutls_assert();  | 
1705  | 0  |       goto done;  | 
1706  | 0  |     }  | 
1707  |  |  | 
1708  | 0  |     for (i = 0; i < (unsigned)elements_in_bag; i++) { | 
1709  | 0  |       int type;  | 
1710  | 0  |       gnutls_datum_t data;  | 
1711  |  | 
  | 
1712  | 0  |       type = gnutls_pkcs12_bag_get_type(bag, i);  | 
1713  | 0  |       if (type < 0) { | 
1714  | 0  |         gnutls_assert();  | 
1715  | 0  |         goto done;  | 
1716  | 0  |       }  | 
1717  |  |  | 
1718  | 0  |       ret = gnutls_pkcs12_bag_get_data(bag, i, &data);  | 
1719  | 0  |       if (ret < 0) { | 
1720  | 0  |         gnutls_assert();  | 
1721  | 0  |         goto done;  | 
1722  | 0  |       }  | 
1723  |  |  | 
1724  | 0  |       switch (type) { | 
1725  | 0  |       case GNUTLS_BAG_PKCS8_ENCRYPTED_KEY:  | 
1726  | 0  |         if (password == NULL) { | 
1727  | 0  |           ret = gnutls_assert_val(  | 
1728  | 0  |             GNUTLS_E_DECRYPTION_FAILED);  | 
1729  | 0  |           goto done;  | 
1730  | 0  |         }  | 
1731  |  |  | 
1732  | 0  |         FALLTHROUGH;  | 
1733  | 0  |       case GNUTLS_BAG_PKCS8_KEY:  | 
1734  | 0  |         if (*key != NULL) { /* too simple to continue */ | 
1735  | 0  |           gnutls_assert();  | 
1736  | 0  |           break;  | 
1737  | 0  |         }  | 
1738  |  |  | 
1739  | 0  |         ret = gnutls_x509_privkey_init(key);  | 
1740  | 0  |         if (ret < 0) { | 
1741  | 0  |           gnutls_assert();  | 
1742  | 0  |           goto done;  | 
1743  | 0  |         }  | 
1744  |  |  | 
1745  | 0  |         ret = gnutls_x509_privkey_import_pkcs8(  | 
1746  | 0  |           *key, &data, GNUTLS_X509_FMT_DER,  | 
1747  | 0  |           password,  | 
1748  | 0  |           type == GNUTLS_BAG_PKCS8_KEY ?  | 
1749  | 0  |             GNUTLS_PKCS_PLAIN :  | 
1750  | 0  |             0);  | 
1751  | 0  |         if (ret < 0) { | 
1752  | 0  |           gnutls_assert();  | 
1753  | 0  |           goto done;  | 
1754  | 0  |         }  | 
1755  |  |  | 
1756  | 0  |         key_id_size = sizeof(key_id);  | 
1757  | 0  |         ret = gnutls_x509_privkey_get_key_id(  | 
1758  | 0  |           *key, 0, key_id, &key_id_size);  | 
1759  | 0  |         if (ret < 0) { | 
1760  | 0  |           gnutls_assert();  | 
1761  | 0  |           goto done;  | 
1762  | 0  |         }  | 
1763  |  |  | 
1764  | 0  |         privkey_ok = 1; /* break */  | 
1765  | 0  |         break;  | 
1766  | 0  |       default:  | 
1767  | 0  |         break;  | 
1768  | 0  |       }  | 
1769  | 0  |     }  | 
1770  |  |  | 
1771  | 0  |     idx++;  | 
1772  | 0  |     gnutls_pkcs12_bag_deinit(bag);  | 
1773  | 0  |     bag = NULL;  | 
1774  |  | 
  | 
1775  | 0  |     if (privkey_ok != 0) /* private key was found */  | 
1776  | 0  |       break;  | 
1777  | 0  |   }  | 
1778  |  |  | 
1779  | 0  |   if (privkey_ok == 0) { /* no private key */ | 
1780  | 0  |     gnutls_assert();  | 
1781  | 0  |     return GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE;  | 
1782  | 0  |   }  | 
1783  |  |  | 
1784  |  |   /* now find the corresponding certificate   | 
1785  |  |    */  | 
1786  | 0  |   idx = 0;  | 
1787  | 0  |   bag = NULL;  | 
1788  | 0  |   for (;;) { | 
1789  | 0  |     ret = gnutls_pkcs12_bag_init(&bag);  | 
1790  | 0  |     if (ret < 0) { | 
1791  | 0  |       bag = NULL;  | 
1792  | 0  |       gnutls_assert();  | 
1793  | 0  |       goto done;  | 
1794  | 0  |     }  | 
1795  |  |  | 
1796  | 0  |     ret = gnutls_pkcs12_get_bag(p12, idx, bag);  | 
1797  | 0  |     if (ret == GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE) { | 
1798  | 0  |       gnutls_pkcs12_bag_deinit(bag);  | 
1799  | 0  |       bag = NULL;  | 
1800  | 0  |       break;  | 
1801  | 0  |     }  | 
1802  | 0  |     if (ret < 0) { | 
1803  | 0  |       gnutls_assert();  | 
1804  | 0  |       goto done;  | 
1805  | 0  |     }  | 
1806  |  |  | 
1807  | 0  |     ret = gnutls_pkcs12_bag_get_type(bag, 0);  | 
1808  | 0  |     if (ret < 0) { | 
1809  | 0  |       gnutls_assert();  | 
1810  | 0  |       goto done;  | 
1811  | 0  |     }  | 
1812  |  |  | 
1813  | 0  |     if (ret == GNUTLS_BAG_ENCRYPTED) { | 
1814  | 0  |       ret = gnutls_pkcs12_bag_decrypt(bag, password);  | 
1815  | 0  |       if (ret < 0) { | 
1816  | 0  |         gnutls_assert();  | 
1817  | 0  |         goto done;  | 
1818  | 0  |       }  | 
1819  | 0  |     }  | 
1820  |  |  | 
1821  | 0  |     elements_in_bag = gnutls_pkcs12_bag_get_count(bag);  | 
1822  | 0  |     if (elements_in_bag < 0) { | 
1823  | 0  |       gnutls_assert();  | 
1824  | 0  |       goto done;  | 
1825  | 0  |     }  | 
1826  |  |  | 
1827  | 0  |     for (i = 0; i < (unsigned)elements_in_bag; i++) { | 
1828  | 0  |       int type;  | 
1829  | 0  |       gnutls_datum_t data;  | 
1830  | 0  |       gnutls_x509_crt_t this_cert;  | 
1831  |  | 
  | 
1832  | 0  |       type = gnutls_pkcs12_bag_get_type(bag, i);  | 
1833  | 0  |       if (type < 0) { | 
1834  | 0  |         gnutls_assert();  | 
1835  | 0  |         goto done;  | 
1836  | 0  |       }  | 
1837  |  |  | 
1838  | 0  |       ret = gnutls_pkcs12_bag_get_data(bag, i, &data);  | 
1839  | 0  |       if (ret < 0) { | 
1840  | 0  |         gnutls_assert();  | 
1841  | 0  |         goto done;  | 
1842  | 0  |       }  | 
1843  |  |  | 
1844  | 0  |       switch (type) { | 
1845  | 0  |       case GNUTLS_BAG_CERTIFICATE:  | 
1846  | 0  |         ret = gnutls_x509_crt_init(&this_cert);  | 
1847  | 0  |         if (ret < 0) { | 
1848  | 0  |           gnutls_assert();  | 
1849  | 0  |           goto done;  | 
1850  | 0  |         }  | 
1851  |  |  | 
1852  | 0  |         ret = gnutls_x509_crt_import(  | 
1853  | 0  |           this_cert, &data, GNUTLS_X509_FMT_DER);  | 
1854  | 0  |         if (ret < 0) { | 
1855  | 0  |           gnutls_assert();  | 
1856  | 0  |           gnutls_x509_crt_deinit(this_cert);  | 
1857  | 0  |           this_cert = NULL;  | 
1858  | 0  |           goto done;  | 
1859  | 0  |         }  | 
1860  |  |  | 
1861  |  |         /* check if the key id match */  | 
1862  | 0  |         cert_id_size = sizeof(cert_id);  | 
1863  | 0  |         ret = gnutls_x509_crt_get_key_id(  | 
1864  | 0  |           this_cert, 0, cert_id, &cert_id_size);  | 
1865  | 0  |         if (ret < 0) { | 
1866  | 0  |           gnutls_assert();  | 
1867  | 0  |           gnutls_x509_crt_deinit(this_cert);  | 
1868  | 0  |           this_cert = NULL;  | 
1869  | 0  |           goto done;  | 
1870  | 0  |         }  | 
1871  |  |  | 
1872  | 0  |         if (memcmp(cert_id, key_id, cert_id_size) !=  | 
1873  | 0  |             0) { /* they don't match - skip the certificate */ | 
1874  | 0  |           if (unlikely(INT_ADD_OVERFLOW(  | 
1875  | 0  |                 _extra_certs_len, 1))) { | 
1876  | 0  |             ret = gnutls_assert_val(  | 
1877  | 0  |               GNUTLS_E_MEMORY_ERROR);  | 
1878  | 0  |             goto done;  | 
1879  | 0  |           }  | 
1880  |  |  | 
1881  | 0  |           _extra_certs = _gnutls_reallocarray_fast(  | 
1882  | 0  |             _extra_certs,  | 
1883  | 0  |             ++_extra_certs_len,  | 
1884  | 0  |             sizeof(_extra_certs[0]));  | 
1885  | 0  |           if (!_extra_certs) { | 
1886  | 0  |             gnutls_assert();  | 
1887  | 0  |             ret = GNUTLS_E_MEMORY_ERROR;  | 
1888  | 0  |             goto done;  | 
1889  | 0  |           }  | 
1890  | 0  |           _extra_certs[_extra_certs_len - 1] =  | 
1891  | 0  |             this_cert;  | 
1892  | 0  |           this_cert = NULL;  | 
1893  | 0  |         } else { | 
1894  | 0  |           if (chain && _chain_len == 0) { | 
1895  | 0  |             _chain = gnutls_malloc(  | 
1896  | 0  |               sizeof(_chain[0]) *  | 
1897  | 0  |               (++_chain_len));  | 
1898  | 0  |             if (!_chain) { | 
1899  | 0  |               gnutls_assert();  | 
1900  | 0  |               ret = GNUTLS_E_MEMORY_ERROR;  | 
1901  | 0  |               goto done;  | 
1902  | 0  |             }  | 
1903  | 0  |             _chain[_chain_len - 1] =  | 
1904  | 0  |               this_cert;  | 
1905  | 0  |             this_cert = NULL;  | 
1906  | 0  |           } else { | 
1907  | 0  |             gnutls_x509_crt_deinit(  | 
1908  | 0  |               this_cert);  | 
1909  | 0  |             this_cert = NULL;  | 
1910  | 0  |           }  | 
1911  | 0  |         }  | 
1912  | 0  |         break;  | 
1913  |  |  | 
1914  | 0  |       case GNUTLS_BAG_CRL:  | 
1915  | 0  |         if (crl == NULL || *crl != NULL) { | 
1916  | 0  |           gnutls_assert();  | 
1917  | 0  |           break;  | 
1918  | 0  |         }  | 
1919  |  |  | 
1920  | 0  |         ret = gnutls_x509_crl_init(crl);  | 
1921  | 0  |         if (ret < 0) { | 
1922  | 0  |           gnutls_assert();  | 
1923  | 0  |           goto done;  | 
1924  | 0  |         }  | 
1925  |  |  | 
1926  | 0  |         ret = gnutls_x509_crl_import(  | 
1927  | 0  |           *crl, &data, GNUTLS_X509_FMT_DER);  | 
1928  | 0  |         if (ret < 0) { | 
1929  | 0  |           gnutls_assert();  | 
1930  | 0  |           gnutls_x509_crl_deinit(*crl);  | 
1931  | 0  |           *crl = NULL;  | 
1932  | 0  |           goto done;  | 
1933  | 0  |         }  | 
1934  | 0  |         break;  | 
1935  |  |  | 
1936  | 0  |       case GNUTLS_BAG_ENCRYPTED:  | 
1937  |  |         /* XXX Bother to recurse one level down?  Unlikely to  | 
1938  |  |            use the same password anyway. */  | 
1939  | 0  |       case GNUTLS_BAG_EMPTY:  | 
1940  | 0  |       default:  | 
1941  | 0  |         break;  | 
1942  | 0  |       }  | 
1943  | 0  |     }  | 
1944  |  |  | 
1945  | 0  |     idx++;  | 
1946  | 0  |     gnutls_pkcs12_bag_deinit(bag);  | 
1947  | 0  |     bag = NULL;  | 
1948  | 0  |   }  | 
1949  |  |  | 
1950  | 0  |   if (chain != NULL) { | 
1951  | 0  |     if (_chain_len != 1) { | 
1952  | 0  |       ret = GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE;  | 
1953  | 0  |       goto done;  | 
1954  | 0  |     }  | 
1955  |  |  | 
1956  | 0  |     ret = make_chain(&_chain, &_chain_len, &_extra_certs,  | 
1957  | 0  |          &_extra_certs_len, flags);  | 
1958  | 0  |     if (ret < 0) { | 
1959  | 0  |       gnutls_assert();  | 
1960  | 0  |       goto done;  | 
1961  | 0  |     }  | 
1962  | 0  |   }  | 
1963  |  |  | 
1964  | 0  |   ret = 0;  | 
1965  |  | 
  | 
1966  | 0  | done:  | 
1967  | 0  |   if (bag)  | 
1968  | 0  |     gnutls_pkcs12_bag_deinit(bag);  | 
1969  |  | 
  | 
1970  | 0  |   if (ret < 0) { | 
1971  | 0  |     if (*key) { | 
1972  | 0  |       gnutls_x509_privkey_deinit(*key);  | 
1973  | 0  |       *key = NULL;  | 
1974  | 0  |     }  | 
1975  | 0  |     if (crl != NULL && *crl != NULL) { | 
1976  | 0  |       gnutls_x509_crl_deinit(*crl);  | 
1977  | 0  |       *crl = NULL;  | 
1978  | 0  |     }  | 
1979  | 0  |     if (_extra_certs_len && _extra_certs != NULL) { | 
1980  | 0  |       for (i = 0; i < _extra_certs_len; i++)  | 
1981  | 0  |         gnutls_x509_crt_deinit(_extra_certs[i]);  | 
1982  | 0  |       gnutls_free(_extra_certs);  | 
1983  | 0  |     }  | 
1984  | 0  |     if (_chain_len && _chain != NULL) { | 
1985  | 0  |       for (i = 0; i < _chain_len; i++)  | 
1986  | 0  |         gnutls_x509_crt_deinit(_chain[i]);  | 
1987  | 0  |       gnutls_free(_chain);  | 
1988  | 0  |     }  | 
1989  |  | 
  | 
1990  | 0  |     return ret;  | 
1991  | 0  |   }  | 
1992  |  |  | 
1993  | 0  |   if (extra_certs && _extra_certs_len > 0) { | 
1994  | 0  |     *extra_certs = _extra_certs;  | 
1995  | 0  |     *extra_certs_len = _extra_certs_len;  | 
1996  | 0  |   } else { | 
1997  | 0  |     if (extra_certs) { | 
1998  | 0  |       *extra_certs = NULL;  | 
1999  | 0  |       *extra_certs_len = 0;  | 
2000  | 0  |     }  | 
2001  | 0  |     for (i = 0; i < _extra_certs_len; i++)  | 
2002  | 0  |       gnutls_x509_crt_deinit(_extra_certs[i]);  | 
2003  | 0  |     gnutls_free(_extra_certs);  | 
2004  | 0  |   }  | 
2005  |  | 
  | 
2006  | 0  |   if (chain != NULL) { | 
2007  | 0  |     *chain = _chain;  | 
2008  | 0  |     *chain_len = _chain_len;  | 
2009  | 0  |   }  | 
2010  |  | 
  | 
2011  | 0  |   return ret;  | 
2012  | 0  | }  | 
2013  |  |  | 
2014  |  | /**  | 
2015  |  |  * gnutls_pkcs12_mac_info:  | 
2016  |  |  * @pkcs12: A pkcs12 type  | 
2017  |  |  * @mac: the MAC algorithm used as %gnutls_mac_algorithm_t  | 
2018  |  |  * @salt: the salt used for string to key (if non-NULL then @salt_size initially holds its size)  | 
2019  |  |  * @salt_size: string to key salt size  | 
2020  |  |  * @iter_count: string to key iteration count  | 
2021  |  |  * @oid: if non-NULL it will contain an allocated null-terminated variable with the OID  | 
2022  |  |  *  | 
2023  |  |  * This function will provide information on the MAC algorithm used  | 
2024  |  |  * in a PKCS #12 structure. If the structure algorithms  | 
2025  |  |  * are unknown the code %GNUTLS_E_UNKNOWN_HASH_ALGORITHM will be returned,  | 
2026  |  |  * and only @oid, will be set. That is, @oid will be set on structures  | 
2027  |  |  * with a MAC whether supported or not. It must be deinitialized using gnutls_free().  | 
2028  |  |  * The other variables are only set on supported structures.  | 
2029  |  |  *  | 
2030  |  |  * Returns: %GNUTLS_E_INVALID_REQUEST if the provided structure doesn't contain a MAC,  | 
2031  |  |  *  %GNUTLS_E_UNKNOWN_HASH_ALGORITHM if the structure's MAC isn't supported, or  | 
2032  |  |  *  another negative error code in case of a failure. Zero on success.  | 
2033  |  |  **/  | 
2034  |  | int gnutls_pkcs12_mac_info(gnutls_pkcs12_t pkcs12, unsigned int *mac,  | 
2035  |  |          void *salt, unsigned int *salt_size,  | 
2036  |  |          unsigned int *iter_count, char **oid)  | 
2037  | 0  | { | 
2038  | 0  |   int ret;  | 
2039  | 0  |   gnutls_datum_t tmp = { NULL, 0 }, dsalt = { NULL, 0 }; | 
2040  | 0  |   gnutls_mac_algorithm_t algo;  | 
2041  |  | 
  | 
2042  | 0  |   if (oid)  | 
2043  | 0  |     *oid = NULL;  | 
2044  |  | 
  | 
2045  | 0  |   if (pkcs12 == NULL) { | 
2046  | 0  |     gnutls_assert();  | 
2047  | 0  |     return GNUTLS_E_INVALID_REQUEST;  | 
2048  | 0  |   }  | 
2049  |  |  | 
2050  | 0  |   ret = _gnutls_x509_read_value(  | 
2051  | 0  |     pkcs12->pkcs12, "macData.mac.digestAlgorithm.algorithm", &tmp);  | 
2052  | 0  |   if (ret < 0) { | 
2053  | 0  |     gnutls_assert();  | 
2054  | 0  |     return GNUTLS_E_INVALID_REQUEST;  | 
2055  | 0  |   }  | 
2056  |  |  | 
2057  | 0  |   if (oid) { | 
2058  | 0  |     *oid = (char *)tmp.data;  | 
2059  | 0  |   }  | 
2060  |  | 
  | 
2061  | 0  |   if (strcmp((char *)tmp.data, PBMAC1_OID) == 0) { | 
2062  | 0  |     algo = GNUTLS_MAC_PBMAC1;  | 
2063  | 0  |   } else { | 
2064  | 0  |     algo = DIG_TO_MAC(gnutls_oid_to_digest((char *)tmp.data));  | 
2065  | 0  |   }  | 
2066  | 0  |   if (algo == GNUTLS_MAC_UNKNOWN || mac_to_entry(algo) == NULL) { | 
2067  | 0  |     gnutls_assert();  | 
2068  | 0  |     return GNUTLS_E_UNKNOWN_HASH_ALGORITHM;  | 
2069  | 0  |   }  | 
2070  |  |  | 
2071  | 0  |   if (oid) { | 
2072  | 0  |     tmp.data = NULL;  | 
2073  | 0  |   }  | 
2074  |  | 
  | 
2075  | 0  |   if (mac) { | 
2076  | 0  |     *mac = algo;  | 
2077  | 0  |   }  | 
2078  |  | 
  | 
2079  | 0  |   if (iter_count) { | 
2080  | 0  |     ret = _gnutls_x509_read_uint(pkcs12->pkcs12,  | 
2081  | 0  |                "macData.iterations", iter_count);  | 
2082  | 0  |     if (ret < 0) { | 
2083  | 0  |       *iter_count = 1; /* the default */  | 
2084  | 0  |     }  | 
2085  | 0  |   }  | 
2086  |  | 
  | 
2087  | 0  |   if (salt) { | 
2088  |  |     /* Read the salt from the structure.  | 
2089  |  |      */  | 
2090  | 0  |     ret = _gnutls_x509_read_null_value(pkcs12->pkcs12,  | 
2091  | 0  |                "macData.macSalt", &dsalt);  | 
2092  | 0  |     if (ret < 0) { | 
2093  | 0  |       gnutls_assert();  | 
2094  | 0  |       goto cleanup;  | 
2095  | 0  |     }  | 
2096  |  |  | 
2097  | 0  |     if (*salt_size >= (unsigned)dsalt.size) { | 
2098  | 0  |       *salt_size = dsalt.size;  | 
2099  | 0  |       if (dsalt.size > 0)  | 
2100  | 0  |         memcpy(salt, dsalt.data, dsalt.size);  | 
2101  | 0  |     } else { | 
2102  | 0  |       *salt_size = dsalt.size;  | 
2103  | 0  |       ret = gnutls_assert_val(GNUTLS_E_SHORT_MEMORY_BUFFER);  | 
2104  | 0  |       goto cleanup;  | 
2105  | 0  |     }  | 
2106  | 0  |   }  | 
2107  |  |  | 
2108  | 0  |   ret = 0;  | 
2109  | 0  | cleanup:  | 
2110  | 0  |   _gnutls_free_datum(&tmp);  | 
2111  | 0  |   _gnutls_free_datum(&dsalt);  | 
2112  | 0  |   return ret;  | 
2113  | 0  | }  |