Example #1
0
        /// <summary>
        /// Append a timestamp component and a random nonce component to interest's
        /// name. This ensures that the timestamp is greater than the timestamp used in
        /// the previous call.
        /// </summary>
        ///
        /// <param name="interest">The interest whose name is append with components.</param>
        /// <param name="wireFormat">A WireFormat object used to encode the SignatureInfo.</param>
        public void prepareCommandInterestName(Interest interest,
                                               WireFormat wireFormat)
        {
            double timestamp;

            lock (lastUsedTimestampLock_) {
                // nowOffsetMilliseconds_ is only used for testing.
                double now = net.named_data.jndn.util.Common.getNowMilliseconds() + nowOffsetMilliseconds_;
                timestamp = Math.Round(now, MidpointRounding.AwayFromZero);
                while (timestamp <= lastUsedTimestamp_)
                {
                    timestamp += 1.0d;
                }

                // Update the timestamp now while it is locked. In the small chance that
                //   signing fails, it just means that we have bumped the timestamp.
                lastUsedTimestamp_ = timestamp;
            }

            // The timestamp is encoded as a TLV nonNegativeInteger.
            TlvEncoder encoder = new TlvEncoder(8);

            encoder.writeNonNegativeInteger((long)timestamp);
            interest.getName().append(new Blob(encoder.getOutput(), false));

            // The random value is a TLV nonNegativeInteger too, but we know it is 8
            //   bytes, so we don't need to call the nonNegativeInteger encoder.
            ByteBuffer randomBuffer = ILOG.J2CsMapping.NIO.ByteBuffer.allocate(8);

            // Note: SecureRandom is thread safe.
            net.named_data.jndn.util.Common.getRandom().nextBytes(randomBuffer.array());
            interest.getName().append(new Blob(randomBuffer, false));
        }
Example #2
0
        /// <summary>
        /// Encode this Schedule.
        /// </summary>
        ///
        /// <returns>The encoded buffer.</returns>
        public Blob wireEncode()
        {
            // For now, don't use WireFormat and hardcode to use TLV since the encoding
            // doesn't go out over the wire, only into the local SQL database.
            TlvEncoder encoder    = new TlvEncoder(256);
            int        saveLength = encoder.getLength();

            // Encode backwards.
            // Encode the blackIntervalList.
            int saveLengthForList = encoder.getLength();

            for (IIterator i = blackIntervalList_.descendingIterator(); i.HasNext();)
            {
                RepetitiveInterval element = (RepetitiveInterval)i.Next();
                encodeRepetitiveInterval(element, encoder);
            }
            encoder.writeTypeAndLength(net.named_data.jndn.encoding.tlv.Tlv.Encrypt_BlackIntervalList,
                                       encoder.getLength() - saveLengthForList);

            // Encode the whiteIntervalList.
            saveLengthForList = encoder.getLength();
            for (IIterator i_0 = whiteIntervalList_.descendingIterator(); i_0.HasNext();)
            {
                RepetitiveInterval element_1 = (RepetitiveInterval)i_0.Next();
                encodeRepetitiveInterval(element_1, encoder);
            }
            encoder.writeTypeAndLength(net.named_data.jndn.encoding.tlv.Tlv.Encrypt_WhiteIntervalList,
                                       encoder.getLength() - saveLengthForList);

            encoder.writeTypeAndLength(net.named_data.jndn.encoding.tlv.Tlv.Encrypt_Schedule, encoder.getLength()
                                       - saveLength);

            return(new Blob(encoder.getOutput(), false));
        }
Example #3
0
        /// <summary>
        /// Encode this as an NDN-TLV SafeBag.
        /// </summary>
        ///
        /// <returns>The encoded byte array as a Blob.</returns>
        public Blob wireEncode()
        {
            // Encode directly as TLV. We don't support the WireFormat abstraction
            // because this isn't meant to go directly on the wire.
            TlvEncoder encoder    = new TlvEncoder(256);
            int        saveLength = encoder.getLength();

            // Encode backwards.
            encoder.writeBlobTlv(net.named_data.jndn.encoding.tlv.Tlv.SafeBag_EncryptedKeyBag, privateKeyBag_.buf());
            // Add the entire Data packet encoding as is.
            encoder.writeBuffer(certificate_.wireEncode(net.named_data.jndn.encoding.TlvWireFormat.get()).buf());

            encoder.writeTypeAndLength(net.named_data.jndn.encoding.tlv.Tlv.SafeBag_SafeBag, encoder.getLength()
                                       - saveLength);

            return(new Blob(encoder.getOutput(), false));
        }
