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

View file

@ -1,30 +1,25 @@
#pragma once #pragma once
#include <string> #include <string>
#include <fnd/MemoryBlob.h> #include <fnd/ISerialisable.h>
#include <fnd/ISerialiseableBinary.h>
#include <es/ticket.h> #include <es/ticket.h>
namespace es namespace es
{ {
class SectionHeader_V2 : class SectionHeader_V2 :
public fnd::ISerialiseableBinary public fnd::ISerialisable
{ {
public: public:
SectionHeader_V2(); SectionHeader_V2();
SectionHeader_V2(const SectionHeader_V2& other); 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;
bool operator!=(const SectionHeader_V2& other) const; bool operator!=(const SectionHeader_V2& other) const;
void operator=(const SectionHeader_V2& other); void operator=(const SectionHeader_V2& other);
// to be used after export
const byte_t* getBytes() const;
size_t getSize() const;
// export/import binary // export/import binary
virtual void exportBinary(); void toBytes();
virtual void importBinary(const byte_t* bytes, size_t len); void fromBytes(const byte_t* data, size_t len);
const fnd::Vec<byte_t>& getBytes() const;
// variables // variables
virtual void clear(); virtual void clear();
@ -48,7 +43,7 @@ namespace es
const std::string kModuleName = "SECTION_HEADER_V2"; const std::string kModuleName = "SECTION_HEADER_V2";
// raw binary // raw binary
fnd::MemoryBlob mBinaryBlob; fnd::Vec<byte_t> mRawBinary;
// variables // variables
uint32_t mSectionOffset; uint32_t mSectionOffset;
@ -56,11 +51,6 @@ namespace es
uint32_t mSectionSize; uint32_t mSectionSize;
uint16_t mRecordNum; uint16_t mRecordNum;
ticket::SectionType mSectionType; 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 #pragma once
#include <string> #include <string>
#include <fnd/MemoryBlob.h> #include <fnd/ISerialisable.h>
#include <fnd/ISerialiseableBinary.h>
#include <es/sign.h> #include <es/sign.h>
namespace es namespace es
{ {
class SignatureBlock class SignatureBlock
: public fnd::ISerialiseableBinary : public fnd::ISerialisable
{ {
public: public:
SignatureBlock(); SignatureBlock();
@ -17,12 +16,12 @@ namespace es
bool operator==(const SignatureBlock& other) const; bool operator==(const SignatureBlock& other) const;
bool operator!=(const SignatureBlock& other) const; bool operator!=(const SignatureBlock& other) const;
void importBinary(const byte_t* src, size_t size); // export/import binary
void exportBinary(); void toBytes();
void fromBytes(const byte_t* src, size_t size);
const byte_t* getBytes() const; const const fnd::Vec<byte_t>& getBytes() const;
size_t getSize() const;
// variables
void clear(); void clear();
es::sign::SignType getSignType() const; es::sign::SignType getSignType() const;
@ -31,23 +30,19 @@ namespace es
bool isLittleEndian() const; bool isLittleEndian() const;
void setLittleEndian(bool isLE); void setLittleEndian(bool isLE);
const fnd::MemoryBlob& getSignature() const; const fnd::Vec<byte_t>& getSignature() const;
void setSignature(const fnd::MemoryBlob& signature); void setSignature(const fnd::Vec<byte_t>& signature);
private: private:
const std::string kModuleName = "SIGNATURE_BLOCK"; const std::string kModuleName = "SIGNATURE_BLOCK";
// raw binary // raw binary
fnd::MemoryBlob mBinaryBlob; fnd::Vec<byte_t> mRawBinary;
// variables // variables
es::sign::SignType mSignType; es::sign::SignType mSignType;
bool mIsLittleEndian; bool mIsLittleEndian;
fnd::MemoryBlob mSignature; fnd::Vec<byte_t> mSignature;
// helpers
bool isEqual(const SignatureBlock& other) const;
void copyFrom(const SignatureBlock& other);
}; };
} }

View file

@ -1,113 +1,137 @@
#pragma once #pragma once
#include <string> #include <string>
#include <fnd/MemoryBlob.h> #include <fnd/ISerialisable.h>
#include <fnd/ISerialiseableBinary.h>
#include <es/SignatureBlock.h> #include <es/SignatureBlock.h>
namespace es namespace es
{ {
template <class T> template <class T>
class SignedData class SignedData
: public fnd::ISerialiseableBinary : public fnd::ISerialiseable
{ {
public: public:
SignedData() SignedData();
{ SignedData(const SignedData& other);
clear();
}
SignedData(const SignedData& other)
{
copyFrom(other);
}
void operator=(const SignedData& other) void operator=(const SignedData& other);
{ bool operator==(const SignedData& other) const;
copyFrom(other); bool operator!=(const SignedData& other) const;
}
bool operator==(const SignedData& other) const
{
return isEqual(other);
}
bool operator!=(const SignedData& other) const
{
return !(*this == other);
}
void importBinary(const byte_t* src, size_t size) // export/import
{ const void toBytes();
mSignature.importBinary(src, size); void fromBytes(const byte_t* src, size_t size);
mBody.importBinary(src + mSignature.getSize(), size - mSignature.getSize()); const fnd::Vec<byte_t>& getBytes() const;
mBinaryBlob.alloc(mSignature.getSize() + mBody.getSize()); // variables
memcpy(mBinaryBlob.getBytes(), src, mBinaryBlob.getSize()); void clear();
}
void exportBinary() const es::SignatureBlock& getSignature() const;
{ void setSignature(const SignatureBlock& signature);
mSignature.exportBinary();
mBody.exportBinary();
mBinaryBlob.alloc(mSignature.getSize() + mBody.getSize()); const T& getBody() const;
void setBody(const T& body);
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;
}
private: private:
const std::string kModuleName = "SIGNED_DATA"; const std::string kModuleName = "SIGNED_DATA";
// raw binary // raw binary
fnd::MemoryBlob mBinaryBlob; fnd::Vec<byte_t> mRawBinary;
// variables // variables
SignatureBlock mSignature; SignatureBlock mSignature;
T mBody; T mBody;
// helpers
bool isEqual(const SignedData& other) const
{
return (mSignature == other.mSignature) \
&& (mBody == other.mBody);
}
void copyFrom(const SignedData& other)
{
mBinaryBlob = other.mBinaryBlob;
mSignature = other.mSignature;
mBody = other.mBody;
}
}; };
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);
}
template <class T>
inline bool SignedData::operator!=(const SignedData& other) const
{
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 #pragma once
#include <string> #include <string>
#include <fnd/MemoryBlob.h> #include <fnd/ISerialisable.h>
#include <fnd/ISerialiseableBinary.h>
#include <es/ticket.h> #include <es/ticket.h>
namespace es namespace es
{ {
class TicketBody_V2 : class TicketBody_V2 :
public fnd::ISerialiseableBinary public fnd::ISerialisable
{ {
public: public:
TicketBody_V2(); TicketBody_V2();
TicketBody_V2(const TicketBody_V2& other); 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;
bool operator!=(const TicketBody_V2& other) const; bool operator!=(const TicketBody_V2& other) const;
void operator=(const TicketBody_V2& other); void operator=(const TicketBody_V2& other);
// to be used after export
const byte_t* getBytes() const;
size_t getSize() const;
// export/import binary // export/import binary
virtual void exportBinary(); void toBytes();
virtual void importBinary(const byte_t* bytes, size_t len); void fromBytes(const byte_t* bytes, size_t len);
const fnd::Vec<byte_t>& getBytes() const;
// variables // variables
virtual void clear(); void clear();
const std::string& getIssuer() const; const std::string& getIssuer() const;
void setIssuer(const std::string& issuer); void setIssuer(const std::string& issuer);
@ -87,7 +82,7 @@ namespace es
const std::string kModuleName = "TICKET_BODY_V2"; const std::string kModuleName = "TICKET_BODY_V2";
// raw binary // raw binary
fnd::MemoryBlob mBinaryBlob; fnd::Vec<byte_t> mRawBinary;
// variables // variables
std::string mIssuer; std::string mIssuer;
@ -108,10 +103,6 @@ namespace es
uint32_t mSectHeaderOffset; uint32_t mSectHeaderOffset;
uint16_t mSectNum; uint16_t mSectNum;
uint16_t mSectEntrySize; 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) es::CertificateBody::CertificateBody(const CertificateBody& other)
{ {
copyFrom(other); *this = other;
} }
void es::CertificateBody::operator=(const CertificateBody& 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 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 bool es::CertificateBody::operator!=(const CertificateBody& other) const
@ -25,7 +38,55 @@ bool es::CertificateBody::operator!=(const CertificateBody& other) const
return !(*this == other); 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(); clear();
@ -61,11 +122,11 @@ void es::CertificateBody::importBinary(const byte_t* src, size_t size)
} }
// save raw binary // save raw binary
mBinaryBlob.alloc((sizeof(sCertificateHeader) + pubkeySize)); mRawBinary.alloc((sizeof(sCertificateHeader) + pubkeySize));
memcpy(mBinaryBlob.getBytes(), src, mBinaryBlob.getSize()); memcpy(mRawBinary.data(), src, mRawBinary.size());
// save hdr variables // save hdr variables
hdr = (const sCertificateHeader*)mBinaryBlob.getBytes(); hdr = (const sCertificateHeader*)mRawBinary.data();
if (hdr->issuer[0] != 0) if (hdr->issuer[0] != 0)
mIssuer = std::string(hdr->issuer, cert::kIssuerSize); 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 // save public key
if (mPublicKeyType == cert::RSA4096) 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.modulus, pubkey->modulus, sizeof(mRsa4096PublicKey.modulus));
memcpy(mRsa4096PublicKey.public_exponent, pubkey->public_exponent, sizeof(mRsa4096PublicKey.public_exponent)); memcpy(mRsa4096PublicKey.public_exponent, pubkey->public_exponent, sizeof(mRsa4096PublicKey.public_exponent));
} }
else if (mPublicKeyType == cert::RSA2048) 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.modulus, pubkey->modulus, sizeof(mRsa2048PublicKey.modulus));
memcpy(mRsa2048PublicKey.public_exponent, pubkey->public_exponent, sizeof(mRsa2048PublicKey.public_exponent)); memcpy(mRsa2048PublicKey.public_exponent, pubkey->public_exponent, sizeof(mRsa2048PublicKey.public_exponent));
} }
else if (mPublicKeyType == cert::ECDSA240) 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; mEcdsa240PublicKey = pubkey->public_key;
} }
} }
void es::CertificateBody::exportBinary() const fnd::Vec<byte_t>& es::CertificateBody::getBytes() const
{ {
// get public key size return mRawBinary;
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");
}
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() void es::CertificateBody::clear()
{ {
@ -243,27 +252,3 @@ void es::CertificateBody::setEcdsa240PublicKey(const crypto::ecdsa::sEcdsa240Poi
{ {
mEcdsa240PublicKey = key; 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) es::SectionHeader_V2::SectionHeader_V2(const SectionHeader_V2 & other)
{ {
copyFrom(other); *this = other;
}
es::SectionHeader_V2::SectionHeader_V2(const byte_t * bytes, size_t len)
{
importBinary(bytes, len);
} }
bool es::SectionHeader_V2::operator==(const SectionHeader_V2 & other) const 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) \ return (mSectionOffset == other.mSectionOffset) \
&& (mRecordSize == other.mRecordSize) \ && (mRecordSize == other.mRecordSize) \
@ -81,15 +19,20 @@ bool es::SectionHeader_V2::isEqual(const SectionHeader_V2 & other) const
&& (mSectionType == other.mSectionType); && (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)
{
if (other.getBytes().size())
{ {
importBinary(other.getBytes(), other.getSize()); fromBytes(other.getBytes().data(), other.getBytes().size());
} }
else else
{ {
mBinaryBlob.clear(); mRawBinary.clear();
mSectionOffset = other.mSectionOffset; mSectionOffset = other.mSectionOffset;
mRecordSize = other.mRecordSize; mRecordSize = other.mRecordSize;
mSectionSize = other.mSectionSize; 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() void es::SectionHeader_V2::clear()
{ {
mBinaryBlob.clear(); mRawBinary.clear();
mSectionOffset = 0; mSectionOffset = 0;
mRecordSize = 0; mRecordSize = 0;
mSectionSize = 0; mSectionSize = 0;

View file

@ -7,17 +7,22 @@ es::SignatureBlock::SignatureBlock()
es::SignatureBlock::SignatureBlock(const SignatureBlock& other) es::SignatureBlock::SignatureBlock(const SignatureBlock& other)
{ {
copyFrom(other); *this = other;
} }
void es::SignatureBlock::operator=(const SignatureBlock& 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 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 bool es::SignatureBlock::operator!=(const SignatureBlock& other) const
@ -25,7 +30,45 @@ bool es::SignatureBlock::operator!=(const SignatureBlock& other) const
return !(*this == other); 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(); clear();
@ -87,65 +130,22 @@ void es::SignatureBlock::importBinary(const byte_t* src, size_t size)
throw fnd::Exception(kModuleName, "Certificate too small"); throw fnd::Exception(kModuleName, "Certificate too small");
} }
mBinaryBlob.alloc(totalSize); mRawBinary.alloc(totalSize);
memcpy(mBinaryBlob.getBytes(), src, totalSize); memcpy(mRawBinary.data(), src, totalSize);
mSignType = (sign::SignType)signType; mSignType = (sign::SignType)signType;
mSignature.alloc(sigSize); 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; return mRawBinary;
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();
} }
void es::SignatureBlock::clear() void es::SignatureBlock::clear()
{ {
mBinaryBlob.clear(); mRawBinary.clear();
mSignType = sign::SIGN_RSA4096_SHA1; mSignType = sign::SIGN_RSA4096_SHA1;
mIsLittleEndian = false; mIsLittleEndian = false;
mSignature.clear(); mSignature.clear();
@ -171,27 +171,12 @@ void es::SignatureBlock::setLittleEndian(bool isLE)
mIsLittleEndian = isLE; mIsLittleEndian = isLE;
} }
const fnd::MemoryBlob& es::SignatureBlock::getSignature() const const fnd::Vec<byte_t>& es::SignatureBlock::getSignature() const
{ {
return mSignature; return mSignature;
} }
void es::SignatureBlock::setSignature(const fnd::MemoryBlob& signature) void es::SignatureBlock::setSignature(const fnd::Vec<byte_t>& signature)
{ {
mSignature = 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) es::TicketBody_V2::TicketBody_V2(const TicketBody_V2 & other)
{ {
copyFrom(other); *this = other;
}
es::TicketBody_V2::TicketBody_V2(const byte_t * bytes, size_t len)
{
importBinary(bytes, len);
} }
bool es::TicketBody_V2::operator==(const TicketBody_V2 & other) const 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 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) 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(); mRawBinary.alloc(sizeof(sTicketBody_v2));
} sTicketBody_v2* body = (sTicketBody_v2*)mRawBinary.data();
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();
body->format_version = (ticket::kFormatVersion); body->format_version = (ticket::kFormatVersion);
@ -69,7 +94,7 @@ void es::TicketBody_V2::exportBinary()
body->sect_entry_size = (mSectEntrySize); 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)) if (len < sizeof(sTicketBody_v2))
{ {
@ -78,9 +103,9 @@ void es::TicketBody_V2::importBinary(const byte_t * bytes, size_t len)
clear(); clear();
mBinaryBlob.alloc(sizeof(sTicketBody_v2)); mRawBinary.alloc(sizeof(sTicketBody_v2));
memcpy(mBinaryBlob.getBytes(), bytes, mBinaryBlob.getSize()); memcpy(mRawBinary.data(), bytes, mRawBinary.size());
sTicketBody_v2* body = (sTicketBody_v2*)mBinaryBlob.getBytes(); sTicketBody_v2* body = (sTicketBody_v2*)mRawBinary.data();
if (body->format_version != ticket::kFormatVersion) 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(); mSectEntrySize = body->sect_entry_size.get();
} }
const fnd::Vec<byte_t>& es::TicketBody_V2::getBytes() const
{
return mRawBinary;
}
void es::TicketBody_V2::clear() void es::TicketBody_V2::clear()
{ {
mBinaryBlob.clear(); mRawBinary.clear();
mIssuer.clear(); mIssuer.clear();
memset(mEncTitleKey, 0, ticket::kEncTitleKeySize); memset(mEncTitleKey, 0, ticket::kEncTitleKeySize);
mEncType = ticket::AES128_CBC; mEncType = ticket::AES128_CBC;
@ -314,53 +344,3 @@ void es::TicketBody_V2::setSectionEntrySize(uint16_t size)
{ {
mSectEntrySize = 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;
}
}