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