|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using Test.Cryptography; |
| 5 | +using Xunit; |
| 6 | + |
| 7 | +namespace System.Formats.Asn1.Tests.Decoder |
| 8 | +{ |
| 9 | + public sealed class ReadEncodedValueTests |
| 10 | + { |
| 11 | + [Theory] |
| 12 | + [InlineData(AsnEncodingRules.BER)] |
| 13 | + [InlineData(AsnEncodingRules.CER)] |
| 14 | + [InlineData(AsnEncodingRules.DER)] |
| 15 | + public static void ReadEncodedValue_Primitive(AsnEncodingRules ruleSet) |
| 16 | + { |
| 17 | + // OCTET STRING (6 content bytes) |
| 18 | + // NULL |
| 19 | + ReadOnlySpan<byte> data = |
| 20 | + [ |
| 21 | + 0x04, 0x06, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, |
| 22 | + 0x05, 0x00, |
| 23 | + ]; |
| 24 | + |
| 25 | + ExpectSuccess(data, ruleSet, Asn1Tag.PrimitiveOctetString, 2, 6); |
| 26 | + } |
| 27 | + |
| 28 | + [Theory] |
| 29 | + [InlineData(AsnEncodingRules.BER)] |
| 30 | + [InlineData(AsnEncodingRules.CER)] |
| 31 | + [InlineData(AsnEncodingRules.DER)] |
| 32 | + public static void ReadEncodedValue_Indefinite(AsnEncodingRules ruleSet) |
| 33 | + { |
| 34 | + // CONSTRUCTED OCTET STRING (indefinite) |
| 35 | + // OCTET STRING (1 byte) |
| 36 | + // OCTET STRING (5 bytes) |
| 37 | + // END OF CONTENTS |
| 38 | + // NULL |
| 39 | + ReadOnlySpan<byte> data = |
| 40 | + [ |
| 41 | + 0x24, 0x80, |
| 42 | + 0x04, 0x01, 0x01, |
| 43 | + 0x04, 0x05, 0x02, 0x03, 0x04, 0x05, 0x06, |
| 44 | + 0x00, 0x00, |
| 45 | + 0x05, 0x00, |
| 46 | + ]; |
| 47 | + |
| 48 | + // BER: Indefinite length encoding is OK, no requirements on the contents. |
| 49 | + // CER: Indefinite length encoding is required for CONSTRUCTED, the contents are invalid for OCTET STRING, |
| 50 | + // but (Try)ReadEncodedValue doesn't pay attention to that. |
| 51 | + // DER: Indefinite length encoding is never permitted. |
| 52 | + |
| 53 | + if (ruleSet == AsnEncodingRules.DER) |
| 54 | + { |
| 55 | + ExpectFailure(data, ruleSet); |
| 56 | + } |
| 57 | + else |
| 58 | + { |
| 59 | + ExpectSuccess(data, ruleSet, Asn1Tag.ConstructedOctetString, 2, 10, indefiniteLength: true); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + [Theory] |
| 64 | + [InlineData(AsnEncodingRules.BER)] |
| 65 | + [InlineData(AsnEncodingRules.CER)] |
| 66 | + [InlineData(AsnEncodingRules.DER)] |
| 67 | + public static void ReadEncodedValue_DefiniteConstructed(AsnEncodingRules ruleSet) |
| 68 | + { |
| 69 | + // CONSTRUCTED OCTET STRING (11 bytes) |
| 70 | + // OCTET STRING (1 byte) |
| 71 | + // OCTET STRING (5 bytes) |
| 72 | + // NULL |
| 73 | + ReadOnlySpan<byte> data = |
| 74 | + [ |
| 75 | + 0x24, 0x0A, |
| 76 | + 0x04, 0x01, 0x01, |
| 77 | + 0x04, 0x05, 0x02, 0x03, 0x04, 0x05, 0x06, |
| 78 | + 0x05, 0x00, |
| 79 | + ]; |
| 80 | + |
| 81 | + // BER: Indefinite length encoding is OK, no requirements on the contents. |
| 82 | + // CER: Indefinite length encoding is required for CONSTRUCTED, so fail. |
| 83 | + // DER: CONSTRUCTED OCTET STRING is not permitted, but ReadEncodedValue doesn't check for that, |
| 84 | + // since the length is in minimal representation, the read is successful |
| 85 | + |
| 86 | + if (ruleSet == AsnEncodingRules.CER) |
| 87 | + { |
| 88 | + ExpectFailure(data, ruleSet); |
| 89 | + } |
| 90 | + else |
| 91 | + { |
| 92 | + ExpectSuccess(data, ruleSet, Asn1Tag.ConstructedOctetString, 2, 10); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + [Theory] |
| 97 | + [InlineData(AsnEncodingRules.BER)] |
| 98 | + [InlineData(AsnEncodingRules.CER)] |
| 99 | + [InlineData(AsnEncodingRules.DER)] |
| 100 | + public static void ReadEncodedValue_OutOfBoundsLength(AsnEncodingRules ruleSet) |
| 101 | + { |
| 102 | + // SEQUENCE (3 bytes), but only one byte remains. |
| 103 | + ReadOnlySpan<byte> data = [0x30, 0x03, 0x00]; |
| 104 | + |
| 105 | + ExpectFailure(data, ruleSet); |
| 106 | + } |
| 107 | + |
| 108 | + [Theory] |
| 109 | + [InlineData(AsnEncodingRules.BER)] |
| 110 | + [InlineData(AsnEncodingRules.CER)] |
| 111 | + [InlineData(AsnEncodingRules.DER)] |
| 112 | + public static void ReadEncodedValue_LargeOutOfBoundsLength(AsnEncodingRules ruleSet) |
| 113 | + { |
| 114 | + // SEQUENCE (int.MaxValue bytes), but no bytes remain. |
| 115 | + ReadOnlySpan<byte> data = [0x30, 0x84, 0x7F, 0xFF, 0xFF, 0xFF]; |
| 116 | + |
| 117 | + ExpectFailure(data, ruleSet); |
| 118 | + } |
| 119 | + |
| 120 | + [Theory] |
| 121 | + [InlineData(AsnEncodingRules.BER)] |
| 122 | + [InlineData(AsnEncodingRules.CER)] |
| 123 | + [InlineData(AsnEncodingRules.DER)] |
| 124 | + public static void ReadEncodedValue_ExtremelyLargeLength(AsnEncodingRules ruleSet) |
| 125 | + { |
| 126 | + if (!Environment.Is64BitProcess) |
| 127 | + { |
| 128 | + return; |
| 129 | + } |
| 130 | + |
| 131 | + // OCTET STRING ((int.MaxValue - 6) bytes), span will be inflated to make it look valid. |
| 132 | + byte[] data = "04847FFFFFF9".HexToByteArray(); |
| 133 | + |
| 134 | + unsafe |
| 135 | + { |
| 136 | + fixed (byte* ptr = data) |
| 137 | + { |
| 138 | + // Verify that the length can be interpreted this large, but that it doesn't read that far. |
| 139 | + ReadOnlySpan<byte> span = new ReadOnlySpan<byte>(ptr, int.MaxValue); |
| 140 | + ExpectSuccess(span, ruleSet, Asn1Tag.PrimitiveOctetString, 6, int.MaxValue - 6); |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + private static void ExpectSuccess( |
| 146 | + ReadOnlySpan<byte> data, |
| 147 | + AsnEncodingRules ruleSet, |
| 148 | + Asn1Tag expectedTag, |
| 149 | + int expectedContentOffset, |
| 150 | + int expectedContentLength, |
| 151 | + bool indefiniteLength = false) |
| 152 | + { |
| 153 | + Asn1Tag tag; |
| 154 | + int contentOffset; |
| 155 | + int contentLength; |
| 156 | + int bytesConsumed; |
| 157 | + |
| 158 | + bool read = AsnDecoder.TryReadEncodedValue( |
| 159 | + data, |
| 160 | + ruleSet, |
| 161 | + out tag, |
| 162 | + out contentOffset, |
| 163 | + out contentLength, |
| 164 | + out bytesConsumed); |
| 165 | + |
| 166 | + Assert.True(read, "AsnDecoder.TryReadEncodedValue unexpectedly returned false"); |
| 167 | + Assert.Equal(expectedTag, tag); |
| 168 | + Assert.Equal(expectedContentOffset, contentOffset); |
| 169 | + Assert.Equal(expectedContentLength, contentLength); |
| 170 | + |
| 171 | + int expectedBytesConsumed = expectedContentOffset + expectedContentLength + (indefiniteLength ? 2 : 0); |
| 172 | + Assert.Equal(expectedBytesConsumed, bytesConsumed); |
| 173 | + |
| 174 | + contentOffset = contentLength = bytesConsumed = default; |
| 175 | + |
| 176 | + tag = AsnDecoder.ReadEncodedValue( |
| 177 | + data, |
| 178 | + ruleSet, |
| 179 | + out contentOffset, |
| 180 | + out contentLength, |
| 181 | + out bytesConsumed); |
| 182 | + |
| 183 | + Assert.Equal(expectedTag, tag); |
| 184 | + Assert.Equal(expectedContentOffset, contentOffset); |
| 185 | + Assert.Equal(expectedContentLength, contentLength); |
| 186 | + Assert.Equal(expectedBytesConsumed, bytesConsumed); |
| 187 | + } |
| 188 | + |
| 189 | + private static void ExpectFailure(ReadOnlySpan<byte> data, AsnEncodingRules ruleSet) |
| 190 | + { |
| 191 | + Asn1Tag tag; |
| 192 | + int contentOffset; |
| 193 | + int contentLength; |
| 194 | + int bytesConsumed; |
| 195 | + |
| 196 | + bool read = AsnDecoder.TryReadEncodedValue( |
| 197 | + data, |
| 198 | + ruleSet, |
| 199 | + out tag, |
| 200 | + out contentOffset, |
| 201 | + out contentLength, |
| 202 | + out bytesConsumed); |
| 203 | + |
| 204 | + Assert.False(read, "AsnDecoder.TryReadEncodedValue unexpectedly returned true"); |
| 205 | + Assert.Equal(default, tag); |
| 206 | + Assert.Equal(default, contentOffset); |
| 207 | + Assert.Equal(default, contentLength); |
| 208 | + Assert.Equal(default, bytesConsumed); |
| 209 | + |
| 210 | + int seed = Environment.CurrentManagedThreadId; |
| 211 | + Asn1Tag seedTag = new Asn1Tag(TagClass.Private, seed, (seed & 1) == 0); |
| 212 | + tag = seedTag; |
| 213 | + contentOffset = contentLength = bytesConsumed = seed; |
| 214 | + |
| 215 | + try |
| 216 | + { |
| 217 | + tag = AsnDecoder.ReadEncodedValue( |
| 218 | + data, |
| 219 | + ruleSet, |
| 220 | + out contentOffset, |
| 221 | + out contentLength, |
| 222 | + out bytesConsumed); |
| 223 | + |
| 224 | + Assert.Fail("ReadEncodedValue should have thrown AsnContentException"); |
| 225 | + } |
| 226 | + catch (AsnContentException e) |
| 227 | + { |
| 228 | + Assert.IsType<AsnContentException>(e); |
| 229 | + } |
| 230 | + |
| 231 | + Assert.Equal(seedTag, tag); |
| 232 | + Assert.Equal(seed, contentOffset); |
| 233 | + Assert.Equal(seed, contentLength); |
| 234 | + Assert.Equal(seed, bytesConsumed); |
| 235 | + } |
| 236 | + } |
| 237 | +} |
0 commit comments