/// <summary>
        /// Begin get additional aggregator configuration data (async)
        /// </summary>
        /// <param name="callback">callback when configuration request is finished</param>
        /// <param name="asyncState">callback async state object</param>
        /// <returns>async result</returns>
        public IAsyncResult BeginGetAggregatorConfig(AsyncCallback callback, object asyncState)
        {
            if (IsLegacyPduVersion)
            {
                throw new KsiServiceException("Aggregator config request is not supported using PDU version v1. Configure the SDK to use PDU v2 format for the given Aggregator.");
            }

            if (_signingServiceProtocol == null)
            {
                throw new KsiServiceException("Signing service protocol is missing from service.");
            }

            if (_signingServiceCredentials == null)
            {
                throw new KsiServiceException("Signing service credentials are missing.");
            }

            PduHeader header = new PduHeader(_signingServiceCredentials.LoginId);
            AggregatorConfigRequestPayload payload = new AggregatorConfigRequestPayload();
            AggregationRequestPdu          pdu     = new AggregationRequestPdu(header, payload, _signingMacAlgorithm, _signingServiceCredentials.LoginKey);
            ulong requestId = GenerateRequestId();

            Logger.Debug("Begin get aggregator config (request id: {0}){1}{2}", requestId, Environment.NewLine, pdu);
            return(_signingServiceProtocol.BeginGetAggregatorConfig(pdu.Encode(), requestId, callback, asyncState));
        }
예제 #2
0
 /// <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())));
     }
 }
        private IAsyncResult BeginLegacySign(DataHash hash, uint level, AsyncCallback callback, object asyncState)
        {
            PduHeader header    = new PduHeader(_signingServiceCredentials.LoginId);
            ulong     requestId = GenerateRequestId();
            LegacyAggregationRequestPayload payload = level == 0
                ? new LegacyAggregationRequestPayload(requestId, hash)
                : new LegacyAggregationRequestPayload(requestId, hash, level);
            LegacyAggregationPdu pdu = new LegacyAggregationPdu(header, payload, LegacyPdu.GetMacTag(_signingMacAlgorithm, _signingServiceCredentials.LoginKey, header, payload));

            Logger.Debug("Begin legacy sign (request id: {0}){1}{2}", payload.RequestId, Environment.NewLine, pdu);
            return(BeginSignRequest(pdu.Encode(), requestId, hash, level, callback, asyncState));
        }
예제 #4
0
        /// <summary>
        /// Parse child tag
        /// </summary>
        protected override ITlvTag ParseChild(ITlvTag childTag)
        {
            switch (childTag.Type)
            {
            case Constants.PduHeader.TagType:
                return(_header = childTag as PduHeader ?? new PduHeader(childTag));

            case Constants.Pdu.MacTagType:
                return(Mac = GetImprintTag(childTag));

            default:
                return(base.ParseChild(childTag));
            }
        }
        /// <summary>
        ///     Begin extend.
        /// </summary>
        /// <param name="payload">extend request payload</param>
        /// <param name="callback">callback when extending request is finished</param>
        /// <param name="asyncState">callback async state object</param>
        /// <returns>async result</returns>
        private IAsyncResult BeginExtend(ExtendRequestPayload payload, AsyncCallback callback, object asyncState)
        {
            if (_extendingServiceProtocol == null)
            {
                throw new KsiServiceException("Extending service protocol is missing from service.");
            }

            if (_extendingServiceCredentials == null)
            {
                throw new KsiServiceException("Extending service credentials are missing.");
            }

            PduHeader        header = new PduHeader(_extendingServiceCredentials.LoginId);
            ExtendRequestPdu pdu    = new ExtendRequestPdu(header, payload, _extendingMacAlgorithm, _extendingServiceCredentials.LoginKey);

            Logger.Debug("Begin extend. (request id: {0}){1}{2}", payload.RequestId, Environment.NewLine, pdu);
            return(_extendingServiceProtocol.BeginExtend(pdu.Encode(), payload.RequestId, callback, asyncState));
        }
