/*
 * gnome-keyring
 *
 * Copyright (C) 2018 Red Hat, Inc.
 *
 * This program 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 program 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
 * <http://www.gnu.org/licenses/>.
 *
 * Author: Daiki Ueno
 */

#include "config.h"

#include "test-common.h"

#include "gkd-ssh-agent-private.h"
#include "gkd-ssh-agent-util.h"
#include "pkcs11/gkm/gkm-mock.h"

/* RSA private key blob decoded from pkcs11/ssh-store/fixtures/id_rsa_plain */
static const guint8 private_blob[4*6 + 0x101 + 0x1 + 0x101 + 0x80 + 0x81 + 0x81] = {
	/* n */
	0x00, 0x00, 0x01, 0x01, 0x00, 0xa0, 0x3e, 0x95, 0x2a, 0xa9, 0x21, 0x6b,
	0x2e, 0xa9, 0x28, 0x74,	0x91, 0x8c, 0x01, 0x96, 0x59, 0xf1, 0x4f, 0x53,
	0xcc, 0x5f, 0xb2, 0x2d, 0xa0, 0x9c, 0xec, 0x0f,	0xfc, 0x1d, 0x54, 0x1c,
	0x3a, 0x33, 0xb7, 0x1d, 0xdc, 0xce, 0x13, 0xbe, 0xa7, 0x2f, 0xdf, 0x4e,
	0x58, 0x42, 0x9d, 0x23, 0xf5, 0x8e, 0xc8, 0xe4, 0xad, 0x52, 0x19, 0x72,
	0x7c, 0xda, 0x87, 0x67,	0xd4, 0x34, 0x51, 0x51, 0x81, 0x2e, 0x3e, 0x8d,
	0x13, 0x81, 0xb6, 0xf6, 0xe0, 0x1e, 0xc4, 0xbb,	0xd9, 0x5d, 0x44, 0xeb,
	0xe6, 0x68, 0x81, 0x5f, 0xa6, 0x04, 0x95, 0x96, 0x02, 0x1c, 0x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,
	0x29, 0x1e, 0x4a, 0x93, 0x73, 0xd1, 0x7b, 0x47,	0x1d, 0x36, 0x87, 0x89,
	0x1d, 0xbf, 0xd5, 0x1e, 0x02, 0xc2, 0xd1, 0x2b, 0xb3, 0x67, 0x07, 0x65,
	0xf9, 0xbc, 0xcb, 0x74, 0x4c, 0x83, 0x68, 0xa8, 0x6d, 0x30, 0x68, 0x8f,
	0xb5, 0xb9, 0x44, 0x86,	0xb8, 0xde, 0x4e, 0xfc, 0x02, 0x1e, 0x9c, 0x05,
	0x3b, 0x23, 0x1b, 0xdf, 0x79, 0x58, 0x73, 0x51,	0x27, 0xf0, 0xbd, 0x83,
	0x34, 0x38, 0xcb, 0xd0, 0x20, 0x12, 0xcd, 0x1a, 0x07, 0x6e, 0xf7, 0x0a,
	0x92, 0x29, 0xff, 0x2f, 0xbf, 0x30, 0x2a, 0x69, 0x15, 0x4d, 0x8e, 0x6e,
	0x17, 0x26, 0x7b, 0x43,	0xfe, 0x52, 0xd1, 0x83, 0x65, 0x19, 0x22, 0x8b,
	0xd3, 0x6f, 0x97, 0x51, 0x11, 0x3f, 0x17, 0xfe,	0x05, 0xcc, 0xa4, 0x49,
	0x8b
};

/* define gkd_pkcs11_get_base_functions() for gkd-login.c */
CK_FUNCTION_LIST_PTR
gkd_pkcs11_get_base_functions (void);

CK_FUNCTION_LIST_PTR
gkd_pkcs11_get_base_functions (void)
{
	CK_FUNCTION_LIST_PTR funcs;
	gkm_mock_C_GetFunctionList (&funcs);
	return funcs;
}

void
prepare_request_identities (EggBuffer *req)
{
	gboolean ret;

	egg_buffer_reset (req);

	ret = egg_buffer_add_uint32 (req, 1);
	g_assert_true (ret);

	ret = egg_buffer_add_byte (req, GKD_SSH_OP_REQUEST_IDENTITIES);
	g_assert_true (ret);
}

void
check_identities_answer (EggBuffer *resp, gsize count)
{
	uint32_t length;
	unsigned char code;
	size_t offset;
	gboolean ret;

	offset = 0;
	ret = egg_buffer_get_uint32 (resp, offset, &offset, &length);
	g_assert_true (ret);
	g_assert_cmpint (length, ==, resp->len - 4);

	code = 0;
	ret = egg_buffer_get_byte (resp, offset, &offset, &code);
	g_assert_true (ret);
	g_assert_cmpint (code, ==, GKD_SSH_RES_IDENTITIES_ANSWER);

	ret = egg_buffer_get_uint32 (resp, offset, &offset, &length);
	g_assert_true (ret);
	g_assert_cmpint (length, ==, count);
}

