|
| 1 | +/* |
| 2 | + * Minimal replacement for `security find-generic-password -D desc -a account |
| 3 | + * -s service -w`, built natively for arm64/x86_64. |
| 4 | + * |
| 5 | + * Historically this helper was produced by lipo-thinning Apple's own |
| 6 | + * /usr/bin/security down to its x86_64 slice and ad-hoc re-signing it with a |
| 7 | + * custom identifier, purely so the System keychain ACL could trust this one |
| 8 | + * specific binary (see files/update.sh) without granting blanket access to |
| 9 | + * every process that happens to be /usr/bin/security. Apple's Mach-O for |
| 10 | + * /usr/bin/security only ships x86_64 and arm64e slices, and non-Apple |
| 11 | + * ad-hoc signatures of the arm64e slice fail the kernel's pointer |
| 12 | + * authentication ABI check, so the x86_64 slice needed Rosetta. Compiling |
| 13 | + * this ourselves for plain arm64 sidesteps that restriction entirely while |
| 14 | + * keeping the same custom-identity trust model. |
| 15 | + */ |
| 16 | + |
| 17 | +#include <CoreFoundation/CoreFoundation.h> |
| 18 | +#include <Security/Security.h> |
| 19 | +#include <stdio.h> |
| 20 | +#include <string.h> |
| 21 | + |
| 22 | +static void usage(const char *prog) { |
| 23 | + fprintf(stderr, "usage: %s find-generic-password -s service -a account [-D description] -w\n", prog); |
| 24 | +} |
| 25 | + |
| 26 | +int main(int argc, char *argv[]) { |
| 27 | + if (argc < 2 || strcmp(argv[1], "find-generic-password") != 0) { |
| 28 | + usage(argv[0]); |
| 29 | + return 2; |
| 30 | + } |
| 31 | + |
| 32 | + const char *service = NULL, *account = NULL, *description = NULL; |
| 33 | + int want_password = 0; |
| 34 | + |
| 35 | + for (int i = 2; i < argc; i++) { |
| 36 | + if (strcmp(argv[i], "-s") == 0 && i + 1 < argc) { |
| 37 | + service = argv[++i]; |
| 38 | + } else if (strcmp(argv[i], "-a") == 0 && i + 1 < argc) { |
| 39 | + account = argv[++i]; |
| 40 | + } else if (strcmp(argv[i], "-D") == 0 && i + 1 < argc) { |
| 41 | + description = argv[++i]; |
| 42 | + } else if (strcmp(argv[i], "-w") == 0) { |
| 43 | + want_password = 1; |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + if (!service || !account || !want_password) { |
| 48 | + usage(argv[0]); |
| 49 | + return 2; |
| 50 | + } |
| 51 | + |
| 52 | + SecKeychainRef systemKeychain = NULL; |
| 53 | + if (SecKeychainOpen("/Library/Keychains/System.keychain", &systemKeychain) != errSecSuccess || !systemKeychain) { |
| 54 | + return 1; |
| 55 | + } |
| 56 | + |
| 57 | + CFStringRef serviceStr = CFStringCreateWithCString(kCFAllocatorDefault, service, kCFStringEncodingUTF8); |
| 58 | + CFStringRef accountStr = CFStringCreateWithCString(kCFAllocatorDefault, account, kCFStringEncodingUTF8); |
| 59 | + CFStringRef descriptionStr = description |
| 60 | + ? CFStringCreateWithCString(kCFAllocatorDefault, description, kCFStringEncodingUTF8) |
| 61 | + : NULL; |
| 62 | + CFArrayRef searchList = CFArrayCreate(kCFAllocatorDefault, (const void **)&systemKeychain, 1, &kCFTypeArrayCallBacks); |
| 63 | + |
| 64 | + CFMutableDictionaryRef query = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, |
| 65 | + &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); |
| 66 | + CFDictionarySetValue(query, kSecClass, kSecClassGenericPassword); |
| 67 | + CFDictionarySetValue(query, kSecAttrService, serviceStr); |
| 68 | + CFDictionarySetValue(query, kSecAttrAccount, accountStr); |
| 69 | + if (descriptionStr) { |
| 70 | + CFDictionarySetValue(query, kSecAttrDescription, descriptionStr); |
| 71 | + } |
| 72 | + CFDictionarySetValue(query, kSecMatchSearchList, searchList); |
| 73 | + CFDictionarySetValue(query, kSecMatchLimit, kSecMatchLimitOne); |
| 74 | + CFDictionarySetValue(query, kSecReturnData, kCFBooleanTrue); |
| 75 | + |
| 76 | + CFTypeRef result = NULL; |
| 77 | + OSStatus status = SecItemCopyMatching(query, &result); |
| 78 | + |
| 79 | + CFRelease(query); |
| 80 | + CFRelease(searchList); |
| 81 | + CFRelease(serviceStr); |
| 82 | + CFRelease(accountStr); |
| 83 | + if (descriptionStr) { |
| 84 | + CFRelease(descriptionStr); |
| 85 | + } |
| 86 | + CFRelease(systemKeychain); |
| 87 | + |
| 88 | + if (status != errSecSuccess || !result) { |
| 89 | + return 1; |
| 90 | + } |
| 91 | + |
| 92 | + CFDataRef data = (CFDataRef)result; |
| 93 | + fwrite(CFDataGetBytePtr(data), 1, (size_t)CFDataGetLength(data), stdout); |
| 94 | + CFRelease(result); |
| 95 | + return 0; |
| 96 | +} |
0 commit comments