Blame lib/psk.c

Packit 549fdc
/*
Packit 549fdc
 * Copyright (C) 2005-2012 Free Software Foundation, Inc.
Packit 549fdc
 *
Packit 549fdc
 * Author: Nikos Mavrogiannopoulos
Packit 549fdc
 *
Packit 549fdc
 * This file is part of GnuTLS.
Packit 549fdc
 *
Packit 549fdc
 * The GnuTLS is free software; you can redistribute it and/or
Packit 549fdc
 * modify it under the terms of the GNU Lesser General Public License
Packit 549fdc
 * as published by the Free Software Foundation; either version 2.1 of
Packit 549fdc
 * the License, or (at your option) any later version.
Packit 549fdc
 *
Packit 549fdc
 * This library is distributed in the hope that it will be useful, but
Packit 549fdc
 * WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 549fdc
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 549fdc
 * Lesser General Public License for more details.
Packit 549fdc
 *
Packit 549fdc
 * You should have received a copy of the GNU Lesser General Public License
Packit 549fdc
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
Packit 549fdc
 *
Packit 549fdc
 */
Packit 549fdc
Packit 549fdc
/* Functions for manipulating the PSK credentials. */
Packit 549fdc
Packit 549fdc
#include "gnutls_int.h"
Packit 549fdc
#include "errors.h"
Packit 549fdc
#include <auth/psk.h>
Packit 549fdc
#include <state.h>
Packit 549fdc
Packit 549fdc
#ifdef ENABLE_PSK
Packit 549fdc
Packit 549fdc
#include <auth/psk_passwd.h>
Packit 549fdc
#include <num.h>
Packit 549fdc
#include <file.h>
Packit 549fdc
#include <datum.h>
Packit 549fdc
#include "debug.h"
Packit 549fdc
#include "dh.h"
Packit 549fdc
Packit 549fdc
/**
Packit 549fdc
 * gnutls_psk_free_client_credentials:
Packit 549fdc
 * @sc: is a #gnutls_psk_client_credentials_t type.
Packit 549fdc
 *
Packit 549fdc
 * Free a gnutls_psk_client_credentials_t structure.
Packit 549fdc
 **/
Packit 549fdc
void gnutls_psk_free_client_credentials(gnutls_psk_client_credentials_t sc)
Packit 549fdc
{
Packit 549fdc
	_gnutls_free_datum(&sc->username);
Packit 549fdc
	_gnutls_free_datum(&sc->key);
Packit 549fdc
	gnutls_free(sc);
Packit 549fdc
}
Packit 549fdc
Packit 549fdc
/**
Packit 549fdc
 * gnutls_psk_allocate_client_credentials:
Packit 549fdc
 * @sc: is a pointer to a #gnutls_psk_server_credentials_t type.
Packit 549fdc
 *
Packit 549fdc
 * Allocate a gnutls_psk_client_credentials_t structure.
Packit 549fdc
 *
Packit 549fdc
 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise
Packit 549fdc
 *   an error code is returned.
Packit 549fdc
 **/
Packit 549fdc
int
Packit 549fdc
gnutls_psk_allocate_client_credentials(gnutls_psk_client_credentials_t *
Packit 549fdc
				       sc)
Packit 549fdc
{
Packit 549fdc
	*sc = gnutls_calloc(1, sizeof(psk_client_credentials_st));
Packit 549fdc
Packit 549fdc
	if (*sc == NULL)
Packit 549fdc
		return GNUTLS_E_MEMORY_ERROR;
Packit 549fdc
Packit 549fdc
	return 0;
Packit 549fdc
}
Packit 549fdc
Packit 549fdc
/**
Packit 549fdc
 * gnutls_psk_set_client_credentials:
Packit 549fdc
 * @res: is a #gnutls_psk_client_credentials_t type.
Packit 549fdc
 * @username: is the user's zero-terminated userid
Packit 549fdc
 * @key: is the user's key
Packit 549fdc
 * @flags: indicate the format of the key, either
Packit 549fdc
 *   %GNUTLS_PSK_KEY_RAW or %GNUTLS_PSK_KEY_HEX.
Packit 549fdc
 *
Packit 549fdc
 * This function sets the username and password, in a
Packit 549fdc
 * gnutls_psk_client_credentials_t type.  Those will be used in
Packit 549fdc
 * PSK authentication.  @username should be an ASCII string or UTF-8
Packit 549fdc
 * strings prepared using the "SASLprep" profile of "stringprep".  The
Packit 549fdc
 * key can be either in raw byte format or in Hex format (without the
Packit 549fdc
 * 0x prefix).
Packit 549fdc
 *
Packit 549fdc
 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise
Packit 549fdc
 *   an error code is returned.
Packit 549fdc
 **/
