[Cyberduck-trac] [Cyberduck] #5669: third-part addInternetKeyChain for cyberduck

Cyberduck trac at trac.cyberduck.ch
Thu Jan 27 15:09:13 CET 2011

#5669: third-part addInternetKeyChain for cyberduck
    Reporter:  likun   |       Owner:  dkocher
        Type:  defect  |      Status:  closed
    Priority:  normal  |   Milestone:
   Component:  ftp     |     Version:  3.8.1
    Severity:  normal  |  Resolution:  thirdparty
    Keywords:          |    Platform:
Architecture:          |

Comment (by likun):

 //This is my code and the item i add. but it can't work.

 #import <Cocoa/Cocoa.h>
 #include <Security/SecKeychain.h>
 #include <Security/SecKeychainItem.h>
 #include <Security/SecAccess.h>
 #include <Security/SecTrustedApplication.h>
 #include <Security/SecACL.h>

 SecAccessRef createAccess(NSString *accessLabel)
     OSStatus err;
     SecAccessRef access=nil;
     NSArray *trustedApplications=nil;

     //Make an exception list of trusted applications; that is,
     // applications that are allowed to access the item without
     // requiring user confirmation:
     SecTrustedApplicationRef myself, someOther;
         //Create trusted application references; see
     err = SecTrustedApplicationCreateFromPath(NULL, &myself);
     err =
     trustedApplications = [NSArray arrayWithObjects:(id)myself,
 (id)someOther, nil];
         //Create an access object:
     err = SecAccessCreate((CFStringRef)accessLabel,
 (CFArrayRef)trustedApplications, &access);
     if (err) return nil;

     return access;

 void addInternetPassword(NSString *itemLabel, NSString *account, NSString
                                                  SecProtocolType protocol,
 NSString *server, int port, NSString *path)
     OSStatus err;
     SecKeychainItemRef item = nil;
     const char *itemLabelUTF8 = [itemLabel UTF8String];
     const char *accountUTF8 = [account UTF8String];
     const char *passwordUTF8 = [password UTF8String];
         const char *serverUTF8 = [server UTF8String];
         const char *pathUTF8 = [path UTF8String];

     //Create initial access control settings for the item:
     SecAccessRef access = createAccess(itemLabel);

     //Following is the lower-level equivalent to the
     // SecKeychainAddInternetPassword function:

     //Set up the attribute vector (each attribute consists
     // of {tag, length, pointer}):
     SecKeychainAttribute attrs[] = {
         { kSecLabelItemAttr, strlen(itemLabelUTF8), (char *)itemLabelUTF8
         { kSecAccountItemAttr, strlen(accountUTF8), (char *)accountUTF8 },
         { kSecServerItemAttr, strlen(serverUTF8), (char *)serverUTF8 },
         { kSecPortItemAttr, sizeof(int), (int *)&port },
         { kSecProtocolItemAttr, sizeof(SecProtocolType), (SecProtocolType
 *)&protocol },
         { kSecPathItemAttr, strlen(pathUTF8), (char *)pathUTF8 }
     SecKeychainAttributeList attributes = { sizeof(attrs) /
 sizeof(attrs[0]), attrs };

     err = SecKeychainItemCreateFromContent(kSecInternetPasswordItemClass,
 NULL, // use the default keychain
         //NSLog(@"%@", item);

     if (access) CFRelease(access);
     if (item) CFRelease(item);

 int main(int argc, const char *argv[])
     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

     addInternetPassword(@"", @"username", @"password",
 kSecProtocolTypeFTP, @"", 4321, @"");

     [pool release];

     return 0;

Ticket URL: <http://trac.cyberduck.ch/ticket/5669#comment:3>
Cyberduck <http://cyberduck.ch>
Open source FTP, SFTP, WebDAV, Cloud Files, Google Docs & Amazon S3 Browser for Mac & Windows.

More information about the Cyberduck-trac mailing list