Coverage Report

Created: 2026-08-14 06:46

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/gnutls/lib/auth/rsa_psk.c
Line
Count
Source
1
/*
2
 * Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2009, 2010
3
 * Free Software Foundation, Inc.
4
 *
5
 * Copyright (C) 2011
6
 * Bardenheuer GmbH, Munich and Bundesdruckerei GmbH, Berlin
7
 *
8
 * Copyright (C) 2013 Frank Morgner
9
 * Copyright (C) 2013 Nikos Mavrogiannopoulos
10
 *
11
 * This file is part of GnuTLS.
12
 *
13
 * The GnuTLS is free software; you can redistribute it and/or
14
 * modify it under the terms of the GNU Lesser General Public License
15
 * as published by the Free Software Foundation; either version 2.1 of
16
 * the License, or (at your option) any later version.
17
 *
18
 * This library is distributed in the hope that it will be useful, but
19
 * WITHOUT ANY WARRANTY; without even the implied warranty of
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21
 * Lesser General Public License for more details.
22
 *
23
 * You should have received a copy of the GNU Lesser General Public
24
 * License along with this library.  If not, see <https://www.gnu.org/licenses/>.
25
 *
26
 */
27
28
#include "gnutls_int.h"
29
30
#ifdef ENABLE_PSK
31
32
#include "auth.h"
33
#include "dh.h"
34
#include "errors.h"
35
#include "mpi.h"
36
#include "num.h"
37
#include "gnutls_int.h"
38
#include "pk.h"
39
#include "random.h"
40
#include "abstract_int.h"
41
#include "algorithms.h"
42
#include "auth/dh_common.h"
43
#include "auth/psk.h"
44
#include "auth/psk_passwd.h"
45
#include "auth/rsa_common.h"
46
#include "cert.h"
47
#include "datum.h"
48
#include "state.h"
49
50
static int _gnutls_gen_rsa_psk_client_kx(gnutls_session_t session,
51
           gnutls_buffer_st *data);
52
static int _gnutls_proc_rsa_psk_client_kx(gnutls_session_t, uint8_t *, size_t);
53
static int _gnutls_proc_rsa_psk_server_kx(gnutls_session_t session,
54
            uint8_t *data, size_t data_size);
55
56
const mod_auth_st rsa_psk_auth_struct = {
57
  "RSA PSK",
58
  _gnutls_gen_cert_server_crt,
59
  NULL, /* generate_client_certificate */
60
  _gnutls_gen_psk_server_kx,
61
  _gnutls_gen_rsa_psk_client_kx,
62
  NULL, /* generate_client_cert_vrfy */
63
  NULL, /* generate_server_certificate_request */
64
  _gnutls_proc_crt,
65
  NULL, /* process_client_certificate */
66
  _gnutls_proc_rsa_psk_server_kx,
67
  _gnutls_proc_rsa_psk_client_kx,
68
  NULL, /* process_client_cert_vrfy */
69
  NULL /* process_server_certificate_reuqest */
70
};
71
72
/* Set the PSK premaster secret.
73
 */
74
static int set_rsa_psk_session_key(gnutls_session_t session,
75
           gnutls_datum_t *ppsk,
76
           gnutls_datum_t *rsa_secret)
77
0
{
78
0
  unsigned char *p;
79
0
  size_t rsa_secret_size;
80
0
  int ret;
81
82
0
  rsa_secret_size = rsa_secret->size;
83
84
  /* set the session key
85
   */
86
0
  session->key.key.size = 2 + rsa_secret_size + 2 + ppsk->size;
87
0
  session->key.key.data = gnutls_malloc(session->key.key.size);
88
0
  if (session->key.key.data == NULL) {
89
0
    gnutls_assert();
90
0
    ret = GNUTLS_E_MEMORY_ERROR;
91
0
    goto error;
92
0
  }
93
94
  /* format of the premaster secret:
95
   * (uint16_t) other_secret size (48)
96
   * other_secret: 2 byte version + 46 byte random
97
   * (uint16_t) psk_size
98
   * the psk
99
   */
100
0
  _gnutls_write_uint16(rsa_secret_size, session->key.key.data);
101
0
  memcpy(&session->key.key.data[2], rsa_secret->data, rsa_secret->size);
102
0
  p = &session->key.key.data[rsa_secret_size + 2];
103
0
  _gnutls_write_uint16(ppsk->size, p);
104
0
  if (ppsk->data != NULL)
105
0
    memcpy(p + 2, ppsk->data, ppsk->size);
106
107
0
  ret = 0;
108
109
0
error:
110
0
  return ret;
111
0
}
112
113
/* Generate client key exchange message
114
 *
115
 *
116
 * struct {
117
 *    select (KeyExchangeAlgorithm) {
118
 *       uint8_t psk_identity<0..2^16-1>;
119
 *       EncryptedPreMasterSecret;
120
 *    } exchange_keys;
121
 * } ClientKeyExchange;
122
 */
