Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (C) 2001-2012 Free Software Foundation, Inc. |
3 | | * Copyright (C) 2017 Red Hat, Inc. |
4 | | * |
5 | | * Author: Nikos Mavrogiannopoulos |
6 | | * |
7 | | * This file is part of GnuTLS. |
8 | | * |
9 | | * The GnuTLS is free software; you can redistribute it and/or |
10 | | * modify it under the terms of the GNU Lesser General Public License |
11 | | * as published by the Free Software Foundation; either version 2.1 of |
12 | | * the License, or (at your option) any later version. |
13 | | * |
14 | | * This library is distributed in the hope that it will be useful, but |
15 | | * WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
17 | | * Lesser General Public License for more details. |
18 | | * |
19 | | * You should have received a copy of the GNU Lesser General Public License |
20 | | * along with this program. If not, see <https://www.gnu.org/licenses/> |
21 | | * |
22 | | */ |
23 | | |
24 | | #include "gnutls_int.h" |
25 | | #include "errors.h" |
26 | | #include "auth.h" |
27 | | #include "auth.h" |
28 | | #include "algorithms.h" |
29 | | #include "auth/cert.h" |
30 | | #include "auth/psk.h" |
31 | | #include "auth/srp_kx.h" |
32 | | #include "auth/anon.h" |
33 | | #include "datum.h" |
34 | | |
35 | | /* The functions here are used in order for authentication algorithms |
36 | | * to be able to retrieve the needed credentials eg public and private |
37 | | * key etc. |
38 | | */ |
39 | | |
40 | | /** |
41 | | * gnutls_credentials_clear: |
42 | | * @session: is a #gnutls_session_t type. |
43 | | * |
44 | | * Clears all the credentials previously set in this session. |
45 | | **/ |
46 | | void gnutls_credentials_clear(gnutls_session_t session) |
47 | 0 | { |
48 | 0 | if (session->key.cred) { /* beginning of the list */ |
49 | 0 | auth_cred_st *ccred, *ncred; |
50 | 0 | ccred = session->key.cred; |
51 | 0 | while (ccred != NULL) { |
52 | 0 | ncred = ccred->next; |
53 | 0 | gnutls_free(ccred); |
54 | 0 | ccred = ncred; |
55 | 0 | } |
56 | 0 | session->key.cred = NULL; |
57 | 0 | } |
58 | 0 | } |
59 | | |
60 | | /* |
61 | | * This creates a linked list of the form: |
62 | | * { algorithm, credentials, pointer to next } |
63 | | */ |
64 | | /** |
65 | | * gnutls_credentials_set: |
66 | | * @session: is a #gnutls_session_t type. |
67 | | * @type: is the type of the credentials |
68 | | * @cred: the credentials to set |
69 | | * |
70 | | * Sets the needed credentials for the specified type. E.g. username, |
71 | | * password - or public and private keys etc. The @cred parameter is |
72 | | * a structure that depends on the specified type and on the current |
73 | | * session (client or server). |
74 | | * |
75 | | * In order to minimize memory usage, and share credentials between |
76 | | * several threads gnutls keeps a pointer to cred, and not the whole |
77 | | * cred structure. Thus you will have to keep the structure allocated |
78 | | * until you call gnutls_deinit(). |
79 | | * |
80 | | * For %GNUTLS_CRD_ANON, @cred should be |
81 | | * #gnutls_anon_client_credentials_t in case of a client. In case of |
82 | | * a server it should be #gnutls_anon_server_credentials_t. |
83 | | * |
84 | | * For %GNUTLS_CRD_SRP, @cred should be #gnutls_srp_client_credentials_t |
85 | | * in case of a client, and #gnutls_srp_server_credentials_t, in case |
86 | | * of a server. |
87 | | * |
88 | | * For %GNUTLS_CRD_CERTIFICATE, @cred should be |
89 | | * #gnutls_certificate_credentials_t. |
90 | | * |
91 | | * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, |
92 | | * otherwise a negative error code is returned. |
93 | | **/ |
94 | | int gnutls_credentials_set(gnutls_session_t session, |
95 | | gnutls_credentials_type_t type, void *cred) |
96 | 0 | { |
97 | 0 | auth_cred_st *ccred = NULL, *pcred = NULL; |
98 | 0 | int exists = 0; |
99 | |
|
100 | 0 | if (session->key.cred == NULL) { /* beginning of the list */ |
101 | |
|
102 | 0 | session->key.cred = gnutls_malloc(sizeof(auth_cred_st)); |
103 | 0 | if (session->key.cred == NULL) |
104 | 0 | return GNUTLS_E_MEMORY_ERROR; |
105 | | |
106 | | /* copy credentials locally */ |
107 | 0 | session->key.cred->credentials = cred; |
108 | |
|
109 | 0 | session->key.cred->next = NULL; |
110 | 0 | session->key.cred->algorithm = type; |
111 | 0 | } else { |
112 | 0 | ccred = session->key.cred; |
113 | 0 | while (ccred != NULL) { |
114 | 0 | if (ccred->algorithm == type) { |
115 | 0 | exists = 1; |
116 | 0 | break; |
117 | 0 | } |
118 | 0 | pcred = ccred; |
119 | 0 | ccred = ccred->next; |
120 | 0 | } |
121 | | /* After this, pcred is not null. |
122 | | */ |
123 | |
|
124 | 0 | if (exists == 0) { /* new entry */ |
125 | 0 | pcred->next = gnutls_malloc(sizeof(auth_cred_st)); |
126 | 0 | if (pcred->next == NULL) |
127 | 0 | return GNUTLS_E_MEMORY_ERROR; |
128 | | |
129 | 0 | ccred = pcred->next; |
130 | | |
131 | | /* copy credentials locally */ |
132 | 0 | ccred->credentials = cred; |
133 | |
|
134 | 0 | ccred->next = NULL; |
135 | 0 | ccred->algorithm = type; |
136 | 0 | } else { /* modify existing entry */ |
137 | 0 | ccred->credentials = cred; |
138 | 0 | } |
139 | 0 | } |
140 | | |
141 | | /* sanity tests */ |
142 | 0 | if (type == GNUTLS_CRD_CERTIFICATE) { |
143 | 0 | gnutls_certificate_credentials_t c = cred; |
144 | 0 | unsigned i; |
145 | 0 | bool allow_tls13 = 0; |
146 | 0 | unsigned key_usage; |
147 | |
|
148 | 0 | if (c != NULL && c->ncerts != 0) { |
149 | 0 | for (i = 0; i < c->ncerts; i++) { |
150 | 0 | key_usage = get_key_usage( |
151 | 0 | session, |
152 | 0 | c->certs[i].cert_list[0].pubkey); |
153 | 0 | if (key_usage == 0 || |
154 | 0 | (key_usage & |
155 | 0 | GNUTLS_KEY_DIGITAL_SIGNATURE)) { |
156 | 0 | allow_tls13 = 1; |
157 | 0 | break; |
158 | 0 | } |
159 | 0 | } |
160 | |
|
161 | 0 | if (session->security_parameters.entity == |
162 | 0 | GNUTLS_SERVER && |
163 | 0 | !c->tls13_ok) |
164 | 0 | allow_tls13 = 0; |
165 | |
|
166 | 0 | if (!allow_tls13) { |
167 | | /* to prevent the server random indicate TLS1.3 support */ |
168 | 0 | session->internals.flags |= INT_FLAG_NO_TLS13; |
169 | 0 | } |
170 | 0 | } |
171 | 0 | } |
172 | |
|
173 | 0 | return 0; |
174 | 0 | } |
175 | | |
176 | | /** |
177 | | * gnutls_credentials_get: |
178 | | * @session: is a #gnutls_session_t type. |
179 | | * @type: is the type of the credentials to return |
180 | | * @cred: will contain the credentials. |
181 | | * |
182 | | * Returns the previously provided credentials structures. |
183 | | * |
184 | | * For %GNUTLS_CRD_ANON, @cred will be |
185 | | * #gnutls_anon_client_credentials_t in case of a client. In case of |
186 | | * a server it should be #gnutls_anon_server_credentials_t. |
187 | | * |
188 | | * For %GNUTLS_CRD_SRP, @cred will be #gnutls_srp_client_credentials_t |
189 | | * in case of a client, and #gnutls_srp_server_credentials_t, in case |
190 | | * of a server. |
191 | | * |
192 | | * For %GNUTLS_CRD_CERTIFICATE, @cred will be |
193 | | * #gnutls_certificate_credentials_t. |
194 | | * |
195 | | * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, |
196 | | * otherwise a negative error code is returned. |
197 | | * |
198 | | * Since: 3.3.3 |
199 | | **/ |
200 | | int gnutls_credentials_get(gnutls_session_t session, |
201 | | gnutls_credentials_type_t type, void **cred) |
202 | 0 | { |
203 | 0 | const void *_cred; |
204 | |
|
205 | 0 | _cred = _gnutls_get_cred(session, type); |
206 | 0 | if (_cred == NULL) |
207 | 0 | return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST); |
208 | | |
209 | 0 | if (cred) |
210 | 0 | *cred = (void *)_cred; |
211 | |
|
212 | 0 | return 0; |
213 | 0 | } |
214 | | |
215 | | /** |
216 | | * gnutls_auth_get_type: |
217 | | * @session: is a #gnutls_session_t type. |
218 | | * |
219 | | * Returns type of credentials for the current authentication schema. |
220 | | * The returned information is to be used to distinguish the function used |
221 | | * to access authentication data. |
222 | | * |
223 | | * Eg. for CERTIFICATE ciphersuites (key exchange algorithms: |
224 | | * %GNUTLS_KX_RSA, %GNUTLS_KX_DHE_RSA), the same function are to be |
225 | | * used to access the authentication data. |
226 | | * |
227 | | * Note that on resumed sessions, this function returns the schema |
228 | | * used in the original session authentication. |
229 | | * |
230 | | * Returns: The type of credentials for the current authentication |
231 | | * schema, a #gnutls_credentials_type_t type. |
232 | | **/ |
233 | | gnutls_credentials_type_t gnutls_auth_get_type(gnutls_session_t session) |
234 | 0 | { |
235 | 0 | if (session->security_parameters.entity == GNUTLS_SERVER) |
236 | 0 | return gnutls_auth_client_get_type(session); |
237 | 0 | else |
238 | 0 | return gnutls_auth_server_get_type(session); |
239 | 0 | } |
240 | | |
241 | | /** |
242 | | * gnutls_auth_server_get_type: |
243 | | * @session: is a #gnutls_session_t type. |
244 | | * |
245 | | * Returns the type of credentials that were used for server authentication. |
246 | | * The returned information is to be used to distinguish the function used |
247 | | * to access authentication data. |
248 | | * |
249 | | * Note that on resumed sessions, this function returns the schema |
250 | | * used in the original session authentication. |
251 | | * |
252 | | * Returns: The type of credentials for the server authentication |
253 | | * schema, a #gnutls_credentials_type_t type. |
254 | | **/ |
255 | | gnutls_credentials_type_t gnutls_auth_server_get_type(gnutls_session_t session) |
256 | 0 | { |
257 | 0 | return session->security_parameters.server_auth_type; |
258 | 0 | } |
259 | | |
260 | | /** |
261 | | * gnutls_auth_client_get_type: |
262 | | * @session: is a #gnutls_session_t type. |
263 | | * |
264 | | * Returns the type of credentials that were used for client authentication. |
265 | | * The returned information is to be used to distinguish the function used |
266 | | * to access authentication data. |
267 | | * |
268 | | * Note that on resumed sessions, this function returns the schema |
269 | | * used in the original session authentication. |
270 | | * |
271 | | * Returns: The type of credentials for the client authentication |
272 | | * schema, a #gnutls_credentials_type_t type. |
273 | | **/ |
274 | | gnutls_credentials_type_t gnutls_auth_client_get_type(gnutls_session_t session) |
275 | 0 | { |
276 | 0 | return session->security_parameters.client_auth_type; |
277 | 0 | } |
278 | | |
279 | | /* |
280 | | * This returns a pointer to the linked list. Don't |
281 | | * free that!!! |
282 | | */ |
283 | | const void *_gnutls_get_kx_cred(gnutls_session_t session, |
284 | | gnutls_kx_algorithm_t algo) |
285 | 0 | { |
286 | 0 | int server = session->security_parameters.entity == GNUTLS_SERVER ? 1 : |
287 | 0 | 0; |
288 | |
|
289 | 0 | return _gnutls_get_cred(session, _gnutls_map_kx_get_cred(algo, server)); |
290 | 0 | } |
291 | | |
292 | | const void *_gnutls_get_cred(gnutls_session_t session, |
293 | | gnutls_credentials_type_t type) |
294 | 0 | { |
295 | 0 | auth_cred_st *ccred; |
296 | 0 | gnutls_key_st *key = &session->key; |
297 | |
|
298 | 0 | ccred = key->cred; |
299 | 0 | while (ccred != NULL) { |
300 | 0 | if (ccred->algorithm == type) { |
301 | 0 | break; |
302 | 0 | } |
303 | 0 | ccred = ccred->next; |
304 | 0 | } |
305 | 0 | if (ccred == NULL) |
306 | 0 | return NULL; |
307 | | |
308 | 0 | return ccred->credentials; |
309 | 0 | } |
310 | | |
311 | | /*- |
312 | | * _gnutls_free_auth_info - Frees the auth info structure |
313 | | * @session: is a #gnutls_session_t type. |
314 | | * |
315 | | * This function frees the auth info structure and sets it to |
316 | | * null. It must be called since some structures contain malloced |
317 | | * elements. |
318 | | -*/ |
319 | | void _gnutls_free_auth_info(gnutls_session_t session) |
320 | 0 | { |
321 | 0 | dh_info_st *dh_info; |
322 | |
|
323 | 0 | if (session == NULL) { |
324 | 0 | gnutls_assert(); |
325 | 0 | return; |
326 | 0 | } |
327 | | |
328 | 0 | switch (session->key.auth_info_type) { |
329 | 0 | case GNUTLS_CRD_SRP: { |
330 | 0 | srp_server_auth_info_t info = |
331 | 0 | _gnutls_get_auth_info(session, GNUTLS_CRD_SRP); |
332 | |
|
333 | 0 | if (info == NULL) |
334 | 0 | break; |
335 | | |
336 | 0 | gnutls_free(info->username); |
337 | 0 | info->username = NULL; |
338 | 0 | } break; |
339 | 0 | #ifdef ENABLE_ANON |
340 | 0 | case GNUTLS_CRD_ANON: { |
341 | 0 | anon_auth_info_t info = |
342 | 0 | _gnutls_get_auth_info(session, GNUTLS_CRD_ANON); |
343 | |
|
344 | 0 | if (info == NULL) |
345 | 0 | break; |
346 | | |
347 | 0 | dh_info = &info->dh; |
348 | 0 | _gnutls_free_dh_info(dh_info); |
349 | 0 | } break; |
350 | 0 | #endif |
351 | 0 | case GNUTLS_CRD_PSK: { |
352 | 0 | psk_auth_info_t info = |
353 | 0 | _gnutls_get_auth_info(session, GNUTLS_CRD_PSK); |
354 | |
|
355 | 0 | if (info == NULL) |
356 | 0 | break; |
357 | | |
358 | 0 | gnutls_free(info->username); |
359 | 0 | info->username = NULL; |
360 | 0 | info->username_len = 0; |
361 | |
|
362 | 0 | gnutls_free(info->hint); |
363 | 0 | info->hint = NULL; |
364 | 0 | info->hint_len = 0; |
365 | |
|
366 | 0 | #ifdef ENABLE_DHE |
367 | 0 | dh_info = &info->dh; |
368 | 0 | _gnutls_free_dh_info(dh_info); |
369 | 0 | #endif |
370 | 0 | } break; |
371 | 0 | case GNUTLS_CRD_CERTIFICATE: { |
372 | 0 | unsigned int i; |
373 | 0 | cert_auth_info_t info = |
374 | 0 | _gnutls_get_auth_info(session, GNUTLS_CRD_CERTIFICATE); |
375 | |
|
376 | 0 | if (info == NULL) |
377 | 0 | break; |
378 | | |
379 | 0 | dh_info = &info->dh; |
380 | 0 | for (i = 0; i < info->ncerts; i++) { |
381 | 0 | _gnutls_free_datum(&info->raw_certificate_list[i]); |
382 | 0 | } |
383 | |
|
384 | 0 | for (i = 0; i < info->nocsp; i++) { |
385 | 0 | _gnutls_free_datum(&info->raw_ocsp_list[i]); |
386 | 0 | } |
387 | |
|
388 | 0 | gnutls_free(info->raw_certificate_list); |
389 | 0 | gnutls_free(info->raw_ocsp_list); |
390 | 0 | info->ncerts = 0; |
391 | 0 | info->nocsp = 0; |
392 | |
|
393 | 0 | #ifdef ENABLE_DHE |
394 | 0 | _gnutls_free_dh_info(dh_info); |
395 | 0 | #endif |
396 | 0 | } |
397 | | |
398 | 0 | break; |
399 | 0 | default: |
400 | 0 | return; |
401 | 0 | } |
402 | | |
403 | 0 | gnutls_free(session->key.auth_info); |
404 | 0 | session->key.auth_info_size = 0; |
405 | 0 | session->key.auth_info_type = 0; |
406 | 0 | } |
407 | | |
408 | | /* This function will create the auth info structure in the key |
409 | | * structure if needed. |
410 | | * |
411 | | * If allow change is !=0 then this will allow changing the auth |
412 | | * info structure to a different type. |
413 | | */ |
414 | | int _gnutls_auth_info_init(gnutls_session_t session, |
415 | | gnutls_credentials_type_t type, int size, |
416 | | int allow_change) |
417 | 0 | { |
418 | 0 | if (session->key.auth_info == NULL) { |
419 | 0 | session->key.auth_info = gnutls_calloc(1, size); |
420 | 0 | if (session->key.auth_info == NULL) { |
421 | 0 | gnutls_assert(); |
422 | 0 | return GNUTLS_E_MEMORY_ERROR; |
423 | 0 | } |
424 | 0 | session->key.auth_info_type = type; |
425 | 0 | session->key.auth_info_size = size; |
426 | 0 | } else { |
427 | 0 | if (allow_change == 0) { |
428 | | /* If the credentials for the current authentication scheme, |
429 | | * are not the one we want to set, then it's an error. |
430 | | * This may happen if a rehandshake is performed an the |
431 | | * ciphersuite which is negotiated has different authentication |
432 | | * schema. |
433 | | */ |
434 | 0 | if (type != session->key.auth_info_type) { |
435 | 0 | gnutls_assert(); |
436 | 0 | return GNUTLS_E_INVALID_REQUEST; |
437 | 0 | } |
438 | 0 | } else { |
439 | | /* The new behaviour: Here we reallocate the auth info structure |
440 | | * in order to be able to negotiate different authentication |
441 | | * types. Ie. perform an auth_anon and then authenticate again using a |
442 | | * certificate (in order to prevent revealing the certificate's contents, |
443 | | * to passive eavesdropers. |
444 | | */ |
445 | 0 | if (type != session->key.auth_info_type) { |
446 | 0 | _gnutls_free_auth_info(session); |
447 | |
|
448 | 0 | session->key.auth_info = gnutls_calloc(1, size); |
449 | 0 | if (session->key.auth_info == NULL) { |
450 | 0 | gnutls_assert(); |
451 | 0 | return GNUTLS_E_MEMORY_ERROR; |
452 | 0 | } |
453 | | |
454 | 0 | session->key.auth_info_type = type; |
455 | 0 | session->key.auth_info_size = size; |
456 | 0 | } |
457 | 0 | } |
458 | 0 | } |
459 | 0 | return 0; |
460 | 0 | } |