14#include "keyple/card/calypso/AsymmetricCryptoSecuritySettingAdapter.hpp"
21#include "keyple/core/util/HexUtil.hpp"
22#include "keyple/core/util/KeypleAssert.hpp"
23#include "keyple/core/util/cpp/exception/IllegalArgumentException.hpp"
24#include "keyple/core/util/cpp/exception/IllegalStateException.hpp"
25#include "keypop/calypso/card/transaction/CryptoException.hpp"
26#include "keypop/calypso/card/transaction/InvalidCertificateException.hpp"
27#include "keypop/calypso/crypto/asymmetric/AsymmetricCryptoException.hpp"
28#include "keypop/calypso/crypto/asymmetric/certificate/CertificateValidationException.hpp"
29#include "keypop/calypso/crypto/asymmetric/certificate/spi/CaCertificateSpi.hpp"
30#include "keypop/calypso/crypto/asymmetric/certificate/spi/PcaCertificateSpi.hpp"
36using keyple::core::util::Assert;
37using keyple::core::util::HexUtil;
38using keyple::core::util::cpp::exception::IllegalArgumentException;
39using keyple::core::util::cpp::exception::IllegalStateException;
40using keypop::calypso::card::transaction::CryptoException;
41using keypop::calypso::card::transaction::InvalidCertificateException;
42using keypop::calypso::crypto::asymmetric::AsymmetricCryptoException;
43using keypop::calypso::crypto::asymmetric::certificate::
44 CertificateValidationException;
45using keypop::calypso::crypto::asymmetric::certificate::spi::CaCertificateSpi;
46using keypop::calypso::crypto::asymmetric::certificate::spi::PcaCertificateSpi;
49 AsymmetricCryptoSecuritySettingAdapter::MSG_INVALID_CERTIFICATE
50 =
"Invalid certificate";
52 AsymmetricCryptoSecuritySettingAdapter ::MSG_FAILED_TO_CHECK_THE_CERTIFICATE
53 =
"Failed to check the certificate";
54const std::string AsymmetricCryptoSecuritySettingAdapter ::
55 MSG_A_CERTIFICATE_IS_ALREADY_REGISTERED_FOR_THE_PROVIDED_PUBLIC_KEY_REFERENCE
56 =
"A certificate is already registered for the provided public key "
58const std::string AsymmetricCryptoSecuritySettingAdapter ::
59 MSG_THE_ISSUER_CERTIFICATE_IS_NOT_REGISTERED
60 =
"The issuer certificate is not registered: ";
61const std::string AsymmetricCryptoSecuritySettingAdapter ::
62 MSG_A_PARSER_IS_ALREADY_REGISTERED_FOR_THE_CERTIFICATE_TYPE
63 =
"A parser is already registered for the certificate type ";
65AsymmetricCryptoSecuritySettingAdapter::AsymmetricCryptoSecuritySettingAdapter(
66 std::shared_ptr<AsymmetricCryptoCardTransactionManagerFactorySpi>
67 cryptoCardTransactionManagerFactorySpi)
68: mCryptoCardTransactionManagerFactorySpi(
69 cryptoCardTransactionManagerFactorySpi)
73std::shared_ptr<AsymmetricCryptoCardTransactionManagerFactorySpi>
74AsymmetricCryptoSecuritySettingAdapter ::
75 getCryptoCardTransactionManagerFactorySpi()
const
77 return mCryptoCardTransactionManagerFactorySpi;
80AsymmetricCryptoSecuritySetting&
81AsymmetricCryptoSecuritySettingAdapter::addPcaCertificate(
82 std::shared_ptr<PcaCertificate> pcaCertificate)
84 Assert::getInstance().notNull(pcaCertificate,
"pcaCertificate");
86 const auto pcaCertificateSpi(
87 std::dynamic_pointer_cast<PcaCertificateSpi>(pcaCertificate));
88 if (!pcaCertificateSpi) {
89 std::string certificateName =
"null";
90 if (pcaCertificate.get()) {
91 const auto& r = *pcaCertificate.get();
92 certificateName =
typeid(r).name();
95 throw IllegalArgumentException(
96 std::string(
"Cannot cast 'pcaCertificate' to PcaCertificateSpi.")
97 +
" Actual type: " + certificateName);
101 std::shared_ptr<CaCertificateContentSpi> certificateContent;
103 certificateContent = pcaCertificateSpi->checkCertificateAndGetContent();
105 }
catch (
const CertificateValidationException& e) {
106 throw InvalidCertificateException(MSG_INVALID_CERTIFICATE, e);
108 }
catch (
const AsymmetricCryptoException& e) {
109 throw CryptoException(MSG_FAILED_TO_CHECK_THE_CERTIFICATE, e);
113 const std::string pcaKeyRef(
114 HexUtil::toHex(certificateContent->getPublicKeyReference()));
115 if (mCaCertificates.count(pcaKeyRef)) {
116 throw IllegalStateException(
117 MSG_A_CERTIFICATE_IS_ALREADY_REGISTERED_FOR_THE_PROVIDED_PUBLIC_KEY_REFERENCE
120 mCaCertificates.insert({pcaKeyRef, certificateContent});
125AsymmetricCryptoSecuritySetting&
126AsymmetricCryptoSecuritySettingAdapter::addCaCertificate(
127 std::shared_ptr<CaCertificate> caCertificate)
129 Assert::getInstance().notNull(caCertificate,
"caCertificate");
131 const auto caCertificateSpi(
132 std::dynamic_pointer_cast<CaCertificateSpi>(caCertificate));
133 if (!caCertificateSpi) {
134 std::string certificateName =
"null";
135 if (caCertificateSpi.get()) {
136 const auto& r = *caCertificateSpi.get();
137 certificateName =
typeid(r).name();
140 throw IllegalArgumentException(
141 "Cannot cast 'caCertificate' to CaCertificateSpi. Actual type: "
146 const std::string issuerKeyRef
147 = HexUtil::toHex(caCertificateSpi->getIssuerPublicKeyReference());
150 std::shared_ptr<CaCertificateContentSpi> issuerCertificateContent
151 = mCaCertificates.count(issuerKeyRef) ? mCaCertificates[issuerKeyRef]
153 if (issuerCertificateContent ==
nullptr) {
154 throw IllegalStateException(
155 MSG_THE_ISSUER_CERTIFICATE_IS_NOT_REGISTERED + issuerKeyRef);
159 std::shared_ptr<CaCertificateContentSpi> caCertificateContent;
161 caCertificateContent = caCertificateSpi->checkCertificateAndGetContent(
162 issuerCertificateContent);
164 }
catch (
const CertificateValidationException& e) {
165 throw InvalidCertificateException(MSG_INVALID_CERTIFICATE, e);
167 }
catch (
const AsymmetricCryptoException& e) {
168 throw CryptoException(MSG_FAILED_TO_CHECK_THE_CERTIFICATE, e);
172 const std::string caKeyRef
173 = HexUtil::toHex(caCertificateContent->getPublicKeyReference());
174 if (mCaCertificates.count(caKeyRef)) {
175 throw IllegalStateException(
176 MSG_A_CERTIFICATE_IS_ALREADY_REGISTERED_FOR_THE_PROVIDED_PUBLIC_KEY_REFERENCE
180 mCaCertificates.insert({caKeyRef, caCertificateContent});
185AsymmetricCryptoSecuritySetting&
186AsymmetricCryptoSecuritySettingAdapter::addCaCertificateParser(
187 std::shared_ptr<CaCertificateParser> caCertificateParser)
189 Assert::getInstance().notNull(caCertificateParser,
"caCertificateParser");
191 auto caCertificateParserSpi(
192 std::dynamic_pointer_cast<CaCertificateParserSpi>(caCertificateParser));
193 if (!caCertificateParserSpi) {
194 std::string certificateName =
"null";
195 if (caCertificateParserSpi.get()) {
196 const auto& r = *caCertificateParserSpi.get();
197 certificateName =
typeid(r).name();
200 throw IllegalArgumentException(
201 std::string(
"Cannot cast 'caCertificateParser' to ")
202 +
"CaCertificateParserSpi. Actual type: " + certificateName);
206 const uint8_t certificateType
207 = caCertificateParserSpi->getCertificateType();
208 if (mCaCertificateParsers.count(certificateType)) {
209 throw IllegalStateException(
210 MSG_A_PARSER_IS_ALREADY_REGISTERED_FOR_THE_CERTIFICATE_TYPE
211 + HexUtil::toHex(certificateType));
214 mCaCertificateParsers.insert({certificateType, caCertificateParserSpi});
219AsymmetricCryptoSecuritySetting&
220AsymmetricCryptoSecuritySettingAdapter::addCardCertificateParser(
221 std::shared_ptr<CardCertificateParser> cardCertificateParser)
223 Assert::getInstance().notNull(
224 cardCertificateParser,
"cardCertificateParser");
226 auto cardCertificateParserSpi(
227 std::dynamic_pointer_cast<CardCertificateParserSpi>(
228 cardCertificateParser));
229 if (!cardCertificateParserSpi) {
230 std::string certificateName =
"null";
231 if (cardCertificateParserSpi.get()) {
232 const auto& r = *cardCertificateParserSpi.get();
233 certificateName =
typeid(r).name();
236 throw IllegalArgumentException(
237 std::string(
"Cannot cast 'cardCertificateParser' to ")
238 +
"CardCertificateParserSpi. Actual type: " + certificateName);
242 const std::uint8_t certificateType
243 = cardCertificateParserSpi->getCertificateType();
244 if (mCardCertificateParsers.count(certificateType)) {
245 throw IllegalStateException(
246 MSG_A_PARSER_IS_ALREADY_REGISTERED_FOR_THE_CERTIFICATE_TYPE
247 + HexUtil::toHex(certificateType));
250 mCardCertificateParsers.insert({certificateType, cardCertificateParserSpi});
255std::shared_ptr<CaCertificateContentSpi>
256AsymmetricCryptoSecuritySettingAdapter::getCaCertificate(
257 const std::vector<std::uint8_t>& publicKeyReference)
259 const std::string ref = HexUtil::toHex(publicKeyReference);
260 return mCaCertificates.count(ref) ? mCaCertificates[ref] :
nullptr;
263std::shared_ptr<CaCertificateParserSpi>
264AsymmetricCryptoSecuritySettingAdapter::getCaCertificateParser(
265 std::uint8_t certificateType)
267 return mCaCertificateParsers.count(certificateType)
268 ? mCaCertificateParsers[certificateType]
272std::shared_ptr<CardCertificateParserSpi>
273AsymmetricCryptoSecuritySettingAdapter::getCardCertificateParser(
274 std::uint8_t certificateType)
276 return mCardCertificateParsers.count(certificateType)
277 ? mCardCertificateParsers[certificateType]