Last active
June 1, 2016 20:19
-
-
Save espresso3389/47c5ebf5d7b6edd9b9606417793f94c7 to your computer and use it in GitHub Desktop.
Key-chain Access Test (C++)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
#include <stdlib.h> | |
#include <Security/SecKeychain.h> | |
#include <Security/SecKeychainItem.h> | |
#include <Security/SecKeychainSearch.h> | |
#include <Security/SecItem.h> | |
void setCredential(); | |
class CredentialStore | |
{ | |
public: | |
CredentialStore(const char* protocol, const char* hostname, int port, const char* realm) | |
{ | |
asprintf(&m_idBase, "%s://%s:%d/%s/", protocol, hostname, port, realm); | |
} | |
void setCredential(const char* userId, const char* password) | |
{ | |
// Account | |
SecKeychainAttribute attrs[5]; | |
SecKeychainAttribute* pattr = attrs; | |
pattr->tag = kSecAccountItemAttr; | |
pattr->data = (void *)m_idBase; | |
pattr->length = strlen(m_idBase); | |
pattr++; | |
// Kind | |
// a user-visible string describing this particular kind of item | |
static const char* DESCRIPTION = "Shown in Kind box."; | |
pattr->tag = kSecDescriptionItemAttr; | |
pattr->data = (void *)DESCRIPTION; | |
pattr->length = strlen(DESCRIPTION); | |
pattr++; | |
// Name | |
// a user-editable string containing the label for this item | |
static const char* LABEL = "Shown in Name box."; | |
pattr->tag = kSecLabelItemAttr; | |
pattr->data = (void *)LABEL; | |
pattr->length = strlen(LABEL); | |
pattr++; | |
// | |
static const char* CUSTOMDATA = "Custom data."; | |
pattr->tag = kSecGenericItemAttr; | |
pattr->data = (void *)CUSTOMDATA; | |
pattr->length = strlen(CUSTOMDATA); | |
pattr++; | |
/* | |
// Where | |
static const char* WHERE = "Shown in Where box."; | |
pattr->tag = kSecAddressItemAttr; | |
pattr->data = (void *)WHERE; | |
pattr->length = strlen(WHERE); | |
pattr++; | |
SecKeychainAttributeList al; | |
al.count = pattr - attrs; | |
al.attr = attrs; | |
*/ | |
char* cred = "Password"; | |
OSStatus ret = SecKeychainItemCreateFromContent( | |
kSecGenericPasswordItemClass, &al, | |
strlen(cred), | |
(void*)cred, | |
NULL, NULL, NULL); | |
if (result != noErr) | |
{ | |
printf("SecKeychainItemCreateFromContent: status=%d", result); | |
return; | |
} | |
// SecKeychainItemModifyContent | |
} | |
void getCredential() | |
{ | |
SecKeychainAttribute attrs[1]; | |
attrs[0].tag = kSecAccountItemAttr; | |
attrs[0].data = (void *)m_idBase; | |
attrs[0].length = strlen(m_idBase); | |
SecKeychainAttributeList al; | |
al.count = 1; | |
al.attr = attrs; | |
SecKeychainSearchRef search = NULL; | |
OSStatus result = SecKeychainSearchCreateFromAttributes(NULL, kSecGenericPasswordItemClass, &al, &search); | |
if (result != noErr || !search) | |
{ | |
printf("SecKeychainSearchCreateFromAttributes: status=%d", result); | |
return; | |
} | |
SecKeychainItemRef item; | |
while(SecKeychainSearchCopyNext(search, &item) == noErr) | |
{ | |
UInt32 length = 0; | |
void* data = NULL; | |
SecKeychainItemCopyContent(item, NULL, NULL, &length, &data); | |
showDataStr(data, length); | |
SecKeychainItemFreeContent(NULL, data); | |
static const void* keys[] = { | |
kSecReturnAttributes | |
}; | |
static const void* values[] = { | |
kCFBooleanTrue | |
}; | |
CFDictionaryRef query = CFDictionaryCreate( | |
NULL, keys, values, 1, | |
&kCFTypeDictionaryKeyCallBacks, | |
&kCFTypeDictionaryValueCallBacks); | |
CFDictionaryRef attrDict = NULL; | |
OSStatus ret = SecItemCopyMatching(query, (CFTypeRef*)&attrDict); | |
if(ret == noErr) | |
{ | |
printf("SecItemCopyMatching success.\n"); | |
CFDataRef custom = (CFDataRef)CFDictionaryGetValue(attrDict, (void*)kSecGenericItemAttr); | |
showDataStr(CFDataGetBytePtr(custom), CFDataGetLength(custom)); | |
CFRelease(attrDict); | |
} | |
else | |
printf("SecItemCopyMatching failed: status=%d\n", ret); | |
CFRelease(query); | |
CFRelease(item); | |
} | |
} | |
~CredentialStore() | |
{ | |
free(m_idBase); | |
} | |
char* m_idBase; | |
void showDataStr(const void* data, size_t length) | |
{ | |
char* buf = new char[length + 1]; | |
memcpy(buf, data, length); | |
buf[length] = 0; | |
printf("Data: %s\n", buf); | |
delete[] buf; | |
} | |
}; | |
int main() | |
{ | |
CredentialStore store( | |
"http", "www.cuminas.jp", 80, "authenticate.aspx:test1"); | |
store.setCredential("foobar", "pa$$word"); | |
store.getCredential(); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment