Blob Blame History Raw
/*********************************************************/
/*  toksvc: a framework-native token broker service      */
/*  Copyright (C) 2020  Z. Gilboa                        */
/*  Released under GPLv2 and GPLv3; see COPYING.TOKSVC.  */
/*********************************************************/

#include <psxtypes/psxtypes.h>
#include <ntapi/ntapi.h>

#include <toksvc/toksvc.h>
#include "toksvc_driver_impl.h"

int32_t toks_client_token_to_str(
	struct toks_driver_ctx *        dctx,
	struct toks_token_string *      toks)
{
	struct _nt_port_keys *	keys;
	unsigned char *		uch;

	keys = toks_get_driver_keys(dctx);
	uch  = (unsigned char *)toks->token;

	ntapi->tt_generic_memset(
		toks->token,0,
		sizeof(toks->token));

	uch[8]  = '-';
	uch[17] = '-';
	uch[26] = '-';
	uch[35] = '-';
	uch[44] = '-';

	ntapi->tt_uint32_to_hex_utf8(keys->key[0],&uch[0]);
	ntapi->tt_uint32_to_hex_utf8(keys->key[1],&uch[9]);
	ntapi->tt_uint32_to_hex_utf8(keys->key[2],&uch[18]);
	ntapi->tt_uint32_to_hex_utf8(keys->key[3],&uch[27]);
	ntapi->tt_uint32_to_hex_utf8(keys->key[4],&uch[36]);
	ntapi->tt_uint32_to_hex_utf8(keys->key[5],&uch[45]);

	return NT_STATUS_SUCCESS;
}

int32_t toks_client_str_to_token(
	struct toks_driver_ctx *            dctx,
	const struct toks_token_string *    toks)
{
	struct _nt_port_keys *	keys;
	unsigned char *		uch;
	uint32_t		key[6];

	keys = toks_get_driver_keys(dctx);
	uch  = (unsigned char *)toks->token;

	if ((uch[8] != '-') || (uch[17] != '-') || (uch[26] != '-')
		|| (uch[35] != '-') || (uch[44] != '-'))
		return NT_STATUS_INVALID_PARAMETER;

	if (ntapi->tt_hex_utf8_to_uint32(&uch[0],&key[0]))
		return NT_STATUS_INVALID_PARAMETER;

	if (ntapi->tt_hex_utf8_to_uint32(&uch[9],&key[1]))
		return NT_STATUS_INVALID_PARAMETER;

	if (ntapi->tt_hex_utf8_to_uint32(&uch[18],&key[2]))
		return NT_STATUS_INVALID_PARAMETER;

	if (ntapi->tt_hex_utf8_to_uint32(&uch[27],&key[3]))
		return NT_STATUS_INVALID_PARAMETER;

	if (ntapi->tt_hex_utf8_to_uint32(&uch[36],&key[4]))
		return NT_STATUS_INVALID_PARAMETER;

	if (ntapi->tt_hex_utf8_to_uint32(&uch[45],&key[5]))
		return NT_STATUS_INVALID_PARAMETER;

	if (uch[53])
		return NT_STATUS_INVALID_PARAMETER;

	keys->key[0] = key[0];
	keys->key[1] = key[1];
	keys->key[2] = key[2];
	keys->key[3] = key[3];
	keys->key[4] = key[4];
	keys->key[5] = key[5];

	return NT_STATUS_SUCCESS;
}