dnauck / Portable.Licensing

Portable.Licensing is a cross platform software licensing framework which allows you to implement licensing into your application or library. It provides you all tools to create and validate licenses for your software.
http://dev.nauck-it.de/projects/portable-licensing
MIT License
592 stars 173 forks source link

AssertValidLicense throws exceptions against invalid public keys #22

Open makcakaya opened 9 years ago

makcakaya commented 9 years ago

Following unit tests show AssertValidLicense method throwing exceptions when invalid public keys are given. Types of exceptions change depending on the given public key string.

I can add tests to the project to ensure the method does not throw when given unexpected parameters, if it fits the goals.

Exceptions are commented in the code:

        [Fact]
        public void ShouldNotThrow()
        {
            var passPhrase = "TopSecret";
            var generator = KeyGenerator.Create();
            var pair = generator.GenerateKeyPair();
            var privateKey = pair.ToEncryptedPrivateKeyString(passPhrase);
            var publicKey = pair.ToPublicKeyString();

            var license = License.New().CreateAndSignWithPrivateKey(privateKey, passPhrase);
            var errors = license.Validate().Signature("invalidKey").AssertValidLicense();
            Assert.True(!errors.Any());

            //ShouldNotThrow' failed: System.FormatException : Invalid length for a Base-64 char array or string.
            //    at System.Convert.FromBase64_Decode(Char* startInputPtr, Int32 inputLength, Byte* startDestPtr, Int32 destLength)
            //    at System.Convert.FromBase64CharPtr(Char* inputPtr, Int32 inputLength)
            //    at System.Convert.FromBase64String(String s)
            //    at Portable.Licensing.License.VerifySignature(String publicKey)
            //    at Portable.Licensing.Validation.LicenseValidationExtensions.<>c__DisplayClassd.<Signature>b__c(License license)
            //    at Portable.Licensing.Validation.ValidationChainBuilder.<AssertValidLicense>d__1.MoveNext()
            //    at System.Linq.Enumerable.Any[TSource](IEnumerable`1 source)
            //    LicenseClientTests.cs(96,0): at Biltera.Licensing.Client.Tests.LicenseClientTests.ShouldNotThrow()

            //0 passed, 1 failed, 0 skipped, took 1,11 seconds (xUnit.net 2.0.0 build 2929).
        }

        [Fact]
        public void ShouldNotThrow2()
        {
            var passPhrase = "TopSecret";
            var generator = KeyGenerator.Create();
            var pair = generator.GenerateKeyPair();
            var privateKey = pair.ToEncryptedPrivateKeyString(passPhrase);
            var publicKey = pair.ToPublicKeyString();

            var license = License.New().CreateAndSignWithPrivateKey(privateKey, passPhrase);
            var errors = license.Validate().Signature("invalidPublicKey").AssertValidLicense();
            Assert.True(!errors.Any());

            //            ShouldNotThrow2' failed: System.IO.EndOfStreamException : DEF length 123 object truncated by 113
            //    at Org.BouncyCastle.Asn1.DefiniteLengthInputStream.ToArray()
            //    at Org.BouncyCastle.Asn1.Asn1StreamParser.ReadTaggedObject(Boolean constructed, Int32 tag)
            //    at Org.BouncyCastle.Asn1.Asn1InputStream.BuildObject(Int32 tag, Int32 tagNo, Int32 length)
            //    at Org.BouncyCastle.Asn1.Asn1InputStream.ReadObject()
            //    at Org.BouncyCastle.Asn1.Asn1Object.FromByteArray(Byte[] data)
            //    at Org.BouncyCastle.Security.PublicKeyFactory.CreateKey(Byte[] keyInfoData)
            //    at Portable.Licensing.License.VerifySignature(String publicKey)
            //    at Portable.Licensing.Validation.LicenseValidationExtensions.<>c__DisplayClassd.<Signature>b__c(License license)
            //    at Portable.Licensing.Validation.ValidationChainBuilder.<AssertValidLicense>d__1.MoveNext()
            //    at System.Linq.Enumerable.Any[TSource](IEnumerable`1 source)
            //    LicenseClientTests.cs(122,0): at Biltera.Licensing.Client.Tests.LicenseClientTests.ShouldNotThrow2()

            //0 passed, 1 failed, 0 skipped, took 1,16 seconds (xUnit.net 2.0.0 build 2929).
        }
dnauck commented 9 years ago

Related to #21

We should just return false on invalid input.