Skip to content
This repository was archived by the owner on May 20, 2025. It is now read-only.

Commit 9e5c8ca

Browse files
Minor improvements
1 parent 146ecc1 commit 9e5c8ca

File tree

4 files changed

+29
-33
lines changed

4 files changed

+29
-33
lines changed

ios/CodePush/CodePush.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,9 @@ failCallback:(void (^)(NSError *err))failCallback;
184184
expectedHash:(NSString *)expectedHash
185185
error:(NSError **)error;
186186

187-
+ (NSString *)cleanPublicKey:(NSString *)publicKeyString;
187+
+ (NSString *)preparePublicKeyForDecoding:(NSString *)publicKeyString;
188188

189-
+ (NSDictionary *) verifyJWT:(NSString *) signature
189+
+ (NSDictionary *) verifyAndDecodeJWT:(NSString *) jwt
190190
withPublicKey:(NSString *)publicKey
191191
error:(NSError **)error;
192192

ios/CodePush/CodePushConfig.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ - (instancetype)init
3535
NSString *buildVersion = [infoDictionary objectForKey:(NSString *)kCFBundleVersionKey];
3636
NSString *deploymentKey = [infoDictionary objectForKey:@"CodePushDeploymentKey"];
3737
NSString *serverURL = [infoDictionary objectForKey:@"CodePushServerURL"];
38-
NSString *publicKey = [infoDictionary objectForKey:@"PublicKey"];
38+
NSString *publicKey = [infoDictionary objectForKey:@"CodePushPublicKey"];
3939

4040
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
4141
NSString *clientUniqueId = [userDefaults stringForKey:ClientUniqueIDConfigKey];

ios/CodePush/CodePushPackage.m

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ + (void) handleFailedDataIntegrityCheck:(void (^)(NSError *err))failCallback
4343
error:(NSError **)error
4444
{
4545
CPLog(@"The update contents failed the data integrity check.");
46-
if (!error || !*error) {
46+
if (!error) {
4747
failCallback([CodePushErrorUtils errorWithMessage:@"The update contents failed the data integrity check."]);
4848
return;
4949
}
@@ -244,10 +244,10 @@ + (void)downloadPackage:(NSDictionary *)updatePackage
244244
return;
245245
}
246246
}
247-
248-
if(isDiffUpdate){
247+
248+
if (isDiffUpdate) {
249249
CPLog(@"Applying diff update.");
250-
}else{
250+
} else {
251251
CPLog(@"Applying full update.");
252252
}
253253

@@ -256,26 +256,23 @@ + (void)downloadPackage:(NSDictionary *)updatePackage
256256
BOOL isSignatureValid = [CodePushUpdateUtils verifySignatureFor:newUpdateFolderPath
257257
withPublicKey:publicKey
258258
error:&error];
259-
if(!isSignatureValid){
259+
if (!isSignatureValid) {
260260
[self handleFailedDataIntegrityCheck:failCallback error:&error];
261261
return;
262-
}else{
262+
} else {
263263
CPLog(@"The update contents succueded the data integrity check.");
264264
}
265-
}else{
265+
} else {
266266
if (isDiffUpdate) {
267-
268-
if(![CodePushUpdateUtils verifyFolderHash:newUpdateFolderPath
267+
if (![CodePushUpdateUtils verifyFolderHash:newUpdateFolderPath
269268
expectedHash:newUpdateHash
270-
error:&error]){
269+
error:&error]) {
271270

272271
[self handleFailedDataIntegrityCheck:failCallback error:&error];
273272
return;
274-
}
275-
else{
273+
} else {
276274
CPLog(@"The update contents succueded the data integrity check.");
277-
}
278-
275+
}
279276
}
280277
}
281278
} else {
@@ -569,4 +566,4 @@ + (BOOL)updateCurrentPackageInfo:(NSDictionary *)packageInfo
569566
return YES;
570567
}
571568

572-
@end
569+
@end

ios/CodePush/CodePushUpdateUtils.m

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ + (BOOL)verifyFolderHash:(NSString *)finalUpdateFolder
285285
return [updateContentsManifestHash isEqualToString:expectedHash];
286286
}
287287

288-
+ (NSString *)cleanPublicKey:(NSString *)publicKeyString
288+
+ (NSString *)preparePublicKeyForDecoding:(NSString *)publicKeyString
289289
{
290290
publicKeyString = [publicKeyString stringByReplacingOccurrencesOfString:@"-----BEGIN PUBLIC KEY-----\n"
291291
withString:@""];
@@ -303,14 +303,13 @@ + (NSString *)getSignatureFor:(NSString *)folderPath
303303
NSString *signatureFilePath = [NSString stringWithFormat:@"%@/%@/%@", folderPath, ManifestFolderPrefix, BundleJWTFile];
304304
if ([[NSFileManager defaultManager] fileExistsAtPath:signatureFilePath]) {
305305
return [NSString stringWithContentsOfFile:signatureFilePath encoding:NSUTF8StringEncoding error:error];
306-
}else{
306+
} else {
307307
*error = [CodePushErrorUtils errorWithMessage:[NSString stringWithFormat: @"Cannot find signature at %@", signatureFilePath]];
308308
return nil;
309309
}
310-
return nil;
311310
}
312311

313-
+ (NSDictionary *) verifyJWT:(NSString *) signature
312+
+ (NSDictionary *) verifyAndDecodeJWT:(NSString *) jwt
314313
withPublicKey:(NSString *)publicKey
315314
error:(NSError **)error
316315
{
@@ -333,22 +332,22 @@ + (BOOL)verifySignatureFor:(NSString *)folderPath
333332
{
334333
NSLog(@"Verifying signature for folder path: %@", folderPath);
335334

336-
NSString *publicKey = [self cleanPublicKey: publicKeyString];
335+
NSString *publicKey = [self preparePublicKeyForDecoding: publicKeyString];
337336

337+
NSError *signatureVerificationError;
338338
NSString *signature = [self getSignatureFor: folderPath
339-
error: error];
340-
if(signature == nil) {
341-
if(error && *error){
342-
CPLog(@"The update could not be verified because no signature was found. %@", *error);
343-
}else{
344-
CPLog(@"The update could not be verified because no signature was found.");
345-
}
339+
error: &signatureVerificationError];
340+
if(signatureVerificationError) {
341+
CPLog(@"The update could not be verified because no signature was found. %@", signatureVerificationError);
342+
*error = signatureVerificationError;
346343
return false;
347344
}
348345

349-
NSDictionary *envelopedPayload = [self verifyJWT:signature withPublicKey:publicKey error:error];
350-
if(envelopedPayload == nil){
351-
CPLog(@"The update could not be verified because it was not signed by a trusted party. %@", *error);
346+
NSError *payloadDecodingError;
347+
NSDictionary *envelopedPayload = [self verifyAndDecodeJWT:signature withPublicKey:publicKey error:&payloadDecodingError];
348+
if(payloadDecodingError){
349+
CPLog(@"The update could not be verified because it was not signed by a trusted party. %@", payloadDecodingError);
350+
*error = payloadDecodingError;
352351
return false;
353352
}
354353

0 commit comments

Comments
 (0)