kicad/libs/kiplatform/osx/secrets.mm

76 lines
3.4 KiB
Plaintext
Raw Normal View History

/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2023 KiCad Developers, see AUTHORS.txt for contributors.
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <kiplatform/secrets.h>
#import <Security/Security.h>
2023-10-20 14:10:49 +00:00
bool KIPLATFORM::SECRETS::StoreSecret(const wxString& aService, const wxString& aKey, const wxString& aValue)
{
2023-10-20 14:10:49 +00:00
// Create a query for the secret
CFMutableDictionaryRef query = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
CFDictionarySetValue(query, kSecClass, kSecClassGenericPassword);
CFDictionarySetValue(query, kSecAttrService, CFStringCreateWithCString(NULL, aService.utf8_str(), kCFStringEncodingUTF8));
CFDictionarySetValue(query, kSecAttrAccount, CFStringCreateWithCString(NULL, aKey.utf8_str(), kCFStringEncodingUTF8));
2023-10-20 14:10:49 +00:00
// Try to find the existing item
OSStatus status = SecItemCopyMatching(query, NULL);
2023-10-20 14:10:49 +00:00
if (status == errSecItemNotFound)
{
2023-10-20 14:10:49 +00:00
// Add the new secret to the keychain
CFDictionarySetValue(query, kSecValueData, CFDataCreate(NULL, (const UInt8*)aValue.utf8_str(), aValue.length()));
status = SecItemAdd(query, NULL);
}
2023-10-20 14:10:49 +00:00
else if (status == errSecSuccess)
{
2023-10-20 14:10:49 +00:00
// Update the existing secret in the keychain
CFMutableDictionaryRef updateQuery = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
CFDictionarySetValue(updateQuery, kSecValueData, CFDataCreate(NULL, (const UInt8*)aValue.utf8_str(), aValue.length()));
status = SecItemUpdate(query, updateQuery);
CFRelease(updateQuery);
}
2023-10-20 14:10:49 +00:00
CFRelease(query);
return status == errSecSuccess;
}
2023-10-20 14:10:49 +00:00
bool KIPLATFORM::SECRETS::GetSecret(const wxString& aService, const wxString& aKey, wxString& aValue)
{
2023-10-20 14:10:49 +00:00
// Create a query for the secret
CFMutableDictionaryRef query = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
CFDictionarySetValue(query, kSecClass, kSecClassGenericPassword);
CFDictionarySetValue(query, kSecAttrService, CFStringCreateWithCString(NULL, aService.utf8_str(), kCFStringEncodingUTF8));
CFDictionarySetValue(query, kSecAttrAccount, CFStringCreateWithCString(NULL, aKey.utf8_str(), kCFStringEncodingUTF8));
CFDictionarySetValue(query, kSecReturnData, kCFBooleanTrue); // Return the secret data
2023-10-20 14:10:49 +00:00
// Retrieve the secret from the keychain
CFDataRef secretData = NULL;
OSStatus status = SecItemCopyMatching(query, (CFTypeRef*)&secretData);
2023-10-20 14:10:49 +00:00
if (status == errSecSuccess)
{
2023-10-20 14:10:49 +00:00
aValue = wxString::FromUTF8((const char*)CFDataGetBytePtr(secretData), CFDataGetLength(secretData));
CFRelease(secretData);
}
2023-10-20 14:10:49 +00:00
CFRelease(query);
return status == errSecSuccess;
}