[es] Updated to libfnd design changes.

This commit is contained in:
jakcron 2018-06-24 12:46:11 +08:00
parent 9b3a26806a
commit 313422bd57
9 changed files with 377 additions and 452 deletions

View file

@ -1,13 +1,12 @@
#pragma once
#include <string>
#include <fnd/MemoryBlob.h>
#include <fnd/ISerialiseableBinary.h>
#include <fnd/ISerialisable.h>
#include <es/cert.h>
namespace es
{
class CertificateBody
: public fnd::ISerialiseableBinary
: public fnd::ISerialisable
{
public:
CertificateBody();
@ -17,12 +16,12 @@ namespace es
bool operator==(const CertificateBody& other) const;
bool operator!=(const CertificateBody& other) const;
void importBinary(const byte_t* src, size_t size);
void exportBinary();
const byte_t* getBytes() const;
size_t getSize() const;
// export/import binary
void toBytes();
void fromBytes(const byte_t* src, size_t size);
const fnd::Vec<byte_t>& getBytes() const;
// variables
void clear();
const std::string& getIssuer() const;
@ -50,7 +49,7 @@ namespace es
const std::string kModuleName = "CERTIFICATE_BODY";
// raw binary
fnd::MemoryBlob mBinaryBlob;
fnd::Vec<byte_t> mRawBinary;
// variables
std::string mIssuer;
@ -61,9 +60,5 @@ namespace es
crypto::rsa::sRsa4096Key mRsa4096PublicKey;
crypto::rsa::sRsa2048Key mRsa2048PublicKey;
crypto::ecdsa::sEcdsa240Point mEcdsa240PublicKey;
// helpers
bool isEqual(const CertificateBody& other) const;
void copyFrom(const CertificateBody& other);
};
}

View file

@ -1,30 +1,25 @@
#pragma once
#include <string>
#include <fnd/MemoryBlob.h>
#include <fnd/ISerialiseableBinary.h>
#include <fnd/ISerialisable.h>
#include <es/ticket.h>
namespace es
{
class SectionHeader_V2 :
public fnd::ISerialiseableBinary
public fnd::ISerialisable
{
public:
SectionHeader_V2();
SectionHeader_V2(const SectionHeader_V2& other);
SectionHeader_V2(const byte_t* bytes, size_t len);
bool operator==(const SectionHeader_V2& other) const;
bool operator!=(const SectionHeader_V2& other) const;
void operator=(const SectionHeader_V2& other);
// to be used after export
const byte_t* getBytes() const;
size_t getSize() const;
// export/import binary
virtual void exportBinary();
virtual void importBinary(const byte_t* bytes, size_t len);
void toBytes();
void fromBytes(const byte_t* data, size_t len);
const fnd::Vec<byte_t>& getBytes() const;
// variables
virtual void clear();
@ -48,7 +43,7 @@ namespace es
const std::string kModuleName = "SECTION_HEADER_V2";
// raw binary
fnd::MemoryBlob mBinaryBlob;
fnd::Vec<byte_t> mRawBinary;
// variables
uint32_t mSectionOffset;
@ -56,11 +51,6 @@ namespace es
uint32_t mSectionSize;
uint16_t mRecordNum;
ticket::SectionType mSectionType;
// helpers
bool isEqual(const SectionHeader_V2& other) const;
void copyFrom(const SectionHeader_V2& other);
};
}

View file

@ -1,13 +1,12 @@
#pragma once
#include <string>
#include <fnd/MemoryBlob.h>
#include <fnd/ISerialiseableBinary.h>
#include <fnd/ISerialisable.h>
#include <es/sign.h>
namespace es
{
class SignatureBlock
: public fnd::ISerialiseableBinary
: public fnd::ISerialisable
{
public:
SignatureBlock();
@ -17,12 +16,12 @@ namespace es
bool operator==(const SignatureBlock& other) const;
bool operator!=(const SignatureBlock& other) const;
void importBinary(const byte_t* src, size_t size);
void exportBinary();
const byte_t* getBytes() const;
size_t getSize() const;
// export/import binary
void toBytes();
void fromBytes(const byte_t* src, size_t size);
const const fnd::Vec<byte_t>& getBytes() const;
// variables
void clear();
es::sign::SignType getSignType() const;
@ -31,23 +30,19 @@ namespace es
bool isLittleEndian() const;
void setLittleEndian(bool isLE);
const fnd::MemoryBlob& getSignature() const;
void setSignature(const fnd::MemoryBlob& signature);
const fnd::Vec<byte_t>& getSignature() const;
void setSignature(const fnd::Vec<byte_t>& signature);
private:
const std::string kModuleName = "SIGNATURE_BLOCK";
// raw binary
fnd::MemoryBlob mBinaryBlob;
fnd::Vec<byte_t> mRawBinary;
// variables
es::sign::SignType mSignType;
bool mIsLittleEndian;
fnd::MemoryBlob mSignature;
// helpers
bool isEqual(const SignatureBlock& other) const;
void copyFrom(const SignatureBlock& other);
fnd::Vec<byte_t> mSignature;
};
}

View file

