| 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" | |
| 8 | |
| 9 #if USE_ATTACHMENT_BROKER | |
| 10 #include "crypto/random.h" | 7 #include "crypto/random.h" |
| 11 #endif | |
| 12 | 8 |
| 13 namespace IPC { | 9 namespace IPC { |
| 14 | 10 |
| 15 #if USE_ATTACHMENT_BROKER | 11 namespace { |
| 16 BrokerableAttachment::AttachmentId::AttachmentId() { | |
| 17 // In order to prevent mutually untrusted processes from stealing resources | |
| 18 // from one another, the nonce must be secret. This generates a 128-bit, | |
| 19 // cryptographicaly-strong random number. | |
| 20 crypto::RandBytes(nonce, BrokerableAttachment::kNonceSize); | |
| 21 } | |
| 22 #else | |
| 23 BrokerableAttachment::AttachmentId::AttachmentId() { | |
| 24 CHECK(false) << "Not allowed to construct an attachment id if the platform " | |
| 25 "does not support attachment brokering."; | |
| 26 } | |
| 27 #endif | |
| 28 | 12 |
| 29 BrokerableAttachment::AttachmentId::AttachmentId(const char* start_address, | 13 // In order to prevent mutually untrusted processes from stealing resources from |
| 30 size_t size) { | 14 // one another, the nonce must be secret. This generates a 128-bit, |
| 31 DCHECK(size == BrokerableAttachment::kNonceSize); | 15 // cryptographicaly-strong random number. |
| 32 for (size_t i = 0; i < BrokerableAttachment::kNonceSize; ++i) | 16 BrokerableAttachment::AttachmentId GetRandomId() { |
| 33 nonce[i] = start_address[i]; | 17 BrokerableAttachment::AttachmentId id; |
| 18 crypto::RandBytes(id.nonce, BrokerableAttachment::kNonceSize); |
| 19 return id; |
| 34 } | 20 } |
| 35 | 21 |
| 36 void BrokerableAttachment::AttachmentId::SerializeToBuffer(char* start_address, | 22 } // namespace |
| 37 size_t size) { | 23 |
| 38 DCHECK(size == BrokerableAttachment::kNonceSize); | 24 BrokerableAttachment::BrokerableAttachment() |
| 39 for (size_t i = 0; i < BrokerableAttachment::kNonceSize; ++i) | 25 : id_(GetRandomId()), needs_brokering_(false) {} |
| 40 start_address[i] = nonce[i]; | 26 |
| 27 BrokerableAttachment::BrokerableAttachment(const AttachmentId& id, |
| 28 bool needs_brokering) |
| 29 : id_(id), needs_brokering_(needs_brokering) {} |
| 30 |
| 31 BrokerableAttachment::~BrokerableAttachment() { |
| 41 } | 32 } |
| 42 | 33 |
| 43 BrokerableAttachment::BrokerableAttachment() {} | |
| 44 | |
| 45 BrokerableAttachment::BrokerableAttachment(const AttachmentId& id) : id_(id) {} | |
| 46 | |
| 47 BrokerableAttachment::~BrokerableAttachment() {} | |
| 48 | |
| 49 BrokerableAttachment::AttachmentId BrokerableAttachment::GetIdentifier() const { | 34 BrokerableAttachment::AttachmentId BrokerableAttachment::GetIdentifier() const { |
| 50 return id_; | 35 return id_; |
| 51 } | 36 } |
| 52 | 37 |
| 53 bool BrokerableAttachment::NeedsBrokering() const { | 38 bool BrokerableAttachment::NeedsBrokering() const { |
| 54 return GetBrokerableType() == PLACEHOLDER; | 39 return needs_brokering_; |
| 40 } |
| 41 |
| 42 void BrokerableAttachment::SetNeedsBrokering(bool needs_brokering) { |
| 43 needs_brokering_ = needs_brokering; |
| 55 } | 44 } |
| 56 | 45 |
| 57 BrokerableAttachment::Type BrokerableAttachment::GetType() const { | 46 BrokerableAttachment::Type BrokerableAttachment::GetType() const { |
| 58 return TYPE_BROKERABLE_ATTACHMENT; | 47 return TYPE_BROKERABLE_ATTACHMENT; |
| 59 } | 48 } |
| 60 | 49 |
| 61 #if defined(OS_POSIX) | |
| 62 base::PlatformFile BrokerableAttachment::TakePlatformFile() { | |
| 63 NOTREACHED(); | |
| 64 return base::PlatformFile(); | |
| 65 } | |
| 66 #endif // OS_POSIX | |
| 67 | |
| 68 } // namespace IPC | 50 } // namespace IPC |
| OLD | NEW |