#include "keychain_find.h"
#include "keychain_utilities.h"
-#include "readline.h"
+#include "readline_cssm.h"
#include "security_tool.h"
#include <stdio.h>
#include <Security/SecCertificate.h>
#include <CoreFoundation/CFString.h>
#include <ctype.h>
+#include <utilities/SecCFRelease.h>
// SecDigestGetData, SecKeychainSearchCreateForCertificateByEmail, SecCertificateFindByEmail
} else {
char *password = (char *) passwordData;
int doHex = 0;
- for(int i=0; i<passwordLength; i++) if(!isprint(password[i])) doHex = 1;
+ for(uint32_t i=0; i<passwordLength; i++) if(!isprint(password[i])) doHex = 1;
if(doHex) {
- for(int i=0; i<passwordLength; i++) printf("%02x", password[i]);
+ for(uint32_t i=0; i<passwordLength; i++) printf("%02x", password[i]);
} else {
- for(int i=0; i<passwordLength; i++) putchar(password[i]);
+ for(uint32_t i=0; i<passwordLength; i++) putchar(password[i]);
}
putchar('\n');
}
return result;
}
+#define SetKeyToString(dict, key, arg) \
+{ \
+ CFStringRef str = CFStringCreateWithCStringNoCopy(NULL, arg, kCFStringEncodingUTF8, kCFAllocatorNull); \
+ CFDictionarySetValue(dict, key, str); \
+ CFReleaseNull(str); \
+}
+
int
keychain_find_key(int argc, char * const *argv) {
/*
switch (ch)
{
case 'a':
- CFDictionarySetValue(query, kSecAttrApplicationLabel, CFStringCreateWithCStringNoCopy(NULL, optarg, kCFStringEncodingUTF8, kCFAllocatorNull));
+ SetKeyToString(query, kSecAttrApplicationLabel, optarg);
break;
case 'c':
- CFDictionarySetValue(query, kSecAttrCreator, CFStringCreateWithCStringNoCopy(NULL, optarg, kCFStringEncodingUTF8, kCFAllocatorNull));
+ SetKeyToString(query, kSecAttrCreator, optarg);
break;
case 'd':
CFDictionarySetValue(query, kSecAttrCanDecrypt, kCFBooleanTrue);
break;
case 'D':
- CFDictionarySetValue(query, kSecAttrDescription, CFStringCreateWithCStringNoCopy(NULL, optarg, kCFStringEncodingUTF8, kCFAllocatorNull));
+ SetKeyToString(query, kSecAttrDescription, optarg);
break;
case 'e':
CFDictionarySetValue(query, kSecAttrCanEncrypt, kCFBooleanTrue);
break;
case 'j':
- CFDictionarySetValue(query, kSecAttrComment, CFStringCreateWithCStringNoCopy(NULL, optarg, kCFStringEncodingUTF8, kCFAllocatorNull));
+ SetKeyToString(query, kSecAttrComment, optarg);
break;
case 'l':
- CFDictionarySetValue(query, kSecAttrLabel, CFStringCreateWithCStringNoCopy(NULL, optarg, kCFStringEncodingUTF8, kCFAllocatorNull));
+ SetKeyToString(query, kSecAttrLabel, optarg);
break;
case 'r':
CFDictionarySetValue(query, kSecAttrCanDerive, kCFBooleanTrue);
CFDictionarySetValue(query, kSecMatchSearchList, searchList);
CFRelease(searchList);
}
+ CFReleaseNull(keychainOrArray);
OSStatus status = SecItemCopyMatching(query, &results);
if(status) {
switch (ch)
{
case 'a':
- CFDictionarySetValue(query, kSecAttrAccount, CFStringCreateWithCStringNoCopy(NULL, optarg, kCFStringEncodingUTF8, kCFAllocatorNull));
+ SetKeyToString(query, kSecAttrAccount, optarg);
break;
case 'c':
- CFDictionarySetValue(query, kSecAttrCreator, CFStringCreateWithCStringNoCopy(NULL, optarg, kCFStringEncodingUTF8, kCFAllocatorNull));
+ SetKeyToString(query, kSecAttrCreator, optarg);
break;
case 'C':
- CFDictionarySetValue(query, kSecAttrType, CFStringCreateWithCStringNoCopy(NULL, optarg, kCFStringEncodingUTF8, kCFAllocatorNull));
+ SetKeyToString(query, kSecAttrType, optarg);
break;
case 'd':
- CFDictionarySetValue(query, kSecAttrSecurityDomain, CFStringCreateWithCStringNoCopy(NULL, optarg, kCFStringEncodingUTF8, kCFAllocatorNull));
+ SetKeyToString(query, kSecAttrSecurityDomain, optarg);
break;
case 'D':
- CFDictionarySetValue(query, kSecAttrDescription, CFStringCreateWithCStringNoCopy(NULL, optarg, kCFStringEncodingUTF8, kCFAllocatorNull));
+ SetKeyToString(query, kSecAttrDescription, optarg);
break;
case 'j':
- CFDictionarySetValue(query, kSecAttrComment, CFStringCreateWithCStringNoCopy(NULL, optarg, kCFStringEncodingUTF8, kCFAllocatorNull));
+ SetKeyToString(query, kSecAttrComment, optarg);
break;
case 'l':
- CFDictionarySetValue(query, kSecAttrLabel, CFStringCreateWithCStringNoCopy(NULL, optarg, kCFStringEncodingUTF8, kCFAllocatorNull));
+ SetKeyToString(query, kSecAttrLabel, optarg);
break;
case 'p':
- CFDictionarySetValue(query, kSecAttrPath, CFStringCreateWithCStringNoCopy(NULL, optarg, kCFStringEncodingUTF8, kCFAllocatorNull));
+ SetKeyToString(query, kSecAttrPath, optarg);
break;
case 'P':
- CFDictionarySetValue(query, kSecAttrPort, CFStringCreateWithCStringNoCopy(NULL, optarg, kCFStringEncodingUTF8, kCFAllocatorNull));
+ SetKeyToString(query, kSecAttrPort, optarg);
break;
case 'r':
- CFDictionarySetValue(query, kSecAttrProtocol, CFStringCreateWithCStringNoCopy(NULL, optarg, kCFStringEncodingUTF8, kCFAllocatorNull));
+ SetKeyToString(query, kSecAttrProtocol, optarg);
break;
case 's':
- CFDictionarySetValue(query, kSecAttrService, CFStringCreateWithCStringNoCopy(NULL, optarg, kCFStringEncodingUTF8, kCFAllocatorNull));
+ SetKeyToString(query, kSecAttrService, optarg);
break;
case 't':
- CFDictionarySetValue(query, kSecAttrAuthenticationType, CFStringCreateWithCStringNoCopy(NULL, optarg, kCFStringEncodingUTF8, kCFAllocatorNull));
+ SetKeyToString(query, kSecAttrAuthenticationType, optarg);
break;
case 'S':
+ CFReleaseNull(partitionidsinput);
partitionidsinput = CFStringCreateWithCStringNoCopy(NULL, optarg, kCFStringEncodingUTF8, kCFAllocatorNull);
break;
case 'k':
+ CFReleaseNull(password);
password = CFStringCreateWithCStringNoCopy(NULL, optarg, kCFStringEncodingUTF8, kCFAllocatorNull);
break;
case '?':
switch (ch)
{
case 'a':
- CFDictionarySetValue(query, kSecAttrAccount, CFStringCreateWithCStringNoCopy(NULL, optarg, kCFStringEncodingUTF8, kCFAllocatorNull));
+ SetKeyToString(query, kSecAttrAccount, optarg);
break;
case 'c':
- CFDictionarySetValue(query, kSecAttrCreator, CFStringCreateWithCStringNoCopy(NULL, optarg, kCFStringEncodingUTF8, kCFAllocatorNull));
+ SetKeyToString(query, kSecAttrCreator, optarg);
break;
case 'C':
- CFDictionarySetValue(query, kSecAttrType, CFStringCreateWithCStringNoCopy(NULL, optarg, kCFStringEncodingUTF8, kCFAllocatorNull));
+ SetKeyToString(query, kSecAttrType, optarg);
break;
case 'D':
- CFDictionarySetValue(query, kSecAttrDescription, CFStringCreateWithCStringNoCopy(NULL, optarg, kCFStringEncodingUTF8, kCFAllocatorNull));
+ SetKeyToString(query, kSecAttrDescription, optarg);
break;
case 'G':
- CFDictionarySetValue(query, kSecAttrGeneric, CFStringCreateWithCStringNoCopy(NULL, optarg, kCFStringEncodingUTF8, kCFAllocatorNull));
+ SetKeyToString(query, kSecAttrGeneric, optarg);
break;
case 'j':
- CFDictionarySetValue(query, kSecAttrComment, CFStringCreateWithCStringNoCopy(NULL, optarg, kCFStringEncodingUTF8, kCFAllocatorNull));
+ SetKeyToString(query, kSecAttrComment, optarg);
break;
case 'l':
- CFDictionarySetValue(query, kSecAttrLabel, CFStringCreateWithCStringNoCopy(NULL, optarg, kCFStringEncodingUTF8, kCFAllocatorNull));
+ SetKeyToString(query, kSecAttrLabel, optarg);
break;
case 's':
- CFDictionarySetValue(query, kSecAttrService, CFStringCreateWithCStringNoCopy(NULL, optarg, kCFStringEncodingUTF8, kCFAllocatorNull));
+ SetKeyToString(query, kSecAttrService, optarg);
break;
case 'S':
+ CFReleaseNull(partitionidsinput);
partitionidsinput = CFStringCreateWithCStringNoCopy(NULL, optarg, kCFStringEncodingUTF8, kCFAllocatorNull);
break;
case 'k':
+ CFReleaseNull(password);
password = CFStringCreateWithCStringNoCopy(NULL, optarg, kCFStringEncodingUTF8, kCFAllocatorNull);
break;
case '?':
switch (ch)
{
case 'a':
- CFDictionarySetValue(query, kSecAttrApplicationLabel, CFStringCreateWithCStringNoCopy(NULL, optarg, kCFStringEncodingUTF8, kCFAllocatorNull));
+ SetKeyToString(query, kSecAttrApplicationLabel, optarg);
break;
case 'c':
- CFDictionarySetValue(query, kSecAttrCreator, CFStringCreateWithCStringNoCopy(NULL, optarg, kCFStringEncodingUTF8, kCFAllocatorNull));
+ SetKeyToString(query, kSecAttrCreator, optarg);
break;
case 'd':
+ SetKeyToString(query, kSecAttrCanDecrypt, optarg);
CFDictionarySetValue(query, kSecAttrCanDecrypt, kCFBooleanTrue);
break;
case 'D':
- CFDictionarySetValue(query, kSecAttrDescription, CFStringCreateWithCStringNoCopy(NULL, optarg, kCFStringEncodingUTF8, kCFAllocatorNull));
+ SetKeyToString(query, kSecAttrDescription, optarg);
break;
case 'e':
CFDictionarySetValue(query, kSecAttrCanEncrypt, kCFBooleanTrue);
break;
case 'j':
- CFDictionarySetValue(query, kSecAttrComment, CFStringCreateWithCStringNoCopy(NULL, optarg, kCFStringEncodingUTF8, kCFAllocatorNull));
+ SetKeyToString(query, kSecAttrComment, optarg);
break;
case 'l':
- CFDictionarySetValue(query, kSecAttrLabel, CFStringCreateWithCStringNoCopy(NULL, optarg, kCFStringEncodingUTF8, kCFAllocatorNull));
+ SetKeyToString(query, kSecAttrLabel, optarg);
break;
case 'r':
CFDictionarySetValue(query, kSecAttrCanDerive, kCFBooleanTrue);
CFDictionarySetValue(query, kSecAttrCanWrap, kCFBooleanTrue);
break;
case 'S':
+ CFReleaseNull(partitionidsinput);
partitionidsinput = CFStringCreateWithCStringNoCopy(NULL, optarg, kCFStringEncodingUTF8, kCFAllocatorNull);
break;
case 'k':
+ CFReleaseNull(password);
password = CFStringCreateWithCStringNoCopy(NULL, optarg, kCFStringEncodingUTF8, kCFAllocatorNull);
break;
case '?':
result = keychain_parse_args_and_set_partition_list(argc, argv, query, partitionidsinput, password);
cleanup:
+ CFReleaseNull(partitionidsinput);
+ CFReleaseNull(password);
safe_CFRelease(&query);
return result;
}
int result = 0;
const char *keychainName = NULL;
SecKeychainRef kc = NULL;
+ CFStringRef localPassword = NULL;
// if we were given a keychain, use it
if (argc == 1)
result = -1;
goto cleanup;
}
- password = CFStringCreateWithCString(NULL, cpassword, kCFStringEncodingUTF8);
+ localPassword = CFStringCreateWithCString(NULL, cpassword, kCFStringEncodingUTF8);
+ password = localPassword;
free(cpassword);
}
result = keychain_set_partition_list(kc, query, password, partitionidsinput);
cleanup:
+ CFReleaseNull(localPassword);
return result;
}
GetCStringFromCFString(password, &passwordBuf, &passwordLen);
OSStatus status;
+ CFTypeRef results = NULL;
// Unlock the keychain with the given password, since we'll be fetching ACLs
status = SecKeychainUnlock(kc, (UInt32) passwordLen, passwordBuf, true);
goto cleanup;
}
- CFTypeRef results = NULL;
status = SecItemCopyMatching(query, &results);
if(status) {
sec_perror("SecItemCopyMatching", status);