From 15e192d0becd66e3ac176c93a0329be8f0befec3 Mon Sep 17 00:00:00 2001 From: Mento Date: Mon, 27 Aug 2018 11:32:01 +0200 Subject: [PATCH] Offer upload after key extension. --- Resources/en.lproj/Localizable.strings | 7 ++++ Source/GPGKeyMonitoring.m | 57 +++++++++++++++++++++++++- 2 files changed, 62 insertions(+), 2 deletions(-) diff --git a/Resources/en.lproj/Localizable.strings b/Resources/en.lproj/Localizable.strings index d9644f9..8213d33 100644 --- a/Resources/en.lproj/Localizable.strings +++ b/Resources/en.lproj/Localizable.strings @@ -117,6 +117,13 @@ WrongPasswordTryAgain_No = "Cancel"; KeyExpiryFailed_Title = "Failed to extend your key!"; KeyExpiryFailed_Msg = "The key\n%1$@\ncould not be extened.\n\nError: %2$@"; +KeyExtendedWantToUpload_Title = "Your key was extended successfully"; +KeyExtendedWantToUpload_Msg = "In order for others to be able to see the new expiration date, it is necessary for them to import your updated key. Therefore it is recommended to upload your updated public key to the key servers.\n\nWarning: Key servers are public, so the name and email you use in your key will be publicly visible. Keys can not be deleted from the key servers. They can be revoked but not removed.\n\nIf you rather prefer not to use key servers, please consider attaching your public key to your signed and encrypted emails.\n\nDo you want to upload your public key?"; +KeyExtendedWantToUpload_Yes = "Upload Public Key"; +KeyExtendedWantToUpload_No = "No, Thanks!"; + +UploadFailed_Title = "Failed to upload the key!"; +UploadFailed_Msg = "The key could not be uploaded to the keyserver.\n\nError: %@"; UpdaterNotWorking_Title = "The GPG Suite Updater doesn't work as expected!"; diff --git a/Source/GPGKeyMonitoring.m b/Source/GPGKeyMonitoring.m index 55ea277..911d7a6 100644 --- a/Source/GPGKeyMonitoring.m +++ b/Source/GPGKeyMonitoring.m @@ -339,17 +339,39 @@ - (BOOL)showExpiryWarning:(GPGKeyWarningInfo *)info { if (response == NSAlertThirdButtonReturn) { + // Do not show a warning for this key again. return YES; } if (response != NSAlertFirstButtonReturn) { + // Do not extend this key. return NO; } + + + // Asnc test if the key exists on the keyserver. + __block BOOL keyExistsOnServer = NO; + dispatch_semaphore_t semaphore = dispatch_semaphore_create(0); + dispatch_retain(semaphore); + + GPGController *keyExistsGPGC = [GPGController gpgController]; + keyExistsGPGC.keyserverTimeout = 2; + [keyExistsGPGC keysExistOnServer:@[info.primaryKey] callback:^(NSArray *existingKeys, NSArray *nonExistingKeys) { + keyExistsOnServer = existingKeys.count == 1; + + dispatch_semaphore_signal(semaphore); + dispatch_release(semaphore); + }]; + + + + GPGController *gpgc = [GPGController gpgController]; + BOOL failed = NO; NSArray *keys = info.keys; - NSUInteger count = keys.count; NSUInteger i = 0; + NSUInteger count = keys.count; for (; i < count;) { BOOL tryAgain = NO; @@ -358,8 +380,8 @@ - (BOOL)showExpiryWarning:(GPGKeyWarningInfo *)info { [gpgc setExpirationDateForSubkey:subkey fromKey:info.primaryKey daysToExpire:365 * 2]; - if (gpgc.error) { + failed = YES; NSException *exception = gpgc.error; if ([exception isKindOfClass:[GPGException class]]) { GPGErrorCode errorCode = ((GPGException *)exception).errorCode; @@ -369,6 +391,7 @@ - (BOOL)showExpiryWarning:(GPGKeyWarningInfo *)info { if (errorCode == GPGErrorBadPassphrase) { tryAgain = [self wrongPasswordEnteredForKey:key]; if (tryAgain) { + failed = NO; continue; // Do not increment i. } else { break; @@ -385,6 +408,36 @@ - (BOOL)showExpiryWarning:(GPGKeyWarningInfo *)info { // Incerment i here, so continue can be used to jump over the increment. i++; } + + if (!failed) { + // Wait for the result from -keysExistOnServer. + dispatch_time_t timeout = dispatch_time(DISPATCH_TIME_NOW, 2 * NSEC_PER_SEC); + dispatch_semaphore_wait(semaphore, timeout); + + + NSArray *buttons; + NSModalResponse yesButton; + if (keyExistsOnServer) { + buttons = @[@"Yes", @"No"]; + yesButton = NSAlertFirstButtonReturn; + } else { + buttons = @[@"No", @"Yes"]; + yesButton = NSAlertSecondButtonReturn; + } + + NSModalResponse result = [self showAlertWithButtons:buttons prefix:@"KeyExtendedWantToUpload"]; + if (result == yesButton) { + [gpgc sendKeysToServer:@[info.primaryKey]]; + + if (gpgc.error) { + [self showAlertWithButtons:nil prefix:@"UploadFailed", gpgc.error.description]; + } + } + } + + dispatch_release(semaphore); + + return NO; }