@capgo/capacitor-updater 4.41.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.
- package/CapgoCapacitorUpdater.podspec +7 -5
- package/Package.swift +40 -0
- package/README.md +1913 -303
- package/android/build.gradle +41 -8
- package/android/proguard-rules.pro +45 -0
- package/android/src/main/AndroidManifest.xml +1 -3
- package/android/src/main/java/ee/forgr/capacitor_updater/AppLifecycleObserver.java +88 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/BundleInfo.java +223 -195
- package/android/src/main/java/ee/forgr/capacitor_updater/BundleStatus.java +23 -23
- package/android/src/main/java/ee/forgr/capacitor_updater/Callback.java +13 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/CapacitorUpdaterPlugin.java +2720 -1242
- package/android/src/main/java/ee/forgr/capacitor_updater/CapgoUpdater.java +1854 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/CryptoCipher.java +359 -121
- package/android/src/main/java/ee/forgr/capacitor_updater/DataManager.java +28 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/DelayCondition.java +44 -49
- package/android/src/main/java/ee/forgr/capacitor_updater/DelayUntilNext.java +4 -4
- package/android/src/main/java/ee/forgr/capacitor_updater/DelayUpdateUtils.java +296 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/DeviceIdHelper.java +215 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/DownloadService.java +858 -117
- package/android/src/main/java/ee/forgr/capacitor_updater/DownloadWorkerManager.java +156 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/InternalUtils.java +45 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/Logger.java +360 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/ShakeDetector.java +72 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/ShakeMenu.java +603 -0
- package/dist/docs.json +3022 -765
- package/dist/esm/definitions.d.ts +1717 -198
- package/dist/esm/definitions.js +103 -1
- package/dist/esm/definitions.js.map +1 -1
- package/dist/esm/history.d.ts +1 -0
- package/dist/esm/history.js +283 -0
- package/dist/esm/history.js.map +1 -0
- package/dist/esm/index.d.ts +3 -2
- package/dist/esm/index.js +5 -4
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/web.d.ts +43 -42
- package/dist/esm/web.js +122 -37
- package/dist/esm/web.js.map +1 -1
- package/dist/plugin.cjs.js +512 -37
- package/dist/plugin.cjs.js.map +1 -1
- package/dist/plugin.js +512 -37
- package/dist/plugin.js.map +1 -1
- package/ios/Sources/CapacitorUpdaterPlugin/AES.swift +87 -0
- package/ios/Sources/CapacitorUpdaterPlugin/BigInt.swift +55 -0
- package/ios/Sources/CapacitorUpdaterPlugin/BundleInfo.swift +177 -0
- package/ios/{Plugin → Sources/CapacitorUpdaterPlugin}/BundleStatus.swift +12 -12
- package/ios/Sources/CapacitorUpdaterPlugin/CapacitorUpdaterPlugin.swift +2020 -0
- package/ios/Sources/CapacitorUpdaterPlugin/CapgoUpdater.swift +1959 -0
- package/ios/Sources/CapacitorUpdaterPlugin/CryptoCipher.swift +313 -0
- package/ios/Sources/CapacitorUpdaterPlugin/DelayUpdateUtils.swift +257 -0
- package/ios/Sources/CapacitorUpdaterPlugin/DeviceIdHelper.swift +120 -0
- package/ios/Sources/CapacitorUpdaterPlugin/InternalUtils.swift +392 -0
- package/ios/Sources/CapacitorUpdaterPlugin/Logger.swift +310 -0
- package/ios/Sources/CapacitorUpdaterPlugin/RSA.swift +274 -0
- package/ios/Sources/CapacitorUpdaterPlugin/ShakeMenu.swift +441 -0
- package/ios/{Plugin → Sources/CapacitorUpdaterPlugin}/UserDefaultsExtension.swift +1 -2
- package/package.json +49 -41
- package/android/src/main/java/ee/forgr/capacitor_updater/CapacitorUpdater.java +0 -1131
- package/ios/Plugin/BundleInfo.swift +0 -113
- package/ios/Plugin/CapacitorUpdater.swift +0 -850
- package/ios/Plugin/CapacitorUpdaterPlugin.h +0 -10
- package/ios/Plugin/CapacitorUpdaterPlugin.m +0 -27
- package/ios/Plugin/CapacitorUpdaterPlugin.swift +0 -678
- package/ios/Plugin/CryptoCipher.swift +0 -240
- /package/{LICENCE → LICENSE} +0 -0
- /package/ios/{Plugin → Sources/CapacitorUpdaterPlugin}/DelayCondition.swift +0 -0
- /package/ios/{Plugin → Sources/CapacitorUpdaterPlugin}/DelayUntilNext.swift +0 -0
- /package/ios/{Plugin → Sources/CapacitorUpdaterPlugin}/Info.plist +0 -0
|
@@ -12,142 +12,380 @@ package ee.forgr.capacitor_updater;
|
|
|
12
12
|
* references: http://stackoverflow.com/questions/12471999/rsa-encryption-decryption-in-android
|
|
13
13
|
*/
|
|
14
14
|
import android.util.Base64;
|
|
15
|
+
import java.io.BufferedInputStream;
|
|
16
|
+
import java.io.DataInputStream;
|
|
17
|
+
import java.io.File;
|
|
18
|
+
import java.io.FileInputStream;
|
|
19
|
+
import java.io.FileOutputStream;
|
|
20
|
+
import java.io.IOException;
|
|
15
21
|
import java.security.GeneralSecurityException;
|
|
16
22
|
import java.security.InvalidAlgorithmParameterException;
|
|
17
23
|
import java.security.InvalidKeyException;
|
|
18
24
|
import java.security.KeyFactory;
|
|
25
|
+
import java.security.MessageDigest;
|
|
19
26
|
import java.security.NoSuchAlgorithmException;
|
|
20
|
-
import java.security.
|
|
27
|
+
import java.security.PublicKey;
|
|
21
28
|
import java.security.spec.InvalidKeySpecException;
|
|
22
|
-
import java.security.spec.
|
|
23
|
-
import java.security.spec.PKCS8EncodedKeySpec;
|
|
29
|
+
import java.security.spec.X509EncodedKeySpec;
|
|
24
30
|
import javax.crypto.BadPaddingException;
|
|
25
31
|
import javax.crypto.Cipher;
|
|
26
32
|
import javax.crypto.IllegalBlockSizeException;
|
|
27
33
|
import javax.crypto.NoSuchPaddingException;
|
|
28
34
|
import javax.crypto.SecretKey;
|
|
29
35
|
import javax.crypto.spec.IvParameterSpec;
|
|
30
|
-
import javax.crypto.spec.OAEPParameterSpec;
|
|
31
|
-
import javax.crypto.spec.PSource;
|
|
32
36
|
import javax.crypto.spec.SecretKeySpec;
|
|
33
37
|
|
|
34
38
|
public class CryptoCipher {
|
|
35
39
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
0x1,
|
|
111
|
-
0x0, // Integer (0)
|
|
112
|
-
0x30,
|
|
113
|
-
0xD,
|
|
114
|
-
0x6,
|
|
115
|
-
0x9,
|
|
116
|
-
0x2A,
|
|
117
|
-
(byte) 0x86,
|
|
118
|
-
0x48,
|
|
119
|
-
(byte) 0x86,
|
|
120
|
-
(byte) 0xF7,
|
|
121
|
-
0xD,
|
|
122
|
-
0x1,
|
|
123
|
-
0x1,
|
|
124
|
-
0x1,
|
|
125
|
-
0x5,
|
|
126
|
-
0x0, // Sequence: 1.2.840.113549.1.1.1, NULL
|
|
127
|
-
0x4,
|
|
128
|
-
(byte) 0x82,
|
|
129
|
-
(byte) ((pkcs1Length >> 8) & 0xff),
|
|
130
|
-
(byte) (pkcs1Length & 0xff), // Octet string + length
|
|
40
|
+
private static Logger logger;
|
|
41
|
+
|
|
42
|
+
public static void setLogger(Logger loggerInstance) {
|
|
43
|
+
logger = loggerInstance;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
public static byte[] decryptRSA(byte[] source, PublicKey publicKey)
|
|
47
|
+
throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
|
|
48
|
+
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
|
|
49
|
+
cipher.init(Cipher.DECRYPT_MODE, publicKey);
|
|
50
|
+
byte[] decryptedBytes = cipher.doFinal(source);
|
|
51
|
+
return decryptedBytes;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
public static byte[] decryptAES(byte[] cipherText, SecretKey key, byte[] iv) {
|
|
55
|
+
try {
|
|
56
|
+
IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);
|
|
57
|
+
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
|
|
58
|
+
SecretKeySpec keySpec = new SecretKeySpec(key.getEncoded(), "AES");
|
|
59
|
+
cipher.init(Cipher.DECRYPT_MODE, keySpec, ivParameterSpec);
|
|
60
|
+
return cipher.doFinal(cipherText);
|
|
61
|
+
} catch (Exception e) {
|
|
62
|
+
e.printStackTrace();
|
|
63
|
+
}
|
|
64
|
+
return null;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
public static SecretKey byteToSessionKey(byte[] sessionKey) {
|
|
68
|
+
// rebuild key using SecretKeySpec
|
|
69
|
+
return new SecretKeySpec(sessionKey, 0, sessionKey.length, "AES");
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
private static PublicKey readX509PublicKey(byte[] x509Bytes) throws GeneralSecurityException {
|
|
73
|
+
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
|
|
74
|
+
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(x509Bytes);
|
|
75
|
+
try {
|
|
76
|
+
return keyFactory.generatePublic(keySpec);
|
|
77
|
+
} catch (InvalidKeySpecException e) {
|
|
78
|
+
throw new IllegalArgumentException("Unexpected key format!", e);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
public static PublicKey stringToPublicKey(String public_key) throws GeneralSecurityException {
|
|
83
|
+
String pkcs1Pem = public_key
|
|
84
|
+
.replaceAll("\\s+", "")
|
|
85
|
+
.replace("-----BEGINRSAPUBLICKEY-----", "")
|
|
86
|
+
.replace("-----ENDRSAPUBLICKEY-----", "");
|
|
87
|
+
|
|
88
|
+
byte[] pkcs1EncodedBytes = Base64.decode(pkcs1Pem, Base64.DEFAULT);
|
|
89
|
+
return readPkcs1PublicKey(pkcs1EncodedBytes);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// since the public key is in pkcs1 format, we have to convert it to x509 format similar
|
|
93
|
+
// to what needs done with the private key converting to pkcs8 format
|
|
94
|
+
// so, the rest of the code below here is adapted from here https://stackoverflow.com/a/54246646
|
|
95
|
+
private static final int SEQUENCE_TAG = 0x30;
|
|
96
|
+
private static final int BIT_STRING_TAG = 0x03;
|
|
97
|
+
private static final byte[] NO_UNUSED_BITS = new byte[] { 0x00 };
|
|
98
|
+
private static final byte[] RSA_ALGORITHM_IDENTIFIER_SEQUENCE = {
|
|
99
|
+
(byte) 0x30,
|
|
100
|
+
(byte) 0x0d,
|
|
101
|
+
(byte) 0x06,
|
|
102
|
+
(byte) 0x09,
|
|
103
|
+
(byte) 0x2a,
|
|
104
|
+
(byte) 0x86,
|
|
105
|
+
(byte) 0x48,
|
|
106
|
+
(byte) 0x86,
|
|
107
|
+
(byte) 0xf7,
|
|
108
|
+
(byte) 0x0d,
|
|
109
|
+
(byte) 0x01,
|
|
110
|
+
(byte) 0x01,
|
|
111
|
+
(byte) 0x01,
|
|
112
|
+
(byte) 0x05,
|
|
113
|
+
(byte) 0x00
|
|
131
114
|
};
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
115
|
+
|
|
116
|
+
private static PublicKey readPkcs1PublicKey(byte[] pkcs1Bytes)
|
|
117
|
+
throws NoSuchAlgorithmException, InvalidKeySpecException, GeneralSecurityException {
|
|
118
|
+
// convert the pkcs1 public key to an x509 favorable format
|
|
119
|
+
byte[] keyBitString = createDEREncoding(BIT_STRING_TAG, joinPublic(NO_UNUSED_BITS, pkcs1Bytes));
|
|
120
|
+
byte[] keyInfoValue = joinPublic(RSA_ALGORITHM_IDENTIFIER_SEQUENCE, keyBitString);
|
|
121
|
+
byte[] keyInfoSequence = createDEREncoding(SEQUENCE_TAG, keyInfoValue);
|
|
122
|
+
return readX509PublicKey(keyInfoSequence);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
private static byte[] joinPublic(byte[]... bas) {
|
|
126
|
+
int len = 0;
|
|
127
|
+
for (int i = 0; i < bas.length; i++) {
|
|
128
|
+
len += bas[i].length;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
byte[] buf = new byte[len];
|
|
132
|
+
int off = 0;
|
|
133
|
+
for (int i = 0; i < bas.length; i++) {
|
|
134
|
+
System.arraycopy(bas[i], 0, buf, off, bas[i].length);
|
|
135
|
+
off += bas[i].length;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
return buf;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
public static void decryptFile(final File file, final String publicKey, final String ivSessionKey) throws IOException {
|
|
142
|
+
if (publicKey.isEmpty() || ivSessionKey == null || ivSessionKey.isEmpty() || ivSessionKey.split(":").length != 2) {
|
|
143
|
+
logger.info("Encryption not set, no public key or session, ignored");
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
if (!publicKey.startsWith("-----BEGIN RSA PUBLIC KEY-----")) {
|
|
147
|
+
logger.error("The public key is not a valid RSA Public key");
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
try {
|
|
152
|
+
String ivB64 = ivSessionKey.split(":")[0];
|
|
153
|
+
String sessionKeyB64 = ivSessionKey.split(":")[1];
|
|
154
|
+
byte[] iv = Base64.decode(ivB64.getBytes(), Base64.DEFAULT);
|
|
155
|
+
byte[] sessionKey = Base64.decode(sessionKeyB64.getBytes(), Base64.DEFAULT);
|
|
156
|
+
PublicKey pKey = CryptoCipher.stringToPublicKey(publicKey);
|
|
157
|
+
byte[] decryptedSessionKey = CryptoCipher.decryptRSA(sessionKey, pKey);
|
|
158
|
+
|
|
159
|
+
SecretKey sKey = CryptoCipher.byteToSessionKey(decryptedSessionKey);
|
|
160
|
+
byte[] content = new byte[(int) file.length()];
|
|
161
|
+
|
|
162
|
+
try (
|
|
163
|
+
final FileInputStream fis = new FileInputStream(file);
|
|
164
|
+
final BufferedInputStream bis = new BufferedInputStream(fis);
|
|
165
|
+
final DataInputStream dis = new DataInputStream(bis)
|
|
166
|
+
) {
|
|
167
|
+
dis.readFully(content);
|
|
168
|
+
dis.close();
|
|
169
|
+
byte[] decrypted = CryptoCipher.decryptAES(content, sKey, iv);
|
|
170
|
+
// write the decrypted string to the file
|
|
171
|
+
try (final FileOutputStream fos = new FileOutputStream(file.getAbsolutePath())) {
|
|
172
|
+
fos.write(decrypted);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
} catch (GeneralSecurityException e) {
|
|
176
|
+
logger.info("decryptFile fail");
|
|
177
|
+
e.printStackTrace();
|
|
178
|
+
throw new IOException("GeneralSecurityException");
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
private static byte[] hexStringToByteArray(String s) {
|
|
183
|
+
int len = s.length();
|
|
184
|
+
byte[] data = new byte[len / 2];
|
|
185
|
+
for (int i = 0; i < len; i += 2) {
|
|
186
|
+
data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) + Character.digit(s.charAt(i + 1), 16));
|
|
187
|
+
}
|
|
188
|
+
return data;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
public static String decryptChecksum(String checksum, String publicKey) throws IOException {
|
|
192
|
+
if (publicKey.isEmpty()) {
|
|
193
|
+
logger.error("No encryption set (public key) ignored");
|
|
194
|
+
return checksum;
|
|
195
|
+
}
|
|
196
|
+
try {
|
|
197
|
+
// TODO: remove this in a month or two
|
|
198
|
+
// Determine if input is hex or base64 encoded
|
|
199
|
+
// Hex strings only contain 0-9 and a-f, while base64 contains other characters
|
|
200
|
+
byte[] checksumBytes;
|
|
201
|
+
String detectedFormat;
|
|
202
|
+
if (checksum.matches("^[0-9a-fA-F]+$")) {
|
|
203
|
+
// Hex encoded (new format from CLI for plugin versions >= 5.30.0, 6.30.0, 7.30.0)
|
|
204
|
+
checksumBytes = hexStringToByteArray(checksum);
|
|
205
|
+
detectedFormat = "hex";
|
|
206
|
+
} else {
|
|
207
|
+
// TODO: remove backwards compatibility
|
|
208
|
+
// Base64 encoded (old format for backwards compatibility)
|
|
209
|
+
checksumBytes = Base64.decode(checksum, Base64.DEFAULT);
|
|
210
|
+
detectedFormat = "base64";
|
|
211
|
+
}
|
|
212
|
+
logger.debug(
|
|
213
|
+
"Received checksum format: " +
|
|
214
|
+
detectedFormat +
|
|
215
|
+
" (length: " +
|
|
216
|
+
checksum.length() +
|
|
217
|
+
" chars, " +
|
|
218
|
+
checksumBytes.length +
|
|
219
|
+
" bytes)"
|
|
220
|
+
);
|
|
221
|
+
|
|
222
|
+
// RSA-2048 encrypted data must be exactly 256 bytes
|
|
223
|
+
// If the checksum is not 256 bytes, the bundle was not encrypted properly
|
|
224
|
+
if (checksumBytes.length != 256) {
|
|
225
|
+
logger.error(
|
|
226
|
+
"Checksum is not RSA encrypted (size: " +
|
|
227
|
+
checksumBytes.length +
|
|
228
|
+
" bytes, expected 256 for RSA-2048). Bundle must be uploaded with encryption when public key is configured."
|
|
229
|
+
);
|
|
230
|
+
throw new IOException("Bundle checksum is not encrypted. Upload bundle with --key flag when encryption is configured.");
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
PublicKey pKey = CryptoCipher.stringToPublicKey(publicKey);
|
|
234
|
+
byte[] decryptedChecksum = CryptoCipher.decryptRSA(checksumBytes, pKey);
|
|
235
|
+
// Return as hex string to match calcChecksum output format
|
|
236
|
+
StringBuilder hexString = new StringBuilder();
|
|
237
|
+
for (byte b : decryptedChecksum) {
|
|
238
|
+
String hex = Integer.toHexString(0xff & b);
|
|
239
|
+
if (hex.length() == 1) hexString.append('0');
|
|
240
|
+
hexString.append(hex);
|
|
241
|
+
}
|
|
242
|
+
String result = hexString.toString();
|
|
243
|
+
|
|
244
|
+
// Detect checksum algorithm based on length
|
|
245
|
+
String detectedAlgorithm;
|
|
246
|
+
if (decryptedChecksum.length == 32) {
|
|
247
|
+
detectedAlgorithm = "SHA-256";
|
|
248
|
+
} else if (decryptedChecksum.length == 4) {
|
|
249
|
+
detectedAlgorithm = "CRC32 (deprecated)";
|
|
250
|
+
logger.error("CRC32 checksum detected - deprecated algorithm");
|
|
251
|
+
} else {
|
|
252
|
+
detectedAlgorithm = "unknown (" + decryptedChecksum.length + " bytes)";
|
|
253
|
+
logger.error("Unknown checksum algorithm detected");
|
|
254
|
+
logger.debug("Byte count: " + decryptedChecksum.length + ", Expected: 32 (SHA-256)");
|
|
255
|
+
}
|
|
256
|
+
logger.debug(
|
|
257
|
+
"Decrypted checksum: " +
|
|
258
|
+
detectedAlgorithm +
|
|
259
|
+
" hex format (length: " +
|
|
260
|
+
result.length() +
|
|
261
|
+
" chars, " +
|
|
262
|
+
decryptedChecksum.length +
|
|
263
|
+
" bytes)"
|
|
264
|
+
);
|
|
265
|
+
return result;
|
|
266
|
+
} catch (GeneralSecurityException e) {
|
|
267
|
+
logger.error("Checksum decryption failed");
|
|
268
|
+
logger.debug("Error: " + e.getMessage());
|
|
269
|
+
throw new IOException("Decryption failed: " + e.getMessage());
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/**
|
|
274
|
+
* Detect checksum algorithm based on hex string length.
|
|
275
|
+
* SHA-256 = 64 hex chars (32 bytes)
|
|
276
|
+
* CRC32 = 8 hex chars (4 bytes)
|
|
277
|
+
*/
|
|
278
|
+
public static String detectChecksumAlgorithm(String hexChecksum) {
|
|
279
|
+
if (hexChecksum == null || hexChecksum.isEmpty()) {
|
|
280
|
+
return "empty";
|
|
281
|
+
}
|
|
282
|
+
int len = hexChecksum.length();
|
|
283
|
+
if (len == 64) {
|
|
284
|
+
return "SHA-256";
|
|
285
|
+
} else if (len == 8) {
|
|
286
|
+
return "CRC32 (deprecated)";
|
|
287
|
+
} else {
|
|
288
|
+
return "unknown (" + len + " hex chars)";
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
/**
|
|
293
|
+
* Log checksum info and warn if deprecated algorithm detected.
|
|
294
|
+
*/
|
|
295
|
+
public static void logChecksumInfo(String label, String hexChecksum) {
|
|
296
|
+
String algorithm = detectChecksumAlgorithm(hexChecksum);
|
|
297
|
+
logger.debug(label + ": " + algorithm + " hex format (length: " + hexChecksum.length() + " chars)");
|
|
298
|
+
if (algorithm.contains("CRC32")) {
|
|
299
|
+
logger.error("CRC32 checksum detected - deprecated algorithm");
|
|
300
|
+
} else if (algorithm.contains("unknown")) {
|
|
301
|
+
logger.error("Unknown checksum algorithm detected");
|
|
302
|
+
logger.debug("Char count: " + hexChecksum.length() + ", Expected: 64 (SHA-256)");
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
public static String calcChecksum(File file) {
|
|
307
|
+
final int BUFFER_SIZE = 1024 * 1024 * 5; // 5 MB buffer size
|
|
308
|
+
MessageDigest digest;
|
|
309
|
+
try {
|
|
310
|
+
digest = MessageDigest.getInstance("SHA-256");
|
|
311
|
+
} catch (java.security.NoSuchAlgorithmException e) {
|
|
312
|
+
logger.error("SHA-256 algorithm not available");
|
|
313
|
+
return "";
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
try (FileInputStream fis = new FileInputStream(file)) {
|
|
317
|
+
byte[] buffer = new byte[BUFFER_SIZE];
|
|
318
|
+
int length;
|
|
319
|
+
while ((length = fis.read(buffer)) != -1) {
|
|
320
|
+
digest.update(buffer, 0, length);
|
|
321
|
+
}
|
|
322
|
+
byte[] hash = digest.digest();
|
|
323
|
+
StringBuilder hexString = new StringBuilder();
|
|
324
|
+
for (byte b : hash) {
|
|
325
|
+
String hex = Integer.toHexString(0xff & b);
|
|
326
|
+
if (hex.length() == 1) hexString.append('0');
|
|
327
|
+
hexString.append(hex);
|
|
328
|
+
}
|
|
329
|
+
return hexString.toString();
|
|
330
|
+
} catch (IOException e) {
|
|
331
|
+
logger.error("Cannot calculate checksum");
|
|
332
|
+
logger.debug("Path: " + file.getPath() + ", Error: " + e.getMessage());
|
|
333
|
+
return "";
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
private static byte[] createDEREncoding(int tag, byte[] value) {
|
|
338
|
+
if (tag < 0 || tag >= 0xFF) {
|
|
339
|
+
throw new IllegalArgumentException("Currently only single byte tags supported");
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
byte[] lengthEncoding = createDERLengthEncoding(value.length);
|
|
343
|
+
|
|
344
|
+
int size = 1 + lengthEncoding.length + value.length;
|
|
345
|
+
byte[] derEncodingBuf = new byte[size];
|
|
346
|
+
|
|
347
|
+
int off = 0;
|
|
348
|
+
derEncodingBuf[off++] = (byte) tag;
|
|
349
|
+
System.arraycopy(lengthEncoding, 0, derEncodingBuf, off, lengthEncoding.length);
|
|
350
|
+
off += lengthEncoding.length;
|
|
351
|
+
System.arraycopy(value, 0, derEncodingBuf, off, value.length);
|
|
352
|
+
|
|
353
|
+
return derEncodingBuf;
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
private static byte[] createDERLengthEncoding(int size) {
|
|
357
|
+
if (size <= 0x7F) {
|
|
358
|
+
// single byte length encoding
|
|
359
|
+
return new byte[] { (byte) size };
|
|
360
|
+
} else if (size <= 0xFF) {
|
|
361
|
+
// double byte length encoding
|
|
362
|
+
return new byte[] { (byte) 0x81, (byte) size };
|
|
363
|
+
} else if (size <= 0xFFFF) {
|
|
364
|
+
// triple byte length encoding
|
|
365
|
+
return new byte[] { (byte) 0x82, (byte) (size >> Byte.SIZE), (byte) size };
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
throw new IllegalArgumentException("size too large, only up to 64KiB length encoding supported: " + size);
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
/**
|
|
372
|
+
* Get first 20 characters of the public key for identification.
|
|
373
|
+
* Returns 20-character string or empty string if key is invalid/empty.
|
|
374
|
+
* The first 12 chars are always "MIIBCgKCAQEA" for RSA 2048-bit keys,
|
|
375
|
+
* so the unique part starts at character 13.
|
|
376
|
+
*/
|
|
377
|
+
public static String calcKeyId(String publicKey) {
|
|
378
|
+
if (publicKey == null || publicKey.isEmpty()) {
|
|
379
|
+
return "";
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
// Remove PEM headers and whitespace to get the raw key data
|
|
383
|
+
String cleanedKey = publicKey
|
|
384
|
+
.replaceAll("\\s+", "")
|
|
385
|
+
.replace("-----BEGINRSAPUBLICKEY-----", "")
|
|
386
|
+
.replace("-----ENDRSAPUBLICKEY-----", "");
|
|
387
|
+
|
|
388
|
+
// Return first 20 characters of the base64-encoded key
|
|
389
|
+
return cleanedKey.length() >= 20 ? cleanedKey.substring(0, 20) : cleanedKey;
|
|
390
|
+
}
|
|
153
391
|
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
package ee.forgr.capacitor_updater;
|
|
2
|
+
|
|
3
|
+
import org.json.JSONArray;
|
|
4
|
+
|
|
5
|
+
public class DataManager {
|
|
6
|
+
|
|
7
|
+
private static DataManager instance;
|
|
8
|
+
private JSONArray currentManifest;
|
|
9
|
+
|
|
10
|
+
private DataManager() {}
|
|
11
|
+
|
|
12
|
+
public static synchronized DataManager getInstance() {
|
|
13
|
+
if (instance == null) {
|
|
14
|
+
instance = new DataManager();
|
|
15
|
+
}
|
|
16
|
+
return instance;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
public void setManifest(JSONArray manifest) {
|
|
20
|
+
this.currentManifest = manifest;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
public JSONArray getAndClearManifest() {
|
|
24
|
+
JSONArray manifest = this.currentManifest;
|
|
25
|
+
this.currentManifest = null;
|
|
26
|
+
return manifest;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
@@ -6,57 +6,52 @@
|
|
|
6
6
|
|
|
7
7
|
package ee.forgr.capacitor_updater;
|
|
8
8
|
|
|
9
|
-
import
|
|
9
|
+
import androidx.annotation.NonNull;
|
|
10
10
|
import java.util.Objects;
|
|
11
11
|
|
|
12
12
|
public class DelayCondition {
|
|
13
13
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
)
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
public String toString() {
|
|
58
|
-
return (
|
|
59
|
-
"DelayCondition{" + "kind=" + kind + ", value='" + value + '\'' + '}'
|
|
60
|
-
);
|
|
61
|
-
}
|
|
14
|
+
private DelayUntilNext kind;
|
|
15
|
+
|
|
16
|
+
private String value;
|
|
17
|
+
|
|
18
|
+
public DelayCondition(DelayUntilNext kind, String value) {
|
|
19
|
+
this.kind = kind;
|
|
20
|
+
this.value = value;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
public DelayUntilNext getKind() {
|
|
24
|
+
return kind;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
public void setKind(DelayUntilNext kind) {
|
|
28
|
+
this.kind = kind;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
public String getValue() {
|
|
32
|
+
return value;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
public void setValue(String value) {
|
|
36
|
+
this.value = value;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
@Override
|
|
40
|
+
public boolean equals(Object o) {
|
|
41
|
+
if (this == o) return true;
|
|
42
|
+
if (!(o instanceof DelayCondition)) return false;
|
|
43
|
+
DelayCondition that = (DelayCondition) o;
|
|
44
|
+
return (getKind() == that.getKind() && Objects.equals(getValue(), that.getValue()));
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
@Override
|
|
48
|
+
public int hashCode() {
|
|
49
|
+
return Objects.hash(getKind(), getValue());
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
@NonNull
|
|
53
|
+
@Override
|
|
54
|
+
public String toString() {
|
|
55
|
+
return ("DelayCondition{" + "kind=" + kind + ", value='" + value + '\'' + '}');
|
|
56
|
+
}
|
|
62
57
|
}
|