erikchen | 151b2f9 | 2015-06-16 20:20:51 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "ipc/brokerable_attachment.h" |
| 6 | |
erikchen | eece6c3 | 2015-07-07 22:13:11 | [diff] [blame] | 7 | #include "crypto/random.h" |
| 8 | |
erikchen | 151b2f9 | 2015-06-16 20:20:51 | [diff] [blame] | 9 | namespace IPC { |
| 10 | |
erikchen | 06faf0c | 2015-08-27 19:49:58 | [diff] [blame^] | 11 | namespace { |
erikchen | eece6c3 | 2015-07-07 22:13:11 | [diff] [blame] | 12 | |
erikchen | 06faf0c | 2015-08-27 19:49:58 | [diff] [blame^] | 13 | // In order to prevent mutually untrusted processes from stealing resources from |
| 14 | // one another, the nonce must be secret. This generates a 128-bit, |
| 15 | // cryptographicaly-strong random number. |
| 16 | BrokerableAttachment::AttachmentId GetRandomId() { |
| 17 | BrokerableAttachment::AttachmentId id; |
| 18 | crypto::RandBytes(id.nonce, BrokerableAttachment::kNonceSize); |
| 19 | return id; |
erikchen | eece6c3 | 2015-07-07 22:13:11 | [diff] [blame] | 20 | } |
| 21 | |
erikchen | 06faf0c | 2015-08-27 19:49:58 | [diff] [blame^] | 22 | } // namespace |
| 23 | |
| 24 | BrokerableAttachment::BrokerableAttachment() |
| 25 | : id_(GetRandomId()), needs_brokering_(false) {} |
| 26 | |
| 27 | BrokerableAttachment::BrokerableAttachment(const AttachmentId& id, |
| 28 | bool needs_brokering) |
| 29 | : id_(id), needs_brokering_(needs_brokering) {} |
| 30 | |
| 31 | BrokerableAttachment::~BrokerableAttachment() { |
erikchen | 151b2f9 | 2015-06-16 20:20:51 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | BrokerableAttachment::AttachmentId BrokerableAttachment::GetIdentifier() const { |
| 35 | return id_; |
| 36 | } |
| 37 | |
erikchen | de9412b8 | 2015-07-27 18:26:14 | [diff] [blame] | 38 | bool BrokerableAttachment::NeedsBrokering() const { |
erikchen | 06faf0c | 2015-08-27 19:49:58 | [diff] [blame^] | 39 | return needs_brokering_; |
| 40 | } |
| 41 | |
| 42 | void BrokerableAttachment::SetNeedsBrokering(bool needs_brokering) { |
| 43 | needs_brokering_ = needs_brokering; |
erikchen | de9412b8 | 2015-07-27 18:26:14 | [diff] [blame] | 44 | } |
| 45 | |
erikchen | eece6c3 | 2015-07-07 22:13:11 | [diff] [blame] | 46 | BrokerableAttachment::Type BrokerableAttachment::GetType() const { |
| 47 | return TYPE_BROKERABLE_ATTACHMENT; |
| 48 | } |
| 49 | |
erikchen | 151b2f9 | 2015-06-16 20:20:51 | [diff] [blame] | 50 | } // namespace IPC |