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
|
// Copyright (c) 2012 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 "content/browser/ssl/ssl_client_auth_handler.h"
#include "base/bind.h"
#include "base/logging.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/content_browser_client.h"
#include "content/public/browser/resource_request_info.h"
#include "net/cert/x509_certificate.h"
#include "net/ssl/client_cert_store.h"
#include "net/url_request/url_request.h"
namespace content {
namespace {
void CertificateSelectedOnUIThread(
const SSLClientAuthHandler::CertificateCallback& io_thread_callback,
net::X509Certificate* cert) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
BrowserThread::PostTask(
BrowserThread::IO, FROM_HERE,
base::Bind(io_thread_callback, make_scoped_refptr(cert)));
}
void SelectCertificateOnUIThread(
int render_process_host_id,
int render_frame_host_id,
net::SSLCertRequestInfo* cert_request_info,
const SSLClientAuthHandler::CertificateCallback& io_thread_callback) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
GetContentClient()->browser()->SelectClientCertificate(
render_process_host_id, render_frame_host_id, cert_request_info,
base::Bind(&CertificateSelectedOnUIThread, io_thread_callback));
}
} // namespace
// A reference-counted core to allow the ClientCertStore and SSLCertRequestInfo
// to outlive SSLClientAuthHandler if needbe.
class SSLClientAuthHandler::Core : public base::RefCountedThreadSafe<Core> {
public:
Core(const base::WeakPtr<SSLClientAuthHandler>& handler,
scoped_ptr<net::ClientCertStore> client_cert_store,
net::SSLCertRequestInfo* cert_request_info)
: handler_(handler),
client_cert_store_(client_cert_store.Pass()),
cert_request_info_(cert_request_info) {}
bool has_client_cert_store() const { return client_cert_store_; }
void GetClientCerts() {
if (client_cert_store_) {
// TODO(davidben): This is still a cyclical ownership where
// GetClientCerts' requirement that |client_cert_store_| remains alive
// until the call completes is maintained by the reference held in the
// callback.
client_cert_store_->GetClientCerts(
*cert_request_info_, &cert_request_info_->client_certs,
base::Bind(&SSLClientAuthHandler::Core::DidGetClientCerts, this));
} else {
DidGetClientCerts();
}
}
private:
friend class base::RefCountedThreadSafe<Core>;
~Core() {}
// Called when |client_cert_store_| is done retrieving the cert list.
void DidGetClientCerts() {
if (handler_)
handler_->DidGetClientCerts();
}
base::WeakPtr<SSLClientAuthHandler> handler_;
scoped_ptr<net::ClientCertStore> client_cert_store_;
scoped_refptr<net::SSLCertRequestInfo> cert_request_info_;
};
SSLClientAuthHandler::SSLClientAuthHandler(
scoped_ptr<net::ClientCertStore> client_cert_store,
net::URLRequest* request,
net::SSLCertRequestInfo* cert_request_info,
const SSLClientAuthHandler::CertificateCallback& callback)
: request_(request),
cert_request_info_(cert_request_info),
callback_(callback),
weak_factory_(this) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
core_ = new Core(weak_factory_.GetWeakPtr(), client_cert_store.Pass(),
cert_request_info_.get());
}
SSLClientAuthHandler::~SSLClientAuthHandler() {
}
void SSLClientAuthHandler::SelectCertificate() {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
// |core_| will call DidGetClientCerts when done.
core_->GetClientCerts();
}
void SSLClientAuthHandler::DidGetClientCerts() {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
// Note that if |client_cert_store_| is NULL, we intentionally fall through to
// DoCertificateSelected. This is for platforms where the client cert matching
// is not performed by Chrome. Those platforms handle the cert matching before
// showing the dialog.
if (core_->has_client_cert_store() &&
cert_request_info_->client_certs.empty()) {
// No need to query the user if there are no certs to choose from.
CertificateSelected(NULL);
return;
}
int render_process_host_id;
int render_frame_host_id;
if (!ResourceRequestInfo::ForRequest(request_)->GetAssociatedRenderFrame(
&render_process_host_id, &render_frame_host_id)) {
NOTREACHED();
CertificateSelected(NULL);
return;
}
BrowserThread::PostTask(
BrowserThread::UI, FROM_HERE,
base::Bind(&SelectCertificateOnUIThread, render_process_host_id,
render_frame_host_id, cert_request_info_,
base::Bind(&SSLClientAuthHandler::CertificateSelected,
weak_factory_.GetWeakPtr())));
}
void SSLClientAuthHandler::CertificateSelected(net::X509Certificate* cert) {
DVLOG(1) << this << " DoCertificateSelected " << cert;
DCHECK_CURRENTLY_ON(BrowserThread::IO);
callback_.Run(cert);
// |this| may be deleted at this point.
}
} // namespace content
|