summaryrefslogtreecommitdiff
path: root/lib/urls.c
blob: f420646c038f1aec4d69e175db408b95fb2fa0cf (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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
 * Copyright © 2014 Nikos Mavrogiannopoulos
 *
 * Author: Nikos Mavrogiannopoulos
 *
 * GnuTLS is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser 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 <http://www.gnu.org/licenses/>
 *
 */

#include <gnutls_int.h>
#include <gnutls_errors.h>
#include <gnutls_str.h>
#include "urls.h"
#include "system-keys.h"

#define MAX_CUSTOM_URLS 8

gnutls_custom_url_st _gnutls_custom_urls[MAX_CUSTOM_URLS];
unsigned _gnutls_custom_urls_size = 0;

static const char *_types[] =
{ "object-type=cert", "object-type=private", NULL };

static char *append_to_str(const char *str1, const char *str2)
{
	char *str = NULL;
	gnutls_buffer_st buf;
	int ret;

	_gnutls_buffer_init(&buf);

	ret = _gnutls_buffer_append_str(&buf, str1);
	if (ret < 0) {
		goto cleanup;
	}

	ret = _gnutls_buffer_append_data(&buf, ";", 1);
	if (ret < 0) {
		goto cleanup;
	}

	ret = _gnutls_buffer_append_str(&buf, str2);
	if (ret < 0) {
		goto cleanup;
	}

	ret = _gnutls_buffer_append_data(&buf, "\x00", 1);
	if (ret < 0) {
		goto cleanup;
	}

	str = (void*)buf.data;
	ret = 0;
fprintf(stderr, "str: %s\n", str);
 cleanup:
 	if (ret < 0) {
 		_gnutls_buffer_clear(&buf);
 	}
 	return str;	

}

/*
 * @type: 0 for cert, 1 for privkey
 *
 * This function will make sure that the URL is ok (e.g.,
 * that it contains type=cert, when it is a certificate,
 * or type=privkey for PKCS #11 URLs. That allows to use
 * the common URL part as input for keys and certificates.
 *
 *
 */
char *_gnutls_sanitize_url(const char *url, unsigned type)
{
#ifdef ENABLE_PKCS11
	if (strncmp(url, "pkcs11:", 7) == 0) {
		if (strstr(url, _types[type]) != NULL) {
			return gnutls_strdup(url);
		} else {
			return append_to_str(url, _types[type]);
		}
	} else
#endif
	{
		return gnutls_strdup(url);
	}
}

/**
 * gnutls_url_is_supported:
 * @url: A PKCS 11 url
 *
 * Check whether url is supported.  Depending on the system libraries
 * GnuTLS may support pkcs11 or tpmkey URLs.
 *
 * Returns: return non-zero if the given URL is supported, and zero if
 * it is not known.
 *
 * Since: 3.1.0
 **/
int gnutls_url_is_supported(const char *url)
{
	unsigned i;
#ifdef ENABLE_PKCS11
	if (strncmp(url, PKCS11_URL, sizeof(PKCS11_URL)-1) == 0)
		return 1;
#endif
#ifdef HAVE_TROUSERS
	if (strncmp(url, TPMKEY_URL, sizeof(TPMKEY_URL)-1) == 0)
		return 1;
#endif
	if (strncmp(url, SYSTEM_URL, sizeof(SYSTEM_URL)-1) == 0)
		return _gnutls_system_url_is_supported(url);

	for (i=0;i<_gnutls_custom_urls_size;i++) {
		if (strncmp(url, _gnutls_custom_urls[i].name, _gnutls_custom_urls[i].name_size) == 0)
			return 1;
	}

	return 0;
}

int _gnutls_url_is_known(const char *url)
{
	unsigned i;

	if (strncmp(url, PKCS11_URL, sizeof(PKCS11_URL)-1) == 0)
		return 1;
	else if (strncmp(url, TPMKEY_URL, sizeof(TPMKEY_URL)-1) == 0)
		return 1;
	else if (strncmp(url, SYSTEM_URL, sizeof(SYSTEM_URL)-1) == 0)
		return 1;
	else {
		for (i=0;i<_gnutls_custom_urls_size;i++) {
			if (strncmp(url, _gnutls_custom_urls[i].name, _gnutls_custom_urls[i].name_size) == 0)
				return 1;
		}

		return 0;
	}
}

/**
 * gnutls_register_custom_url:
 * @st: A %gnutls_custom_url_st structure
 *
 * Register a custom URL. This will affect the following functions:
 * gnutls_url_is_supported(), gnutls_privkey_import_url(),
 * gnutls_pubkey_import_url, gnutls_x509_crt_import_url() 
 * and all functions that depend on
 * them, e.g., gnutls_certificate_set_x509_key_file2().
 *
 * The provided structure and callback functions must be valid throughout
 * the lifetime of the process. The registration of an existing URL type
 * will fail with %GNUTLS_E_INVALID_REQUEST.
 *
 * This function is not thread safe.
 *
 * Returns: returns zero if the given structure was imported or a negative value otherwise.
 *
 * Since: 3.4.0
 **/
int gnutls_register_custom_url(const gnutls_custom_url_st *st)
{
	unsigned i;

	for (i=0;i<_gnutls_custom_urls_size;i++) {
		if (_gnutls_custom_urls[i].name_size == st->name_size &&
		    strcmp(_gnutls_custom_urls[i].name, st->name) == 0) {
		    return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
		}
	}

	if (_gnutls_custom_urls_size < MAX_CUSTOM_URLS-1) {
		memcpy(&_gnutls_custom_urls[_gnutls_custom_urls_size], st, sizeof(*st));
		_gnutls_custom_urls_size++;
		return 0;
	} else {
		return gnutls_assert_val(GNUTLS_E_UNIMPLEMENTED_FEATURE);
	}
}