Example #4
0
        /// <summary>
        /// Encode this Schedule.
        /// </summary>
        ///
        /// <returns>The encoded buffer.</returns>
        public Blob wireEncode()
        {
            // For now, don't use WireFormat and hardcode to use TLV since the encoding
            // doesn't go out over the wire, only into the local SQL database.
            TlvEncoder encoder    = new TlvEncoder(256);
            int        saveLength = encoder.getLength();

            // Encode backwards.
            // Encode the blackIntervalList.
            int saveLengthForList = encoder.getLength();

            Object[] array = ILOG.J2CsMapping.Collections.Collections.ToArray(blackIntervalList_);
            System.Array.Sort(array);
            for (int i = array.Length - 1; i >= 0; --i)
            {
                RepetitiveInterval element = (RepetitiveInterval)array[i];
                encodeRepetitiveInterval(element, encoder);
            }
            encoder.writeTypeAndLength(net.named_data.jndn.encoding.tlv.Tlv.Encrypt_BlackIntervalList,
                                       encoder.getLength() - saveLengthForList);

            // Encode the whiteIntervalList.
            saveLengthForList = encoder.getLength();
            array             = ILOG.J2CsMapping.Collections.Collections.ToArray(whiteIntervalList_);
            System.Array.Sort(array);
            for (int i_0 = array.Length - 1; i_0 >= 0; --i_0)
            {
                RepetitiveInterval element_1 = (RepetitiveInterval)array[i_0];
                encodeRepetitiveInterval(element_1, encoder);
            }
            encoder.writeTypeAndLength(net.named_data.jndn.encoding.tlv.Tlv.Encrypt_WhiteIntervalList,
                                       encoder.getLength() - saveLengthForList);

            encoder.writeTypeAndLength(net.named_data.jndn.encoding.tlv.Tlv.Encrypt_Schedule, encoder.getLength()
                                       - saveLength);

            return(new Blob(encoder.getOutput(), false));
        }
Example #5
0
        /// <summary>
        /// Append a timestamp component and a random value component to interest's
        /// name. This ensures that the timestamp is greater than the timestamp used in
        /// the previous call. Then use keyChain to sign the interest which appends a
        /// SignatureInfo component and a component with the signature bits. If the
        /// interest lifetime is not set, this sets it.
        /// </summary>
        ///
        /// <param name="interest">The interest whose name is append with components.</param>
        /// <param name="keyChain">The KeyChain for calling sign.</param>
        /// <param name="certificateName">The certificate name of the key to use for signing.</param>
        /// <param name="wireFormat"></param>
        public void generate(Interest interest, KeyChain keyChain,
                             Name certificateName, WireFormat wireFormat)
        {
            double timestamp;

            lock (lastTimestampLock_) {
                timestamp = Math.Round(net.named_data.jndn.util.Common.getNowMilliseconds(), MidpointRounding.AwayFromZero);
                while (timestamp <= lastTimestamp_)
                {
                    timestamp += 1.0d;
                }
                // Update the timestamp now while it is locked. In the small chance that
                //   signing fails, it just means that we have bumped the timestamp.
                lastTimestamp_ = timestamp;
            }

            // The timestamp is encoded as a TLV nonNegativeInteger.
            TlvEncoder encoder = new TlvEncoder(8);

            encoder.writeNonNegativeInteger((long)timestamp);
            interest.getName().append(new Blob(encoder.getOutput(), false));

            // The random value is a TLV nonNegativeInteger too, but we know it is 8 bytes,
            //   so we don't need to call the nonNegativeInteger encoder.
            ByteBuffer randomBuffer = ILOG.J2CsMapping.NIO.ByteBuffer.allocate(8);

            // Note: SecureRandom is thread safe.
            net.named_data.jndn.util.Common.getRandom().nextBytes(randomBuffer.array());
            interest.getName().append(new Blob(randomBuffer, false));

            keyChain.sign(interest, certificateName, wireFormat);

            if (interest.getInterestLifetimeMilliseconds() < 0)
            {
                // The caller has not set the interest lifetime, so set it here.
                interest.setInterestLifetimeMilliseconds(1000.0d);
            }
        }
        wireEncode()
        {
            TlvEncoder encoder    = new TlvEncoder(256);
            int        saveLength = encoder.getLength();

            // Encode backwards.
            if (!other_.isNull())
            {
                encoder.writeBlobTlv(ContentMetaInfo_Other, other_.buf());
            }
            if (hasSegments_)
            {
                encoder.writeTypeAndLength(ContentMetaInfo_HasSegments, 0);
            }
            encoder.writeNonNegativeIntegerTlv
                (ContentMetaInfo_Timestamp, (long)Math.Round(timestamp_));
            encoder.writeBlobTlv
                (ContentMetaInfo_ContentType, new Blob(contentType_).buf());

            encoder.writeTypeAndLength
                (ContentMetaInfo_ContentMetaInfo, encoder.getLength() - saveLength);
            return(new Blob(encoder.getOutput(), false));
        }