2 * Copyright (C) 2012 Free Software Foundation, Inc.
4 * Author: Nikos Mavrogiannopoulos
6 * This file is part of GnuTLS.
8 * The GnuTLS is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public License
10 * as published by the Free Software Foundation; either version 2.1 of
11 * the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>
23 #include <gnutls_int.h>
24 #include <gnutls_errors.h>
26 #include <gnutls_global.h>
27 #include <gnutls_num.h>
28 #include <gnutls_sig.h>
29 #include <gnutls_str.h>
30 #include <gnutls_datum.h>
33 #include "verify-high.h"
34 #include "read-file.h"
36 /* Convenience functions for verify-high functionality
40 * gnutls_x509_trust_list_add_trust_mem:
41 * @list: The structure of the list
42 * @cas: A buffer containing a list of CAs (optional)
43 * @crls: A buffer containing a list of CRLs (optional)
44 * @type: The format of the certificates
45 * @tl_flags: GNUTLS_TL_*
46 * @tl_vflags: gnutls_certificate_verify_flags if flags specifies GNUTLS_TL_VERIFY_CRL
48 * This function will add the given certificate authorities
49 * to the trusted list.
51 * Returns: The number of added elements is returned.
56 gnutls_x509_trust_list_add_trust_mem(gnutls_x509_trust_list_t list,
57 const gnutls_datum_t * cas,
58 const gnutls_datum_t * crls,
59 gnutls_x509_crt_fmt_t type,
60 unsigned int tl_flags,
61 unsigned int tl_vflags)
64 gnutls_x509_crt_t *x509_ca_list = NULL;
65 gnutls_x509_crl_t *x509_crl_list = NULL;
66 unsigned int x509_ncas, x509_ncrls;
69 if (cas != NULL && cas->data != NULL) {
71 gnutls_x509_crt_list_import2(&x509_ca_list, &x509_ncas,
74 return gnutls_assert_val(ret);
77 gnutls_x509_trust_list_add_cas(list, x509_ca_list,
79 gnutls_free(x509_ca_list);
82 return gnutls_assert_val(ret);
87 if (crls != NULL && crls->data != NULL) {
89 gnutls_x509_crl_list_import2(&x509_crl_list,
90 &x509_ncrls, crls, type,
93 return gnutls_assert_val(ret);
96 gnutls_x509_trust_list_add_crls(list, x509_crl_list,
99 gnutls_free(x509_crl_list);
102 return gnutls_assert_val(ret);
111 * gnutls_x509_trust_list_remove_trust_mem:
112 * @list: The structure of the list
113 * @cas: A buffer containing a list of CAs (optional)
114 * @type: The format of the certificates
116 * This function will remove the provided certificate authorities
117 * from the trusted list, and add them into a black list when needed.
119 * See also gnutls_x509_trust_list_remove_cas().
121 * Returns: The number of removed elements is returned.
126 gnutls_x509_trust_list_remove_trust_mem(gnutls_x509_trust_list_t list,
127 const gnutls_datum_t * cas,
128 gnutls_x509_crt_fmt_t type)
131 gnutls_x509_crt_t *x509_ca_list = NULL;
132 unsigned int x509_ncas;
133 unsigned int r = 0, i;
135 if (cas != NULL && cas->data != NULL) {
137 gnutls_x509_crt_list_import2(&x509_ca_list, &x509_ncas,
140 return gnutls_assert_val(ret);
143 gnutls_x509_trust_list_remove_cas(list, x509_ca_list,
146 for (i = 0; i < x509_ncas; i++)
147 gnutls_x509_crt_deinit(x509_ca_list[i]);
148 gnutls_free(x509_ca_list);
151 return gnutls_assert_val(ret);
161 int import_pkcs11_url(gnutls_x509_trust_list_t list, const char *ca_file,
164 gnutls_x509_crt_t *xcrt_list = NULL;
165 gnutls_pkcs11_obj_t *pcrt_list = NULL;
166 unsigned int pcrt_list_size = 0, i;
170 gnutls_pkcs11_obj_list_import_url2(&pcrt_list, &pcrt_list_size,
172 GNUTLS_PKCS11_OBJ_ATTR_CRT_TRUSTED_CA,
175 return gnutls_assert_val(ret);
177 if (pcrt_list_size == 0) {
183 gnutls_malloc(sizeof(gnutls_x509_crt_t) * pcrt_list_size);
184 if (xcrt_list == NULL) {
185 ret = GNUTLS_E_MEMORY_ERROR;
190 gnutls_x509_crt_list_import_pkcs11(xcrt_list, pcrt_list_size,
198 gnutls_x509_trust_list_add_cas(list, xcrt_list, pcrt_list_size,
202 for (i = 0; i < pcrt_list_size; i++)
203 gnutls_pkcs11_obj_deinit(pcrt_list[i]);
204 gnutls_free(pcrt_list);
205 gnutls_free(xcrt_list);
211 int remove_pkcs11_url(gnutls_x509_trust_list_t list, const char *ca_file)
213 gnutls_x509_crt_t *xcrt_list = NULL;
214 gnutls_pkcs11_obj_t *pcrt_list = NULL;
215 unsigned int pcrt_list_size = 0, i;
219 gnutls_pkcs11_obj_list_import_url2(&pcrt_list, &pcrt_list_size,
221 GNUTLS_PKCS11_OBJ_ATTR_CRT_TRUSTED_CA,
224 return gnutls_assert_val(ret);
226 if (pcrt_list_size == 0) {
232 gnutls_malloc(sizeof(gnutls_x509_crt_t) * pcrt_list_size);
233 if (xcrt_list == NULL) {
234 ret = GNUTLS_E_MEMORY_ERROR;
239 gnutls_x509_crt_list_import_pkcs11(xcrt_list, pcrt_list_size,
247 gnutls_x509_trust_list_remove_cas(list, xcrt_list,
251 for (i = 0; i < pcrt_list_size; i++) {
252 gnutls_pkcs11_obj_deinit(pcrt_list[i]);
254 gnutls_x509_crt_deinit(xcrt_list[i]);
256 gnutls_free(pcrt_list);
257 gnutls_free(xcrt_list);
265 * gnutls_x509_trust_list_add_trust_file:
266 * @list: The structure of the list
267 * @ca_file: A file containing a list of CAs (optional)
268 * @crl_file: A file containing a list of CRLs (optional)
269 * @type: The format of the certificates
270 * @tl_flags: GNUTLS_TL_*
271 * @tl_vflags: gnutls_certificate_verify_flags if flags specifies GNUTLS_TL_VERIFY_CRL
273 * This function will add the given certificate authorities
274 * to the trusted list. pkcs11 URLs are also accepted, instead
275 * of files, by this function.
277 * Returns: The number of added elements is returned.
282 gnutls_x509_trust_list_add_trust_file(gnutls_x509_trust_list_t list,
284 const char *crl_file,
285 gnutls_x509_crt_fmt_t type,
286 unsigned int tl_flags,
287 unsigned int tl_vflags)
289 gnutls_datum_t cas = { NULL, 0 };
290 gnutls_datum_t crls = { NULL, 0 };
295 if (strncmp(ca_file, "pkcs11:", 7) == 0) {
296 ret = import_pkcs11_url(list, ca_file, tl_flags);
298 return gnutls_assert_val(ret);
302 cas.data = (void *) read_binary_file(ca_file, &size);
303 if (cas.data == NULL) {
305 return GNUTLS_E_FILE_ERROR;
311 crls.data = (void *) read_binary_file(crl_file, &size);
312 if (crls.data == NULL) {
314 return GNUTLS_E_FILE_ERROR;
320 gnutls_x509_trust_list_add_trust_mem(list, &cas, &crls, type,
321 tl_flags, tl_vflags);
329 * gnutls_x509_trust_list_remove_trust_file:
330 * @list: The structure of the list
331 * @ca_file: A file containing a list of CAs
332 * @type: The format of the certificates
334 * This function will remove the given certificate authorities
335 * from the trusted list, and add them into a black list when needed.
336 * PKCS 11 URLs are also accepted, instead
337 * of files, by this function.
339 * See also gnutls_x509_trust_list_remove_cas().
341 * Returns: The number of added elements is returned.
346 gnutls_x509_trust_list_remove_trust_file(gnutls_x509_trust_list_t list,
348 gnutls_x509_crt_fmt_t type)
350 gnutls_datum_t cas = { NULL, 0 };
355 if (strncmp(ca_file, "pkcs11:", 7) == 0) {
356 ret = remove_pkcs11_url(list, ca_file);
358 return gnutls_assert_val(ret);
362 cas.data = (void *) read_binary_file(ca_file, &size);
363 if (cas.data == NULL) {
365 return GNUTLS_E_FILE_ERROR;
370 ret = gnutls_x509_trust_list_remove_trust_mem(list, &cas, type);