1 /*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #ifndef ESED_PN81A_UTILS_H_
18 #define ESED_PN81A_UTILS_H_
19
20 #include <functional>
21 #include <string>
22
23 #include <hidl/Status.h>
24
25 #include "apdu.h"
26 #include "pn81a.h"
27
28 namespace android {
29 namespace esed {
30 namespace pn81a {
31
32 // HIDL
33 using ::android::hardware::Status;
34
35
36 // libapdu
37 using ::android::CommandApdu;
38 using ResponseApdu = ::android::ResponseApdu<const std::vector<uint8_t>>;
39
40 /**
41 * Reads a 32-bit integer from an iterator.
42 * @param first The first position to read from.
43 * @return A tuple of the value read and an iterator to one after the last position read from.
44 */
45 template<typename InputIt>
readBigEndianInt32(InputIt first)46 std::tuple<uint32_t, InputIt> readBigEndianInt32(InputIt first) {
47 uint32_t ret;
48 auto it = first;
49 ret = *it++ << 24;
50 ret |= *it++ << 16;
51 ret |= *it++ << 8;
52 ret |= *it++;
53 return {ret, it};
54 }
55
56 /**
57 * Writes a 32-bit integer to the iterator in big-endian format.
58 * @param value The value to write.
59 * @param first The first position in the iterator.
60 * @return An iterator to one after the last position written to.
61 */
62 template<typename OutputIt>
writeBigEndian(const uint32_t value,OutputIt first)63 OutputIt writeBigEndian(const uint32_t value, OutputIt first) {
64 auto it = first;
65 *it++ = 0xff & (value >> 24);
66 *it++ = 0xff & (value >> 16);
67 *it++ = 0xff & (value >> 8);
68 *it++ = 0xff & value;
69 return it;
70 }
71
72 /**
73 * Transceive a command with the eSE and perform common error checking. When the
74 * handler is called, it has been checked that the transmission to and reception
75 * from the eSE was successful and that the response is in a valid format.
76 */
77 template<typename T, T OK, T FAILED>
78 T transceive(::android::esed::EseInterface& ese, const CommandApdu& command,
79 std::function<T(const ResponseApdu&)> handler = {}) {
80 // +1 for max size of extended response, +2 for status bytes
81 constexpr size_t MAX_RESPONSE_SIZE = std::numeric_limits<uint16_t>::max() + 1 + 2;
82 std::vector<uint8_t> responseBuffer(MAX_RESPONSE_SIZE);
83 const int ret = ese.transceive(command.vector(), responseBuffer);
84
85 // Check eSE communication was successful
86 if (ret < 0) {
87 std::string errMsg = "Failed to transceive data between AP and eSE";
88 if (ese.error()) {
89 errMsg += " (" + std::to_string(ese.error_code()) + "): " + ese.error_message();
90 } else {
91 errMsg += ": reason unknown";
92 }
93 LOG(ERROR) << errMsg;
94 return FAILED;
95 }
96 const size_t recvd = static_cast<size_t>(ret);
97
98 // Need to recalculate the maximum response size if this fails
99 if (recvd > MAX_RESPONSE_SIZE) {
100 LOG(ERROR) << "eSE response was longer than the buffer, check the buffer size.";
101 return FAILED;
102 }
103 responseBuffer.resize(recvd);
104
105 // Check for ISO 7816-4 APDU response format errors
106 ResponseApdu apdu{responseBuffer};
107 if (!apdu.ok()) {
108 LOG(ERROR) << "eSE response was invalid.";
109 return FAILED;
110 }
111
112 // Call handler if one was provided
113 if (handler) {
114 return handler(apdu);
115 }
116
117 return OK;
118 }
119
120 /**
121 * Checks that the amount of data in the response APDU matches the expected
122 * value.
123 */
124 template<typename T, T OK, T FAILED>
checkLength(const ResponseApdu & apdu,const size_t size)125 T checkLength(const ResponseApdu& apdu, const size_t size) {
126 if (apdu.dataSize() != size) {
127 LOG(ERROR) << "eSE response was the wrong length.";
128 return FAILED;
129 }
130 return OK;
131 }
132
133 /**
134 * Checks that the response APDU does no encode an error and that the amount of
135 * data matches the expected value.
136 */
137 template<typename T, T OK, T FAILED>
checkNoErrorAndLength(const ResponseApdu & apdu,const size_t size)138 T checkNoErrorAndLength(const ResponseApdu& apdu, const size_t size) {
139 if (apdu.isError()) {
140 LOG(ERROR) << "eSE operation failed";
141 return FAILED;
142 }
143 return checkLength<T, OK, FAILED>(apdu, size);
144 }
145
146 } // namespace android
147 } // namespace esed
148 } // namespace pn81a
149
150 #endif // ESED_PN81A_UTILS_H_
151