Удалить все доверенные приложения из элемента цепочки для ключей - PullRequest
0 голосов
/ 11 апреля 2020

Я написал приведенный ниже код, чтобы добавить общий пароль c из моего приложения. SecKeychainAddGenericPassword () добавляет приложение в качестве доверенного приложения в связку ключей в соответствии с дизайном. Я хочу удалить свое приложение из списка доверенных приложений. Я вызвал SecKeychainItemSetAccess (), чтобы сделать это, но я все еще вижу свое приложение в списке доверенных приложений.

 addgenericpassword(const std::string& service,const std::string& account,
const std::string& password) {
      SecKeychainItemRef item_ref;
      OSStatus status = SecKeychainAddGenericPassword(NULL,
                                                      service.length(),
                                                      service.data(),
                                                      account.length(),
                                                      account.data(),
                                                      password.length(),
                                                      password.data(),
                                                      &item_ref);

    //Creating an secAccess object that has an empty trusted application list
    //https://developer.apple.com/documentation/security/1393522-secaccesscreate?language=objc
      CFArrayRef applicationList=CFArrayCreate (NULL,NULL,0,NULL);
      SecAccessRef accessref;
      CFStringRef description=CFStringCreateWithCString(NULL, "Generic description", kCFStringEncodingASCII);

      status = SecAccessCreate(description,applicationList,&accessref);

      //Set the access of a keychain item "item_ref".
      status = SecKeychainItemSetAccess(item_ref,accessref);

      CFRelease(item_ref);
      CFRelease(accessref);
      CFRelease(applicationList);
      CFRelease(description);
      return 0;
    }

Обновление: изменено описание в соответствии с именем службы. Все еще не повезло

CFStringRef description=CFStringCreateWithCString(NULL, service.data(), kCFStringEncodingASCII);

1 Ответ

0 голосов
/ 11 апреля 2020

Мне удалось получить требуемую функциональность. Однако я не уверен, что это правильный способ сделать это.

    SecAccessRef accessref;
    SecKeychainItemCopyAccess(item_ref, &accessref);
    CFArrayRef aclList;
    SecAccessCopyACLList(accessref, &aclList);

    CFIndex count = CFArrayGetCount(aclList);
    //Array with 0 Applications / Empty Array . Not the same as passing NULL
    CFArrayRef zero_applications = CFArrayCreate(NULL, NULL, 0, NULL);
    for (int i = 0; i < count; i++) {
        SecACLRef acl = (SecACLRef) CFArrayGetValueAtIndex(aclList, i);
        CFArrayRef applicationList;
        CFStringRef description;
        CSSM_ACL_KEYCHAIN_PROMPT_SELECTOR promptSelector;
        SecACLCopySimpleContents(acl, &applicationList, &description,
                                  &promptSelector);
       if (applicationList == NULL) {
         continue;
       }
        CFIndex appCount = CFArrayGetCount(applicationList);

        for (int j = 0; j < appCount; j++) {
          status= SecACLSetContents(acl, zero_applications, description, 1);
          break;
        }
        CFRelease(applicationList);
        CFRelease(description);
    }

  // Set the modified copy to the item now
  status = SecKeychainItemSetAccess(item_ref, accessref);
...