Skip to content

Commit edf2b59

Browse files
committed
Export the created firmware file (instead of flashing directly)
Running swift-format
1 parent cf5103f commit edf2b59

File tree

23 files changed

+1360
-1108
lines changed

23 files changed

+1360
-1108
lines changed

CVE-2020-9986/OFReadKeys/OFFetchReports/AppDelegate.swift

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,30 +13,30 @@ import SwiftUI
1313
@main
1414
class AppDelegate: NSObject, NSApplicationDelegate {
1515

16-
var window: NSWindow!
17-
18-
func applicationDidFinishLaunching(_ aNotification: Notification) {
19-
// Create the SwiftUI view that provides the window contents.
20-
let contentView = OFFetchReportsMainView()
21-
22-
// Create the window and set the content view.
23-
window = NSWindow(
24-
contentRect: NSRect(x: 0, y: 0, width: 480, height: 300),
25-
styleMask: [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView],
26-
backing: .buffered, defer: false)
27-
window.isReleasedWhenClosed = false
28-
window.center()
29-
window.setFrameAutosaveName("Main Window")
30-
window.contentView = NSHostingView(rootView: contentView)
31-
window.makeKeyAndOrderFront(nil)
32-
}
33-
34-
func applicationWillTerminate(_ aNotification: Notification) {
35-
// Insert code here to tear down your application
36-
}
37-
38-
func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
39-
return true
40-
}
16+
var window: NSWindow!
17+
18+
func applicationDidFinishLaunching(_ aNotification: Notification) {
19+
// Create the SwiftUI view that provides the window contents.
20+
let contentView = OFFetchReportsMainView()
21+
22+
// Create the window and set the content view.
23+
window = NSWindow(
24+
contentRect: NSRect(x: 0, y: 0, width: 480, height: 300),
25+
styleMask: [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView],
26+
backing: .buffered, defer: false)
27+
window.isReleasedWhenClosed = false
28+
window.center()
29+
window.setFrameAutosaveName("Main Window")
30+
window.contentView = NSHostingView(rootView: contentView)
31+
window.makeKeyAndOrderFront(nil)
32+
}
33+
34+
func applicationWillTerminate(_ aNotification: Notification) {
35+
// Insert code here to tear down your application
36+
}
37+
38+
func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
39+
return true
40+
}
4141

4242
}

CVE-2020-9986/OFReadKeys/OFFetchReports/ContentView.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@
1010
import SwiftUI
1111

1212
struct ContentView: View {
13-
var body: some View {
14-
Text("Hello, World!")
15-
.frame(maxWidth: .infinity, maxHeight: .infinity)
16-
}
13+
var body: some View {
14+
Text("Hello, World!")
15+
.frame(maxWidth: .infinity, maxHeight: .infinity)
16+
}
1717
}
1818

1919
struct ContentView_Previews: PreviewProvider {
20-
static var previews: some View {
21-
ContentView()
22-
}
20+
static var previews: some View {
21+
ContentView()
22+
}
2323
}

CVE-2020-9986/OFReadKeys/OFFetchReports/FindMy/DecryptReports.swift

Lines changed: 76 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -7,93 +7,100 @@
77
// SPDX-License-Identifier: AGPL-3.0-only
88
//
99

10-
import Foundation
1110
import CryptoKit
11+
import Foundation
1212