Packit 549fdc
int
Packit 549fdc
gnutls_psk_set_client_credentials(gnutls_psk_client_credentials_t res,
Packit 549fdc
				  const char *username,
Packit 549fdc
				  const gnutls_datum_t * key,
Packit 549fdc
				  gnutls_psk_key_flags flags)
Packit 549fdc
{
Packit 549fdc
	int ret;
Packit 549fdc
Packit 549fdc
	if (username == NULL || key == NULL || key->data == NULL) {
Packit 549fdc
		gnutls_assert();
Packit 549fdc
		return GNUTLS_E_INVALID_REQUEST;
Packit 549fdc
	}
Packit 549fdc
Packit 549fdc
	ret =
Packit 549fdc
	    _gnutls_set_datum(&res->username, username, strlen(username));
Packit 549fdc
	if (ret < 0)
Packit 549fdc
		return ret;
Packit 549fdc
Packit 549fdc
	if (flags == GNUTLS_PSK_KEY_RAW) {
Packit 549fdc
		if (_gnutls_set_datum(&res->key, key->data, key->size) < 0) {
Packit 549fdc
			gnutls_assert();
Packit 549fdc
			ret = GNUTLS_E_MEMORY_ERROR;
Packit 549fdc
			goto error;
Packit 549fdc
		}
Packit 549fdc
	} else {		/* HEX key */
Packit 549fdc
		size_t size;
Packit 549fdc
		size = res->key.size = key->size / 2;
Packit 549fdc
		res->key.data = gnutls_malloc(size);
Packit 549fdc
		if (res->key.data == NULL) {
Packit 549fdc
			gnutls_assert();
Packit 549fdc
			ret = GNUTLS_E_MEMORY_ERROR;
Packit 549fdc
			goto error;
Packit 549fdc
		}
Packit 549fdc
Packit 549fdc
		ret =
Packit 549fdc
		    gnutls_hex_decode(key, (char *) res->key.data, &size);
Packit 549fdc
		res->key.size = (unsigned int) size;
Packit 549fdc
		if (ret < 0) {
Packit 549fdc
			
Packit 549fdc
			gnutls_assert();
Packit 549fdc
			goto error;
Packit 549fdc
		}
Packit 549fdc
Packit 549fdc
		if (size < 4) {
Packit 549fdc
			gnutls_assert();
Packit 549fdc
			ret = GNUTLS_E_INVALID_REQUEST;
Packit 549fdc
			goto error;
Packit 549fdc
		}
Packit 549fdc
	}
Packit 549fdc
Packit 549fdc
	return 0;
Packit 549fdc
Packit 549fdc
      error:
Packit 549fdc
	_gnutls_free_datum(&res->username);
Packit 549fdc
	_gnutls_free_datum(&res->key);
Packit 549fdc
Packit 549fdc
	return ret;
Packit 549fdc
}
Packit 549fdc
Packit 549fdc
/**
Packit 549fdc
 * gnutls_psk_free_server_credentials:
Packit 549fdc
 * @sc: is a #gnutls_psk_server_credentials_t type.
Packit 549fdc
 *
Packit 549fdc
 * Free a gnutls_psk_server_credentials_t structure.
Packit 549fdc
 **/
