2018-07-29 12:27:08 +00:00
|
|
|
#include <iostream>
|
|
|
|
#include <iomanip>
|
2018-07-10 15:01:34 +00:00
|
|
|
#include <fnd/SimpleTextOutput.h>
|
2018-10-06 08:45:09 +00:00
|
|
|
#include <fnd/OffsetAdjustedIFile.h>
|
2018-08-07 07:17:51 +00:00
|
|
|
#include <nn/pki/SignUtils.h>
|
2018-08-06 09:11:15 +00:00
|
|
|
#include "PkiCertProcess.h"
|
2018-08-05 15:09:07 +00:00
|
|
|
#include "PkiValidator.h"
|
2018-07-10 15:01:34 +00:00
|
|
|
|
2018-08-06 09:11:15 +00:00
|
|
|
PkiCertProcess::PkiCertProcess() :
|
2018-09-23 03:29:22 +00:00
|
|
|
mFile(),
|
2018-07-10 15:01:34 +00:00
|
|
|
mCliOutputMode(_BIT(OUTPUT_BASIC)),
|
|
|
|
mVerify(false)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-08-06 09:11:15 +00:00
|
|
|
void PkiCertProcess::process()
|
2018-07-10 15:01:34 +00:00
|
|
|
{
|
|
|
|
importCerts();
|
2018-08-13 17:14:21 +00:00
|
|
|
|
2018-07-10 15:01:34 +00:00
|
|
|
if (mVerify)
|
|
|
|
validateCerts();
|
|
|
|
|
|
|
|
if (_HAS_BIT(mCliOutputMode, OUTPUT_BASIC))
|
|
|
|
displayCerts();
|
|
|
|
}
|
|
|
|
|
2018-09-23 03:29:22 +00:00
|
|
|
void PkiCertProcess::setInputFile(const fnd::SharedPtr<fnd::IFile>& file)
|
2018-07-10 15:01:34 +00:00
|
|
|
{
|
|
|
|
mFile = file;
|
|
|
|
}
|
|
|
|
|
2018-08-21 12:03:19 +00:00
|
|
|
void PkiCertProcess::setKeyCfg(const KeyConfiguration& keycfg)
|
2018-07-10 15:01:34 +00:00
|
|
|
{
|
2018-08-21 12:03:19 +00:00
|
|
|
mKeyCfg = keycfg;
|
2018-07-10 15:01:34 +00:00
|
|
|
}
|
|
|
|
|
2018-08-06 09:11:15 +00:00
|
|
|
void PkiCertProcess::setCliOutputMode(CliOutputMode mode)
|
2018-07-10 15:01:34 +00:00
|
|
|
{
|
|
|
|
mCliOutputMode = mode;
|
|
|
|
}
|
|
|
|
|
2018-08-06 09:11:15 +00:00
|
|
|
void PkiCertProcess::setVerifyMode(bool verify)
|
2018-07-10 15:01:34 +00:00
|
|
|
{
|
|
|
|
mVerify = verify;
|
|
|
|
}
|
|
|
|
|
2018-08-06 09:11:15 +00:00
|
|
|
void PkiCertProcess::importCerts()
|
2018-07-10 15:01:34 +00:00
|
|
|
{
|
|
|
|
fnd::Vec<byte_t> scratch;
|
|
|
|
|
2018-09-23 03:29:22 +00:00
|
|
|
if (*mFile == nullptr)
|
2018-08-13 17:14:21 +00:00
|
|
|
{
|
|
|
|
throw fnd::Exception(kModuleName, "No file reader set.");
|
|
|
|
}
|
|
|
|
|
2018-09-23 03:29:22 +00:00
|
|
|
scratch.alloc((*mFile)->size());
|
|
|
|
(*mFile)->read(scratch.data(), 0, scratch.size());
|
2018-07-10 15:01:34 +00:00
|
|
|
|
2018-08-07 08:13:18 +00:00
|
|
|
nn::pki::SignedData<nn::pki::CertificateBody> cert;
|
2018-07-10 15:01:34 +00:00
|
|
|
for (size_t f_pos = 0; f_pos < scratch.size(); f_pos += cert.getBytes().size())
|
|
|
|
{
|
|
|
|
cert.fromBytes(scratch.data() + f_pos, scratch.size() - f_pos);
|
|
|
|
mCert.addElement(cert);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-06 09:11:15 +00:00
|
|
|
void PkiCertProcess::validateCerts()
|
2018-07-10 15:01:34 +00:00
|
|
|
{
|
2018-08-05 15:09:07 +00:00
|
|
|
PkiValidator pki;
|
2018-07-29 19:18:02 +00:00
|
|
|
|
2018-08-05 15:09:07 +00:00
|
|
|
try
|
2018-07-10 15:01:34 +00:00
|
|
|
{
|
2018-08-21 12:03:19 +00:00
|
|
|
pki.setKeyCfg(mKeyCfg);
|
2018-08-05 15:09:07 +00:00
|
|
|
pki.addCertificates(mCert);
|
2018-07-10 15:01:34 +00:00
|
|
|
}
|
2018-08-05 15:09:07 +00:00
|
|
|
catch (const fnd::Exception& e)
|
2018-07-10 15:01:34 +00:00
|
|
|
{
|
2018-08-05 15:09:07 +00:00
|
|
|
std::cout << "[WARNING] " << e.error() << std::endl;
|
2018-07-10 15:01:34 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-06 09:11:15 +00:00
|
|
|
void PkiCertProcess::displayCerts()
|
2018-07-10 15:01:34 +00:00
|
|
|
{
|
|
|
|
for (size_t i = 0; i < mCert.size(); i++)
|
|
|
|
{
|
|
|
|
displayCert(mCert[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-07 08:13:18 +00:00
|
|
|
void PkiCertProcess::displayCert(const nn::pki::SignedData<nn::pki::CertificateBody>& cert)
|
2018-07-10 15:01:34 +00:00
|
|
|
{
|
2018-08-06 09:11:15 +00:00
|
|
|
std::cout << "[NNPKI Certificate]" << std::endl;
|
2018-07-29 12:27:08 +00:00
|
|
|
|
|
|
|
std::cout << " SignType " << getSignTypeStr(cert.getSignature().getSignType());
|
2018-07-10 15:01:34 +00:00
|
|
|
if (_HAS_BIT(mCliOutputMode, OUTPUT_EXTENDED))
|
2018-08-14 07:24:43 +00:00
|
|
|
std::cout << " (0x" << std::hex << cert.getSignature().getSignType() << ") (" << getEndiannessStr(cert.getSignature().isLittleEndian()) << ")";
|
2018-07-29 12:27:08 +00:00
|
|
|
std::cout << std::endl;
|
|
|
|
|
|
|
|
std::cout << " Issuer: " << cert.getBody().getIssuer() << std::endl;
|
|
|
|
std::cout << " Subject: " << cert.getBody().getSubject() << std::endl;
|
2018-07-29 12:28:01 +00:00
|
|
|
std::cout << " PublicKeyType: " << getPublicKeyTypeStr(cert.getBody().getPublicKeyType());
|
2018-07-10 15:01:34 +00:00
|
|
|
if (_HAS_BIT(mCliOutputMode, OUTPUT_EXTENDED))
|
2018-07-29 12:27:08 +00:00
|
|
|
std::cout << " (" << std::dec << cert.getBody().getPublicKeyType() << ")";
|
|
|
|
std::cout << std::endl;
|
|
|
|
std::cout << " CertID: 0x" << std::hex << cert.getBody().getCertId() << std::endl;
|
2018-07-10 15:01:34 +00:00
|
|
|
|
2018-08-07 08:13:18 +00:00
|
|
|
if (cert.getBody().getPublicKeyType() == nn::pki::cert::RSA4096)
|
2018-07-10 15:01:34 +00:00
|
|
|
{
|
2018-07-29 12:27:08 +00:00
|
|
|
std::cout << " PublicKey:" << std::endl;
|
|
|
|
std::cout << " Modulus:" << std::endl;
|
2018-08-07 08:35:03 +00:00
|
|
|
fnd::SimpleTextOutput::hexDump(cert.getBody().getRsa4098PublicKey().modulus, getHexDumpLen(fnd::rsa::kRsa4096Size), 0x10, 6);
|
2018-07-29 12:27:08 +00:00
|
|
|
std::cout << " Public Exponent:" << std::endl;
|
2018-08-07 08:35:03 +00:00
|
|
|
fnd::SimpleTextOutput::hexDump(cert.getBody().getRsa4098PublicKey().public_exponent, fnd::rsa::kRsaPublicExponentSize, 0x10, 6);
|
2018-07-10 15:01:34 +00:00
|
|
|
}
|
2018-08-07 08:13:18 +00:00
|
|
|
else if (cert.getBody().getPublicKeyType() == nn::pki::cert::RSA2048)
|
2018-07-10 15:01:34 +00:00
|
|
|
{
|
2018-07-29 12:27:08 +00:00
|
|
|
std::cout << " PublicKey:" << std::endl;
|
|
|
|
std::cout << " Modulus:" << std::endl;
|
2018-08-14 07:24:43 +00:00
|
|
|
fnd::SimpleTextOutput::hexDump(cert.getBody().getRsa2048PublicKey().modulus, getHexDumpLen(fnd::rsa::kRsa2048Size), 0x10, 6);
|
|
|
|
std::cout << " Public Exponent:" << std::endl;
|
2018-08-07 08:35:03 +00:00
|
|
|
fnd::SimpleTextOutput::hexDump(cert.getBody().getRsa2048PublicKey().public_exponent, fnd::rsa::kRsaPublicExponentSize, 0x10, 6);
|
2018-07-10 15:01:34 +00:00
|
|
|
}
|
2018-08-07 08:13:18 +00:00
|
|
|
else if (cert.getBody().getPublicKeyType() == nn::pki::cert::ECDSA240)
|
2018-07-10 15:01:34 +00:00
|
|
|
{
|
2018-07-29 12:27:08 +00:00
|
|
|
std::cout << " PublicKey:" << std::endl;
|
|
|
|
std::cout << " R:" << std::endl;
|
2018-08-07 08:35:03 +00:00
|
|
|
fnd::SimpleTextOutput::hexDump(cert.getBody().getEcdsa240PublicKey().r, getHexDumpLen(fnd::ecdsa::kEcdsa240Size), 0x10, 6);
|
2018-07-29 12:27:08 +00:00
|
|
|
std::cout << " S:" << std::endl;
|
2018-08-07 08:35:03 +00:00
|
|
|
fnd::SimpleTextOutput::hexDump(cert.getBody().getEcdsa240PublicKey().s, getHexDumpLen(fnd::ecdsa::kEcdsa240Size), 0x10, 6);
|
2018-07-10 15:01:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-06 09:11:15 +00:00
|
|
|
size_t PkiCertProcess::getHexDumpLen(size_t max_size) const
|
|
|
|
{
|
|
|
|
return _HAS_BIT(mCliOutputMode, OUTPUT_EXTENDED) ? max_size : kSmallHexDumpLen;
|
|
|
|
}
|
|
|
|
|
2018-08-07 08:13:18 +00:00
|
|
|
const char* PkiCertProcess::getSignTypeStr(nn::pki::sign::SignatureId type) const
|
2018-07-10 15:01:34 +00:00
|
|
|
{
|
|
|
|
const char* str;
|
|
|
|
switch (type)
|
|
|
|
{
|
2018-08-07 08:13:18 +00:00
|
|
|
case (nn::pki::sign::SIGN_ID_RSA4096_SHA1):
|
2018-07-10 15:01:34 +00:00
|
|
|
str = "RSA4096-SHA1";
|
|
|
|
break;
|
2018-08-07 08:13:18 +00:00
|
|
|
case (nn::pki::sign::SIGN_ID_RSA2048_SHA1):
|
2018-07-10 15:01:34 +00:00
|
|
|
str = "RSA2048-SHA1";
|
|
|
|
break;
|
2018-08-07 08:13:18 +00:00
|
|
|
case (nn::pki::sign::SIGN_ID_ECDSA240_SHA1):
|
2018-07-10 15:01:34 +00:00
|
|
|
str = "ECDSA240-SHA1";
|
|
|
|
break;
|
2018-08-07 08:13:18 +00:00
|
|
|
case (nn::pki::sign::SIGN_ID_RSA4096_SHA256):
|
2018-07-10 15:01:34 +00:00
|
|
|
str = "RSA4096-SHA256";
|
|
|
|
break;
|
2018-08-07 08:13:18 +00:00
|
|
|
case (nn::pki::sign::SIGN_ID_RSA2048_SHA256):
|
2018-07-10 15:01:34 +00:00
|
|
|
str = "RSA2048-SHA256";
|
|
|
|
break;
|
2018-08-07 08:13:18 +00:00
|
|
|
case (nn::pki::sign::SIGN_ID_ECDSA240_SHA256):
|
2018-07-10 15:01:34 +00:00
|
|
|
str = "ECDSA240-SHA256";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
str = "Unknown";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2018-08-06 09:11:15 +00:00
|
|
|
const char* PkiCertProcess::getEndiannessStr(bool isLittleEndian) const
|
2018-07-10 15:01:34 +00:00
|
|
|
{
|
|
|
|
return isLittleEndian ? "LittleEndian" : "BigEndian";
|
|
|
|
}
|
|
|
|
|
2018-08-07 08:13:18 +00:00
|
|
|
const char* PkiCertProcess::getPublicKeyTypeStr(nn::pki::cert::PublicKeyType type) const
|
2018-07-10 15:01:34 +00:00
|
|
|
{
|
|
|
|
const char* str;
|
|
|
|
switch (type)
|
|
|
|
{
|
2018-08-07 08:13:18 +00:00
|
|
|
case (nn::pki::cert::RSA4096):
|
2018-07-10 15:01:34 +00:00
|
|
|
str = "RSA4096";
|
|
|
|
break;
|
2018-08-07 08:13:18 +00:00
|
|
|
case (nn::pki::cert::RSA2048):
|
2018-07-10 15:01:34 +00:00
|
|
|
str = "RSA2048";
|
|
|
|
break;
|
2018-08-07 08:13:18 +00:00
|
|
|
case (nn::pki::cert::ECDSA240):
|
2018-07-10 15:01:34 +00:00
|
|
|
str = "ECDSA240";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
str = "Unknown";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return str;
|
|
|
|
}
|