nstool/lib/libpki/source/CertificateBody.cpp

254 lines
7 KiB
C++
Raw Permalink Normal View History

2018-08-07 07:17:51 +00:00
#include <nn/pki/CertificateBody.h>
2018-06-22 13:57:55 +00:00
2018-08-07 07:17:51 +00:00
nn::pki::CertificateBody::CertificateBody()
2018-06-22 13:57:55 +00:00
{
clear();
}
2018-08-07 07:17:51 +00:00
nn::pki::CertificateBody::CertificateBody(const CertificateBody& other)
2018-06-22 13:57:55 +00:00
{
2018-06-24 04:46:11 +00:00
*this = other;
2018-06-22 13:57:55 +00:00
}
2018-08-07 07:17:51 +00:00
void nn::pki::CertificateBody::operator=(const CertificateBody& other)
2018-06-22 13:57:55 +00:00
{
2018-06-24 04:46:11 +00:00
mRawBinary = other.mRawBinary;
mIssuer = other.mIssuer;
mSubject = other.mSubject;
mCertId = other.mCertId;
mPublicKeyType = other.mPublicKeyType;
mRsa4096PublicKey = other.mRsa4096PublicKey;
mRsa2048PublicKey = other.mRsa2048PublicKey;
mEcdsa240PublicKey = other.mEcdsa240PublicKey;
2018-06-22 13:57:55 +00:00
}
2018-08-07 07:17:51 +00:00
bool nn::pki::CertificateBody::operator==(const CertificateBody& other) const
2018-06-22 13:57:55 +00:00
{
2018-06-24 04:46:11 +00:00
return (mIssuer == other.mIssuer) \
&& (mSubject == other.mSubject) \
&& (mCertId == other.mCertId) \
&& (mPublicKeyType == other.mPublicKeyType) \
&& (mRsa4096PublicKey == other.mRsa4096PublicKey) \
&& (mRsa2048PublicKey == other.mRsa2048PublicKey) \
&& (mEcdsa240PublicKey == other.mEcdsa240PublicKey);
2018-06-22 13:57:55 +00:00
}
2018-08-07 07:17:51 +00:00
bool nn::pki::CertificateBody::operator!=(const CertificateBody& other) const
2018-06-22 13:57:55 +00:00
{
return !(*this == other);
}
2018-08-07 07:17:51 +00:00
void nn::pki::CertificateBody::toBytes()
2018-06-24 04:46:11 +00:00
{
// 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;
}
}
2018-08-07 07:17:51 +00:00
void nn::pki::CertificateBody::fromBytes(const byte_t* src, size_t size)
2018-06-22 13:57:55 +00:00
{
clear();
// check minimum size
if (size < sizeof(sCertificateHeader))
{
throw fnd::Exception(kModuleName, "Certificate too small");
}
const sCertificateHeader* hdr = (const sCertificateHeader*)src;
// get public key size
size_t pubkeySize = 0;
switch (hdr->key_type.get())
{
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");
}
// check total size
if (size < (sizeof(sCertificateHeader) + pubkeySize))
{
throw fnd::Exception(kModuleName, "Certificate too small");
}
// save raw binary
2018-06-24 04:46:11 +00:00
mRawBinary.alloc((sizeof(sCertificateHeader) + pubkeySize));
memcpy(mRawBinary.data(), src, mRawBinary.size());
2018-06-22 13:57:55 +00:00
// save hdr variables
2018-06-24 04:46:11 +00:00
hdr = (const sCertificateHeader*)mRawBinary.data();
2018-06-22 13:57:55 +00:00
if (hdr->issuer[0] != 0)
mIssuer = std::string(hdr->issuer, _MIN(strlen(hdr->issuer), cert::kIssuerSize));
2018-06-22 13:57:55 +00:00
mPublicKeyType = (cert::PublicKeyType)hdr->key_type.get();
if (hdr->subject[0] != 0)
mSubject = std::string(hdr->subject, _MIN(strlen(hdr->subject), cert::kSubjectSize));
2018-06-22 13:57:55 +00:00
mCertId = hdr->cert_id.get();
// save public key
if (mPublicKeyType == cert::RSA4096)
{
2018-06-24 04:46:11 +00:00
const sRsa4096PublicKeyBlock* pubkey = (const sRsa4096PublicKeyBlock*)(mRawBinary.data() + sizeof(sCertificateHeader));
2018-06-22 13:57:55 +00:00
memcpy(mRsa4096PublicKey.modulus, pubkey->modulus, sizeof(mRsa4096PublicKey.modulus));
memcpy(mRsa4096PublicKey.public_exponent, pubkey->public_exponent, sizeof(mRsa4096PublicKey.public_exponent));
}
else if (mPublicKeyType == cert::RSA2048)
{
2018-06-24 04:46:11 +00:00
const sRsa2048PublicKeyBlock* pubkey = (const sRsa2048PublicKeyBlock*)(mRawBinary.data() + sizeof(sCertificateHeader));
2018-06-22 13:57:55 +00:00
memcpy(mRsa2048PublicKey.modulus, pubkey->modulus, sizeof(mRsa2048PublicKey.modulus));
memcpy(mRsa2048PublicKey.public_exponent, pubkey->public_exponent, sizeof(mRsa2048PublicKey.public_exponent));
}
else if (mPublicKeyType == cert::ECDSA240)
{
2018-06-24 04:46:11 +00:00
const sEcdsa240PublicKeyBlock* pubkey = (const sEcdsa240PublicKeyBlock*)(mRawBinary.data() + sizeof(sCertificateHeader));
2018-06-22 13:57:55 +00:00
mEcdsa240PublicKey = pubkey->public_key;
}
}
2018-08-07 07:17:51 +00:00
const fnd::Vec<byte_t>& nn::pki::CertificateBody::getBytes() const
2018-06-22 13:57:55 +00:00
{
2018-06-24 04:46:11 +00:00
return mRawBinary;
2018-06-22 13:57:55 +00:00
}
2018-08-07 07:17:51 +00:00
void nn::pki::CertificateBody::clear()
2018-06-22 13:57:55 +00:00
{
mIssuer.clear();
mSubject.clear();
mCertId = 0;
mPublicKeyType = cert::RSA2048;
memset(&mRsa4096PublicKey, 0, sizeof(fnd::rsa::sRsa4096Key));
memset(&mRsa2048PublicKey, 0, sizeof(fnd::rsa::sRsa2048Key));
memset(&mEcdsa240PublicKey, 0, sizeof(fnd::ecdsa::sEcdsa240Point));
2018-06-22 13:57:55 +00:00
}
2018-08-07 07:17:51 +00:00
const std::string& nn::pki::CertificateBody::getIssuer() const
2018-06-22 13:57:55 +00:00
{
return mIssuer;
}
2018-08-07 07:17:51 +00:00
void nn::pki::CertificateBody::setIssuer(const std::string& issuer)
2018-06-22 13:57:55 +00:00
{
if (issuer.size() > cert::kIssuerSize)
{
throw fnd::Exception(kModuleName, "Issuer name too long");
}
mIssuer = issuer;
}
2018-08-07 07:17:51 +00:00
nn::pki::cert::PublicKeyType nn::pki::CertificateBody::getPublicKeyType() const
2018-06-22 13:57:55 +00:00
{
return mPublicKeyType;
}
2018-08-07 07:17:51 +00:00
void nn::pki::CertificateBody::setPublicKeyType(cert::PublicKeyType type)
2018-06-22 13:57:55 +00:00
{
mPublicKeyType = type;
}
2018-08-07 07:17:51 +00:00
const std::string& nn::pki::CertificateBody::getSubject() const
2018-06-22 13:57:55 +00:00
{
return mSubject;
}
2018-08-07 07:17:51 +00:00
void nn::pki::CertificateBody::setSubject(const std::string& subject)
2018-06-22 13:57:55 +00:00
{
if (subject.size() > cert::kSubjectSize)
{
throw fnd::Exception(kModuleName, "Subject name too long");
}
mSubject = subject;
}
2018-08-07 07:17:51 +00:00
uint32_t nn::pki::CertificateBody::getCertId() const
2018-06-22 13:57:55 +00:00
{
return mCertId;
}
2018-08-07 07:17:51 +00:00
void nn::pki::CertificateBody::setCertId(uint32_t id)
2018-06-22 13:57:55 +00:00
{
mCertId = id;
}
const fnd::rsa::sRsa4096Key& nn::pki::CertificateBody::getRsa4098PublicKey() const
2018-06-22 13:57:55 +00:00
{
return mRsa4096PublicKey;
}
void nn::pki::CertificateBody::setRsa4098PublicKey(const fnd::rsa::sRsa4096Key& key)
2018-06-22 13:57:55 +00:00
{
mRsa4096PublicKey = key;
}
const fnd::rsa::sRsa2048Key& nn::pki::CertificateBody::getRsa2048PublicKey() const
2018-06-22 13:57:55 +00:00
{
return mRsa2048PublicKey;
}
void nn::pki::CertificateBody::setRsa2048PublicKey(const fnd::rsa::sRsa2048Key& key)
2018-06-22 13:57:55 +00:00
{
mRsa2048PublicKey = key;
}
const fnd::ecdsa::sEcdsa240Point& nn::pki::CertificateBody::getEcdsa240PublicKey() const
2018-06-22 13:57:55 +00:00
{
return mEcdsa240PublicKey;
}
void nn::pki::CertificateBody::setEcdsa240PublicKey(const fnd::ecdsa::sEcdsa240Point& key)
2018-06-22 13:57:55 +00:00
{
mEcdsa240PublicKey = key;
2018-06-24 04:46:11 +00:00
}