@ -1,113 +1,137 @@
#pragma once
#include <string>
#include <fnd/MemoryBlob.h>
#include <fnd/ISerialiseableBinary.h>
#include <fnd/ISerialisable.h>
#include <es/SignatureBlock.h>
namespace es
{
template <class T>
class SignedData
: public fnd::ISerialiseableBinary
: public fnd::ISerialiseable
{
public:
SignedData()
{
clear();
}
SignedData(const SignedData& other)
{
copyFrom(other);
}
SignedData();
SignedData(const SignedData& other);
void operator=(const SignedData& other)
{
copyFrom(other);
}
bool operator==(const SignedData& other) const
{
return isEqual(other);
}
bool operator!=(const SignedData& other) const
{
return !(*this == other);
}
void operator=(const SignedData& other);
bool operator==(const SignedData& other) const;
bool operator!=(const SignedData& other) const;
void importBinary(const byte_t* src, size_t size)
{
mSignature.importBinary(src, size);
mBody.importBinary(src + mSignature.getSize(), size - mSignature.getSize());
// export/import
const void toBytes();
void fromBytes(const byte_t* src, size_t size);
const fnd::Vec<byte_t>& getBytes() const;
mBinaryBlob.alloc(mSignature.getSize() + mBody.getSize());
memcpy(mBinaryBlob.getBytes(), src, mBinaryBlob.getSize());
}
// variables
void clear();
void exportBinary()
{
mSignature.exportBinary();
mBody.exportBinary();
const es::SignatureBlock& getSignature() const;
void setSignature(const SignatureBlock& signature);
mBinaryBlob.alloc(mSignature.getSize() + mBody.getSize());
memcpy(mBinaryBlob.getBytes(), mSignature.getBytes(), mSignature.getSize());
memcpy(mBinaryBlob.getBytes() + mSignature.getSize(), mBody.getBytes(), mBody.getSize());
}
const byte_t* getBytes() const
{
return mBinaryBlob.getBytes();
}
size_t getSize() const
{
return mBinaryBlob.getSize();
}
void clear()
{
mBinaryBlob.clear();
mSignature.clear();
mBody.clear();
}
const es::SignatureBlock& getSignature() const
{
return mSignature;
}
void setSignature(const SignatureBlock& signature)
{
mSignature = signature;
}
const T& getBody() const
{
return mBody;
}
void setBody(const T& body)
{
mBody = body;
}
const T& getBody() const;
void setBody(const T& body);
private:
const std::string kModuleName = "SIGNED_DATA";
// raw binary
fnd::MemoryBlob mBinaryBlob;
fnd::Vec<byte_t> mRawBinary;
// variables
SignatureBlock mSignature;
T mBody;
};
// helpers
bool isEqual(const SignedData& other) const
template <class T>
inline SignedData::SignedData()
{
clear();
}
template <class T>
inline SignedData::SignedData(const SignedData& other)
{
*this = other;
}
template <class T>
inline void SignedData::operator=(const SignedData& other)
{
mRawBinary = other.mRawBinary;
mSignature = other.mSignature;
mBody = other.mBody;
}
template <class T>
inline bool SignedData::operator==(const SignedData& other) const
{
return (mSignature == other.mSignature) \
&& (mBody == other.mBody);
}
void copyFrom(const SignedData& other)
template <class T>
inline bool SignedData::operator!=(const SignedData& other) const
{
mBinaryBlob = other.mBinaryBlob;
mSignature = other.mSignature;
mBody = other.mBody;
}
};
return !(*this == other);
}
template <class T>
inline const void SignedData::toBytes()
{
mSignature.toBytes();
mBody.toBytes();
mRawBinary.alloc(mSignature.getBytes().size() + mBody.getBytes().size());
memcpy(mRawBinary.getBytes().data(), mSignature.getBytes().data(), mSignature.getBytes().size());
memcpy(mRawBinary.getBytes().data() + mSignature.getBytes().size(), mBody.getBytes().data(), mBody.getBytes().size());
}
template <class T>
inline void SignedData::fromBytes(const byte_t* src, size_t size)
{
mSignature.fromBytes(src, size);
mBody.fromBytes(src + mSignature.getBytes().size(), size - mSignature.getBytes().size());
mRawBinary.alloc(mSignature.getBytes().size() + mBody.getBytes().size());
memcpy(mRawBinary.getBytes().data(), src, mRawBinary.getBytes().size());
}
template <class T>
inline const fnd::Vec<byte_t>& SignedData::getBytes() const
{
return mRawBinary;
}
template <class T>
inline void SignedData::clear()
{
mRawBinary.clear();
mSignature.clear();
mBody.clear();
}
template <class T>
inline const es::SignatureBlock& SignedData::getSignature() const
{
return mSignature;
}
template <class T>
inline void SignedData::setSignature(const SignatureBlock& signature)
{
mSignature = signature;
}
template <class T>
inline const T& SignedData::getBody() const
{
return mBody;
}
template <class T>
inline void SignedData::setBody(const T& body)
{
mBody = body;
}
}

View file

