1 use core_foundation_sys::base::{Boolean, CFTypeID, CFTypeRef, OSStatus};
2 use std::os::raw::{c_char, c_uint, c_void};
3 
4 use crate::base::{SecAccessRef, SecKeychainItemRef, SecKeychainRef};
5 
6 pub const SEC_KEYCHAIN_SETTINGS_VERS1: c_uint = 1;
7 
8 #[repr(C)]
9 pub struct SecKeychainSettings {
10     pub version: c_uint,
11     pub lockOnSleep: Boolean,
12     pub useLockInterval: Boolean,
13     pub lockInterval: c_uint,
14 }
15 
16 /// Like Apple's headers, it assumes Little Endian,
17 /// as there are no supported Big Endian machines any more :(
18 macro_rules! char_lit {
19     ($e:expr) => {
20         ($e[3] as u32) + (($e[2] as u32) << 8) + (($e[1] as u32) << 16) + (($e[0] as u32) << 24)
21     };
22 }
23 
24 macro_rules! char_lit_swapped {
25     ($e:expr) => {
26         ($e[0] as u32) + (($e[1] as u32) << 8) + (($e[2] as u32) << 16) + (($e[3] as u32) << 24)
27     };
28 }
29 
30 #[repr(u32)]
31 #[derive(Copy, Clone, Eq, PartialEq, Debug)]
32 pub enum SecProtocolType {
33     FTP = char_lit!(b"ftp "),
34     FTPAccount = char_lit!(b"ftpa"),
35     HTTP = char_lit!(b"http"),
36     IRC = char_lit!(b"irc "),
37     NNTP = char_lit!(b"nntp"),
38     POP3 = char_lit!(b"pop3"),
39     SMTP = char_lit!(b"smtp"),
40     SOCKS = char_lit!(b"sox "),
41     IMAP = char_lit!(b"imap"),
42     LDAP = char_lit!(b"ldap"),
43     AppleTalk = char_lit!(b"atlk"),
44     AFP = char_lit!(b"afp "),
45     Telnet = char_lit!(b"teln"),
46     SSH = char_lit!(b"ssh "),
47     FTPS = char_lit!(b"ftps"),
48     HTTPS = char_lit!(b"htps"),
49     HTTPProxy = char_lit!(b"htpx"),
50     HTTPSProxy = char_lit!(b"htsx"),
51     FTPProxy = char_lit!(b"ftpx"),
52     CIFS = char_lit!(b"cifs"),
53     SMB = char_lit!(b"smb "),
54     RTSP = char_lit!(b"rtsp"),
55     RTSPProxy = char_lit!(b"rtsx"),
56     DAAP = char_lit!(b"daap"),
57     EPPC = char_lit!(b"eppc"),
58     IPP = char_lit!(b"ipp "),
59     NNTPS = char_lit!(b"ntps"),
60     LDAPS = char_lit!(b"ldps"),
61     TelnetS = char_lit!(b"tels"),
62     IMAPS = char_lit!(b"imps"),
63     IRCS = char_lit!(b"ircs"),
64     POP3S = char_lit!(b"pops"),
65     CVSpserver = char_lit!(b"cvsp"),
66     SVN = char_lit!(b"svn "),
67     Any = 0,
68 }
69 
70 #[repr(u32)]
71 #[derive(Copy, Clone, Eq, PartialEq, Debug)]
72 pub enum SecAuthenticationType {
73     // [sic] Apple has got two related enums each with a different endianness!
74     NTLM = char_lit_swapped!(b"ntlm"),
75     MSN = char_lit_swapped!(b"msna"),
76     DPA = char_lit_swapped!(b"dpaa"),
77     RPA = char_lit_swapped!(b"rpaa"),
78     HTTPBasic = char_lit_swapped!(b"http"),
79     HTTPDigest = char_lit_swapped!(b"httd"),
80     HTMLForm = char_lit_swapped!(b"form"),
81     Default = char_lit_swapped!(b"dflt"),
82     Any = 0,
83 }
84 
85 extern "C" {
SecKeychainGetTypeID() -> CFTypeID86     pub fn SecKeychainGetTypeID() -> CFTypeID;
SecKeychainCopyDefault(keychain: *mut SecKeychainRef) -> OSStatus87     pub fn SecKeychainCopyDefault(keychain: *mut SecKeychainRef) -> OSStatus;
SecKeychainCreate( pathName: *const c_char, passwordLength: c_uint, password: *const c_void, promptUser: Boolean, initialAccess: SecAccessRef, keychain: *mut SecKeychainRef, ) -> OSStatus88     pub fn SecKeychainCreate(
89         pathName: *const c_char,
90         passwordLength: c_uint,
91         password: *const c_void,
92         promptUser: Boolean,
93         initialAccess: SecAccessRef,
94         keychain: *mut SecKeychainRef,
95     ) -> OSStatus;
SecKeychainOpen(pathName: *const c_char, keychain: *mut SecKeychainRef) -> OSStatus96     pub fn SecKeychainOpen(pathName: *const c_char, keychain: *mut SecKeychainRef) -> OSStatus;
SecKeychainUnlock( keychain: SecKeychainRef, passwordLength: c_uint, password: *const c_void, usePassword: Boolean, ) -> OSStatus97     pub fn SecKeychainUnlock(
98         keychain: SecKeychainRef,
99         passwordLength: c_uint,
100         password: *const c_void,
101         usePassword: Boolean,
102     ) -> OSStatus;
103     #[cfg(target_os = "macos")]
SecKeychainFindGenericPassword( keychainOrArray: CFTypeRef, serviceNameLength: u32, serviceName: *const c_char, accountNameLength: u32, accountName: *const c_char, passwordLength: *mut u32, passwordData: *mut *mut c_void, itemRef: *mut SecKeychainItemRef, ) -> OSStatus104     pub fn SecKeychainFindGenericPassword(
105         keychainOrArray: CFTypeRef,
106         serviceNameLength: u32,
107         serviceName: *const c_char,
108         accountNameLength: u32,
109         accountName: *const c_char,
110         passwordLength: *mut u32,
111         passwordData: *mut *mut c_void,
112         itemRef: *mut SecKeychainItemRef,
113     ) -> OSStatus;
114 
115     #[cfg(target_os = "macos")]
SecKeychainFindInternetPassword( keychainOrArray: CFTypeRef, serverNameLength: u32, serverName: *const c_char, securityDomainLength: u32, securityDomain: *const c_char, accountNameLength: u32, accountName: *const c_char, pathLength: u32, path: *const c_char, port: u16, protocol: SecProtocolType, authenticationType: SecAuthenticationType, passwordLength: *mut u32, passwordData: *mut *mut c_void, itemRef: *mut SecKeychainItemRef, ) -> OSStatus116     pub fn SecKeychainFindInternetPassword(
117         keychainOrArray: CFTypeRef,
118         serverNameLength: u32,
119         serverName: *const c_char,
120         securityDomainLength: u32,
121         securityDomain: *const c_char,
122         accountNameLength: u32,
123         accountName: *const c_char,
124         pathLength: u32,
125         path: *const c_char,
126         port: u16,
127         protocol: SecProtocolType,
128         authenticationType: SecAuthenticationType,
129         passwordLength: *mut u32,
130         passwordData: *mut *mut c_void,
131         itemRef: *mut SecKeychainItemRef,
132     ) -> OSStatus;
133 
134     #[cfg(target_os = "macos")]
SecKeychainAddGenericPassword( keychain: SecKeychainRef, serviceNameLength: u32, serviceName: *const c_char, accountNameLength: u32, accountName: *const c_char, passwordLength: u32, passwordData: *const c_void, itemRef: *mut SecKeychainItemRef, ) -> OSStatus135     pub fn SecKeychainAddGenericPassword(
136         keychain: SecKeychainRef,
137         serviceNameLength: u32,
138         serviceName: *const c_char,
139         accountNameLength: u32,
140         accountName: *const c_char,
141         passwordLength: u32,
142         passwordData: *const c_void,
143         itemRef: *mut SecKeychainItemRef,
144     ) -> OSStatus;
145 
146     #[cfg(target_os = "macos")]
SecKeychainAddInternetPassword( keychain: SecKeychainRef, serverNameLength: u32, serverName: *const c_char, securityDomainLength: u32, securityDomain: *const c_char, accountNameLength: u32, accountName: *const c_char, pathLength: u32, path: *const c_char, port: u16, protocol: SecProtocolType, authenticationType: SecAuthenticationType, passwordLength: u32, passwordData: *const c_void, itemRef: *mut SecKeychainItemRef, ) -> OSStatus147     pub fn SecKeychainAddInternetPassword(
148         keychain: SecKeychainRef,
149         serverNameLength: u32,
150         serverName: *const c_char,
151         securityDomainLength: u32,
152         securityDomain: *const c_char,
153         accountNameLength: u32,
154         accountName: *const c_char,
155         pathLength: u32,
156         path: *const c_char,
157         port: u16,
158         protocol: SecProtocolType,
159         authenticationType: SecAuthenticationType,
160         passwordLength: u32,
161         passwordData: *const c_void,
162         itemRef: *mut SecKeychainItemRef,
163     ) -> OSStatus;
164 
SecKeychainSetSettings( keychain: SecKeychainRef, newSettings: *const SecKeychainSettings, ) -> OSStatus165     pub fn SecKeychainSetSettings(
166         keychain: SecKeychainRef,
167         newSettings: *const SecKeychainSettings,
168     ) -> OSStatus;
169 }
170