archive_mac.mm 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // Copyright (c) 2021 Slack Technologies, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #include "shell/common/asar/archive.h"
  5. #include <CommonCrypto/CommonDigest.h>
  6. #include <CoreFoundation/CoreFoundation.h>
  7. #include <Foundation/Foundation.h>
  8. #include <iomanip>
  9. #include <string>
  10. #include "base/logging.h"
  11. #include "base/mac/bundle_locations.h"
  12. #include "base/mac/foundation_util.h"
  13. #include "base/mac/scoped_cftyperef.h"
  14. #include "base/strings/sys_string_conversions.h"
  15. #include "shell/common/asar/asar_util.h"
  16. namespace asar {
  17. absl::optional<base::FilePath> Archive::RelativePath() const {
  18. base::FilePath bundle_path = base::mac::MainBundlePath().Append("Contents");
  19. base::FilePath relative_path;
  20. if (!bundle_path.AppendRelativePath(path_, &relative_path))
  21. return absl::nullopt;
  22. return relative_path;
  23. }
  24. absl::optional<IntegrityPayload> Archive::HeaderIntegrity() const {
  25. absl::optional<base::FilePath> relative_path = RelativePath();
  26. // Callers should have already asserted this
  27. CHECK(relative_path.has_value());
  28. NSDictionary* integrity = [[NSBundle mainBundle]
  29. objectForInfoDictionaryKey:@"ElectronAsarIntegrity"];
  30. // Integrity not provided
  31. if (!integrity)
  32. return absl::nullopt;
  33. NSString* ns_relative_path =
  34. base::mac::FilePathToNSString(relative_path.value());
  35. NSDictionary* integrity_payload = [integrity objectForKey:ns_relative_path];
  36. if (!integrity_payload)
  37. return absl::nullopt;
  38. NSString* algorithm = [integrity_payload objectForKey:@"algorithm"];
  39. NSString* hash = [integrity_payload objectForKey:@"hash"];
  40. if (algorithm && hash && [algorithm isEqualToString:@"SHA256"]) {
  41. IntegrityPayload header_integrity;
  42. header_integrity.algorithm = HashAlgorithm::SHA256;
  43. header_integrity.hash = base::SysNSStringToUTF8(hash);
  44. return header_integrity;
  45. }
  46. return absl::nullopt;
  47. }
  48. } // namespace asar