[btls]: Add option to specify the certificate search paths. (#4318)

* [btls]: Add option to specify the certificate search paths.

* MonoTlsSettings.CertificateSearchPaths: new property.

Valid values are

- @default - add default search paths
- @user - add default user search path
- @machine - add default machine search path
- @trust - add 'MonoTlsSettings.TrustedRoots'
- @pem:<directory> - custom directory containing certificates in PEM format
- @der:<directory> - same, in DER format

* Make it internal for the moment.

* Cosmetic; use "?.".
This commit is contained in:
Martin Baulig authored and GitHub committed 2017-02-06 23:18:21 +01:00
1 parent 17c867e883
commit 4ecb683985
3 files changed
+73 -4

No files matched your search

@@ -75,6 +75,10 @@ namespace Mono.Security.Interface
get; set;
}
internal string[] CertificateSearchPaths {
get; set;
}
/*
* If you set this here, then it will override 'ServicePointManager.SecurityProtocol'.
*/
@@ -161,7 +165,13 @@ namespace Mono.Security.Interface
UserSettings = other.UserSettings;
EnabledProtocols = other.EnabledProtocols;
EnabledCiphers = other.EnabledCiphers;
TrustAnchors = other.TrustAnchors;
if (other.TrustAnchors != null)
TrustAnchors = new X509CertificateCollection (other.TrustAnchors);
if (other.CertificateSearchPaths != null) {
CertificateSearchPaths = new string [other.CertificateSearchPaths.Length];
other.CertificateSearchPaths.CopyTo (CertificateSearchPaths, 0);
}
cloned = true;
}
@@ -220,7 +220,7 @@ namespace Mono.Btls
void SetupCertificateStore ()
{
MonoBtlsProvider.SetupCertificateStore (ctx.CertificateStore);
MonoBtlsProvider.SetupCertificateStore (ctx.CertificateStore, Settings, IsServer);
if (Settings != null && Settings.TrustAnchors != null) {
var trust = IsServer ? MonoBtlsX509TrustKind.TRUST_CLIENT : MonoBtlsX509TrustKind.TRUST_SERVER;
+61 -2
View File
@@ -150,7 +150,7 @@ namespace Mono.Btls
using (var nativeChain = MonoBtlsProvider.GetNativeChain (certificates))
using (var param = GetVerifyParam (targetHost, serverMode))
using (var storeCtx = new MonoBtlsX509StoreCtx ()) {
SetupCertificateStore (store);
SetupCertificateStore (store, validator.Settings, serverMode);
storeCtx.Initialize (store, nativeChain);
@@ -201,21 +201,80 @@ namespace Mono.Btls
}
}
internal static void SetupCertificateStore (MonoBtlsX509Store store, MonoTlsSettings settings, bool server)
{
#if MONODROID
SetupCertificateStore (store);
return;
#else
if (settings?.CertificateSearchPaths == null) {
SetupCertificateStore (store);
return;
}
foreach (var path in settings.CertificateSearchPaths) {
if (string.Equals (path, "@default", StringComparison.Ordinal)) {
AddTrustedRoots (store, settings, server);
AddUserStore (store);
AddMachineStore (store);
} else if (string.Equals (path, "@user", StringComparison.Ordinal))
AddUserStore (store);
else if (string.Equals (path, "@machine", StringComparison.Ordinal))
AddMachineStore (store);
else if (string.Equals (path, "@trusted", StringComparison.Ordinal))
AddTrustedRoots (store, settings, server);
else if (path.StartsWith ("@pem:", StringComparison.Ordinal)) {
var realPath = path.Substring (5);
if (Directory.Exists (realPath))
store.AddDirectoryLookup (realPath, MonoBtlsX509FileType.PEM);
} else if (path.StartsWith ("@der:", StringComparison.Ordinal)) {
var realPath = path.Substring (5);
if (Directory.Exists (realPath))
store.AddDirectoryLookup (realPath, MonoBtlsX509FileType.ASN1);
} else {
if (Directory.Exists (path))
store.AddDirectoryLookup (path, MonoBtlsX509FileType.PEM);
}
}
#endif
}
internal static void SetupCertificateStore (MonoBtlsX509Store store)
{
#if MONODROID
store.SetDefaultPaths ();
store.AddAndroidLookup ();
#else
AddUserStore (store);
AddMachineStore (store);
#endif
}
#if !MONODROID
static void AddUserStore (MonoBtlsX509Store store)
{
var userPath = MonoBtlsX509StoreManager.GetStorePath (MonoBtlsX509StoreType.UserTrustedRoots);
if (Directory.Exists (userPath))
store.AddDirectoryLookup (userPath, MonoBtlsX509FileType.PEM);
}
static void AddMachineStore (MonoBtlsX509Store store)
{
var machinePath = MonoBtlsX509StoreManager.GetStorePath (MonoBtlsX509StoreType.MachineTrustedRoots);
if (Directory.Exists (machinePath))
store.AddDirectoryLookup (machinePath, MonoBtlsX509FileType.PEM);
#endif
}
static void AddTrustedRoots (MonoBtlsX509Store store, MonoTlsSettings settings, bool server)
{
if (settings?.TrustAnchors == null)
return;
var trust = server ? MonoBtlsX509TrustKind.TRUST_CLIENT : MonoBtlsX509TrustKind.TRUST_SERVER;
store.AddCollection (settings.TrustAnchors, trust);
}
#endif
public static string GetSystemStoreLocation ()
{
#if MONODROID