+/*
+ * get PSK from keyChain.
+ */
+vchar_t *
+getpskfromkeychain(const char *name)
+{
+ SecKeychainRef keychain = NULL;
+ vchar_t *key = NULL;
+ void *cur_password = NULL;
+ UInt32 cur_password_len = 0;
+ OSStatus status;
+ char serviceName[] = "com.apple.net.racoon";
+
+ status = SecKeychainSetPreferenceDomain(kSecPreferencesDomainSystem);
+ if (status != noErr) {
+ plog(LLV_ERROR, LOCATION, NULL,
+ "failed to set system keychain domain.\n");
+ goto end;
+ }
+
+ status = SecKeychainCopyDomainDefault(kSecPreferencesDomainSystem,
+ &keychain);
+ if (status != noErr) {
+ plog(LLV_ERROR, LOCATION, NULL,
+ "failed to get system keychain domain.\n");
+ goto end;
+ }
+
+ status = SecKeychainFindGenericPassword(keychain,
+ strlen(serviceName),
+ serviceName,
+ strlen(name),
+ name,
+ &cur_password_len,
+ &cur_password,
+ NULL);
+
+ switch (status) {
+
+ case noErr :
+ break;
+
+ case errSecItemNotFound :
+ break;
+
+ default :
+ plog(LLV_ERROR, LOCATION, NULL,
+ "failed to get preshared key from system keychain (error %d).\n", status);
+ }
+
+end:
+
+ if (cur_password) {
+ key = vmalloc(cur_password_len + 1);
+ if (key == NULL) {
+ plog(LLV_ERROR, LOCATION, NULL,
+ "failed to allocate key buffer.\n");
+ }
+ else {
+ memcpy(key->v, cur_password, key->l);
+ key->v[cur_password_len] = 0;
+ }
+ free(cur_password);
+ }
+
+ if (keychain)
+ CFRelease(keychain);
+
+ return key;
+}
+