static unsafe OSStatus AddInternetPassword (byte[] label, byte[] desc, SecAuthenticationType auth, byte[] user, byte[] passwd, SecProtocolType protocol, byte[] host, int port, byte[] path) { // Note: the following code does more-or-less the same as: //SecKeychainAddInternetPassword (CurrentKeychain, (uint) host.Length, host, 0, null, // (uint) user.Length, user, (uint) path.Length, path, (ushort) port, // protocol, auth, (uint) passwd.Length, passwd, ref item); fixed (byte* labelPtr = label, descPtr = desc, userPtr = user, hostPtr = host, pathPtr = path) { SecKeychainAttribute* attrs = stackalloc SecKeychainAttribute [8]; int* protoPtr = (int*) &protocol; int* authPtr = (int*) &auth; int* portPtr = &port; int n = 0; attrs[n++] = new SecKeychainAttribute (SecItemAttr.Label, (uint) label.Length, (IntPtr) labelPtr); if (desc != null) attrs[n++] = new SecKeychainAttribute (SecItemAttr.Description, (uint) desc.Length, (IntPtr) descPtr); attrs[n++] = new SecKeychainAttribute (SecItemAttr.Account, (uint) user.Length, (IntPtr) userPtr); attrs[n++] = new SecKeychainAttribute (SecItemAttr.Protocol, (uint) 4, (IntPtr) protoPtr); attrs[n++] = new SecKeychainAttribute (SecItemAttr.AuthType, (uint) 4, (IntPtr) authPtr); attrs[n++] = new SecKeychainAttribute (SecItemAttr.Server, (uint) host.Length, (IntPtr) hostPtr); attrs[n++] = new SecKeychainAttribute (SecItemAttr.Port, (uint) 4, (IntPtr) portPtr); attrs[n++] = new SecKeychainAttribute (SecItemAttr.Path, (uint) path.Length, (IntPtr) pathPtr); SecKeychainAttributeList attrList = new SecKeychainAttributeList (n, (IntPtr) attrs); var item = IntPtr.Zero; var result = SecKeychainItemCreateFromContent (SecItemClass.InternetPassword, &attrList, (uint) passwd.Length, passwd, CurrentKeychain, IntPtr.Zero, ref item); CFRelease (item); return result; } }
static extern OSStatus SecKeychainFindInternetPassword (IntPtr keychain, uint serverNameLength, byte[] serverName, uint securityDomainLength, byte[] securityDomain, uint accountNameLength, byte[] accountName, uint pathLength, byte[] path, ushort port, SecProtocolType protocol, SecAuthenticationType authType, out uint passwordLength, out IntPtr passwordData, ref IntPtr itemRef);