+ // is it token login?
+ CFRef<CFDictionaryRef> tokenLoginContext;
+ CFRef<CFStringRef> smartCardPassword;
+ OSStatus tokenContextStatus = TokenLoginGetContext(password, passwordLength, tokenLoginContext.take());
+ // if login.keychain does not exist at this point, create it
+ if (!loginKeychainExists || (isReset && !loginKeychainDbExists)) {
+ // when we creating new KC and user is logged using token (i.e. smart card), we have to get
+ // the password for that account first
+ if (tokenContextStatus == errSecSuccess) {
+ secnotice("KCLogin", "Going to create login keychain for sc login");
+ AuthorizationRef authRef;
+ OSStatus status = AuthorizationCreate(NULL, NULL, 0, &authRef);
+ if (status == errSecSuccess) {
+ AuthorizationItem right = { "com.apple.builtin.sc-kc-new-passphrase", 0, NULL, 0 };
+ AuthorizationItemSet rightSet = { 1, &right };
+
+ uint32_t reason, tries;
+ reason = 0;
+ tries = 0;
+ AuthorizationItem envRights[] = {
+ { AGENT_HINT_RETRY_REASON, sizeof(reason), &reason, 0 },
+ { AGENT_HINT_TRIES, sizeof(tries), &tries, 0 }};
+
+ AuthorizationItemSet envSet = { sizeof(envRights) / sizeof(*envRights), envRights };
+ status = AuthorizationCopyRights(authRef, &rightSet, &envSet, kAuthorizationFlagDefaults|kAuthorizationFlagInteractionAllowed|kAuthorizationFlagExtendRights, NULL);
+ if (status == errSecSuccess) {
+ AuthorizationItemSet *returnedInfo;
+ status = AuthorizationCopyInfo(authRef, NULL, &returnedInfo);
+ if (status == errSecSuccess) {
+ if (returnedInfo && (returnedInfo->count > 0)) {
+ for (uint32_t index = 0; index < returnedInfo->count; index++) {
+ AuthorizationItem &item = returnedInfo->items[index];
+ if (!strcmp(AGENT_PASSWORD, item.name)) {
+ CFIndex len = item.valueLength;
+ if (len) {
+ secnotice("KCLogin", "User entered pwd");
+ smartCardPassword = CFStringCreateWithBytes(SecCFAllocatorZeroize(), (UInt8 *)item.value, (CFIndex)len, kCFStringEncodingUTF8, TRUE);
+ memset(item.value, 0, len);
+ }
+ }
+ }
+ }
+ }
+ if(returnedInfo) {
+ AuthorizationFreeItemSet(returnedInfo);
+ }
+ }
+ AuthorizationFree(authRef, 0);
+ }
+ }
+