updated makefiles
[gnutls.git] / doc / examples / ex-pkcs11-list.c
blob70849beada1af4b0669a11916af47ae97254a0d7
1 /* This example code is placed in the public domain. */
3 #include <config.h>
4 #include <gnutls/gnutls.h>
5 #include <gnutls/pkcs11.h>
6 #include <stdio.h>
7 #include <stdlib.h>
9 #define URL "pkcs11:URL"
11 int
12 main (int argc, char** argv)
14 gnutls_pkcs11_obj_t *obj_list;
15 gnutls_x509_crt_t xcrt;
16 unsigned int obj_list_size = 0;
17 gnutls_datum_t cinfo;
18 int ret;
19 unsigned int i;
21 obj_list_size = 0;
22 ret = gnutls_pkcs11_obj_list_import_url (NULL, &obj_list_size, URL,
23 GNUTLS_PKCS11_OBJ_ATTR_CRT_WITH_PRIVKEY,
24 0);
25 if (ret < 0 && ret != GNUTLS_E_SHORT_MEMORY_BUFFER)
26 return -1;
28 /* no error checking from now on */
29 obj_list = malloc (sizeof (*obj_list) * obj_list_size);
31 gnutls_pkcs11_obj_list_import_url (obj_list, &obj_list_size, URL,
32 GNUTLS_PKCS11_OBJ_ATTR_CRT_WITH_PRIVKEY,
33 0);
35 /* now all certificates are in obj_list */
36 for (i = 0; i < obj_list_size; i++)
39 gnutls_x509_crt_init (&xcrt);
41 gnutls_x509_crt_import_pkcs11 (xcrt, obj_list[i]);
43 gnutls_x509_crt_print (xcrt, GNUTLS_CRT_PRINT_FULL, &cinfo);
45 fprintf (stdout, "cert[%d]:\n %s\n\n", i, cinfo.data);
47 gnutls_free (cinfo.data);
48 gnutls_x509_crt_deinit (xcrt);
51 return 0;