c++ - SecKeychainFindGenericPassord 因 errSecItemNotFound 而失败
问题描述
errSecItemNotFound
尝试访问钥匙串以在 MacOs-Catalina 上检索代理凭据,由于或“无法在钥匙串中找到指定的项目” ,我无法检索任何内容。
但是,通过 访问钥匙串Keychain Access
,我可以过滤并查看给定的凭据。
问题最终是:如何访问钥匙串来检索代理凭据?
但更具体地说:我缺少什么概念?这有什么问题?
这是用于测试的最小代码:
#include <iostream>
#include <CoreFoundation/CoreFoundation.h>
#include <Security/Security.h>
#include <Security/SecKeychain.h>
using namespace std;
/// CFSTR equivalent, not throwing old-c cast warning.
#define CFSTRv2(cStr) (reinterpret_cast<const CFStringRef>(\
__builtin___CFStringMakeConstantString ("" cStr "")))
std::string getPasswordForService(const std::string& service)
{
std::string account="collector";
char* buffer=nullptr;
uint32_t size=0;
auto errorCode = SecKeychainFindGenericPassword (
nullptr,//CFSTRv2("login"),
static_cast<uint32_t>(service.size()),
service.c_str(),
static_cast<uint32_t>(account.size()),
account.c_str(),
&size,
reinterpret_cast<void**>(&buffer),
nullptr);
if (errorCode != errSecSuccess)
{
//cerr << CFStringRefToStdString(SecCopyErrorMessageString(errorCode, nullptr),
// "no-error-string") << endl;
cerr << "Error returned: " << static_cast<int>(errorCode) << endl;
}
std::string result(buffer, size);
return result;
}
int main(int, char*[])
{
std::string service{"proxy_test"};
auto password = getPasswordForService(service);
cout << "Passoword for: " <<service << " : " << password << endl;
}
此外,这是 Keystore 的屏幕截图:
最后,只有当它让你的生活更轻松时,这里是 CMakeFile:
cmake_minimum_required(VERSION 3.0)
if (APPLE)
set(CMAKE_OSX_DEPLOYMENT_TARGET "10.12" CACHE STRING "Minimum OS X deployment version")
endif()
project("Password_support")
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -framework Foundation -framework Security")
find_library(CORE_FOUNDATION CoreFoundation)
set ( TESTCPP password.cpp )
set ( LIBS ${CORE_FOUNDATION})
if (APPLE)
add_executable( ${PROJECT_NAME} ${TESTCPP} )
target_link_libraries(${PROJECT_NAME} ${LIBS})
endif()
接下来,一些相关的问题有助于但没有回答我的问题:
解决方案
该问题与用于检索密码的功能有关:SecKeychainGenericPassword
. 代理是互联网技术,因此,kind
无论这意味着什么,密码都存储在另一个中。
检索这个的正确功能是SecKeychainFindInternetPassword
auto errorCode = SecKeychainFindInternetPassword (
nullptr,
static_cast<uint32_t>(service.size()),
service.c_str(),
static_cast<uint32_t>(0),
nullptr,
static_cast<uint32_t>(0),
nullptr,
static_cast<uint32_t>(0),
nullptr,
static_cast<uint16_t>(1080),
SecProtocolType::kSecProtocolTypeAny,
SecAuthenticationType::kSecAuthenticationTypeAny,
&size,
reinterpret_cast<void**>(&buffer),
nullptr);
心理提示:如果我的员工写了这么多参数的函数,就杀了他(开玩笑)
推荐阅读
- javascript - 无法读取未定义的属性“键”
- rabbitmq - 如何将 RabbitMQ ssl 端口配置为仅监听特定的 VPC 接口
- text - 如何清理回车和换行的 Base64 文件?
- sql - 为什么两条 SQL 语句的结果不同?
- javascript - 如何在 express、typescript 代码中解决这些更漂亮的错误
- docker - Docker from windows is unable to pull images from my private registry once Kubernetes is enabled
- android - jar 文件放在 Assets/Plugins/Android 的子文件夹下会不会被打包?
- javascript - 我的 if 语句做错了什么?
- android - How to get current build variant in kotlin
- hash-function - What is Perl's default hash implementation?