|
2 | 2 | // The .NET Foundation licenses this file to you under the MIT license. |
3 | 3 | // See the LICENSE file in the project root for more information. |
4 | 4 |
|
| 5 | +using System.Numerics; |
5 | 6 | using System.Security.Cryptography.Asn1; |
6 | 7 | using Test.Cryptography; |
7 | 8 | using Xunit; |
@@ -415,6 +416,112 @@ public static void GetIntegerBytes() |
415 | 416 | Assert.Equal(Payload, contents.ByteArrayToHex()); |
416 | 417 | } |
417 | 418 |
|
| 419 | + [Theory] |
| 420 | + [InlineData(PublicEncodingRules.BER)] |
| 421 | + [InlineData(PublicEncodingRules.CER)] |
| 422 | + [InlineData(PublicEncodingRules.DER)] |
| 423 | + public static void GetBigInteger(PublicEncodingRules ruleSet) |
| 424 | + { |
| 425 | + byte[] inputData = ( |
| 426 | + "0282010100A46861FA9D5DB763633BF5A64EF6E7C2C2367F48D2D46643A22DFC" + |
| 427 | + "FCCB24E58A14D0F06BDC956437F2A56BA4BEF70BA361BF12964A0D665AFD84B0" + |
| 428 | + "F7494C8FA4ABC5FCA2E017C06178AEF2CDAD1B5F18E997A14B965C074E8F5649" + |
| 429 | + "70607276B00583932240FE6E2DD013026F9AE13D7C91CC07C4E1E8E87737DC06" + |
| 430 | + "EF2B575B89D62EFE46859F8255A123692A706C68122D4DAFE11CB205A7B3DE06" + |
| 431 | + "E553F7B95F978EF8601A8DF819BF32040BDF92A0DE0DF269B4514282E17AC699" + |
| 432 | + "34E8440A48AB9D1F5DF89A502CEF6DFDBE790045BD45E0C94E5CA8ADD76A013E" + |
| 433 | + "9C978440FC8A9E2A9A4940B2460819C3E302AA9C9F355AD754C86D3ED77DDAA3" + |
| 434 | + "DA13810B4D").HexToByteArray(); |
| 435 | + |
| 436 | + BigInteger expected = BigInteger.Parse( |
| 437 | + "2075455505718444046766086325128514549301113944667492053189486607" + |
| 438 | + "5638152321436011512404808361119326026027238444019992518081753153" + |
| 439 | + "5965931647037093368608713442955529617501657176146245891571745113" + |
| 440 | + "4028700771890451167051818999837042261788828826028681595867897235" + |
| 441 | + "7967091503500375497498573022675671178275171110498592645868107163" + |
| 442 | + "8525996766798322809764200941677343791419428587801897366593842552" + |
| 443 | + "7272226864578661449281241619675217353931828233756506947863330597" + |
| 444 | + "8338073826285687331647183058971791153730741973483420110408271570" + |
| 445 | + "1367336140572971505716740825623220507359429297584634909330541150" + |
| 446 | + "79473593821332264673455059897928082590541"); |
| 447 | + |
| 448 | + AsnReader reader = new AsnReader(inputData, (AsnEncodingRules)ruleSet); |
| 449 | + Assert.Equal(expected, reader.GetInteger()); |
| 450 | + } |
| 451 | + |
| 452 | + [Theory] |
| 453 | + [InlineData(PublicEncodingRules.BER)] |
| 454 | + [InlineData(PublicEncodingRules.CER)] |
| 455 | + [InlineData(PublicEncodingRules.DER)] |
| 456 | + public static void GetNegativeBigInteger(PublicEncodingRules ruleSet) |
| 457 | + { |
| 458 | + // This uses a length that doesn't line up with the array pool size so |
| 459 | + // the fill code gets tested on netstandard. |
| 460 | + // It's the same data as above, minus the padding and lead byte (and the |
| 461 | + // next byte changed from 0x68 to 0x88) |
| 462 | + byte[] inputData = ( |
| 463 | + "0281FF8861FA9D5DB763633BF5A64EF6E7C2C2367F48D2D46643A22DFC" + |
| 464 | + "FCCB24E58A14D0F06BDC956437F2A56BA4BEF70BA361BF12964A0D665AFD84B0" + |
| 465 | + "F7494C8FA4ABC5FCA2E017C06178AEF2CDAD1B5F18E997A14B965C074E8F5649" + |
| 466 | + "70607276B00583932240FE6E2DD013026F9AE13D7C91CC07C4E1E8E87737DC06" + |
| 467 | + "EF2B575B89D62EFE46859F8255A123692A706C68122D4DAFE11CB205A7B3DE06" + |
| 468 | + "E553F7B95F978EF8601A8DF819BF32040BDF92A0DE0DF269B4514282E17AC699" + |
| 469 | + "34E8440A48AB9D1F5DF89A502CEF6DFDBE790045BD45E0C94E5CA8ADD76A013E" + |
| 470 | + "9C978440FC8A9E2A9A4940B2460819C3E302AA9C9F355AD754C86D3ED77DDAA3" + |
| 471 | + "DA13810B4D").HexToByteArray(); |
| 472 | + |
| 473 | + BigInteger expected = BigInteger.Parse( |
| 474 | + "-" + |
| 475 | + "5898547409447487884446992857601985651316300515844052200158198046" + |
| 476 | + "7814538020408452501006415149581619776188797413593169277984980446" + |
| 477 | + "1302361382932378450492052337986628823880000831383555853860116718" + |
| 478 | + "5361729331647715885538858385106930514758305144777880150203212976" + |
| 479 | + "6715081632226412951106013360243549075631850526067219857352295397" + |
| 480 | + "2308328327377769665309386917336850273904442596855844458638806936" + |
| 481 | + "6169824439111394938336579524651037239551388910737675470211780509" + |
| 482 | + "8035477769907389338548451561341965157059382875181284370047601682" + |
| 483 | + "6924486017215979427815833587119797658480104671279234402026468966" + |
| 484 | + "86109928634475300812601680679147599027"); |
| 485 | + |
| 486 | + AsnReader reader = new AsnReader(inputData, (AsnEncodingRules)ruleSet); |
| 487 | + Assert.Equal(expected, reader.GetInteger()); |
| 488 | + } |
| 489 | + |
| 490 | + [Theory] |
| 491 | + [InlineData(PublicEncodingRules.BER)] |
| 492 | + [InlineData(PublicEncodingRules.CER)] |
| 493 | + [InlineData(PublicEncodingRules.DER)] |
| 494 | + public static void GetDiminutiveBigInteger(PublicEncodingRules ruleSet) |
| 495 | + { |
| 496 | + // GetBigInteger with the last byte removed. |
| 497 | + // Since it is no longer an ArrayPool alignment size the fill code gets tested on netstandard. |
| 498 | + byte[] inputData = ( |
| 499 | + "0282010000A46861FA9D5DB763633BF5A64EF6E7C2C2367F48D2D46643A22DFC" + |
| 500 | + "FCCB24E58A14D0F06BDC956437F2A56BA4BEF70BA361BF12964A0D665AFD84B0" + |
| 501 | + "F7494C8FA4ABC5FCA2E017C06178AEF2CDAD1B5F18E997A14B965C074E8F5649" + |
| 502 | + "70607276B00583932240FE6E2DD013026F9AE13D7C91CC07C4E1E8E87737DC06" + |
| 503 | + "EF2B575B89D62EFE46859F8255A123692A706C68122D4DAFE11CB205A7B3DE06" + |
| 504 | + "E553F7B95F978EF8601A8DF819BF32040BDF92A0DE0DF269B4514282E17AC699" + |
| 505 | + "34E8440A48AB9D1F5DF89A502CEF6DFDBE790045BD45E0C94E5CA8ADD76A013E" + |
| 506 | + "9C978440FC8A9E2A9A4940B2460819C3E302AA9C9F355AD754C86D3ED77DDAA3" + |
| 507 | + "DA13810B").HexToByteArray(); |
| 508 | + |
| 509 | + BigInteger expected = BigInteger.Parse( |
| 510 | + "2075455505718444046766086325128514549301113944667492053189486607" + |
| 511 | + "5638152321436011512404808361119326026027238444019992518081753153" + |
| 512 | + "5965931647037093368608713442955529617501657176146245891571745113" + |
| 513 | + "4028700771890451167051818999837042261788828826028681595867897235" + |
| 514 | + "7967091503500375497498573022675671178275171110498592645868107163" + |
| 515 | + "8525996766798322809764200941677343791419428587801897366593842552" + |
| 516 | + "7272226864578661449281241619675217353931828233756506947863330597" + |
| 517 | + "8338073826285687331647183058971791153730741973483420110408271570" + |
| 518 | + "1367336140572971505716740825623220507359429297584634909330541150" + |
| 519 | + "79473593821332264673455059897928082590541") >> 8; |
| 520 | + |
| 521 | + AsnReader reader = new AsnReader(inputData, (AsnEncodingRules)ruleSet); |
| 522 | + Assert.Equal(expected, reader.GetInteger()); |
| 523 | + } |
| 524 | + |
418 | 525 | [Theory] |
419 | 526 | [InlineData(PublicEncodingRules.BER)] |
420 | 527 | [InlineData(PublicEncodingRules.CER)] |
|
0 commit comments