/src/gnutls/lib/handshake-checks.c
Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | /*  | 
2  |  |  * Copyright (C) 2015-2016 Red Hat, Inc.  | 
3  |  |  *  | 
4  |  |  * Author: Nikos Mavrogiannopoulos  | 
5  |  |  *  | 
6  |  |  * This file is part of GnuTLS.  | 
7  |  |  *  | 
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.  | 
12  |  |  *  | 
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.  | 
17  |  |  *  | 
18  |  |  * You should have received a copy of the GNU Lesser General Public License  | 
19  |  |  * along with this program.  If not, see <https://www.gnu.org/licenses/>  | 
20  |  |  *  | 
21  |  |  */  | 
22  |  |  | 
23  |  | /* Functions that relate to the TLS handshake procedure.  | 
24  |  |  */  | 
25  |  |  | 
26  |  | #include "gnutls_int.h"  | 
27  |  | #include "errors.h"  | 
28  |  | #include "debug.h"  | 
29  |  | #include "handshake.h"  | 
30  |  | #include "auth/cert.h"  | 
31  |  | #include "constate.h"  | 
32  |  | #include "record.h"  | 
33  |  | #include "state.h"  | 
34  |  | #include "ext/safe_renegotiation.h"  | 
35  |  | #include "auth/anon.h" /* for gnutls_anon_server_credentials_t */  | 
36  |  | #include "auth/psk.h" /* for gnutls_psk_server_credentials_t */  | 
37  |  | #ifdef ENABLE_SRP  | 
38  |  | #include "auth/srp_kx.h"  | 
39  |  | #endif  | 
40  |  |  | 
41  |  | int _gnutls_check_id_for_change(gnutls_session_t session)  | 
42  | 0  | { | 
43  | 0  |   int cred_type;  | 
44  |  |  | 
45  |  |   /* This checks in PSK and SRP ciphersuites that the username remained the  | 
46  |  |    * same on a rehandshake. */  | 
47  | 0  |   if (session->internals.flags & GNUTLS_ALLOW_ID_CHANGE)  | 
48  | 0  |     return 0;  | 
49  |  |  | 
50  | 0  |   cred_type = gnutls_auth_get_type(session);  | 
51  | 0  |   if (cred_type == GNUTLS_CRD_PSK || cred_type == GNUTLS_CRD_SRP) { | 
52  | 0  |     const char *username = NULL;  | 
53  | 0  |     int username_length;  | 
54  |  | 
  | 
55  | 0  |     if (cred_type == GNUTLS_CRD_PSK) { | 
56  | 0  |       psk_auth_info_t ai;  | 
57  |  | 
  | 
58  | 0  |       ai = _gnutls_get_auth_info(session, GNUTLS_CRD_PSK);  | 
59  | 0  |       if (ai == NULL)  | 
60  | 0  |         return gnutls_assert_val(  | 
61  | 0  |           GNUTLS_E_INTERNAL_ERROR);  | 
62  |  |  | 
63  | 0  |       username = ai->username;  | 
64  | 0  |       username_length = ai->username_len;  | 
65  |  | #ifdef ENABLE_SRP  | 
66  |  |     } else { | 
67  |  |       srp_server_auth_info_t ai =  | 
68  |  |         _gnutls_get_auth_info(session, GNUTLS_CRD_SRP);  | 
69  |  |       if (ai == NULL)  | 
70  |  |         return gnutls_assert_val(  | 
71  |  |           GNUTLS_E_INTERNAL_ERROR);  | 
72  |  |  | 
73  |  |       username = ai->username;  | 
74  |  |       username_length = strlen(ai->username);  | 
75  |  | #endif  | 
76  | 0  |     }  | 
77  |  |  | 
78  | 0  |     if (username == NULL)  | 
79  | 0  |       return gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR);  | 
80  |  |  | 
81  | 0  |     if (session->internals.saved_username &&  | 
82  | 0  |         session->internals.saved_username_size != -1) { | 
83  | 0  |       if (session->internals.saved_username_size ==  | 
84  | 0  |             username_length &&  | 
85  | 0  |           strncmp(session->internals.saved_username, username,  | 
86  | 0  |             username_length)) { | 
87  | 0  |         _gnutls_debug_log(  | 
88  | 0  |           "Session's PSK username changed during rehandshake; aborting!\n");  | 
89  | 0  |         return gnutls_assert_val(  | 
90  | 0  |           GNUTLS_E_SESSION_USER_ID_CHANGED);  | 
91  | 0  |       }  | 
92  | 0  |     } else if (session->internals.saved_username == NULL &&  | 
93  | 0  |          session->internals.saved_username_size == -1) { | 
94  | 0  |       if (username_length > MAX_USERNAME_SIZE)  | 
95  | 0  |         return gnutls_assert_val(  | 
96  | 0  |           GNUTLS_E_INTERNAL_ERROR);  | 
97  | 0  |       char *tmp = gnutls_malloc(username_length + 1);  | 
98  | 0  |       if (tmp == NULL)  | 
99  | 0  |         return gnutls_assert_val(GNUTLS_E_MEMORY_ERROR);  | 
100  | 0  |       memcpy(tmp, username, username_length);  | 
101  | 0  |       tmp[username_length] = '\0';  | 
102  | 0  |       session->internals.saved_username = tmp;  | 
103  | 0  |       session->internals.saved_username_size =  | 
104  | 0  |         username_length;  | 
105  | 0  |     } else  | 
106  | 0  |       return gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR);  | 
107  | 0  |   }  | 
108  |  |  | 
109  | 0  |   return 0;  | 
110  | 0  | }  | 
111  |  |  | 
112  |  | int _gnutls_check_if_cert_hash_is_same(gnutls_session_t session,  | 
113  |  |                gnutls_certificate_credentials_t cred)  | 
114  | 0  | { | 
115  | 0  |   cert_auth_info_t ai;  | 
116  | 0  |   char tmp[32];  | 
117  | 0  |   int ret;  | 
118  |  | 
  | 
119  | 0  |   if (session->internals.flags & GNUTLS_ALLOW_ID_CHANGE)  | 
120  | 0  |     return 0;  | 
121  |  |  | 
122  | 0  |   ai = _gnutls_get_auth_info(session, GNUTLS_CRD_CERTIFICATE);  | 
123  | 0  |   if (ai == NULL || ai->ncerts == 0)  | 
124  | 0  |     return 0;  | 
125  |  |  | 
126  | 0  |   ret = gnutls_hash_fast(GNUTLS_DIG_SHA256,  | 
127  | 0  |              ai->raw_certificate_list[0].data,  | 
128  | 0  |              ai->raw_certificate_list[0].size, tmp);  | 
129  | 0  |   if (ret < 0)  | 
130  | 0  |     return gnutls_assert_val(ret);  | 
131  |  |  | 
132  | 0  |   if (session->internals.cert_hash_set) { | 
133  | 0  |     if (memcmp(tmp, session->internals.cert_hash, 32) != 0) { | 
134  | 0  |       _gnutls_debug_log(  | 
135  | 0  |         "Session certificate changed during rehandshake; aborting!\n");  | 
136  | 0  |       return gnutls_assert_val(  | 
137  | 0  |         GNUTLS_E_SESSION_USER_ID_CHANGED);  | 
138  | 0  |     }  | 
139  | 0  |   } else { | 
140  | 0  |     memcpy(session->internals.cert_hash, tmp, 32);  | 
141  | 0  |     session->internals.cert_hash_set = 1;  | 
142  | 0  |   }  | 
143  |  |  | 
144  | 0  |   return 0;  | 
145  | 0  | }  |