OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #import "remoting/host/mac/me2me_preference_pane.h" | 5 #import "remoting/host/mac/me2me_preference_pane.h" |
6 | 6 |
7 #import <Cocoa/Cocoa.h> | 7 #import <Cocoa/Cocoa.h> |
8 #include <CommonCrypto/CommonHMAC.h> | 8 #include <CommonCrypto/CommonHMAC.h> |
9 #include <errno.h> | 9 #include <errno.h> |
10 #include <launch.h> | 10 #include <launch.h> |
11 #import <PreferencePanes/PreferencePanes.h> | 11 #import <PreferencePanes/PreferencePanes.h> |
12 #import <SecurityInterface/SFAuthorizationView.h> | 12 #import <SecurityInterface/SFAuthorizationView.h> |
13 #include <stddef.h> | 13 #include <stddef.h> |
14 #include <stdlib.h> | 14 #include <stdlib.h> |
15 #include <unistd.h> | 15 #include <unistd.h> |
16 | 16 |
17 #include <fstream> | 17 #include <fstream> |
18 | 18 |
19 #include "base/mac/scoped_launch_data.h" | 19 #include "base/mac/scoped_launch_data.h" |
20 #include "base/memory/scoped_ptr.h" | 20 #include "base/memory/scoped_ptr.h" |
21 #include "base/posix/eintr_wrapper.h" | 21 #include "base/posix/eintr_wrapper.h" |
22 #include "remoting/host/constants_mac.h" | 22 #include "remoting/host/constants_mac.h" |
23 #include "remoting/host/host_config.h" | 23 #include "remoting/host/host_config.h" |
24 #include "remoting/host/pin_hash.h" | |
25 #import "remoting/host/mac/me2me_preference_pane_confirm_pin.h" | 24 #import "remoting/host/mac/me2me_preference_pane_confirm_pin.h" |
26 #import "remoting/host/mac/me2me_preference_pane_disable.h" | 25 #import "remoting/host/mac/me2me_preference_pane_disable.h" |
27 #include "third_party/jsoncpp/source/include/json/reader.h" | 26 #include "third_party/jsoncpp/source/include/json/reader.h" |
28 #include "third_party/jsoncpp/source/include/json/writer.h" | 27 #include "third_party/jsoncpp/source/include/json/writer.h" |
| 28 #include "third_party/modp_b64/modp_b64.h" |
29 | 29 |
30 namespace { | 30 namespace { |
31 | 31 |
32 bool GetTemporaryConfigFilePath(std::string* path) { | 32 bool GetTemporaryConfigFilePath(std::string* path) { |
33 NSString* filename = NSTemporaryDirectory(); | 33 NSString* filename = NSTemporaryDirectory(); |
34 if (filename == nil) | 34 if (filename == nil) |
35 return false; | 35 return false; |
36 | 36 |
37 *path = [[NSString stringWithFormat:@"%@/%s", | 37 *path = [[NSString stringWithFormat:@"%@/%s", |
38 filename, remoting::kHostConfigFileName] UTF8String]; | 38 filename, remoting::kHostConfigFileName] UTF8String]; |
39 return true; | 39 return true; |
40 } | 40 } |
41 | 41 |
42 bool IsConfigValid(const remoting::JsonHostConfig* config) { | 42 bool IsConfigValid(const remoting::JsonHostConfig* config) { |
43 std::string value; | 43 std::string value; |
44 return (config->GetString(remoting::kHostIdConfigPath, &value) && | 44 return (config->GetString(remoting::kHostIdConfigPath, &value) && |
45 config->GetString(remoting::kHostSecretHashConfigPath, &value) && | 45 config->GetString(remoting::kHostSecretHashConfigPath, &value) && |
46 config->GetString(remoting::kXmppLoginConfigPath, &value)); | 46 config->GetString(remoting::kXmppLoginConfigPath, &value)); |
47 } | 47 } |
48 | 48 |
| 49 bool IsPinValid(const std::string& pin, const std::string& host_id, |
| 50 const std::string& host_secret_hash) { |
| 51 // TODO(lambroslambrou): Once the "base" target supports building for 64-bit |
| 52 // on Mac OS X, remove this code and replace it with |VerifyHostPinHash()| |
| 53 // from host/pin_hash.h. |
| 54 size_t separator = host_secret_hash.find(':'); |
| 55 if (separator == std::string::npos) |
| 56 return false; |
| 57 |
| 58 std::string method = host_secret_hash.substr(0, separator); |
| 59 if (method != "hmac") { |
| 60 NSLog(@"Authentication method '%s' not supported", method.c_str()); |
| 61 return false; |
| 62 } |
| 63 |
| 64 std::string hash_base64 = host_secret_hash.substr(separator + 1); |
| 65 |
| 66 // Convert |hash_base64| to |hash|, based on code from base/base64.cc. |
| 67 int hash_base64_size = static_cast<int>(hash_base64.size()); |
| 68 std::string hash; |
| 69 hash.resize(modp_b64_decode_len(hash_base64_size)); |
| 70 |
| 71 // modp_b64_decode_len() returns at least 1, so hash[0] is safe here. |
| 72 int hash_size = modp_b64_decode(&(hash[0]), hash_base64.data(), |
| 73 hash_base64_size); |
| 74 if (hash_size < 0) { |
| 75 NSLog(@"Failed to parse host_secret_hash"); |
| 76 return false; |
| 77 } |
| 78 hash.resize(hash_size); |
| 79 |
| 80 std::string computed_hash; |
| 81 computed_hash.resize(CC_SHA256_DIGEST_LENGTH); |
| 82 |
| 83 CCHmac(kCCHmacAlgSHA256, |
| 84 host_id.data(), host_id.size(), |
| 85 pin.data(), pin.size(), |
| 86 &(computed_hash[0])); |
| 87 |
| 88 // Normally, a constant-time comparison function would be used, but it is |
| 89 // unnecessary here as the "secret" is already readable by the user |
| 90 // supplying input to this routine. |
| 91 return computed_hash == hash; |
| 92 } |
| 93 |
49 } // namespace | 94 } // namespace |
50 | 95 |
51 // These methods are copied from base/mac, but with the logging changed to use | 96 // These methods are copied from base/mac, but with the logging changed to use |
52 // NSLog(). | 97 // NSLog(). |
53 // | 98 // |
54 // TODO(lambroslambrou): Once the "base" target supports building for 64-bit | 99 // TODO(lambroslambrou): Once the "base" target supports building for 64-bit |
55 // on Mac OS X, remove these implementations and use the ones in base/mac. | 100 // on Mac OS X, remove these implementations and use the ones in base/mac. |
56 namespace base { | 101 namespace base { |
57 namespace mac { | 102 namespace mac { |
58 | 103 |
(...skipping 230 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
289 | 334 |
290 std::string pin_utf8 = [pin UTF8String]; | 335 std::string pin_utf8 = [pin UTF8String]; |
291 std::string host_id, host_secret_hash; | 336 std::string host_id, host_secret_hash; |
292 bool result = (config_->GetString(remoting::kHostIdConfigPath, &host_id) && | 337 bool result = (config_->GetString(remoting::kHostIdConfigPath, &host_id) && |
293 config_->GetString(remoting::kHostSecretHashConfigPath, | 338 config_->GetString(remoting::kHostSecretHashConfigPath, |
294 &host_secret_hash)); | 339 &host_secret_hash)); |
295 if (!result) { | 340 if (!result) { |
296 [self showError]; | 341 [self showError]; |
297 return; | 342 return; |
298 } | 343 } |
299 if (!VerifyHostPinHash(pin_utf8, host_id, host_secret_hash)) { | 344 if (!IsPinValid(pin_utf8, host_id, host_secret_hash)) { |
300 [self showIncorrectPinMessage]; | 345 [self showIncorrectPinMessage]; |
301 return; | 346 return; |
302 } | 347 } |
303 | 348 |
304 [self applyNewServiceConfig]; | 349 [self applyNewServiceConfig]; |
305 [self updateUI]; | 350 [self updateUI]; |
306 } | 351 } |
307 | 352 |
308 - (void)onDisable:(id)sender { | 353 - (void)onDisable:(id)sender { |
309 // Ensure the authorization token is up-to-date before using it. | 354 // Ensure the authorization token is up-to-date before using it. |
(...skipping 399 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
709 NSArray* arguments = [NSArray arrayWithObjects:@"--relaunch-prefpane", nil]; | 754 NSArray* arguments = [NSArray arrayWithObjects:@"--relaunch-prefpane", nil]; |
710 [task setLaunchPath:command]; | 755 [task setLaunchPath:command]; |
711 [task setArguments:arguments]; | 756 [task setArguments:arguments]; |
712 [task setStandardInput:[NSPipe pipe]]; | 757 [task setStandardInput:[NSPipe pipe]]; |
713 [task launch]; | 758 [task launch]; |
714 [task release]; | 759 [task release]; |
715 [NSApp terminate:nil]; | 760 [NSApp terminate:nil]; |
716 } | 761 } |
717 | 762 |
718 @end | 763 @end |
OLD | NEW |