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 extern unsafe OSStatus SecKeychainItemModifyAttributesAndData(IntPtr itemRef, SecKeychainAttributeList *attrList, uint length, byte [] data);
static extern unsafe OSStatus SecKeychainItemCreateFromContent(SecItemClass itemClass, SecKeychainAttributeList *attrList, uint passwordLength, byte[] password, IntPtr keychain, IntPtr initialAccess, ref IntPtr itemRef);
static extern unsafe OSStatus SecKeychainSearchCreateFromAttributes(IntPtr keychainOrArray, SecItemClass itemClass, SecKeychainAttributeList *attrList, out IntPtr searchRef);
static extern unsafe SecStatusCode SecKeychainItemFreeAttributesAndData(SecKeychainAttributeList *list, IntPtr data);
static unsafe SecKeychainAttribute *GetUsernameAttributeFromAttributeList(SecKeychainAttributeList *attributeList) { return((SecKeychainAttribute *)attributeList->Attrs); }