-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
[Android] Allow using installed certificates for client authentication in SslStream #103337
Merged
simonrozsival
merged 13 commits into
dotnet:main
from
simonrozsival:android-sslstream-support-keystore-privatekeyentry
Jun 19, 2024
Merged
Changes from 3 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
24c687f
Add support for KeyStore.PrivateKeyEntry
simonrozsival fef9203
Add test
simonrozsival 598508c
Merge branch 'main' into android-sslstream-support-keystore-privateke…
simonrozsival 35690e1
Update src/libraries/Common/tests/TestUtilities/System/AndroidKeyStor…
simonrozsival dadc2d5
Fix outdated comment
simonrozsival 25b2088
Fix tests
simonrozsival aa7a643
Merge branch 'main' of github.com:dotnet/runtime into android-sslstre…
simonrozsival 4ccb253
Fix typo
simonrozsival b9e78eb
Add helper method
simonrozsival d93c08e
Address lifetime concerns
simonrozsival a62afea
Fix bool marshalling
simonrozsival 86327ef
Improve code quality
simonrozsival 49663a6
Improve tests
simonrozsival File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
src/libraries/Common/tests/TestUtilities/System/AndroidKeyStoreHelper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
|
||
simonrozsival marked this conversation as resolved.
Show resolved
Hide resolved
|
||
using System.Security.Cryptography.X509Certificates; | ||
|
||
namespace System | ||
{ | ||
public static partial class AndroidKeyStoreHelper | ||
{ | ||
public static (X509Store, string) AddCertificate(X509Certificate2 cert) | ||
{ | ||
// Add the certificate to the Android keystore via X509Store | ||
// the alias is the certificate hash string (sha256) | ||
X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser); | ||
store.Open(OpenFlags.ReadWrite); | ||
store.Add(cert); | ||
string alias = cert.GetCertHashString(System.Security.Cryptography.HashAlgorithmName.SHA256); | ||
return (store, alias); | ||
} | ||
|
||
public static X509Certificate2 GetCertificateViaAlias(X509Store store, string alias) | ||
{ | ||
var privateKeyEntry = Interop.AndroidCrypto.X509StoreGetPrivateKeyEntry(store.StoreHandle, alias); | ||
return new X509Certificate2(privateKeyEntry); | ||
} | ||
|
||
public static bool DeleteAlias(X509Store store, string alias) | ||
{ | ||
return Interop.AndroidCrypto.X509StoreDeleteEntry(store.StoreHandle, alias); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
...tem.Security.Cryptography.Native.Android/net/dot/android/crypto/DotnetX509KeyManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
package net.dot.android.crypto; | ||
|
||
import java.net.Socket; | ||
import java.security.KeyStore; | ||
import java.security.Principal; | ||
import java.security.PrivateKey; | ||
import java.security.cert.Certificate; | ||
import java.security.cert.X509Certificate; | ||
import java.util.ArrayList; | ||
|
||
import javax.net.ssl.X509KeyManager; | ||
|
||
public final class DotnetX509KeyManager implements X509KeyManager { | ||
private static final String CLIENT_CERTIFICATE_ALIAS = "DOTNET_SSLStream_ClientCertificateContext"; | ||
|
||
private final PrivateKey privateKey; | ||
private final X509Certificate[] certificateChain; | ||
|
||
public DotnetX509KeyManager(KeyStore.PrivateKeyEntry privateKeyEntry) { | ||
if (privateKeyEntry == null) { | ||
throw new IllegalArgumentException("PrivateKeyEntry must not be null"); | ||
} | ||
|
||
this.privateKey = privateKeyEntry.getPrivateKey(); | ||
|
||
Certificate[] certificates = privateKeyEntry.getCertificateChain(); | ||
ArrayList<X509Certificate> x509Certificates = new ArrayList<>(); | ||
for (Certificate certificate : certificates) { | ||
if (certificate instanceof X509Certificate) { | ||
x509Certificates.add((X509Certificate) certificate); | ||
} | ||
} | ||
|
||
if (x509Certificates.size() == 0) { | ||
throw new IllegalArgumentException("No valid X509 certificates found in the chain"); | ||
} | ||
|
||
this.certificateChain = x509Certificates.toArray(new X509Certificate[0]); | ||
} | ||
|
||
@Override | ||
public String[] getClientAliases(String keyType, Principal[] issuers) { | ||
return new String[] { CLIENT_CERTIFICATE_ALIAS }; | ||
} | ||
|
||
@Override | ||
public String chooseClientAlias(String[] keyType, Principal[] issuers, Socket socket) { | ||
return CLIENT_CERTIFICATE_ALIAS; | ||
} | ||
|
||
@Override | ||
public String[] getServerAliases(String keyType, Principal[] issuers) { | ||
return new String[0]; | ||
} | ||
|
||
@Override | ||
public String chooseServerAlias(String keyType, Principal[] issuers, Socket socket) { | ||
return null; | ||
} | ||
|
||
@Override | ||
public X509Certificate[] getCertificateChain(String alias) { | ||
return certificateChain; | ||
} | ||
|
||
@Override | ||
public PrivateKey getPrivateKey(String alias) { | ||
return privateKey; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@simonrozsival the native code returns int32_t here so we should change this to UnmanagedType.Bool (4 bytes), or change the native code to use a C
bool
which is 1 byte. We seem to be using both variants in the Android crypto imports...There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At least in the Linux and MacOS Desktop PALs, we try to use
int32_t
and not a Cbool
since there is no guarantee by the C standard that it is exactly one byte.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh, thanks for pointing that out!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#103825