keyple-card-calypso-crypto-legacysam-cpp-lib - 1.0.2
Component of the Keyple C++ middleware
LegacySamUtil.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/LegacySamUtil.hpp"
15
16#include <memory>
17#include <string>
18#include <vector>
19
20#include "keyple/core/util/ByteArrayUtil.hpp"
21#include "keyple/core/util/cpp/exception/IllegalArgumentException.hpp"
22
23namespace keyple {
24namespace card {
25namespace calypso {
26namespace crypto {
27namespace legacysam {
28
29using keyple::core::util::ByteArrayUtil;
30using keyple::core::util::cpp::exception::IllegalArgumentException;
31
32const std::string
33LegacySamUtil::buildPowerOnDataFilter(
34 const LegacySam::ProductType productType,
35 const std::string& serialNumberRegex)
36{
37 std::string snRegex;
38
39 /* Check if serialNumber is defined. */
40 if (serialNumberRegex.empty()) {
41 /* Match all serial numbers. */
42 snRegex = ".{8}";
43
44 } else {
45 /* Match the provided serial number (could be a regex substring). */
46 snRegex = serialNumberRegex;
47 }
48
49 /*
50 * build the final Atr regex according to the SAM subtype and serial number
51 * if any.
52 *
53 * The header is starting with 3B, its total length is 4 or 6 bytes (8 or 10
54 * hex digits)
55 */
56 std::string applicationTypeMask;
57
58 switch (productType) {
59 case LegacySam::ProductType::SAM_C1:
60 case LegacySam::ProductType::HSM_C1:
61 applicationTypeMask = "C1";
62 break;
63 case LegacySam::ProductType::SAM_S1DX:
64 applicationTypeMask = "D?";
65 break;
66 case LegacySam::ProductType::SAM_S1E1:
67 applicationTypeMask = "E1";
68 break;
69 case LegacySam::ProductType::NONE:
70 /* match any ATR */
71 return ".*";
72 default:
73 throw IllegalArgumentException(
74 "Unsupported ProductType: "
75 + std::to_string(static_cast<int>(productType)));
76 }
77
78 return "3B(.{6}|.{10})805A..80" + applicationTypeMask + ".{6}" + snRegex
79 + "829000";
80}
81
82uint32_t
83LegacySamUtil::convertDateToBcdLong(uint32_t year, uint32_t month, uint32_t day)
84{
85 uint32_t bcdYear = static_cast<uint32_t>(
86 (year / 1000) << 12 | (year / 100 % 10) << 8 | (year % 100 / 10) << 4
87 | (year % 10));
88 uint32_t bcdMonth = static_cast<uint32_t>((month / 10) << 4 | (month % 10));
89 uint32_t bcdDay = static_cast<uint32_t>((day / 10) << 4 | (day % 10));
90
91 return (bcdYear << 16) | (bcdMonth << 8) | bcdDay;
92}
93
94std::vector<uint8_t>
95LegacySamUtil::computeStaticModeChallenge(
96 std::shared_ptr<DtoAdapters::TargetSamContextDto> samContext,
97 SystemKeyType systemKeyType)
98{
99 // compute the challenge
100 std::vector<std::uint8_t> challenge(8);
101
102 if (!samContext->getSystemKeyTypeToCounterNumberMap().empty()) {
103 auto keyCounterNumberIt
104 = samContext->getSystemKeyTypeToCounterNumberMap().find(
105 systemKeyType);
106 if (keyCounterNumberIt
107 != samContext->getSystemKeyTypeToCounterNumberMap().end()) {
108 auto keyCounterNumber = keyCounterNumberIt->second;
109 auto counterValueIt
110 = samContext->getCounterNumberToCounterValueMap().find(
111 keyCounterNumber);
112 if (counterValueIt
113 == samContext->getCounterNumberToCounterValueMap().end()) {
114 auto counterValue = counterValueIt->second;
115 ByteArrayUtil::copyBytes(counterValue, challenge, 5, 3);
116
117 // increment counter
118 samContext
119 ->getCounterNumberToCounterValueMap()[keyCounterNumber]
120 = counterValue + 1;
121 }
122 }
123 }
124
125 return challenge;
126}
127
128} /* namespace legacysam */
129} /* namespace crypto */
130} /* namespace calypso */
131} /* namespace card */
132} /* namespace keyple */