Esempio n. 1
0
        def fromNumber(number):
            """
            Create a component whose value is the nonNegativeInteger encoding of
            the number.

            :param int number: The number to be encoded.
            :return: The component value.
            :rtype: Name.Component
            """
            encoder = TlvEncoder(8)
            encoder.writeNonNegativeInteger(number)
            return Name.Component(Blob(encoder.getOutput(), False))
    def generate(self, interest, keyChain, certificateName, wireFormat = None):
        """
        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.

        :param Interest interest: The interest whose name is append with 
          components.
        :param KeyChain keyChain: The KeyChain for calling sign.
        :param Name certificateName: The certificate name of the key to use for 
          signing.
        :param wireFormat: (optional) A WireFormat object used to encode the 
          SignatureInfo and to encode interest name for signing. If omitted, use
          WireFormat.getDefaultWireFormat().
        :type wireFormat: A subclass of WireFormat
        """
        if wireFormat == None:
            # Don't use a default argument since getDefaultWireFormat can change.
            wireFormat = WireFormat.getDefaultWireFormat()

        timestamp =  round(Common.getNowMilliseconds())
        while timestamp <= self._lastTimestamp:
          timestamp += 1.0

        # The timestamp is encoded as a TLV nonNegativeInteger.
        encoder = TlvEncoder(8)
        encoder.writeNonNegativeInteger(int(timestamp))
        interest.getName().append(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.        
        randomBuffer = bytearray(8)
        for i in range(len(randomBuffer)):
            randomBuffer[i] = _systemRandom.randint(0, 0xff)                
        interest.getName().append(Blob(randomBuffer, False))

        keyChain.sign(interest, certificateName, wireFormat)

        if (interest.getInterestLifetimeMilliseconds() == None or
            interest.getInterestLifetimeMilliseconds()< 0):
          # The caller has not set the interest lifetime, so set it here.
          interest.setInterestLifetimeMilliseconds(1000.0)

        # We successfully signed the interest, so update the timestamp.
        self._lastTimestamp = timestamp
        
Esempio n. 3
0
        def fromNumberWithMarker(number, marker):
            """
            Create a component whose value is the marker appended with the
            nonNegativeInteger encoding of the number.

            :param int number: The number to be encoded.
            :param int marker: The marker to use as the first byte of the
              component.
            :return: The component value.
            :rtype: Name.Component
            """
            encoder = TlvEncoder(9)
            # Encode backwards.
            encoder.writeNonNegativeInteger(number)
            encoder.writeNonNegativeInteger(marker)
            return Name.Component(Blob(encoder.getOutput(), False))
Esempio n. 4
0
        def fromNumberWithMarker(number, marker):
            """
            Create a component whose value is the marker appended with the
            nonNegativeInteger encoding of the number.

            :param int number: The number to be encoded.
            :param int marker: The marker to use as the first byte of the
              component.
            :return: The component value.
            :rtype: Name.Component
            """
            encoder = TlvEncoder(9)
            # Encode backwards.
            encoder.writeNonNegativeInteger(number)
            encoder.writeNonNegativeInteger(marker)
            return Name.Component(Blob(encoder.getOutput(), False))
Esempio n. 5
0
    def generate(self, interest, keyChain, certificateName, wireFormat=None):
        """
        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.

        :param Interest interest: The interest whose name is append with
          components.
        :param KeyChain keyChain: The KeyChain for calling sign.
        :param Name certificateName: The certificate name of the key to use for
          signing.
        :param wireFormat: (optional) A WireFormat object used to encode the
          SignatureInfo and to encode interest name for signing. If omitted, use
          WireFormat.getDefaultWireFormat().
        :type wireFormat: A subclass of WireFormat
        """
        if wireFormat == None:
            # Don't use a default argument since getDefaultWireFormat can change.
            wireFormat = WireFormat.getDefaultWireFormat()

        timestamp = round(Common.getNowMilliseconds())
        while timestamp <= self._lastTimestamp:
            timestamp += 1.0

        # The timestamp is encoded as a TLV nonNegativeInteger.
        encoder = TlvEncoder(8)
        encoder.writeNonNegativeInteger(int(timestamp))
        interest.getName().append(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.
        randomBuffer = bytearray(8)
        for i in range(len(randomBuffer)):
            randomBuffer[i] = _systemRandom.randint(0, 0xff)
        interest.getName().append(Blob(randomBuffer, False))

        keyChain.sign(interest, certificateName, wireFormat)

        if (interest.getInterestLifetimeMilliseconds() == None
                or interest.getInterestLifetimeMilliseconds() < 0):
            # The caller has not set the interest lifetime, so set it here.
            interest.setInterestLifetimeMilliseconds(1000.0)

        # We successfully signed the interest, so update the timestamp.
        self._lastTimestamp = timestamp
Esempio n. 6
0
        def fromNumber(number, type=None, otherTypeCode=None):
            """
            Create a component whose value is the nonNegativeInteger encoding of
            the number.

            :param int number: The number to be encoded.
            :param int type: (optional) The component type as an int from the
              ComponentType enum. If name component type is not a recognized
              ComponentType enum value, then set this to ComponentType.OTHER_CODE
              and use the otherTypeCode parameter. If omitted, use
              ComponentType.GENERIC.
            :param int otherTypeCode: (optional) If type is
              ComponentType.OTHER_CODE, then this is the packet's unrecognized
              content type code, which must be non-negative.
            :return: The new component value.
            :rtype: Name.Component
            """
            encoder = TlvEncoder(8)
            encoder.writeNonNegativeInteger(number)
            return Name.Component(Blob(encoder.getOutput(), False), type,
                                  otherTypeCode)
    def prepareCommandInterestName(self, interest, wireFormat=None):
        """
        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.

        :param Interest interest: The interest whose name is append with
          components.
        :param WireFormat wireFormat: (optional) A WireFormat object used to
          encode the SignatureInfo. If omitted, use WireFormat
          getDefaultWireFormat().
        """
        if wireFormat == None:
            wireFormat = WireFormat.getDefaultWireFormat()

        # _nowOffsetMilliseconds is only used for testing.
        now = Common.getNowMilliseconds() + self._nowOffsetMilliseconds
        timestamp = round(now)
        while timestamp <= self._lastUsedTimestamp:
            timestamp += 1.0

        # Update the timestamp now. In the small chance that signing fails, it
        # just means that we have bumped the timestamp.
        self._lastUsedTimestamp = timestamp

        # The timestamp is encoded as a TLV nonNegativeInteger.
        encoder = TlvEncoder(8)
        encoder.writeNonNegativeInteger(int(timestamp))
        interest.getName().append(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.
        randomBuffer = bytearray(8)
        for i in range(len(randomBuffer)):
            randomBuffer[i] = _systemRandom.randint(0, 0xff)
        interest.getName().append(Blob(randomBuffer, False))
    def prepareCommandInterestName(self, interest, wireFormat = None):
        """
        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.

        :param Interest interest: The interest whose name is append with
          components.
        :param WireFormat wireFormat: (optional) A WireFormat object used to
          encode the SignatureInfo. If omitted, use WireFormat
          getDefaultWireFormat().
        """
        if wireFormat == None:
            wireFormat = WireFormat.getDefaultWireFormat()

        # _nowOffsetMilliseconds is only used for testing.
        now = Common.getNowMilliseconds() + self._nowOffsetMilliseconds
        timestamp =  round(now)
        while timestamp <= self._lastUsedTimestamp:
          timestamp += 1.0

        # Update the timestamp now. In the small chance that signing fails, it
        # just means that we have bumped the timestamp.
        self._lastUsedTimestamp = timestamp

        # The timestamp is encoded as a TLV nonNegativeInteger.
        encoder = TlvEncoder(8)
        encoder.writeNonNegativeInteger(int(timestamp))
        interest.getName().append(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.
        randomBuffer = bytearray(8)
        for i in range(len(randomBuffer)):
            randomBuffer[i] = _systemRandom.randint(0, 0xff)
        interest.getName().append(Blob(randomBuffer, False))
    def makeCommandInterest(self, name, params=None, wireFormat=None):
        """
        Append the timestamp and nonce name components to the supplied name,
        create an Interest object and signs it with the KeyChain given to the
        constructor. This ensures that the timestamp is greater than the
        timestamp used in the previous call.

        :param Name name: The Name for the Interest, which is copied.
        :param SigningInfo params: (optional) The signing parameters. If omitted,
          use a default SigningInfo().
        :param WireFormat wireFormat: (optional) A WireFormat object used to
          encode the SignatureInfo and to encode interest name for signing. If
          omitted, use WireFormat getDefaultWireFormat().
        :return: The new command Interest object.
        :rtype: Interest
        """
        arg2 = params
        arg3 = wireFormat
        if isinstance(arg2, SigningInfo):
            params = arg2
        else:
            params = None

        if isinstance(arg2, WireFormat):
            wireFormat = arg2
        elif isinstance(arg3, WireFormat):
            wireFormat = arg3
        else:
            wireFormat = None

        if params == None:
            params = SigningInfo()

        if wireFormat == None:
            wireFormat = WireFormat.getDefaultWireFormat()

        # This copies the Name.
        commandInterest = Interest(name)

        # _nowOffsetMilliseconds is only used for testing.
        now = Common.getNowMilliseconds() + self._nowOffsetMilliseconds
        timestamp = round(now)
        while timestamp <= self._lastUsedTimestamp:
            timestamp += 1.0

        # The timestamp is encoded as a TLV nonNegativeInteger.
        encoder = TlvEncoder(8)
        encoder.writeNonNegativeInteger(int(timestamp))
        commandInterest.getName().append(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.
        randomBuffer = bytearray(8)
        for i in range(len(randomBuffer)):
            randomBuffer[i] = _systemRandom.randint(0, 0xff)
        commandInterest.getName().append(Blob(randomBuffer, False))

        self._keyChain.sign(commandInterest, params, wireFormat)

        # We successfully signed the interest, so update the timestamp.
        self._lastUsedTimestamp = timestamp

        return commandInterest