forked from kmiit/FakeOmapi
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSession.cpp
More file actions
159 lines (140 loc) · 5.8 KB
/
Session.cpp
File metadata and controls
159 lines (140 loc) · 5.8 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
#include "Session.h"
#include "Channel.h"
#include "Reader.h"
#include "ByteArrayConverter.h"
namespace aidl::android::se::omapi {
using aidl::android::se::SecureElementReader;
SecureElementSession::SecureElementSession(std::shared_ptr<SecureElementReader> reader)
: mReader(reader) {
if (reader != nullptr) {
mAtr = reader->getAtr();
}
}
SecureElementSession::~SecureElementSession() = default;
::ndk::ScopedAStatus SecureElementSession::getReader(std::shared_ptr<ISecureElementReader>* outReader) {
auto r = mReader.lock();
if (r == nullptr) {
*outReader = nullptr;
return ::ndk::ScopedAStatus::fromExceptionCodeWithMessage(
EX_ILLEGAL_STATE, "Reader is gone");
}
*outReader = std::static_pointer_cast<ISecureElementReader>(r);
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__;
if (mIsClosed.exchange(true)) {
return ::ndk::ScopedAStatus::ok();
}
closeChannels();
if (auto r = mReader.lock()) {
r->removeSession(this);
}
return ::ndk::ScopedAStatus::ok();
}
::ndk::ScopedAStatus SecureElementSession::removeChannel(Channel* channel) {
LOG(INFO) << __func__;
std::lock_guard<std::mutex> lock(mLock);
mChannels.erase(
std::remove_if(
mChannels.begin(), mChannels.end(),
[channel](const std::shared_ptr<Channel>& c) { return c.get() == channel; }
),
mChannels.end()
);
LOG(INFO) << "Removed channel: " << channel->getChannelNumber();
return ::ndk::ScopedAStatus::ok();
}
::ndk::ScopedAStatus SecureElementSession::closeChannels() {
LOG(INFO) << __func__;
std::vector<std::shared_ptr<Channel>> snapshot;
{
std::lock_guard<std::mutex> lock(mLock);
snapshot = mChannels;
}
for (auto& channel : snapshot) {
if (channel) {
channel->close();
LOG(INFO) << "Closed channel: " << channel->getChannelNumber();
}
}
return ::ndk::ScopedAStatus::ok();
}
::ndk::ScopedAStatus SecureElementSession::isClosed(bool* isClosed) {
LOG(INFO) << __func__;
*isClosed = mIsClosed.load();
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=" << hex2string(aid) << ", P2=" << int(p2);
if (mIsClosed) {
*outChannel = nullptr;
return ::ndk::ScopedAStatus::fromServiceSpecificErrorWithMessage(EX_SERVICE_SPECIFIC,
"Session is closed");
}
if (listener == nullptr) {
*outChannel = nullptr;
return ::ndk::ScopedAStatus::fromExceptionCodeWithMessage(
EX_NULL_POINTER, "Listener is null");
}
if ((p2 != 0x00) && (p2 != 0x04) && (p2 != 0x08) && (p2 != 0x0C)) {
LOG(ERROR) << __func__ << ": Unsupported p2: " << int(p2 & 0xFF);
*outChannel = nullptr;
return ::ndk::ScopedAStatus::fromServiceSpecificErrorWithMessage(EX_SERVICE_SPECIFIC,
"Unsupported p2 operation");
}
auto reader = mReader.lock();
if (!reader) {
return ::ndk::ScopedAStatus::fromServiceSpecificErrorWithMessage(-1, "Reader is gone");
}
std::shared_ptr<Channel> channel =
reader->getTerminal()->openBasicChannel(this->ref<SecureElementSession>(), aid, p2, listener);
if (channel == nullptr) {
return ::ndk::ScopedAStatus::fromServiceSpecificErrorWithMessage(
-1, "Failed to openBasicChannel");
}
std::lock_guard<std::mutex> lock(mLock);
mChannels.push_back(channel);
*outChannel = ndk::SharedRefBase::make<SecureElementChannel>(channel);
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=" << int(p2);
if (mIsClosed) {
*outChannel = nullptr;
return ::ndk::ScopedAStatus::fromServiceSpecificErrorWithMessage(EX_SERVICE_SPECIFIC,
"Session is closed");
}
if (listener == nullptr) {
*outChannel = nullptr;
return ::ndk::ScopedAStatus::fromExceptionCodeWithMessage(
EX_NULL_POINTER, "Listener is null");
}
if ((p2 != 0x00) && (p2 != 0x04) && (p2 != 0x08) && (p2 != 0x0C)) {
LOG(ERROR) << __func__ << ": Unsupported p2: " << int(p2 & 0xFF);
*outChannel = nullptr;
return ::ndk::ScopedAStatus::fromServiceSpecificErrorWithMessage(EX_SERVICE_SPECIFIC,
"Unsupported p2 operation");
}
auto reader = mReader.lock();
if (!reader) {
return ::ndk::ScopedAStatus::fromServiceSpecificErrorWithMessage(-1, "Reader is gone");
}
std::shared_ptr<Channel> channel =
reader->getTerminal()->openLogicalChannel(this->ref<SecureElementSession>(), aid, p2, listener);
if (channel == nullptr) {
return ::ndk::ScopedAStatus::fromServiceSpecificErrorWithMessage(
-1, "Failed to openLogicalChannel");
}
std::lock_guard<std::mutex> lock(mLock);
mChannels.push_back(channel);
*outChannel = ndk::SharedRefBase::make<SecureElementChannel>(channel);
return ::ndk::ScopedAStatus::ok();
}
}