|
| 1 | +use std::ffi::{CString, c_void}; |
| 2 | +use failure::Error; |
| 3 | +use std::ptr; |
| 4 | + |
| 5 | +#[link(name = "Security", kind = "framework")] |
| 6 | +extern "C" { |
| 7 | + fn SecItemAdd(attributes: *const c_void, result: *mut *const c_void) -> i32; |
| 8 | + fn SecItemCopyMatching(query: *const c_void, result: *mut *const c_void) -> i32; |
| 9 | + fn SecItemDelete(query: *const c_void) -> i32; |
| 10 | + |
| 11 | + static kSecClass: *const c_void; |
| 12 | + static kSecClassGenericPassword: *const c_void; |
| 13 | + static kSecAttrService: *const c_void; |
| 14 | + static kSecAttrAccount: *const c_void; |
| 15 | + static kSecValueData: *const c_void; |
| 16 | + static kSecReturnData: *const c_void; |
| 17 | + static kCFBooleanTrue: *const c_void; |
| 18 | + static kSecAttrAccessGroup: *const c_void; |
| 19 | +} |
| 20 | + |
| 21 | +const ERR_SUCCESS: i32 = 0; |
| 22 | + |
| 23 | +pub fn add_keychain_item(service: &str, account: &str, secret: &str) -> Result<(), Error> { |
| 24 | + let service = CString::new(service).unwrap(); |
| 25 | + let account = CString::new(account).unwrap(); |
| 26 | + let secret_bytes = secret.as_bytes(); |
| 27 | + |
| 28 | + let cf_service = unsafe { |
| 29 | + CFStringCreateWithCString(ptr::null(), service.as_ptr(), 0x08000100) // UTF-8 encoding |
| 30 | + }; |
| 31 | + let cf_account = unsafe { |
| 32 | + CFStringCreateWithCString(ptr::null(), account.as_ptr(), 0x08000100) // UTF-8 encoding |
| 33 | + }; |
| 34 | + let cf_data = unsafe { CFDataCreate(ptr::null(), secret_bytes.as_ptr(), secret_bytes.len()) }; |
| 35 | + let cf_access_group = unsafe { |
| 36 | + CFStringCreateWithCString( |
| 37 | + ptr::null(), |
| 38 | + "7WV56FL599.keychain_tool".as_ptr() as *const i8, // Fixed pointer type |
| 39 | + 0x08000100, |
| 40 | + ) |
| 41 | + }; |
| 42 | + |
| 43 | + assert!(!cf_service.is_null(), "Failed to create CFString for service"); |
| 44 | + assert!(!cf_account.is_null(), "Failed to create CFString for account"); |
| 45 | + assert!(!cf_data.is_null(), "Failed to create CFData for secret"); |
| 46 | + assert!(!cf_access_group.is_null(), "Failed to create CFString for access group"); |
| 47 | + |
| 48 | + let attributes = vec![ |
| 49 | + (unsafe { kSecClass }, unsafe { kSecClassGenericPassword }), |
| 50 | + (unsafe { kSecAttrService }, cf_service), |
| 51 | + (unsafe { kSecAttrAccount }, cf_account), |
| 52 | + (unsafe { kSecValueData }, cf_data), |
| 53 | + (unsafe { kSecAttrAccessGroup }, cf_access_group), // Added access group |
| 54 | + ]; |
| 55 | + |
| 56 | + let attributes_ptr = attributes_to_dict(&attributes); |
| 57 | + assert!(!attributes_ptr.is_null(), "CFDictionary creation failed!"); |
| 58 | + |
| 59 | + unsafe { |
| 60 | + let mut result: *mut c_void = ptr::null_mut(); |
| 61 | + let status = SecItemAdd(attributes_ptr, &mut result as *mut *mut c_void as *mut *const c_void); |
| 62 | + |
| 63 | + CFRelease(attributes_ptr); // Release dictionary |
| 64 | + CFRelease(cf_service); // Release service string |
| 65 | + CFRelease(cf_account); // Release account string |
| 66 | + CFRelease(cf_data); // Release data |
| 67 | + CFRelease(cf_access_group); // Release access group string |
| 68 | + |
| 69 | + if status == ERR_SUCCESS { |
| 70 | + Ok(()) |
| 71 | + } else { |
| 72 | + Err(failure::err_msg(format!("SecItemAdd failed with status: {}", status))) |
| 73 | + } |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +pub fn get_keychain_item(service: &str, account: &str) -> Result<String, String> { |
| 78 | + let service = CString::new(service).unwrap(); |
| 79 | + let account = CString::new(account).unwrap(); |
| 80 | + |
| 81 | + let cf_service = unsafe { |
| 82 | + CFStringCreateWithCString(ptr::null(), service.as_ptr() as *const i8, 0x08000100) // UTF-8 encoding |
| 83 | + }; |
| 84 | + let cf_account = unsafe { |
| 85 | + CFStringCreateWithCString(ptr::null(), account.as_ptr() as *const i8, 0x08000100) // UTF-8 encoding |
| 86 | + }; |
| 87 | + |
| 88 | + assert!(!cf_service.is_null(), "Failed to create CFString for service"); |
| 89 | + assert!(!cf_account.is_null(), "Failed to create CFString for account"); |
| 90 | + |
| 91 | + let query = vec![ |
| 92 | + (unsafe { kSecClass }, unsafe { kSecClassGenericPassword }), |
| 93 | + (unsafe { kSecAttrService }, cf_service), |
| 94 | + (unsafe { kSecAttrAccount }, cf_account), |
| 95 | + (unsafe { kSecReturnData }, unsafe { kCFBooleanTrue }), |
| 96 | + ]; |
| 97 | + |
| 98 | + let query_ptr = attributes_to_dict(&query); |
| 99 | + assert!(!query_ptr.is_null(), "CFDictionary creation failed for query!"); |
| 100 | + |
| 101 | + unsafe { |
| 102 | + let mut result: *mut c_void = ptr::null_mut(); |
| 103 | + let status = SecItemCopyMatching(query_ptr, &mut result as *mut *mut c_void as *mut *const c_void); |
| 104 | + |
| 105 | + CFRelease(query_ptr); // Release query dictionary |
| 106 | + CFRelease(cf_service); // Release service string |
| 107 | + CFRelease(cf_account); // Release account string |
| 108 | + |
| 109 | + if status == ERR_SUCCESS { |
| 110 | + assert!(!result.is_null(), "SecItemCopyMatching returned a null result"); |
| 111 | + |
| 112 | + // Convert the result to a Rust String |
| 113 | + let data_ptr = CFDataGetBytePtr(result); |
| 114 | + let data_len = CFDataGetLength(result); |
| 115 | + let bytes = std::slice::from_raw_parts(data_ptr, data_len); |
| 116 | + let secret = String::from_utf8_lossy(bytes).to_string(); |
| 117 | + |
| 118 | + CFRelease(result); // Release result data |
| 119 | + |
| 120 | + Ok(secret) |
| 121 | + } else { |
| 122 | + Err(format!("SecItemCopyMatching failed with status: {}", status)) |
| 123 | + } |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +// Helpers for Keychain Constants and Conversions |
| 128 | + |
| 129 | +#[link(name = "CoreFoundation", kind = "framework")] |
| 130 | +extern "C" { |
| 131 | + fn CFRelease(cf: *const c_void); |
| 132 | + fn CFDataGetLength(data: *const c_void) -> usize; |
| 133 | + fn CFDataGetBytePtr(data: *const c_void) -> *const u8; |
| 134 | +} |
| 135 | + |
| 136 | +extern "C" { |
| 137 | + fn CFDictionaryCreate( |
| 138 | + allocator: *const c_void, |
| 139 | + keys: *const *const c_void, |
| 140 | + values: *const *const c_void, |
| 141 | + count: usize, |
| 142 | + key_callbacks: *const c_void, |
| 143 | + value_callbacks: *const c_void, |
| 144 | + ) -> *const c_void; |
| 145 | + |
| 146 | + fn CFStringCreateWithCString( |
| 147 | + allocator: *const c_void, |
| 148 | + cstr: *const i8, |
| 149 | + encoding: u32, |
| 150 | + ) -> *const c_void; |
| 151 | + |
| 152 | + fn CFDataCreate( |
| 153 | + allocator: *const c_void, |
| 154 | + bytes: *const u8, |
| 155 | + length: usize, |
| 156 | + ) -> *const c_void; |
| 157 | +} |
| 158 | + |
| 159 | +fn attributes_to_dict(attrs: &[(/* key: */ *const c_void, /* value: */ *const c_void)]) -> *const c_void { |
| 160 | + let keys: Vec<*const c_void> = attrs.iter().map(|(key, _)| *key).collect(); |
| 161 | + let values: Vec<*const c_void> = attrs.iter().map(|(_, value)| *value).collect(); |
| 162 | + |
| 163 | + unsafe { |
| 164 | + CFDictionaryCreate( |
| 165 | + ptr::null(), |
| 166 | + keys.as_ptr(), |
| 167 | + values.as_ptr(), |
| 168 | + attrs.len(), |
| 169 | + ptr::null(), |
| 170 | + ptr::null(), |
| 171 | + ) |
| 172 | + } |
| 173 | +} |
| 174 | + |
| 175 | +fn CFDataToString(data: *const c_void) -> String { |
| 176 | + unsafe { |
| 177 | + let length = CFDataGetLength(data); |
| 178 | + let bytes = CFDataGetBytePtr(data); |
| 179 | + String::from_utf8(Vec::from_raw_parts(bytes as *mut u8, length, length)).unwrap() |
| 180 | + } |
| 181 | +} |
0 commit comments