archive_mac.mm 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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/apple/bundle_locations.h"
  11. #include "base/apple/foundation_util.h"
  12. #include "base/apple/scoped_cftyperef.h"
  13. #include "base/files/file_util.h"
  14. #include "base/strings/sys_string_conversions.h"
  15. #include "shell/common/asar/asar_util.h"
  16. namespace asar {
  17. std::optional<base::FilePath> Archive::RelativePath() const {
  18. base::FilePath bundle_path = base::MakeAbsoluteFilePath(
  19. base::apple::MainBundlePath().Append("Contents"));
  20. base::FilePath relative_path;
  21. if (!bundle_path.AppendRelativePath(path_, &relative_path))
  22. return std::nullopt;
  23. return relative_path;
  24. }
  25. std::optional<IntegrityPayload> Archive::HeaderIntegrity() const {
  26. std::optional<base::FilePath> relative_path = RelativePath();
  27. // Callers should have already asserted this
  28. CHECK(relative_path.has_value());
  29. NSDictionary* integrity = [[NSBundle mainBundle]
  30. objectForInfoDictionaryKey:@"ElectronAsarIntegrity"];
  31. // Integrity not provided
  32. if (!integrity)
  33. return std::nullopt;
  34. NSString* ns_relative_path =
  35. base::apple::FilePathToNSString(relative_path.value());
  36. NSDictionary* integrity_payload = [integrity objectForKey:ns_relative_path];
  37. if (!integrity_payload)
  38. return std::nullopt;
  39. NSString* algorithm = [integrity_payload objectForKey:@"algorithm"];
  40. NSString* hash = [integrity_payload objectForKey:@"hash"];
  41. if (algorithm && hash && [algorithm isEqualToString:@"SHA256"]) {
  42. IntegrityPayload header_integrity;
  43. header_integrity.algorithm = HashAlgorithm::kSHA256;
  44. header_integrity.hash = base::SysNSStringToUTF8(hash);
  45. return header_integrity;
  46. }
  47. return std::nullopt;
  48. }
  49. } // namespace asar