Packit 549fdc
void gnutls_psk_free_server_credentials(gnutls_psk_server_credentials_t sc)
Packit 549fdc
{
Packit 549fdc
	if (sc->deinit_dh_params) {
Packit 549fdc
		gnutls_dh_params_deinit(sc->dh_params);
Packit 549fdc
	}
Packit 549fdc
Packit 549fdc
	gnutls_free(sc->password_file);
Packit 549fdc
	gnutls_free(sc->hint);
Packit 549fdc
	gnutls_free(sc);
Packit 549fdc
}
Packit 549fdc
Packit 549fdc
/**
Packit 549fdc
 * gnutls_psk_allocate_server_credentials:
Packit 549fdc
 * @sc: is a pointer to a #gnutls_psk_server_credentials_t type.
Packit 549fdc
 *
Packit 549fdc
 * Allocate a gnutls_psk_server_credentials_t structure.
Packit 549fdc
 *
Packit 549fdc
 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise
Packit 549fdc
 *   an error code is returned.
Packit 549fdc
 **/
Packit 549fdc
int
Packit 549fdc
gnutls_psk_allocate_server_credentials(gnutls_psk_server_credentials_t *
Packit 549fdc
				       sc)
Packit 549fdc
{
Packit 549fdc
	*sc = gnutls_calloc(1, sizeof(psk_server_cred_st));
Packit 549fdc
Packit 549fdc
	if (*sc == NULL)
Packit 549fdc
		return GNUTLS_E_MEMORY_ERROR;
Packit 549fdc
Packit 549fdc
	return 0;
Packit 549fdc
}
Packit 549fdc
Packit 549fdc
Packit 549fdc
/**
Packit 549fdc
 * gnutls_psk_set_server_credentials_file:
Packit 549fdc
 * @res: is a #gnutls_psk_server_credentials_t type.
Packit 549fdc
 * @password_file: is the PSK password file (passwd.psk)
Packit 549fdc
 *
Packit 549fdc
 * This function sets the password file, in a
Packit 549fdc
 * #gnutls_psk_server_credentials_t type.  This password file
Packit 549fdc
 * holds usernames and keys and will be used for PSK authentication.
Packit 549fdc
 *
Packit 549fdc
 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise
Packit 549fdc
 *   an error code is returned.
Packit 549fdc
 **/
Packit 549fdc
int
Packit 549fdc
gnutls_psk_set_server_credentials_file(gnutls_psk_server_credentials_t
Packit 549fdc
				       res, const char *password_file)
Packit 549fdc
{
Packit 549fdc
Packit 549fdc
	if (password_file == NULL) {
Packit 549fdc
		gnutls_assert();
Packit 549fdc
		return GNUTLS_E_INVALID_REQUEST;
Packit 549fdc
	}
Packit 549fdc
Packit 549fdc
	/* Check if the files can be opened */
Packit 549fdc
	if (_gnutls_file_exists(password_file) != 0) {
Packit 549fdc
		gnutls_assert();
Packit 549fdc
		return GNUTLS_E_FILE_ERROR;
Packit 549fdc
	}
Packit 549fdc
Packit 549fdc
	res->password_file = gnutls_strdup(password_file);
Packit 549fdc
	if (res->password_file == NULL) {
Packit 549fdc
		gnutls_assert();
Packit 549fdc
		return GNUTLS_E_MEMORY_ERROR;
Packit 549fdc
	}
Packit 549fdc
Packit 549fdc
	return 0;
Packit 549fdc
}
Packit 549fdc
Packit 549fdc
/**
Packit 549fdc
 * gnutls_psk_set_server_credentials_hint:
Packit 549fdc
 * @res: is a #gnutls_psk_server_credentials_t type.
Packit 549fdc
 * @hint: is the PSK identity hint string
Packit 549fdc
 *
Packit 549fdc
 * This function sets the identity hint, in a
Packit 549fdc
 * #gnutls_psk_server_credentials_t type.  This hint is sent to
Packit 549fdc
 * the client to help it chose a good PSK credential (i.e., username
Packit 549fdc
 * and password).
Packit 549fdc
 *
Packit 549fdc
 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise
Packit 549fdc
 *   an error code is returned.
Packit 549fdc
 *
Packit 549fdc
 * Since: 2.4.0
Packit 549fdc
 **/
