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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/tab_contents/tab_contents_ssl_helper.h"
#include <string>
#include <vector>
#include "base/basictypes.h"
#include "base/command_line.h"
#include "base/string_number_conversions.h"
#include "base/utf_string_conversions.h"
#include "base/values.h"
#include "chrome/browser/certificate_viewer.h"
#include "chrome/browser/content_settings/host_content_settings_map.h"
#include "chrome/browser/infobars/infobar.h"
#include "chrome/browser/infobars/infobar_tab_helper.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ssl/ssl_add_cert_handler.h"
#include "chrome/browser/ssl_client_certificate_selector.h"
#include "chrome/browser/tab_contents/confirm_infobar_delegate.h"
#include "chrome/browser/tab_contents/simple_alert_infobar_delegate.h"
#include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h"
#include "chrome/common/chrome_notification_types.h"
#include "chrome/common/chrome_switches.h"
#include "content/browser/ssl/ssl_client_auth_handler.h"
#include "content/common/notification_details.h"
#include "content/common/notification_source.h"
#include "grit/generated_resources.h"
#include "grit/theme_resources_standard.h"
#include "net/base/net_errors.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/resource/resource_bundle.h"
namespace {
gfx::Image* GetCertIcon() {
// TODO(davidben): use a more appropriate icon.
return &ResourceBundle::GetSharedInstance().GetNativeImageNamed(
IDR_INFOBAR_SAVE_PASSWORD);
}
bool CertMatchesFilter(const net::X509Certificate& cert,
const base::DictionaryValue& filter) {
// TODO(markusheintz): This is the minimal required filter implementation.
// Implement a better matcher.
// An empty filter matches any client certificate since no requirements are
// specified at all.
if (filter.empty())
return true;
std::string common_name;
if (filter.GetString("ISSUER.CN", &common_name) &&
(cert.issuer().common_name == common_name)) {
return true;
}
return false;
}
// SSLCertAddedInfoBarDelegate ------------------------------------------------
class SSLCertAddedInfoBarDelegate : public ConfirmInfoBarDelegate {
public:
SSLCertAddedInfoBarDelegate(InfoBarTabHelper* infobar_helper,
net::X509Certificate* cert);
private:
virtual ~SSLCertAddedInfoBarDelegate();
// ConfirmInfoBarDelegate:
virtual gfx::Image* GetIcon() const OVERRIDE;
virtual Type GetInfoBarType() const OVERRIDE;
virtual string16 GetMessageText() const OVERRIDE;
virtual int GetButtons() const OVERRIDE;
virtual string16 GetButtonLabel(InfoBarButton button) const OVERRIDE;
virtual bool Accept() OVERRIDE;
scoped_refptr<net::X509Certificate> cert_; // The cert we added.
};
SSLCertAddedInfoBarDelegate::SSLCertAddedInfoBarDelegate(
InfoBarTabHelper* infobar_helper,
net::X509Certificate* cert)
: ConfirmInfoBarDelegate(infobar_helper),
cert_(cert) {
}
SSLCertAddedInfoBarDelegate::~SSLCertAddedInfoBarDelegate() {
}
gfx::Image* SSLCertAddedInfoBarDelegate::GetIcon() const {
return GetCertIcon();
}
InfoBarDelegate::Type SSLCertAddedInfoBarDelegate::GetInfoBarType() const {
return PAGE_ACTION_TYPE;
}
string16 SSLCertAddedInfoBarDelegate::GetMessageText() const {
// TODO(evanm): GetDisplayName should return UTF-16.
return l10n_util::GetStringFUTF16(IDS_ADD_CERT_SUCCESS_INFOBAR_LABEL,
UTF8ToUTF16(cert_->issuer().GetDisplayName()));
}
int SSLCertAddedInfoBarDelegate::GetButtons() const {
return BUTTON_OK;
}
string16 SSLCertAddedInfoBarDelegate::GetButtonLabel(
InfoBarButton button) const {
DCHECK_EQ(BUTTON_OK, button);
return l10n_util::GetStringUTF16(IDS_ADD_CERT_SUCCESS_INFOBAR_BUTTON);
}
bool SSLCertAddedInfoBarDelegate::Accept() {
ShowCertificateViewer(owner()->tab_contents()->GetDialogRootWindow(), cert_);
return false; // Hiding the infobar just as the dialog opens looks weird.
}
} // namespace
// TabContentsSSLHelper::SSLAddCertData ---------------------------------------
class TabContentsSSLHelper::SSLAddCertData : public NotificationObserver {
public:
explicit SSLAddCertData(TabContentsWrapper* tab_contents);
virtual ~SSLAddCertData();
// Displays |delegate| as an infobar in |tab_|, replacing our current one if
// still active.
void ShowInfoBar(InfoBarDelegate* delegate);
// Same as above, for the common case of wanting to show a simple alert
// message.
void ShowErrorInfoBar(const string16& message);
private:
// NotificationObserver:
virtual void Observe(int type,
const NotificationSource& source,
const NotificationDetails& details);
TabContentsWrapper* tab_contents_;
InfoBarDelegate* infobar_delegate_;
NotificationRegistrar registrar_;
DISALLOW_COPY_AND_ASSIGN(SSLAddCertData);
};
TabContentsSSLHelper::SSLAddCertData::SSLAddCertData(
TabContentsWrapper* tab_contents)
: tab_contents_(tab_contents),
infobar_delegate_(NULL) {
Source<InfoBarTabHelper> source(tab_contents_->infobar_tab_helper());
registrar_.Add(this, chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED,
source);
registrar_.Add(this, chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REPLACED,
source);
}
TabContentsSSLHelper::SSLAddCertData::~SSLAddCertData() {
}
void TabContentsSSLHelper::SSLAddCertData::ShowInfoBar(
InfoBarDelegate* delegate) {
if (infobar_delegate_)
tab_contents_->infobar_tab_helper()->ReplaceInfoBar(infobar_delegate_,
delegate);
else
tab_contents_->infobar_tab_helper()->AddInfoBar(delegate);
infobar_delegate_ = delegate;
}
void TabContentsSSLHelper::SSLAddCertData::ShowErrorInfoBar(
const string16& message) {
ShowInfoBar(new SimpleAlertInfoBarDelegate(
tab_contents_->infobar_tab_helper(), GetCertIcon(), message, true));
}
void TabContentsSSLHelper::SSLAddCertData::Observe(
int type,
const NotificationSource& source,
const NotificationDetails& details) {
DCHECK(type == chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED ||
type == chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REPLACED);
if (infobar_delegate_ ==
((type == chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED) ?
Details<InfoBarRemovedDetails>(details)->first :
Details<InfoBarReplacedDetails>(details)->first))
infobar_delegate_ = NULL;
}
// TabContentsSSLHelper -------------------------------------------------------
TabContentsSSLHelper::TabContentsSSLHelper(TabContentsWrapper* tab_contents)
: tab_contents_(tab_contents) {
}
TabContentsSSLHelper::~TabContentsSSLHelper() {
}
void TabContentsSSLHelper::SelectClientCertificate(
scoped_refptr<SSLClientAuthHandler> handler) {
net::SSLCertRequestInfo* cert_request_info = handler->cert_request_info();
GURL requesting_url("https://" + cert_request_info->host_and_port);
DCHECK(requesting_url.is_valid()) << "Invalid URL string: https://"
<< cert_request_info->host_and_port;
HostContentSettingsMap* map =
tab_contents_->profile()->GetHostContentSettingsMap();
scoped_ptr<Value> filter(map->GetContentSettingValue(
requesting_url,
requesting_url,
CONTENT_SETTINGS_TYPE_AUTO_SELECT_CERTIFICATE,
std::string()));
scoped_refptr<net::X509Certificate> selected_cert;
if (filter.get()) {
// Try to automatically select a client certificate.
DCHECK(filter->IsType(Value::TYPE_DICTIONARY));
DictionaryValue* filter_dict = static_cast<DictionaryValue*>(filter.get());
const std::vector<scoped_refptr<net::X509Certificate> >& all_client_certs =
cert_request_info->client_certs;
for (size_t i = 0; i < all_client_certs.size(); ++i) {
if (CertMatchesFilter(*all_client_certs[i], *filter_dict)) {
selected_cert = all_client_certs[i];
// Use the first certificate that is matched by the filter.
break;
}
}
}
if (selected_cert) {
handler->CertificateSelected(selected_cert);
} else {
ShowClientCertificateRequestDialog(handler);
}
}
void TabContentsSSLHelper::ShowClientCertificateRequestDialog(
scoped_refptr<SSLClientAuthHandler> handler) {
// TODO(rhashimoto): WebUI certificate selector for touch.
#if !defined(TOUCH_UI)
browser::ShowSSLClientCertificateSelector(
tab_contents_, handler->cert_request_info(), handler);
#endif
}
void TabContentsSSLHelper::OnVerifyClientCertificateError(
scoped_refptr<SSLAddCertHandler> handler, int error_code) {
SSLAddCertData* add_cert_data = GetAddCertData(handler);
// Display an infobar with the error message.
// TODO(davidben): Display a more user-friendly error string.
add_cert_data->ShowErrorInfoBar(
l10n_util::GetStringFUTF16(IDS_ADD_CERT_ERR_INVALID_CERT,
base::IntToString16(-error_code),
ASCIIToUTF16(net::ErrorToString(error_code))));
}
void TabContentsSSLHelper::AskToAddClientCertificate(
scoped_refptr<SSLAddCertHandler> handler) {
NOTREACHED(); // Not implemented yet.
}
void TabContentsSSLHelper::OnAddClientCertificateSuccess(
scoped_refptr<SSLAddCertHandler> handler) {
SSLAddCertData* add_cert_data = GetAddCertData(handler);
// Display an infobar to inform the user.
add_cert_data->ShowInfoBar(new SSLCertAddedInfoBarDelegate(
tab_contents_->infobar_tab_helper(), handler->cert()));
}
void TabContentsSSLHelper::OnAddClientCertificateError(
scoped_refptr<SSLAddCertHandler> handler, int error_code) {
SSLAddCertData* add_cert_data = GetAddCertData(handler);
// Display an infobar with the error message.
// TODO(davidben): Display a more user-friendly error string.
add_cert_data->ShowErrorInfoBar(
l10n_util::GetStringFUTF16(IDS_ADD_CERT_ERR_FAILED,
base::IntToString16(-error_code),
ASCIIToUTF16(net::ErrorToString(error_code))));
}
void TabContentsSSLHelper::OnAddClientCertificateFinished(
scoped_refptr<SSLAddCertHandler> handler) {
// Clean up.
request_id_to_add_cert_data_.erase(handler->network_request_id());
}
TabContentsSSLHelper::SSLAddCertData* TabContentsSSLHelper::GetAddCertData(
SSLAddCertHandler* handler) {
// Find/create the slot.
linked_ptr<SSLAddCertData>& ptr_ref =
request_id_to_add_cert_data_[handler->network_request_id()];
// Fill it if necessary.
if (!ptr_ref.get())
ptr_ref.reset(new SSLAddCertData(tab_contents_));
return ptr_ref.get();
}
|