keyple-card-calypso-crypto-legacysam-cpp-lib - 1.0.2
Component of the Keyple C++ middleware
CommandGiveRandom.cpp
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (c) 2019 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/CommandGiveRandom.hpp"
15
16#include <map>
17#include <memory>
18#include <vector>
19
20#include "keyple/card/calypso/crypto/legacysam/IllegalParameterException.hpp"
21#include "keyple/core/util/ApduUtil.hpp"
22#include "keyple/core/util/cpp/exception/IllegalArgumentException.hpp"
23
24namespace keyple {
25namespace card {
26namespace calypso {
27namespace crypto {
28namespace legacysam {
29
30using keyple::core::util::ApduUtil;
31using keyple::core::util::cpp::exception::IllegalArgumentException;
32
33const std::map<int, const std::shared_ptr<StatusProperties>>
34 CommandGiveRandom::STATUS_TABLE = [] {
35 std::map<int, const std::shared_ptr<StatusProperties>> m(
36 Command::STATUS_TABLE);
37
38 m.insert(
39 {{0x6700,
40 std::make_shared<StatusProperties>(
41 "Incorrect Lc", typeid(IllegalParameterException))}});
42
43 return m;
44 }();
45
46CommandGiveRandom::CommandGiveRandom(
47 std::shared_ptr<DtoAdapters::CommandContextDto> context,
48 const std::vector<uint8_t>& random)
49/* The SAM answers with a status word only, hence no expected data length. */
50: Command(CommandRef::GIVE_RANDOM, 0, context)
51{
52 const uint8_t cla = context->getTargetSam()->getClassByte();
53 const uint8_t p1 = 0x00;
54 const uint8_t p2 = 0x00;
55
56 if (random.size() != 8) {
57 throw IllegalArgumentException(
58 "Random value is an incorrect length. Expected 8 bytes, got "
59 + std::to_string(random.size()));
60 }
61 setApduRequest(
62 std::make_shared<ApduRequestAdapter>(ApduUtil::build(
63 cla, getCommandRef().getInstructionByte(), p1, p2, random)));
64}
65
66void
67CommandGiveRandom::finalizeRequest()
68{
69 /* nothing to do */
70}
71
72bool
73CommandGiveRandom::isControlSamRequiredToFinalizeRequest() const
74{
75 return false;
76}
77
78void
79CommandGiveRandom::parseResponse(std::shared_ptr<ApduResponseApi> apduResponse)
80{
81 setResponseAndCheckStatus(apduResponse);
82}
83
84const std::map<int, const std::shared_ptr<StatusProperties>>&
85CommandGiveRandom::getStatusTable() const
86{
87 return STATUS_TABLE;
88}
89
90} /* namespace legacysam */
91} /* namespace crypto */
92} /* namespace calypso */
93} /* namespace card */
94} /* namespace keyple */