Packit 549fdc
int
Packit 549fdc
gnutls_psk_set_server_credentials_hint(gnutls_psk_server_credentials_t res,
Packit 549fdc
				       const char *hint)
Packit 549fdc
{
Packit 549fdc
	res->hint = gnutls_strdup(hint);
Packit 549fdc
	if (res->hint == NULL) {
Packit 549fdc
		gnutls_assert();
Packit 549fdc
		return GNUTLS_E_MEMORY_ERROR;
Packit 549fdc
	}
Packit 549fdc
Packit 549fdc
	return 0;
Packit 549fdc
}
Packit 549fdc
Packit 549fdc
/**
Packit 549fdc
 * gnutls_psk_set_server_credentials_function:
Packit 549fdc
 * @cred: is a #gnutls_psk_server_credentials_t type.
Packit 549fdc
 * @func: is the callback function
Packit 549fdc
 *
Packit 549fdc
 * This function can be used to set a callback to retrieve the user's PSK credentials.
Packit 549fdc
 * The callback's function form is:
Packit 549fdc
 * int (*callback)(gnutls_session_t, const char* username,
Packit 549fdc
 *  gnutls_datum_t* key);
Packit 549fdc
 *
Packit 549fdc
 * @username contains the actual username.
Packit 549fdc
 * The @key must be filled in using the gnutls_malloc().
Packit 549fdc
 *
Packit 549fdc
 * In case the callback returned a negative number then gnutls will
Packit 549fdc
 * assume that the username does not exist.
Packit 549fdc
 *
Packit 549fdc
 * The callback function will only be called once per handshake.  The
Packit 549fdc
 * callback function should return 0 on success, while -1 indicates
Packit 549fdc
 * an error.
Packit 549fdc
 **/
Packit 549fdc
void
Packit 549fdc
gnutls_psk_set_server_credentials_function(gnutls_psk_server_credentials_t
Packit 549fdc
					   cred,
Packit 549fdc
					   gnutls_psk_server_credentials_function
Packit 549fdc
					   * func)
Packit 549fdc
{
Packit 549fdc
	cred->pwd_callback = func;
Packit 549fdc
}
Packit 549fdc
Packit 549fdc
/**
Packit 549fdc
 * gnutls_psk_set_client_credentials_function:
Packit 549fdc
 * @cred: is a #gnutls_psk_server_credentials_t type.
Packit 549fdc
 * @func: is the callback function
Packit 549fdc
 *
Packit 549fdc
 * This function can be used to set a callback to retrieve the username and
Packit 549fdc
 * password for client PSK authentication.
Packit 549fdc
 * The callback's function form is:
Packit 549fdc
 * int (*callback)(gnutls_session_t, char** username,
Packit 549fdc
 *  gnutls_datum_t* key);
Packit 549fdc
 *
Packit 549fdc
 * The @username and @key->data must be allocated using gnutls_malloc().
Packit 549fdc
 * @username should be ASCII strings or UTF-8 strings prepared using
Packit 549fdc
 * the "SASLprep" profile of "stringprep".
Packit 549fdc
 *
Packit 549fdc
 * The callback function will be called once per handshake.
Packit 549fdc
 *
Packit 549fdc
 * The callback function should return 0 on success.
Packit 549fdc
 * -1 indicates an error.
Packit 549fdc
 **/
Packit 549fdc
void
Packit 549fdc
gnutls_psk_set_client_credentials_function(gnutls_psk_client_credentials_t
Packit 549fdc
					   cred,
Packit 549fdc
					   gnutls_psk_client_credentials_function
Packit 549fdc
					   * func)
Packit 549fdc
{
Packit 549fdc
	cred->get_function = func;
Packit 549fdc
}
Packit 549fdc
Packit 549fdc
Packit 549fdc
/**
Packit 549fdc
 * gnutls_psk_server_get_username:
Packit 549fdc
 * @session: is a gnutls session
Packit 549fdc
 *
Packit 549fdc
 * This should only be called in case of PSK authentication and in
Packit 549fdc
 * case of a server.
Packit 549fdc
 *
Packit 549fdc
 * Returns: the username of the peer, or %NULL in case of an error.
Packit 549fdc
 **/
