nstool/lib/libpki/source/SignatureBlock.cpp

183 lines
4.2 KiB
C++
Raw Normal View History

2018-08-07 07:17:51 +00:00
#include <nn/pki/SignatureBlock.h>
2018-06-22 12:54:35 +00:00
2018-08-07 07:17:51 +00:00
nn::pki::SignatureBlock::SignatureBlock()
2018-06-22 12:54:35 +00:00
{
clear();
}
2018-08-07 07:17:51 +00:00
nn::pki::SignatureBlock::SignatureBlock(const SignatureBlock& other)
2018-06-22 12:54:35 +00:00
{
2018-06-24 04:46:11 +00:00
*this = other;
2018-06-22 12:54:35 +00:00
}
2018-08-07 07:17:51 +00:00
void nn::pki::SignatureBlock::operator=(const SignatureBlock& other)
2018-06-22 12:54:35 +00:00
{
2018-06-24 04:46:11 +00:00
mRawBinary = other.mRawBinary;
mSignType = other.mSignType;
mIsLittleEndian = other.mIsLittleEndian;
mSignature = other.mSignature;
2018-06-22 12:54:35 +00:00
}
2018-08-07 07:17:51 +00:00
bool nn::pki::SignatureBlock::operator==(const SignatureBlock& other) const
2018-06-22 12:54:35 +00:00
{
2018-06-24 04:46:11 +00:00
return (mSignType == other.mSignType) \
&& (mIsLittleEndian == other.mIsLittleEndian) \
&& (mSignature == other.mSignature);
2018-06-22 12:54:35 +00:00
}
2018-08-07 07:17:51 +00:00
bool nn::pki::SignatureBlock::operator!=(const SignatureBlock& other) const
2018-06-22 12:54:35 +00:00
{
return !(*this == other);
}
2018-08-07 07:17:51 +00:00
void nn::pki::SignatureBlock::toBytes()
2018-06-24 04:46:11 +00:00
{
size_t totalSize = 0;
size_t sigSize = 0;
switch (mSignType)
{
2018-07-29 19:18:02 +00:00
case (sign::SIGN_ID_RSA4096_SHA1):
case (sign::SIGN_ID_RSA4096_SHA256):
2018-06-24 04:46:11 +00:00
totalSize = sizeof(sRsa4096SignBlock);
sigSize = fnd::rsa::kRsa4096Size;
2018-06-24 04:46:11 +00:00
break;
2018-07-29 19:18:02 +00:00
case (sign::SIGN_ID_RSA2048_SHA1):
case (sign::SIGN_ID_RSA2048_SHA256):
2018-06-24 04:46:11 +00:00
totalSize = sizeof(sRsa2048SignBlock);
sigSize = fnd::rsa::kRsa2048Size;
2018-06-24 04:46:11 +00:00
break;
2018-07-29 19:18:02 +00:00
case (sign::SIGN_ID_ECDSA240_SHA1):
case (sign::SIGN_ID_ECDSA240_SHA256):
2018-06-24 04:46:11 +00:00
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);
}
2018-08-07 07:17:51 +00:00
void nn::pki::SignatureBlock::fromBytes(const byte_t* src, size_t size)
2018-06-22 12:54:35 +00:00
{
clear();
size_t totalSize = 0;
size_t sigSize = 0;
uint32_t signType = 0;
// try Big Endian sign type
signType = ((be_uint32_t*)src)->get();
switch (signType)
{
2018-07-29 19:18:02 +00:00
case (sign::SIGN_ID_RSA4096_SHA1):
case (sign::SIGN_ID_RSA4096_SHA256):
2018-06-22 12:54:35 +00:00
totalSize = sizeof(sRsa4096SignBlock);
sigSize = fnd::rsa::kRsa4096Size;
2018-06-22 12:54:35 +00:00
break;
2018-07-29 19:18:02 +00:00
case (sign::SIGN_ID_RSA2048_SHA1):
case (sign::SIGN_ID_RSA2048_SHA256):
2018-06-22 12:54:35 +00:00
totalSize = sizeof(sRsa2048SignBlock);
sigSize = fnd::rsa::kRsa2048Size;
2018-06-22 12:54:35 +00:00
break;
2018-07-29 19:18:02 +00:00
case (sign::SIGN_ID_ECDSA240_SHA1):
case (sign::SIGN_ID_ECDSA240_SHA256):
2018-06-22 12:54:35 +00:00
totalSize = sizeof(sEcdsa240SignBlock);
sigSize = sign::kEcdsaSigSize;
break;
}
// try Big Endian sign type
if (totalSize == 0)
{
signType = ((le_uint32_t*)src)->get();
switch (signType)
{
2018-07-29 19:18:02 +00:00
case (sign::SIGN_ID_RSA4096_SHA1):
case (sign::SIGN_ID_RSA4096_SHA256):
2018-06-22 12:54:35 +00:00
totalSize = sizeof(sRsa4096SignBlock);
sigSize = fnd::rsa::kRsa4096Size;
2018-06-22 12:54:35 +00:00
break;
2018-07-29 19:18:02 +00:00
case (sign::SIGN_ID_RSA2048_SHA1):
case (sign::SIGN_ID_RSA2048_SHA256):
2018-06-22 12:54:35 +00:00
totalSize = sizeof(sRsa2048SignBlock);
sigSize = fnd::rsa::kRsa2048Size;
2018-06-22 12:54:35 +00:00
break;
2018-07-29 19:18:02 +00:00
case (sign::SIGN_ID_ECDSA240_SHA1):
case (sign::SIGN_ID_ECDSA240_SHA256):
2018-06-22 12:54:35 +00:00
totalSize = sizeof(sEcdsa240SignBlock);
sigSize = sign::kEcdsaSigSize;
break;
default:
throw fnd::Exception(kModuleName, "Unknown signature type");
}
mIsLittleEndian = true;
}
if (totalSize > size)
{
throw fnd::Exception(kModuleName, "Certificate too small");
}
2018-06-24 04:46:11 +00:00
mRawBinary.alloc(totalSize);
memcpy(mRawBinary.data(), src, totalSize);
2018-06-22 12:54:35 +00:00
2018-07-29 19:18:02 +00:00
mSignType = (sign::SignatureId)signType;
2018-06-22 12:54:35 +00:00
mSignature.alloc(sigSize);
2018-06-24 04:46:11 +00:00
memcpy(mSignature.data(), mRawBinary.data() + 4, sigSize);
2018-06-22 12:54:35 +00:00
}
2018-08-07 07:17:51 +00:00
const fnd::Vec<byte_t>& nn::pki::SignatureBlock::getBytes() const
2018-06-22 12:54:35 +00:00
{
2018-06-24 04:46:11 +00:00
return mRawBinary;
2018-06-22 12:54:35 +00:00
}
2018-08-07 07:17:51 +00:00
void nn::pki::SignatureBlock::clear()
2018-06-22 12:54:35 +00:00
{
2018-06-24 04:46:11 +00:00
mRawBinary.clear();
2018-07-29 19:18:02 +00:00
mSignType = sign::SIGN_ID_RSA4096_SHA1;
2018-06-22 12:54:35 +00:00
mIsLittleEndian = false;
mSignature.clear();
}
2018-08-07 07:17:51 +00:00
nn::pki::sign::SignatureId nn::pki::SignatureBlock::getSignType() const
2018-06-22 12:54:35 +00:00
{
return mSignType;
}
2018-08-07 07:17:51 +00:00
void nn::pki::SignatureBlock::setSignType(nn::pki::sign::SignatureId type)
2018-06-22 12:54:35 +00:00
{
mSignType = type;
}
2018-08-07 07:17:51 +00:00
bool nn::pki::SignatureBlock::isLittleEndian() const
2018-06-22 12:54:35 +00:00
{
return mIsLittleEndian;
}
2018-08-07 07:17:51 +00:00
void nn::pki::SignatureBlock::setLittleEndian(bool isLE)
2018-06-22 12:54:35 +00:00
{
mIsLittleEndian = isLE;
}
2018-08-07 07:17:51 +00:00
const fnd::Vec<byte_t>& nn::pki::SignatureBlock::getSignature() const
2018-06-22 12:54:35 +00:00
{
return mSignature;
}
2018-08-07 07:17:51 +00:00
void nn::pki::SignatureBlock::setSignature(const fnd::Vec<byte_t>& signature)
2018-06-22 12:54:35 +00:00
{
mSignature = signature;
}