예제 #6
0
        /// <summary>
        /// Parse child tag
        /// </summary>
        protected override ITlvTag ParseChild(ITlvTag childTag)
        {
            foreach (uint tagType in Constants.AllPayloadTypes)
            {
                if (tagType == childTag.Type)
                {
                    return(childTag);
                }
            }

            switch (childTag.Type)
            {
            case Constants.PduHeader.TagType:
                _headerIndex = Count;
                return(Header = childTag as PduHeader ?? new PduHeader(childTag));

            case Constants.Pdu.MacTagType:
                _macIndex = Count;
                return(Mac = GetImprintTag(childTag));
            }

            return(base.ParseChild(childTag));
        }
        /// <summary>
        ///     Begin create signature with given data hash (async).
        /// </summary>
        /// <param name="hash">data hash</param>
        /// <param name="level">the level value of the aggregation tree node</param>
        /// <param name="callback">callback when creating signature is finished</param>
        /// <param name="asyncState">callback async state object</param>
        /// <returns>async result</returns>
        public IAsyncResult BeginSign(DataHash hash, uint level, AsyncCallback callback, object asyncState)
        {
            if (hash == null)
            {
                throw new ArgumentNullException(nameof(hash));
            }

            if (hash.Algorithm.HasDeprecatedSinceDate)
            {
                throw new KsiServiceException(string.Format("Hash algorithm {0} is deprecated since {1} and can not be used for signing.", hash.Algorithm.Name,
                                                            hash.Algorithm.DeprecatedSinceDate?.ToString(Constants.DateFormat)));
            }

            if (_signingServiceProtocol == null)
            {
                throw new KsiServiceException("Signing service protocol is missing from service.");
            }

            if (_signingServiceCredentials == null)
            {
                throw new KsiServiceException("Signing service credentials are missing.");
            }

            if (IsLegacyPduVersion)
            {
                return(BeginLegacySign(hash, level, callback, asyncState));
            }

            PduHeader header    = new PduHeader(_signingServiceCredentials.LoginId);
            ulong     requestId = GenerateRequestId();
            AggregationRequestPayload payload = level == 0 ? new AggregationRequestPayload(requestId, hash) : new AggregationRequestPayload(requestId, hash, level);
            AggregationRequestPdu     pdu     = new AggregationRequestPdu(header, payload, _signingMacAlgorithm, _signingServiceCredentials.LoginKey);

            Logger.Debug("Begin sign (request id: {0}){1}{2}", payload.RequestId, Environment.NewLine, pdu);
            return(BeginSignRequest(pdu.Encode(), requestId, hash, level, callback, asyncState));
        }
예제 #8
0
 /// <summary>
 ///     Create aggregation pdu TLV element from KSI header and payload.
 /// </summary>
 /// <param name="tagType">PDU TLV tag type</param>
 /// <param name="header">PDU header</param>
 /// <param name="payload">aggregation payload</param>
 /// <param name="hmacAlgorithm">MAC algorithm</param>
 /// <param name="key">hmac key</param>
 protected Pdu(uint tagType, PduHeader header, PduPayload payload, HashAlgorithm hmacAlgorithm, byte[] key)
     : base(tagType, false, false, new ITlvTag[] { header, payload, GetEmptyMacTag(hmacAlgorithm) })
 {
     SetMacValue(hmacAlgorithm, key);
 }
 /// <summary>
 ///     Create aggregation request pdu TLV element from KSI header and payload.
 /// </summary>
 /// <param name="header">PDU header</param>
 /// <param name="payload">aggregation payload</param>
 /// <param name="hmacAlgorithm">HMAC algorithm</param>
 /// <param name="key">hmac key</param>
 public AggregationRequestPdu(PduHeader header, PduPayload payload, HashAlgorithm hmacAlgorithm, byte[] key)
     : base(Constants.AggregationRequestPdu.TagType, header, payload, hmacAlgorithm, key)
 {
 }
예제 #10
0
 public LegacyExtendPdu(PduHeader header, PduPayload payload, ImprintTag mac)
     : base(Constants.LegacyExtendPdu.TagType, false, false, new ITlvTag[] { header, payload, mac })
 {
 }