Skip to content
This repository has been archived by the owner on May 15, 2024. It is now read-only.

GH-464 Dispose of any SecRecord inside of the keychain #499

Merged
merged 4 commits into from
Oct 1, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 34 additions & 30 deletions Xamarin.Essentials/SecureStorage/SecureStorage.ios.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,57 +69,61 @@ SecRecord ExistingRecordForKey(string key, string service)

internal string ValueForKey(string key, string service)
{
var record = ExistingRecordForKey(key, service);
var match = SecKeyChain.QueryAsRecord(record, out var resultCode);

if (resultCode == SecStatusCode.Success)
return NSString.FromData(match.ValueData, NSStringEncoding.UTF8);
else
return null;
using (var record = ExistingRecordForKey(key, service))
using (var match = SecKeyChain.QueryAsRecord(record, out var resultCode))
{
if (resultCode == SecStatusCode.Success)
return NSString.FromData(match.ValueData, NSStringEncoding.UTF8);
else
return null;
}
}

internal void SetValueForKey(string value, string key, string service)
{
var record = ExistingRecordForKey(key, service);
if (string.IsNullOrEmpty(value))
using (var record = ExistingRecordForKey(key, service))
{
if (string.IsNullOrEmpty(value))
{
if (!string.IsNullOrEmpty(ValueForKey(key, service)))
RemoveRecord(record);

return;
}

// if the key already exists, remove it
if (!string.IsNullOrEmpty(ValueForKey(key, service)))
RemoveRecord(record);

return;
}

// if the key already exists, remove it
if (!string.IsNullOrEmpty(ValueForKey(key, service)))
RemoveRecord(record);

var result = SecKeyChain.Add(CreateRecordForNewKeyValue(key, value, service));
if (result != SecStatusCode.Success)
throw new Exception($"Error adding record: {result}");
using (var newRecord = CreateRecordForNewKeyValue(key, value, service))
{
var result = SecKeyChain.Add(newRecord);
if (result != SecStatusCode.Success)
throw new Exception($"Error adding record: {result}");
}
}

internal bool Remove(string key, string service)
{
var record = ExistingRecordForKey(key, service);
var match = SecKeyChain.QueryAsRecord(record, out var resultCode);

if (resultCode == SecStatusCode.Success)
using (var record = ExistingRecordForKey(key, service))
using (var match = SecKeyChain.QueryAsRecord(record, out var resultCode))
{
RemoveRecord(record);
return true;
if (resultCode == SecStatusCode.Success)
{
RemoveRecord(record);
return true;
}
}

return false;
}

internal void RemoveAll(string service)
{
var query = new SecRecord(SecKind.GenericPassword)
using (var query = new SecRecord(SecKind.GenericPassword) { Service = service })
{
Service = service
};

SecKeyChain.Remove(query);
SecKeyChain.Remove(query);
}
}

SecRecord CreateRecordForNewKeyValue(string key, string value, string service)
Expand Down