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/apdu.h>
26 
27 #include "pn81a.h"
28 
29 namespace android {
30 namespace esed {
31 namespace pn81a {
32 
33 // HIDL
34 using ::android::hardware::Status;
35 
36 
37 // libapdu
38 using ::android::CommandApdu;
39 using ResponseApdu = ::android::ResponseApdu<const std::vector<uint8_t>>;
40 
41 /**
42  * Reads a 32-bit integer from an iterator.
43  * @param first The first position to read from.
44  * @return A tuple of the value read and an iterator to one after the last position read from.
45  */
46 template<typename InputIt>
readBigEndianInt32(InputIt first)47 std::tuple<uint32_t, InputIt> readBigEndianInt32(InputIt first) {
48     uint32_t ret;
49     auto it = first;
50     ret  = *it++ << 24;
51     ret |= *it++ << 16;
52     ret |= *it++ <<  8;
53     ret |= *it++;
54     return {ret, it};
55 }
56 
57 /**
58  * Writes a 32-bit integer to the iterator in big-endian format.
59  * @param value The value to write.
60  * @param first The first position in the iterator.
61  * @return An iterator to one after the last position written to.
62  */
63 template<typename OutputIt>
writeBigEndian(const uint32_t value,OutputIt first)64 OutputIt writeBigEndian(const uint32_t value, OutputIt first) {
65     auto it = first;
66     *it++ = 0xff & (value >> 24);
67     *it++ = 0xff & (value >> 16);
68     *it++ = 0xff & (value >>  8);
69     *it++ = 0xff & value;
70     return it;
71 }
72 
73 /**
74  * Transceive a command with the eSE and perform common error checking. When the
75  * handler is called, it has been checked that the transmission to and reception
76  * from the eSE was successful and that the response is in a valid format.
77  */
78 template<typename T, T OK, T FAILED>
79 T transceive(::android::esed::EseInterface& ese, const CommandApdu& command,
80                   std::function<T(const ResponseApdu&)> handler = {}) {
81     // +1 for max size of extended response, +2 for status bytes
82     constexpr size_t MAX_RESPONSE_SIZE = std::numeric_limits<uint16_t>::max() + 1 + 2;
83     std::vector<uint8_t> responseBuffer(MAX_RESPONSE_SIZE);
84     const int ret = ese.transceive(command.vector(), responseBuffer);
85 
86     // Check eSE communication was successful
87     if (ret < 0) {
88         std::string errMsg = "Failed to transceive data between AP and eSE";
89         if (ese.error()) {
90             errMsg += " (" + std::to_string(ese.error_code()) + "): " + ese.error_message();
91         } else {
92             errMsg += ": reason unknown";
93         }
94         LOG(ERROR) << errMsg;
95         return FAILED;
96     }
97     const size_t recvd = static_cast<size_t>(ret);
98 
99     // Need to recalculate the maximum response size if this fails
100     if (recvd > MAX_RESPONSE_SIZE) {
101         LOG(ERROR) << "eSE response was longer than the buffer, check the buffer size.";
102         return FAILED;
103     }
104     responseBuffer.resize(recvd);
105 
106     // Check for ISO 7816-4 APDU response format errors
107     ResponseApdu apdu{responseBuffer};
108     if (!apdu.ok()) {
109         LOG(ERROR) << "eSE response was invalid.";
110         return FAILED;
111     }
112 
113     // Call handler if one was provided
114     if (handler) {
115         return handler(apdu);
116     }
117 
118     return OK;
119 }
120 
121 /**
122  * Checks that the amount of data in the response APDU matches the expected
123  * value.
124  */
125 template<typename T, T OK, T FAILED>
checkLength(const ResponseApdu & apdu,const size_t size)126 T checkLength(const ResponseApdu& apdu, const size_t size) {
127     if (apdu.dataSize() != size) {
128         LOG(ERROR) << "eSE response was the wrong length.";
129         return FAILED;
130     }
131     return OK;
132 }
133 
134 /**
135  * Checks that the response APDU does no encode an error and that the amount of
136  * data matches the expected value.
137  */
138 template<typename T, T OK, T FAILED>
checkNoErrorAndLength(const ResponseApdu & apdu,const size_t size)139 T checkNoErrorAndLength(const ResponseApdu& apdu, const size_t size) {
140     if (apdu.isError()) {
141         LOG(ERROR) << "eSE operation failed";
142         return FAILED;
143     }
144     return checkLength<T, OK, FAILED>(apdu, size);
145 }
146 
147 } // namespace android
148 } // namespace esed
149 } // namespace pn81a
150 
151 #endif // ESED_PN81A_UTILS_H_
152