Vitaly Buka | 4615e0d | 2015-10-14 15:35:12 -0700 | [diff] [blame] | 1 | // Copyright 2015 The Weave Authors. All rights reserved. |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Vitaly Buka | 912b698 | 2015-07-06 11:13:03 -0700 | [diff] [blame] | 5 | #ifndef LIBWEAVE_SRC_PRIVET_SECURITY_MANAGER_H_ |
| 6 | #define LIBWEAVE_SRC_PRIVET_SECURITY_MANAGER_H_ |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 7 | |
| 8 | #include <map> |
| 9 | #include <memory> |
| 10 | #include <set> |
| 11 | #include <string> |
| 12 | #include <vector> |
| 13 | |
| 14 | #include <base/callback.h> |
Vitaly Buka | 0d50107 | 2015-08-18 18:09:46 -0700 | [diff] [blame] | 15 | #include <base/gtest_prod_util.h> |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 16 | #include <base/memory/weak_ptr.h> |
Vitaly Buka | 0801a1f | 2015-08-14 10:03:46 -0700 | [diff] [blame] | 17 | #include <weave/error.h> |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 18 | |
Stefan Sauer | 2d16dfa | 2015-09-25 17:08:35 +0200 | [diff] [blame] | 19 | #include "src/privet/security_delegate.h" |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 20 | |
| 21 | namespace crypto { |
| 22 | class P224EncryptedKeyExchange; |
| 23 | } // namespace crypto |
| 24 | |
Vitaly Buka | b6f015a | 2015-07-09 14:59:23 -0700 | [diff] [blame] | 25 | namespace weave { |
Vitaly Buka | 823fdda | 2015-08-13 00:33:00 -0700 | [diff] [blame] | 26 | |
Vitaly Buka | 1e36367 | 2015-09-25 14:01:16 -0700 | [diff] [blame] | 27 | namespace provider { |
Vitaly Buka | 823fdda | 2015-08-13 00:33:00 -0700 | [diff] [blame] | 28 | class TaskRunner; |
Vitaly Buka | 1e36367 | 2015-09-25 14:01:16 -0700 | [diff] [blame] | 29 | } |
Vitaly Buka | 823fdda | 2015-08-13 00:33:00 -0700 | [diff] [blame] | 30 | |
Vitaly Buka | b6f015a | 2015-07-09 14:59:23 -0700 | [diff] [blame] | 31 | namespace privet { |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 32 | |
Vitaly Buka | f08caeb | 2015-12-02 13:47:48 -0800 | [diff] [blame] | 33 | class AuthManager; |
| 34 | |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 35 | class SecurityManager : public SecurityDelegate { |
| 36 | public: |
| 37 | using PairingStartListener = |
| 38 | base::Callback<void(const std::string& session_id, |
| 39 | PairingType pairing_type, |
| 40 | const std::vector<uint8_t>& code)>; |
| 41 | using PairingEndListener = |
| 42 | base::Callback<void(const std::string& session_id)>; |
| 43 | |
| 44 | class KeyExchanger { |
| 45 | public: |
Vitaly Buka | 3bfb13d | 2015-11-24 14:46:13 -0800 | [diff] [blame] | 46 | virtual ~KeyExchanger() {} |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 47 | |
| 48 | virtual const std::string& GetMessage() = 0; |
| 49 | virtual bool ProcessMessage(const std::string& message, |
Vitaly Buka | 0801a1f | 2015-08-14 10:03:46 -0700 | [diff] [blame] | 50 | ErrorPtr* error) = 0; |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 51 | virtual const std::string& GetKey() const = 0; |
| 52 | }; |
| 53 | |
Vitaly Buka | f08caeb | 2015-12-02 13:47:48 -0800 | [diff] [blame] | 54 | SecurityManager(AuthManager* auth_manager, |
Vitaly Buka | 8589b05 | 2015-09-29 00:46:14 -0700 | [diff] [blame] | 55 | const std::set<PairingType>& pairing_modes, |
Vitaly Buka | 8cb91d7 | 2015-08-16 00:40:51 -0700 | [diff] [blame] | 56 | const std::string& embedded_code, |
| 57 | bool disable_security, |
Vitaly Buka | 8589b05 | 2015-09-29 00:46:14 -0700 | [diff] [blame] | 58 | // TODO(vitalybuka): Remove task_runner. |
Vitaly Buka | 1e36367 | 2015-09-25 14:01:16 -0700 | [diff] [blame] | 59 | provider::TaskRunner* task_runner); |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 60 | ~SecurityManager() override; |
| 61 | |
| 62 | // SecurityDelegate methods |
| 63 | std::string CreateAccessToken(const UserInfo& user_info, |
| 64 | const base::Time& time) override; |
| 65 | UserInfo ParseAccessToken(const std::string& token, |
| 66 | base::Time* time) const override; |
| 67 | std::set<PairingType> GetPairingTypes() const override; |
| 68 | std::set<CryptoType> GetCryptoTypes() const override; |
| 69 | bool IsValidPairingCode(const std::string& auth_code) const override; |
| 70 | |
| 71 | bool StartPairing(PairingType mode, |
| 72 | CryptoType crypto, |
| 73 | std::string* session_id, |
| 74 | std::string* device_commitment, |
Vitaly Buka | 0801a1f | 2015-08-14 10:03:46 -0700 | [diff] [blame] | 75 | ErrorPtr* error) override; |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 76 | |
| 77 | bool ConfirmPairing(const std::string& session_id, |
| 78 | const std::string& client_commitment, |
| 79 | std::string* fingerprint, |
| 80 | std::string* signature, |
Vitaly Buka | 0801a1f | 2015-08-14 10:03:46 -0700 | [diff] [blame] | 81 | ErrorPtr* error) override; |
| 82 | bool CancelPairing(const std::string& session_id, ErrorPtr* error) override; |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 83 | |
| 84 | void RegisterPairingListeners(const PairingStartListener& on_start, |
| 85 | const PairingEndListener& on_end); |
| 86 | |
Vitaly Buka | 3d851b4 | 2015-12-08 16:17:01 -0800 | [diff] [blame] | 87 | const AuthManager* GetAuthManager() const { return auth_manager_; } |
| 88 | |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 89 | private: |
| 90 | FRIEND_TEST_ALL_PREFIXES(SecurityManagerTest, ThrottlePairing); |
| 91 | // Allows limited number of new sessions without successful authorization. |
Vitaly Buka | 0801a1f | 2015-08-14 10:03:46 -0700 | [diff] [blame] | 92 | bool CheckIfPairingAllowed(ErrorPtr* error); |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 93 | bool ClosePendingSession(const std::string& session_id); |
| 94 | bool CloseConfirmedSession(const std::string& session_id); |
| 95 | |
Vitaly Buka | f08caeb | 2015-12-02 13:47:48 -0800 | [diff] [blame] | 96 | AuthManager* auth_manager_{nullptr}; |
| 97 | |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 98 | // If true allows unencrypted pairing and accepts any access code. |
| 99 | bool is_security_disabled_{false}; |
| 100 | std::set<PairingType> pairing_modes_; |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 101 | std::string embedded_code_; |
Vitaly Buka | f9630fb | 2015-08-12 21:15:40 -0700 | [diff] [blame] | 102 | // TODO(vitalybuka): Session cleanup can be done without posting tasks. |
Vitaly Buka | 1e36367 | 2015-09-25 14:01:16 -0700 | [diff] [blame] | 103 | provider::TaskRunner* task_runner_{nullptr}; |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 104 | std::map<std::string, std::unique_ptr<KeyExchanger>> pending_sessions_; |
| 105 | std::map<std::string, std::unique_ptr<KeyExchanger>> confirmed_sessions_; |
| 106 | mutable int pairing_attemts_{0}; |
| 107 | mutable base::Time block_pairing_until_; |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 108 | PairingStartListener on_start_; |
| 109 | PairingEndListener on_end_; |
| 110 | |
| 111 | base::WeakPtrFactory<SecurityManager> weak_ptr_factory_{this}; |
| 112 | |
| 113 | DISALLOW_COPY_AND_ASSIGN(SecurityManager); |
| 114 | }; |
| 115 | |
Vitaly Buka | b6f015a | 2015-07-09 14:59:23 -0700 | [diff] [blame] | 116 | } // namespace privet |
| 117 | } // namespace weave |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 118 | |
Vitaly Buka | 912b698 | 2015-07-06 11:13:03 -0700 | [diff] [blame] | 119 | #endif // LIBWEAVE_SRC_PRIVET_SECURITY_MANAGER_H_ |