-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathSession.cpp
More file actions
179 lines (156 loc) · 8.25 KB
/
Session.cpp
File metadata and controls
179 lines (156 loc) · 8.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include "Session.h"
#include "Channel.h"
#include "Reader.h"
#include "ByteArrayConverter.h"
namespace aidl::android::se::omapi {
using aidl::android::se::SecureElementReader;
SecureElementSession::SecureElementSession(SecureElementReader* reader) {
mReader = std::shared_ptr<SecureElementReader>(reader);
mAtr = mReader->getAtr();
mIsClosed = false;
}
::ndk::ScopedAStatus SecureElementSession::getReader(std::shared_ptr<ISecureElementReader>* outReader) {
*outReader = std::static_pointer_cast<ISecureElementReader>(mReader);
return ::ndk::ScopedAStatus::ok();
}
::ndk::ScopedAStatus SecureElementSession::getAtr(std::vector<uint8_t>* outAtr) {
*outAtr = mAtr;
return ::ndk::ScopedAStatus::ok();
}
::ndk::ScopedAStatus SecureElementSession::close() {
LOG(INFO) << __func__;
closeChannels();
mReader->removeSession(this);
std::lock_guard<std::mutex> lock(mLock);
mIsClosed = true;
return ::ndk::ScopedAStatus::ok();
}
::ndk::ScopedAStatus SecureElementSession::removeChannel(Channel* channel) {
LOG(INFO) << __func__;
std::lock_guard<std::mutex> lock(mLock);
mChannels.erase(
std::remove(mChannels.begin(), mChannels.end(), channel),
mChannels.end()
);
LOG(INFO) << "Removed channel: " << channel->getChannelNumber();
return ::ndk::ScopedAStatus::ok();
}
::ndk::ScopedAStatus SecureElementSession::closeChannels() {
LOG(INFO) << __func__;
std::lock_guard<std::mutex> lock(mLock);
for (auto channel : mChannels) {
channel->close();
LOG(INFO) << "Closed channel: " << channel->getChannelNumber();
}
return ::ndk::ScopedAStatus::ok();
}
::ndk::ScopedAStatus SecureElementSession::isClosed(bool* isClosed) {
LOG(INFO) << __func__;
std::lock_guard<std::mutex> lock(mLock);
*isClosed = mIsClosed;
return ::ndk::ScopedAStatus::ok();
}
::ndk::ScopedAStatus SecureElementSession::openBasicChannel(const std::vector<uint8_t>& aid, int8_t p2,
const std::shared_ptr<ISecureElementListener>& listener, std::shared_ptr<ISecureElementChannel>* outChannel) {
LOG(INFO) << __func__ << " AID = " << p2;
if(mIsClosed) {
LOG(ERROR) << __func__ << ": Session is closed!";
*outChannel = nullptr;
return ::ndk::ScopedAStatus::fromServiceSpecificErrorWithMessage(EX_SERVICE_SPECIFIC,
"Session is closed");
} else if(listener == nullptr) {
LOG(ERROR) << __func__ << ": listener is null!";
*outChannel = nullptr;
return ::ndk::ScopedAStatus::fromServiceSpecificErrorWithMessage(EX_SERVICE_SPECIFIC,
"Listener is null");
} else if ((p2 != 0x00) && (p2 != 0x04) && (p2 != 0x08) && (p2 != 0x0C)) {
LOG(ERROR) << __func__ << ": Unsupported p2 operation: " << (p2 & 0xFF);
*outChannel = nullptr;
return ::ndk::ScopedAStatus::fromServiceSpecificErrorWithMessage(EX_SERVICE_SPECIFIC,
"Unsupported p2 operation");
}
std::string packageName = "";
// const std::vector<uint8_t>& uuid
/* Ignore getting package name */
// LOG(INFO) << "openBasicChannel() trying to find mapping uuid";
/* hardcode uuid, refer to /vendor/etc/hal_uuid_map.xml */
/* Skip judging of equal of mReader and ESE_TERMINAL */
// int uid = AIBinder_getCallingUid();
Terminal& terminal = mReader->getTerminal();
std::shared_ptr<Channel> channel = nullptr;
for (std::string &uuid : UUIDs) {
mUuid = hexStringToBytes(uuid);
LOG(INFO) << __func__ << ": Trying to open basic channel with uuid: " << uuid;
channel = terminal.openBasicChannel(this, aid, p2, listener,
packageName, mUuid,
AIBinder_getCallingPid());
if (channel != nullptr) {
LOG(INFO) << __func__ << ": Open basic channel success. Channel: " << channel->getChannelNumber();
break;
}
}
if (channel == nullptr) {
LOG(ERROR) << __func__ << ": returning null";
LOG(ERROR) << __func__ << ": Please check /(vendor|odm)/etc/hal_uuid_map_config.xml for correct uuid!";
return ::ndk::ScopedAStatus::fromServiceSpecificErrorWithMessage(
-1, "Failed to openBasicChannel");
}
LOG(INFO) << "Open basic channel success. Channel: " << channel->getChannelNumber();
std::lock_guard<std::mutex> lock(mLock);
mChannels.push_back(channel.get()); // Store raw pointer if mChannels remains std::vector<Channel*>
// auto sChannel = std::shared_ptr<Channel>(channel); // This was the problematic line
auto sChannel = channel; // Correctly copy the shared_ptr
*outChannel = ndk::SharedRefBase::make<SecureElementChannel>(sChannel);
return ::ndk::ScopedAStatus::ok();
}
::ndk::ScopedAStatus SecureElementSession::openLogicalChannel(const std::vector<uint8_t>& aid, int8_t p2,
const std::shared_ptr<ISecureElementListener>& listener, std::shared_ptr<ISecureElementChannel>* outChannel) {
LOG(INFO) << __func__ << " AID = " << hex2string(aid) << ", P2 = " << p2;
if(mIsClosed) {
LOG(ERROR) << __func__ << ": Session is closed!";
*outChannel = nullptr;
return ::ndk::ScopedAStatus::fromServiceSpecificErrorWithMessage(EX_SERVICE_SPECIFIC,
"Session is closed");
} else if(listener == nullptr) {
LOG(ERROR) << __func__ << ": Listener is null!";
*outChannel = nullptr;
return ::ndk::ScopedAStatus::fromServiceSpecificErrorWithMessage(EX_SERVICE_SPECIFIC,
"Listener is null");
} else if ((p2 != 0x00) && (p2 != 0x04) && (p2 != 0x08) && (p2 != 0x0C)) {
LOG(ERROR) << __func__ << ": Unsupported p2 operation: " << (p2 & 0xFF);
*outChannel = nullptr;
return ::ndk::ScopedAStatus::fromServiceSpecificErrorWithMessage(EX_SERVICE_SPECIFIC,
"Unsupported p2 operation");
}
std::string packageName = ""; /* getPackageNameFromCallingUid, empty on native env */
// if(mReader->getTerminal().getName().starts_with(SecureElementService::ESE_TERMINAL)) {
// getUUID logic, hardcode it
// }
Terminal& terminal = mReader->getTerminal();
std::shared_ptr<Channel> channel = nullptr;
for (std::string &uuid : UUIDs) {
mUuid = hexStringToBytes(uuid);
LOG(INFO) << __func__ << ": Trying to open logic channel with uuid: " << uuid;
channel = terminal.openLogicalChannel(this, aid, p2, listener,
packageName, mUuid,
AIBinder_getCallingPid());
if (channel != nullptr) {
LOG(INFO) << __func__ << ": Open logic channel success. Channel: " << channel->getChannelNumber();
break;
}
}
if (channel == nullptr) {
LOG(ERROR) << __func__ << ": returning null";
LOG(ERROR) << __func__ << ": Please check /(vendor|odm)/etc/hal_uuid_map_config.xml for correct uuid!";
return ::ndk::ScopedAStatus::fromServiceSpecificErrorWithMessage(
-1, "Failed to openLogicalChannel");
}
LOG(INFO) << __func__ << ": openLogicalChannel() Success. Channel: " << channel->getChannelNumber();
std::lock_guard<std::mutex> lock(mLock);
mChannels.push_back(channel.get()); // Store raw pointer if mChannels remains std::vector<Channel*>
// auto sChannel = std::shared_ptr<Channel>(channel); // This was the problematic line
auto sChannel = channel; // Correctly copy the shared_ptr
*outChannel = ndk::SharedRefBase::make<SecureElementChannel>(sChannel);
return ::ndk::ScopedAStatus::ok();
}
}