public void TestTlvTagHashCode() { RawTag tag = new RawTag(0x1, false, false, new byte[] { 0x74, 0x65, 0x73, 0x74, 0x20, 0x6D, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x0 }); Assert.AreEqual(-10917305, tag.GetHashCode(), "Hash code should be correct"); }
/// <summary> /// Create signature and verify with given verification policy /// </summary> /// <param name="signatureRaw">KSI signature</param> /// <param name="hash">Signed hash</param> private KsiSignature CreateAndVerify(RawTag signatureRaw, DataHash hash) { KsiSignature signature = new KsiSignature(signatureRaw); Verify(signature, hash); return(signature); }
async Task ReloadTag(RawTag value) { var entry = DbContext.Entry(value); await entry.ReloadAsync(); await entry.Collection(x => x.Items).LoadAsync(); }
public void TestImprintTagCreateFromTag() { RawTag rawTag = new RawTag(0x1, false, false, new byte[] { 0x1, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x30, 0x31, 0x32 }); ImprintTag tag = new ImprintTag(rawTag); Assert.AreEqual(0x1, tag.Type, "Tag type should be correct"); Assert.IsFalse(tag.NonCritical, "Tag non critical flag should be correct"); Assert.IsFalse(tag.Forward, "Tag forward flag should be correct"); Assert.AreEqual( new DataHash(HashAlgorithm.Sha2256, new byte[] { 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x30, 0x31, 0x32 }), tag.Value, "Tag value should be decoded correctly"); Assert.AreEqual("TLV[0x1]:0x010102030405060708091011121314151617181920212223242526272829303132", tag.ToString(), "Tag string representation should be correct"); ImprintTag newTag = new ImprintTag(rawTag); Assert.AreEqual(newTag, tag, "Value should be equal"); }
/// <summary> /// Extracts the raw markup block based on the passed in OffsetItems /// </summary> /// <param name="item"></param> /// <param name="nextSibling"></param> /// <returns></returns> protected string GetRawBlock(OffsetItem item, OffsetItem nextSibling) { string rawBlock; int startIndex = item.GetAbsolutePosition(); int closeIndex = item.GetAbsoluteEndPosition(); if (item.EndPosition > 0) { closeIndex = item.GetAbsoluteEndPosition(); } else { if (nextSibling != null) { closeIndex = item.GetAbsoluteEndPosition(nextSibling.Position); } else { closeIndex = RawTag.Length - 1; } } rawBlock = RawTag.Substring(startIndex, closeIndex - startIndex); return(rawBlock); }
override public void Render(TextWriter writer) { if (!String.IsNullOrEmpty(RawTag)) { if (SuppressCDATATags && RawTag.Contains(CDATA_START_TAG) && RawTag.Trim().StartsWith(CDATA_START_TAG)) { string newRaw; int startPos = RawTag.IndexOf(CDATA_START_TAG) + CDATA_START_TAG.Length; int endPos = RawTag.LastIndexOf(CDATA_END_TAG); newRaw = RawTag.Substring(startPos, endPos - startPos); //look for extra at the end if (endPos + CDATA_END_TAG.Length < RawTag.Length) { newRaw += RawTag.Substring(endPos + CDATA_END_TAG.Length); } writer.Write(ResolveDataContextVariables(newRaw, MyDataContext)); } else { writer.Write(ResolveDataContextVariables(RawTag, MyDataContext)); } } }
public bool OffsetsAreParsed() { bool basicIsParsed = (null != MyOffset && null != MyOffset.ChildOffsets); if (basicIsParsed) { //test advanced parsing needs //if empty control, don't check count if (string.IsNullOrEmpty(RawTag)) { return(true); } int startPos = RawTag.IndexOf("<"); if (-1 == startPos) { return(true); } int endPos = RawTag.IndexOf(">", startPos); int emptyEnd = RawTag.IndexOf("/>", startPos); if (emptyEnd < endPos) { return(true); } else { return(MyOffset.ChildOffsets.Count > 0); } } return(false); }
/// <summary> /// Parse child tag /// </summary> protected override ITlvTag ParseChild(ITlvTag childTag) { switch (childTag.Type) { case Constants.PublicationsFileHeader.TagType: _headerIndex = Count; return(_publicationsHeader = childTag as PublicationsFileHeader ?? new PublicationsFileHeader(childTag)); case Constants.CertificateRecord.TagType: _lastCertRecordIndex = Count; CertificateRecord certificateRecord = childTag as CertificateRecord ?? new CertificateRecord(childTag); _certificateRecordList.Add(certificateRecord); return(certificateRecord); case Constants.PublicationRecord.TagTypeInPublicationsFile: if (_firstPublicationRecordIndex == 0) { _firstPublicationRecordIndex = Count; } PublicationRecordInPublicationFile publicationRecord = childTag as PublicationRecordInPublicationFile ?? new PublicationRecordInPublicationFile(childTag); _publicationRecordList.Add(publicationRecord); return(publicationRecord); case Constants.PublicationsFile.CmsSignatureTagType: _cmsSignatureIndex = Count; return(_cmsSignature = GetRawTag(childTag)); default: return(base.ParseChild(childTag)); } }
/// <summary> /// Parse child tag /// </summary> protected override ITlvTag ParseChild(ITlvTag childTag) { switch (childTag.Type) { case Constants.AggregationHashChain.AggregationTimeTagType: return(_aggregationTime = GetIntegerTag(childTag)); case Constants.AggregationHashChain.ChainIndexTagType: IntegerTag chainIndexTag = GetIntegerTag(childTag); _chainIndex.Add(chainIndexTag); return(chainIndexTag); case Constants.AggregationHashChain.InputDataTagType: return(_inputData = GetRawTag(childTag)); case Constants.AggregationHashChain.InputHashTagType: return(_inputHash = GetImprintTag(childTag)); case Constants.AggregationHashChain.AggregationAlgorithmIdTagType: IntegerTag aggrAlgorithmTag = GetIntegerTag(childTag); _aggrAlgorithm = HashAlgorithm.GetById((byte)aggrAlgorithmTag.Value); return(aggrAlgorithmTag); case (uint)LinkDirection.Left: case (uint)LinkDirection.Right: Link linkTag = childTag as Link ?? new Link(childTag); _links.Add(linkTag); return(linkTag); default: return(base.ParseChild(childTag)); } }
/// <summary> /// Get info of all payloads that response PDU contains. /// </summary> /// <param name="pdu"></param> /// <returns></returns> private List <KsiServiceResponsePayloadInfo> GetResponsePayloadInfos(RawTag pdu) { switch (pdu.Type) { case Constants.AggregationResponsePdu.TagType: return(GetAggregatorResponsePayloadInfos(pdu)); case Constants.LegacyAggregationPdu.TagType: return(new List <KsiServiceResponsePayloadInfo>() { GetLegacyAggregatorResponsePayloadInfos(pdu) }); case Constants.ExtendResponsePdu.TagType: return(GetExtenderResponsePayloadInfos(pdu)); case Constants.LegacyExtendPdu.TagType: return(new List <KsiServiceResponsePayloadInfo>() { GetLegacyExtenderResponsePayloadInfos(pdu) }); default: throw new KsiServiceProtocolException("Unknown response PDU type: " + pdu.Type); } }
public static ITlvTag GetCompositeTag(Type type, uint tagType, ITlvTag[] childTags) { RawTag raw; using (TlvWriter writer = new TlvWriter(new MemoryStream())) { foreach (ITlvTag tag in childTags) { writer.WriteTag(tag); } raw = new RawTag(tagType, false, false, ((MemoryStream)writer.BaseStream).ToArray()); } object[] args = new object[] { raw }; ITlvTag value = (ITlvTag)Activator.CreateInstance(type, args); // set _value inside CompositeTag FieldInfo field = typeof(CompositeTag).GetField("_childTags", BindingFlags.Instance | BindingFlags.NonPublic); if (field == null) { throw new Exception("Cannot find field '_value' inside CompositeTag class."); } field.SetValue(value, new List <ITlvTag>(childTags)); return(value); }
public void TestStringTagDecodeNotEndingWithNullByte() { RawTag rawTag = new RawTag(0x1, true, true, new byte[] { 0x74, 0x65, 0x73, 0x74, 0x20, 0x6D, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65 }); Assert.That(delegate { new StringTag(rawTag); }, Throws.TypeOf <KsiException>().With.Message.StartWith("String must be null terminated")); }
public void TestTlvTagCreateFromData() { RawTag tag = new RawTag(0x1, true, true, new byte[] { 0x1, 0x2, 0x3 }); Assert.AreEqual(0x1, tag.Type, "Tag type should be preserved"); Assert.IsTrue(tag.NonCritical, "Tag non critical flag should be preserved"); Assert.IsTrue(tag.Forward, "Tag forward flag should be preserved"); CollectionAssert.AreEqual(new byte[] { 0x1, 0x2, 0x3 }, tag.Value, "Tag value should be preserved"); Assert.AreEqual("TLV[0x1,N,F]:0x010203", tag.ToString()); }
public void TestTlvTagToString() { RawTag tag = new RawTag(0x1, false, false, new byte[] { 0x74, 0x65, 0x73, 0x74, 0x20, 0x6D, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x0 }); Assert.AreEqual("TLV[0x1]:0x74657374206D65737361676500", tag.ToString(), "Tag byte hex representation should be correct"); tag = new RawTag(0x1, true, true, new byte[] { 0x74, 0x65, 0x73, 0x74, 0x20, 0x6D, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x0 }); Assert.AreEqual("TLV[0x1,N,F]:0x74657374206D65737361676500", tag.ToString(), "Tag byte hex representation should be correct"); }
public void TestTlvTagEquals() { RawTag tag = new RawTag(0x1, false, false, new byte[] { 0x1, 0x2, 0x3 }); Assert.AreEqual(new RawTag(0x1, false, false, new byte[] { 0x1, 0x2, 0x3 }), tag, "Tag Equals function should compare correctly"); Assert.IsTrue(tag.Equals(tag), "Tags should be equal"); Assert.IsTrue(tag == new RawTag(0x1, false, false, new byte[] { 0x1, 0x2, 0x3 }), "Tag should compare correctly with other objects"); Assert.IsTrue(tag != new ChildRawTag(0x1, false, false, new byte[] { 0x1, 0x2, 0x3 }), "Tag should compare correctly with other objects"); Assert.IsFalse(tag.Equals(new StringTag(0x1, false, false, "test")), "Tag Equals function should compare correctly with other objects"); Assert.IsFalse(tag.Equals(new object()), "Tag Equals function should compare correctly with other objects"); }
/// <summary> /// Get all child tags from byte array /// </summary> /// <param name="bytes">byte array containing tags</param> /// <returns></returns> private static IEnumerable <RawTag> GetChildren(byte[] bytes) { using (TlvReader tlvReader = new TlvReader(new MemoryStream(bytes))) { while (tlvReader.BaseStream.Position < tlvReader.BaseStream.Length) { RawTag raw = tlvReader.ReadTag(); yield return(raw); } } }
public void TestTlvTagCreateFromTag() { RawTag tlvTag = new RawTag(0x1, false, false, new byte[] { 0x1, 0x2, 0x3 }); RawTag tag = new RawTag(tlvTag); Assert.AreEqual(0x1, tag.Type, "Tag type should be correct"); Assert.IsFalse(tag.NonCritical, "Tag non critical flag should be correct"); Assert.IsFalse(tag.Forward, "Tag forward flag should be correct"); CollectionAssert.AreEqual(new byte[] { 0x1, 0x2, 0x3 }, tag.Value, "Tag value should be decoded correctly"); Assert.AreEqual("TLV[0x1]:0x010203", tag.ToString(), "Tag string representation should be correct"); }
private CertificateRecord GetCertificateRecord(byte[] id, byte[] cert) { byte[] idTagBytes = new RawTag(Constants.CertificateRecord.CertificateIdTagType, false, false, id).Encode(); byte[] certTagBytes = new RawTag(Constants.CertificateRecord.X509CertificateTagType, false, false, cert).Encode(); MemoryStream stream = new MemoryStream(); stream.Write(idTagBytes, 0, idTagBytes.Length); stream.Write(certTagBytes, 0, certTagBytes.Length); return(new CertificateRecord(new RawTag(Constants.CertificateRecord.TagType, false, false, stream.ToArray()))); }
/// <summary> /// Get legacy PDU tag from raw tag. /// </summary> /// <param name="rawTag"></param> /// <returns></returns> private LegacyPdu GetLegacyPdu(RawTag rawTag) { switch (GetPduTagType(true)) { case Constants.LegacyAggregationPdu.TagType: return(new LegacyAggregationPdu(rawTag)); case Constants.LegacyExtendPdu.TagType: return(new LegacyExtendPdu(rawTag)); default: throw new KsiServiceException("Unhandled tag type: " + GetPduTagType(true)); } }
/// <summary> /// Get PDU tag from raw tag. /// </summary> /// <param name="rawTag"></param> /// <returns></returns> private Pdu GetPdu(RawTag rawTag) { switch (GetPduTagType(false)) { case Constants.AggregationResponsePdu.TagType: return(new AggregationResponsePdu(rawTag)); case Constants.ExtendResponsePdu.TagType: return(new ExtendResponsePdu(rawTag)); default: throw new KsiServiceException("Unhandled tag type: " + GetPduTagType(false)); } }
SilkTagBase ParseRawTag(string inputRawTag, TagFactory tFactory) { RawTag newRawTag = new RawTag(); string[] rawArguments; //string inputRawTag = rawTag.Replace(" ", String.Empty); for (int i = 0; i < inputRawTag.Length; i++) { if (i == 2) { //inputRawTag.Replace(" ", ""); for (int j = i; j < inputRawTag.Length; j++) { //Debug.Log(inputRawTag[j]); if (!inputRawTag.Contains("=")) { if (inputRawTag[j] != '>') { newRawTag.RawTagName += inputRawTag[j]; } else { break; } } else if (inputRawTag[j] == '=') { rawArguments = inputRawTag.Substring(j + 1).Split(','); for (int r = 0; r < rawArguments.Length; r++) { string rawArgument = rawArguments[r].Replace("\"", "").Replace(">>", String.Empty); newRawTag.AddArgument(rawArgument); } break; } else { newRawTag.RawTagName += inputRawTag[j]; } } } } return(tFactory.SetTag(newRawTag.RawTagName, newRawTag.TagArgs)); //return newRawTag; }
public void TestIntegerTagCreateFromTag() { RawTag rawTag = new RawTag(0x1, false, false, new byte[] { 0x1 }); IntegerTag tag = new IntegerTag(rawTag); Assert.AreEqual(0x1, tag.Type, "Tag type should be correct"); Assert.IsFalse(tag.NonCritical, "Tag non critical flag should be correct"); Assert.IsFalse(tag.Forward, "Tag forward flag should be correct"); Assert.AreEqual(1, tag.Value, "Tag value should be decoded correctly"); Assert.AreEqual("TLV[0x1]:i1", tag.ToString(), "Tag string representation should be correct"); IntegerTag newTag = new IntegerTag(rawTag); Assert.AreEqual(newTag, tag, "Value should be equal"); }
public void TlvTagBuilderGetTagByTypeReturnNullTest() { RawTag child1 = new RawTag(0x1, true, false, new byte[] { 0x1, 0x2 }); CompositeTestTag tag = new CompositeTestTag(0x1, false, false, new ITlvTag[] { child1, }); TlvTagBuilder builder = new TlvTagBuilder(tag); ITlvTag searchedTag = builder.GetChildByType(0x2); Assert.IsNull(searchedTag, "Invalid return tag"); }
public async Task <RawTag> AddTag(RawTagMutation value) { var tag = new RawTag { Id = value.Id ?? Guid.NewGuid().ToString(), Name = value.Name ?? "", Domain = value.Domain ?? "", }; DbContext.Tags.Add(tag); await DbContext.SaveChangesAsync(); await ReloadTag(tag); return(tag); }
private static List <KsiServiceResponsePayloadInfo> GetExtenderResponsePayloadInfos(RawTag pdu) { List <KsiServiceResponsePayloadInfo> list = new List <KsiServiceResponsePayloadInfo>(); IEnumerable <RawTag> children = GetChildren(pdu.Value); bool containsUnknownPayload = false; foreach (RawTag child in children) { switch (child.Type) { case Constants.ExtendResponsePayload.TagType: RawTag requestIdTag = GetTagByType(child.Value, Constants.PduPayload.RequestIdTagType); if (requestIdTag == null) { throw new KsiServiceProtocolException("Cannot find request id tag from extender response payload."); } list.Add(new KsiServiceResponsePayloadInfo(KsiServiceResponsePayloadType.Extending, new IntegerTag(requestIdTag).Value)); break; case Constants.ExtenderConfigResponsePayload.TagType: list.Add(new KsiServiceResponsePayloadInfo(KsiServiceResponsePayloadType.ExtenderConfig)); break; case Constants.ErrorPayload.TagType: list.Add(new KsiServiceResponsePayloadInfo(KsiServiceResponsePayloadType.Error)); break; case Constants.PduHeader.TagType: case Constants.Pdu.MacTagType: break; default: containsUnknownPayload = true; break; } } if (containsUnknownPayload) { // try to parse PDU to check if critical unknown tags are included in which case a parsing exceptions is thrown by ExtendResponsePdu ExtendResponsePdu extendResponsePdu = new ExtendResponsePdu(pdu); Logger.Warn(string.Format("TCP response processor received unexpected response payloads!{0}PDU:{0}{1}", Environment.NewLine, extendResponsePdu)); } return(list); }
public void TlvTagBuilderGetTagByTypeTest() { RawTag child1 = new RawTag(0x1, true, false, new byte[] { 0x1, 0x2 }); RawTag child2 = new RawTag(0x2, true, false, new byte[] { 0x3, 0x4 }); CompositeTestTag tag = new CompositeTestTag(0x1, false, false, new ITlvTag[] { child1, child2 }); TlvTagBuilder builder = new TlvTagBuilder(tag); ITlvTag seachedTag = builder.GetChildByType(0x2); CollectionAssert.AreEqual(seachedTag.EncodeValue(), child2.EncodeValue(), "Invalid child returned."); }
private void ApplyToString() { const string format = "{0,33} {1,6}: {2} [{3}]"; switch (Tag) { case TagLengthValueType.ApplicationExpirationDate: case TagLengthValueType.ApplicationEffectiveDate: toString = b => string.Format(format, Tag, RawTag.AsString(false), b.DefaultDecoder(), b.DateDecoder()); break; case TagLengthValueType.PrimaryAccountNumber: toString = b => string.Format( format, Tag, RawTag.AsString(false), b.DefaultDecoder().ObfuscateCardNumber(), b.BcdDecoder().ObfuscateCardNumber()); break; case TagLengthValueType.IccTrack2EquivData: toString = b => string.Format("{0,33} {1,6}: {2}", Tag, RawTag.AsString(false), b.ObfuscateTrack2()); break; case TagLengthValueType.CardMovementTrack2Data: toString = b => string.Format("{0,33} {1,6}: {2}", Tag, RawTag.AsString(false), b.ObfuscateTrack2(b.Count())); break; case TagLengthValueType.CardholderName: case TagLengthValueType.PreferredName: toString = b => string.Format(format, Tag, RawTag.AsString(false), b.DefaultDecoder(), b.AsciiDecoder()); break; case TagLengthValueType.InformationDescription: toString = b => string.Format("{0,33} {1,6}: {2}", Tag, RawTag.AsString(false), Encoding.ASCII.GetString(b)); break; default: toString = b => string.Format("{0,33} {1,6}: {2}", Tag, RawTag.AsString(false), b.DefaultDecoder()); break; } }
public void TlvTagBuilderRemoveNullTest() { RawTag child1 = new RawTag(0x1, true, false, new byte[] { 0x1, 0x2 }); CompositeTestTag tag = new CompositeTestTag(0x1, false, false, new ITlvTag[] { child1 }); TlvTagBuilder builder = new TlvTagBuilder(tag); ArgumentNullException ex = Assert.Throws <ArgumentNullException>(delegate { builder.RemoveChildTag(null); }); Assert.AreEqual("childTag", ex.ParamName); }
public void TlvTagBuilderAddTest() { RawTag child1 = new RawTag(0x1, true, false, new byte[] { 0x1, 0x2 }); RawTag child2 = new RawTag(0x2, true, false, new byte[] { 0x3, 0x4 }); CompositeTestTag tag = new CompositeTestTag(0x1, false, false, new ITlvTag[] { child1 }); TlvTagBuilder builder = new TlvTagBuilder(tag); builder.AddChildTag(child2); CompositeTestTag newTag = new CompositeTestTag(builder.BuildTag()); CollectionAssert.AreEqual(newTag[1].EncodeValue(), child2.EncodeValue(), "Invalid second child."); }
/// <summary> /// Parse child tag /// </summary> protected override ITlvTag ParseChild(ITlvTag childTag) { switch (childTag.Type) { case Constants.SignatureData.SignatureTypeTagType: return(_signatureType = GetStringTag(childTag)); case Constants.SignatureData.SignatureValueTagType: return(_signatureValue = GetRawTag(childTag)); case Constants.SignatureData.CertificateIdTagType: return(_certificateId = GetRawTag(childTag)); case Constants.SignatureData.CertificateRepositoryUriTagType: return(_certificateRepositoryUri = GetStringTag(childTag)); default: return(base.ParseChild(childTag)); } }