123
static int _gnutls_gen_rsa_psk_client_kx(gnutls_session_t session,
124
           gnutls_buffer_st *data)
125
0
{
126
0
  cert_auth_info_t auth = session->key.auth_info;
127
0
  gnutls_datum_t sdata; /* data to send */
128
0
  gnutls_pk_params_st params;
129
0
  gnutls_psk_client_credentials_t cred;
130
0
  gnutls_datum_t username, key;
131
0
  int ret, free;
132
0
  unsigned init_pos;
133
134
0
  if (auth == NULL) {
135
    /* this shouldn't have happened. The proc_certificate
136
     * function should have detected that.
137
     */
138
0
    gnutls_assert();
139
0
    return GNUTLS_E_INSUFFICIENT_CREDENTIALS;
140
0
  }
141
142
0
  gnutls_datum_t premaster_secret;
143
0
  premaster_secret.size = GNUTLS_MASTER_SIZE;
144
0
  premaster_secret.data = gnutls_malloc(premaster_secret.size);
145
146
0
  if (premaster_secret.data == NULL) {
147
0
    gnutls_assert();
148
0
    return GNUTLS_E_MEMORY_ERROR;
149
0
  }
150
151
  /* Generate random */
152
0
  ret = gnutls_rnd(GNUTLS_RND_RANDOM, premaster_secret.data,
153
0
       premaster_secret.size);
154
0
  if (ret < 0) {
155
0
    gnutls_assert();
156
0
    return ret;
157
0
  }
158
159
  /* Set version */
160
0
  if (session->internals.rsa_pms_version[0] == 0) {
161
0
    premaster_secret.data[0] =
162
0
      _gnutls_get_adv_version_major(session);
163
0
    premaster_secret.data[1] =
164
0
      _gnutls_get_adv_version_minor(session);
165
0
  } else { /* use the version provided */
166
0
    premaster_secret.data[0] =
167
0
      session->internals.rsa_pms_version[0];
168
0
    premaster_secret.data[1] =
169
0
      session->internals.rsa_pms_version[1];
170
0
  }
171
172
  /* move RSA parameters to key (session).
173
   */
174
0
  if ((ret = _gnutls_get_public_rsa_params(session, &params)) < 0) {
175
0
    gnutls_assert();
176
0
    return ret;
177
0
  }
178
179
  /* Encrypt premaster secret */
180
0
  if ((ret = _gnutls_pk_encrypt(GNUTLS_PK_RSA, &sdata, &premaster_secret,
181
0
              &params, &params.spki)) < 0) {
182
0
    gnutls_assert();
183
0
    return ret;
184
0
  }
185
186
0
  gnutls_pk_params_release(&params);
187
188
0
  cred = (gnutls_psk_client_credentials_t)_gnutls_get_cred(
189
0
    session, GNUTLS_CRD_PSK);
190
191
0
  if (cred == NULL) {
192
0
    gnutls_assert();
193
0
    return GNUTLS_E_INSUFFICIENT_CREDENTIALS;
194
0
  }
195
196
0
  ret = _gnutls_find_psk_key(session, cred, &username, &key, NULL, &free);
197
0
  if (ret < 0)
198
0
    return gnutls_assert_val(ret);
199
200
  /* Here we set the PSK key */
201
0
  ret = set_rsa_psk_session_key(session, &key, &premaster_secret);
202
0
  if (ret < 0) {
203
0
    gnutls_assert();
204
0
    goto cleanup;
205
0
  }
206
207
  /* Create message for client key exchange
208
   *
209
   * struct {
210
   *   uint8_t psk_identity<0..2^16-1>;
211
   *   EncryptedPreMasterSecret;
212
   * }
213
   */
214
215
0
  init_pos = data->length;
216
217
  /* Write psk_identity and EncryptedPreMasterSecret into data stream
218
   */
219
0
  ret = _gnutls_buffer_append_data_prefix16(data, username.data,
220
0
              username.size);
221
0
  if (ret < 0) {
222
0
    gnutls_assert();
223
0
    goto cleanup;
224
0
  }
225
226
0
  ret = _gnutls_buffer_append_data_prefix16(data, sdata.data, sdata.size);
227
0
  if (ret < 0) {
228
0
    gnutls_assert();
229
0
    goto cleanup;
230
0
  }
231
232
0
  ret = data->length - init_pos;
233
234
0
cleanup:
235
0
  _gnutls_free_datum(&sdata);
236
0
  _gnutls_free_key_datum(&premaster_secret);
237
0
  if (free) {
238
0
    _gnutls_free_key_datum(&key);
239
0
    gnutls_free(username.data);
240
0
  }
241
242
0
  return ret;
243
0
}
244
245
/*
246
  Process the client key exchange message
247
*/
248
static int _gnutls_proc_rsa_psk_client_kx(gnutls_session_t session,
249
            uint8_t *data, size_t data_size)