@ -1,33 +1,28 @@
#pragma once
#include <string>
#include <fnd/MemoryBlob.h>
#include <fnd/ISerialiseableBinary.h>
#include <fnd/ISerialisable.h>
#include <es/ticket.h>
namespace es
{
class TicketBody_V2 :
public fnd::ISerialiseableBinary
public fnd::ISerialisable
{
public:
TicketBody_V2();
TicketBody_V2(const TicketBody_V2& other);
TicketBody_V2(const byte_t* bytes, size_t len);
bool operator==(const TicketBody_V2& other) const;
bool operator!=(const TicketBody_V2& other) const;
void operator=(const TicketBody_V2& other);
// to be used after export
const byte_t* getBytes() const;
size_t getSize() const;
// export/import binary
virtual void exportBinary();
virtual void importBinary(const byte_t* bytes, size_t len);
void toBytes();
void fromBytes(const byte_t* bytes, size_t len);
const fnd::Vec<byte_t>& getBytes() const;
// variables
virtual void clear();
void clear();
const std::string& getIssuer() const;
void setIssuer(const std::string& issuer);
@ -87,7 +82,7 @@ namespace es
const std::string kModuleName = "TICKET_BODY_V2";
// raw binary
fnd::MemoryBlob mBinaryBlob;
fnd::Vec<byte_t> mRawBinary;
// variables
std::string mIssuer;
@ -108,10 +103,6 @@ namespace es
uint32_t mSectHeaderOffset;
uint16_t mSectNum;
uint16_t mSectEntrySize;
// helpers
bool isEqual(const TicketBody_V2& other) const;
void copyFrom(const TicketBody_V2& other);
};
}

View file

