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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
|
// 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 "remoting/protocol/content_description.h"
#include "base/base64.h"
#include "base/logging.h"
#include "base/string_number_conversions.h"
#include "remoting/base/constants.h"
#include "remoting/proto/auth.pb.h"
#include "third_party/libjingle/source/talk/xmllite/xmlelement.h"
using buzz::QName;
using buzz::XmlElement;
namespace remoting {
namespace protocol {
namespace {
const char kDefaultNs[] = "";
const char kChromotingContentName[] = "chromoting";
// Following constants are used to format session description in XML.
const char kDescriptionTag[] = "description";
const char kControlTag[] = "control";
const char kEventTag[] = "event";
const char kVideoTag[] = "video";
const char kResolutionTag[] = "initial-resolution";
const char kAuthenticationTag[] = "authentication";
const char kCertificateTag[] = "certificate";
const char kMasterKeyTag[] = "master-key";
const char kAuthTokenTag[] = "auth-token";
const char kTransportAttr[] = "transport";
const char kVersionAttr[] = "version";
const char kCodecAttr[] = "codec";
const char kWidthAttr[] = "width";
const char kHeightAttr[] = "height";
const char kStreamTransport[] = "stream";
const char kDatagramTransport[] = "datagram";
const char kSrtpTransport[] = "srtp";
const char kRtpDtlsTransport[] = "rtp-dtls";
const char kVp8Codec[] = "vp8";
const char kZipCodec[] = "zip";
const char* GetTransportName(ChannelConfig::TransportType type) {
switch (type) {
case ChannelConfig::TRANSPORT_STREAM:
return kStreamTransport;
case ChannelConfig::TRANSPORT_DATAGRAM:
return kDatagramTransport;
case ChannelConfig::TRANSPORT_SRTP:
return kSrtpTransport;
case ChannelConfig::TRANSPORT_RTP_DTLS:
return kRtpDtlsTransport;
}
NOTREACHED();
return NULL;
}
const char* GetCodecName(ChannelConfig::Codec type) {
switch (type) {
case ChannelConfig::CODEC_VP8:
return kVp8Codec;
case ChannelConfig::CODEC_ZIP:
return kZipCodec;
default:
break;
}
NOTREACHED();
return NULL;
}
// Format a channel configuration tag for chromotocol session description,
// e.g. for video channel:
// <video transport="srtp" version="1" codec="vp8" />
XmlElement* FormatChannelConfig(const ChannelConfig& config,
const std::string& tag_name) {
XmlElement* result = new XmlElement(
QName(kChromotingXmlNamespace, tag_name));
result->AddAttr(QName(kDefaultNs, kTransportAttr),
GetTransportName(config.transport));
result->AddAttr(QName(kDefaultNs, kVersionAttr),
base::IntToString(config.version));
if (config.codec != ChannelConfig::CODEC_UNDEFINED) {
result->AddAttr(QName(kDefaultNs, kCodecAttr),
GetCodecName(config.codec));
}
return result;
}
bool ParseTransportName(const std::string& value,
ChannelConfig::TransportType* transport) {
if (value == kStreamTransport) {
*transport = ChannelConfig::TRANSPORT_STREAM;
} else if (value == kDatagramTransport) {
*transport = ChannelConfig::TRANSPORT_DATAGRAM;
} else if (value == kSrtpTransport) {
*transport = ChannelConfig::TRANSPORT_SRTP;
} else if (value == kRtpDtlsTransport) {
*transport = ChannelConfig::TRANSPORT_RTP_DTLS;
} else {
return false;
}
return true;
}
bool ParseCodecName(const std::string& value, ChannelConfig::Codec* codec) {
if (value == kVp8Codec) {
*codec = ChannelConfig::CODEC_VP8;
} else if (value == kZipCodec) {
*codec = ChannelConfig::CODEC_ZIP;
} else {
return false;
}
return true;
}
// Returns false if the element is invalid.
bool ParseChannelConfig(const XmlElement* element, bool codec_required,
ChannelConfig* config) {
if (!ParseTransportName(element->Attr(QName(kDefaultNs, kTransportAttr)),
&config->transport) ||
!base::StringToInt(element->Attr(QName(kDefaultNs, kVersionAttr)),
&config->version)) {
return false;
}
if (codec_required) {
if (!ParseCodecName(element->Attr(QName(kDefaultNs, kCodecAttr)),
&config->codec)) {
return false;
}
} else {
config->codec = ChannelConfig::CODEC_UNDEFINED;
}
return true;
}
} // namespace
ContentDescription::ContentDescription(
const CandidateSessionConfig* candidate_config,
const std::string& auth_token,
const std::string& master_key,
scoped_refptr<net::X509Certificate> certificate)
: candidate_config_(candidate_config),
auth_token_(auth_token),
master_key_(master_key),
certificate_(certificate) {
}
ContentDescription::~ContentDescription() { }
// ToXml() creates content description for chromoting session. The
// description looks as follows:
// <description xmlns="google:remoting">
// <control transport="stream" version="1" />
// <event transport="datagram" version="1" />
// <video transport="srtp" codec="vp8" version="1" />
// <initial-resolution width="800" height="600" />
// <authentication>
// <certificate>[BASE64 Encoded Certificate]</certificate>
// <master-key>[master key encrypted with hosts
// public key encoded with BASE64]</master-key>
// <auth-token>...</auth-token> // IT2Me only.
// </authentication>
// </description>
//
XmlElement* ContentDescription::ToXml() const {
XmlElement* root = new XmlElement(
QName(kChromotingXmlNamespace, kDescriptionTag), true);
std::vector<ChannelConfig>::const_iterator it;
for (it = config()->control_configs().begin();
it != config()->control_configs().end(); ++it) {
root->AddElement(FormatChannelConfig(*it, kControlTag));
}
for (it = config()->event_configs().begin();
it != config()->event_configs().end(); ++it) {
root->AddElement(FormatChannelConfig(*it, kEventTag));
}
for (it = config()->video_configs().begin();
it != config()->video_configs().end(); ++it) {
root->AddElement(FormatChannelConfig(*it, kVideoTag));
}
XmlElement* resolution_tag = new XmlElement(
QName(kChromotingXmlNamespace, kResolutionTag));
resolution_tag->AddAttr(QName(kDefaultNs, kWidthAttr),
base::IntToString(
config()->initial_resolution().width));
resolution_tag->AddAttr(QName(kDefaultNs, kHeightAttr),
base::IntToString(
config()->initial_resolution().height));
root->AddElement(resolution_tag);
if (certificate() || !auth_token().empty()) {
XmlElement* authentication_tag = new XmlElement(
QName(kChromotingXmlNamespace, kAuthenticationTag));
if (certificate()) {
XmlElement* certificate_tag = new XmlElement(
QName(kChromotingXmlNamespace, kCertificateTag));
std::string der_cert;
if (!certificate()->GetDEREncoded(&der_cert)) {
LOG(DFATAL) << "Cannot obtain DER encoded certificate";
}
std::string base64_cert;
if (!base::Base64Encode(der_cert, &base64_cert)) {
LOG(DFATAL) << "Cannot perform base64 encode on certificate";
}
certificate_tag->SetBodyText(base64_cert);
authentication_tag->AddElement(certificate_tag);
}
if (!master_key().empty()) {
XmlElement* master_key_tag = new XmlElement(
QName(kChromotingXmlNamespace, kMasterKeyTag));
std::string master_key_base64;
if (!base::Base64Encode(master_key(), &master_key_base64)) {
LOG(DFATAL) << "Cannot perform base64 encode on master key";
}
master_key_tag->SetBodyText(master_key_base64);
authentication_tag->AddElement(master_key_tag);
}
if (!auth_token().empty()) {
XmlElement* auth_token_tag = new XmlElement(
QName(kChromotingXmlNamespace, kAuthTokenTag));
auth_token_tag->SetBodyText(auth_token());
authentication_tag->AddElement(auth_token_tag);
}
root->AddElement(authentication_tag);
}
return root;
}
// static
cricket::ContentDescription* ContentDescription::ParseXml(
const XmlElement* element) {
if (element->Name() == QName(kChromotingXmlNamespace, kDescriptionTag)) {
scoped_ptr<CandidateSessionConfig> config(
CandidateSessionConfig::CreateEmpty());
const XmlElement* child = NULL;
// <control> tags.
QName control_tag(kChromotingXmlNamespace, kControlTag);
child = element->FirstNamed(control_tag);
while (child) {
ChannelConfig channel_config;
if (!ParseChannelConfig(child, false, &channel_config))
return NULL;
config->mutable_control_configs()->push_back(channel_config);
child = child->NextNamed(control_tag);
}
// <event> tags.
QName event_tag(kChromotingXmlNamespace, kEventTag);
child = element->FirstNamed(event_tag);
while (child) {
ChannelConfig channel_config;
if (!ParseChannelConfig(child, false, &channel_config))
return NULL;
config->mutable_event_configs()->push_back(channel_config);
child = child->NextNamed(event_tag);
}
// <video> tags.
QName video_tag(kChromotingXmlNamespace, kVideoTag);
child = element->FirstNamed(video_tag);
while (child) {
ChannelConfig channel_config;
if (!ParseChannelConfig(child, true, &channel_config))
return NULL;
config->mutable_video_configs()->push_back(channel_config);
child = child->NextNamed(video_tag);
}
// <initial-resolution> tag.
child = element->FirstNamed(QName(kChromotingXmlNamespace, kResolutionTag));
if (!child)
return NULL; // Resolution must always be specified.
int width;
int height;
if (!base::StringToInt(child->Attr(QName(kDefaultNs, kWidthAttr)),
&width) ||
!base::StringToInt(child->Attr(QName(kDefaultNs, kHeightAttr)),
&height)) {
return NULL;
}
ScreenResolution resolution(width, height);
if (!resolution.IsValid()) {
return NULL;
}
*config->mutable_initial_resolution() = resolution;
// Parse authentication information.
scoped_refptr<net::X509Certificate> certificate;
std::string auth_token;
std::string master_key;
child = element->FirstNamed(QName(kChromotingXmlNamespace,
kAuthenticationTag));
if (child) {
// Parse the certificate.
const XmlElement* cert_tag =
child->FirstNamed(QName(kChromotingXmlNamespace, kCertificateTag));
if (cert_tag) {
std::string base64_cert = cert_tag->BodyText();
std::string der_cert;
if (!base::Base64Decode(base64_cert, &der_cert)) {
LOG(ERROR) << "Failed to decode certificate received from the peer.";
return NULL;
}
certificate = net::X509Certificate::CreateFromBytes(der_cert.data(),
der_cert.length());
if (!certificate) {
LOG(ERROR) << "Failed to create platform-specific certificate handle";
return NULL;
}
}
// Parse master-key.
const XmlElement* master_key_tag =
child->FirstNamed(QName(kChromotingXmlNamespace, kMasterKeyTag));
if (master_key_tag) {
if (!base::Base64Decode(master_key_tag->BodyText(), &master_key)) {
LOG(ERROR) << "Failed to decode master-key received from the peer.";
return NULL;
}
master_key = master_key_tag->BodyText();
}
// Parse auth-token.
const XmlElement* auth_token_tag =
child->FirstNamed(QName(kChromotingXmlNamespace, kAuthTokenTag));
if (auth_token_tag) {
auth_token = auth_token_tag->BodyText();
}
}
return new ContentDescription(config.release(), auth_token, master_key,
certificate);
}
LOG(ERROR) << "Invalid description: " << element->Str();
return NULL;
}
} // namespace protocol
} // namespace remoting
|