Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Mono.Android] Add convenience KeyChain APIs #9047

Merged
merged 6 commits into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
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
93 changes: 93 additions & 0 deletions src/Mono.Android/Android.Security/KeyChain.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#nullable enable
using System;
using System.Runtime.Versioning;
using System.Security.Cryptography.X509Certificates;
using System.Threading.Tasks;

namespace Android.Security
{
public partial class KeyChain
{
public static X509Certificate2? GetX509Certificate2WithPrivateKey (Android.Content.Context context, string alias)
{
var privateKey = KeyChain.GetPrivateKey (context, alias);
if (privateKey is null) {
return null;
}

var chain = KeyChain.GetCertificateChain (context, alias);
if (chain is null) {
return null;
}

var privateKeyEntry = new Java.Security.KeyStore.PrivateKeyEntry (privateKey, chain);
var certificate = new X509Certificate2 (privateKeyEntry.Handle);
GC.KeepAlive (privateKeyEntry);
return certificate;
}

[SupportedOSPlatform("android23.0")]
public static async Task<string?> ChoosePrivateKeyAliasAsync (
Android.App.Activity activity,
string[]? keyTypes,
Java.Security.IPrincipal[]? issuers,
Android.Net.Uri? uri,
string? alias)
{
var tcs = new TaskCompletionSource<string?> ();
KeyChain.ChoosePrivateKeyAlias (activity, new KeyChainAliasCallback(tcs), keyTypes, issuers, uri, alias);
return await tcs.Task;
}

public static async Task<string?> ChoosePrivateKeyAliasAsync (
Android.App.Activity activity,
string[]? keyTypes,
Java.Security.IPrincipal[]? issuers,
string? host,
int port,
string? alias)
{
var tcs = new TaskCompletionSource<string?> ();
KeyChain.ChoosePrivateKeyAlias (activity, new KeyChainAliasCallback(tcs), keyTypes, issuers, host, port, alias);
return await tcs.Task;
}

[SupportedOSPlatform("android23.0")]
public static async Task<X509Certificate2?> ChooseX509Certificate2WithPrivateKeyAsync (
Android.App.Activity activity,
string[]? keyTypes,
Java.Security.IPrincipal[]? issuers,
Android.Net.Uri? uri,
string? alias)
{
alias = await ChoosePrivateKeyAliasAsync (activity, keyTypes, issuers, uri, alias);
if (alias is null) {
return null;
}

return GetX509Certificate2WithPrivateKey (activity, alias);
}

public static async Task<X509Certificate2?> ChooseX509Certificate2WithPrivateKeyAsync (
Android.App.Activity activity,
string[]? keyTypes,
Java.Security.IPrincipal[]? issuers,
string? host,
int port,
string? alias)
{
alias = await ChoosePrivateKeyAliasAsync (activity, keyTypes, issuers, host, port, alias);
if (alias is null) {
return null;
}

return GetX509Certificate2WithPrivateKey (activity, alias);
}

private sealed class KeyChainAliasCallback(TaskCompletionSource<string?> tcs)
: Java.Lang.Object, IKeyChainAliasCallback
{
public void Alias (string? alias) => tcs.SetResult (alias);
}
}
}
5 changes: 3 additions & 2 deletions src/Mono.Android/Mono.Android.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
<AdditionalFiles Include="PublicAPI\API-$(AndroidApiLevel)\PublicAPI.Shipped.txt" />
<AdditionalFiles Include="PublicAPI\API-$(AndroidApiLevel)\PublicAPI.Unshipped.txt" />
</ItemGroup>

<PropertyGroup>
<JavaCallableWrapperAbsAssembly>$([System.IO.Path]::GetFullPath ('$(OutputPath)$(AssemblyName).dll'))</JavaCallableWrapperAbsAssembly>
</PropertyGroup>
Expand Down Expand Up @@ -263,6 +263,7 @@
<Compile Include="Android.Runtime\XAPeerMembers.cs" />
<Compile Include="Android.Runtime\XmlPullParserReader.cs" />
<Compile Include="Android.Runtime\XmlReaderPullParser.cs" />
<Compile Include="Android.Security\KeyChain.cs" />
<Compile Include="Android.Telephony\CellInfo.cs" />
<Compile Include="Android.Telephony\PhoneNumberUtils.cs" />
<Compile Include="Android.Telephony\TelephonyManager.cs" />
Expand Down Expand Up @@ -395,7 +396,7 @@
<PropertyGroup Condition=" '$(AndroidApiLevel)' &lt; '$(AndroidDefaultTargetDotnetApiLevel)' ">
<BuildDependsOn></BuildDependsOn>
</PropertyGroup>

<PropertyGroup Condition=" '$(AndroidApiLevel)' &gt;= '$(AndroidDefaultTargetDotnetApiLevel)' ">
<BuildDependsOn>
$(BuildDependsOn);
Expand Down
5 changes: 5 additions & 0 deletions src/Mono.Android/PublicAPI/API-35/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3957,3 +3957,8 @@ virtual Java.Util.Zip.Inflater.SetDictionary(Java.Nio.ByteBuffer? dictionary) ->
virtual Java.Util.Zip.Inflater.SetInput(Java.Nio.ByteBuffer? input) -> void
virtual Java.Util.Zip.ZipEntry.TimeLocal.get -> Java.Time.LocalDateTime?
virtual Java.Util.Zip.ZipEntry.TimeLocal.set -> void
static Android.Security.KeyChain.GetX509Certificate2WithPrivateKey(Android.Content.Context! context, string! alias) -> System.Security.Cryptography.X509Certificates.X509Certificate2?
static Android.Security.KeyChain.ChooseX509Certificate2WithPrivateKeyAsync(Android.App.Activity! activity, string![]? keyTypes, Java.Security.IPrincipal![]? issuers, Android.Net.Uri? uri, string? alias) -> System.Threading.Tasks.Task<System.Security.Cryptography.X509Certificates.X509Certificate2?>!
static Android.Security.KeyChain.ChooseX509Certificate2WithPrivateKeyAsync(Android.App.Activity! activity, string![]? keyTypes, Java.Security.IPrincipal![]? issuers, string? host, int port, string? alias) -> System.Threading.Tasks.Task<System.Security.Cryptography.X509Certificates.X509Certificate2?>!
static Android.Security.KeyChain.ChoosePrivateKeyAliasAsync(Android.App.Activity! activity, string![]? keyTypes, Java.Security.IPrincipal![]? issuers, Android.Net.Uri? uri, string? alias) -> System.Threading.Tasks.Task<string?>!
static Android.Security.KeyChain.ChoosePrivateKeyAliasAsync(Android.App.Activity! activity, string![]? keyTypes, Java.Security.IPrincipal![]? issuers, string? host, int port, string? alias) -> System.Threading.Tasks.Task<string?>!
Loading