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

add support for .net 3.5 #4

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ obj/
_ReSharper*/
[Tt]est[Rr]esult*
package/
packages/
*.nupkg
6 changes: 4 additions & 2 deletions JWT.sln
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@

Microsoft Visual Studio Solution File, Format Version 11.00
# Visual Studio 2010
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2013
VisualStudioVersion = 12.0.21005.1
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JWT", "JWT\JWT.csproj", "{A80B51B8-DDF6-4026-98A4-B59653E50B38}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnitTests", "UnitTests\UnitTests.csproj", "{2484B30F-97B4-4C2A-802D-07EE1F330C80}"
Expand Down
89 changes: 89 additions & 0 deletions JWT/JWT35.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>8.0.30703</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{7B6B0007-BFB7-4B7D-83A8-53D199DCA02D}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Jose</RootNamespace>
<AssemblyName>jose-jwt-35</AssemblyName>
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<TargetFrameworkProfile>Client</TargetFrameworkProfile>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>TRACE;DEBUG;NET35</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE;NET35</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Security" />
</ItemGroup>
<ItemGroup>
<Compile Include="crypto\AesGcm.cs" />
<Compile Include="crypto\AesKeyWrap.cs" />
<Compile Include="crypto\RsaOaep.cs" />
<Compile Include="jwe\IJweAlgorithm.cs" />
<Compile Include="util\Arrays.cs" />
<Compile Include="util\Base64Url.cs" />
<Compile Include="util\Compact.cs" />
<Compile Include="crypto\ConcatKDF.cs" />
<Compile Include="jwa\AesGcmKeyWrapManagement.cs" />
<Compile Include="jwa\AesKeyWrapManagement.cs" />
<Compile Include="jwa\EcdhKeyManagement.cs" />
<Compile Include="jwa\EcdhKeyManagementWithAesKeyWrap.cs" />
<Compile Include="jwa\Pbse2HmacShaKeyManagementWithAesKeyWrap.cs" />
<Compile Include="native\BCrypt.cs" />
<Compile Include="native\NCrypt.cs" />
<Compile Include="crypto\PBKDF2.cs" />
<Compile Include="crypto\RsaPss.cs" />
<Compile Include="Security\Cryptography\EccKey.cs" />
<Compile Include="util\Dictionaries.cs" />
<Compile Include="util\Ensure.cs" />
<Compile Include="json\IJsonMapper.cs" />
<Compile Include="json\JSSerializerMapper.cs" />
<Compile Include="jwe\AesCbcHmacEncryption.cs" />
<Compile Include="jwe\AesGcmEncryption.cs" />
<Compile Include="compression\DeflateCompression.cs" />
<Compile Include="jwa\DirectKeyManagement.cs" />
<Compile Include="compression\ICompression.cs" />
<Compile Include="jwa\IKeyManagement.cs" />
<Compile Include="jwa\RsaKeyManagement.cs" />
<Compile Include="jws\EcdsaUsingSha.cs" />
<Compile Include="jws\HmacUsingSha.cs" />
<Compile Include="jws\IJwsAlgorithm.cs" />
<Compile Include="jws\Plaintext.cs" />
<Compile Include="jws\RsaPssUsingSha.cs" />
<Compile Include="jws\RsaUsingSha.cs" />
<Compile Include="JWT.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Security\Cryptography\RsaKey.cs" />
<Compile Include="util\Extensions.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
18 changes: 18 additions & 0 deletions JWT/json/JSSerializerMapper.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,26 @@
using System;
using System.IO;
using System.Linq;
#if !NET35
using System.Web.Script.Serialization;
#endif
using System.Text;

namespace Jose
{
public class JSSerializerMapper:IJsonMapper
{
#if NET35
public string Serialize(object obj)
{
throw new Exception("Must implement IJsonMapper");
}

public T Parse<T>(string json)
{
throw new Exception("Must implement IJsonMapper");
}
#else
private static JavaScriptSerializer js;

private JavaScriptSerializer JS
Expand All @@ -20,5 +37,6 @@ public T Parse<T>(string json)
{
return JS.Deserialize<T>(json);
}
#endif
}
}
6 changes: 5 additions & 1 deletion JWT/jwa/Pbse2HmacShaKeyManagementWithAesKeyWrap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ public byte[] Unwrap(byte[] encryptedCek, object key, int cekSizeBits, IDictiona
Ensure.Contains(header, new[] { "p2s" }, "Pbse2HmacShaKeyManagementWithAesKeyWrap algorithm expects 'p2s' param in JWT header, but was not found");

byte[] algId=Encoding.UTF8.GetBytes((string) header["alg"]);
int iterationCount=(int) header["p2c"];
#if NET35
int iterationCount = (int)(Int64)header["p2c"];
#else
int iterationCount = (int)header["p2c"];
#endif
byte[] saltInput = Base64Url.Decode((string) header["p2s"]);

byte[] salt = Arrays.Concat(algId, Arrays.Zero, saltInput);
Expand Down
6 changes: 5 additions & 1 deletion JWT/native/NCrypt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ public NCryptBufferDesc(params NCryptBuffer[] buffers)

foreach (var buf in buffers)
{
Marshal.StructureToPtr(buf, pBuffers+totalSizeBytes, false);
#if NET35
Marshal.StructureToPtr(buf, new IntPtr(pBuffers.ToInt32() + totalSizeBytes), false);
#else
Marshal.StructureToPtr(buf, pBuffers + totalSizeBytes, false);
#endif
totalSizeBytes += Marshal.SizeOf(buf);
}
}
Expand Down
7 changes: 6 additions & 1 deletion JWT/util/Ensure.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,13 @@ public static void BitSize(int actualSize, int expectedSize, string msg)

