Skip to content
This repository was archived by the owner on Oct 14, 2021. It is now read-only.

Commit ec17560

Browse files
authored
Sign dlls and fix private key load from register on net framework (#52)
1 parent 105bbd8 commit ec17560

File tree

11 files changed

+136
-49
lines changed

11 files changed

+136
-49
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,4 @@ dotnet/dotnetframework/GeneXusFtps/obj/
6161
dotnet/dotnetframework/GeneXusFtps/bin/
6262
dotnet/dotnetframework/GeneXusFtps/packages.config
6363
dotnet/dotnetcore/GeneXusFtpsNetCore/obj/
64+
dotnet/resources/key.snk

SecurityAPIParent.sln

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,13 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "dotnetframework", "dotnetfr
4141
EndProject
4242
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SecurityAPITest", "test\dotnetframework\SecurityAPITest\SecurityAPITest.csproj", "{9100025A-68E0-4618-B83C-4D626CB89DBB}"
4343
EndProject
44-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SecurityAPITestNetCore", "test\dotnetcore\SecurityAPITestNetCore\SecurityAPITestNetCore.csproj", "{44B8B2B6-0842-4018-A547-EF33920EFC8E}"
44+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SecurityAPITestNetCore", "test\dotnetcore\SecurityAPITestNetCore\SecurityAPITestNetCore.csproj", "{44B8B2B6-0842-4018-A547-EF33920EFC8E}"
45+
EndProject
46+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "resources", "resources", "{88EB1BC4-0D68-4906-AA36-E1EA1F6AFD35}"
47+
ProjectSection(SolutionItems) = preProject
48+
dotnet\resources\key.snk = dotnet\resources\key.snk
49+
dotnet\resources\Neon.SSH.NET.dll = dotnet\resources\Neon.SSH.NET.dll
50+
EndProjectSection
4551
EndProject
4652
Global
4753
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -128,6 +134,7 @@ Global
128134
{7F5F1DDC-09BE-4567-8CD4-41387AD91BEE} = {750343D3-EAA6-433E-A099-71BC6851EA71}
129135
{9100025A-68E0-4618-B83C-4D626CB89DBB} = {7F5F1DDC-09BE-4567-8CD4-41387AD91BEE}
130136
{44B8B2B6-0842-4018-A547-EF33920EFC8E} = {89FFECB3-7A69-41F8-88C7-F05193E5CDF4}
137+
{88EB1BC4-0D68-4906-AA36-E1EA1F6AFD35} = {5CB1725B-3FA5-49B1-A1F8-41EFCACA3CF1}
131138
EndGlobalSection
132139
GlobalSection(ExtensibilityGlobals) = postSolution
133140
SolutionGuid = {AEB1693D-906F-402A-AD66-A1A9AE2ABBE2}

dotnet/dotnetframework/GeneXusCryptography/GeneXusCryptography.csproj

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,13 @@
3131
<ErrorReport>prompt</ErrorReport>
3232
<WarningLevel>4</WarningLevel>
3333
</PropertyGroup>
34+
<PropertyGroup>
35+
<SignAssembly>true</SignAssembly>
36+
</PropertyGroup>
37+
<PropertyGroup>
38+
<AssemblyOriginatorKeyFile>..\..\resources\key.snk</AssemblyOriginatorKeyFile>
39+
</PropertyGroup>
3440
<ItemGroup>
35-
<Reference Include="BouncyCastle.Crypto, Version=1.8.6.0, Culture=neutral, PublicKeyToken=0e99375e54769942">
36-
<HintPath>..\..\..\packages\BouncyCastle.1.8.6.1\lib\BouncyCastle.Crypto.dll</HintPath>
37-
</Reference>
3841
<Reference Include="System" />
3942
<Reference Include="System.Core" />
4043
<Reference Include="System.Xml.Linq" />
@@ -85,6 +88,9 @@
8588
</ProjectReference>
8689
</ItemGroup>
8790
<ItemGroup>
91+
<None Include="..\..\resources\key.snk">
92+
<Link>key.snk</Link>
93+
</None>
8894
<None Include="packages.config" />
8995
</ItemGroup>
9096
<ItemGroup />

dotnet/dotnetframework/GeneXusFtps/GeneXusFtps.csproj

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,13 @@
3131
<ErrorReport>prompt</ErrorReport>
3232
<WarningLevel>4</WarningLevel>
3333
</PropertyGroup>
34+
<PropertyGroup>
35+
<SignAssembly>true</SignAssembly>
36+
</PropertyGroup>
37+
<PropertyGroup>
38+
<AssemblyOriginatorKeyFile>..\..\resources\key.snk</AssemblyOriginatorKeyFile>
39+
</PropertyGroup>
3440
<ItemGroup>
35-
<Reference Include="FluentFTP, Version=31.3.2.0, Culture=neutral, PublicKeyToken=f4af092b1d8df44f, processorArchitecture=MSIL">
36-
<HintPath>..\..\..\packages\FluentFTP.31.3.2\lib\net45\FluentFTP.dll</HintPath>
37-
</Reference>
3841
<Reference Include="System" />
3942
<Reference Include="System.Core" />
4043
<Reference Include="System.Web" />
@@ -62,6 +65,9 @@
6265
</ProjectReference>
6366
</ItemGroup>
6467
<ItemGroup>
68+
<None Include="..\..\resources\key.snk">
69+
<Link>key.snk</Link>
70+
</None>
6571
<None Include="packages.config" />
6672
</ItemGroup>
6773
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />

dotnet/dotnetframework/GeneXusJWT/GeneXusJWT.csproj

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -31,30 +31,18 @@
3131
<ErrorReport>prompt</ErrorReport>
3232
<WarningLevel>4</WarningLevel>
3333
</PropertyGroup>
34+
<PropertyGroup>
35+
<SignAssembly>true</SignAssembly>
36+
</PropertyGroup>
37+
<PropertyGroup>
38+
<AssemblyOriginatorKeyFile>..\..\resources\key.snk</AssemblyOriginatorKeyFile>
39+
</PropertyGroup>
3440
<PropertyGroup>
3541
<AppConfig>App.$(Configuration).config</AppConfig>
3642
</PropertyGroup>
3743
<ItemGroup>
38-
<Reference Include="BouncyCastle.Crypto, Version=1.8.6.0, Culture=neutral, PublicKeyToken=0e99375e54769942">
39-
<HintPath>..\..\..\packages\BouncyCastle.1.8.6.1\lib\BouncyCastle.Crypto.dll</HintPath>
40-
</Reference>
41-
<Reference Include="Microsoft.IdentityModel.JsonWebTokens, Version=6.5.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
42-
<HintPath>..\..\..\packages\Microsoft.IdentityModel.JsonWebTokens.6.5.1\lib\net45\Microsoft.IdentityModel.JsonWebTokens.dll</HintPath>
43-
</Reference>
44-
<Reference Include="Microsoft.IdentityModel.Logging, Version=6.5.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
45-
<HintPath>..\..\..\packages\Microsoft.IdentityModel.Logging.6.5.1\lib\net45\Microsoft.IdentityModel.Logging.dll</HintPath>
46-
</Reference>
47-
<Reference Include="Microsoft.IdentityModel.Tokens, Version=6.5.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
48-
<HintPath>..\..\..\packages\Microsoft.IdentityModel.Tokens.6.5.1\lib\net45\Microsoft.IdentityModel.Tokens.dll</HintPath>
49-
</Reference>
50-
<Reference Include="Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
51-
<HintPath>..\..\..\packages\Newtonsoft.Json.10.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
52-
</Reference>
5344
<Reference Include="System" />
5445
<Reference Include="System.Core" />
55-
<Reference Include="System.IdentityModel.Tokens.Jwt, Version=6.5.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
56-
<HintPath>..\..\..\packages\System.IdentityModel.Tokens.Jwt.6.5.1\lib\net45\System.IdentityModel.Tokens.Jwt.dll</HintPath>
57-
</Reference>
5846
<Reference Include="System.Xml.Linq" />
5947
<Reference Include="System.Data.DataSetExtensions" />
6048
<Reference Include="Microsoft.CSharp" />
@@ -84,6 +72,9 @@
8472
<Compile Include="Utils\RevocationList.cs" />
8573
</ItemGroup>
8674
<ItemGroup>
75+
<None Include="..\..\resources\key.snk">
76+
<Link>key.snk</Link>
77+
</None>
8778
<None Include="app.config" />
8879
<None Include="App.Debug.config" />
8980
<None Include="packages.config" />

dotnet/dotnetframework/GeneXusSftp/GeneXusSftp.csproj

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@
2222
<DefineConstants>DEBUG;TRACE</DefineConstants>
2323
<ErrorReport>prompt</ErrorReport>
2424
<WarningLevel>4</WarningLevel>
25+
</PropertyGroup>
26+
<PropertyGroup>
27+
<SignAssembly>true</SignAssembly>
28+
</PropertyGroup>
29+
<PropertyGroup>
30+
<AssemblyOriginatorKeyFile>..\..\resources\key.snk</AssemblyOriginatorKeyFile>
2531
</PropertyGroup>
2632
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
2733
<DebugType>pdbonly</DebugType>
@@ -32,8 +38,8 @@
3238
<WarningLevel>4</WarningLevel>
3339
</PropertyGroup>
3440
<ItemGroup>
35-
<Reference Include="Neon.SSH.NET, Version=0.0.0.0, Culture=neutral, processorArchitecture=MSIL">
36-
<HintPath>..\..\..\packages\Neon.SSH.NET.1.0.4\lib\netstandard2.0\Neon.SSH.NET.dll</HintPath>
41+
<Reference Include="Neon.SSH.NET">
42+
<HintPath>..\..\resources\Neon.SSH.NET.dll</HintPath>
3743
</Reference>
3844
<Reference Include="System" />
3945
<Reference Include="System.Core" />
@@ -54,6 +60,9 @@
5460
<Compile Include="Utils\KnownHostStore.cs" />
5561
</ItemGroup>
5662
<ItemGroup>
63+
<None Include="..\..\resources\key.snk">
64+
<Link>key.snk</Link>
65+
</None>
5766
<None Include="packages.config" />
5867
</ItemGroup>
5968
<ItemGroup>

dotnet/dotnetframework/GeneXusXmlSignature/GeneXusXmlSignature.csproj

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@
2323
<ErrorReport>prompt</ErrorReport>
2424
<WarningLevel>4</WarningLevel>
2525
</PropertyGroup>
26+
<PropertyGroup>
27+
<SignAssembly>true</SignAssembly>
28+
</PropertyGroup>
29+
<PropertyGroup>
30+
<AssemblyOriginatorKeyFile>..\..\resources\key.snk</AssemblyOriginatorKeyFile>
31+
</PropertyGroup>
2632
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
2733
<DebugType>pdbonly</DebugType>
2834
<Optimize>true</Optimize>
@@ -32,9 +38,6 @@
3238
<WarningLevel>4</WarningLevel>
3339
</PropertyGroup>
3440
<ItemGroup>
35-
<Reference Include="BouncyCastle.Crypto, Version=1.8.6.0, Culture=neutral, PublicKeyToken=0e99375e54769942, processorArchitecture=MSIL">
36-
<HintPath>..\..\..\packages\Portable.BouncyCastle.1.8.6.7\lib\net40\BouncyCastle.Crypto.dll</HintPath>
37-
</Reference>
3841
<Reference Include="System" />
3942
<Reference Include="System.Core" />
4043
<Reference Include="System.Security" />
@@ -61,6 +64,9 @@
6164
<Compile Include="Utils\XmlSignatureWrapper.cs" />
6265
</ItemGroup>
6366
<ItemGroup>
67+
<None Include="..\..\resources\key.snk">
68+
<Link>key.snk</Link>
69+
</None>
6470
<None Include="packages.config" />
6571
</ItemGroup>
6672
<ItemGroup>

dotnet/dotnetframework/SecurityAPICommons/Config/Global.cs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
using System.Runtime.CompilerServices;
22

33

4-
[assembly: InternalsVisibleTo("SecurityAPITestNetCore")]
5-
[assembly: InternalsVisibleTo("SecurityAPITest")]
4+
65
namespace SecurityAPICommons.Config
76
{
87

9-
internal static class Global
8+
public static class Global
109
{
1110
private static string global_encoding = "UTF_8";
1211
public static string GLOBAL_ENCODING
@@ -24,7 +23,22 @@ public static string GLOBAL_ENCODING
2423
global_encoding = value;
2524
}
2625
}
27-
26+
private static string global_keyContainerName = "";
27+
public static string GLOBAL_KEY_COONTAINER_NAME
28+
{
29+
get
30+
{
31+
if (global_keyContainerName == null)
32+
{
33+
return "UTF_8";
34+
}
35+
return global_keyContainerName;
36+
}
37+
set
38+
{
39+
global_keyContainerName = value;
40+
}
41+
}
2842

2943

3044
}

