Revision: 7027
http://skim-app.svn.sourceforge.net/skim-app/?rev=7027&view=rev
Author: hofman
Date: 2010-12-30 23:48:52 +0000 (Thu, 30 Dec 2010)
Log Message:
-----------
More appropriate way to store PDF passwords in keychain. Use "Skim PDF
password" as service and the file ID as account. Makes it possible to copy
keychain items to a different account, as they don't depend on the user name.
Also more user friendly label. Backward compatible with old style keychain
items.
Modified Paths:
--------------
trunk/SKMainDocument.m
Modified: trunk/SKMainDocument.m
===================================================================
--- trunk/SKMainDocument.m 2010-12-30 23:14:56 UTC (rev 7026)
+++ trunk/SKMainDocument.m 2010-12-30 23:48:52 UTC (rev 7027)
@@ -107,7 +107,9 @@
#define SKTagsKey @"Tags"
#define SKRatingKey @"Rating"
+static NSString *SKPDFPasswordServiceName = @"Skim PDF password";
+
@interface PDFAnnotation (SKPrivateDeclarations)
- (void)setPage:(PDFPage *)newPage;
@end
@@ -1562,49 +1564,63 @@
#pragma mark Passwords
-- (const char *)keychainServiceName {
- NSArray *fileIDStrings = [self fileIDStrings];
- if ([fileIDStrings count])
- return [[@"Skim - " stringByAppendingString:[fileIDStrings
objectAtIndex:0]] UTF8String];
- NSString *md5HexString = [pdfData md5String];
- if (md5HexString)
- return [[@"Skim - " stringByAppendingString:md5HexString] UTF8String];
- return NULL;
+static OSStatus findPDFPassword(NSString *fileID, UInt32 *passwordLength, void
**passwordData, SecKeychainItemRef *itemRef) {
+ const char *service = [SKPDFPasswordServiceName UTF8String];
+ const char *account = [fileID UTF8String];
+
+ OSStatus err = SecKeychainFindGenericPassword(NULL, strlen(service),
service, strlen(account), account, passwordLength, passwordData, itemRef);
+
+ if (err == errSecItemNotFound) {
+ // try to find an item in the old format
+ service = [[@"Skim - " stringByAppendingString:fileID] UTF8String];
+ account = [NSUserName() UTF8String];
+
+ err = SecKeychainFindGenericPassword(NULL, strlen(service), service,
strlen(account), account, passwordLength, passwordData, itemRef);
+ }
+
+ return err;
}
+static inline SecKeychainAttribute makeKeychainAttribute(SecKeychainAttrType
tag, NSString *string) {
+ const char *data = [string UTF8String];
+ SecKeychainAttribute attr;
+ attr.tag = tag;
+ attr.length = strlen(data);
+ attr.data = (void *)data;
+ return attr;
+}
+
- (void)passwordAlertDidEnd:(NSAlert *)alert returnCode:(NSInteger)returnCode
contextInfo:(void *)contextInfo {
NSString *password = [(NSString *)contextInfo autorelease];
if (returnCode == NSAlertDefaultReturn) {
- const char *serviceName = [self keychainServiceName];
- if (serviceName != NULL) {
- const char *userName = [NSUserName() UTF8String];
- const char *comment = [[[self fileURL] path] UTF8String];
+ NSString *fileID = [[self fileIDStrings] lastObject] ?: [pdfData
md5String];
+ if (fileID) {
+ // first see if the password exists in the keychain
+ SecKeychainItemRef itemRef = NULL;
+ OSStatus err = findPDFPassword(fileID, NULL, NULL, &itemRef);
- OSStatus err;
- SecKeychainItemRef itemRef = NULL;
const void *passwordData = [password UTF8String];
+ NSString *comment = [[self fileURL] path];
+ NSUInteger attrCount = comment ? 4 : 3;
+ SecKeychainAttributeList attributes;
+ SecKeychainAttribute attrs[attrCount];
- // first see if the password exists in the keychain
- err = SecKeychainFindGenericPassword(NULL, strlen(serviceName),
serviceName, strlen(userName), userName, NULL, NULL, &itemRef);
+ attrs[0] = makeKeychainAttribute(kSecServiceItemAttr,
SKPDFPasswordServiceName);
+ attrs[1] = makeKeychainAttribute(kSecAccountItemAttr, fileID);
+ attrs[2] = makeKeychainAttribute(kSecLabelItemAttr, [@"Skim: "
stringByAppendingString:[self displayName]]);
+ if (comment)
+ attrs[3] = makeKeychainAttribute(kSecCommentItemAttr, comment);
- if(err == noErr){
+ attributes.count = attrCount;
+ attributes.attr = attrs;
+
+ if (err == noErr)
// password was on keychain, so modify the keychain
- SecKeychainAttribute attrs[] = { { kSecCommentItemAttr,
comment == NULL ? 0 : strlen(comment), (char *)comment } };
- const SecKeychainAttributeList attributes = { comment == NULL
? 0 : 1, attrs };
-
- err = SecKeychainItemModifyAttributesAndData(itemRef, (comment
== NULL ? NULL : &attributes), strlen(passwordData), passwordData);
- } else if(err == errSecItemNotFound){
+ err = SecKeychainItemModifyAttributesAndData(itemRef,
&attributes, strlen(passwordData), passwordData);
+ else if(err == errSecItemNotFound)
// password not on keychain, so add it
- err = SecKeychainAddGenericPassword(NULL, strlen(serviceName),
serviceName, strlen(userName), userName, strlen(passwordData), passwordData,
&itemRef);
- if (err == noErr && comment != NULL) {
- SecKeychainAttribute attrs[] = { { kSecCommentItemAttr,
strlen(comment), (char *)comment } };
- const SecKeychainAttributeList attributes = { 1, attrs };
-
- err = SecKeychainItemModifyAttributesAndData(itemRef,
&attributes, strlen(passwordData), passwordData);
- }
- // accroding to the docs we should release the itemRef, even
though the function name does not contain Copy or Create
- SKCFDESTROY(itemRef);
- } else
+ err =
SecKeychainItemCreateFromContent(kSecGenericPasswordItemClass, &attributes,
strlen(passwordData), passwordData, NULL, NULL, NULL);
+ else
NSLog(@"Error %d occurred setting password", err);
}
}
@@ -1633,19 +1649,18 @@
- (BOOL)tryToUnlockDocument:(PDFDocument *)document {
if (NSAlertAlternateReturn != [[NSUserDefaults standardUserDefaults]
integerForKey:SKSavePasswordOptionKey]) {
- const char *serviceName = [self keychainServiceName];
- if (serviceName != NULL) {
- const char *userName = [NSUserName() UTF8String];
+ NSString *fileID = [[self fileIDStrings] lastObject] ?: [pdfData
md5String];
+ if (fileID) {
void *passwordData = NULL;
UInt32 passwordLength = 0;
- NSData *data = nil;
- NSString *aPassword = nil;
- OSErr err = SecKeychainFindGenericPassword(NULL,
strlen(serviceName), serviceName, strlen(userName), userName, &passwordLength,
&passwordData, NULL);
+ OSErr err = findPDFPassword(fileID, &passwordLength,
&passwordData, NULL);
+
if (err == noErr) {
- data = [NSData dataWithBytes:passwordData
length:passwordLength];
+ NSString *password = [[[NSString alloc]
initWithBytes:passwordData length:passwordLength encoding:NSUTF8StringEncoding]
autorelease];
+
SecKeychainItemFreeContent(NULL, passwordData);
- aPassword = [[[NSString alloc] initWithData:data
encoding:NSUTF8StringEncoding] autorelease];
- return [document unlockWithPassword:aPassword];
+
+ return [document unlockWithPassword:password];
}
}
}
This was sent by the SourceForge.net collaborative development platform, the
world's largest Open Source development site.
------------------------------------------------------------------------------
Learn how Oracle Real Application Clusters (RAC) One Node allows customers
to consolidate database storage, standardize their database environment, and,
should the need arise, upgrade to a full multi-node Oracle RAC database
without downtime or disruption
http://p.sf.net/sfu/oracle-sfdevnl
_______________________________________________
Skim-app-commit mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/skim-app-commit