public static void IsNotEmpty(string arg, string msg, params object[] args)
{
if(string.IsNullOrWhiteSpace(arg))
#if NET35
if (String.IsNullOrEmpty(arg) || arg.Trim().Length == 0)
throw new ArgumentException(msg);
#else
if (string.IsNullOrWhiteSpace(arg))
throw new ArgumentException(msg);
#endif
}

public static void Divisible(int arg, int divisor, string msg, params object[] args)
Expand Down
23 changes: 23 additions & 0 deletions JWT/util/Extensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace Jose
{
#if NET35
public static class Extensions
{
public static void CopyTo(this Stream input, Stream output)
{
var buffer = new byte[1024];
int read;
while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
{
output.Write(buffer, 0, read);
}
}
}
#endif
}
31 changes: 31 additions & 0 deletions JWT35.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2013
VisualStudioVersion = 12.0.21005.1
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JWT35", "JWT\JWT35.csproj", "{7B6B0007-BFB7-4B7D-83A8-53D199DCA02D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnitTests35", "UnitTests\UnitTests35.csproj", "{2484B30F-97B4-4C2A-802D-07EE1F330C80}"
ProjectSection(ProjectDependencies) = postProject
{7B6B0007-BFB7-4B7D-83A8-53D199DCA02D} = {7B6B0007-BFB7-4B7D-83A8-53D199DCA02D}
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{7B6B0007-BFB7-4B7D-83A8-53D199DCA02D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7B6B0007-BFB7-4B7D-83A8-53D199DCA02D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7B6B0007-BFB7-4B7D-83A8-53D199DCA02D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7B6B0007-BFB7-4B7D-83A8-53D199DCA02D}.Release|Any CPU.Build.0 = Release|Any CPU
{2484B30F-97B4-4C2A-802D-07EE1F330C80}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2484B30F-97B4-4C2A-802D-07EE1F330C80}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2484B30F-97B4-4C2A-802D-07EE1F330C80}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2484B30F-97B4-4C2A-802D-07EE1F330C80}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
43 changes: 43 additions & 0 deletions UnitTests/JSSerializeMapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System;
using System.Linq;
using Jose;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace UnitTests
{
class JSSerializeMapper : IJsonMapper
{
public string Serialize(object obj)
{
return JsonConvert.SerializeObject(obj);
}

public T Parse<T>(string json)
{
return DeserializeRecursive<T>(json);
}

private static T DeserializeRecursive<T>(string json)
{
return (T)ToObject(JToken.Parse(json));
}

private static object ToObject(JToken token)
{
if (token.Type == JTokenType.Object)
{
return ((JObject) token).Properties().ToDictionary(prop => prop.Name, prop => ToObject(prop.Value));
}
else if (token.Type == JTokenType.Array)
{
return token.Values().Select(ToObject).ToList();
}
else
{
return ((JValue)token).Value;
}
}

}
}
5 changes: 4 additions & 1 deletion UnitTests/TestSuite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ public class TestSuite
private byte[] aes512Key = new byte[] { 238, 71, 183, 66, 57, 207, 194, 93, 82, 80, 80, 152, 92, 242, 84, 206, 194, 46, 67, 43, 231, 118, 208, 168, 156, 212, 33, 105, 27, 45, 60, 160, 232, 63, 61, 235, 68, 171, 206, 35, 152, 11, 142, 121, 174, 165, 140, 11, 172, 212, 13, 101, 13, 190, 82, 244, 109, 113, 70, 150, 251, 82, 215, 226 };

[SetUp]
public void SetUp() {}
public void SetUp()
{
JWT.JsonMapper = new JSSerializeMapper();
}

[Test]
public void DecodePlaintext()
Expand Down
83 changes: 83 additions & 0 deletions UnitTests/UnitTests35.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>8.0.30703</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{2484B30F-97B4-4C2A-802D-07EE1F330C80}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>UnitTests</RootNamespace>
<AssemblyName>UnitTests-35</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\Newtonsoft.Json.6.0.6\lib\net40\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="nunit.framework">
<HintPath>lib\nunit.framework.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Numerics" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="AesGcmTest.cs" />
<Compile Include="AesKeyWrapTest.cs" />
<Compile Include="ArraysTest.cs" />
<Compile Include="Base64UrlTest.cs" />
<Compile Include="CompactTest.cs" />
<Compile Include="ConcatKDFTest.cs" />
<Compile Include="DictionariesTest.cs" />
<Compile Include="JSSerializeMapper.cs" />
<Compile Include="PBKDF2Test.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="TestSuite.cs" />
</ItemGroup>
<ItemGroup>
<None Include="jwt-2048.p12">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\JWT\JWT35.csproj">
<Project>{7b6b0007-bfb7-4b7d-83a8-53d199dca02d}</Project>
<Name>JWT35</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
4 changes: 4 additions & 0 deletions UnitTests/packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Newtonsoft.Json" version="6.0.6" targetFramework="net40" />
</packages>