Verify code signing in generate_appcast (#2077)

In generate_appcast we:

* Validate code signing integrity of new updates
* Add hidden flag to disable checking for nested code during validation
* Warn if new and old apps have differing signing identities when generating deltas
This commit is contained in:
Zorg authored and GitHub committed 2022-01-23 14:46:52 -08:00
1 parent 89e20a490d
commit 91bccdf0ad
8 files changed
+55 -14

No files matched your search

+7 -1
View File
@@ -12,8 +12,14 @@
#import <Foundation/Foundation.h>
@interface SUCodeSigningVerifier : NSObject
+ (BOOL)codeSignatureAtBundleURL:(NSURL *)oldBundleURL matchesSignatureAtBundleURL:(NSURL *)newBundleURL error:(NSError **)error;
+ (BOOL)codeSignatureIsValidAtBundleURL:(NSURL *)bundleURL error:(NSError **)error;
+ (BOOL)codeSignatureIsValidAtBundleURL:(NSURL *)bundleURL checkNestedCode:(BOOL)checkNestedCode error:(NSError **)error;
// Same as above except does not check for nested code. This method should be used by the framework.
+ (BOOL)codeSignatureIsValidAtBundleURL:(NSURL *)bundleURL error:(NSError *__autoreleasing *)error;
+ (BOOL)bundleAtURLIsCodeSigned:(NSURL *)bundleURL;
@end
+10 -1
View File
@@ -127,6 +127,11 @@ finally:
}
+ (BOOL)codeSignatureIsValidAtBundleURL:(NSURL *)bundleURL error:(NSError *__autoreleasing *)error
{
return [self codeSignatureIsValidAtBundleURL:bundleURL checkNestedCode:NO error:error];
}
+ (BOOL)codeSignatureIsValidAtBundleURL:(NSURL *)bundleURL checkNestedCode:(BOOL)checkNestedCode error:(NSError *__autoreleasing *)error
{
OSStatus result;
SecStaticCodeRef staticCode = NULL;
@@ -143,8 +148,12 @@ finally:
goto finally;
}
// See in -codeSignatureAtBundleURL:matchesSignatureAtBundleURL:error: for why kSecCSCheckNestedCode is not passed
// See in -codeSignatureAtBundleURL:matchesSignatureAtBundleURL:error: for why kSecCSCheckNestedCode is not always passed
SecCSFlags flags = (SecCSFlags) (kSecCSDefaultFlags | kSecCSCheckAllArchitectures);
if (checkNestedCode) {
flags |= kSecCSCheckNestedCode;
}
result = SecStaticCodeCheckValidityWithErrors(staticCode, flags, NULL, &cfError);
if (result != errSecSuccess) {
+2
View File
@@ -340,6 +340,7 @@
7293A1AE1CEE933800B957A7 /* SPUURLRequest.h in Headers */ = {isa = PBXBuildFile; fileRef = 7293A1AC1CEE933800B957A7 /* SPUURLRequest.h */; };
7293A1AF1CEE933800B957A7 /* SPUURLRequest.m in Sources */ = {isa = PBXBuildFile; fileRef = 7293A1AD1CEE933800B957A7 /* SPUURLRequest.m */; };
7293A1B11CEE9B9F00B957A7 /* SPUURLRequest.m in Sources */ = {isa = PBXBuildFile; fileRef = 7293A1AD1CEE933800B957A7 /* SPUURLRequest.m */; };
729743AB279D1BD2009910B2 /* SUCodeSigningVerifier.m in Sources */ = {isa = PBXBuildFile; fileRef = 7267E5991D3D8A5A00D1BF90 /* SUCodeSigningVerifier.m */; };
729924941DF4A45000DBCDF5 /* SUUpdateValidator.m in Sources */ = {isa = PBXBuildFile; fileRef = 729924931DF4A45000DBCDF5 /* SUUpdateValidator.m */; };
729F7EAC27366353004592DC /* test-links.xml in Resources */ = {isa = PBXBuildFile; fileRef = 729F7EAB27366353004592DC /* test-links.xml */; };
729F7EAF273F1840004592DC /* SPUUserAgent+Private.h in Headers */ = {isa = PBXBuildFile; fileRef = 729F7EAD273F1840004592DC /* SPUUserAgent+Private.h */; settings = {ATTRIBUTES = (Private, ); }; };
@@ -3290,6 +3291,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
729743AB279D1BD2009910B2 /* SUCodeSigningVerifier.m in Sources */,
725EE489277D39B400D820CE /* SPUDeltaArchive.m in Sources */,
725EE48A277D39B400D820CE /* SPUXarDeltaArchive.m in Sources */,
721D5B1B25C692BB00D23BEA /* SUFlatPackageUnarchiver.m in Sources */,
+18 -2
View File
@@ -11,10 +11,10 @@ func makeError(code: SUError, _ description: String) -> NSError {
])
}
func makeAppcast(archivesSourceDir: URL, cacheDirectory cacheDir: URL, keys: PrivateKeys, versions: Set<String>?, maximumDeltas: Int, deltaCompressionModeDescription: String, deltaCompressionLevel: UInt8, verbose: Bool) throws -> [String: [ArchiveItem]] {
func makeAppcast(archivesSourceDir: URL, cacheDirectory cacheDir: URL, keys: PrivateKeys, versions: Set<String>?, maximumDeltas: Int, deltaCompressionModeDescription: String, deltaCompressionLevel: UInt8, disableNestedCodeCheck: Bool, verbose: Bool) throws -> [String: [ArchiveItem]] {
let comparator = SUStandardVersionComparator()
let allUpdates = (try unarchiveUpdates(archivesSourceDir: archivesSourceDir, archivesDestDir: cacheDir, verbose: verbose))
let allUpdates = (try unarchiveUpdates(archivesSourceDir: archivesSourceDir, archivesDestDir: cacheDir, disableNestedCodeCheck: disableNestedCodeCheck, verbose: verbose))
.sorted(by: {
.orderedDescending == comparator.compareVersion($0.version, toVersion: $1.version)
})
@@ -108,6 +108,22 @@ func makeAppcast(archivesSourceDir: URL, cacheDirectory cacheDir: URL, keys: Pri
continue
}
if !fm.fileExists(atPath: deltaPath.path) {
// Test if old and new app have the same code signing signature. If not, omit a warning.
// This is a good time to do this check because our delta handling code sets a marker
// to avoid this path each time generate_appcast is called.
let oldAppCodeSigned = SUCodeSigningVerifier.bundle(atURLIsCodeSigned: item.appPath)
let newAppCodeSigned = SUCodeSigningVerifier.bundle(atURLIsCodeSigned: latestItem.appPath)
if oldAppCodeSigned != newAppCodeSigned && !newAppCodeSigned {
print("Warning: New app is not code signed but older version (\(item)) is: \(latestItem)")
} else if oldAppCodeSigned && newAppCodeSigned {
do {
try SUCodeSigningVerifier.codeSignature(atBundleURL: item.appPath, matchesSignatureAtBundleURL: latestItem.appPath)
} catch {
print("Warning: found mismatch code signing identity between \(item) and \(latestItem)")
}
}
do {
// Decide the most appropriate delta version
let deltaVersion: SUBinaryDeltaMajorVersion
+7 -1
View File
@@ -85,7 +85,7 @@ class ArchiveItem: CustomStringConvertible {
self.deltas = []
}
convenience init(fromArchive archivePath: URL, unarchivedDir: URL) throws {
convenience init(fromArchive archivePath: URL, unarchivedDir: URL, validateBundle: Bool, disableNestedCodeCheck: Bool) throws {
let resourceKeys = [URLResourceKey.typeIdentifierKey]
let items = try FileManager.default.contentsOfDirectory(at: unarchivedDir, includingPropertiesForKeys: resourceKeys, options: .skipsHiddenFiles)
@@ -102,6 +102,12 @@ class ArchiveItem: CustomStringConvertible {
}
let appPath = bundles[0]
// If requested to validate the bundle, ensure it is properly signed
if validateBundle && SUCodeSigningVerifier.bundle(atURLIsCodeSigned: appPath) {
try SUCodeSigningVerifier.codeSignatureIsValid(atBundleURL: appPath, checkNestedCode: !disableNestedCodeCheck)
}
guard let infoPlist = NSDictionary(contentsOf: appPath.appendingPathComponent("Contents/Info.plist")) else {
throw makeError(code: .unarchivingError, "No plist \(appPath.path)")
}
+1
View File
@@ -9,5 +9,6 @@
#import "SUBinaryDeltaApply.h"
#import "SUBinaryDeltaCommon.h"
#import "SUSignatures.h"
#import "SUCodeSigningVerifier.h"
#import "SPUInstallationType.h"
#import "ed25519.h" // Run `git submodule update --init` if you get an error here
+6 -8
View File
@@ -43,7 +43,7 @@ func unarchive(itemPath: URL, archiveDestDir: URL, callback: @escaping (Error?)
}
}
func unarchiveUpdates(archivesSourceDir: URL, archivesDestDir: URL, verbose: Bool) throws -> [ArchiveItem] {
func unarchiveUpdates(archivesSourceDir: URL, archivesDestDir: URL, disableNestedCodeCheck: Bool, verbose: Bool) throws -> [ArchiveItem] {
if verbose {
print("Unarchiving to temp directory", archivesDestDir.path)
}
@@ -71,9 +71,9 @@ func unarchiveUpdates(archivesSourceDir: URL, archivesDestDir: URL, verbose: Boo
continue
}
let addItem = {
let addItem = { (validateBundle: Bool) in
do {
let item = try ArchiveItem(fromArchive: itemPath, unarchivedDir: archiveDestDir)
let item = try ArchiveItem(fromArchive: itemPath, unarchivedDir: archiveDestDir, validateBundle: validateBundle, disableNestedCodeCheck: disableNestedCodeCheck)
if verbose {
print("Found archive", item)
}
@@ -81,21 +81,19 @@ func unarchiveUpdates(archivesSourceDir: URL, archivesDestDir: URL, verbose: Boo
unarchived.append(item)
objc_sync_exit(unarchived)
} catch {
if verbose {
print("Skipped", item, error)
}
print("Skipped", item, error)
}
}
if fileManager.fileExists(atPath: archiveDestDir.path) {
addItem()
addItem(false)
} else {
group.enter()
unarchive(itemPath: itemPath, archiveDestDir: archiveDestDir) { (error: Error?) in
if let error = error {
print("Could not unarchive", itemPath.path, error)
} else {
addItem()
addItem(true)
}
group.leave()
}
+4 -1
View File
@@ -129,6 +129,9 @@ struct GenerateAppcast: ParsableCommand {
@Option(name: .customLong("max-cdata-threshold"), help: .hidden)
var maxCDATAThreshold: Int = DEFAULT_MAX_CDATA_THRESHOLD
@Flag(name: .customLong("disable-nested-code-check"), help: .hidden)
var disableNestedCodeCheck: Bool = false
static var configuration = CommandConfiguration(
abstract: "Generate appcast from a directory of Sparkle update archives.",
discussion: """
@@ -217,7 +220,7 @@ struct GenerateAppcast: ParsableCommand {
let keys = loadPrivateKeys(account, privateDSAKey, privateEdString)
do {
let allUpdates = try makeAppcast(archivesSourceDir: archivesSourceDir, cacheDirectory: GenerateAppcast.cacheDirectory, keys: keys, versions: versions, maximumDeltas: maximumDeltas, deltaCompressionModeDescription: deltaCompression, deltaCompressionLevel: deltaCompressionLevel, verbose: verbose)
let allUpdates = try makeAppcast(archivesSourceDir: archivesSourceDir, cacheDirectory: GenerateAppcast.cacheDirectory, keys: keys, versions: versions, maximumDeltas: maximumDeltas, deltaCompressionModeDescription: deltaCompression, deltaCompressionLevel: deltaCompressionLevel, disableNestedCodeCheck: disableNestedCodeCheck, verbose: verbose)
// If a URL prefix was provided, set on the archive items
if downloadURLPrefix != nil || releaseNotesURLPrefix != nil {