keyple-card-calypso-crypto-legacysam-cpp-lib - 1.0.2
Component of the Keyple C++ middleware
CommandCardGenerateAsymmetricKeyPair.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/CommandCardGenerateAsymmetricKeyPair.hpp"
15
16#include <map>
17#include <memory>
18#include <vector>
19
20#include "keyple/card/calypso/crypto/legacysam/AccessForbiddenException.hpp"
21#include "keyple/card/calypso/crypto/legacysam/DataAccessException.hpp"
22#include "keyple/card/calypso/crypto/legacysam/DtoAdapters.hpp"
23#include "keyple/card/calypso/crypto/legacysam/IllegalParameterException.hpp"
24#include "keyple/card/calypso/crypto/legacysam/IncorrectInputDataException.hpp"
25#include "keyple/card/calypso/crypto/legacysam/LegacySamConstants.hpp"
26#include "keyple/core/util/ApduUtil.hpp"
27#include "keyple/core/util/HexUtil.hpp"
28#include "keyple/core/util/cpp/Arrays.hpp"
29
30namespace keyple {
31namespace card {
32namespace calypso {
33namespace crypto {
34namespace legacysam {
35
36using keyple::core::util::ApduUtil;
37using keyple::core::util::HexUtil;
38using keyple::core::util::cpp::Arrays;
39
40const std::map<int, const std::shared_ptr<StatusProperties>>
41 CommandCardGenerateAsymmetricKeyPair::STATUS_TABLE = [] {
42 std::map<int, const std::shared_ptr<StatusProperties>> m(
43 Command::STATUS_TABLE);
44
45 m.insert(
46 {{0x6700,
47 std::make_shared<StatusProperties>(
48 "Incorrect Lc", typeid(IllegalParameterException))},
49 {0x6985,
50 std::make_shared<StatusProperties>(
51 "Preconditions not satisfied",
52 typeid(AccessForbiddenException))},
53 {0x6A80,
54 std::make_shared<StatusProperties>(
55 "Incorrect value in the incoming data (OID unknown)",
56 typeid(IncorrectInputDataException))},
57 {0x6B00,
58 std::make_shared<StatusProperties>(
59 "Incorrect P1", typeid(IllegalParameterException))}});
60 return m;
61 }();
62
63CommandCardGenerateAsymmetricKeyPair::CommandCardGenerateAsymmetricKeyPair(
64 std::shared_ptr<DtoAdapters::CommandContextDto> context,
65 std::shared_ptr<KeyPairContainer> keyPairContainer)
66: Command(
67 CommandRef::CARD_GENERATE_ASYMMETRIC_KEY_PAIR,
68 LegacySamConstants::TagInfo::GENERATED_CARD_ECC_KEY_PAIR.getTotalLength(),
69 context)
70, mKeyPairContainer(
71 std::dynamic_pointer_cast<KeyPairContainerAdapter>(keyPairContainer))
72{
73 const uint8_t cla = context->getTargetSam()->getClassByte();
74 const uint8_t inst = getCommandRef().getInstructionByte();
75 const uint8_t p1 = 0x80;
76 const uint8_t p2 = 0x00;
77 const std::vector<uint8_t> oid
78 = core::util::HexUtil::toByteArray("06082A8648CE3D030107");
79
80 setApduRequest(
81 std::make_shared<ApduRequestAdapter>(
82 core::util::ApduUtil::build(cla, inst, p1, p2, oid, 0x63)));
83}
84
85void
86CommandCardGenerateAsymmetricKeyPair::finalizeRequest()
87{
88 /* Nothing to do */
89}
90
91bool
92CommandCardGenerateAsymmetricKeyPair::isControlSamRequiredToFinalizeRequest()
93 const
94{
95 return false;
96}
97
98void
99CommandCardGenerateAsymmetricKeyPair::parseResponse(
100 std::shared_ptr<ApduResponseApi> apduResponse)
101{
102 setResponseAndCheckStatus(apduResponse);
103
104 std::vector<uint8_t> dataOut = apduResponse->getDataOut();
105 if (!dataOut.empty()) {
106 /* Check BER-TLV header */
107 std::vector<uint8_t> header
108 = LegacySamConstants::TagInfo::GENERATED_CARD_ECC_KEY_PAIR
109 .getHeader();
110 for (size_t i = 0; i < header.size(); i++) {
111 if (dataOut[i] != header[i]) {
112 throw DataAccessException(
113 "Inconsistent BER-TLV tag. Expected: "
114 + HexUtil::toHex(header[i])
115 + "h, Actual: " + HexUtil::toHex(dataOut[i]) + "h");
116 }
117 }
118
119 mKeyPairContainer->setKeyPair(
120 Arrays::copyOfRange(dataOut, header.size(), dataOut.size()));
121 }
122}
123
124const std::map<int, const std::shared_ptr<StatusProperties>>&
125CommandCardGenerateAsymmetricKeyPair::getStatusTable() const
126{
127 return STATUS_TABLE;
128}
129
130} /* namespace legacysam */
131} /* namespace crypto */
132} /* namespace calypso */
133} /* namespace card */
134} /* namespace keyple */
DtoAdapters::KeyPairContainerAdapter KeyPairContainerAdapter