Packit 549fdc
const char *gnutls_psk_server_get_username(gnutls_session_t session)
Packit 549fdc
{
Packit 549fdc
	psk_auth_info_t info;
Packit 549fdc
Packit 549fdc
	CHECK_AUTH(GNUTLS_CRD_PSK, NULL);
Packit 549fdc
Packit 549fdc
	info = _gnutls_get_auth_info(session, GNUTLS_CRD_PSK);
Packit 549fdc
	if (info == NULL)
Packit 549fdc
		return NULL;
Packit 549fdc
Packit 549fdc
	if (info->username[0] != 0)
Packit 549fdc
		return info->username;
Packit 549fdc
Packit 549fdc
	return NULL;
Packit 549fdc
}
Packit 549fdc
Packit 549fdc
/**
Packit 549fdc
 * gnutls_psk_client_get_hint:
Packit 549fdc
 * @session: is a gnutls session
Packit 549fdc
 *
Packit 549fdc
 * The PSK identity hint may give the client help in deciding which
Packit 549fdc
 * username to use.  This should only be called in case of PSK
Packit 549fdc
 * authentication and in case of a client.
Packit 549fdc
 *
Packit 549fdc
 * Returns: the identity hint of the peer, or %NULL in case of an error.
Packit 549fdc
 *
Packit 549fdc
 * Since: 2.4.0
Packit 549fdc
 **/
Packit 549fdc
const char *gnutls_psk_client_get_hint(gnutls_session_t session)
Packit 549fdc
{
Packit 549fdc
	psk_auth_info_t info;
Packit 549fdc
Packit 549fdc
	CHECK_AUTH(GNUTLS_CRD_PSK, NULL);
Packit 549fdc
Packit 549fdc
	info = _gnutls_get_auth_info(session, GNUTLS_CRD_PSK);
Packit 549fdc
	if (info == NULL)
Packit 549fdc
		return NULL;
Packit 549fdc
Packit 549fdc
	if (info->hint[0] != 0)
Packit 549fdc
		return info->hint;
Packit 549fdc
Packit 549fdc
	return NULL;
Packit 549fdc
}
Packit 549fdc
Packit 549fdc
/**
Packit 549fdc
 * gnutls_psk_set_server_dh_params:
Packit 549fdc
 * @res: is a gnutls_psk_server_credentials_t type
Packit 549fdc
 * @dh_params: is a structure that holds Diffie-Hellman parameters.
Packit 549fdc
 *
Packit 549fdc
 * This function will set the Diffie-Hellman parameters for an
Packit 549fdc
 * anonymous server to use. These parameters will be used in
Packit 549fdc
 * Diffie-Hellman exchange with PSK cipher suites.
Packit 549fdc
 *
Packit 549fdc
 * Deprecated: This function is unnecessary and discouraged on GnuTLS 3.6.0
Packit 549fdc
 * or later. Since 3.6.0, DH parameters are negotiated
Packit 549fdc
 * following RFC7919.
Packit 549fdc
 *
Packit 549fdc
 **/
Packit 549fdc
void
Packit 549fdc
gnutls_psk_set_server_dh_params(gnutls_psk_server_credentials_t res,
Packit 549fdc
				gnutls_dh_params_t dh_params)
