2 * Copyright (c) 2016 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the xLicense.
21 * @APPLE_LICENSE_HEADER_END@
26 #include "keychain_regressions.h"
27 #include "kc-helpers.h"
28 #include "kc-item-helpers.h"
30 #import <Foundation/Foundation.h>
31 #include <CoreFoundation/CoreFoundation.h>
32 #include <Security/Security.h>
33 #include <CoreServices/CoreServices.h>
35 //Call SecKeychainAddGenericPassword to add a new password to the keychain:
36 static OSStatus StorePasswordKeychain (SecKeychainRef keychain, void* password,UInt32 passwordLength)
39 status = SecKeychainAddGenericPassword (
41 10, // length of service name
42 "SurfWriter", // service name
43 10, // length of account name
44 "MyUserAcct", // account name
45 passwordLength, // length of password
46 password, // pointer to password data
47 NULL // the item reference
50 ok_status(status, "%s: SecKeychainAddGenericPassword", testName);
54 //Call SecKeychainFindGenericPassword to get a password from the keychain:
55 static OSStatus GetPasswordKeychain (SecKeychainRef keychain, void *passwordData,UInt32 *passwordLength,SecKeychainItemRef *itemRef)
60 status = SecKeychainFindGenericPassword (
62 10, // length of service name
63 "SurfWriter", // service name
64 10, // length of account name
65 "MyUserAcct", // account name
66 passwordLength, // length of password
67 passwordData, // pointer to password data
68 itemRef // the item reference
70 ok_status(status, "%s: SecKeychainFindGenericPassword", testName);
74 //Call SecKeychainItemModifyAttributesAndData to change the password for an item already in the keychain:
75 static OSStatus ChangePasswordKeychain (SecKeychainItemRef itemRef)
78 void * password = "myNewP4sSw0rD";
79 UInt32 passwordLength = (UInt32) strlen(password);
80 void * label = "New Item Label";
81 UInt32 labelLength = (UInt32) strlen(label);
83 NSString *account = @"New Account";
84 NSString *service = @"New Service";
85 const char *serviceUTF8 = [service UTF8String];
86 const char *accountUTF8 = [account UTF8String];
88 //%%% IMPORTANT: While SecKeychainItemCreateFromContent() will accept a kSecLabelItemAttr, it cannot
89 // be changed later via SecKeychainItemModifyAttributesAndData(). ##### THIS IS A BUG. #####
90 // To work around the bug, pass 7 instead of kSecLabelItemAttr. This value is the index of the label
91 // attribute in the database schema (and in the SecItemAttr enumeration).
93 //#define LABEL_ITEM_ATTR_TAG 7
94 #define LABEL_ITEM_ATTR_TAG kSecLabelItemAttr
96 // set up attribute vector (each attribute consists of {tag, length, pointer})
97 SecKeychainAttribute attrs[] = {
98 { LABEL_ITEM_ATTR_TAG, labelLength, (char *)label },
99 { kSecAccountItemAttr, (UInt32) strlen(accountUTF8), (char *)accountUTF8 },
100 { kSecServiceItemAttr, (UInt32) strlen(serviceUTF8), (char *)serviceUTF8 } };
101 const SecKeychainAttributeList attributes = { sizeof(attrs) / sizeof(attrs[0]), attrs };
104 status = SecKeychainItemModifyAttributesAndData (
105 itemRef, // the item reference
106 &attributes, // attributes to change
107 passwordLength, // length of password
108 password // pointer to password data
110 ok_status(status, "%s: SecKeychainItemModifyAttributesAndData", testName);
114 int kc_15_item_update_label_skimaad(int argc, char *const *argv)
117 initializeKeychainTests(__FUNCTION__);
119 SecKeychainRef keychain = getPopulatedTestKeychain();
121 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
125 CFStringRef theLabel = CFSTR("Notice");
127 CFStringRef theStatusStr = nil;
129 void * myPassword = "myP4sSw0rD";
130 UInt32 myPasswordLength = (UInt32) strlen(myPassword);
131 void *passwordData = nil; // will be allocated and filled in by SecKeychainFindGenericPassword
132 UInt32 passwordLength = 0;
133 SecKeychainItemRef itemRef = nil;
135 StorePasswordKeychain(keychain, myPassword, myPasswordLength);
137 itemRef = checkN(testName, makeQueryCustomItemDictionaryWithService(keychain, kSecClassGenericPassword, CFSTR("SurfWriter"), CFSTR("SurfWriter")), 1);
138 checkN(testName, makeQueryCustomItemDictionaryWithService(keychain, kSecClassGenericPassword, CFSTR("New Item Label"), CFSTR("New Service")), 0);
139 readPasswordContents(itemRef, CFSTR("myP4sSw0rD"));
140 CFReleaseNull(itemRef);
142 GetPasswordKeychain (keychain, &passwordData,&passwordLength,&itemRef); //Call SecKeychainFindGenericPassword
145 free the data allocated by SecKeychainFindGenericPassword:
147 status = SecKeychainItemFreeContent (
148 NULL, //No attribute data to release
149 passwordData //Release data buffer allocated by SecKeychainFindGenericPassword
151 ok_status(status, "%s: SecKeychainItemFreeContent", testName);
153 ChangePasswordKeychain(itemRef);
155 checkN(testName, makeQueryCustomItemDictionaryWithService(keychain, kSecClassGenericPassword, CFSTR("SurfWriter"), CFSTR("SurfWriter")), 0);
156 itemRef = checkN(testName, makeQueryCustomItemDictionaryWithService(keychain, kSecClassGenericPassword, CFSTR("New Item Label"), CFSTR("New Service")), 1);
157 readPasswordContents(itemRef, CFSTR("myNewP4sSw0rD"));
158 CFReleaseNull(itemRef);
162 checkPrompts(0, "no prompts during test");
164 ok_status(SecKeychainDelete(keychain), "%s: SecKeychainDelete", testName);
165 CFReleaseNull(keychain);