OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ipc/brokerable_attachment.h" | 5 #include "ipc/brokerable_attachment.h" |
6 | 6 |
7 #include "ipc/attachment_broker.h" | 7 #include "ipc/attachment_broker.h" |
8 | 8 |
9 #if USE_ATTACHMENT_BROKER | |
10 #include "crypto/random.h" | |
11 #endif | |
12 | |
13 namespace IPC { | 9 namespace IPC { |
14 | 10 |
15 | |
16 #if USE_ATTACHMENT_BROKER | |
17 BrokerableAttachment::AttachmentId::AttachmentId() { | |
18 // In order to prevent mutually untrusted processes from stealing resources from | |
19 // one another, the nonce must be secret. This generates a 128-bit, | |
20 // cryptographicaly-strong random number. | |
21 crypto::RandBytes(nonce, BrokerableAttachment::kNonceSize); | |
22 } | |
23 #else | |
24 BrokerableAttachment::AttachmentId::AttachmentId() { | |
25 CHECK(false) << "Not allowed to construct an attachment id if the platform " | |
26 "does not support attachment brokering."; | |
27 } | |
28 #endif | |
29 | |
30 BrokerableAttachment::AttachmentId::AttachmentId(const char* start_address, | |
31 size_t size) { | |
32 DCHECK(size == BrokerableAttachment::kNonceSize); | |
33 for (size_t i = 0; i < BrokerableAttachment::kNonceSize; ++i) | |
34 nonce[i] = start_address[i]; | |
35 } | |
36 | |
37 void BrokerableAttachment::AttachmentId::SerializeToBuffer(char* start_address, | |
38 size_t size) { | |
39 DCHECK(size == BrokerableAttachment::kNonceSize); | |
40 for (size_t i = 0; i < BrokerableAttachment::kNonceSize; ++i) | |
41 start_address[i] = nonce[i]; | |
42 } | |
43 | |
44 BrokerableAttachment::BrokerableAttachment() | |
45 : needs_brokering_(false) {} | |
46 | |
47 BrokerableAttachment::BrokerableAttachment(const AttachmentId& id, | 11 BrokerableAttachment::BrokerableAttachment(const AttachmentId& id, |
48 bool needs_brokering) | 12 bool needs_brokering) |
49 : id_(id), needs_brokering_(needs_brokering) {} | 13 : id_(id), needs_brokering_(needs_brokering) {} |
50 | 14 |
51 BrokerableAttachment::~BrokerableAttachment() { | 15 BrokerableAttachment::~BrokerableAttachment() { |
52 } | 16 } |
53 | 17 |
54 BrokerableAttachment::AttachmentId BrokerableAttachment::GetIdentifier() const { | 18 BrokerableAttachment::AttachmentId BrokerableAttachment::GetIdentifier() const { |
55 return id_; | 19 return id_; |
56 } | 20 } |
57 | 21 |
58 bool BrokerableAttachment::NeedsBrokering() const { | 22 bool BrokerableAttachment::NeedsBrokering() const { |
59 return needs_brokering_; | 23 return needs_brokering_; |
60 } | 24 } |
61 | 25 |
62 void BrokerableAttachment::SetNeedsBrokering(bool needs_brokering) { | 26 void BrokerableAttachment::SetNeedsBrokering(bool needs_brokering) { |
63 needs_brokering_ = needs_brokering; | 27 needs_brokering_ = needs_brokering; |
64 } | 28 } |
65 | 29 |
66 BrokerableAttachment::Type BrokerableAttachment::GetType() const { | 30 BrokerableAttachment::Type BrokerableAttachment::GetType() const { |
67 return TYPE_BROKERABLE_ATTACHMENT; | 31 return TYPE_BROKERABLE_ATTACHMENT; |
68 } | 32 } |
69 | 33 |
70 } // namespace IPC | 34 } // namespace IPC |
OLD | NEW |