summary refs log tree commit diff
path: root/crypto/test/src/asn1/test/ASN1IntegerTest.cs
blob: 10570c8008637292f2518a97c3cdd9f2599ab912 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
using System;

using NUnit.Framework;

using Org.BouncyCastle.Math;
using Org.BouncyCastle.Utilities;
using Org.BouncyCastle.Utilities.Encoders;
using Org.BouncyCastle.Utilities.Test;

namespace Org.BouncyCastle.Asn1.Tests
{
    [TestFixture]
    public class Asn1IntegerTest
        : SimpleTest
    {
        private static readonly byte[] suspectKey = Base64.Decode(
            "MIGJAoGBAHNc+iExm94LUrJdPSJ4QJ9tDRuvaNmGVHpJ4X7a5zKI02v+2E7RotuiR2MHDJfVJkb9LUs2kb3XBlyENhtMLsbeH+3Muy3" +
            "hGDlh/mLJSh1s4c5jDKBRYOHom7Uc8wP0P2+zBCA+OEdikNDFBaP5PbR2Xq9okG2kPh35M2quAiMTAgMBAAE=");

        public override string Name
        {
            get { return "Asn1Integer"; }
        }

        public override void PerformTest()
        {
            SetAllowUnsafeProperty(true);

            Asn1Sequence.GetInstance(suspectKey);

            DoTestValidEncodingSingleByte();
            DoTestValidEncodingMultiByte();
            DoTestInvalidEncoding_00();
            DoTestInvalidEncoding_ff();
            DoTestInvalidEncoding_00_32bits();
            DoTestInvalidEncoding_ff_32bits();
            //DoTestLooseInvalidValidEncoding_FF_32B();
            //DoTestLooseInvalidValidEncoding_zero_32B();
            DoTestLooseValidEncoding_zero_32BAligned();
            DoTestLooseValidEncoding_FF_32BAligned();
            DoTestLooseValidEncoding_FF_32BAligned_1not0();
            DoTestLooseValidEncoding_FF_32BAligned_2not0();
            DoTestOversizedEncoding();

            SetAllowUnsafeProperty(true);

            new DerInteger(Hex.Decode("ffda47bfc776bcd269da4832626ac332adfca6dd835e8ecd83cd1ebe7d709b0e"));

            new DerEnumerated(Hex.Decode("005a47bfc776bcd269da4832626ac332adfca6dd835e8ecd83cd1ebe7d709b0e"));

            SetAllowUnsafeProperty(false);

            try
            {
                new DerInteger(Hex.Decode("ffda47bfc776bcd269da4832626ac332adfca6dd835e8ecd83cd1ebe7d709b"));

                Fail("no exception");
            }
            catch (ArgumentException e)
            {
                CheckArgumentException(e, "malformed integer");
            }

            // No support for thread-local override in C# version
            //IsTrue(!Properties.SetThreadOverride("org.bouncycastle.asn1.allow_unsafe_integer", true));

            //new DerInteger(Hex.Decode("ffda47bfc776bcd269da4832626ac332adfca6dd835e8ecd83cd1ebe7d709b"));

            //IsTrue(Properties.RemoveThreadOverride("org.bouncycastle.asn1.allow_unsafe_integer"));

            try
            {
                Asn1Sequence.GetInstance(suspectKey);

                Fail("no exception");
            }
            catch (ArgumentException e)
            {
                CheckArgumentException("test 1", e, "failed to construct sequence from byte[]: corrupted stream detected");
            }

            try
            {
                new DerInteger(Hex.Decode("ffda47bfc776bcd269da4832626ac332adfca6dd835e8ecd83cd1ebe7d709b0e"));

                Fail("no exception");
            }
            catch (ArgumentException e)
            {
                CheckArgumentException(e, "malformed integer");
            }

            try
            {
                new DerEnumerated(Hex.Decode("ffda47bfc776bcd269da4832626ac332adfca6dd835e8ecd83cd1ebe7d709b0e"));

                Fail("no exception");
            }
            catch (ArgumentException e)
            {
                CheckArgumentException(e, "malformed enumerated");
            }

            try
            {
                new DerEnumerated(Hex.Decode("005a47bfc776bcd269da4832626ac332adfca6dd835e8ecd83cd1ebe7d709b0e"));

                Fail("no exception");
            }
            catch (ArgumentException e)
            {
                CheckArgumentException(e, "malformed enumerated");
            }
        }

        /**
         * Ensure existing single byte behavior.
         */
        public void DoTestValidEncodingSingleByte()
        {
            SetAllowUnsafeProperty(false);

            //
            // Without property, single byte.
            //
            byte[] rawInt = Hex.Decode("10");
            DerInteger i = new DerInteger(rawInt);
            CheckIntValue(i, 16);

            //
            // With property set.
            //
            SetAllowUnsafeProperty(true);

            rawInt = Hex.Decode("10");
            i = new DerInteger(rawInt);
            CheckIntValue(i, 16);
        }

        public void DoTestValidEncodingMultiByte()
        {
            SetAllowUnsafeProperty(false);

            //
            // Without property, multi-byte.
            //
            byte[] rawInt = Hex.Decode("10FF");
            DerInteger i = new DerInteger(rawInt);
            CheckIntValue(i, 4351);

            //
            // With property set.
            //
            SetAllowUnsafeProperty(true);

            rawInt = Hex.Decode("10FF");
            i = new DerInteger(rawInt);
            CheckIntValue(i, 4351);
        }

        public void DoTestInvalidEncoding_00()
        {
            SetAllowUnsafeProperty(false);
            try
            {
                byte[] rawInt = Hex.Decode("0010FF");
                DerInteger i = new DerInteger(rawInt);
                IsEquals(i.Value.IntValue, 4351);
                Fail("Expecting illegal argument exception.");
            }
            catch (ArgumentException e)
            {
                CheckArgumentException(e, "malformed integer");
            }
        }

        public void DoTestInvalidEncoding_ff()
        {
            SetAllowUnsafeProperty(false);

            try
            {
                byte[] rawInt = Hex.Decode("FF81FF");
                new DerInteger(rawInt);
                Fail("Expecting illegal argument exception.");
            }
            catch (ArgumentException e)
            {
                CheckArgumentException(e, "malformed integer");
            }
        }

        public void DoTestInvalidEncoding_00_32bits()
        {
            SetAllowUnsafeProperty(false);

            //
            // Check what would pass loose validation fails outside of loose validation.
            //
            try
            {
                byte[] rawInt = Hex.Decode("0000000010FF");
                new DerInteger(rawInt);
                Fail("Expecting illegal argument exception.");
            }
            catch (ArgumentException e)
            {
                CheckArgumentException(e, "malformed integer");
            }
        }

        public void DoTestInvalidEncoding_ff_32bits()
        {
            SetAllowUnsafeProperty(false);

            //
            // Check what would pass loose validation fails outside of loose validation.
            //
            try
            {
                byte[] rawInt = Hex.Decode("FFFFFFFF01FF");
                new DerInteger(rawInt);
                Fail("Expecting illegal argument exception.");
            }
            catch (ArgumentException e)
            {
                CheckArgumentException(e, "malformed integer");
            }
        }

        /*
         Unfortunately it turns out that integers stored without sign bits that are assumed to be
         unsigned.. this means a string of FF may occur and then the user will call getPositiveValue().
         Sigh..
        public void DoTestLooseInvalidValidEncoding_zero_32B()
            throws Exception
        {
            //
            // Should still fail as loose validation only permits 3 leading 0x00 bytes.
            //
            try
            {
                SetAllowUnsafeProperty(true);
                byte[] rawInt = Hex.Decode("0000000010FF");
                new DerInteger(rawInt);
                Fail("Expecting illegal argument exception.");
            }
            catch (ArgumentException e)
            {
                CheckArgumentException(e, "malformed integer");
            }
        }

        public void DoTestLooseInvalidValidEncoding_FF_32B()
            throws Exception
        {
            //
            // Should still fail as loose validation only permits 3 leading 0xFF bytes.
            //
            try
            {
                SetAllowUnsafeProperty(true);
                byte[] rawInt = Hex.Decode("FFFFFFFF10FF");
                new DerInteger(rawInt);
                Fail("Expecting illegal argument exception.");
            }
            catch (ArgumentException e)
            {
                CheckArgumentException(e, "malformed integer");
            }
        }
        */

        public void DoTestLooseValidEncoding_zero_32BAligned()
        {
            //
            // Should pass as loose validation permits 3 leading 0x00 bytes.
            //
            SetAllowUnsafeProperty(true);
            byte[] rawInt = Hex.Decode("00000010FF000000");
            DerInteger i = new DerInteger(rawInt);
            CheckLongValue(i, 72997666816L);
        }

        public void DoTestLooseValidEncoding_FF_32BAligned()
        {
            //
            // Should pass as loose validation permits 3 leading 0xFF bytes
            //
            SetAllowUnsafeProperty(true);
            byte[] rawInt = Hex.Decode("FFFFFF10FF000000");
            DerInteger i = new DerInteger(rawInt);
            CheckLongValue(i, -1026513960960L);
        }

        public void DoTestLooseValidEncoding_FF_32BAligned_1not0()
        {
            //
            // Should pass as loose validation permits 3 leading 0xFF bytes.
            //
            SetAllowUnsafeProperty(true);
            byte[] rawInt = Hex.Decode("FFFEFF10FF000000");
            DerInteger i = new DerInteger(rawInt);
            CheckLongValue(i, -282501490671616L);
        }

        public void DoTestLooseValidEncoding_FF_32BAligned_2not0()
        {
            //
            // Should pass as loose validation permits 3 leading 0xFF bytes.
            //
            SetAllowUnsafeProperty(true);
            byte[] rawInt = Hex.Decode("FFFFFE10FF000000");
            DerInteger i = new DerInteger(rawInt);
            CheckLongValue(i, -2126025588736L);
        }

        public void DoTestOversizedEncoding()
        {
            //
            // Should pass as loose validation permits 3 leading 0xFF bytes.
            //
            SetAllowUnsafeProperty(true);
            byte[] rawInt = Hex.Decode("FFFFFFFE10FF000000000000");
            DerInteger i = new DerInteger(rawInt);
            IsEquals(new BigInteger(Hex.Decode("FFFFFFFE10FF000000000000")), i.Value);

            rawInt = Hex.Decode("FFFFFFFFFE10FF000000000000");
            try
            {
                new DerInteger(rawInt);
            }
            catch (ArgumentException e)
            {
                CheckArgumentException(e, "malformed integer");
            }
        }
        private void CheckArgumentException(ArgumentException e, string expectedMessage)
        {
            IsTrue(e.Message.StartsWith(expectedMessage));
        }

        private void CheckArgumentException(string errorText, ArgumentException e, string expectedMessage)
        {
            IsTrue(errorText, e.Message.StartsWith(expectedMessage));
        }

        private void SetAllowUnsafeProperty(bool allowUnsafe)
        {
            Environment.SetEnvironmentVariable(DerInteger.AllowUnsafeProperty, allowUnsafe ? "true" : "false");
        }

        private void CheckIntValue(DerInteger i, int n)
        {
            BigInteger val = i.Value;
            IsEquals(val.IntValue, n);
            IsEquals(val.IntValueExact, n);
            IsEquals(i.IntValueExact, n);
            IsTrue(i.HasValue(n));
        }

        private void CheckLongValue(DerInteger i, long n)
        {
            BigInteger val = i.Value;
            IsEquals(val.LongValue, n);
            IsEquals(val.LongValueExact, n);
            IsEquals(i.LongValueExact, n);
            IsTrue(i.HasValue(n));
        }

        [Test]
        public void TestFunction()
        {
            string resultText = Perform().ToString();

            Assert.AreEqual(Name + ": Okay", resultText);
        }
    }
}