summaryrefslogtreecommitdiff
path: root/lib/auth/psk.h
blob: 06d7913c858a3f30609ae375bcf1b007467ee790 (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
/*
 * Copyright (C) 2005-2012 Free Software Foundation, Inc.
 *
 * Author: Nikos Mavrogiannopoulos
 *
 * This file is part of GnuTLS.
 *
 * The 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 <https://www.gnu.org/licenses/>
 *
 */

#ifndef GNUTLS_LIB_AUTH_PSK_H
#define GNUTLS_LIB_AUTH_PSK_H

#include <auth.h>
#include <auth/dh_common.h>

#define _gnutls_copy_psk_username(info, datum)                            \
	_gnutls_copy_psk_string(&(info)->username, &(info)->username_len, \
				(datum))

#define _gnutls_copy_psk_hint(info, datum) \
	_gnutls_copy_psk_string(&(info)->hint, &(info)->hint_len, (datum))

typedef struct gnutls_psk_client_credentials_st {
	gnutls_datum_t username;
	gnutls_datum_t key;
	gnutls_psk_client_credentials_function2 *get_function;
	gnutls_psk_client_credentials_function *get_function_legacy;
	/* TLS 1.3 - The HMAC algorithm to use to compute the binder values */
	const mac_entry_st *binder_algo;
} psk_client_credentials_st;

typedef struct gnutls_psk_server_credentials_st {
	char *password_file;
	/* callback function, instead of reading the
	 * password files.
	 */
	gnutls_psk_server_credentials_function2 *pwd_callback;
	gnutls_psk_server_credentials_function *pwd_callback_legacy;

	/* For DHE_PSK */
	gnutls_dh_params_t dh_params;
	unsigned int deinit_dh_params;
	gnutls_sec_param_t dh_sec_param;
	/* this callback is used to retrieve the DH or RSA
	 * parameters.
	 */
	gnutls_params_function *params_func;

	/* Identity hint. */
	char *hint;
	/* TLS 1.3 - HMAC algorithm for the binder values */
	const mac_entry_st *binder_algo;
} psk_server_cred_st;

typedef struct psk_auth_info_st {
	char *username;
	uint16_t username_len;
	dh_info_st dh;
	char *hint;
	uint16_t hint_len;
} *psk_auth_info_t;

typedef struct psk_auth_info_st psk_auth_info_st;

inline static int _gnutls_copy_psk_string(char **dest, uint16_t *dest_len,
					  const gnutls_datum_t str)
{
	char *_tmp;

	assert(MAX_USERNAME_SIZE >= str.size);

	_tmp = gnutls_malloc(str.size + 1);
	if (_tmp == NULL)
		return GNUTLS_E_MEMORY_ERROR;
	memcpy(_tmp, str.data, str.size);
	_tmp[str.size] = '\0';

	gnutls_free(*dest);
	*dest = _tmp;
	*dest_len = str.size;

	return GNUTLS_E_SUCCESS;
}

#ifdef ENABLE_PSK

int _gnutls_set_psk_session_key(gnutls_session_t session, gnutls_datum_t *key,
				gnutls_datum_t *psk2);
int _gnutls_gen_psk_server_kx(gnutls_session_t session, gnutls_buffer_st *data);
int _gnutls_gen_psk_client_kx(gnutls_session_t, gnutls_buffer_st *);

#else
#define _gnutls_set_psk_session_key(x, y, z) GNUTLS_E_UNIMPLEMENTED_FEATURE
#endif /* ENABLE_PSK */

#endif /* GNUTLS_LIB_AUTH_PSK_H */