internal static Oid?GetSharedOrNullOid(ref AsnValueReader asnValueReader, Asn1Tag?expectedTag = null) { #if NET Asn1Tag tag = asnValueReader.PeekTag(); // This isn't a valid OID, so return null and let whatever's going to happen happen. if (tag.IsConstructed) { return(null); } Asn1Tag expected = expectedTag.GetValueOrDefault(Asn1Tag.ObjectIdentifier); Debug.Assert( expected.TagClass != TagClass.Universal || expected.TagValue == (int)UniversalTagNumber.ObjectIdentifier, $"{nameof(GetSharedOrNullOid)} was called with the wrong Universal class tag: {expectedTag}"); // Not the tag we're expecting, so don't match. if (!tag.HasSameClassAndValue(expected)) { return(null); } ReadOnlySpan <byte> contentBytes = asnValueReader.PeekContentBytes(); Oid?ret = contentBytes switch {
internal static void ValidateDer(ReadOnlySpan <byte> encodedValue) { try { Asn1Tag tag; AsnValueReader reader = new AsnValueReader(encodedValue, AsnEncodingRules.DER); while (reader.HasData) { tag = reader.PeekTag(); // If the tag is in the UNIVERSAL class // // DER limits the constructed encoding to SEQUENCE and SET, as well as anything which gets // a defined encoding as being an IMPLICIT SEQUENCE. if (tag.TagClass == TagClass.Universal) { switch ((UniversalTagNumber)tag.TagValue) { case UniversalTagNumber.External: case UniversalTagNumber.Embedded: case UniversalTagNumber.Sequence: case UniversalTagNumber.Set: case UniversalTagNumber.UnrestrictedCharacterString: if (!tag.IsConstructed) { throw new CryptographicException(SR.Cryptography_Der_Invalid_Encoding); } break; default: if (tag.IsConstructed) { throw new CryptographicException(SR.Cryptography_Der_Invalid_Encoding); } break; } } if (tag.IsConstructed) { ValidateDer(reader.PeekContentBytes()); } // Skip past the current value. reader.ReadEncodedValue(); } } catch (AsnContentException e) { throw new CryptographicException(SR.Cryptography_Der_Invalid_Encoding, e); } }
public static void PeekContentBytes_InvalidLength() { byte[] badLength = "04040203".HexToByteArray(); AsnReader reader = new AsnReader(badLength, AsnEncodingRules.BER); Assert.Throws <CryptographicException>(() => reader.PeekContentBytes()); Assert.Throws <CryptographicException>( () => { AsnValueReader valueReader = new AsnValueReader(badLength, AsnEncodingRules.BER); valueReader.PeekContentBytes(); }); }