dotnet/dotnetframework/SecurityAPICommons/Keys/PrivateKeyManager.cs

Lines changed: 53 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
using SecurityAPICommons.Commons;
1919
using SecurityAPICommons.Utils;
2020
using Org.BouncyCastle.Utilities.Encoders;
21+
using System.Security.AccessControl;
2122

2223
namespace SecurityAPICommons.Keys
2324
{
@@ -153,15 +154,35 @@ public AsymmetricAlgorithm getPrivateKeyForXML()
153154
byte[] serializedPrivateBytes = this.privateKeyInfo.ToAsn1Object().GetDerEncoded();
154155
string serializedPrivate = Convert.ToBase64String(serializedPrivateBytes);
155156
RsaPrivateCrtKeyParameters privateKey = (RsaPrivateCrtKeyParameters)PrivateKeyFactory.CreateKey(Convert.FromBase64String(serializedPrivate));
157+
#if NETCORE
158+
return DotNetUtilities.ToRSA(privateKey);
159+
#else
160+
161+
156162
/****System.Security.Cryptography.CryptographicException: The system cannot find the file specified.****/
157163
/****HACK****/
158164
//https://social.msdn.microsoft.com/Forums/vstudio/en-US/7ea48fd0-8d6b-43ed-b272-1a0249ae490f/systemsecuritycryptographycryptographicexception-the-system-cannot-find-the-file-specified?forum=clr#37d4d83d-0eb3-497a-af31-030f5278781a
159165
CspParameters cspParameters = new CspParameters();
160166
cspParameters.Flags = CspProviderFlags.UseMachineKeyStore;
161-
cspParameters.KeyContainerName = "MyKeyContainerName";
162-
return DotNetUtilities.ToRSA(privateKey, cspParameters);
167+
if (SecurityUtils.compareStrings(Config.Global.GLOBAL_KEY_COONTAINER_NAME, ""))
168+
{
169+
string uid = Guid.NewGuid().ToString();
170+
cspParameters.KeyContainerName = uid;
171+
Config.Global.GLOBAL_KEY_COONTAINER_NAME = uid;
172+
System.Security.Principal.SecurityIdentifier userId = new System.Security.Principal.SecurityIdentifier(System.Security.Principal.WindowsIdentity.GetCurrent().User.ToString());
173+
CryptoKeyAccessRule rule = new CryptoKeyAccessRule(userId, CryptoKeyRights.FullControl, AccessControlType.Allow);
174+
cspParameters.CryptoKeySecurity = new CryptoKeySecurity();
175+
cspParameters.CryptoKeySecurity.SetAccessRule(rule);
176+
}
177+
else
178+
{
179+
cspParameters.KeyContainerName = Config.Global.GLOBAL_KEY_COONTAINER_NAME;
180+
181+
}
163182
/****System.Security.Cryptography.CryptographicException: The system cannot find the file specified.****/
164183
/****HACK****/
184+
return DotNetUtilities.ToRSA(privateKey, cspParameters);
185+
#endif
165186

166187

167188
}
@@ -524,17 +545,37 @@ public AsymmetricAlgorithm getPrivateKeyForJWT()
524545
byte[] serializedPrivateBytes = this.privateKeyInfo.ToAsn1Object().GetDerEncoded();
525546
string serializedPrivate = Convert.ToBase64String(serializedPrivateBytes);
526547
RsaPrivateCrtKeyParameters privateKey = (RsaPrivateCrtKeyParameters)PrivateKeyFactory.CreateKey(Convert.FromBase64String(serializedPrivate));
527-
/****System.Security.Cryptography.CryptographicException: The system cannot find the file specified.****/
528-
/****HACK****/
529-
//https://social.msdn.microsoft.com/Forums/vstudio/en-US/7ea48fd0-8d6b-43ed-b272-1a0249ae490f/systemsecuritycryptographycryptographicexception-the-system-cannot-find-the-file-specified?forum=clr#37d4d83d-0eb3-497a-af31-030f5278781a
530-
CspParameters cspParameters = new CspParameters();
531-
cspParameters.Flags = CspProviderFlags.UseMachineKeyStore;
532-
cspParameters.KeyContainerName = "MyKeyContainerName";
533-
alg = DotNetUtilities.ToRSA(privateKey, cspParameters);
534-
/****System.Security.Cryptography.CryptographicException: The system cannot find the file specified.****/
535-
/****HACK****/
548+
#if NETCORE
549+
alg = DotNetUtilities.ToRSA(privateKey);
550+
#else
551+
552+
553+
/****System.Security.Cryptography.CryptographicException: The system cannot find the file specified.****/
554+
/****HACK****/
555+
//https://social.msdn.microsoft.com/Forums/vstudio/en-US/7ea48fd0-8d6b-43ed-b272-1a0249ae490f/systemsecuritycryptographycryptographicexception-the-system-cannot-find-the-file-specified?forum=clr#37d4d83d-0eb3-497a-af31-030f5278781a
556+
CspParameters cspParameters = new CspParameters();
557+
cspParameters.Flags = CspProviderFlags.UseMachineKeyStore;
558+
if (SecurityUtils.compareStrings(Config.Global.GLOBAL_KEY_COONTAINER_NAME, ""))
559+
{
560+
string uid = Guid.NewGuid().ToString();
561+
cspParameters.KeyContainerName = uid;
562+
Config.Global.GLOBAL_KEY_COONTAINER_NAME = uid;
563+
System.Security.Principal.SecurityIdentifier userId = new System.Security.Principal.SecurityIdentifier(System.Security.Principal.WindowsIdentity.GetCurrent().User.ToString());
564+
CryptoKeyAccessRule rule = new CryptoKeyAccessRule(userId, CryptoKeyRights.FullControl, AccessControlType.Allow);
565+
cspParameters.CryptoKeySecurity = new CryptoKeySecurity();
566+
cspParameters.CryptoKeySecurity.SetAccessRule(rule);
567+
}
568+
else
569+
{
570+
cspParameters.KeyContainerName = Config.Global.GLOBAL_KEY_COONTAINER_NAME;
571+
536572
}
537-
else if (SecurityUtils.compareStrings("ECDSA", algorithm))
573+
/****System.Security.Cryptography.CryptographicException: The system cannot find the file specified.****/
574+
/****HACK****/
575+
alg = DotNetUtilities.ToRSA(privateKey, cspParameters);
576+
#endif
577+
}
578+
else if (SecurityUtils.compareStrings("ECDSA", algorithm))
538579
{
539580
string b64Encoded = this.ToBase64();
540581
byte[] privKeyBytes8 = Convert.FromBase64String(b64Encoded);//Encoding.UTF8.GetBytes(privKeyEcc);

dotnet/dotnetframework/SecurityAPICommons/SecurityAPICommons.csproj

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,14 @@
3030
<DefineConstants>TRACE</DefineConstants>
3131
<ErrorReport>prompt</ErrorReport>
3232
<WarningLevel>4</WarningLevel>
33+
</PropertyGroup>
34+
<PropertyGroup>
35+
<SignAssembly>true</SignAssembly>
36+
</PropertyGroup>
37+
<PropertyGroup>
38+
<AssemblyOriginatorKeyFile>..\..\resources\key.snk</AssemblyOriginatorKeyFile>
3339
</PropertyGroup>
3440
<ItemGroup>
35-
<Reference Include="BouncyCastle.Crypto, Version=1.8.6.0, Culture=neutral, PublicKeyToken=0e99375e54769942">
36-
<HintPath>..\..\..\packages\BouncyCastle.1.8.6.1\lib\BouncyCastle.Crypto.dll</HintPath>
37-
</Reference>
3841
<Reference Include="System" />
3942
<Reference Include="System.Core" />
4043
<Reference Include="System.Security.Cryptography.Algorithms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
@@ -73,6 +76,9 @@
7376
<Compile Include="Utils\SecurityUtils.cs" />
7477
</ItemGroup>
7578
<ItemGroup>
79+
<None Include="..\..\resources\key.snk">
80+
<Link>key.snk</Link>
81+
</None>
7682
<None Include="packages.config" />
7783
</ItemGroup>
7884
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />

0 commit comments

Comments
 (0)