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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
content / browser / smart_card / smart_card_service.cc [blame]
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/smart_card/smart_card_service.h"
#include "base/check_deref.h"
#include "base/containers/map_util.h"
#include "content/browser/renderer_host/render_frame_host_impl.h"
#include "content/public/browser/isolated_context_util.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/smart_card_delegate.h"
#include "services/device/public/mojom/smart_card.mojom.h"
#include "third_party/blink/public/common/features_generated.h"
namespace content {
using device::mojom::SmartCardConnectResult;
using device::mojom::SmartCardCreateContextResult;
using device::mojom::SmartCardError;
namespace {
SmartCardDelegate& GetSmartCardDelegate() {
return CHECK_DEREF(GetContentClient()->browser()->GetSmartCardDelegate());
}
} // namespace
SmartCardService::SmartCardService(
RenderFrameHost& render_frame_host,
mojo::PendingReceiver<blink::mojom::SmartCardService> receiver,
mojo::PendingRemote<device::mojom::SmartCardContextFactory> context_factory)
: DocumentService(render_frame_host, std::move(receiver)),
context_factory_(std::move(context_factory)) {
context_wrapper_receivers_.set_disconnect_handler(
base::BindRepeating(&SmartCardService::OnMojoWrapperContextDisconnected,
base::Unretained(this)));
}
SmartCardService::~SmartCardService() {}
// static
void SmartCardService::Create(
RenderFrameHost* render_frame_host,
mojo::PendingReceiver<blink::mojom::SmartCardService> receiver) {
BrowserContext* browser_context = render_frame_host->GetBrowserContext();
DCHECK(browser_context);
if (!base::FeatureList::IsEnabled(blink::features::kSmartCard)) {
mojo::ReportBadMessage("The SmartCard feature is disabled.");
return;
}
if (!render_frame_host->IsFeatureEnabled(
blink::mojom::PermissionsPolicyFeature::kSmartCard)) {
mojo::ReportBadMessage(
"Access to the feature \"smart-card\" is disallowed by permissions "
"policy.");
return;
}
if (!HasIsolatedContextCapability(render_frame_host)) {
mojo::ReportBadMessage(
"Frame is not sufficiently isolated to use the Smart Card API.");
return;
}
SmartCardDelegate* delegate =
GetContentClient()->browser()->GetSmartCardDelegate();
if (!delegate) {
mojo::ReportBadMessage("Browser has no Smart Card delegate.");
return;
}
new SmartCardService(*render_frame_host, std::move(receiver),
delegate->GetSmartCardContextFactory(*browser_context));
}
void SmartCardService::CreateContext(CreateContextCallback callback) {
if (GetSmartCardDelegate().IsPermissionBlocked(render_frame_host())) {
std::move(callback).Run(SmartCardCreateContextResult::NewError(
SmartCardError::kPermissionDenied));
return;
}
context_factory_->CreateContext(
base::BindOnce(&SmartCardService::OnContextCreated,
weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
}
void SmartCardService::ListReaders(ListReadersCallback callback) {
mojo::ReceiverId context_wrapper_id =
context_wrapper_receivers_.current_receiver();
mojo::Remote<SmartCardContext>& context_remote =
CHECK_DEREF(base::FindOrNull(context_remotes_, context_wrapper_id));
context_remote->ListReaders(
base::BindOnce(&SmartCardService::OnListReadersResult,
weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
}
void SmartCardService::GetStatusChange(
base::TimeDelta timeout,
std::vector<device::mojom::SmartCardReaderStateInPtr> reader_states,
GetStatusChangeCallback callback) {
mojo::ReceiverId context_wrapper_id =
context_wrapper_receivers_.current_receiver();
mojo::Remote<SmartCardContext>& context_remote =
CHECK_DEREF(base::FindOrNull(context_remotes_, context_wrapper_id));
context_remote->GetStatusChange(timeout, std::move(reader_states),
std::move(callback));
}
void SmartCardService::Cancel(CancelCallback callback) {
mojo::ReceiverId context_wrapper_id =
context_wrapper_receivers_.current_receiver();
mojo::Remote<SmartCardContext>& context_remote =
CHECK_DEREF(base::FindOrNull(context_remotes_, context_wrapper_id));
context_remote->Cancel(std::move(callback));
}
void SmartCardService::Connect(
const std::string& reader,
device::mojom::SmartCardShareMode share_mode,
device::mojom::SmartCardProtocolsPtr preferred_protocols,
ConnectCallback callback) {
SmartCardDelegate& delegate = GetSmartCardDelegate();
mojo::ReceiverId context_wrapper_id =
context_wrapper_receivers_.current_receiver();
if (delegate.HasReaderPermission(render_frame_host(), reader)) {
mojo::Remote<SmartCardContext>& context_remote =
CHECK_DEREF(base::FindOrNull(context_remotes_, context_wrapper_id));
context_remote->Connect(reader, share_mode, std::move(preferred_protocols),
std::move(callback));
return;
}
if (!valid_reader_names_.contains(reader)) {
// Avoid showing the user a string that comes directly from the application.
//
// This will also block the case where an application asks to connect to a
// reader without first checking whether it exists in the system (via
// ListReaders). But no sane application should be doing this anyway. If
// this turns out to be a problem we will have to do a ListReaders() here on
// our own before coming to a decision.
std::move(callback).Run(
SmartCardConnectResult::NewError(SmartCardError::kPermissionDenied));
return;
}
delegate.RequestReaderPermission(
render_frame_host(), reader,
base::BindOnce(&SmartCardService::OnReaderPermissionResult,
weak_ptr_factory_.GetWeakPtr(), context_wrapper_id, reader,
share_mode, std::move(preferred_protocols),
std::move(callback)));
}
void SmartCardService::OnReaderPermissionResult(
mojo::ReceiverId context_wrapper_id,
const std::string& reader,
device::mojom::SmartCardShareMode share_mode,
device::mojom::SmartCardProtocolsPtr preferred_protocols,
ConnectCallback callback,
bool granted) {
auto it = context_remotes_.find(context_wrapper_id);
if (it == context_remotes_.end()) {
// Can happen if the renderer has dropped the wrapper remote in the
// meantime.
std::move(callback).Run(
SmartCardConnectResult::NewError(SmartCardError::kUnexpected));
return;
}
if (!granted) {
std::move(callback).Run(
SmartCardConnectResult::NewError(SmartCardError::kPermissionDenied));
return;
}
mojo::Remote<SmartCardContext>& context_remote = it->second;
context_remote->Connect(reader, share_mode, std::move(preferred_protocols),
std::move(callback));
}
void SmartCardService::OnMojoWrapperContextDisconnected() {
mojo::ReceiverId context_wrapper_id =
context_wrapper_receivers_.current_receiver();
context_remotes_.erase(context_wrapper_id);
}
void SmartCardService::OnListReadersResult(
ListReadersCallback callback,
device::mojom::SmartCardListReadersResultPtr result) {
if (result->is_readers()) {
// Update our set of valid reader names.
// Note that this is larger than the set of "currently available readers".
for (const auto& reader : result->get_readers()) {
valid_reader_names_.insert(reader);
}
}
// And finally forward the result to the original caller.
std::move(callback).Run(std::move(result));
}
void SmartCardService::OnContextCreated(
CreateContextCallback callback,
::device::mojom::SmartCardCreateContextResultPtr result) {
if (result->is_error()) {
std::move(callback).Run(std::move(result));
return;
}
// Wrap the context so that we can do permission checking/prompting before
// forwarding a call where appropriate.
mojo::PendingRemote<device::mojom::SmartCardContext> wrapper_remote;
mojo::ReceiverId context_wrapper_id = context_wrapper_receivers_.Add(
this, wrapper_remote.InitWithNewPipeAndPassReceiver());
context_remotes_[context_wrapper_id] =
mojo::Remote<SmartCardContext>(std::move(result->get_context()));
result->set_context(std::move(wrapper_remote));
std::move(callback).Run(std::move(result));
}
} // namespace content