Skip to content

Commit a3ffd09

Browse files
committed
Env. prop.: Org.BouncyCastle.Asn1.AllowUnsafeInteger
- set to "true" to weaken ASN.1 INTEGER checks - see #156
1 parent c6f976f commit a3ffd09

File tree

8 files changed

+387
-20
lines changed

8 files changed

+387
-20
lines changed

crypto/NBuild.build

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,21 +43,21 @@
4343
<property name="switch" value="-" />
4444
</target>
4545
<target name="set-mono-2.0-framework-props">
46-
<property name="compile-defines" value="NET_1_1" />
46+
<property name="compile-defines" value="NET_2_0" />
4747
<property name="debug-extension" value="dll.mdb" />
4848
<property name="enable-nostdlib" value="false" />
4949
<property name="nunit-console" value="nunit-console" />
5050
<property name="switch" value="-" />
5151
</target>
5252
<target name="set-mono-3.5-framework-props">
53-
<property name="compile-defines" value="NET_1_1" />
53+
<property name="compile-defines" value="NET_2_0" />
5454
<property name="debug-extension" value="dll.mdb" />
5555
<property name="enable-nostdlib" value="false" />
5656
<property name="nunit-console" value="nunit-console" />
5757
<property name="switch" value="-" />
5858
</target>
5959
<target name="set-mono-4.0-framework-props">
60-
<property name="compile-defines" value="NET_1_1" />
60+
<property name="compile-defines" value="NET_2_0" />
6161
<property name="debug-extension" value="dll.mdb" />
6262
<property name="enable-nostdlib" value="false" />
6363
<property name="nunit-console" value="nunit-console" />
@@ -71,21 +71,21 @@
7171
<property name="switch" value="/" />
7272
</target>
7373
<target name="set-net-2.0-framework-props">
74-
<property name="compile-defines" value="NET_1_1" />
74+
<property name="compile-defines" value="NET_2_0" />
7575
<property name="debug-extension" value="pdb" />
7676
<property name="enable-nostdlib" value="true" />
7777
<property name="nunit-console" value="nunit-console.exe" />
7878
<property name="switch" value="/" />
7979
</target>
8080
<target name="set-net-3.5-framework-props">
81-
<property name="compile-defines" value="NET_1_1" />
81+
<property name="compile-defines" value="NET_2_0" />
8282
<property name="debug-extension" value="pdb" />
8383
<property name="enable-nostdlib" value="true" />
8484
<property name="nunit-console" value="nunit-console.exe" />
8585
<property name="switch" value="/" />
8686
</target>
8787
<target name="set-net-4.0-framework-props">
88-
<property name="compile-defines" value="NET_1_1" />
88+
<property name="compile-defines" value="NET_2_0" />
8989
<property name="debug-extension" value="pdb" />
9090
<property name="enable-nostdlib" value="true" />
9191
<property name="nunit-console" value="nunit-console.exe" />

crypto/crypto.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11194,6 +11194,11 @@
1119411194
SubType = "Code"
1119511195
BuildAction = "Compile"
1119611196
/>
11197+
<File
11198+
RelPath = "test\src\asn1\test\ASN1IntegerTest.cs"
11199+
SubType = "Code"
11200+
BuildAction = "Compile"
11201+
/>
1119711202
<File
1119811203
RelPath = "test\src\asn1\test\ASN1SequenceParserTest.cs"
1119911204
SubType = "Code"

crypto/src/asn1/DerEnumerated.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,19 +62,18 @@ public DerEnumerated(
6262
}
6363

6464
public DerEnumerated(
65-
byte[] bytes)
65+
byte[] bytes)
6666
{
6767
if (bytes.Length > 1)
6868
{
69-
if (bytes[0] == 0 && (bytes[1] & 0x80) == 0)
69+
if ((bytes[0] == 0 && (bytes[1] & 0x80) == 0)
70+
|| (bytes[0] == (byte)0xff && (bytes[1] & 0x80) != 0))
7071
{
71-
throw new ArgumentException("malformed enumerated");
72-
}
73-
if (bytes[0] == (byte)0xff && (bytes[1] & 0x80) != 0)
74-
{
75-
throw new ArgumentException("malformed enumerated");
72+
if (!DerInteger.AllowUnsafe())
73+
throw new ArgumentException("malformed enumerated");
7674
}
7775
}
76+
7877
this.bytes = Arrays.Clone(bytes);
7978
}
8079

crypto/src/asn1/DerInteger.cs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@ namespace Org.BouncyCastle.Asn1
88
public class DerInteger
99
: Asn1Object
1010
{
11+
public const string AllowUnsafeProperty = "Org.BouncyCastle.Asn1.AllowUnsafeInteger";
12+
13+
internal static bool AllowUnsafe()
14+
{
15+
string allowUnsafeValue = Platform.GetEnvironmentVariable(AllowUnsafeProperty);
16+
return allowUnsafeValue != null && Platform.EqualsIgnoreCase("true", allowUnsafeValue);
17+
}
18+
1119
private readonly byte[] bytes;
1220

1321
/**
@@ -72,13 +80,11 @@ public DerInteger(
7280
{
7381
if (bytes.Length > 1)
7482
{
75-
if (bytes[0] == 0 && (bytes[1] & 0x80) == 0)
76-
{
77-
throw new ArgumentException("malformed integer");
78-
}
79-
if (bytes[0] == (byte)0xff && (bytes[1] & 0x80) != 0)
83+
if ((bytes[0] == 0 && (bytes[1] & 0x80) == 0)
84+
|| (bytes[0] == (byte)0xff && (bytes[1] & 0x80) != 0))
8085
{
81-
throw new ArgumentException("malformed integer");
86+
if (!AllowUnsafe())
87+
throw new ArgumentException("malformed integer");
8288
}
8389
}
8490
this.bytes = Arrays.Clone(bytes);

crypto/src/util/Platform.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ internal static bool EqualsIgnoreCase(string a, string b)
4141
#endif
4242
}
4343

44-
#if NETCF_1_0 || NETCF_2_0 || SILVERLIGHT || PORTABLE
44+
#if NETCF_1_0 || NETCF_2_0 || SILVERLIGHT || (PORTABLE && !DOTNET)
4545
internal static string GetEnvironmentVariable(
4646
string variable)
4747
{

crypto/test/UnitTests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
</ProjectReference>
5252
</ItemGroup>
5353
<ItemGroup>
54+
<Compile Include="src\asn1\test\ASN1IntegerTest.cs" />
5455
<Compile Include="src\asn1\test\ASN1SequenceParserTest.cs" />
5556
<Compile Include="src\asn1\test\ASN1UnitTest.cs" />
5657
<Compile Include="src\asn1\test\AdditionalInformationSyntaxUnitTest.cs" />

0 commit comments

Comments
 (0)