Usa AsnReader.ThrowIfNotEmpty instead of explicit checks.
authorFilip Navara <filip.navara@gmail.com>
Wed, 25 Jul 2018 22:26:03 +0000 (00:26 +0200)
committerJeremy Barton <jbarton@microsoft.com>
Wed, 25 Jul 2018 22:26:03 +0000 (15:26 -0700)
Commit migrated from https://github.com/dotnet/corefx/commit/90aed6e851d7ad2d7374b294cca601dbaeb01083

src/libraries/System.Security.Cryptography.Pkcs/src/Internal/Cryptography/Pal/AnyOS/ManagedPal.Asn.cs
src/libraries/System.Security.Cryptography.Pkcs/src/System/Security/Cryptography/Pkcs/SignerInfo.cs

index a02c2b1..3031ec5 100644 (file)
@@ -62,10 +62,7 @@ namespace Internal.Cryptography.Pal.AnyOS
                     data = contents.Span;
                 }
 
-                if (reader.HasData)
-                {
-                    throw new CryptographicException(SR.Cryptography_Der_Invalid_Encoding);
-                }
+                reader.ThrowIfNotEmpty();
 
                 return data.ToArray();
             }
@@ -114,12 +111,7 @@ namespace Internal.Cryptography.Pal.AnyOS
             // Read using BER because the CMS specification says the encoding is BER.
             AsnReader reader = new AsnReader(encodedUtcTime, AsnEncodingRules.BER);
             DateTimeOffset value = reader.GetUtcTime();
-
-            if (reader.HasData)
-            {
-                throw new CryptographicException(SR.Cryptography_Der_Invalid_Encoding);
-            }
-
+            reader.ThrowIfNotEmpty();
             return value.UtcDateTime;
         }
 
@@ -134,12 +126,7 @@ namespace Internal.Cryptography.Pal.AnyOS
             // Read using BER because the CMS specification says the encoding is BER.
             AsnReader reader = new AsnReader(encodedOid, AsnEncodingRules.BER);
             string value = reader.ReadObjectIdentifierAsString();
-
-            if (reader.HasData)
-            {
-                throw new CryptographicException(SR.Cryptography_Der_Invalid_Encoding);
-            }
-
+            reader.ThrowIfNotEmpty();
             return value;
         }
 
index ff75374..98456bd 100644 (file)
@@ -178,10 +178,7 @@ namespace System.Security.Cryptography.Pkcs
                     AsnReader reader = new AsnReader(modifiedAttr.AttrValues, AsnEncodingRules.BER);
                     AsnReader collReader = reader.ReadSetOf();
 
-                    if (reader.HasData)
-                    {
-                        throw new CryptographicException(SR.Cryptography_Der_Invalid_Encoding);
-                    }
+                    reader.ThrowIfNotEmpty();
 
                     // re-add old values
                     while (collReader.HasData)
@@ -245,10 +242,7 @@ namespace System.Security.Cryptography.Pkcs
                     AsnReader reader = new AsnReader(attributeAsn.AttrValues, AsnEncodingRules.BER);
                     AsnReader collReader = reader.ReadSetOf();
 
-                    if (reader.HasData)
-                    {
-                        throw new CryptographicException(SR.Cryptography_Der_Invalid_Encoding);
-                    }
+                    reader.ThrowIfNotEmpty();
 
                     while (collReader.HasData)
                     {
@@ -377,10 +371,7 @@ namespace System.Security.Cryptography.Pkcs
                     AsnReader reader = new AsnReader(attributeAsn.AttrValues, AsnEncodingRules.BER);
                     AsnReader collReader = reader.ReadSetOf();
 
-                    if (reader.HasData)
-                    {
-                        throw new CryptographicException(SR.Cryptography_Der_Invalid_Encoding);
-                    }
+                    reader.ThrowIfNotEmpty();
 
                     int j = 0;
 
@@ -822,10 +813,7 @@ namespace System.Security.Cryptography.Pkcs
             AsnReader reader = new AsnReader(attrSetBytes, AsnEncodingRules.BER);
             AsnReader collReader = reader.ReadSetOf();
 
-            if (reader.HasData)
-            {
-                throw new CryptographicException(SR.Cryptography_Der_Invalid_Encoding);
-            }
+            reader.ThrowIfNotEmpty();
 
             AsnEncodedDataCollection valueColl = new AsnEncodedDataCollection();
 
@@ -856,10 +844,7 @@ namespace System.Security.Cryptography.Pkcs
             AsnReader reader = new AsnReader(attributeValues, AsnEncodingRules.BER);
             AsnReader collReader = reader.ReadSetOf();
 
-            if (reader.HasData)
-            {
-                throw new CryptographicException(SR.Cryptography_Der_Invalid_Encoding);
-            }
+            reader.ThrowIfNotEmpty();
 
             for (int i = 0; collReader.HasData; i++)
             {
@@ -907,10 +892,7 @@ namespace System.Security.Cryptography.Pkcs
                 AsnReader reader = new AsnReader(modifiedAttr.AttrValues, writer.RuleSet);
                 AsnReader collReader = reader.ReadSetOf();
 
-                if (reader.HasData)
-                {
-                    throw new CryptographicException(SR.Cryptography_Der_Invalid_Encoding);
-                }
+                reader.ThrowIfNotEmpty();
 
                 int i = 0;