SKKeychain.m 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. //
  2. // SKKeychain.m
  3. // Skim
  4. //
  5. // Created by Christiaan on 29/01/2018.
  6. /*
  7. This software is Copyright (c) 2018
  8. Christiaan Hofman. All rights reserved.
  9. Redistribution and use in source and binary forms, with or without
  10. modification, are permitted provided that the following conditions
  11. are met:
  12. - Redistributions of source code must retain the above copyright
  13. notice, this list of conditions and the following disclaimer.
  14. - Redistributions in binary form must reproduce the above copyright
  15. notice, this list of conditions and the following disclaimer in
  16. the documentation and/or other materials provided with the
  17. distribution.
  18. - Neither the name of Christiaan Hofman nor the names of any
  19. contributors may be used to endorse or promote products derived
  20. from this software without specific prior written permission.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. */
  33. #import "SKKeychain.h"
  34. #import <Security/Security.h>
  35. @implementation SKKeychain
  36. + (SKPasswordStatus)getPassword:(NSString **)password item:(id *)itemPtr forService:(NSString *)service account:(NSString *)account {
  37. void *passwordData = NULL;
  38. UInt32 passwordLength = 0;
  39. const char *serviceData = [service UTF8String];
  40. const char *accountData = [account UTF8String];
  41. OSStatus err = SecKeychainFindGenericPassword(NULL, strlen(serviceData), serviceData, strlen(accountData), accountData, password ? &passwordLength : NULL, password ? &passwordData : NULL, NULL);
  42. if (err == noErr) {
  43. if (password) {
  44. *password = [[NSString alloc] initWithBytes:passwordData length:passwordLength encoding:NSUTF8StringEncoding];
  45. SecKeychainItemFreeContent(NULL, passwordData);
  46. }
  47. // if (itemPtr)
  48. // [*itemPtr autorelease];
  49. return SKPasswordStatusFound;
  50. } else if (err == errSecItemNotFound) {
  51. return SKPasswordStatusNotFound;
  52. } else {
  53. if (err != errSecUserCanceled)
  54. NSLog(@"Error %d occurred finding password: %@", (int)err, SecCopyErrorMessageString(err, NULL));
  55. return SKPasswordStatusError;
  56. }
  57. }
  58. static inline SecKeychainAttribute makeKeychainAttribute(SecKeychainAttrType tag, NSString *string) {
  59. const char *data = [string UTF8String];
  60. SecKeychainAttribute attr;
  61. attr.tag = tag;
  62. attr.length = strlen(data);
  63. attr.data = (void *)data;
  64. return attr;
  65. }
  66. + (void)setPassword:(NSString *)password item:(id)item forService:(NSString *)service account:(NSString *)account label:(NSString *)label comment:(NSString *)comment {
  67. const void *passwordData = [password UTF8String];
  68. UInt32 passwordLength = password ? strlen(passwordData) : 0;
  69. NSUInteger attrCount = 2;
  70. SecKeychainAttributeList attributes;
  71. SecKeychainAttribute attrs[4];
  72. OSStatus err;
  73. attrs[0] = makeKeychainAttribute(kSecServiceItemAttr, service);
  74. attrs[1] = makeKeychainAttribute(kSecAccountItemAttr, account);
  75. if (label)
  76. attrs[attrCount++] = makeKeychainAttribute(kSecLabelItemAttr, label);
  77. if (comment)
  78. attrs[attrCount++] = makeKeychainAttribute(kSecCommentItemAttr, comment);
  79. attributes.count = attrCount;
  80. attributes.attr = attrs;
  81. if (item) {
  82. // password was on keychain, so modify the keychain
  83. err = SecKeychainItemModifyAttributesAndData((SecKeychainItemRef)CFBridgingRetain(item), &attributes, passwordLength, passwordData);
  84. if (err != noErr && err != errSecUserCanceled)
  85. NSLog(@"Error %d occurred modifying password: %@", (int)err, SecCopyErrorMessageString(err, NULL));
  86. } else if (password) {
  87. // password not on keychain, so add it
  88. err = SecKeychainItemCreateFromContent(kSecGenericPasswordItemClass, &attributes, passwordLength, passwordData, NULL, NULL, NULL);
  89. if (err != noErr && err != errSecUserCanceled)
  90. NSLog(@"Error %d occurred adding password: %@", (int)err, SecCopyErrorMessageString(err, NULL));
  91. }
  92. }
  93. @end