1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/browser/renderer_host/pepper/pepper_socket_utils.h"
10 #include "base/logging.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/strings/string_util.h"
13 #include "content/public/browser/browser_thread.h"
14 #include "content/public/browser/content_browser_client.h"
15 #include "content/public/browser/render_frame_host.h"
16 #include "content/public/browser/site_instance.h"
17 #include "content/public/common/content_client.h"
18 #include "net/cert/x509_certificate.h"
19 #include "ppapi/c/private/ppb_net_address_private.h"
20 #include "ppapi/shared_impl/private/net_address_private_impl.h"
21 #include "ppapi/shared_impl/private/ppb_x509_certificate_private_shared.h"
24 namespace pepper_socket_utils
{
26 SocketPermissionRequest
CreateSocketPermissionRequest(
27 SocketPermissionRequest::OperationType type
,
28 const PP_NetAddress_Private
& net_addr
) {
29 std::string host
= ppapi::NetAddressPrivateImpl::DescribeNetAddress(net_addr
,
32 std::vector
<unsigned char> address
;
33 ppapi::NetAddressPrivateImpl::NetAddressToIPEndPoint(net_addr
,
36 return SocketPermissionRequest(type
, host
, port
);
39 bool CanUseSocketAPIs(bool external_plugin
,
41 const SocketPermissionRequest
* params
,
42 int render_process_id
,
43 int render_frame_id
) {
44 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
45 if (!external_plugin
) {
46 // Always allow socket APIs for out-process plugins (other than external
47 // plugins instantiated by the embeeder through
48 // BrowserPpapiHost::CreateExternalPluginProcess).
52 RenderFrameHost
* render_frame_host
=
53 RenderFrameHost::FromID(render_process_id
, render_frame_id
);
54 if (!render_frame_host
)
56 SiteInstance
* site_instance
= render_frame_host
->GetSiteInstance();
59 if (!GetContentClient()->browser()->AllowPepperSocketAPI(
60 site_instance
->GetBrowserContext(),
61 site_instance
->GetSiteURL(),
64 LOG(ERROR
) << "Host " << site_instance
->GetSiteURL().host()
65 << " cannot use socket API or destination is not allowed";
72 bool GetCertificateFields(const net::X509Certificate
& cert
,
73 ppapi::PPB_X509Certificate_Fields
* fields
) {
74 const net::CertPrincipal
& issuer
= cert
.issuer();
75 fields
->SetField(PP_X509CERTIFICATE_PRIVATE_ISSUER_COMMON_NAME
,
76 new base::StringValue(issuer
.common_name
));
77 fields
->SetField(PP_X509CERTIFICATE_PRIVATE_ISSUER_LOCALITY_NAME
,
78 new base::StringValue(issuer
.locality_name
));
79 fields
->SetField(PP_X509CERTIFICATE_PRIVATE_ISSUER_STATE_OR_PROVINCE_NAME
,
80 new base::StringValue(issuer
.state_or_province_name
));
81 fields
->SetField(PP_X509CERTIFICATE_PRIVATE_ISSUER_COUNTRY_NAME
,
82 new base::StringValue(issuer
.country_name
));
83 fields
->SetField(PP_X509CERTIFICATE_PRIVATE_ISSUER_ORGANIZATION_NAME
,
84 new base::StringValue(JoinString(issuer
.organization_names
, '\n')));
85 fields
->SetField(PP_X509CERTIFICATE_PRIVATE_ISSUER_ORGANIZATION_UNIT_NAME
,
86 new base::StringValue(JoinString(issuer
.organization_unit_names
, '\n')));
88 const net::CertPrincipal
& subject
= cert
.subject();
89 fields
->SetField(PP_X509CERTIFICATE_PRIVATE_SUBJECT_COMMON_NAME
,
90 new base::StringValue(subject
.common_name
));
91 fields
->SetField(PP_X509CERTIFICATE_PRIVATE_SUBJECT_LOCALITY_NAME
,
92 new base::StringValue(subject
.locality_name
));
93 fields
->SetField(PP_X509CERTIFICATE_PRIVATE_SUBJECT_STATE_OR_PROVINCE_NAME
,
94 new base::StringValue(subject
.state_or_province_name
));
95 fields
->SetField(PP_X509CERTIFICATE_PRIVATE_SUBJECT_COUNTRY_NAME
,
96 new base::StringValue(subject
.country_name
));
97 fields
->SetField(PP_X509CERTIFICATE_PRIVATE_SUBJECT_ORGANIZATION_NAME
,
98 new base::StringValue(JoinString(subject
.organization_names
, '\n')));
99 fields
->SetField(PP_X509CERTIFICATE_PRIVATE_SUBJECT_ORGANIZATION_UNIT_NAME
,
100 new base::StringValue(JoinString(subject
.organization_unit_names
, '\n')));
102 const std::string
& serial_number
= cert
.serial_number();
103 fields
->SetField(PP_X509CERTIFICATE_PRIVATE_SERIAL_NUMBER
,
104 base::BinaryValue::CreateWithCopiedBuffer(serial_number
.data(),
105 serial_number
.length()));
106 fields
->SetField(PP_X509CERTIFICATE_PRIVATE_VALIDITY_NOT_BEFORE
,
107 new base::FundamentalValue(cert
.valid_start().ToDoubleT()));
108 fields
->SetField(PP_X509CERTIFICATE_PRIVATE_VALIDITY_NOT_AFTER
,
109 new base::FundamentalValue(cert
.valid_expiry().ToDoubleT()));
111 net::X509Certificate::GetDEREncoded(cert
.os_cert_handle(), &der
);
112 fields
->SetField(PP_X509CERTIFICATE_PRIVATE_RAW
,
113 base::BinaryValue::CreateWithCopiedBuffer(der
.data(), der
.length()));
117 bool GetCertificateFields(const char* der
,
119 ppapi::PPB_X509Certificate_Fields
* fields
) {
120 scoped_refptr
<net::X509Certificate
> cert
=
121 net::X509Certificate::CreateFromBytes(der
, length
);
124 return GetCertificateFields(*cert
.get(), fields
);
127 } // namespace pepper_socket_utils
128 } // namespace content