static unsafe SecKeychainAttributeList *GetAttributeListFromKeychainItemRef(IntPtr itemRef) { int[] attributeTags = { (int)SecItemAttr.Account }; int[] formatConstants = { CSSM_DB_ATTRIBUTE_FORMAT_STRING }; fixed(int *tags = attributeTags, formats = formatConstants) { var attributeInfo = new SecKeychainAttributeInfo { Count = 1, Tag = tags, Format = formats }; uint length = 0; IntPtr outData = IntPtr.Zero; SecKeychainAttributeList *attributeList; OSStatus attributeStatus = SecKeychainItemCopyAttributesAndData(itemRef, ref attributeInfo, IntPtr.Zero, &attributeList, ref length, ref outData); if (attributeStatus == OSStatus.ItemNotFound) { throw new Exception("Could not add internet password to keychain: " + GetError(attributeStatus)); } if (attributeStatus != OSStatus.Ok) { throw new Exception("Could not find internet username and password: " + GetError(attributeStatus)); } return(attributeList); } }
public static extern int SecKeychainItemCopyAttributesAndData( IntPtr itemRef, ref SecKeychainAttributeInfo info, IntPtr itemClass, // SecItemClass* out IntPtr attrList, // SecKeychainAttributeList* out uint dataLength, IntPtr data);
private static byte[] GetAccountNameAttributeData(IntPtr itemRef) { IntPtr tagArrayPtr = IntPtr.Zero; IntPtr formatArrayPtr = IntPtr.Zero; IntPtr attrListPtr = IntPtr.Zero; // SecKeychainAttributeList try { // Extract the user name by querying for the item's 'account' attribute tagArrayPtr = Marshal.AllocCoTaskMem(sizeof(SecKeychainAttrType)); Marshal.Copy(new[] { (int)SecKeychainAttrType.AccountItem }, 0, tagArrayPtr, 1); formatArrayPtr = Marshal.AllocCoTaskMem(sizeof(CssmDbAttributeFormat)); Marshal.Copy(new[] { (int)CssmDbAttributeFormat.String }, 0, formatArrayPtr, 1); var attributeInfo = new SecKeychainAttributeInfo { Count = 1, Tag = tagArrayPtr, Format = formatArrayPtr, }; ThrowOnError( SecKeychainItemCopyAttributesAndData( itemRef, ref attributeInfo, IntPtr.Zero, out attrListPtr, out var _, IntPtr.Zero) ); SecKeychainAttributeList attrList = Marshal.PtrToStructure <SecKeychainAttributeList>(attrListPtr); Debug.Assert(attrList.Count == 1); byte[] attrListArrayBytes = NativeMethods.ToByteArray( attrList.Attributes, Marshal.SizeOf <SecKeychainAttribute>() * attrList.Count); SecKeychainAttribute[] attributes = NativeMethods.ToStructArray <SecKeychainAttribute>(attrListArrayBytes); Debug.Assert(attributes.Length == 1); return(NativeMethods.ToByteArray(attributes[0].Data, attributes[0].Length)); } finally { if (tagArrayPtr != IntPtr.Zero) { Marshal.FreeCoTaskMem(tagArrayPtr); } if (formatArrayPtr != IntPtr.Zero) { Marshal.FreeCoTaskMem(formatArrayPtr); } if (attrListPtr != IntPtr.Zero) { SecKeychainItemFreeAttributesAndData(attrListPtr, IntPtr.Zero); } } }
private static byte[] GetAccountNameAttributeData(IntPtr itemRef) { IntPtr tagArrayPtr = IntPtr.Zero; IntPtr formatArrayPtr = IntPtr.Zero; IntPtr attrListPtr = IntPtr.Zero; // SecKeychainAttributeList try { // Extract the user name by querying for the item's 'account' attribute tagArrayPtr = Marshal.AllocHGlobal(sizeof(SecKeychainAttrType)); Marshal.WriteInt32(tagArrayPtr, (int)SecKeychainAttrType.AccountItem); formatArrayPtr = Marshal.AllocHGlobal(sizeof(CssmDbAttributeFormat)); Marshal.WriteInt32(formatArrayPtr, (int)CssmDbAttributeFormat.String); var attributeInfo = new SecKeychainAttributeInfo { Count = 1, Tag = tagArrayPtr, Format = formatArrayPtr, }; ThrowIfError( SecKeychainItemCopyAttributesAndData( itemRef, ref attributeInfo, IntPtr.Zero, out attrListPtr, out _, IntPtr.Zero) ); SecKeychainAttributeList attrList = Marshal.PtrToStructure <SecKeychainAttributeList>(attrListPtr); Debug.Assert(attrList.Count == 1, "Only expecting a list structure containing one attribute to be returned"); SecKeychainAttribute attribute = Marshal.PtrToStructure <SecKeychainAttribute>(attrList.Attributes); return(InteropUtils.ToByteArray(attribute.Data, attribute.Length)); } finally { if (tagArrayPtr != IntPtr.Zero) { Marshal.FreeHGlobal(tagArrayPtr); } if (formatArrayPtr != IntPtr.Zero) { Marshal.FreeHGlobal(formatArrayPtr); } if (attrListPtr != IntPtr.Zero) { SecKeychainItemFreeAttributesAndData(attrListPtr, IntPtr.Zero); } } }
static unsafe string GetUsernameFromKeychainItemRef(IntPtr itemRef) { int[] formatConstants = { (int)CssmDbAttributeFormat.String }; int[] attributeTags = { (int)SecItemAttr.Account }; fixed(int *tags = attributeTags, formats = formatConstants) { var attributeInfo = new SecKeychainAttributeInfo { Count = 1, Tag = tags, Format = formats }; SecKeychainAttributeList *attributeList = null; SecItemClass itemClass = 0; try { SecStatusCode status = SecKeychainItemCopyAttributesAndData(itemRef, &attributeInfo, ref itemClass, &attributeList, IntPtr.Zero, IntPtr.Zero); if (status == SecStatusCode.ItemNotFound) { throw new Exception("Could not add internet password to keychain: " + status.GetStatusDescription()); } if (status != SecStatusCode.Success) { throw new Exception("Could not find internet username and password: " + status.GetStatusDescription()); } var userNameAttr = (SecKeychainAttribute *)attributeList->Attrs; if (userNameAttr->Length == 0) { return(null); } return(Marshal.PtrToStringAuto(userNameAttr->Data, (int)userNameAttr->Length)); } finally { SecKeychainItemFreeAttributesAndData(attributeList, IntPtr.Zero); } } }
static unsafe string GetUsernameFromKeychainItemRef(IntPtr itemRef) { int[] formatConstants = { (int)CssmDbAttributeFormat.String }; int[] attributeTags = { (int)SecItemAttr.Account }; fixed(int *tags = attributeTags, formats = formatConstants) { var attributeInfo = new SecKeychainAttributeInfo { Count = 1, Tag = tags, Format = formats }; SecKeychainAttributeList *attributeList; IntPtr outData = IntPtr.Zero; SecItemClass itemClass = 0; uint length = 0; OSStatus status = SecKeychainItemCopyAttributesAndData(itemRef, &attributeInfo, ref itemClass, &attributeList, ref length, ref outData); if (status == OSStatus.ItemNotFound) { throw new Exception("Could not add internet password to keychain: " + GetError(status)); } if (status != OSStatus.Ok) { throw new Exception("Could not find internet username and password: " + GetError(status)); } var userNameAttr = (SecKeychainAttribute *)attributeList->Attrs; if (userNameAttr->Length == 0) { return(null); } return(Marshal.PtrToStringAuto(userNameAttr->Data, (int)userNameAttr->Length)); } }
static unsafe string GetUsernameFromKeychainItemRef (IntPtr itemRef) { int[] formatConstants = { (int) CssmDbAttributeFormat.String }; int[] attributeTags = { (int) SecItemAttr.Account }; fixed (int* tags = attributeTags, formats = formatConstants) { var attributeInfo = new SecKeychainAttributeInfo { Count = 1, Tag = tags, Format = formats }; SecKeychainAttributeList* attributeList; IntPtr outData = IntPtr.Zero; SecItemClass itemClass = 0; uint length = 0; OSStatus status = SecKeychainItemCopyAttributesAndData (itemRef, &attributeInfo, ref itemClass, &attributeList, ref length, ref outData); if (status == OSStatus.ItemNotFound) throw new Exception ("Could not add internet password to keychain: " + GetError (status)); if (status != OSStatus.Ok) throw new Exception ("Could not find internet username and password: " + GetError (status)); var userNameAttr = (SecKeychainAttribute*) attributeList->Attrs; if (userNameAttr->Length == 0) return null; return Marshal.PtrToStringAuto (userNameAttr->Data, (int) userNameAttr->Length); } }
static extern unsafe OSStatus SecKeychainItemCopyAttributesAndData (IntPtr itemRef, SecKeychainAttributeInfo* info, ref SecItemClass itemClass, SecKeychainAttributeList** attrList, ref uint length, ref IntPtr outData);
static extern unsafe OSStatus SecKeychainItemCopyAttributesAndData(IntPtr itemRef, ref SecKeychainAttributeInfo info, IntPtr itemClass, SecKeychainAttributeList **attrList, ref uint length, ref IntPtr outData);