summaryrefslogtreecommitdiff
path: root/tests/pkcs11/list-tokens.c
blob: ccd0ad6e72af1c9cfe1cf76b8e895b49098bfed8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
 * Copyright (C) 2016-2017 Red Hat, Inc.
 *
 * Author: Nikos Mavrogiannopoulos
 *
 * This file is part of GnuTLS.
 *
 * GnuTLS is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * GnuTLS is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

#include <gnutls/gnutls.h>
#include <gnutls/x509.h>
#include <gnutls/abstract.h>
#include <getopt.h>
#include <assert.h>
#define P11_KIT_FUTURE_UNSTABLE_API
#include <p11-kit/p11-kit.h>
#include "cert-common.h"

/* lists the registered PKCS#11 modules by p11-kit.
 */

static void tls_log_func(int level, const char *str)
{
	fprintf(stderr, "|<%d>| %s", level, str);
}

int
_gnutls_pkcs11_token_get_url(unsigned int seq,
                            gnutls_pkcs11_url_type_t detailed, char **url,
                            unsigned flags);

int main(int argc, char **argv)
{
	int ret;
	unsigned i;
	int opt;
	char *url, *mod;
	gnutls_x509_trust_list_t tl;
	gnutls_x509_crt_t crt;
	gnutls_pkcs11_privkey_t key;
	unsigned flag = 1;
	unsigned int status;
	bool need_init = false;
	unsigned init_flags = 0;

	ret = gnutls_global_init();
	if (ret != 0) {
		fprintf(stderr, "error at %d: %s\n", __LINE__, gnutls_strerror(ret));
		exit(1);
	}

	gnutls_global_set_log_function(tls_log_func);
	//gnutls_global_set_log_level(4711);

	while((opt = getopt(argc, argv, "s:o:mvatdpi")) != -1) {
		switch(opt) {
			case 'o':
				mod = strdup(optarg);
				p11_kit_override_system_files(NULL, NULL, mod, mod, NULL);
				break;
			case 'm':
				/* initialize manually - i.e., do no module loading */
				need_init = true;
				init_flags |= GNUTLS_PKCS11_FLAG_MANUAL;
				break;
			case 's':
				/* load module */
				ret = gnutls_pkcs11_add_provider(optarg, NULL);
				if (ret != 0) {
					fprintf(stderr, "error at %d: %s\n", __LINE__, gnutls_strerror(ret));
					exit(1);
				}
				break;
			case 'd':
				/* when call _gnutls_pkcs11_token_get_url() do proper initialization
				 * if none done */
				flag = 0;
				break;
			case 'p':
				/* do private key operations */
				assert(gnutls_pkcs11_privkey_init(&key) >= 0);
				gnutls_pkcs11_privkey_import_url(key, "pkcs11:", 0);
				gnutls_pkcs11_privkey_deinit(key);
				break;
			case 'a':
				/* initialize auto - i.e., do module loading */
				need_init = true;
				init_flags |= GNUTLS_PKCS11_FLAG_AUTO;
				break;
			case 't':
				/* do trusted module loading */
				need_init = true;
				init_flags |= GNUTLS_PKCS11_FLAG_AUTO_TRUSTED;
				break;
			case 'i':
				/* ignore duplicate modules */
				init_flags |= GNUTLS_PKCS11_FLAG_IGNORE_DUPLICATE;
				break;
			case 'v':
				/* do verification which should trigger trusted module loading */
				assert(gnutls_x509_crt_init(&crt) >= 0);
				assert(gnutls_x509_crt_import(crt, &ca3_cert, GNUTLS_X509_FMT_PEM) >= 0);

				assert(gnutls_x509_trust_list_init(&tl, 0) >= 0);
				assert(gnutls_x509_trust_list_add_system_trust(tl, 0, 0) >= 0);
				gnutls_x509_trust_list_verify_crt2(tl, &crt, 1, NULL, 0, 0, &status, NULL);
				gnutls_x509_trust_list_deinit(tl, 1);
				gnutls_x509_crt_deinit(crt);
				break;
			default:
				fprintf(stderr, "Unknown option %c\n", (char)opt);
				exit(1);
		}
	}

	if (need_init) {
		ret = gnutls_pkcs11_init(init_flags, NULL);
		if (ret != 0) {
			fprintf(stderr, "error at %d: %s\n", __LINE__, gnutls_strerror(ret));
			exit(1);
		}
	}

	for (i=0;;i++) {
		ret = _gnutls_pkcs11_token_get_url(i, 0, &url, flag);
		if (ret < 0)
			break;
		printf("%s\n", url);
		free(url);
	}

	/* try whether these URIs are operational */
	for (i=0;;i++) {
		unsigned tflags;

		ret = _gnutls_pkcs11_token_get_url(i, 0, &url, flag);
		if (ret < 0)
			break;
		ret = gnutls_pkcs11_token_get_flags(url, &tflags);
		if (ret < 0) {
			fprintf(stderr, "cannot get token %s flags: %s\n", url, gnutls_strerror(ret));
			exit(1);
		}
		free(url);
	}

	gnutls_global_deinit();
}