/// <summary> /// Extend signature to publication. /// </summary> /// <param name="calendarHashChain">extended calendar hash chain</param> /// <param name="publicationRecord">extended publication record</param> /// <param name="signatureFactory">signature factory to be used when creating extended signature</param> /// <returns>extended KSI signature</returns> public IKsiSignature Extend(CalendarHashChain calendarHashChain, PublicationRecordInSignature publicationRecord, IKsiSignatureFactory signatureFactory = null) { Logger.Debug("Extending KSI signature."); if (calendarHashChain == null) { throw new ArgumentNullException(nameof(calendarHashChain)); } if (CalendarHashChain != null && !CalendarHashChain.AreRightLinksEqual(calendarHashChain)) { throw new KsiException("Right links of signature calendar hash chain and extended calendar hash chain do not match"); } if (publicationRecord == null) { publicationRecord = new PublicationRecordInSignature(false, false, calendarHashChain.PublicationData); } if (signatureFactory == null) { signatureFactory = new KsiSignatureFactory(); } using (TlvWriter writer = new TlvWriter(new MemoryStream())) { foreach (ITlvTag childTag in this) { switch (childTag.Type) { case Constants.CalendarHashChain.TagType: case Constants.CalendarAuthenticationRecord.TagType: case Constants.PublicationRecord.TagTypeInSignature: break; default: writer.WriteTag(childTag); break; } } writer.WriteTag(calendarHashChain); writer.WriteTag(publicationRecord); try { IKsiSignature signature = signatureFactory.CreateByContent(((MemoryStream)writer.BaseStream).ToArray(), InputHash); Logger.Debug("Extending KSI signature successful."); return(signature); } catch (TlvException e) { Logger.Warn("Extending KSI signature failed: {0}", e); throw; } } }
/// <summary> /// Calculate MAC and attach it to PDU. /// </summary> /// <param name="macAlgorithm">MAC algorithm</param> /// <param name="key">hmac key</param> /// <param name="header">KSI header</param> /// <param name="payload">KSI payload</param> public static ImprintTag GetMacTag(HashAlgorithm macAlgorithm, byte[] key, PduHeader header, PduPayload payload) { using (TlvWriter writer = new TlvWriter(new MemoryStream())) { writer.WriteTag(header); writer.WriteTag(payload); return(new ImprintTag(Constants.Pdu.MacTagType, false, false, CalculateMac(macAlgorithm, key, ((MemoryStream)writer.BaseStream).ToArray()))); } }
public void TestWriteNullValue() { using (TlvWriter writer = new TlvWriter(new MemoryStream())) { writer.WriteTag(new AllowNullValueTlvTag(0x1, false, false)); writer.WriteTag(new AllowNullValueTlvTag(0x257, true, true)); CollectionAssert.AreEqual(new byte[] { 0x1, 0x0, 0xe2, 0x57, 0x0 }, ((MemoryStream)writer.BaseStream).ToArray(), "Writer should output correct byte array"); } }
public void WriteTo(Stream outputStream) { using (TlvWriter writer = new TlvWriter(outputStream)) { writer.WriteTag(this); } }
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 TestWriteNullTag() { using (TlvWriter writer = new TlvWriter(new MemoryStream())) { writer.WriteTag(null); CollectionAssert.AreEqual(new byte[] { }, ((MemoryStream)writer.BaseStream).ToArray(), "Writer should output correct byte array"); } }
public void TestWriteTagShort() { using (TlvWriter writer = new TlvWriter(new MemoryStream())) { writer.WriteTag(new RawTag(0x1, false, true, new byte[] { 0x0, 0x1, 0x2, 0x3 })); CollectionAssert.AreEqual(new byte[] { 0x21, 0x4, 0x0, 0x1, 0x2, 0x3 }, ((MemoryStream)writer.BaseStream).ToArray(), "Writer should output correct byte array"); } }
/// <summary> /// Calculate MAC value. /// </summary> /// <param name="macAlgorithm">MAC algorithm</param> /// <param name="key">HMAC key</param> private DataHash CalcMacValue(HashAlgorithm macAlgorithm, byte[] key) { MemoryStream stream = new MemoryStream(); using (TlvWriter writer = new TlvWriter(stream)) { writer.WriteTag(this); return(CalcMacValue(stream.ToArray(), macAlgorithm, key)); } }
public void TestWriteTagWithTooLongData() { using (TlvWriter writer = new TlvWriter(new MemoryStream())) { ArgumentOutOfRangeException ex = Assert.Throws <ArgumentOutOfRangeException>(delegate { writer.WriteTag(new RawTag(0x1, true, true, new byte[ushort.MaxValue + 1])); }); Assert.AreEqual("data", ex.ParamName); } }
public void TestWriteTagWithTooLongType() { using (TlvWriter writer = new TlvWriter(new MemoryStream())) { ArgumentOutOfRangeException ex = Assert.Throws <ArgumentOutOfRangeException>(delegate { writer.WriteTag(new RawTag(0x2000, true, true, new byte[256])); }); Assert.AreEqual("tag", ex.ParamName); } }
/// <summary> /// Get signed bytes. /// </summary> /// <returns>signed bytes</returns> public byte[] GetSignedBytes() { using (TlvWriter writer = new TlvWriter(new MemoryStream())) { writer.Write(FileBeginningMagicBytes); // get all but last tag for (int i = 0; i < Count - 1; i++) { writer.WriteTag(this[i]); } return(((MemoryStream)writer.BaseStream).ToArray()); } }
public void TestWriteTagLongWithLongType() { using (TlvWriter writer = new TlvWriter(new MemoryStream())) { writer.WriteTag(new RawTag(0x257, true, true, new byte[256])); byte[] result = new byte[260]; result[0] = 0xe2; result[1] = 0x57; result[2] = 0x1; result[3] = 0x0; Array.Copy(new byte[256], 0, result, 4, 256); CollectionAssert.AreEqual(result, ((MemoryStream)writer.BaseStream).ToArray(), "Writer should output correct byte array"); } }
private static IKsiService GetService(List <PduPayload> payloads, ulong requestId = 1584727637) { List <ITlvTag> childTags = new List <ITlvTag> { new PduHeader(Settings.Default.HttpExtendingServiceUser) }; childTags.AddRange(payloads); childTags.Add(new ImprintTag(Constants.Pdu.MacTagType, false, false, new DataHash(HashAlgorithm.Sha2256, new byte[32]))); ExtendResponsePdu pdu = TestUtil.GetCompositeTag <ExtendResponsePdu>(Constants.ExtendResponsePdu.TagType, childTags.ToArray()); MethodInfo m = pdu.GetType().GetMethod("SetMacValue", BindingFlags.Instance | BindingFlags.NonPublic); m.Invoke(pdu, new object[] { HashAlgorithm.Sha2256, Util.EncodeNullTerminatedUtf8String(TestConstants.ServicePass) }); MemoryStream stream = new MemoryStream(); using (TlvWriter writer = new TlvWriter(stream)) { writer.WriteTag(pdu); } return(GetStaticKsiService(stream.ToArray(), requestId)); }