void
prepare_add_identity (EggBuffer *req)
{
	gboolean ret;

	egg_buffer_reset (req);

	ret = egg_buffer_add_uint32 (req, 0);
	g_assert_true (ret);

	ret = egg_buffer_add_byte (req, GKD_SSH_OP_ADD_IDENTITY);
	g_assert_true (ret);

	ret = egg_buffer_add_string (req, "ssh-rsa");
	g_assert_true (ret);

	ret = egg_buffer_append (req, private_blob, G_N_ELEMENTS(private_blob));
	g_assert_true (ret);

	ret = egg_buffer_add_string (req, "comment");
	g_assert_true (ret);

	ret = egg_buffer_set_uint32 (req, 0, req->len - 4);
	g_assert_true (ret);
}

GBytes *
public_key_from_file (const gchar *path, gchar **comment)
{
	GBytes *public_bytes;
	GBytes *public_key;

	GError *error = NULL;
	gchar *contents;
	gsize length;

	if (!g_file_get_contents (path, &contents, &length, &error)) {
		g_message ("couldn't read file: %s: %s", path, error->message);
		g_error_free (error);
		return NULL;
	}

	public_bytes = g_bytes_new_take (contents, length);
	public_key = _gkd_ssh_agent_parse_public_key (public_bytes, comment);
	g_bytes_unref (public_bytes);

	return public_key;
}

void
prepare_remove_identity (EggBuffer *req)
{
	GBytes *public_key;
	gchar *comment;
	gsize length;
	const guchar *blob;
	gboolean ret;

	public_key = public_key_from_file (SRCDIR "/pkcs11/ssh-store/fixtures/id_rsa_plain.pub", &comment);
	g_free (comment);
	blob = g_bytes_get_data (public_key, &length);

	egg_buffer_reset (req);
	ret = egg_buffer_add_uint32 (req, 0);
	g_assert_true (ret);

	ret = egg_buffer_add_byte (req, GKD_SSH_OP_REMOVE_IDENTITY);
	g_assert_true (ret);

	ret = egg_buffer_add_byte_array (req, blob, length);
	g_assert_true (ret);

	ret = egg_buffer_set_uint32 (req, 0, req->len - 4);
	g_assert_true (ret);

	g_bytes_unref (public_key);
}

void
prepare_remove_all_identities (EggBuffer *req)
{
	gboolean ret;

	egg_buffer_reset (req);
	ret = egg_buffer_add_uint32 (req, 1);
	g_assert_true (ret);

	ret = egg_buffer_add_byte (req, GKD_SSH_OP_REMOVE_ALL_IDENTITIES);
	g_assert_true (ret);
}

void
check_response (EggBuffer *resp, unsigned char expected)
{
	uint32_t length;
	unsigned char code;
	size_t offset;
	gboolean ret;

	offset = 0;
	ret = egg_buffer_get_uint32 (resp, offset, &offset, &length);
	g_assert_true (ret);
	g_assert_cmpint (length, ==, resp->len - 4);

	code = 0;
	ret = egg_buffer_get_byte (resp, offset, &offset, &code);
	g_assert_true (ret);
	g_assert_cmpint (expected, ==, code);
}

void
check_success (EggBuffer *resp)
{
	check_response (resp, GKD_SSH_RES_SUCCESS);
}

void
check_failure (EggBuffer *resp)
{
	check_response (resp, GKD_SSH_RES_FAILURE);
}

void
prepare_sign_request (EggBuffer *req)
{
	GBytes *public_key;
	gchar *comment;
	gsize length;
	const guchar *blob;
	gboolean ret;

	public_key = public_key_from_file (SRCDIR "/pkcs11/ssh-store/fixtures/id_rsa_plain.pub", &comment);
	g_free (comment);
	blob = g_bytes_get_data (public_key, &length);

	egg_buffer_reset (req);
	ret = egg_buffer_add_uint32 (req, 0);
	g_assert_true (ret);

	ret = egg_buffer_add_byte (req, GKD_SSH_OP_SIGN_REQUEST);
	g_assert_true (ret);

	ret = egg_buffer_add_byte_array (req, blob, length);
	g_assert_true (ret);

	ret = egg_buffer_add_string (req, "data");
	g_assert_true (ret);

	ret = egg_buffer_add_uint32 (req, 0);
	g_assert_true (ret);

	ret = egg_buffer_set_uint32 (req, 0, req->len - 4);
	g_assert_true (ret);

	g_bytes_unref (public_key);
}

void
check_sign_response (EggBuffer *resp)
{
	uint32_t length;
	unsigned char code;
	size_t offset;
	gboolean ret;

	offset = 0;
	ret = egg_buffer_get_uint32 (resp, offset, &offset, &length);
	g_assert_true (ret);
	g_assert_cmpint (length, ==, resp->len - 4);

	code = 0;
	ret = egg_buffer_get_byte (resp, offset, &offset, &code);
	g_assert_true (ret);
	g_assert_cmpint (code, ==, GKD_SSH_RES_SIGN_RESPONSE);

	ret = egg_buffer_get_uint32 (resp, offset, &offset, &length);
	g_assert_true (ret);
}
