keyple-card-calypso-crypto-legacysam-cpp-lib - 1.0.2
Component of the Keyple C++ middleware
LegacySamResourceProfileExtensionAdapter.cpp
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (c) 2025 Calypso Networks Association https://calypsonet.org/ *
3 * *
4 * See the NOTICE file(s) distributed with this work for additional *
5 * information regarding copyright ownership. *
6 * *
7 * This program and the accompanying materials are made available under the *
8 * terms of the Eclipse Public License 2.0 which is available at *
9 * http://www.eclipse.org/legal/epl-2.0 *
10 * *
11 * SPDX-License-Identifier: EPL-2.0 *
12 ******************************************************************************/
13
14#include "keyple/card/calypso/crypto/legacysam/LegacySamResourceProfileExtensionAdapter.hpp"
15
16#include <memory>
17#include <regex>
18#include <string>
19
20#include "keyple/core/util/cpp/exception/Exception.hpp"
21#include "keypop/reader/selection/CardSelectionResult.hpp"
22
23namespace keyple {
24namespace card {
25namespace calypso {
26namespace crypto {
27namespace legacysam {
28
29using keyple::core::util::cpp::exception::Exception;
30using keypop::reader::selection::CardSelectionResult;
31
32LegacySamResourceProfileExtensionAdapter::
33 LegacySamResourceProfileExtensionAdapter(
34 std::shared_ptr<LegacySamSelectionExtensionAdapter>
35 samSelectionExtension,
36 const std::string& powerOnDataRegex)
37: mLegacySamSelectionExtension(samSelectionExtension)
38, mPowerOnDataRegex(powerOnDataRegex)
39{
40}
41
42std::shared_ptr<SmartCard>
43LegacySamResourceProfileExtensionAdapter::matches(
44 std::shared_ptr<CardReader> reader,
45 std::shared_ptr<ReaderApiFactory> readerApiFactory)
46{
47 /* Is SAM inserted? */
48 if (!reader->isCardPresent()) {
49 return nullptr;
50 }
51
52 /* Init the SAM selector */
53 auto cardSelector = readerApiFactory->createBasicCardSelector();
54 if (!mPowerOnDataRegex.empty()) {
55 cardSelector->filterByPowerOnData(mPowerOnDataRegex);
56 }
57
58 /*
59 * Associate the provided reader to the prepared LegacySAM selection
60 * extension and prepare an additional "Get Challenge" command for network
61 * optimization.
62 */
63 mLegacySamSelectionExtension->setSamReader(reader);
64 mLegacySamSelectionExtension->prepareGetChallengeIfNeeded();
65
66 /* Prepare the SAM selection scenario */
67 auto samCardSelectionManager
68 = readerApiFactory->createCardSelectionManager();
69 samCardSelectionManager->prepareSelection(
70 cardSelector, mLegacySamSelectionExtension);
71
72 /* Process the SAM selection scenario */
73 std::shared_ptr<CardSelectionResult> samCardSelectionResult = nullptr;
74 try {
75 samCardSelectionResult
76 = samCardSelectionManager->processCardSelectionScenario(reader);
77
78 } catch (const Exception& e) {
79 mLogger->error("SAM selection failed [error=%]\n", e.getMessage());
80 }
81
82 return samCardSelectionResult ? samCardSelectionResult->getActiveSmartCard()
83 : nullptr;
84}
85
86std::shared_ptr<SmartCard>
87LegacySamResourceProfileExtensionAdapter::matches(
88 const std::shared_ptr<SmartCard> smartCard)
89{
90 auto legacySamAdapter
91 = std::dynamic_pointer_cast<LegacySamAdapter>(smartCard);
92 if (!legacySamAdapter) {
93 return nullptr;
94 }
95
96 if (!mPowerOnDataRegex.empty()) {
97 std::regex regex(mPowerOnDataRegex);
98 if (!std::regex_match(smartCard->getPowerOnData(), regex)) {
99 return nullptr;
100 }
101 }
102
103 return smartCard;
104}
105
106} /* namespace legacysam */
107} /* namespace crypto */
108} /* namespace calypso */
109} /* namespace card */
110} /* namespace keyple */