]> git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_keychain/regressions/kc-15-item-update-label-skimaad.m
Security-59754.80.3.tar.gz
[apple/security.git] / OSX / libsecurity_keychain / regressions / kc-15-item-update-label-skimaad.m
1 /*
2 * Copyright (c) 2016 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
11 * file.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 // <rdar://3425797>
25
26 #include "keychain_regressions.h"
27 #include "kc-helpers.h"
28 #include "kc-item-helpers.h"
29
30 #import <Foundation/Foundation.h>
31 #include <CoreFoundation/CoreFoundation.h>
32 #include <Security/Security.h>
33 #include <CoreServices/CoreServices.h>
34
35 //Call SecKeychainAddGenericPassword to add a new password to the keychain:
36 static OSStatus StorePasswordKeychain (SecKeychainRef keychain, void* password,UInt32 passwordLength)
37 {
38 OSStatus status;
39 status = SecKeychainAddGenericPassword (
40 keychain,
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
48 );
49
50 ok_status(status, "%s: SecKeychainAddGenericPassword", testName);
51 return (status);
52 }
53
54 //Call SecKeychainFindGenericPassword to get a password from the keychain:
55 static OSStatus GetPasswordKeychain (SecKeychainRef keychain, void *passwordData,UInt32 *passwordLength,SecKeychainItemRef *itemRef)
56 {
57 OSStatus status;
58
59
60 status = SecKeychainFindGenericPassword (
61 keychain,
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
69 );
70 ok_status(status, "%s: SecKeychainFindGenericPassword", testName);
71 return (status);
72 }
73
74 //Call SecKeychainItemModifyAttributesAndData to change the password for an item already in the keychain:
75 static OSStatus ChangePasswordKeychain (SecKeychainItemRef itemRef)
76 {
77 OSStatus status;
78 void * password = "myNewP4sSw0rD";
79 UInt32 passwordLength = (UInt32) strlen(password);
80 void * label = "New Item Label";
81 UInt32 labelLength = (UInt32) strlen(label);
82
83 NSString *account = @"New Account";
84 NSString *service = @"New Service";
85 const char *serviceUTF8 = [service UTF8String];
86 const char *accountUTF8 = [account UTF8String];
87
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).
92 //
93 //#define LABEL_ITEM_ATTR_TAG 7
94 #define LABEL_ITEM_ATTR_TAG kSecLabelItemAttr
95
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 };
102
103
104 status = SecKeychainItemModifyAttributesAndData (
105 itemRef, // the item reference
106 &attributes, // attributes to change
107 passwordLength, // length of password
108 password // pointer to password data
109 );
110 ok_status(status, "%s: SecKeychainItemModifyAttributesAndData", testName);
111 return (status);
112 }
113
114 int kc_15_item_update_label_skimaad(int argc, char *const *argv)
115 {
116 plan_tests(28);
117 initializeKeychainTests(__FUNCTION__);
118
119 SecKeychainRef keychain = getPopulatedTestKeychain();
120
121 OSStatus status;
122
123 void * myPassword = "myP4sSw0rD";
124 UInt32 myPasswordLength = (UInt32) strlen(myPassword);
125 void *passwordData = nil; // will be allocated and filled in by SecKeychainFindGenericPassword
126 UInt32 passwordLength = 0;
127 SecKeychainItemRef itemRef = nil;
128
129 StorePasswordKeychain(keychain, myPassword, myPasswordLength);
130
131 itemRef = checkNCopyFirst(testName, createQueryCustomItemDictionaryWithService(keychain, kSecClassGenericPassword, CFSTR("SurfWriter"), CFSTR("SurfWriter")), 1);
132 checkN(testName, createQueryCustomItemDictionaryWithService(keychain, kSecClassGenericPassword, CFSTR("New Item Label"), CFSTR("New Service")), 0);
133 readPasswordContents(itemRef, CFSTR("myP4sSw0rD"));
134 CFReleaseNull(itemRef);
135
136 GetPasswordKeychain (keychain, &passwordData,&passwordLength,&itemRef); //Call SecKeychainFindGenericPassword
137
138 ok(passwordData, "Recieved password data");
139 /*
140 free the data allocated by SecKeychainFindGenericPassword:
141 */
142 status = SecKeychainItemFreeContent (
143 NULL, //No attribute data to release
144 passwordData //Release data buffer allocated by SecKeychainFindGenericPassword
145 );
146 ok_status(status, "%s: SecKeychainItemFreeContent", testName);
147
148 ChangePasswordKeychain(itemRef);
149
150 checkN(testName, createQueryCustomItemDictionaryWithService(keychain, kSecClassGenericPassword, CFSTR("SurfWriter"), CFSTR("SurfWriter")), 0);
151 itemRef = checkNCopyFirst(testName, createQueryCustomItemDictionaryWithService(keychain, kSecClassGenericPassword, CFSTR("New Item Label"), CFSTR("New Service")), 1);
152 readPasswordContents(itemRef, CFSTR("myNewP4sSw0rD"));
153 CFReleaseNull(itemRef);
154
155 checkPrompts(0, "no prompts during test");
156
157 ok_status(SecKeychainDelete(keychain), "%s: SecKeychainDelete", testName);
158 CFReleaseNull(keychain);
159
160 deleteTestFiles();
161 return 0;
162 }