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);
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); } }
public static unsafe Tuple <string, string> FindInternetUserNameAndPassword(Uri uri, SecProtocolType protocol) { var pathStr = string.Join(string.Empty, uri.Segments); byte[] path = pathStr.Length > 0 ? Encoding.UTF8.GetBytes(pathStr.Substring(1)) : new byte[0]; // don't include the leading '/' byte[] host = Encoding.UTF8.GetBytes(uri.Host); var auth = GetSecAuthenticationType(uri.Query); IntPtr passwordData; IntPtr item = IntPtr.Zero; uint passwordLength = 0; var result = SecKeychainFindInternetPassword( CurrentKeychain, (uint)host.Length, host, 0, null, 0, null, (uint)path.Length, path, (ushort)uri.Port, protocol, auth, out passwordLength, out passwordData, ref item); if (result != OSStatus.Ok) { return(null); } var username = GetUsernameFromKeychainItemRef(item); return(Tuple.Create(username, Marshal.PtrToStringAuto(passwordData, (int)passwordLength))); }
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);
public static unsafe Tuple <string, string> FindInternetUserNameAndPassword(Uri uri, SecProtocolType protocol) { byte[] path = uri.ToPathBytes(); byte[] host = Encoding.UTF8.GetBytes(uri.Host); var auth = GetSecAuthenticationType(uri.Query); IntPtr passwordData; IntPtr item = IntPtr.Zero; uint passwordLength = 0; var result = SecKeychainFindInternetPassword( CurrentKeychain, (uint)host.Length, host, 0, null, 0, null, (uint)path.Length, path, (ushort)uri.Port, protocol, auth, out passwordLength, out passwordData, ref item); try { if (result != SecStatusCode.Success) { return(null); } var username = GetUsernameFromKeychainItemRef(item); return(Tuple.Create(username, Marshal.PtrToStringAuto(passwordData, (int)passwordLength))); } finally { SecKeychainItemFreeContent(IntPtr.Zero, passwordData); CFRelease(item); } }
public static unsafe Tuple<string, string> FindInternetUserNameAndPassword (Uri uri, SecProtocolType protocol) { var pathStr = string.Join (string.Empty, uri.Segments); byte[] path = pathStr.Length > 0 ? Encoding.UTF8.GetBytes (pathStr.Substring (1)) : new byte[0]; // don't include the leading '/' byte[] host = Encoding.UTF8.GetBytes (uri.Host); var auth = GetSecAuthenticationType (uri.Query); IntPtr passwordData; IntPtr item = IntPtr.Zero; uint passwordLength = 0; var result = SecKeychainFindInternetPassword ( CurrentKeychain, (uint) host.Length, host, 0, null, 0, null, (uint) path.Length, path, (ushort) uri.Port, protocol, auth, out passwordLength, out passwordData, ref item); try { if (result != OSStatus.Ok) return null; var username = GetUsernameFromKeychainItemRef (item); return Tuple.Create (username, Marshal.PtrToStringAuto (passwordData, (int) passwordLength)); } finally { CFRelease (item); } }