1313
struct DecryptReports {
1414

15-
/// Decrypt a find my report with the according key
16-
/// - Parameters:
17-
/// - report: An encrypted FindMy Report
18-
/// - key: A FindMyKey
19-
/// - Throws: Errors if the decryption fails
20-
/// - Returns: An decrypted location report
21-
static func decrypt(report: FindMyReport, with key: FindMyKey) throws -> FindMyLocationReport {
22-
let payloadData = report.payload
23-
let keyData = key.privateKey
15+
/// Decrypt a find my report with the according key
16+
/// - Parameters:
17+
/// - report: An encrypted FindMy Report
18+
/// - key: A FindMyKey
19+
/// - Throws: Errors if the decryption fails
20+
/// - Returns: An decrypted location report
21+
static func decrypt(report: FindMyReport, with key: FindMyKey) throws -> FindMyLocationReport {
22+
let payloadData = report.payload
23+
let keyData = key.privateKey
24+
25+
let privateKey = keyData
26+
let ephemeralKey = payloadData.subdata(in: 5..<62)
27+
28+
guard
29+
let sharedKey = BoringSSL.deriveSharedKey(
30+
fromPrivateKey: privateKey,
31+
andEphemeralKey: ephemeralKey)
32+
else {
33+
throw FindMyError.decryptionError(description: "Failed generating shared key")
34+
}
2435

25-
let privateKey = keyData
26-
let ephemeralKey = payloadData.subdata(in: 5..<62)
36+
let derivedKey = self.kdf(fromSharedSecret: sharedKey, andEphemeralKey: ephemeralKey)
2737

28-
guard let sharedKey = BoringSSL.deriveSharedKey(
29-
fromPrivateKey: privateKey,
30-
andEphemeralKey: ephemeralKey) else {
31-
throw FindMyError.decryptionError(description: "Failed generating shared key")
32-
}
38+
print("Derived key \(derivedKey.base64EncodedString())")
3339

34-
let derivedKey = self.kdf(fromSharedSecret: sharedKey, andEphemeralKey: ephemeralKey)
40+
let encData = payloadData.subdata(in: 62..<72)
41+
let tag = payloadData.subdata(in: 72..<payloadData.endIndex)
3542

36-
print("Derived key \(derivedKey.base64EncodedString())")
43+
let decryptedContent = try self.decryptPayload(
44+
payload: encData, symmetricKey: derivedKey, tag: tag)
45+
let locationReport = self.decode(content: decryptedContent, report: report)
46+
print(locationReport)
47+
return locationReport
48+
}
3749

38-
let encData = payloadData.subdata(in: 62..<72)
39-
let tag = payloadData.subdata(in: 72..<payloadData.endIndex)
50+
/// Decrypt the payload
51+
/// - Parameters:
52+
/// - payload: Encrypted payload part
53+
/// - symmetricKey: Symmetric key
54+
/// - tag: AES GCM tag
55+
/// - Throws: AES GCM error
56+
/// - Returns: Decrypted error
57+
static func decryptPayload(payload: Data, symmetricKey: Data, tag: Data) throws -> Data {
58+
let decryptionKey = symmetricKey.subdata(in: 0..<16)
59+
let iv = symmetricKey.subdata(in: 16..<symmetricKey.endIndex)
4060

41-
let decryptedContent = try self.decryptPayload(payload: encData, symmetricKey: derivedKey, tag: tag)
42-
let locationReport = self.decode(content: decryptedContent, report: report)
43-
print(locationReport)
44-
return locationReport
45-
}
61+
print("Decryption Key \(decryptionKey.base64EncodedString())")
62+
print("IV \(iv.base64EncodedString())")
4663

47-
/// Decrypt the payload
48-
/// - Parameters:
49-
/// - payload: Encrypted payload part
50-
/// - symmetricKey: Symmetric key
51-
/// - tag: AES GCM tag
52-
/// - Throws: AES GCM error
53-
/// - Returns: Decrypted error
54-
static func decryptPayload(payload: Data, symmetricKey: Data, tag: Data) throws -> Data {
55-
let decryptionKey = symmetricKey.subdata(in: 0..<16)
56-
let iv = symmetricKey.subdata(in: 16..<symmetricKey.endIndex)
57-
58-
print("Decryption Key \(decryptionKey.base64EncodedString())")
59-
print("IV \(iv.base64EncodedString())")
60-
61-
let sealedBox = try AES.GCM.SealedBox(nonce: AES.GCM.Nonce(data: iv), ciphertext: payload, tag: tag)
62-
let symKey = SymmetricKey(data: decryptionKey)
63-
let decrypted = try AES.GCM.open(sealedBox, using: symKey)
64-
65-
return decrypted
66-
}
64+
let sealedBox = try AES.GCM.SealedBox(
65+
nonce: AES.GCM.Nonce(data: iv), ciphertext: payload, tag: tag)
66+
let symKey = SymmetricKey(data: decryptionKey)
67+
let decrypted = try AES.GCM.open(sealedBox, using: symKey)
6768

68-
static func decode(content: Data, report: FindMyReport) -> FindMyLocationReport {
69-
var longitude: Int32 = 0
70-
_ = withUnsafeMutableBytes(of: &longitude, {content.subdata(in: 4..<8).copyBytes(to: $0)})
71-
longitude = Int32(bigEndian: longitude)
69+
return decrypted
70+
}
7271

73-
var latitude: Int32 = 0
74-
_ = withUnsafeMutableBytes(of: &latitude, {content.subdata(in: 0..<4).copyBytes(to: $0)})
75-
latitude = Int32(bigEndian: latitude)
72+
static func decode(content: Data, report: FindMyReport) -> FindMyLocationReport {
73+
var longitude: Int32 = 0
74+
_ = withUnsafeMutableBytes(of: &longitude, { content.subdata(in: 4..<8).copyBytes(to: $0) })
75+
longitude = Int32(bigEndian: longitude)
7676

77-
var accuracy: UInt8 = 0
78-
_ = withUnsafeMutableBytes(of: &accuracy, {content.subdata(in: 8..<9).copyBytes(to: $0)})
77+
var latitude: Int32 = 0
78+
_ = withUnsafeMutableBytes(of: &latitude, { content.subdata(in: 0..<4).copyBytes(to: $0) })
79+
latitude = Int32(bigEndian: latitude)
7980

80-
let latitudeDec = Double(latitude)/10000000.0
81-
let longitudeDec = Double(longitude)/10000000.0
81+
var accuracy: UInt8 = 0
82+
_ = withUnsafeMutableBytes(of: &accuracy, { content.subdata(in: 8..<9).copyBytes(to: $0) })
8283

83-
return FindMyLocationReport(lat: latitudeDec, lng: longitudeDec, acc: accuracy, dP: report.datePublished, t: report.timestamp, c: report.confidence)
84-
}
84+
let latitudeDec = Double(latitude) / 10000000.0
85+
let longitudeDec = Double(longitude) / 10000000.0
8586

86-
static func kdf(fromSharedSecret secret: Data, andEphemeralKey ephKey: Data) -> Data {
87+
return FindMyLocationReport(
88+
lat: latitudeDec, lng: longitudeDec, acc: accuracy, dP: report.datePublished,
89+
t: report.timestamp, c: report.confidence)
90+
}
8791

88-
var shaDigest = SHA256()
89-
shaDigest.update(data: secret)
90-
var counter: Int32 = 1
91-
let counterData = Data(Data(bytes: &counter, count: MemoryLayout.size(ofValue: counter)).reversed())
92-
shaDigest.update(data: counterData)
93-
shaDigest.update(data: ephKey)
92+
static func kdf(fromSharedSecret secret: Data, andEphemeralKey ephKey: Data) -> Data {
9493

95-
let derivedKey = shaDigest.finalize()
94+
var shaDigest = SHA256()
95+
shaDigest.update(data: secret)
96+
var counter: Int32 = 1
97+
let counterData = Data(
98+
Data(bytes: &counter, count: MemoryLayout.size(ofValue: counter)).reversed())
99+
shaDigest.update(data: counterData)
100+
shaDigest.update(data: ephKey)
96101

97-
return Data(derivedKey)
98-
}
102+
let derivedKey = shaDigest.finalize()
103+
104+
return Data(derivedKey)
105+
}
99106
}

0 commit comments

Comments
 (0)