Put everything back.

This commit is contained in:
jakcron
2022-04-16 21:27:49 +08:00
parent 5d62e839e7
commit bc04de6d09
844 changed files with 114383 additions and 29 deletions
@@ -0,0 +1,117 @@
#pragma once
#include <bitset>
#include <tc/types.h>
#include <tc/io/IStream.h>
#include <tc/crypto/RsaKey.h>
#include <brd/es/es_cert.h>
#include <ntd/n3ds/es/Signature.h>
#include <tc/ArgumentNullException.h>
#include <tc/ArgumentOutOfRangeException.h>
#include <tc/InvalidOperationException.h>
namespace ntd { namespace n3ds { namespace es {
/**
* @brief Get total size of certificate, from raw certificate blob.
*
* @param[in] data Raw certificate data.
*
* @return Size in bytes of certificate data, 0 if invalid data.
*/
size_t getCertificateSize(byte_t* data);
/**
* @brief Get total size of certificate signature structure, from raw certificate blob.
*
* @param[in] data Raw certificate data.
*
* @return Size in bytes of certificate signature structure, 0 if invalid data.
*/
size_t getCertificateSignatureSize(byte_t* data);
/**
* @brief Get offset of certificate signed data, from raw certificate blob.
*
* @param[in] data Raw certificate data.
*
* @return Offset in bytes of signed certificate data, 0 if invalid data.
*/
size_t getCertificateSignableOffset(byte_t* data);
/**
* @brief Get size of certificate signed data, from raw certificate blob.
*
* @param[in] data Raw certificate data.
*
* @return Size in bytes of signed certificate data, 0 if invalid data.
*/
size_t getCertificateSignableSize(byte_t* data);
/**
* @brief Get pointer to certificate header, from raw certificate blob.
*
* @param[in] data Raw certificate data.
*
* @return Void pointer to certificate header. See @ref ntd::es::ESCertHeader.
*/
void* getCertificateHeaderPtr(byte_t* data);
/**
* @brief Get pointer to certificate public key, from raw certificate blob.
*
* @param[in] data Raw certificate data.
*
* @return Void pointer to certificate public. See @ref ntd::es::ESCertRsa4096PublicKey @ref ntd::es::ESCertRsa2048PublicKey @ref ntd::es::ESCertEcc233PublicKey.
*/
void* getCertificatePublicKeyPtr(byte_t* data);
struct Certificate
{
public:
Certificate() :
signature(),
subject(),
date(),
public_key_type(),
rsa4096_public_key(),
rsa2048_public_key(),
ecc233_public_key()
{
memset(calculated_hash.data(), 0, calculated_hash.size());
}
public:
// these fields are only used when deserialised
ntd::n3ds::es::Signature signature; // This includes the signature type, signature data, and issuer
std::array<byte_t, 32> calculated_hash; // This hash is calculated when deserialised so that signature validation can be performed.
// these fields are used in both deserialisation & serialisation
std::string subject;
uint32_t date; // 32bit unix timestamp, not always set
// public key
brd::es::ESCertPubKeyType public_key_type;
brd::es::Rsa4096PublicKey rsa4096_public_key;
brd::es::Rsa2048PublicKey rsa2048_public_key;
brd::es::Ecc233PublicKey ecc233_public_key;
};
class CertificateDeserialiser : public Certificate
{
public:
// cert stream
CertificateDeserialiser(const std::shared_ptr<tc::io::IStream>& cert_stream);
private:
std::string mModuleLabel;
};
/*
class CertificateSerialiser : public tc::io::IStream
{
public:
CertificateSerialiser();
private:
std::string mModuleLabel;
}
*/
}}} // namespace ntd::n3ds::es
@@ -0,0 +1,18 @@
#pragma once
#include <bitset>
#include <tc/types.h>
#include <brd/es/es_sign.h>
namespace ntd { namespace n3ds { namespace es {
class ISigner
{
public:
virtual ~ISigner() = default;
virtual const std::string& getIssuer() = 0;
virtual brd::es::ESSigType getSigType() = 0;
virtual bool signHash(const byte_t* hash, byte_t* signature) = 0;
virtual bool verifyHash(const byte_t* hash, const byte_t* signature) = 0;
};
}}} // namespace ntd::n3ds::es
@@ -0,0 +1,30 @@
#pragma once
#include <bitset>
#include <tc/types.h>
#include <tc/crypto/RsaKey.h>
#include <ntd/n3ds/es/ISigner.h>
#include <tc/InvalidOperationException.h>
#include <tc/ArgumentOutOfRangeException.h>
namespace ntd { namespace n3ds { namespace es {
class RsaSigner : public ntd::n3ds::es::ISigner
{
public:
RsaSigner(brd::es::ESSigType sig_type, const std::string& issuer, const tc::crypto::RsaKey& rsa_key);
const std::string& getIssuer();
brd::es::ESSigType getSigType();
bool signHash(const byte_t* hash, byte_t* signature);
bool verifyHash(const byte_t* hash, const byte_t* signature);
private:
brd::es::ESSigType mSigType;
std::string mIssuer;
tc::crypto::RsaKey mRsaKey;
};
}}} // namespace ntd::n3ds::es
@@ -0,0 +1,50 @@
#pragma once
#include <bitset>
#include <tc/types.h>
#include <tc/io/IStream.h>
#include <tc/crypto/RsaKey.h>
#include <brd/es/es_sign.h>
#include <tc/ArgumentNullException.h>
#include <tc/ArgumentOutOfRangeException.h>
#include <tc/InvalidOperationException.h>
namespace ntd { namespace n3ds { namespace es {
size_t getSignatureSizeFromSigType(brd::es::ESSigType sig_type);
size_t getSignatureIssuerOffset(brd::es::ESSigType sig_type);
struct Signature
{
public:
Signature() :
sig_type(),
sig(),
issuer()
{}
public:
brd::es::ESSigType sig_type;
tc::ByteData sig;
std::string issuer;
};
class SignatureDeserialiser : public Signature
{
public:
// input stream
SignatureDeserialiser(const std::shared_ptr<tc::io::IStream>& stream);
private:
std::string mModuleLabel;
};
/*
class SignatureSerialiser : public tc::io::IStream
{
public:
SignatureSerialiser();
private:
std::string mModuleLabel;
}
*/
}}} // namespace ntd::n3ds::es
@@ -0,0 +1,72 @@
#pragma once
#include <bitset>
#include <tc/types.h>
#include <tc/io/IStream.h>
#include <ntd/n3ds/es/Signature.h>
#include <tc/ArgumentNullException.h>
#include <tc/ArgumentOutOfRangeException.h>
#include <tc/InvalidOperationException.h>
namespace ntd { namespace n3ds { namespace es {
struct Ticket
{
public:
Ticket() :
signature(),
ticket_id(0),
device_id(0),
title_id(0),
ticket_version(0),
license_type(0),
key_id(0),
ec_account_id(0),
launch_count(0),
enabled_content()
{
memset(calculated_hash.data(), 0, calculated_hash.size());
memset(title_key.data(), 0, title_key.size());
}
public:
// these fields are only used in deserialisation
ntd::n3ds::es::Signature signature;
std::array<byte_t, 32> calculated_hash;
// these fields are used in both deserialisation & serialisation
std::array<byte_t, 16> title_key;
uint64_t ticket_id;
uint32_t device_id;
uint64_t title_id;
uint16_t ticket_version;
byte_t license_type;
byte_t key_id;
// reserved region data
uint32_t ec_account_id;
// lp record
uint32_t launch_count; // 0 = unlimited, x = limited to x launches
std::bitset<0x10000> enabled_content;
};
class TicketDeserialiser : public Ticket
{
public:
// tik stream
TicketDeserialiser(const std::shared_ptr<tc::io::IStream>& tik_stream);
private:
std::string mModuleLabel;
};
/*
class TicketSerialiser : public tc::io::IStream
{
public:
// tmd Ticket, issuer, RsaKey
TicketSerialiser();
private:
std::string mModuleLabel;
}
*/
}}} // namespace ntd::n3ds::es
@@ -0,0 +1,106 @@
#pragma once
#include <tc/types.h>
#include <tc/io/IStream.h>
#include <ntd/n3ds/es/Signature.h>
#include <tc/ArgumentNullException.h>
#include <tc/ArgumentOutOfRangeException.h>
#include <tc/InvalidOperationException.h>
namespace ntd { namespace n3ds { namespace es {
struct TitleMetaData
{
public:
TitleMetaData() :
signature(),
title_id(0),
title_version(0),
content_info()
{
memset(calculated_hash.data(), 0, calculated_hash.size());
twl_custom_data.public_save_data_size = 0;
twl_custom_data.private_save_data_size = 0;
twl_custom_data.flag = 0;
ctr_custom_data.save_data_size = 0;
ctr_custom_data.is_snake_only = false;
}
struct ContentInfo
{
public:
ContentInfo() :
id(0),
index(0),
is_encrypted(0),
is_optional(0),
size(0)
{
memset(hash.data(), 0, hash.size());
}
ContentInfo(uint32_t id, uint16_t index, bool is_encrypted, bool is_optional, int64_t size, std::array<byte_t, 32>& hash) :
id(id),
index(index),
is_encrypted(is_encrypted),
is_optional(is_optional),
size(size),
hash(hash)
{
}
public:
uint32_t id;
uint16_t index;
bool is_encrypted;
bool is_optional;
int64_t size;
std::array<byte_t, 32> hash;
};
public:
// these fields are only used in deserialisation
ntd::n3ds::es::Signature signature;
std::array<byte_t, 32> calculated_hash;
// these fields are used in both deserialisation & serialisation
uint64_t title_id;
uint16_t title_version;
struct TwlCustomData
{
uint32_t public_save_data_size;
uint32_t private_save_data_size;
uint8_t flag;
} twl_custom_data;
struct CtrCustomData
{
uint32_t save_data_size;
bool is_snake_only;
} ctr_custom_data;
std::vector<ContentInfo> content_info;
};
class TitleMetaDataDeserialiser : public TitleMetaData
{
public:
// tmd stream
TitleMetaDataDeserialiser(const std::shared_ptr<tc::io::IStream>& tmd_stream);
private:
TitleMetaDataDeserialiser();
std::string mModuleLabel;
};
/*
class TitleMetaDataSerialiser : public tc::io::IStream
{
public:
// tmd TitleMetaData, issuer, RsaKey
TitleMetaDataSerialiser();
private:
std::string mModuleLabel;
}
*/
}}} // namespace ntd::n3ds::es