@capgo/capacitor-updater 8.52.1 → 8.53.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (25) hide show
  1. package/README.md +198 -66
  2. package/android/build.gradle +2 -1
  3. package/android/src/main/java/ee/forgr/capacitor_updater/CapacitorUpdaterPlugin.java +414 -150
  4. package/android/src/main/java/ee/forgr/capacitor_updater/CapgoUpdater.java +693 -217
  5. package/android/src/main/java/ee/forgr/capacitor_updater/CryptoCipher.java +2 -1
  6. package/android/src/main/java/ee/forgr/capacitor_updater/DelayUpdateUtils.java +226 -105
  7. package/android/src/main/java/ee/forgr/capacitor_updater/DownloadService.java +30 -10
  8. package/android/src/main/java/ee/forgr/capacitor_updater/HeadlessUpdateWorker.java +324 -0
  9. package/android/src/main/java/ee/forgr/capacitor_updater/InternalUtils.java +34 -0
  10. package/android/src/main/java/ee/forgr/capacitor_updater/NativeUpdatePrompt.java +303 -0
  11. package/android/src/main/java/ee/forgr/capacitor_updater/ShakeMenu.java +4 -4
  12. package/android/src/main/java/ee/forgr/capacitor_updater/Version.java +1 -1
  13. package/dist/docs.json +378 -27
  14. package/dist/esm/definitions.d.ts +206 -7
  15. package/dist/esm/definitions.js.map +1 -1
  16. package/dist/plugin.cjs.js.map +1 -1
  17. package/dist/plugin.js.map +1 -1
  18. package/ios/Sources/CapacitorUpdaterPlugin/AES.swift +104 -52
  19. package/ios/Sources/CapacitorUpdaterPlugin/CapacitorUpdaterPlugin.swift +360 -104
  20. package/ios/Sources/CapacitorUpdaterPlugin/CapgoUpdater.swift +166 -10
  21. package/ios/Sources/CapacitorUpdaterPlugin/CryptoCipher.swift +2 -1
  22. package/ios/Sources/CapacitorUpdaterPlugin/DelayUpdateUtils.swift +202 -90
  23. package/ios/Sources/CapacitorUpdaterPlugin/NativeUpdatePrompt.swift +183 -0
  24. package/ios/Sources/CapacitorUpdaterPlugin/ShakeMenu.swift +5 -2
  25. package/package.json +7 -3
@@ -8,7 +8,25 @@ import Darwin
8
8
  ///
9
9
  private enum AESConstants {
10
10
  static let aesAlgorithm: CCAlgorithm = CCAlgorithm(kCCAlgorithmAES)
11
- static let aesOptions: CCOptions = CCOptions(kCCOptionPKCS7Padding)
11
+ // CommonCrypto's PKCS7 mode accepts corrupted padding and ciphertext that is not block aligned,
12
+ // so decryption runs without padding and PKCS#7 is checked and removed by `strictPkcs7PaddingLength`.
13
+ static let aesOptions: CCOptions = CCOptions(0)
14
+ static let blockSize = kCCBlockSizeAES128
15
+ }
16
+
17
+ /// Returns the PKCS#7 padding length of the final plaintext block, or nil when the padding is invalid.
18
+ func strictPkcs7PaddingLength(lastBlock: [UInt8]) -> Int? {
19
+ guard lastBlock.count == AESConstants.blockSize, let padding = lastBlock.last else {
20
+ return nil
21
+ }
22
+ let length = Int(padding)
23
+ guard length >= 1 && length <= AESConstants.blockSize else {
24
+ return nil
25
+ }
26
+ for byte in lastBlock.suffix(length) where byte != padding {
27
+ return nil
28
+ }
29
+ return length
12
30
  }
13
31
 
14
32
  // We do all this stuff because ios is shit and open source libraries allow to do decryption with public key
