internal static void Decode(ref AsnValueReader reader, ReadOnlyMemory <byte> rebind, out DirectoryStringAsn decoded) { decoded = default; Asn1Tag tag = reader.PeekTag(); ReadOnlySpan <byte> rebindSpan = rebind.Span; int offset; ReadOnlySpan <byte> tmpSpan; if (tag.HasSameClassAndValue(new Asn1Tag(UniversalTagNumber.T61String))) { decoded.TeletexString = reader.ReadCharacterString(UniversalTagNumber.T61String); } else if (tag.HasSameClassAndValue(new Asn1Tag(UniversalTagNumber.PrintableString))) { decoded.PrintableString = reader.ReadCharacterString(UniversalTagNumber.PrintableString); } else if (tag.HasSameClassAndValue(new Asn1Tag((UniversalTagNumber)28))) { tmpSpan = reader.ReadEncodedValue(); decoded.UniversalString = rebindSpan.Overlaps(tmpSpan, out offset) ? rebind.Slice(offset, tmpSpan.Length) : tmpSpan.ToArray(); } else if (tag.HasSameClassAndValue(new Asn1Tag(UniversalTagNumber.UTF8String))) { decoded.Utf8String = reader.ReadCharacterString(UniversalTagNumber.UTF8String); } else if (tag.HasSameClassAndValue(new Asn1Tag(UniversalTagNumber.BMPString))) { decoded.BmpString = reader.ReadCharacterString(UniversalTagNumber.BMPString); } else { throw new CryptographicException(); } }
internal static void Decode(ref AsnValueReader reader, Asn1Tag expectedTag, ReadOnlyMemory <byte> rebind, out FieldID decoded) { decoded = default; AsnValueReader sequenceReader = reader.ReadSequence(expectedTag); ReadOnlySpan <byte> rebindSpan = rebind.Span; int offset; ReadOnlySpan <byte> tmpSpan; decoded.FieldType = sequenceReader.ReadObjectIdentifierAsString(); tmpSpan = sequenceReader.ReadEncodedValue(); decoded.Parameters = rebindSpan.Overlaps(tmpSpan, out offset) ? rebind.Slice(offset, tmpSpan.Length) : tmpSpan.ToArray(); sequenceReader.ThrowIfNotEmpty(); }
/// <summary> /// Reads the next value as a SET-OF with the specified tag /// and returns the result as an <see cref="AsnValueReader"/> positioned at the first /// value in the set-of (or with <see cref="HasData"/> == <c>false</c>). /// </summary> /// <param name="expectedTag">The tag to check for before reading.</param> /// <param name="skipSortOrderValidation"> /// <c>true</c> to always accept the data in the order it is presented, /// <c>false</c> to verify that the data is sorted correctly when the /// encoding rules say sorting was required (CER and DER). /// </param> /// <returns> /// an <see cref="AsnValueReader"/> positioned at the first /// value in the set-of (or with <see cref="HasData"/> == <c>false</c>). /// </returns> /// <remarks> /// the nested content is not evaluated by this method (aside from sort order, when /// required), and may contain data which is not valid under the current encoding rules. /// </remarks> /// <exception cref="CryptographicException"> /// the next value does not have the correct tag --OR-- /// the length encoding is not valid under the current encoding rules --OR-- /// the contents are not valid under the current encoding rules /// </exception> /// <exception cref="ArgumentException"> /// <paramref name="expectedTag"/>.<see cref="Asn1Tag.TagClass"/> is /// <see cref="TagClass.Universal"/>, but /// <paramref name="expectedTag"/>.<see cref="Asn1Tag.TagValue"/> is not correct for /// the method /// </exception> public AsnValueReader ReadSetOf(Asn1Tag expectedTag, bool skipSortOrderValidation = false) { Asn1Tag tag = ReadTagAndLength(out int?length, out int headerLength); CheckExpectedTag(tag, expectedTag, UniversalTagNumber.SetOf); // T-REC-X.690-201508 sec 8.12.1 if (!tag.IsConstructed) { throw new CryptographicException(SR.Cryptography_Der_Invalid_Encoding); } int suffix = 0; if (length == null) { length = SeekEndOfContents(_data.Slice(headerLength)); suffix = EndOfContentsEncodedLength; } ReadOnlySpan <byte> contents = Slice(_data, headerLength, length.Value); if (!skipSortOrderValidation) { // T-REC-X.690-201508 sec 11.6 // BER data is not required to be sorted. if (RuleSet == AsnEncodingRules.DER || RuleSet == AsnEncodingRules.CER) { AsnValueReader reader = OpenUnchecked(contents, RuleSet); ReadOnlySpan <byte> current = ReadOnlySpan <byte> .Empty; while (reader.HasData) { ReadOnlySpan <byte> previous = current; current = reader.ReadEncodedValue(); if (SetOfValueComparer.Compare(current, previous) < 0) { throw new CryptographicException(SR.Cryptography_Der_Invalid_Encoding); } } } } _data = _data.Slice(headerLength + contents.Length + suffix); return(OpenUnchecked(contents, RuleSet)); }
public static void Decode(ref AsnValueReader reader, Asn1Tag expectedTag, ReadOnlyMemory <byte> rebind, out AlgorithmIdentifierAsn decoded) { decoded = default; AsnValueReader sequenceReader = reader.ReadSequence(expectedTag); ReadOnlySpan <byte> rebindSpan = rebind.Span; int offset; ReadOnlySpan <byte> tmpSpan; decoded.Algorithm = sequenceReader.ReadObjectIdentifier(); if (sequenceReader.HasData) { tmpSpan = sequenceReader.ReadEncodedValue(); decoded.Parameters = rebindSpan.Overlaps(tmpSpan, out offset) ? rebind.Slice(offset, tmpSpan.Length) : tmpSpan.ToArray(); } sequenceReader.ThrowIfNotEmpty(); }
public static void Decode(ref AsnValueReader reader, ReadOnlyMemory <byte> rebind, out GeneralNameAsn decoded) { decoded = default; Asn1Tag tag = reader.PeekTag(); AsnValueReader explicitReader; ReadOnlySpan <byte> rebindSpan = rebind.Span; int offset; ReadOnlySpan <byte> tmpSpan; if (tag.HasSameClassAndValue(new Asn1Tag(TagClass.ContextSpecific, 0))) { Medikit.Security.Cryptography.Asn1.OtherNameAsn tmpOtherName; Medikit.Security.Cryptography.Asn1.OtherNameAsn.Decode(ref reader, new Asn1Tag(TagClass.ContextSpecific, 0), rebind, out tmpOtherName); decoded.OtherName = tmpOtherName; } else if (tag.HasSameClassAndValue(new Asn1Tag(TagClass.ContextSpecific, 1))) { decoded.Rfc822Name = reader.ReadCharacterString(new Asn1Tag(TagClass.ContextSpecific, 1), UniversalTagNumber.IA5String); } else if (tag.HasSameClassAndValue(new Asn1Tag(TagClass.ContextSpecific, 2))) { decoded.DnsName = reader.ReadCharacterString(new Asn1Tag(TagClass.ContextSpecific, 2), UniversalTagNumber.IA5String); } else if (tag.HasSameClassAndValue(new Asn1Tag(TagClass.ContextSpecific, 3))) { tmpSpan = reader.ReadEncodedValue(); decoded.X400Address = rebindSpan.Overlaps(tmpSpan, out offset) ? rebind.Slice(offset, tmpSpan.Length) : tmpSpan.ToArray(); } else if (tag.HasSameClassAndValue(new Asn1Tag(TagClass.ContextSpecific, 4))) { explicitReader = reader.ReadSequence(new Asn1Tag(TagClass.ContextSpecific, 4)); tmpSpan = explicitReader.ReadEncodedValue(); decoded.DirectoryName = rebindSpan.Overlaps(tmpSpan, out offset) ? rebind.Slice(offset, tmpSpan.Length) : tmpSpan.ToArray(); explicitReader.ThrowIfNotEmpty(); } else if (tag.HasSameClassAndValue(new Asn1Tag(TagClass.ContextSpecific, 5))) { Medikit.Security.Cryptography.Asn1.EdiPartyNameAsn tmpEdiPartyName; Medikit.Security.Cryptography.Asn1.EdiPartyNameAsn.Decode(ref reader, new Asn1Tag(TagClass.ContextSpecific, 5), rebind, out tmpEdiPartyName); decoded.EdiPartyName = tmpEdiPartyName; } else if (tag.HasSameClassAndValue(new Asn1Tag(TagClass.ContextSpecific, 6))) { decoded.Uri = reader.ReadCharacterString(new Asn1Tag(TagClass.ContextSpecific, 6), UniversalTagNumber.IA5String); } else if (tag.HasSameClassAndValue(new Asn1Tag(TagClass.ContextSpecific, 7))) { if (reader.TryReadPrimitiveOctetStringBytes(new Asn1Tag(TagClass.ContextSpecific, 7), out tmpSpan)) { decoded.IPAddress = rebindSpan.Overlaps(tmpSpan, out offset) ? rebind.Slice(offset, tmpSpan.Length) : tmpSpan.ToArray(); } else { decoded.IPAddress = reader.ReadOctetString(new Asn1Tag(TagClass.ContextSpecific, 7)); } } else if (tag.HasSameClassAndValue(new Asn1Tag(TagClass.ContextSpecific, 8))) { decoded.RegisteredId = reader.ReadObjectIdentifierAsString(new Asn1Tag(TagClass.ContextSpecific, 8)); } else { throw new CryptographicException(); } }