2010-12-08 08:26:18 +01:00
|
|
|
#include "tiktmd.h"
|
|
|
|
#include "tools.h"
|
|
|
|
#include "aes.h"
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
Tmd::Tmd( QByteArray stuff )
|
|
|
|
{
|
|
|
|
data = stuff;
|
|
|
|
p_tmd = NULL;
|
|
|
|
if( data.isEmpty() || data.size() < 4 )//maybe 4 is still too low?
|
|
|
|
return;
|
|
|
|
|
|
|
|
SetPointer();
|
2010-12-09 12:30:25 +01:00
|
|
|
if( (quint32)data.size() != SignedSize() )
|
|
|
|
{
|
|
|
|
data.resize( SignedSize() );
|
|
|
|
SetPointer();
|
|
|
|
}
|
2010-12-08 08:26:18 +01:00
|
|
|
//hexdump( stuff );
|
|
|
|
}
|
|
|
|
|
|
|
|
quint64 Tmd::Tid()
|
|
|
|
{
|
|
|
|
if( !p_tmd )
|
|
|
|
return 0;
|
|
|
|
return qFromBigEndian( p_tmd->title_id );
|
|
|
|
}
|
|
|
|
|
2010-12-09 12:30:25 +01:00
|
|
|
bool Tmd::SetTid( quint64 tid )
|
|
|
|
{
|
|
|
|
if( !p_tmd )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
p_tmd->title_id = qFromBigEndian( tid );
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-12-08 08:26:18 +01:00
|
|
|
QString Tmd::Cid( quint16 i )
|
|
|
|
{
|
2010-12-08 19:07:57 +01:00
|
|
|
if( !p_tmd || i > qFromBigEndian( p_tmd->num_contents ) )
|
2010-12-08 08:26:18 +01:00
|
|
|
return QString();
|
|
|
|
return QString( "%1" ).arg( qFromBigEndian( p_tmd->contents[ i ].cid ), 8, 16, QChar( '0' ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
QByteArray Tmd::Hash( quint16 i )
|
|
|
|
{
|
2010-12-08 19:07:57 +01:00
|
|
|
if( !p_tmd || i > qFromBigEndian( p_tmd->num_contents ) )
|
2010-12-08 08:26:18 +01:00
|
|
|
return QByteArray();
|
|
|
|
|
|
|
|
return QByteArray( (const char*)&p_tmd->contents[ i ].hash, 20 );
|
|
|
|
}
|
|
|
|
|
2010-12-09 12:30:25 +01:00
|
|
|
bool Tmd::SetHash( quint16 cid, const QByteArray hash )
|
|
|
|
{
|
|
|
|
if( !p_tmd || cid >= qFromBigEndian( p_tmd->num_contents ) || hash.size() != 20 )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const char* h = hash.data();
|
|
|
|
for( quint8 i = 0; i < 20; i++ )
|
|
|
|
p_tmd->contents[ cid ].hash[ i ] = h[ i ];
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-12-08 19:07:57 +01:00
|
|
|
quint16 Tmd::Count()
|
|
|
|
{
|
|
|
|
if( !p_tmd )
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return qFromBigEndian( p_tmd->num_contents );
|
|
|
|
}
|
|
|
|
|
|
|
|
quint16 Tmd::Version()
|
|
|
|
{
|
|
|
|
if( !p_tmd )
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return qFromBigEndian( p_tmd->title_version );
|
|
|
|
}
|
|
|
|
|
2010-12-09 12:30:25 +01:00
|
|
|
bool Tmd::SetVersion( quint16 v )
|
|
|
|
{
|
|
|
|
if( !p_tmd )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
p_tmd->title_version = qFromBigEndian( v );
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-12-08 08:26:18 +01:00
|
|
|
quint64 Tmd::Size( quint16 i )
|
|
|
|
{
|
2010-12-08 19:07:57 +01:00
|
|
|
if( !p_tmd || i > qFromBigEndian( p_tmd->num_contents ) )
|
2010-12-08 08:26:18 +01:00
|
|
|
return 0;
|
|
|
|
return qFromBigEndian( p_tmd->contents[ i ].size );
|
|
|
|
}
|
|
|
|
|
2010-12-09 12:30:25 +01:00
|
|
|
bool Tmd::SetSize( quint16 cid, quint32 size )
|
|
|
|
{
|
|
|
|
if( !p_tmd || cid >= qFromBigEndian( p_tmd->num_contents ) )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
p_tmd->contents[ cid ].size = qFromBigEndian( size );
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-12-08 08:26:18 +01:00
|
|
|
quint16 Tmd::Type( quint16 i )
|
|
|
|
{
|
2010-12-08 19:07:57 +01:00
|
|
|
if( !p_tmd || i > qFromBigEndian( p_tmd->num_contents ) )
|
2010-12-08 08:26:18 +01:00
|
|
|
return 0;
|
|
|
|
return qFromBigEndian( p_tmd->contents[ i ].type );
|
|
|
|
}
|
|
|
|
|
2010-12-09 12:30:25 +01:00
|
|
|
bool Tmd::SetType( quint16 cid, quint16 type )
|
|
|
|
{
|
|
|
|
if( !p_tmd || cid >= qFromBigEndian( p_tmd->num_contents ) )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
p_tmd->contents[ cid ].type = qFromBigEndian( type );
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-12-08 08:26:18 +01:00
|
|
|
quint32 Tmd::SignedSize()
|
|
|
|
{
|
2010-12-08 19:07:57 +01:00
|
|
|
if( !p_tmd )
|
|
|
|
return 0;
|
2010-12-08 08:26:18 +01:00
|
|
|
return payLoadOffset + sizeof( tmd ) + ( sizeof( tmd_content ) * qFromBigEndian( p_tmd->num_contents ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
void Tmd::SetPointer()
|
|
|
|
{
|
|
|
|
payLoadOffset = 0;
|
|
|
|
if( data.startsWith( "\x0\x1\x0\x0" ) )
|
|
|
|
payLoadOffset = sizeof( sig_rsa2048 );
|
|
|
|
|
|
|
|
else if( data.startsWith( "\x0\x1\x0\x1" ) )
|
|
|
|
payLoadOffset = sizeof( sig_rsa4096 );
|
|
|
|
|
|
|
|
else if( data.startsWith( "\x0\x1\x0\x2" ) )
|
|
|
|
payLoadOffset = sizeof( sig_ecdsa );
|
|
|
|
|
|
|
|
p_tmd = (tmd*)((quint8*)data.data() + payLoadOffset);
|
|
|
|
}
|
|
|
|
|
2010-12-09 12:30:25 +01:00
|
|
|
void Tmd::Dbg()
|
|
|
|
{
|
|
|
|
if( !p_tmd )
|
|
|
|
return;
|
|
|
|
|
|
|
|
QString contents;
|
|
|
|
quint16 cnt = Count();
|
|
|
|
for( quint16 i = 0; i < cnt; i++ )
|
|
|
|
{
|
|
|
|
contents += QString( "%1 %2 %3 %4 " ).arg( i, 2, 16 ).arg( Type( i ), 4, 16 ).arg( Cid( i ) ).arg( Size( i ), 8, 16 )
|
|
|
|
+ Hash( i ).toHex() + "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
QString s = QString( "TMD Dbg:\ntid:: %1\ncnt:: %2\n" )
|
|
|
|
.arg( Tid(), 16, 16, QChar( '0' ) )
|
|
|
|
.arg( cnt ) + contents;
|
|
|
|
qDebug() << s;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Tmd::FakeSign()
|
|
|
|
{
|
|
|
|
if( !p_tmd || payLoadOffset < 5 )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
quint32 size = SignedSize();
|
|
|
|
memset( (void*)( data.data() + 4 ), 0, payLoadOffset - 4 );//zero the RSA
|
|
|
|
|
|
|
|
quint16 i = 0;
|
|
|
|
bool ret = false;//brute force the sha1
|
|
|
|
do
|
|
|
|
{
|
|
|
|
p_tmd->zero = i;//no need to worry about endian here
|
|
|
|
if( GetSha1( data.mid( payLoadOffset, size ) ).startsWith( '\0' ) )
|
|
|
|
{
|
|
|
|
ret = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while( ++i );
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-12-08 08:26:18 +01:00
|
|
|
Ticket::Ticket( QByteArray stuff )
|
|
|
|
{
|
|
|
|
data = stuff;
|
|
|
|
p_tik = NULL;
|
|
|
|
if( data.isEmpty() || data.size() < 4 )//maybe 4 is still too low?
|
|
|
|
return;
|
|
|
|
|
|
|
|
SetPointer();
|
2010-12-09 12:30:25 +01:00
|
|
|
if( (quint32)data.size() != SignedSize() )
|
|
|
|
{
|
|
|
|
data.resize( SignedSize() );
|
|
|
|
SetPointer();
|
|
|
|
}
|
2010-12-08 08:26:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
quint64 Ticket::Tid()
|
|
|
|
{
|
|
|
|
if( !p_tik )
|
|
|
|
return 0;
|
|
|
|
return qFromBigEndian( p_tik->titleid );
|
|
|
|
}
|
|
|
|
|
2010-12-09 12:30:25 +01:00
|
|
|
bool Ticket::SetTid( quint64 tid )
|
|
|
|
{
|
|
|
|
if( !p_tik )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
p_tik->titleid = qFromBigEndian( tid );
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-12-08 08:26:18 +01:00
|
|
|
QByteArray Ticket::DecryptedKey()
|
|
|
|
{
|
|
|
|
quint8 iv[ 16 ];
|
|
|
|
quint8 keyin[ 16 ];
|
|
|
|
quint8 keyout[ 16 ];
|
|
|
|
static quint8 commonkey[ 16 ] = COMMON_KEY;
|
|
|
|
|
|
|
|
quint8 *enc_key = (quint8 *)&p_tik->cipher_title_key;
|
|
|
|
memcpy( keyin, enc_key, sizeof keyin );
|
|
|
|
memset( keyout, 0, sizeof keyout );
|
|
|
|
memset( iv, 0, sizeof iv);
|
|
|
|
memcpy( iv, &p_tik->titleid, sizeof p_tik->titleid );
|
|
|
|
|
|
|
|
aes_set_key( commonkey );
|
|
|
|
aes_decrypt( iv, keyin, keyout, sizeof( keyin ) );
|
|
|
|
|
|
|
|
return QByteArray( (const char*)&keyout, 16 );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
quint32 Ticket::SignedSize()
|
|
|
|
{
|
|
|
|
return payLoadOffset + sizeof( tik );
|
|
|
|
}
|
|
|
|
|
|
|
|
void Ticket::SetPointer()
|
|
|
|
{
|
|
|
|
payLoadOffset = 0;
|
|
|
|
if( data.startsWith( "\x0\x1\x0\x0" ) )
|
|
|
|
payLoadOffset = sizeof( sig_rsa2048 );
|
|
|
|
|
|
|
|
else if( data.startsWith( "\x0\x1\x0\x1" ) )
|
|
|
|
payLoadOffset = sizeof( sig_rsa4096 );
|
|
|
|
|
|
|
|
else if( data.startsWith( "\x0\x1\x0\x2" ) )
|
|
|
|
payLoadOffset = sizeof( sig_ecdsa );
|
|
|
|
|
|
|
|
p_tik = (tik*)((quint8*)data.data() + payLoadOffset);
|
|
|
|
}
|
2010-12-09 12:30:25 +01:00
|
|
|
|
|
|
|
bool Ticket::FakeSign()
|
|
|
|
{
|
|
|
|
if( !p_tik || payLoadOffset < 5 )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
quint32 size = SignedSize();
|
|
|
|
memset( (void*)( data.data() + 4 ), 0, payLoadOffset - 4 );//zero the RSA
|
|
|
|
|
|
|
|
quint16 i = 0;
|
|
|
|
bool ret = false;//brute force the sha1
|
|
|
|
do
|
|
|
|
{
|
|
|
|
p_tik->padding = i;//no need to worry about endian here
|
|
|
|
if( GetSha1( data.mid( payLoadOffset, size ) ).startsWith( '\0' ) )
|
|
|
|
{
|
|
|
|
ret = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while( ++i );
|
|
|
|
return ret;
|
|
|
|
}
|