]>
git.saurik.com Git - apple/security.git/blob - OSX/sec/Security/Regressions/secitem/si-76-shared-credentials.c
2 // si-76-shared-credentials.c
7 #include <CoreFoundation/CoreFoundation.h>
8 #include <Security/SecBasePriv.h>
9 #include <Security/SecCertificate.h>
10 #include <Security/SecCertificatePriv.h>
11 #include <Security/SecCertificateInternal.h>
12 #include <Security/SecItem.h>
13 #include <Security/SecItemPriv.h>
14 #include <Security/SecIdentityPriv.h>
15 #include <Security/SecIdentity.h>
16 #include <Security/SecPolicy.h>
17 #include <Security/SecPolicyPriv.h>
18 #include <Security/SecPolicyInternal.h>
19 #include <Security/SecSharedCredential.h>
20 #include <Security/SecCMS.h>
21 #include <utilities/SecCFWrappers.h>
25 #include "Security_regressions.h"
29 #define WAIT_WHILE(X) { while ((X)) { (void)CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.1, TRUE); } }
31 static bool expected_failure(OSStatus status
)
33 return ((status
== errSecMissingEntitlement
) ||
34 (status
== errSecBadReq
));
37 static void tests(void)
39 // look up our entry for localhost
40 CFStringRef acct1
= CFSTR("local");
41 CFStringRef acct2
= CFSTR("admin");
42 CFStringRef fqdn
= CFSTR("localhost");
43 CFStringRef not_my_fqdn
= CFSTR("store.apple.com"); // something we aren't entitled to share
45 __block
bool requesting
;
46 __block
bool deleting
;
48 // UInt8 buf[6] = { 'l', 'o', 'c', 'a', 'l', '\0' };
49 // CFDataRef cred = CFDataCreate(kCFAllocatorDefault, (const UInt8 *)&buf, sizeof(buf));
50 CFStringRef cred
= CFStringCreateCopy(kCFAllocatorDefault
, CFSTR("local"));
52 // should get denied if we request a fqdn which is not in our entitlement
54 SecRequestSharedWebCredential(not_my_fqdn
, NULL
, ^void (CFArrayRef credentials
, CFErrorRef error
) {
55 OSStatus status
= (OSStatus
)((error
) ? CFErrorGetCode(error
) : errSecSuccess
);
56 is(status
== errSecItemNotFound
|| expected_failure(status
), true, "fqdn not entitled");
57 is(CFArrayGetCount(credentials
) > 0, false, "returned credential array == 0");
60 WAIT_WHILE(requesting
);
62 // add (or update) credentials for two different accounts on the same server
64 SecAddSharedWebCredential(fqdn
, acct1
, cred
, ^void (CFErrorRef error
) {
65 OSStatus status
= (OSStatus
)((error
) ? CFErrorGetCode(error
) : errSecSuccess
);
66 // TODO: need a proper teamID-enabled application identifier to succeed; expect auth failure
67 if (status
== errSecAuthFailed
|| expected_failure(status
)) { status
= errSecSuccess
; }
74 SecAddSharedWebCredential(fqdn
, acct2
, cred
, ^void (CFErrorRef error
) {
75 OSStatus status
= (OSStatus
)((error
) ? CFErrorGetCode(error
) : errSecSuccess
);
76 // TODO: need a proper teamID-enabled application identifier to succeed; expect auth failure
77 if (status
== errSecAuthFailed
|| expected_failure(status
)) { status
= errSecSuccess
; }
83 // look up credential with specific account
85 SecRequestSharedWebCredential(fqdn
, acct1
, ^void (CFArrayRef credentials
, CFErrorRef error
) {
86 OSStatus status
= (OSStatus
)((error
) ? CFErrorGetCode(error
) : errSecSuccess
);
87 // TODO: need a proper teamID-enabled application identifier to succeed; expect no items
88 bool notFound
= false;
89 if (status
== errSecItemNotFound
|| expected_failure(status
)) {
90 status
= errSecSuccess
; notFound
= true;
94 // should find only one credential if a specific account is provided
95 CFIndex credentialCount
= CFArrayGetCount(credentials
);
96 // TODO: need a proper teamID-enabled application identifier to succeed; expect 0 items
97 if (credentialCount
== 0 && notFound
) { credentialCount
= 1; }
98 is(credentialCount
== 1, true, "returned credentials == 1");
101 WAIT_WHILE(requesting
);
103 // look up credential with NULL account parameter
105 SecRequestSharedWebCredential(fqdn
, NULL
, ^void (CFArrayRef credentials
, CFErrorRef error
) {
106 OSStatus status
= (OSStatus
)((error
) ? CFErrorGetCode(error
) : errSecSuccess
);
107 // TODO: need a proper teamID-enabled application identifier to succeed; expect auth failure
108 bool notFound
= false;
109 if (status
== errSecItemNotFound
|| expected_failure(status
)) {
110 status
= errSecSuccess
; notFound
= true;
114 // should find only one credential if no account is provided
115 // (since UI dialog only permits one credential to be selected)
116 CFIndex credentialCount
= CFArrayGetCount(credentials
);
117 // TODO: need a proper teamID-enabled application identifier to succeed
118 if (credentialCount
== 0 && notFound
) { credentialCount
= 1; }
119 is(credentialCount
== 1, true, "returned credentials == 1");
122 WAIT_WHILE(requesting
);
124 // pass NULL to delete our credentials
126 SecAddSharedWebCredential(fqdn
, acct1
, NULL
, ^void (CFErrorRef error
) {
127 OSStatus status
= (OSStatus
)((error
) ? CFErrorGetCode(error
) : errSecSuccess
);
128 // TODO: need a proper teamID-enabled application identifier to succeed; expect auth failure
129 if (status
== errSecAuthFailed
|| expected_failure(status
)) { status
= errSecSuccess
; }
133 WAIT_WHILE(deleting
);
136 SecAddSharedWebCredential(fqdn
, acct2
, NULL
, ^void (CFErrorRef error
) {
137 OSStatus status
= (OSStatus
)((error
) ? CFErrorGetCode(error
) : errSecSuccess
);
138 // TODO: need a proper teamID-enabled application identifier to succeed; expect auth failure
139 if (status
== errSecAuthFailed
|| expected_failure(status
)) { status
= errSecSuccess
; }
143 WAIT_WHILE(deleting
);
145 // look up credentials again; should find nothing this time
147 SecRequestSharedWebCredential(fqdn
, NULL
, ^void (CFArrayRef credentials
, CFErrorRef error
) {
148 OSStatus status
= (OSStatus
)((error
) ? CFErrorGetCode(error
) : errSecSuccess
);
149 // TODO: need a proper teamID-enabled application identifier to succeed; expect auth failure
150 if (status
== errSecAuthFailed
|| expected_failure(status
)) { status
= errSecItemNotFound
; }
151 is_status(status
, errSecItemNotFound
);
152 is(CFArrayGetCount(credentials
) > 0, false, "returned credential array == 0");
155 WAIT_WHILE(requesting
);
160 #endif // !TARGET_OS_WATCH
162 int si_76_shared_credentials(int argc
, char *const *argv
)