Keyple Card Calypso C++ Library - 3.2.2
Component of the Keyple C++ middleware
CommandGetDataCertificate.cpp
Go to the documentation of this file.
1/**************************************************************************************************
2 * Copyright (c) 2023 Calypso Networks Association https://calypsonet.org/ *
3 * *
4 * See the NOTICE file(s) distributed with this work for additional information
5 * regarding * copyright ownership.
6 * *
7 * *
8 * This program and the accompanying materials are made available under the
9 * terms of the Eclipse *
10 * Public License 2.0 which is available at http://www.eclipse.org/legal/epl-2.0 *
11 * *
12 * SPDX-License-Identifier: EPL-2.0 *
13 **************************************************************************************************/
14
15#include "keyple/card/calypso/CommandGetDataCertificate.hpp"
16
17#include <map>
18#include <memory>
19#include <string>
20#include <vector>
21
22#include "keyple/card/calypso/CalypsoCardAdapter.hpp"
23#include "keyple/card/calypso/CalypsoCardConstant.hpp"
24#include "keyple/card/calypso/CardDataAccessException.hpp"
25#include "keyple/core/util/ApduUtil.hpp"
26#include "keyple/core/util/cpp/Arrays.hpp"
27#include "keyple/core/util/cpp/System.hpp"
28
29namespace keyple {
30namespace card {
31namespace calypso {
32
33using keyple::core::util::ApduUtil;
34using keyple::core::util::cpp::Arrays;
35using keyple::core::util::cpp::System;
36
37const std::map<int, const std::shared_ptr<Command::StatusProperties>>
38 CommandGetDataCertificate::STATUS_TABLE = [] {
39 std::map<int, const std::shared_ptr<Command::StatusProperties>> m(
40 Command::STATUS_TABLE);
41
42 m.insert(
43 {{0x6A88,
44 std::make_shared<StatusProperties>(
45 "Data object not found (optional mode not available)",
46 typeid(CardDataAccessException))},
47 {0x6B00,
48 std::make_shared<StatusProperties>(
49 "P1 or P2 value not supported",
50 typeid(CardDataAccessException))}});
51 return m;
52 }();
53
54CommandGetDataCertificate::CommandGetDataCertificate(
55 const std::shared_ptr<DtoAdapters::TransactionContextDto>&
56 transactionContext,
57 const std::shared_ptr<DtoAdapters::CommandContextDto>& commandContext,
58 bool isCardCertificate,
59 bool isFirstPart)
60: Command(CardCommandRef::GET_DATA, nullptr, transactionContext, commandContext)
61, mIsCardCertificate(isCardCertificate)
62, mIsFirstPart(isFirstPart)
63{
64 const std::uint8_t cardClass
65 = transactionContext->getCard() != nullptr
66 ? transactionContext->getCard()->getCardClass().getValue()
67 : CalypsoCardClass::ISO.getValue();
68
69 std::uint8_t p1;
70 std::uint8_t p2;
71
72 if (isCardCertificate) {
73 p1 = CalypsoCardConstant::TAG_CARD_CERTIFICATE_MSB;
74 p2 = CalypsoCardConstant::TAG_CARD_CERTIFICATE_LSB;
75 } else {
76 p1 = CalypsoCardConstant::TAG_CA_CERTIFICATE_MSB;
77 p2 = CalypsoCardConstant::TAG_CA_CERTIFICATE_LSB;
78 }
79
80 if (!isFirstPart) {
81 p2++;
82 }
83
84 /* APDU Case 2 - always outside secure session */
85 setApduRequest(
86 std::make_shared<DtoAdapters::ApduRequestAdapter>(ApduUtil::build(
87 cardClass, getCommandRef().getInstructionByte(), p1, p2)));
88
89 if (isCardCertificate) {
90 addSubName("CARD_CERTIFICATE");
91 } else {
92 addSubName("CA_CERTIFICATE");
93 }
94}
95
96void
97CommandGetDataCertificate::finalizeRequest()
98{
99 /* NOP */
100}
101
102bool
103CommandGetDataCertificate::isCryptoServiceRequiredToFinalizeRequest() const
104{
105 return false;
106}
107
108bool
109CommandGetDataCertificate::synchronizeCryptoServiceBeforeCardProcessing()
110{
111 return true;
112}
113
114void
115CommandGetDataCertificate::parseResponse(
116 std::shared_ptr<ApduResponseApi> apduResponse)
117{
118 Command::setApduResponseAndCheckStatus(apduResponse);
119
120 const std::vector<std::uint8_t> dataOut = apduResponse->getDataOut();
121 std::vector<std::uint8_t> certificateBytes;
122
123 if (mIsFirstPart) {
124 /*
125 * Extract the certificate bytes, skipping the 5-byte tag and length
126 * prefix.
127 */
128 certificateBytes = std::vector<std::uint8_t>(
129 dataOut.size() - CalypsoCardConstant::TAG_CERTIFICATE_HEADER_SIZE);
130 System::arraycopy(
131 dataOut,
132 CalypsoCardConstant::TAG_CERTIFICATE_HEADER_SIZE,
133 certificateBytes,
134 0,
135 dataOut.size() - CalypsoCardConstant::TAG_CERTIFICATE_HEADER_SIZE);
136 } else {
137 /*
138 * For subsequent parts, the entire dataOut is assumed to be the
139 * certificate data.
140 */
141 certificateBytes = dataOut;
142 }
143
144 if (mIsCardCertificate) {
145 getTransactionContext()->getCard()->addCardCertificateBytes(
146 certificateBytes, mIsFirstPart);
147 } else {
148 getTransactionContext()->getCard()->addCaCertificateBytes(
149 certificateBytes, mIsFirstPart);
150 }
151}
152
153const std::map<int, const std::shared_ptr<Command::StatusProperties>>&
154CommandGetDataCertificate::getStatusTable() const
155{
156 return STATUS_TABLE;
157}
158
159} /* namespace calypso */
160} /* namespace card */
161} /* namespace keyple */