@ -7,17 +7,30 @@ es::CertificateBody::CertificateBody()
es::CertificateBody::CertificateBody(const CertificateBody& other)
{
copyFrom(other);
*this = other;
}
void es::CertificateBody::operator=(const CertificateBody& other)
{
copyFrom(other);
mRawBinary = other.mRawBinary;
mIssuer = other.mIssuer;
mSubject = other.mSubject;
mCertId = other.mCertId;
mPublicKeyType = other.mPublicKeyType;
mRsa4096PublicKey = other.mRsa4096PublicKey;
mRsa2048PublicKey = other.mRsa2048PublicKey;
mEcdsa240PublicKey = other.mEcdsa240PublicKey;
}
bool es::CertificateBody::operator==(const CertificateBody& other) const
{
return isEqual(other);
return (mIssuer == other.mIssuer) \
&& (mSubject == other.mSubject) \
&& (mCertId == other.mCertId) \
&& (mPublicKeyType == other.mPublicKeyType) \
&& (mRsa4096PublicKey == other.mRsa4096PublicKey) \
&& (mRsa2048PublicKey == other.mRsa2048PublicKey) \
&& (mEcdsa240PublicKey == other.mEcdsa240PublicKey);
}
bool es::CertificateBody::operator!=(const CertificateBody& other) const
@ -25,7 +38,55 @@ bool es::CertificateBody::operator!=(const CertificateBody& other) const
return !(*this == other);
}
void es::CertificateBody::importBinary(const byte_t* src, size_t size)
void es::CertificateBody::toBytes()
{
// get public key size
size_t pubkeySize = 0;
switch (mPublicKeyType)
{
case (cert::RSA4096):
pubkeySize = sizeof(sRsa4096PublicKeyBlock);
break;
case (cert::RSA2048):
pubkeySize = sizeof(sRsa2048PublicKeyBlock);
break;
case (cert::ECDSA240):
pubkeySize = sizeof(sEcdsa240PublicKeyBlock);
break;
default:
throw fnd::Exception(kModuleName, "Unknown public key type");
}
mRawBinary.alloc(sizeof(sCertificateHeader) + pubkeySize);
sCertificateHeader* hdr = (sCertificateHeader*)mRawBinary.data();
// copy header vars
strncpy(hdr->issuer, mIssuer.c_str(), cert::kIssuerSize);
hdr->key_type = mPublicKeyType;
strncpy(hdr->subject, mSubject.c_str(), cert::kSubjectSize);
hdr->cert_id = mCertId;
// copy public key
if (mPublicKeyType == cert::RSA4096)
{
sRsa4096PublicKeyBlock* pubkey = (sRsa4096PublicKeyBlock*)(mRawBinary.data() + sizeof(sCertificateHeader));
memcpy(pubkey->modulus, mRsa4096PublicKey.modulus, sizeof(mRsa4096PublicKey.modulus));
memcpy(pubkey->public_exponent, mRsa4096PublicKey.public_exponent, sizeof(mRsa4096PublicKey.public_exponent));
}
else if (mPublicKeyType == cert::RSA2048)
{
sRsa2048PublicKeyBlock* pubkey = (sRsa2048PublicKeyBlock*)(mRawBinary.data() + sizeof(sCertificateHeader));
memcpy(pubkey->modulus, mRsa2048PublicKey.modulus, sizeof(mRsa2048PublicKey.modulus));
memcpy(pubkey->public_exponent, mRsa2048PublicKey.public_exponent, sizeof(mRsa2048PublicKey.public_exponent));
}
else if (mPublicKeyType == cert::ECDSA240)
{
sEcdsa240PublicKeyBlock* pubkey = (sEcdsa240PublicKeyBlock*)(mRawBinary.data() + sizeof(sCertificateHeader));
pubkey->public_key = mEcdsa240PublicKey;
}
}
void es::CertificateBody::fromBytes(const byte_t* src, size_t size)
{
clear();
@ -61,11 +122,11 @@ void es::CertificateBody::importBinary(const byte_t* src, size_t size)
}
// save raw binary
mBinaryBlob.alloc((sizeof(sCertificateHeader) + pubkeySize));
memcpy(mBinaryBlob.getBytes(), src, mBinaryBlob.getSize());
mRawBinary.alloc((sizeof(sCertificateHeader) + pubkeySize));
memcpy(mRawBinary.data(), src, mRawBinary.size());
// save hdr variables
hdr = (const sCertificateHeader*)mBinaryBlob.getBytes();
hdr = (const sCertificateHeader*)mRawBinary.data();
if (hdr->issuer[0] != 0)
mIssuer = std::string(hdr->issuer, cert::kIssuerSize);
@ -77,80 +138,28 @@ void es::CertificateBody::importBinary(const byte_t* src, size_t size)
// save public key
if (mPublicKeyType == cert::RSA4096)
{
const sRsa4096PublicKeyBlock* pubkey = (const sRsa4096PublicKeyBlock*)(mBinaryBlob.getBytes() + sizeof(sCertificateHeader));
const sRsa4096PublicKeyBlock* pubkey = (const sRsa4096PublicKeyBlock*)(mRawBinary.data() + sizeof(sCertificateHeader));
memcpy(mRsa4096PublicKey.modulus, pubkey->modulus, sizeof(mRsa4096PublicKey.modulus));
memcpy(mRsa4096PublicKey.public_exponent, pubkey->public_exponent, sizeof(mRsa4096PublicKey.public_exponent));
}
else if (mPublicKeyType == cert::RSA2048)
{
const sRsa2048PublicKeyBlock* pubkey = (const sRsa2048PublicKeyBlock*)(mBinaryBlob.getBytes() + sizeof(sCertificateHeader));
const sRsa2048PublicKeyBlock* pubkey = (const sRsa2048PublicKeyBlock*)(mRawBinary.data() + sizeof(sCertificateHeader));
memcpy(mRsa2048PublicKey.modulus, pubkey->modulus, sizeof(mRsa2048PublicKey.modulus));
memcpy(mRsa2048PublicKey.public_exponent, pubkey->public_exponent, sizeof(mRsa2048PublicKey.public_exponent));
}
else if (mPublicKeyType == cert::ECDSA240)
{
const sEcdsa240PublicKeyBlock* pubkey = (const sEcdsa240PublicKeyBlock*)(mBinaryBlob.getBytes() + sizeof(sCertificateHeader));
const sEcdsa240PublicKeyBlock* pubkey = (const sEcdsa240PublicKeyBlock*)(mRawBinary.data() + sizeof(sCertificateHeader));
mEcdsa240PublicKey = pubkey->public_key;
}
}
void es::CertificateBody::exportBinary()
const fnd::Vec<byte_t>& es::CertificateBody::getBytes() const
{
// get public key size
size_t pubkeySize = 0;
switch (mPublicKeyType)
{
case (cert::RSA4096):
pubkeySize = sizeof(sRsa4096PublicKeyBlock);
break;
case (cert::RSA2048):
pubkeySize = sizeof(sRsa2048PublicKeyBlock);
break;
case (cert::ECDSA240):
pubkeySize = sizeof(sEcdsa240PublicKeyBlock);
break;
default:
throw fnd::Exception(kModuleName, "Unknown public key type");
return mRawBinary;
}
mBinaryBlob.alloc(sizeof(sCertificateHeader) + pubkeySize);
sCertificateHeader* hdr = (sCertificateHeader*)mBinaryBlob.getBytes();
// copy header vars
strncpy(hdr->issuer, mIssuer.c_str(), cert::kIssuerSize);
hdr->key_type = mPublicKeyType;
strncpy(hdr->subject, mSubject.c_str(), cert::kSubjectSize);
hdr->cert_id = mCertId;
// copy public key
if (mPublicKeyType == cert::RSA4096)
{
sRsa4096PublicKeyBlock* pubkey = (sRsa4096PublicKeyBlock*)(mBinaryBlob.getBytes() + sizeof(sCertificateHeader));
memcpy(pubkey->modulus, mRsa4096PublicKey.modulus, sizeof(mRsa4096PublicKey.modulus));
memcpy(pubkey->public_exponent, mRsa4096PublicKey.public_exponent, sizeof(mRsa4096PublicKey.public_exponent));
}
else if (mPublicKeyType == cert::RSA2048)
{
sRsa2048PublicKeyBlock* pubkey = (sRsa2048PublicKeyBlock*)(mBinaryBlob.getBytes() + sizeof(sCertificateHeader));
memcpy(pubkey->modulus, mRsa2048PublicKey.modulus, sizeof(mRsa2048PublicKey.modulus));
memcpy(pubkey->public_exponent, mRsa2048PublicKey.public_exponent, sizeof(mRsa2048PublicKey.public_exponent));
}
else if (mPublicKeyType == cert::ECDSA240)
{
sEcdsa240PublicKeyBlock* pubkey = (sEcdsa240PublicKeyBlock*)(mBinaryBlob.getBytes() + sizeof(sCertificateHeader));
pubkey->public_key = mEcdsa240PublicKey;
}
}
const byte_t* es::CertificateBody::getBytes() const
{
return mBinaryBlob.getBytes();
}
size_t es::CertificateBody::getSize() const
{
return mBinaryBlob.getSize();
}
void es::CertificateBody::clear()
{
@ -243,27 +252,3 @@ void es::CertificateBody::setEcdsa240PublicKey(const crypto::ecdsa::sEcdsa240Poi
{
mEcdsa240PublicKey = key;
}
bool es::CertificateBody::isEqual(const CertificateBody& other) const
{
return (mIssuer == other.mIssuer) \
&& (mSubject == other.mSubject) \
&& (mCertId == other.mCertId) \
&& (mPublicKeyType == other.mPublicKeyType) \
&& (mRsa4096PublicKey == other.mRsa4096PublicKey) \
&& (mRsa2048PublicKey == other.mRsa2048PublicKey) \
&& (mEcdsa240PublicKey == other.mEcdsa240PublicKey);
}
void es::CertificateBody::copyFrom(const CertificateBody& other)
{
mBinaryBlob = other.mBinaryBlob;
mIssuer = other.mIssuer;
mSubject = other.mSubject;
mCertId = other.mCertId;
mPublicKeyType = other.mPublicKeyType;
mRsa4096PublicKey = other.mRsa4096PublicKey;
mRsa2048PublicKey = other.mRsa2048PublicKey;
mEcdsa240PublicKey = other.mEcdsa240PublicKey;
}

View file

@ -7,72 +7,10 @@ es::SectionHeader_V2::SectionHeader_V2()
es::SectionHeader_V2::SectionHeader_V2(const SectionHeader_V2 & other)
{
copyFrom(other);
}
es::SectionHeader_V2::SectionHeader_V2(const byte_t * bytes, size_t len)
{
importBinary(bytes, len);
*this = other;
}
bool es::SectionHeader_V2::operator==(const SectionHeader_V2 & other) const
{
return isEqual(other);
}
bool es::SectionHeader_V2::operator!=(const SectionHeader_V2 & other) const
{
return !isEqual(other);
}
void es::SectionHeader_V2::operator=(const SectionHeader_V2 & other)
{
copyFrom(other);
}
const byte_t * es::SectionHeader_V2::getBytes() const
{
return mBinaryBlob.getBytes();
}
size_t es::SectionHeader_V2::getSize() const
{
return mBinaryBlob.getSize();
}
void es::SectionHeader_V2::exportBinary()
{
mBinaryBlob.alloc(sizeof(sSectionHeader_v2));
sSectionHeader_v2* hdr = (sSectionHeader_v2*)mBinaryBlob.getBytes();
hdr->section_offset = (mSectionOffset);
hdr->record_size = (mRecordSize);
hdr->section_size = (mSectionSize);
hdr->record_num = (mRecordNum);
hdr->section_type = (mSectionType);
}
void es::SectionHeader_V2::importBinary(const byte_t * bytes, size_t len)
{
if (len < sizeof(sSectionHeader_v2))
{
throw fnd::Exception(kModuleName, "Binary too small");
}
clear();
mBinaryBlob.alloc(sizeof(sSectionHeader_v2));
memcpy(mBinaryBlob.getBytes(), bytes, mBinaryBlob.getSize());
sSectionHeader_v2* hdr = (sSectionHeader_v2*)mBinaryBlob.getBytes();
mSectionOffset = hdr->section_offset.get();
mRecordSize = hdr->record_size.get();
mSectionSize = hdr->section_size.get();
mRecordNum = hdr->record_num.get();
mSectionType = (ticket::SectionType)hdr->section_type.get();
}
bool es::SectionHeader_V2::isEqual(const SectionHeader_V2 & other) const
{
return (mSectionOffset == other.mSectionOffset) \
&& (mRecordSize == other.mRecordSize) \
@ -81,15 +19,20 @@ bool es::SectionHeader_V2::isEqual(const SectionHeader_V2 & other) const
&& (mSectionType == other.mSectionType);
}
void es::SectionHeader_V2::copyFrom(const SectionHeader_V2 & other)
bool es::SectionHeader_V2::operator!=(const SectionHeader_V2 & other) const
{
if (other.getSize())
return !(*this ==other);
}
void es::SectionHeader_V2::operator=(const SectionHeader_V2 & other)
{
importBinary(other.getBytes(), other.getSize());
if (other.getBytes().size())
{
fromBytes(other.getBytes().data(), other.getBytes().size());
}
else
{
mBinaryBlob.clear();
mRawBinary.clear();
mSectionOffset = other.mSectionOffset;
mRecordSize = other.mRecordSize;
mSectionSize = other.mSectionSize;
@ -98,9 +41,46 @@ void es::SectionHeader_V2::copyFrom(const SectionHeader_V2 & other)
}
}
void es::SectionHeader_V2::toBytes()
{
mRawBinary.alloc(sizeof(sSectionHeader_v2));
sSectionHeader_v2* hdr = (sSectionHeader_v2*)mRawBinary.data();
hdr->section_offset = (mSectionOffset);
hdr->record_size = (mRecordSize);
hdr->section_size = (mSectionSize);
hdr->record_num = (mRecordNum);
hdr->section_type = (mSectionType);
}
void es::SectionHeader_V2::fromBytes(const byte_t * bytes, size_t len)
{
if (len < sizeof(sSectionHeader_v2))
{
throw fnd::Exception(kModuleName, "Binary too small");
}
clear();
mRawBinary.alloc(sizeof(sSectionHeader_v2));
memcpy(mRawBinary.data(), bytes, mRawBinary.size());
sSectionHeader_v2* hdr = (sSectionHeader_v2*)mRawBinary.data();
mSectionOffset = hdr->section_offset.get();
mRecordSize = hdr->record_size.get();
mSectionSize = hdr->section_size.get();
mRecordNum = hdr->record_num.get();
mSectionType = (ticket::SectionType)hdr->section_type.get();
}
const fnd::Vec<byte_t>& es::SectionHeader_V2::getBytes() const
{
return mRawBinary;
}
void es::SectionHeader_V2::clear()
{
mBinaryBlob.clear();
mRawBinary.clear();
mSectionOffset = 0;
mRecordSize = 0;
mSectionSize = 0;

View file

@ -7,17 +7,22 @@ es::SignatureBlock::SignatureBlock()
es::SignatureBlock::SignatureBlock(const SignatureBlock& other)
{
copyFrom(other);
*this = other;
}
void es::SignatureBlock::operator=(const SignatureBlock& other)
{
copyFrom(other);
mRawBinary = other.mRawBinary;
mSignType = other.mSignType;
mIsLittleEndian = other.mIsLittleEndian;
mSignature = other.mSignature;
}
bool es::SignatureBlock::operator==(const SignatureBlock& other) const
{
return isEqual(other);
return (mSignType == other.mSignType) \
&& (mIsLittleEndian == other.mIsLittleEndian) \
&& (mSignature == other.mSignature);
}
bool es::SignatureBlock::operator!=(const SignatureBlock& other) const
@ -25,7 +30,45 @@ bool es::SignatureBlock::operator!=(const SignatureBlock& other) const
return !(*this == other);
}
void es::SignatureBlock::importBinary(const byte_t* src, size_t size)
void es::SignatureBlock::toBytes()
{
size_t totalSize = 0;
size_t sigSize = 0;
switch (mSignType)
{
case (sign::SIGN_RSA4096_SHA1):
case (sign::SIGN_RSA4096_SHA256):
totalSize = sizeof(sRsa4096SignBlock);
sigSize = crypto::rsa::kRsa4096Size;
break;
case (sign::SIGN_RSA2048_SHA1):
case (sign::SIGN_RSA2048_SHA256):
totalSize = sizeof(sRsa2048SignBlock);
sigSize = crypto::rsa::kRsa2048Size;
break;
case (sign::SIGN_ECDSA240_SHA1):
case (sign::SIGN_ECDSA240_SHA256):
totalSize = sizeof(sEcdsa240SignBlock);
sigSize = sign::kEcdsaSigSize;
break;
default:
throw fnd::Exception(kModuleName, "Unknown signature type");
}
if (mSignature.size() != sigSize)
throw fnd::Exception(kModuleName, "Signature size is incorrect");
// commit to binary
mRawBinary.alloc(totalSize);
if (mIsLittleEndian)
*(le_uint32_t*)(mRawBinary.data()) = mSignType;
else
*(be_uint32_t*)(mRawBinary.data()) = mSignType;
memcpy(mRawBinary.data() + 4, mSignature.data(), sigSize);
}
void es::SignatureBlock::fromBytes(const byte_t* src, size_t size)
{
clear();
@ -87,65 +130,22 @@ void es::SignatureBlock::importBinary(const byte_t* src, size_t size)
throw fnd::Exception(kModuleName, "Certificate too small");
}
mBinaryBlob.alloc(totalSize);
memcpy(mBinaryBlob.getBytes(), src, totalSize);
mRawBinary.alloc(totalSize);
memcpy(mRawBinary.data(), src, totalSize);
mSignType = (sign::SignType)signType;
mSignature.alloc(sigSize);
memcpy(mSignature.getBytes(), mBinaryBlob.getBytes() + 4, sigSize);
memcpy(mSignature.data(), mRawBinary.data() + 4, sigSize);
}
void es::SignatureBlock::exportBinary()
const fnd::Vec<byte_t>& es::SignatureBlock::getBytes() const
{
size_t totalSize = 0;
size_t sigSize = 0;
switch (mSignType)
{
case (sign::SIGN_RSA4096_SHA1):
case (sign::SIGN_RSA4096_SHA256):
totalSize = sizeof(sRsa4096SignBlock);
sigSize = crypto::rsa::kRsa4096Size;
break;
case (sign::SIGN_RSA2048_SHA1):
case (sign::SIGN_RSA2048_SHA256):
totalSize = sizeof(sRsa2048SignBlock);
sigSize = crypto::rsa::kRsa2048Size;
break;
case (sign::SIGN_ECDSA240_SHA1):
case (sign::SIGN_ECDSA240_SHA256):
totalSize = sizeof(sEcdsa240SignBlock);
sigSize = sign::kEcdsaSigSize;
break;
default:
throw fnd::Exception(kModuleName, "Unknown signature type");
}
if (mSignature.getSize() != sigSize)
throw fnd::Exception(kModuleName, "Signature size is incorrect");
// commit to binary
mBinaryBlob.alloc(totalSize);
if (mIsLittleEndian)
*(le_uint32_t*)(mBinaryBlob.getBytes()) = mSignType;
else
*(be_uint32_t*)(mBinaryBlob.getBytes()) = mSignType;
memcpy(mBinaryBlob.getBytes() + 4, mSignature.getBytes(), sigSize);
}
const byte_t* es::SignatureBlock::getBytes() const
{
return mBinaryBlob.getBytes();
}
size_t es::SignatureBlock::getSize() const
{
return mBinaryBlob.getSize();
return mRawBinary;
}
void es::SignatureBlock::clear()
{
mBinaryBlob.clear();
mRawBinary.clear();
mSignType = sign::SIGN_RSA4096_SHA1;
mIsLittleEndian = false;
mSignature.clear();
@ -171,27 +171,12 @@ void es::SignatureBlock::setLittleEndian(bool isLE)
mIsLittleEndian = isLE;
}
const fnd::MemoryBlob& es::SignatureBlock::getSignature() const
const fnd::Vec<byte_t>& es::SignatureBlock::getSignature() const
{
return mSignature;
}
void es::SignatureBlock::setSignature(const fnd::MemoryBlob& signature)
void es::SignatureBlock::setSignature(const fnd::Vec<byte_t>& signature)
{
mSignature = signature;
}
bool es::SignatureBlock::isEqual(const SignatureBlock& other) const
{
return (mSignType == other.mSignType) \
&& (mIsLittleEndian == other.mIsLittleEndian) \
&& (mSignature == other.mSignature);
}
void es::SignatureBlock::copyFrom(const SignatureBlock& other)
{
mBinaryBlob = other.mBinaryBlob;
mSignType = other.mSignType;
mIsLittleEndian = other.mIsLittleEndian;
mSignature = other.mSignature;
}

View file

@ -9,43 +9,68 @@ es::TicketBody_V2::TicketBody_V2()
es::TicketBody_V2::TicketBody_V2(const TicketBody_V2 & other)
{
copyFrom(other);
}
es::TicketBody_V2::TicketBody_V2(const byte_t * bytes, size_t len)
{
importBinary(bytes, len);
*this = other;
}
bool es::TicketBody_V2::operator==(const TicketBody_V2 & other) const
{
return isEqual(other);
return (mIssuer == other.mIssuer) \
&& (memcmp(mEncTitleKey, other.mEncTitleKey, ticket::kEncTitleKeySize) == 0) \
&& (mEncType == other.mEncType) \
&& (mTicketVersion == other.mTicketVersion) \
&& (mLicenseType == other.mLicenseType) \
&& (mPreInstall == other.mPreInstall) \
&& (mSharedTitle == other.mSharedTitle) \
&& (mAllowAllContent == other.mAllowAllContent) \
&& (memcmp(mReservedRegion, other.mReservedRegion, ticket::kReservedRegionSize) == 0) \
&& (mTicketId == other.mTicketId) \
&& (mDeviceId == other.mDeviceId) \
&& (memcmp(mRightsId, other.mRightsId, ticket::kRightsIdSize) == 0) \
&& (mAccountId == other.mAccountId) \
&& (mSectTotalSize == other.mSectTotalSize) \
&& (mSectHeaderOffset == other.mSectHeaderOffset) \
&& (mSectNum == other.mSectNum) \
&& (mSectEntrySize == other.mSectEntrySize);
}
bool es::TicketBody_V2::operator!=(const TicketBody_V2 & other) const
{
return !isEqual(other);
return !(*this == other);
}
void es::TicketBody_V2::operator=(const TicketBody_V2 & other)
{
copyFrom(other);
if (other.getBytes().size())
{
fromBytes(other.getBytes().data(), other.getBytes().size());
}
else
{
clear();
mIssuer = other.mIssuer;
memcpy(mEncTitleKey, other.mEncTitleKey, ticket::kEncTitleKeySize);
mEncType = other.mEncType;
mTicketVersion = other.mTicketVersion;
mLicenseType = other.mLicenseType;
mPreInstall = other.mPreInstall;
mSharedTitle = other.mSharedTitle;
mAllowAllContent = other.mAllowAllContent;
memcpy(mReservedRegion, other.mReservedRegion, ticket::kReservedRegionSize);
mTicketId = other.mTicketId;
mDeviceId = other.mDeviceId;
memcpy(mRightsId, other.mRightsId, ticket::kRightsIdSize);
mAccountId = other.mAccountId;
mSectTotalSize = other.mSectTotalSize;
mSectHeaderOffset = other.mSectHeaderOffset;
mSectNum = other.mSectNum;
mSectEntrySize = other.mSectEntrySize;
}
}
const byte_t * es::TicketBody_V2::getBytes() const
void es::TicketBody_V2::toBytes()
{
return mBinaryBlob.getBytes();
}
size_t es::TicketBody_V2::getSize() const
{
return mBinaryBlob.getSize();
}
void es::TicketBody_V2::exportBinary()
{
mBinaryBlob.alloc(sizeof(sTicketBody_v2));
sTicketBody_v2* body = (sTicketBody_v2*)mBinaryBlob.getBytes();
mRawBinary.alloc(sizeof(sTicketBody_v2));
sTicketBody_v2* body = (sTicketBody_v2*)mRawBinary.data();
body->format_version = (ticket::kFormatVersion);
@ -69,7 +94,7 @@ void es::TicketBody_V2::exportBinary()
body->sect_entry_size = (mSectEntrySize);
}
void es::TicketBody_V2::importBinary(const byte_t * bytes, size_t len)
void es::TicketBody_V2::fromBytes(const byte_t * bytes, size_t len)
{
if (len < sizeof(sTicketBody_v2))
{
@ -78,9 +103,9 @@ void es::TicketBody_V2::importBinary(const byte_t * bytes, size_t len)
clear();
mBinaryBlob.alloc(sizeof(sTicketBody_v2));
memcpy(mBinaryBlob.getBytes(), bytes, mBinaryBlob.getSize());
sTicketBody_v2* body = (sTicketBody_v2*)mBinaryBlob.getBytes();
mRawBinary.alloc(sizeof(sTicketBody_v2));
memcpy(mRawBinary.data(), bytes, mRawBinary.size());
sTicketBody_v2* body = (sTicketBody_v2*)mRawBinary.data();
if (body->format_version != ticket::kFormatVersion)
{
@ -106,9 +131,14 @@ void es::TicketBody_V2::importBinary(const byte_t * bytes, size_t len)
mSectEntrySize = body->sect_entry_size.get();
}
const fnd::Vec<byte_t>& es::TicketBody_V2::getBytes() const
{
return mRawBinary;
}
void es::TicketBody_V2::clear()
{
mBinaryBlob.clear();
mRawBinary.clear();
mIssuer.clear();
memset(mEncTitleKey, 0, ticket::kEncTitleKeySize);
mEncType = ticket::AES128_CBC;
@ -314,53 +344,3 @@ void es::TicketBody_V2::setSectionEntrySize(uint16_t size)
{
mSectEntrySize = size;
}
bool es::TicketBody_V2::isEqual(const TicketBody_V2 & other) const
{
return (mIssuer == other.mIssuer) \
&& (memcmp(mEncTitleKey, other.mEncTitleKey, ticket::kEncTitleKeySize) == 0) \
&& (mEncType == other.mEncType) \
&& (mTicketVersion == other.mTicketVersion) \
&& (mLicenseType == other.mLicenseType) \
&& (mPreInstall == other.mPreInstall) \
&& (mSharedTitle == other.mSharedTitle) \
&& (mAllowAllContent == other.mAllowAllContent) \
&& (memcmp(mReservedRegion, other.mReservedRegion, ticket::kReservedRegionSize) == 0) \
&& (mTicketId == other.mTicketId) \
&& (mDeviceId == other.mDeviceId) \
&& (memcmp(mRightsId, other.mRightsId, ticket::kRightsIdSize) == 0) \
&& (mAccountId == other.mAccountId) \
&& (mSectTotalSize == other.mSectTotalSize) \
&& (mSectHeaderOffset == other.mSectHeaderOffset) \
&& (mSectNum == other.mSectNum) \
&& (mSectEntrySize == other.mSectEntrySize);
}
void es::TicketBody_V2::copyFrom(const TicketBody_V2 & other)
{
if (other.getSize())
{
importBinary(other.getBytes(), other.getSize());
}
else
{
clear();
mIssuer = other.mIssuer;
memcpy(mEncTitleKey, other.mEncTitleKey, ticket::kEncTitleKeySize);
mEncType = other.mEncType;
mTicketVersion = other.mTicketVersion;
mLicenseType = other.mLicenseType;
mPreInstall = other.mPreInstall;
mSharedTitle = other.mSharedTitle;
mAllowAllContent = other.mAllowAllContent;
memcpy(mReservedRegion, other.mReservedRegion, ticket::kReservedRegionSize);
mTicketId = other.mTicketId;
mDeviceId = other.mDeviceId;
memcpy(mRightsId, other.mRightsId, ticket::kRightsIdSize);
mAccountId = other.mAccountId;
mSectTotalSize = other.mSectTotalSize;
mSectHeaderOffset = other.mSectHeaderOffset;
mSectNum = other.mSectNum;
mSectEntrySize = other.mSectEntrySize;
}
}