@@ -39,54 +57,65 @@ public struct AES128Key {
39
57
  /// and provide it `ivData` for the initialization vector. Will use cipher block chaining (CBC) as
40
58
  /// the mode of operation.
41
59
  ///
42
- /// Returns the decrypted data.
60
+ /// Returns the decrypted data, or nil when the ciphertext is not block aligned or the PKCS#7 padding is invalid.
43
61
  ///
44
62
  public func decrypt(data: Data) -> Data? {
45
- let encryptedData: UnsafePointer<UInt8> = (data as NSData).bytes.bindMemory(
46
- to: UInt8.self, capacity: data.count)
47
- let encryptedDataLength: Int = data.count
48
-
49
- if let result: NSMutableData = NSMutableData(length: encryptedDataLength) {
50
- let keyData: UnsafePointer<UInt8> = (self.aes128Key as NSData).bytes.bindMemory(
51
- to: UInt8.self, capacity: self.aes128Key.count)
52
- let keyLength: size_t = size_t(self.aes128Key.count)
53
- let ivData: UnsafePointer<UInt8> = (initVector as NSData).bytes.bindMemory(
54
- to: UInt8.self, capacity: self.initVector.count)
55
-
56
- let decryptedData: UnsafeMutablePointer<UInt8> = UnsafeMutablePointer<UInt8>(
57
- result.mutableBytes.assumingMemoryBound(to: UInt8.self))
58
- let decryptedDataLength: size_t = size_t(result.length)
59
-
60
- var decryptedLength: size_t = 0
61
-
62
- let status: CCCryptorStatus = CCCrypt(
63
- CCOperation(kCCDecrypt),
64
- AESConstants.aesAlgorithm,
65
- AESConstants.aesOptions,
66
- keyData,
67
- keyLength,
68
- ivData,
69
- encryptedData,
70
- encryptedDataLength,
71
- decryptedData,
72
- decryptedDataLength,
73
- &decryptedLength)
74
-
75
- if Int32(status) == Int32(kCCSuccess) {
76
- result.length = Int(decryptedLength)
77
- return result as Data
78
- } else {
79
- logger.error("AES decryption failed with status: \(status)")
80
- return nil
81
- }
82
- } else {
83
- logger.error("Failed to allocate memory for AES decryption")
63
+ guard let plain = decryptBlocks(data) else {
64
+ return nil
65
+ }
66
+ guard let padding = strictPkcs7PaddingLength(lastBlock: Array(plain.suffix(AESConstants.blockSize))) else {
67
+ logger.error("AES decryption failed: invalid padding")
84
68
  return nil
85
69
  }
70
+ return plain.prefix(plain.count - padding)
86
71
  }
87
72
 
88
- /// AES-CBC file-to-file. Never holds the whole ciphertext in RAM.
89
- func decrypt(from source: URL, to destination: URL) throws {
73
+ /// AES-CBC decryption of whole blocks, padding left in place. Nil when `data` is empty or not block aligned.
74
+ func decryptBlocks(_ data: Data) -> Data? {
75
+ guard !data.isEmpty, data.count % AESConstants.blockSize == 0 else {
76
+ logger.error("AES ciphertext is not block aligned")
77
+ return nil
78
+ }
79
+ var output = [UInt8](repeating: 0, count: data.count)
80
+ var decryptedLength: size_t = 0
81
+ let input = [UInt8](data)
82
+ let keyBytes = [UInt8](aes128Key)
83
+ let ivBytes = [UInt8](initVector)
84
+ let status: CCCryptorStatus = CCCrypt(
85
+ CCOperation(kCCDecrypt),
86
+ AESConstants.aesAlgorithm,
87
+ AESConstants.aesOptions,
88
+ keyBytes,
89
+ keyBytes.count,
90
+ ivBytes,
91
+ input,
92
+ input.count,
93
+ &output,
94
+ output.count,
95
+ &decryptedLength
96
+ )
97
+ guard status == kCCSuccess else {
98
+ logger.error("AES decryption failed with status: \(status)")
99
+ return nil
100
+ }
101
+ return Data(output.prefix(decryptedLength))
102
+ }
103
+
104
+ /// Rejects empty ciphertext and ciphertext that is not a whole number of AES blocks.
105
+ private func requireBlockAlignedCiphertext(at source: URL) throws {
106
+ let attributes = try FileManager.default.attributesOfItem(atPath: source.path)
107
+ let sourceSize = (attributes[.size] as? NSNumber)?.intValue ?? 0
108
+ guard sourceSize > 0, sourceSize % AESConstants.blockSize == 0 else {
109
+ logger.error("AES ciphertext is not block aligned")
110
+ throw NSError(
111
+ domain: "AESDecryptError",
112
+ code: Int(kCCAlignmentError),
113
+ userInfo: [NSLocalizedDescriptionKey: "AES ciphertext is not block aligned"]
114
+ )
115
+ }
116
+ }
117
+
118
+ private func makeDecryptor() throws -> CCCryptorRef {
90
119
  var cryptor: CCCryptorRef?
91
120
  let createStatus: CCCryptorStatus = aes128Key.withUnsafeBytes { keyBytes in
92
121
  initVector.withUnsafeBytes { ivBytes in
@@ -108,6 +137,13 @@ public struct AES128Key {
108
137
  logger.error("Failed to create AES cryptor")
109
138
  throw NSError(domain: "AESDecryptError", code: Int(createStatus), userInfo: nil)
110
139
  }
140
+ return cryptor
141
+ }
142
+
143
+ /// AES-CBC file-to-file. Never holds the whole ciphertext in RAM.
144
+ func decrypt(from source: URL, to destination: URL) throws {
145
+ try requireBlockAlignedCiphertext(at: source)
146
+ let cryptor = try makeDecryptor()
111
147
  defer {
112
148
  CCCryptorRelease(cryptor)
113
149
  }
@@ -134,15 +170,30 @@ public struct AES128Key {
134
170
  var inBuf = [UInt8](repeating: 0, count: bufferSize)
135
171
  var outBuf = [UInt8](repeating: 0, count: outBufSize)
136
172
  let outFd = output.fileDescriptor
173
+ // The last decrypted block carries the padding, so it is held back until the end.
174
+ var heldBlock: [UInt8] = []
175
+ func emit(count: Int) throws {
176
+ guard count > 0 else {
177
+ return
178
+ }
179
+ if !heldBlock.isEmpty {
180
+ try Self.writeAll(fd: outFd, buffer: &heldBlock, count: heldBlock.count)
181
+ }
182
+ let writeCount = count - AESConstants.blockSize
183
+ if writeCount > 0 {
184
+ try Self.writeAll(fd: outFd, buffer: &outBuf, count: writeCount)
185
+ }
186
+ heldBlock = Array(outBuf[writeCount..<count])
187
+ }
137
188
 
138
189
  while true {
139
- let n = inBuf.withUnsafeMutableBufferPointer { ptr in
190
+ let readCount = inBuf.withUnsafeMutableBufferPointer { ptr in
140
191
  input.read(ptr.baseAddress!, maxLength: ptr.count)
141
192
  }
142
- if n == 0 {
193
+ if readCount == 0 {
143
194
  break
144
195
  }
145
- if n < 0 {
196
+ if readCount < 0 {
146
197
  throw input.streamError ?? NSError(domain: "AESDecryptError", code: 2, userInfo: [NSLocalizedDescriptionKey: "AES stream read failed"])
147
198
  }
148
199
  var moved: size_t = 0
@@ -151,7 +202,7 @@ public struct AES128Key {
151
202
  CCCryptorUpdate(
152
203
  cryptor,
153
204
  inRaw.baseAddress,
154
- n,
205
+ readCount,
155
206
  outRaw.baseAddress,
156
207
  outBufSize,
157
208
  &moved
@@ -162,9 +213,7 @@ public struct AES128Key {
162
213
  logger.error("AES stream update failed")
163
214
  throw NSError(domain: "AESDecryptError", code: Int(status), userInfo: nil)
164
215
  }
165
- if moved > 0 {
166
- try Self.writeAll(fd: outFd, buffer: &outBuf, count: moved)
167
- }
216
+ try emit(count: moved)
168
217
  }
169
218
 
170
219
  var moved: size_t = 0
@@ -175,9 +224,12 @@ public struct AES128Key {
175
224
  logger.error("AES stream finalize failed")
176
225
  throw NSError(domain: "AESDecryptError", code: Int(finalStatus), userInfo: nil)
177
226
  }
178
- if moved > 0 {
179
- try Self.writeAll(fd: outFd, buffer: &outBuf, count: moved)
227
+ try emit(count: moved)
228
+ guard let padding = strictPkcs7PaddingLength(lastBlock: heldBlock) else {
229
+ logger.error("AES decryption failed: invalid padding")
230
+ throw NSError(domain: "AESDecryptError", code: Int(kCCDecodeError), userInfo: [NSLocalizedDescriptionKey: "Invalid AES padding"])
180
231
  }
232
+ try Self.writeAll(fd: outFd, buffer: &heldBlock, count: heldBlock.count - padding)
181
233
  try output.close()
182
234
 
183
235
  let decryptedSize = (try fileManager.attributesOfItem(atPath: tempURL.path)[.size] as? NSNumber)?.uint64Value ?? 0