250
0
{
251
0
  gnutls_datum_t username;
252
0
  psk_auth_info_t info;
253
0
  gnutls_datum_t ciphertext;
254
0
  gnutls_datum_t pwd_psk = { NULL, 0 };
255
0
  int ret;
256
0
  gnutls_psk_server_credentials_t cred;
257
0
  volatile uint8_t ver_maj, ver_min;
258
0
  unsigned int rsa_key_bits;
259
0
  size_t dsize;
260
261
0
  cred = (gnutls_psk_server_credentials_t)_gnutls_get_cred(
262
0
    session, GNUTLS_CRD_PSK);
263
264
0
  if (cred == NULL) {
265
0
    gnutls_assert();
266
0
    return GNUTLS_E_INSUFFICIENT_CREDENTIALS;
267
0
  }
268
269
0
  ret = _gnutls_auth_info_init(session, GNUTLS_CRD_PSK,
270
0
             sizeof(psk_auth_info_st), 1);
271
0
  if (ret < 0) {
272
0
    gnutls_assert();
273
0
    return ret;
274
0
  }
275
276
  /*** 1. Extract user psk_identity ***/
277
278
0
  DECR_LEN(data_size, 2);
279
0
  username.size = _gnutls_read_uint16(&data[0]);
280
281
0
  DECR_LEN(data_size, username.size);
282
283
0
  username.data = &data[2];
284
285
  /* copy the username to the auth info structures
286
   */
287
0
  info = _gnutls_get_auth_info(session, GNUTLS_CRD_PSK);
288
0
  if (info == NULL) {
289
0
    gnutls_assert();
290
0
    return GNUTLS_E_INTERNAL_ERROR;
291
0
  }
292
293
0
  if (username.size > MAX_USERNAME_SIZE) {
294
0
    gnutls_assert();
295
0
    return GNUTLS_E_ILLEGAL_SRP_USERNAME;
296
0
  }
297
298
0
  ret = _gnutls_copy_psk_username(info, username);
299
0
  if (ret < 0)
300
0
    gnutls_assert_val(ret);
301
302
  /* Adjust data so it points to EncryptedPreMasterSecret */
303
0
  data += username.size + 2;
304
305
  /*** 2. Decrypt and extract EncryptedPreMasterSecret ***/
306
307
0
  DECR_LEN(data_size, 2);
308
0
  ciphertext.data = &data[2];
309
0
  dsize = _gnutls_read_uint16(data);
310
311
0
  if (dsize != data_size) {
312
0
    gnutls_assert();
313
0
    return GNUTLS_E_UNEXPECTED_PACKET_LENGTH;
314
0
  }
315
0
  ciphertext.size = dsize;
316
0
  gnutls_privkey_get_pk_algorithm(session->internals.selected_key,
317
0
          &rsa_key_bits);
318
0
  if (ciphertext.size != (rsa_key_bits + 7) / 8)
319
0
    return gnutls_assert_val(GNUTLS_E_DECRYPTION_FAILED);
320
321
0
  ver_maj = _gnutls_get_adv_version_major(session);
322
0
  ver_min = _gnutls_get_adv_version_minor(session);
323
324
  /* Find the key of this username. A random value will be
325
   * filled in if the key is not found.
326
   */
327
0
  ret = _gnutls_psk_pwd_find_entry(session, info->username,
328
0
           info->username_len, &pwd_psk, NULL);
329
0
  if (ret < 0)
330
0
    return gnutls_assert_val(ret);
331
332
  /* Allocate memory for premaster secret, and fill in the
333
   * fields except the decryption result.
334
   */
335
0
  session->key.key.size = 2 + GNUTLS_MASTER_SIZE + 2 + pwd_psk.size;
336
0
  session->key.key.data = gnutls_malloc(session->key.key.size);
337
0
  if (session->key.key.data == NULL) {
338
0
    gnutls_assert();
339
0
    _gnutls_free_key_datum(&pwd_psk);
340
    /* No need to zeroize, as the secret is not copied in yet */
341
0
    _gnutls_free_datum(&session->key.key);
342
0
    return GNUTLS_E_MEMORY_ERROR;
343
0
  }
344
345
  /* Fallback value when decryption fails. Needs to be unpredictable. */
346
0
  ret = gnutls_rnd(GNUTLS_RND_NONCE, session->key.key.data + 2,
347
0
       GNUTLS_MASTER_SIZE);
348
0
  if (ret < 0) {
349
0
    gnutls_assert();
350
0
    _gnutls_free_key_datum(&pwd_psk);
351
    /* No need to zeroize, as the secret is not copied in yet */
352
0
    _gnutls_free_datum(&session->key.key);
353
0
    return ret;
354
0
  }
355
356
0
  _gnutls_write_uint16(GNUTLS_MASTER_SIZE, session->key.key.data);
357
0
  _gnutls_write_uint16(pwd_psk.size,
358
0
           &session->key.key.data[2 + GNUTLS_MASTER_SIZE]);
359
0
  memcpy(&session->key.key.data[2 + GNUTLS_MASTER_SIZE + 2], pwd_psk.data,
360
0
         pwd_psk.size);
361
0
  _gnutls_free_key_datum(&pwd_psk);
362
363
0
  gnutls_privkey_decrypt_data2(session->internals.selected_key, 0,
364
0
             &ciphertext, session->key.key.data + 2,
365
0
             GNUTLS_MASTER_SIZE);
366
  /* After this point, any conditional on failure that cause differences
367
   * in execution may create a timing or cache access pattern side
368
   * channel that can be used as an oracle, so tread carefully */
369
370
  /* Error handling logic:
371
   * In case decryption fails then don't inform the peer. Just use the
372
   * random key previously generated. (in order to avoid attack against
373
   * pkcs-1 formatting).
374
   *
375
   * If we get version mismatches no error is returned either. We
376
   * proceed normally. This is to defend against the attack described
377
   * in the paper "Attacking RSA-based sessions in SSL/TLS" by
378
   * Vlastimil Klima, Ondej Pokorny and Tomas Rosa.
379
   */
380
381
  /* This is here to avoid the version check attack
382
   * discussed above.
383
   */
384
0
  session->key.key.data[2] = ver_maj;
385
0
  session->key.key.data[3] = ver_min;
386
387
0
  return 0;
388
0
}
389
390
static int _gnutls_proc_rsa_psk_server_kx(gnutls_session_t session,
391
            uint8_t *data, size_t data_size)
392
0
{
393
  /* In RSA-PSK the key is calculated elsewhere.
394
   * Moreover, since we only keep a single auth info structure, we cannot
395
   * store the hint (as we store certificate auth info).
396
   * Ideally we need to handle that by multiple auth info
397
   * structures or something similar.
398
   */
399
400
0
  return 0;
401
0
}
402
403
#endif /* ENABLE_PSK */