-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved things around so relevant components are easier to locate
Some minor function renaming
- Loading branch information
1 parent
101dab3
commit 1cdcbbc
Showing
14 changed files
with
656 additions
and
539 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// | ||
// Data+additions.swift | ||
// Outset | ||
// | ||
// Created by Bart E Reardon on 5/9/2023. | ||
// | ||
|
||
import Foundation | ||
import CommonCrypto | ||
|
||
extension Data { | ||
// extension to the Data class that lets us compute sha256 | ||
func sha256() -> Data { | ||
var hash = [UInt8](repeating: 0, count: Int(CC_SHA256_DIGEST_LENGTH)) | ||
self.withUnsafeBytes { | ||
_ = CC_SHA256($0.baseAddress, CC_LONG(count), &hash) | ||
} | ||
return Data(hash) | ||
} | ||
|
||
func hexEncodedString() -> String { | ||
return map { String(format: "%02hhx", $0) }.joined() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// | ||
// String+additions.swift | ||
// Outset | ||
// | ||
// Created by Bart E Reardon on 5/9/2023. | ||
// | ||
|
||
import Foundation | ||
|
||
extension String { | ||
func camelCaseToUnderscored() -> String { | ||
let regex = try? NSRegularExpression(pattern: "([a-z])([A-Z])", options: []) | ||
let range = NSRange(location: 0, length: utf16.count) | ||
return regex?.stringByReplacingMatches(in: self, options: [], range: range, withTemplate: "$1_$2").lowercased() ?? self | ||
} | ||
} | ||
|
||
func getValueForKey(_ key: String, inArray array: [String: String]) -> String? { | ||
// short function that treats a [String: String] as a key value pair. | ||
return array[key] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// | ||
// URL+additions.swift | ||
// Outset | ||
// | ||
// Created by Bart E Reardon on 5/9/2023. | ||
// | ||
|
||
import Foundation | ||
|
||
extension URL { | ||
var isDirectory: Bool { | ||
(try? resourceValues(forKeys: [.isDirectoryKey]))?.isDirectory == true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
// | ||
// Checksum.swift | ||
// Outset | ||
// | ||
// Created by Bart E Reardon on 5/9/2023. | ||
// | ||
|
||
import Foundation | ||
|
||
struct FileHashes: Codable { | ||
var sha256sum: [String: String] = [String: String]() | ||
} | ||
|
||
func checksumLoadApprovedFiles() -> [String: String] { | ||
// imports the list of file hashes that are approved to run | ||
var outsetFileHashList = FileHashes() | ||
|
||
let defaults = UserDefaults.standard | ||
let hashes = defaults.object(forKey: "sha256sum") | ||
|
||
if let data = hashes as? [String: String] { | ||
for (key, value) in data { | ||
outsetFileHashList.sha256sum[key] = value | ||
} | ||
} | ||
|
||
return outsetFileHashList.sha256sum | ||
} | ||
|
||
func verifySHASUMForFile(filename: String, shasumArray: [String: String]) -> Bool { | ||
// Verify that the file | ||
var proceed = false | ||
let errorMessage = "no required hash or file hash mismatch for: \(filename). Skipping" | ||
writeLog("checking hash for \(filename)", logLevel: .debug) | ||
let url = URL(fileURLWithPath: filename) | ||
if let fileHash = sha256(for: url) { | ||
writeLog("file hash : \(fileHash)", logLevel: .debug) | ||
if let storedHash = getValueForKey(filename, inArray: shasumArray) { | ||
writeLog("required hash : \(storedHash)", logLevel: .debug) | ||
if storedHash == fileHash { | ||
proceed = true | ||
} | ||
} | ||
} | ||
if !proceed { | ||
writeLog(errorMessage, logLevel: .error) | ||
} | ||
|
||
return proceed | ||
} | ||
|
||
func sha256(for url: URL) -> String? { | ||
// computes a sha256sum for the specified file path and returns a string | ||
do { | ||
let fileData = try Data(contentsOf: url) | ||
let sha256 = fileData.sha256() | ||
return sha256.hexEncodedString() | ||
} catch { | ||
return nil | ||
} | ||
} | ||
|
||
func checksumAllFiles() { | ||
// compute checksum (SHA256) for all files in the outset directory | ||
// returns data in two formats to stdout: | ||
// plaintext | ||
// as plist format ready for import into an MDM or converting to a .mobileconfig | ||
|
||
let url = URL(fileURLWithPath: outsetDirectory) | ||
writeLog("CHECKSUM", logLevel: .info) | ||
var shasumPlist = FileHashes() | ||
if let enumerator = FileManager.default.enumerator(at: url, includingPropertiesForKeys: [.isRegularFileKey], options: [.skipsHiddenFiles, .skipsPackageDescendants]) { | ||
for case let fileURL as URL in enumerator { | ||
do { | ||
let fileAttributes = try fileURL.resourceValues(forKeys: [.isRegularFileKey]) | ||
if fileAttributes.isRegularFile! && fileURL.pathExtension != "plist" && fileURL.lastPathComponent != "outset" { | ||
if let shasum = sha256(for: fileURL) { | ||
print("\(fileURL.relativePath) : \(shasum)") | ||
shasumPlist.sha256sum[fileURL.relativePath] = shasum | ||
} | ||
} | ||
} catch { print(error, fileURL) } | ||
} | ||
|
||
writeLog("PLIST", logLevel: .info) | ||
let encoder = PropertyListEncoder() | ||
encoder.outputFormat = .xml | ||
do { | ||
let data = try encoder.encode(shasumPlist) | ||
if let plist = try PropertyListSerialization.propertyList(from: data, options: [], format: nil) as? [String: Any] { | ||
let formatted = try PropertyListSerialization.data(fromPropertyList: plist, format: .xml, options: 0) | ||
if let string = String(data: formatted, encoding: .utf8) { | ||
print(string) | ||
} | ||
} | ||
} catch { | ||
writeLog("plist encoding failed", logLevel: .error) | ||
} | ||
} | ||
} |
Oops, something went wrong.