2021-09-09 21:49:01 +00:00
|
|
|
// Copyright (c) 2021 Slack Technologies, Inc.
|
|
|
|
// Use of this source code is governed by the MIT license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
#include "shell/common/asar/archive.h"
|
|
|
|
|
|
|
|
#include <CommonCrypto/CommonDigest.h>
|
|
|
|
#include <CoreFoundation/CoreFoundation.h>
|
|
|
|
#include <Foundation/Foundation.h>
|
|
|
|
|
|
|
|
#include <iomanip>
|
|
|
|
#include <string>
|
|
|
|
|
2023-05-23 19:58:58 +00:00
|
|
|
#include "base/apple/bundle_locations.h"
|
2022-06-28 23:02:00 +00:00
|
|
|
#include "base/files/file_util.h"
|
2021-09-09 21:49:01 +00:00
|
|
|
#include "base/logging.h"
|
|
|
|
#include "base/mac/foundation_util.h"
|
|
|
|
#include "base/mac/scoped_cftyperef.h"
|
|
|
|
#include "base/strings/sys_string_conversions.h"
|
|
|
|
#include "shell/common/asar/asar_util.h"
|
|
|
|
|
|
|
|
namespace asar {
|
|
|
|
|
|
|
|
absl::optional<base::FilePath> Archive::RelativePath() const {
|
2022-06-28 23:02:00 +00:00
|
|
|
base::FilePath bundle_path = base::MakeAbsoluteFilePath(
|
2023-05-23 19:58:58 +00:00
|
|
|
base::apple::MainBundlePath().Append("Contents"));
|
2021-09-09 21:49:01 +00:00
|
|
|
|
|
|
|
base::FilePath relative_path;
|
|
|
|
if (!bundle_path.AppendRelativePath(path_, &relative_path))
|
|
|
|
return absl::nullopt;
|
|
|
|
|
|
|
|
return relative_path;
|
|
|
|
}
|
|
|
|
|
|
|
|
absl::optional<IntegrityPayload> Archive::HeaderIntegrity() const {
|
|
|
|
absl::optional<base::FilePath> relative_path = RelativePath();
|
|
|
|
// Callers should have already asserted this
|
|
|
|
CHECK(relative_path.has_value());
|
|
|
|
|
|
|
|
NSDictionary* integrity = [[NSBundle mainBundle]
|
|
|
|
objectForInfoDictionaryKey:@"ElectronAsarIntegrity"];
|
|
|
|
|
|
|
|
// Integrity not provided
|
|
|
|
if (!integrity)
|
|
|
|
return absl::nullopt;
|
|
|
|
|
|
|
|
NSString* ns_relative_path =
|
|
|
|
base::mac::FilePathToNSString(relative_path.value());
|
|
|
|
|
|
|
|
NSDictionary* integrity_payload = [integrity objectForKey:ns_relative_path];
|
|
|
|
|
|
|
|
if (!integrity_payload)
|
|
|
|
return absl::nullopt;
|
|
|
|
|
|
|
|
NSString* algorithm = [integrity_payload objectForKey:@"algorithm"];
|
|
|
|
NSString* hash = [integrity_payload objectForKey:@"hash"];
|
|
|
|
if (algorithm && hash && [algorithm isEqualToString:@"SHA256"]) {
|
|
|
|
IntegrityPayload header_integrity;
|
2023-05-12 13:23:42 +00:00
|
|
|
header_integrity.algorithm = HashAlgorithm::kSHA256;
|
2021-09-09 21:49:01 +00:00
|
|
|
header_integrity.hash = base::SysNSStringToUTF8(hash);
|
|
|
|
return header_integrity;
|
|
|
|
}
|
|
|
|
|
|
|
|
return absl::nullopt;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace asar
|