@capgo/capacitor-updater 4.42.0 → 4.43.5

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 (67) hide show
  1. package/CapgoCapacitorUpdater.podspec +7 -5
  2. package/Package.swift +40 -0
  3. package/README.md +1913 -303
  4. package/android/build.gradle +41 -8
  5. package/android/proguard-rules.pro +45 -0
  6. package/android/src/main/AndroidManifest.xml +1 -3
  7. package/android/src/main/java/ee/forgr/capacitor_updater/AppLifecycleObserver.java +88 -0
  8. package/android/src/main/java/ee/forgr/capacitor_updater/BundleInfo.java +223 -195
  9. package/android/src/main/java/ee/forgr/capacitor_updater/BundleStatus.java +23 -23
  10. package/android/src/main/java/ee/forgr/capacitor_updater/Callback.java +13 -0
  11. package/android/src/main/java/ee/forgr/capacitor_updater/CapacitorUpdaterPlugin.java +2720 -1242
  12. package/android/src/main/java/ee/forgr/capacitor_updater/CapgoUpdater.java +1854 -0
  13. package/android/src/main/java/ee/forgr/capacitor_updater/CryptoCipher.java +359 -121
  14. package/android/src/main/java/ee/forgr/capacitor_updater/DataManager.java +28 -0
  15. package/android/src/main/java/ee/forgr/capacitor_updater/DelayCondition.java +44 -49
  16. package/android/src/main/java/ee/forgr/capacitor_updater/DelayUntilNext.java +4 -4
  17. package/android/src/main/java/ee/forgr/capacitor_updater/DelayUpdateUtils.java +296 -0
  18. package/android/src/main/java/ee/forgr/capacitor_updater/DeviceIdHelper.java +215 -0
  19. package/android/src/main/java/ee/forgr/capacitor_updater/DownloadService.java +858 -117
  20. package/android/src/main/java/ee/forgr/capacitor_updater/DownloadWorkerManager.java +156 -0
  21. package/android/src/main/java/ee/forgr/capacitor_updater/InternalUtils.java +45 -0
  22. package/android/src/main/java/ee/forgr/capacitor_updater/Logger.java +360 -0
  23. package/android/src/main/java/ee/forgr/capacitor_updater/ShakeDetector.java +72 -0
  24. package/android/src/main/java/ee/forgr/capacitor_updater/ShakeMenu.java +603 -0
  25. package/dist/docs.json +3022 -765
  26. package/dist/esm/definitions.d.ts +1717 -198
  27. package/dist/esm/definitions.js +103 -1
  28. package/dist/esm/definitions.js.map +1 -1
  29. package/dist/esm/history.d.ts +1 -0
  30. package/dist/esm/history.js +283 -0
  31. package/dist/esm/history.js.map +1 -0
  32. package/dist/esm/index.d.ts +3 -2
  33. package/dist/esm/index.js +5 -4
  34. package/dist/esm/index.js.map +1 -1
  35. package/dist/esm/web.d.ts +43 -42
  36. package/dist/esm/web.js +122 -37
  37. package/dist/esm/web.js.map +1 -1
  38. package/dist/plugin.cjs.js +512 -37
  39. package/dist/plugin.cjs.js.map +1 -1
  40. package/dist/plugin.js +512 -37
  41. package/dist/plugin.js.map +1 -1
  42. package/ios/Sources/CapacitorUpdaterPlugin/AES.swift +87 -0
  43. package/ios/Sources/CapacitorUpdaterPlugin/BigInt.swift +55 -0
  44. package/ios/Sources/CapacitorUpdaterPlugin/BundleInfo.swift +177 -0
  45. package/ios/{Plugin → Sources/CapacitorUpdaterPlugin}/BundleStatus.swift +12 -12
  46. package/ios/Sources/CapacitorUpdaterPlugin/CapacitorUpdaterPlugin.swift +2020 -0
  47. package/ios/Sources/CapacitorUpdaterPlugin/CapgoUpdater.swift +1959 -0
  48. package/ios/Sources/CapacitorUpdaterPlugin/CryptoCipher.swift +313 -0
  49. package/ios/Sources/CapacitorUpdaterPlugin/DelayUpdateUtils.swift +257 -0
  50. package/ios/Sources/CapacitorUpdaterPlugin/DeviceIdHelper.swift +120 -0
  51. package/ios/Sources/CapacitorUpdaterPlugin/InternalUtils.swift +392 -0
  52. package/ios/Sources/CapacitorUpdaterPlugin/Logger.swift +310 -0
  53. package/ios/Sources/CapacitorUpdaterPlugin/RSA.swift +274 -0
  54. package/ios/Sources/CapacitorUpdaterPlugin/ShakeMenu.swift +441 -0
  55. package/ios/{Plugin → Sources/CapacitorUpdaterPlugin}/UserDefaultsExtension.swift +1 -2
  56. package/package.json +49 -41
  57. package/android/src/main/java/ee/forgr/capacitor_updater/CapacitorUpdater.java +0 -1131
  58. package/ios/Plugin/BundleInfo.swift +0 -113
  59. package/ios/Plugin/CapacitorUpdater.swift +0 -850
  60. package/ios/Plugin/CapacitorUpdaterPlugin.h +0 -10
  61. package/ios/Plugin/CapacitorUpdaterPlugin.m +0 -27
  62. package/ios/Plugin/CapacitorUpdaterPlugin.swift +0 -678
  63. package/ios/Plugin/CryptoCipher.swift +0 -240
  64. /package/{LICENCE → LICENSE} +0 -0
  65. /package/ios/{Plugin → Sources/CapacitorUpdaterPlugin}/DelayCondition.swift +0 -0
  66. /package/ios/{Plugin → Sources/CapacitorUpdaterPlugin}/DelayUntilNext.swift +0 -0
  67. /package/ios/{Plugin → Sources/CapacitorUpdaterPlugin}/Info.plist +0 -0
