Skip to content

Commit

Permalink
Offer upload after key extension.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mento committed Aug 27, 2018
1 parent 76c8334 commit 15e192d
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 2 deletions.
7 changes: 7 additions & 0 deletions Resources/en.lproj/Localizable.strings
Expand Up @@ -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!";
Expand Down
57 changes: 55 additions & 2 deletions Source/GPGKeyMonitoring.m
Expand Up @@ -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<GPGKey *> *keys = info.keys;
NSUInteger count = keys.count;
NSUInteger i = 0;
NSUInteger count = keys.count;

for (; i < count;) {
BOOL tryAgain = NO;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
}

Expand Down

0 comments on commit 15e192d

Please sign in to comment.