Changeset 117 for branches

Show
Ignore:
Timestamp:
08/21/07 18:10:47 (1 year ago)
Author:
eric
Message:

Clean up some Keychain issues. While NSString length is good 99% of the time it will fail if there are extended characters. We should not need to terminate the string with 0 if we put it in the keychain properly.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/eric/Controller.m

    r116 r117  
    4141 
    4242        [conn runInNewThread]; 
    43         [conn removeRunLoop:[NSRunLoop currentRunLoop]]; 
     43        [conn removeRunLoop:[NSRunLoop currentRunLoop]];  
    4444 
    4545        /* Register the default settings */ 
     
    413413                 
    414414 
    415                 int r = NSRunAlertPanel(local(@"UnknownHostKey"), question, local(@"No"), local(@"Yes"), nil); 
     415                int r = NSRunAlertPanel( 
     416                        local(@"UnknownHostKey"),  
     417                        question, local(@"No"),  
     418                        local(@"Yes"), nil); 
    416419 
    417420                SetFrontProcess(&focusSerialNumber); 
     
    436439                                 
    437440                                if(keychainStatus & 1) { 
    438                                         returnStatus = SecKeychainFindGenericPassword(keychain, strlen(serviceName), serviceName, strlen(accountName), accountName, &passwordLength, (void **)&kcPassword, nil); 
     441                                        returnStatus = SecKeychainFindGenericPassword( 
     442                                                keychain, strlen(serviceName), serviceName,  
     443                                                strlen(accountName), accountName, &passwordLength,  
     444                                                (void **)&kcPassword, nil); 
    439445                                         
    440446                                        if(returnStatus == 0) { 
     
    449455                else 
    450456                { 
    451                         returnStatus = SecKeychainFindGenericPassword(nil, strlen(serviceName), serviceName, strlen(accountName), accountName, &passwordLength, (void **)&kcPassword, nil); 
     457                        returnStatus = SecKeychainFindGenericPassword( 
     458                                nil, strlen(serviceName), serviceName, strlen(accountName),  
     459                                accountName, &passwordLength, (void **)&kcPassword, nil); 
    452460                } 
    453461                 
     
    460468                if(returnStatus == 0) 
    461469                { 
    462                         kcPassword[passwordLength] = '\0'; 
    463  
    464                         NSString *returnString = [NSString stringWithCString:kcPassword]; 
    465  
    466                         SecKeychainItemFreeContent(NULL, kcPassword); 
     470                        NSString *returnString; 
     471                         
     472                        if ( kcPassword[passwordLength] != 0 ) { 
     473                                /* Don't trust memory allocated from system, copy it over 
     474                                First before making it a CString */ 
     475 
     476                                NSLog(@"Buggy password in keycahin workaround"); 
     477                                char * buffer = (char*)malloc((passwordLength+1)*sizeof(char)); 
     478                                strncpy(buffer, kcPassword, passwordLength); 
     479                                buffer[passwordLength] = '\0'; 
     480                         
     481 
     482                                returnString = [NSString stringWithUTF8String:buffer]; 
     483 
     484                                SecKeychainItemFreeContent(NULL, kcPassword); 
     485                                free(buffer); 
     486                        } else { 
     487                                returnString = [NSString stringWithUTF8String:kcPassword]; 
     488 
     489                                SecKeychainItemFreeContent(NULL, kcPassword); 
     490                        } 
    467491                         
    468492                        return returnString; 
     
    525549                 
    526550                /* Get the passphrase from the textfield. */ 
    527                 enteredPassphrase = CFUserNotificationGetResponseValue(notification, kCFUserNotificationTextFieldValuesKey, 0); 
     551                enteredPassphrase = CFUserNotificationGetResponseValue(notification,  
     552                        kCFUserNotificationTextFieldValuesKey, 0); 
    528553 
    529554                if(enteredPassphrase != nil) 
     
    536561                                serviceName = "SSHKeychain"; 
    537562                                 
    538                                 SecKeychainAddGenericPassword(nil, strlen(serviceName), serviceName, strlen(accountName), accountName, [passphrase length], (const void *)[passphrase UTF8String], nil); 
     563                                const char * utf8password = [passphrase UTF8String]; 
     564                                 
     565                                SecKeychainAddGenericPassword(nil, strlen(serviceName),  
     566                                        serviceName, strlen(accountName), accountName,  
     567                                        strlen(utf8password) + 1,  
     568                                        (const void *)utf8password, nil); 
    539569                        } 
    540570