/*-------------------------------------------------------------

libcios.c -- Custom IOS library

Copyright (C) 2008 (waninkoko)

This software is provided 'as-is', without any express or implied
warranty.  In no event will the authors be held liable for any
damages arising from the use of this software.

Permission is granted to anyone to use this software for any
purpose, including commercial applications, and to alter it and
redistribute it freely, subject to the following restrictions:

1.	The origin of this software must not be misrepresented; you
must not claim that you wrote the original software. If you use
this software in a product, an acknowledgment in the product
documentation would be appreciated but is not required.

2.	Altered source versions must be plainly marked as such, and
must not be misrepresented as being the original software.

3.	This notice may not be removed or altered from any source
distribution.

-------------------------------------------------------------*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#include <ogcsys.h>

#include "libcios.h"

/* Ticket structure */
typedef struct {
	sig_rsa2048 signature;
	tik tik_data;
} ATTRIBUTE_PACKED signed_tik;

/* Constants */
#define CIOS_TITLEID	(0x100000000ULL | CIOS_VERSION)

#define CERTS_SIZE	0xA00

/* Variables */
static const char certs_fs[] ATTRIBUTE_ALIGN(32) = "/sys/cert.sys";


/*
 * __CIOS_GetCerts() - Reads the system certificates
 */

s32 __CIOS_GetCerts(signed_blob **out, u32 *len)
{
	signed_blob *p_certs = NULL;
	s32 fd, ret;

	/* Open certificates file */
	fd = IOS_Open(certs_fs, ISFS_OPEN_READ);
	if (fd < 0)
		return fd;

	/* Allocate memory */
	p_certs = (signed_blob *)memalign(32, CERTS_SIZE);
	if (!p_certs) {
		ret = IPC_ENOMEM;
		goto err;
	}

	/* Read certificates */
	ret = IOS_Read(fd, p_certs, CERTS_SIZE);
	if (ret < 0)
		goto err;

	/* Set values */
	*out = p_certs;
	*len = CERTS_SIZE;

	ret = 0;
	goto out;

err:
	/* Free memory */
	if (p_certs)
		free(p_certs);

out:
	/* Close file */
	if (fd >= 0)
		IOS_Close(fd);

	return ret;
}


/*
 * __CIOS_GetTMD() - Reads the stored TMD of the specified title
 */

s32 __CIOS_GetTMD(u64 tid, signed_blob **out, u32 *len)
{
	signed_blob *p_tmd = NULL;

	u32 tmd_len;
	s32 ret;

	/* Retrieve TMD length */
	ret = ES_GetStoredTMDSize(tid, &tmd_len);
	if (ret < 0)
		return ret;

	/* Allocate memory */
	p_tmd = (signed_blob *)memalign(32, tmd_len);
	if (!p_tmd)
		return IPC_ENOMEM;

	/* Retrieve TMD */
	ret = ES_GetStoredTMD(tid, p_tmd, tmd_len);
	if (ret < 0) {
		free(p_tmd);
		return ret;
	}

	/* Set values */
	*out = p_tmd;
	*len = tmd_len;

	return 0;
}


/*
 * __CIOS_GenerateTik() - Generates a fake ticket
 */

s32 __CIOS_GenerateTik(signed_blob **out, u32 *len)
{
	signed_tik *p_tik = NULL;

	/* Allocate memory */
	p_tik = (signed_tik *)memalign(32, sizeof(signed_tik));
	if (!p_tik)
		return IPC_ENOMEM;

	/* Clean ticket */
	memset(p_tik, 0, sizeof(signed_tik));

	/* Generate signature */
	p_tik->signature.type = ES_SIG_RSA2048;

	/* Generate ticket */
	memset(p_tik->tik_data.cidx_mask, 0xFF, 32);
	strcpy(p_tik->tik_data.issuer, "Root-CA00000001-XS00000003");

	/* Set values */
	*out = (signed_blob *)p_tik;
	*len = sizeof(signed_tik);

	return 0;
}


/*
 * CIOS_Load() - Load Custom IOS
 */

s32 CIOS_Load(void)
{
	/* Load Custom IOS */
	return IOS_ReloadIOS(CIOS_VERSION);
}


/*
 * CIOS_IsLoaded() - Check if Custom IOS is loaded
 */

s32 CIOS_IsLoaded(void)
{
	/* Check if Custom IOS is loaded */
	return (IOS_GetVersion() == CIOS_VERSION);
}


/*
 * CIOS_Identify()- Identify as Custom IOS
 */

s32 CIOS_Identify(void)
{
	/* Identify as Custom IOS */
	return CIOS_IdentifyAsTitle(CIOS_TITLEID);
}


/*
 * CIOS_IdentifyAsTitle() - Identify as specified title
 */

s32 CIOS_IdentifyAsTitle(u64 tid)
{
	signed_blob *p_certs = NULL, *p_tik = NULL, *p_tmd = NULL;
	u32 certs_len, tik_len, tmd_len;

	s32 ret;

	/* Read certificates */
	ret = __CIOS_GetCerts(&p_certs, &certs_len);
	if (ret < 0)
		goto out;

	/* Generate ticket */
	ret = __CIOS_GenerateTik(&p_tik, &tik_len);
	if (ret < 0)
		goto out;

	/* Read TMD */
	ret = __CIOS_GetTMD(tid, &p_tmd, &tmd_len);
	if (ret < 0)
		goto out;

	/* Identify */
	ret = ES_Identify(p_certs, certs_len, p_tmd, tmd_len, p_tik, tik_len, NULL);

out:
	/* Free memory */
	if (p_certs)
		free(p_certs);
	if (p_tik)
		free(p_tik);
	if (p_tmd)
		free(p_tmd);

	return ret;
}
