Skip to content

Commit 742878d

Browse files
committed
Integrate cert pinning into DropboxCertHelper.
1 parent a35dfbb commit 742878d

File tree

5 files changed

+37
-26
lines changed

5 files changed

+37
-26
lines changed

Dropbox.Api/DropboxCertHelper.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ namespace Dropbox.Api
1010
using System.Collections.Generic;
1111
using System.Diagnostics.CodeAnalysis;
1212
using System.Linq;
13+
using System.Net;
1314
using System.Text;
1415

1516
/// <summary>
@@ -224,5 +225,29 @@ public static bool IsKnownRootCertPublicKey(byte[] publicKey)
224225

225226
return ValidRoots.Contains(publicKeyString);
226227
}
228+
229+
# if PORTABLE
230+
# elif PORTABLE40
231+
# else
232+
/// <summary>
233+
/// Initializes ssl certificate pinning.
234+
/// </summary>
235+
public static void InitializeCertPinning()
236+
{
237+
ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) =>
238+
{
239+
if (sslPolicyErrors != System.Net.Security.SslPolicyErrors.None)
240+
{
241+
return false;
242+
}
243+
244+
var root = chain.ChainElements[chain.ChainElements.Count - 1];
245+
var publicKey = root.Certificate.GetPublicKeyString();
246+
247+
return DropboxCertHelper.IsKnownRootCertPublicKey(publicKey);
248+
};
249+
}
250+
#endif
251+
227252
}
228253
}

Examples/SimpleBlogDemo/SimpleBlogDemo.csproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -231,9 +231,9 @@
231231
<Content Include="packages.config" />
232232
</ItemGroup>
233233
<ItemGroup>
234-
<ProjectReference Include="..\..\Dropbox.Api\Dropbox.Api.Portable.csproj">
235-
<Project>{786c830f-07a1-408b-bd7f-6ee04809d6db}</Project>
236-
<Name>Dropbox.Api.Portable</Name>
234+
<ProjectReference Include="..\..\Dropbox.Api\Dropbox.Api.csproj">
235+
<Project>{68180b54-4724-4cd1-baa6-ee7bc309797c}</Project>
236+
<Name>Dropbox.Api</Name>
237237
</ProjectReference>
238238
</ItemGroup>
239239
<PropertyGroup>
@@ -271,4 +271,4 @@
271271
</Target>
272272
<Target Name="AfterBuild">
273273
</Target> -->
274-
</Project>
274+
</Project>

Examples/SimpleBusinessDashboard/SimpleBusinessDashboard.csproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?xml version="1.0" encoding="utf-8"?>
1+
<?xml version="1.0" encoding="utf-8"?>
22
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
33
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
44
<PropertyGroup>
@@ -220,9 +220,9 @@
220220
<Content Include="packages.config" />
221221
</ItemGroup>
222222
<ItemGroup>
223-
<ProjectReference Include="..\..\Dropbox.Api\Dropbox.Api.Portable.csproj">
224-
<Project>{786c830f-07a1-408b-bd7f-6ee04809d6db}</Project>
225-
<Name>Dropbox.Api.Portable</Name>
223+
<ProjectReference Include="..\..\Dropbox.Api\Dropbox.Api.csproj">
224+
<Project>{68180b54-4724-4cd1-baa6-ee7bc309797c}</Project>
225+
<Name>Dropbox.Api</Name>
226226
</ProjectReference>
227227
</ItemGroup>
228228
<PropertyGroup>

Examples/SimpleTest/Program.cs

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ static int Main(string[] args)
3232

3333
private async Task<int> Run()
3434
{
35-
InitializeCertPinning();
35+
DropboxCertHelper.InitializeCertPinning();
3636

3737
var accessToken = await this.GetAccessToken();
3838
if (string.IsNullOrEmpty(accessToken))
@@ -126,20 +126,6 @@ private async Task RunTeamTests(DropboxTeamClient client)
126126
}
127127
}
128128

129-
/// <summary>
130-
/// Initializes ssl certificate pinning.
131-
/// </summary>
132-
private void InitializeCertPinning()
133-
{
134-
ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) =>
135-
{
136-
var root = chain.ChainElements[chain.ChainElements.Count - 1];
137-
var publicKey = root.Certificate.GetPublicKeyString();
138-
139-
return DropboxCertHelper.IsKnownRootCertPublicKey(publicKey);
140-
};
141-
}
142-
143129
/// <summary>
144130
/// Gets the dropbox access token.
145131
/// <para>

Examples/SimpleTest/SimpleTest.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,9 @@
9191
<Reference Include="WindowsBase" />
9292
</ItemGroup>
9393
<ItemGroup>
94-
<ProjectReference Include="..\..\Dropbox.Api\Dropbox.Api.Portable.csproj">
95-
<Project>{786c830f-07a1-408b-bd7f-6ee04809d6db}</Project>
96-
<Name>Dropbox.Api.Portable</Name>
94+
<ProjectReference Include="..\..\Dropbox.Api\Dropbox.Api.csproj">
95+
<Project>{68180b54-4724-4cd1-baa6-ee7bc309797c}</Project>
96+
<Name>Dropbox.Api</Name>
9797
</ProjectReference>
9898
</ItemGroup>
9999
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />

0 commit comments

Comments
 (0)