@@ -0,0 +1,274 @@
1
+ import BigInt
2
+ import Foundation
3
+ import CommonCrypto
4
+ import CryptoKit
5
+
6
+ ///
7
+ /// Constants
8
+ ///
9
+ private enum RSAConstants {
10
+ static let rsaKeySizeInBits: NSNumber = 2048
11
+ static let rsaAlgorithm: SecKeyAlgorithm = .rsaEncryptionOAEPSHA256
12
+ }
13
+
14
+ // We do all this stuff because ios is shit and open source libraries allow to do decryption with public key
15
+ // So we have to do it manually, while in nodejs or Java it's ok and done at language level.
16
+
17
+ ///
18
+ /// The RSA public key.
19
+ ///
20
+ public struct RSAPublicKey {
21
+ private let manualKey: ManualRSAPublicKey
22
+
23
+ fileprivate init(manualKey: ManualRSAPublicKey) {
24
+ self.manualKey = manualKey
25
+ }
26
+
27
+ ///
28
+ /// Takes the data and uses the public key to decrypt it.
29
+ /// Returns the decrypted data.
30
+ ///
31
+ public func decrypt(data: Data) -> Data? {
32
+ return manualKey.decrypt(data)
33
+ }
34
+
35
+ ///
36
+ /// Allows you to load an RSA public key (i.e. one downloaded from the net).
37
+ ///
38
+ public static func load(rsaPublicKey: String) -> RSAPublicKey? {
39
+ // Clean up the key string
40
+ var pubKey: String = rsaPublicKey
41
+ pubKey = pubKey.replacingOccurrences(of: "-----BEGIN RSA PUBLIC KEY-----", with: "")
42
+ pubKey = pubKey.replacingOccurrences(of: "-----END RSA PUBLIC KEY-----", with: "")
43
+ pubKey = pubKey.replacingOccurrences(of: "-----BEGIN PUBLIC KEY-----", with: "")
44
+ pubKey = pubKey.replacingOccurrences(of: "-----END PUBLIC KEY-----", with: "")
45
+ pubKey = pubKey.replacingOccurrences(of: "\\n+", with: "", options: .regularExpression)
46
+ pubKey = pubKey.replacingOccurrences(of: "\n", with: "")
47
+ pubKey = pubKey.trimmingCharacters(in: .whitespacesAndNewlines)
48
+
49
+ do {
50
+ guard let rsaPublicKeyData: Data = Data(base64Encoded: String(pubKey)) else {
51
+ throw CustomError.cannotDecode
52
+ }
53
+
54
+ // Try parsing as PKCS#1
55
+ if let manualKey = ManualRSAPublicKey.fromPKCS1(rsaPublicKeyData) {
56
+ return RSAPublicKey(manualKey: manualKey)
57
+ }
58
+
59
+ // Most common public exponent is 65537 (0x010001)
60
+ let commonExponent = Data([0x01, 0x00, 0x01]) // 65537 in big-endian
61
+
62
+ // Assume the entire key data is the modulus
63
+ let lastResortKey = ManualRSAPublicKey(modulus: rsaPublicKeyData, exponent: commonExponent)
64
+ return RSAPublicKey(manualKey: lastResortKey)
65
+ } catch {
66
+ return nil
67
+ }
68
+ }
69
+ }
70
+
71
+ // Manual RSA Public Key Implementation using the BigInt library
72
+ struct ManualRSAPublicKey {
73
+ let modulus: BigInt
74
+ let exponent: BigInt
75
+
76
+ init(modulus: Data, exponent: Data) {
77
+ // Create positive BigInts from Data
78
+ let modulusBytes = [UInt8](modulus)
79
+ var modulusValue = BigUInt(0)
80
+ for byte in modulusBytes {
81
+ modulusValue = (modulusValue << 8) | BigUInt(byte)
82
+ }
83
+ self.modulus = BigInt(modulusValue)
84
+ let exponentBytes = [UInt8](exponent)
85
+ var exponentValue = BigUInt(0)
86
+ for byte in exponentBytes {
87
+ exponentValue = (exponentValue << 8) | BigUInt(byte)
88
+ }
89
+ self.exponent = BigInt(exponentValue)
90
+ }
91
+
92
+ // Parse PKCS#1 format public key
93
+ static func fromPKCS1(_ publicKeyData: Data) -> ManualRSAPublicKey? {
94
+ // Parse ASN.1 DER encoded RSA public key
95
+ // Format: RSAPublicKey ::= SEQUENCE { modulus INTEGER, publicExponent INTEGER }
96
+
97
+ guard publicKeyData.count > 0 else {
98
+ return nil
99
+ }
100
+
101
+ let bytes = [UInt8](publicKeyData)
102
+
103
+ // Check for sequence tag (0x30)
104
+ guard bytes[0] == 0x30 else {
105
+ // Try direct modulus/exponent approach as fallback
106
+ if publicKeyData.count >= 3 {
107
+ // Assume this is a raw RSA public key with modulus + exponent
108
+ // Most common: modulus is 256 bytes (2048 bits), exponent is 3 bytes (0x010001 = 65537)
109
+ let modulusSize = publicKeyData.count - 3
110
+ if modulusSize > 0 {
111
+ let modulusData = publicKeyData.prefix(modulusSize)
112
+ let exponentData = publicKeyData.suffix(3)
113
+ return ManualRSAPublicKey(modulus: modulusData, exponent: exponentData)
114
+ }
115
+ }
116
+
117
+ return nil
118
+ }
119
+
120
+ var index = 1
121
+
122
+ // Skip length
123
+ if bytes[index] & 0x80 != 0 {
124
+ let lenBytes = Int(bytes[index] & 0x7F)
125
+ if (index + 1 + lenBytes) >= bytes.count {
126
+ return nil
127
+ }
128
+ index += 1 + lenBytes
129
+ } else {
130
+ index += 1
131
+ }
132
+
133
+ // Check for INTEGER tag for modulus (0x02)
134
+ if index >= bytes.count {
135
+ return nil
136
+ }
137
+
138
+ guard bytes[index] == 0x02 else {
139
+ return nil
140
+ }
141
+ index += 1
142
+
143
+ // Get modulus length
144
+ if index >= bytes.count {
145
+ return nil
146
+ }
147
+
148
+ var modulusLength = 0
149
+ if bytes[index] & 0x80 != 0 {
150
+ let lenBytes = Int(bytes[index] & 0x7F)
151
+ if (index + 1 + lenBytes) >= bytes.count {
152
+ return nil
153
+ }
154
+ index += 1
155
+ for i in 0..<lenBytes {
156
+ modulusLength = (modulusLength << 8) | Int(bytes[index + i])
157
+ }
158
+ index += lenBytes
159
+ } else {
160
+ modulusLength = Int(bytes[index])
161
+ index += 1
162
+ }
163
+
164
+ // Skip any leading zero in modulus (for unsigned integer)
165
+ if index < bytes.count && bytes[index] == 0x00 {
166
+ index += 1
167
+ modulusLength -= 1
168
+ }
169
+
170
+ // Extract modulus
171
+ if (index + modulusLength) > bytes.count {
172
+ return nil
173
+ }
174
+
175
+ let modulusData = Data(bytes[index..<(index + modulusLength)])
176
+ index += modulusLength
177
+
178
+ // Check for INTEGER tag for exponent
179
+ if index >= bytes.count {
180
+ return nil
181
+ }
182
+
183
+ guard bytes[index] == 0x02 else {
184
+ return nil
185
+ }
186
+ index += 1
187
+
188
+ // Get exponent length
189
+ if index >= bytes.count {
190
+ return nil
191
+ }
192
+
193
+ var exponentLength = 0
194
+ if bytes[index] & 0x80 != 0 {
195
+ let lenBytes = Int(bytes[index] & 0x7F)
196
+ if (index + 1 + lenBytes) >= bytes.count {
197
+ return nil
198
+ }
199
+ index += 1
200
+ for i in 0..<lenBytes {
201
+ exponentLength = (exponentLength << 8) | Int(bytes[index + i])
202
+ }
203
+ index += lenBytes
204
+ } else {
205
+ exponentLength = Int(bytes[index])
206
+ index += 1
207
+ }
208
+
209
+ // Extract exponent
210
+ if (index + exponentLength) > bytes.count {
211
+ return nil
212
+ }
213
+
214
+ let exponentData = Data(bytes[index..<(index + exponentLength)])
215
+ return ManualRSAPublicKey(modulus: modulusData, exponent: exponentData)
216
+ }
217
+
218
+ // Decrypt data using raw RSA operation (c^d mod n)
219
+ func decrypt(_ encryptedData: Data) -> Data? {
220
+ // Create positive BigInt from encrypted data
221
+ let encryptedBytes = [UInt8](encryptedData)
222
+ var encryptedValue = BigUInt(0)
223
+ for byte in encryptedBytes {
224
+ encryptedValue = (encryptedValue << 8) | BigUInt(byte)
225
+ }
226
+ let encrypted = BigInt(encryptedValue)
227
+
228
+ // In Node.js:
229
+ // privateEncrypt uses the private key (d) to encrypt
230
+ // publicDecrypt uses the public key (e) to decrypt
231
+ // The operation we want is: ciphertext^e mod n
232
+
233
+ // RSA operation: c^e mod n
234
+ let decrypted = encrypted.manualPower(exponent, modulus: modulus)
235
+
236
+ // Convert to bytes with proper padding
237
+ guard let bigUIntValue = decrypted.magnitude as? BigUInt else {
238
+ return nil
239
+ }
240
+
241
+ // Convert BigUInt to bytes with padding
242
+ var resultBytes = [UInt8]()
243
+ var tempValue = bigUIntValue
244
+ while tempValue > 0 {
245
+ let byte = UInt8(tempValue & 0xFF)
246
+ resultBytes.insert(byte, at: 0) // Prepend to get big-endian
247
+ tempValue >>= 8
248
+ }
249
+
250
+ // Ensure we have at least 256 bytes (2048 bits) with leading zeros
251
+ let paddedBytes = [UInt8](repeating: 0, count: max(0, 256 - resultBytes.count)) + resultBytes
252
+
253
+ // For PKCS1 padding from Node.js privateEncrypt, the format is:
254
+ // 0x00 || 0x01 || PS || 0x00 || actual data
255
+ // where PS is a string of 0xFF bytes
256
+
257
+ // Check for privateEncrypt padding format (0x00 || 0x01 || PS || 0x00)
258
+ var startIndex = 0
259
+ if paddedBytes.count > 2 && paddedBytes[0] == 0x00 && paddedBytes[1] == 0x01 {
260
+ for i in 2..<paddedBytes.count {
261
+ if paddedBytes[i] == 0x00 {
262
+ startIndex = i + 1
263
+ break
264
+ }
265
+ }
266
+ }
267
+ if startIndex < paddedBytes.count {
268
+ let result = Data(paddedBytes[startIndex...])
269
+ return result
270
+ } else {
271
+ return Data(paddedBytes)
272
+ }
273
+ }
274
+ }