Packit 549fdc
{
Packit 549fdc
	if (res->deinit_dh_params) {
Packit 549fdc
		res->deinit_dh_params = 0;
Packit 549fdc
		gnutls_dh_params_deinit(res->dh_params);
Packit 549fdc
		res->dh_params = NULL;
Packit 549fdc
	}
Packit 549fdc
Packit 549fdc
	res->dh_params = dh_params;
Packit 549fdc
	res->dh_sec_param = gnutls_pk_bits_to_sec_param(GNUTLS_PK_DH, _gnutls_mpi_get_nbits(dh_params->params[0]));
Packit 549fdc
}
Packit 549fdc
Packit 549fdc
/**
Packit 549fdc
 * gnutls_psk_set_server_known_dh_params:
Packit 549fdc
 * @res: is a gnutls_psk_server_credentials_t type
Packit 549fdc
 * @sec_param: is an option of the %gnutls_sec_param_t enumeration
Packit 549fdc
 *
Packit 549fdc
 * This function will set the Diffie-Hellman parameters for a
Packit 549fdc
 * PSK server to use. These parameters will be used in
Packit 549fdc
 * Ephemeral Diffie-Hellman cipher suites and will be selected from
Packit 549fdc
 * the FFDHE set of RFC7919 according to the security level provided.
Packit 549fdc
 *
Packit 549fdc
 * Deprecated: This function is unnecessary and discouraged on GnuTLS 3.6.0
Packit 549fdc
 * or later. Since 3.6.0, DH parameters are negotiated
Packit 549fdc
 * following RFC7919.
Packit 549fdc
 *
Packit 549fdc
 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
Packit 549fdc
 *   negative error value.
Packit 549fdc
 *
Packit 549fdc
 * Since: 3.5.6
Packit 549fdc
 **/
Packit 549fdc
int
Packit 549fdc
gnutls_psk_set_server_known_dh_params(gnutls_psk_server_credentials_t res,
Packit 549fdc
				       gnutls_sec_param_t sec_param)
Packit 549fdc
{
Packit 549fdc
	res->dh_sec_param = sec_param;
Packit 549fdc
Packit 549fdc
	return 0;
Packit 549fdc
}
Packit 549fdc
Packit 549fdc
/**
Packit 549fdc
 * gnutls_psk_set_server_params_function:
Packit 549fdc
 * @res: is a #gnutls_certificate_credentials_t type
Packit 549fdc
 * @func: is the function to be called
Packit 549fdc
 *
Packit 549fdc
 * This function will set a callback in order for the server to get
Packit 549fdc
 * the Diffie-Hellman parameters for PSK authentication.  The callback
Packit 549fdc
 * should return %GNUTLS_E_SUCCESS (0) on success.
Packit 549fdc
 *
Packit 549fdc
 * Deprecated: This function is unnecessary and discouraged on GnuTLS 3.6.0
Packit 549fdc
 * or later. Since 3.6.0, DH parameters are negotiated
Packit 549fdc
 * following RFC7919.
Packit 549fdc
 *
Packit 549fdc
 **/
Packit 549fdc
void
Packit 549fdc
gnutls_psk_set_server_params_function(gnutls_psk_server_credentials_t res,
Packit 549fdc
				      gnutls_params_function * func)
Packit 549fdc
{
Packit 549fdc
	res->params_func = func;
Packit 549fdc
}
Packit 549fdc
Packit 549fdc
/**
Packit 549fdc
 * gnutls_psk_set_params_function:
Packit 549fdc
 * @res: is a gnutls_psk_server_credentials_t type
Packit 549fdc
 * @func: is the function to be called
Packit 549fdc
 *
Packit 549fdc
 * This function will set a callback in order for the server to get
Packit 549fdc
 * the Diffie-Hellman or RSA parameters for PSK authentication.  The
Packit 549fdc
 * callback should return %GNUTLS_E_SUCCESS (0) on success.
Packit 549fdc
 *
Packit 549fdc
 * Deprecated: This function is unnecessary and discouraged on GnuTLS 3.6.0
Packit 549fdc
 * or later. Since 3.6.0, DH parameters are negotiated
Packit 549fdc
 * following RFC7919.
Packit 549fdc
 *
Packit 549fdc
 **/
Packit 549fdc
void
Packit 549fdc
gnutls_psk_set_params_function(gnutls_psk_server_credentials_t res,
Packit 549fdc
			       gnutls_params_function * func)
Packit 549fdc
{
Packit 549fdc
	res->params_func = func;
Packit 549fdc
}
Packit 549fdc
Packit 549fdc
#endif				/* ENABLE_PSK */