def decodeInterest(self, interest, input): """ Decode input as an NDN-TLV interest and set the fields of the interest object. :param Interest interest: The Interest object whose fields are updated. :param input: The array with the bytes to decode. :type input: An array type with int elements :return: A Tuple of (signedPortionBeginOffset, signedPortionEndOffset) where signedPortionBeginOffset is the offset in the encoding of the beginning of the signed portion, and signedPortionEndOffset is the offset in the encoding of the end of the signed portion. The signed portion starts from the first name component and ends just before the final name component (which is assumed to be a signature for a signed interest). :rtype: (int, int) """ decoder = TlvDecoder(input) endOffset = decoder.readNestedTlvsStart(Tlv.Interest) self._decodeName(interest.getName(), decoder) if decoder.peekType(Tlv.Selectors, endOffset): self._decodeSelectors(interest, decoder) # Require a Nonce, but don't force it to be 4 bytes. nonce = Blob(decoder.readBlobTlv(Tlv.Nonce)) interest.setScope(decoder.readOptionalNonNegativeIntegerTlv( Tlv.Scope, endOffset)) interest.setInterestLifetimeMilliseconds( decoder.readOptionalNonNegativeIntegerTlvAsFloat (Tlv.InterestLifetime, endOffset)) # Set the nonce last because setting other interest fields clears it. interest.setNonce(nonce) decoder.finishNestedTlvs(endOffset)
def wireDecode(self, input): """ Decode the input as an NDN-TLV SafeBag and update this object. :param input: The array with the bytes to decode. :type input: A Blob or an array type with int elements """ if isinstance(input, Blob): input = input.buf() # Decode directly as TLV. We don't support the WireFormat abstraction # because this isn't meant to go directly on the wire. decoder = TlvDecoder(input) endOffset = decoder.readNestedTlvsStart(Tlv.SafeBag_SafeBag) # Get the bytes of the certificate and decode. certificateBeginOffset = decoder.getOffset() certificateEndOffset = decoder.readNestedTlvsStart(Tlv.Data) decoder.seek(certificateEndOffset) self._certificate = Data() self._certificate.wireDecode( decoder.getSlice(certificateBeginOffset, certificateEndOffset), TlvWireFormat.get()) self._privateKeyBag = Blob( decoder.readBlobTlv(Tlv.SafeBag_EncryptedKeyBag), True) decoder.finishNestedTlvs(endOffset)
def decodeStateVector(input): """ Decode the input as a TLV state vector. :param input: The array with the bytes to decode. :type input: An array type with int elements :return: A new dictionary where the key is the member ID string and the value is the sequence number. If the input encoding has repeated entries with the same member ID, this uses only the last entry. :rtype: dict<str,int> :raises ValueError: For invalid encoding. """ stateVector = {} # If input is a blob, get its buf(). decodeBuffer = input.buf() if isinstance(input, Blob) else input decoder = TlvDecoder(decodeBuffer) endOffset = decoder.readNestedTlvsStart( StateVectorSync2018.TLV_StateVector) while decoder.getOffset() < endOffset: entryEndOffset = decoder.readNestedTlvsStart( StateVectorSync2018.TLV_StateVectorEntry) memberIdBlob = Blob( decoder.readBlobTlv( StateVectorSync2018.TLV_StateVector_MemberId), False) stateVector[str(memberIdBlob)] = decoder.readNonNegativeIntegerTlv( StateVectorSync2018.TLV_StateVector_SequenceNumber) decoder.finishNestedTlvs(entryEndOffset) decoder.finishNestedTlvs(endOffset) return stateVector
def decodeData(self, data, input): """ Decode input as an NDN-TLV data packet, set the fields in the data object, and return the signed offsets. :param Data data: The Data object whose fields are updated. :param input: The array with the bytes to decode. :type input: An array type with int elements :return: A Tuple of (signedPortionBeginOffset, signedPortionEndOffset) where signedPortionBeginOffset is the offset in the encoding of the beginning of the signed portion, and signedPortionEndOffset is the offset in the encoding of the end of the signed portion. :rtype: (int, int) """ decoder = TlvDecoder(input) endOffset = decoder.readNestedTlvsStart(Tlv.Data) signedPortionBeginOffset = decoder.getOffset() self._decodeName(data.getName(), decoder) self._decodeMetaInfo(data.getMetaInfo(), decoder) data.setContent(Blob(decoder.readBlobTlv(Tlv.Content))) self._decodeSignatureInfo(data, decoder) signedPortionEndOffset = decoder.getOffset() data.getSignature().setSignature( Blob(decoder.readBlobTlv(Tlv.SignatureValue))) decoder.finishNestedTlvs(endOffset) return (signedPortionBeginOffset, signedPortionEndOffset)
def decodeData(self, data, input): """ Decode input as an NDN-TLV data packet, set the fields in the data object, and return the signed offsets. :param Data data: The Data object whose fields are updated. :param input: The array with the bytes to decode. :type input: An array type with int elements :return: A Tuple of (signedPortionBeginOffset, signedPortionEndOffset) where signedPortionBeginOffset is the offset in the encoding of the beginning of the signed portion, and signedPortionEndOffset is the offset in the encoding of the end of the signed portion. :rtype: (int, int) """ decoder = TlvDecoder(input) endOffset = decoder.readNestedTlvsStart(Tlv.Data) signedPortionBeginOffset = decoder.getOffset() self._decodeName(data.getName(), decoder) self._decodeMetaInfo(data.getMetaInfo(), decoder) data.setContent(Blob(decoder.readBlobTlv(Tlv.Content))) self._decodeSignatureInfo(data, decoder) signedPortionEndOffset = decoder.getOffset() # TODO: The library needs to handle other signature types than # SignatureSha256WithRsa. data.getSignature().setSignature(Blob(decoder.readBlobTlv(Tlv.SignatureValue))) decoder.finishNestedTlvs(endOffset) return (signedPortionBeginOffset, signedPortionEndOffset)
def decodeInterest(self, interest, input): """ Decode input as an NDN-TLV interest and set the fields of the interest object. :param Interest interest: The Interest object whose fields are updated. :param input: The array with the bytes to decode. :type input: An array type with int elements :return: A Tuple of (signedPortionBeginOffset, signedPortionEndOffset) where signedPortionBeginOffset is the offset in the encoding of the beginning of the signed portion, and signedPortionEndOffset is the offset in the encoding of the end of the signed portion. The signed portion starts from the first name component and ends just before the final name component (which is assumed to be a signature for a signed interest). :rtype: (int, int) """ decoder = TlvDecoder(input) endOffset = decoder.readNestedTlvsStart(Tlv.Interest) self._decodeName(interest.getName(), decoder) if decoder.peekType(Tlv.Selectors, endOffset): self._decodeSelectors(interest, decoder) # Require a Nonce, but don't force it to be 4 bytes. nonce = Blob(decoder.readBlobTlv(Tlv.Nonce)) interest.setScope( decoder.readOptionalNonNegativeIntegerTlv(Tlv.Scope, endOffset)) interest.setInterestLifetimeMilliseconds( decoder.readOptionalNonNegativeIntegerTlvAsFloat( Tlv.InterestLifetime, endOffset)) # Set the nonce last because setting other interest fields clears it. interest.setNonce(nonce) decoder.finishNestedTlvs(endOffset)
def decodeControlResponse(self, controlResponse, input): """ Decode input as an NDN-TLV ControlResponse and set the fields of the controlResponse object. :param ControlResponse controlResponse: The ControlResponse object whose fields are updated. :param input: The array with the bytes to decode. :type input: An array type with int elements """ controlResponse.clear() decoder = TlvDecoder(input) endOffset = decoder.readNestedTlvsStart( Tlv.NfdCommand_ControlResponse) # decode face ID controlResponse.setStatusCode(decoder.readNonNegativeIntegerTlv (Tlv.NfdCommand_StatusCode)) statusText = Blob( decoder.readBlobTlv(Tlv.NfdCommand_StatusText), False) controlResponse.setStatusText(str(statusText)) # Decode the body. if decoder.peekType(Tlv.ControlParameters_ControlParameters, endOffset): controlResponse.setBodyAsControlParameters(ControlParameters()) # Decode into the existing ControlParameters to avoid copying. self._decodeControlParameters( controlResponse.getBodyAsControlParameters(), decoder) decoder.finishNestedTlvs(endOffset)
def decodeInterest(self, interest, input): """ Decode input as an NDN-TLV interest and set the fields of the interest object. :param Interest interest: The Interest object whose fields are updated. :param input: The array with the bytes to decode. :type input: An array type with int elements """ decoder = TlvDecoder(input) endOffset = decoder.readNestedTlvsStart(Tlv.Interest) self._decodeName(interest.getName(), decoder) if decoder.peekType(Tlv.Selectors, endOffset): self._decodeSelectors(interest, decoder) # Require a Nonce, but don't force it to be 4 bytes. nonce = Blob(decoder.readBlobTlv(Tlv.Nonce)) interest.setScope(decoder.readOptionalNonNegativeIntegerTlv( Tlv.Scope, endOffset)) interest.setInterestLifetimeMilliseconds( decoder.readOptionalNonNegativeIntegerTlvAsFloat (Tlv.InterestLifetime, endOffset)) # Set the nonce last because setting other interest fields clears it. interest.setNonce(nonce) decoder.finishNestedTlvs(endOffset)
def _encodeSignatureInfo(signature, encoder): """ An internal method to encode signature as the appropriate form of SignatureInfo in NDN-TLV. :param Signature signature: An object of a subclass of Signature to encode. :param TlvEncoder encoder: The TlvEncoder to receive the encoding. """ if isinstance(signature, GenericSignature): # Handle GenericSignature separately since it has the entire encoding. encoding = signature.getSignatureInfoEncoding() # Do a test decoding to sanity check that it is valid TLV. try: decoder = TlvDecoder(encoding.buf()) endOffset = decoder.readNestedTlvsStart(Tlv.SignatureInfo) decoder.readNonNegativeIntegerTlv(Tlv.SignatureType) decoder.finishNestedTlvs(endOffset) except ValueError as ex: raise ValueError( "The GenericSignature encoding is not a valid NDN-TLV SignatureInfo: " + ex) encoder.writeBuffer(encoding.buf()) return saveLength = len(encoder) if isinstance(signature, Sha256WithRsaSignature): # Encode backwards. Tlv0_1_1WireFormat._encodeKeyLocator( Tlv.KeyLocator, signature.getKeyLocator(), encoder) encoder.writeNonNegativeIntegerTlv( Tlv.SignatureType, Tlv.SignatureType_SignatureSha256WithRsa) elif isinstance(signature, Sha256WithEcdsaSignature): # Encode backwards. Tlv0_1_1WireFormat._encodeKeyLocator( Tlv.KeyLocator, signature.getKeyLocator(), encoder) encoder.writeNonNegativeIntegerTlv( Tlv.SignatureType, Tlv.SignatureType_SignatureSha256WithEcdsa) elif isinstance(signature, HmacWithSha256Signature): Tlv0_1_1WireFormat._encodeKeyLocator( Tlv.KeyLocator, signature.getKeyLocator(), encoder) encoder.writeNonNegativeIntegerTlv( Tlv.SignatureType, Tlv.SignatureType_SignatureHmacWithSha256) elif isinstance(signature, DigestSha256Signature): encoder.writeNonNegativeIntegerTlv( Tlv.SignatureType, Tlv.SignatureType_DigestSha256) else: raise RuntimeError( "encodeSignatureInfo: Unrecognized Signature object type") encoder.writeTypeAndLength(Tlv.SignatureInfo, len(encoder) - saveLength)
def decodeInterest(self, interest, input): """ Decode input as an NDN-TLV interest and set the fields of the interest object. :param Interest interest: The Interest object whose fields are updated. :param input: The array with the bytes to decode. :type input: An array type with int elements :return: A Tuple of (signedPortionBeginOffset, signedPortionEndOffset) where signedPortionBeginOffset is the offset in the encoding of the beginning of the signed portion, and signedPortionEndOffset is the offset in the encoding of the end of the signed portion. The signed portion starts from the first name component and ends just before the final name component (which is assumed to be a signature for a signed interest). :rtype: (int, int) """ decoder = TlvDecoder(input) endOffset = decoder.readNestedTlvsStart(Tlv.Interest) offsets = self._decodeName(interest.getName(), decoder) if decoder.peekType(Tlv.Selectors, endOffset): self._decodeSelectors(interest, decoder) # Require a Nonce, but don't force it to be 4 bytes. nonce = Blob(decoder.readBlobTlv(Tlv.Nonce)) interest.setInterestLifetimeMilliseconds( decoder.readOptionalNonNegativeIntegerTlvAsFloat (Tlv.InterestLifetime, endOffset)) if decoder.peekType(Tlv.Data, endOffset): # Get the bytes of the Link TLV. linkBeginOffset = decoder.getOffset() linkEndOffset = decoder.readNestedTlvsStart(Tlv.Data) decoder.seek(linkEndOffset) interest.setLinkWireEncoding( Blob(decoder.getSlice(linkBeginOffset, linkEndOffset), True), self) else: interest.unsetLink() interest.setSelectedDelegationIndex( decoder.readOptionalNonNegativeIntegerTlv( Tlv.SelectedDelegation, endOffset)) if (interest.getSelectedDelegationIndex() != None and interest.getSelectedDelegationIndex() >= 0 and not interest.hasLink()): raise RuntimeError( "Interest has a selected delegation, but no link object") # Set the nonce last because setting other interest fields clears it. interest.setNonce(nonce) decoder.finishNestedTlvs(endOffset) return offsets
def decodeForwardingEntry(self, forwardingEntry, input): """ Decode input as an forwardingEntry and set the fields of the forwardingEntry object. :param forwardingEntry: The ForwardingEntry object whose fields are updated. :type forwardingEntry: ForwardingEntry :param input: The array with the bytes to decode. :type input: An array type with int elements """ decoder = TlvDecoder(input) endOffset = decoder.readNestedTlvsStart(Tlv.ForwardingEntry) actionBytes = decoder.readOptionalBlobTlv(Tlv.Action, endOffset) if actionBytes != None: # Convert bytes to a str. forwardingEntry.setAction("".join(map(chr, actionBytes))) else: forwardingEntry.setAction(None) if decoder.peekType(Tlv.Name, endOffset): self._decodeName(forwardingEntry.getPrefix(), decoder) else: forwardingEntry.getPrefix().clear() forwardingEntry.setFaceId( decoder.readOptionalNonNegativeIntegerTlv(Tlv.FaceID, endOffset)) forwardingEntryFlags = decoder.readOptionalNonNegativeIntegerTlv( Tlv.ForwardingFlags, endOffset) if forwardingEntryFlags != None: forwardingEntry.getForwardingFlags().setForwardingEntryFlags( forwardingEntryFlags) else: # This sets the default flags. forwardingEntry.setForwardingFlags(ForwardingFlags()) forwardingEntry.setFreshnessPeriod( decoder.readOptionalNonNegativeIntegerTlvAsFloat( Tlv.FreshnessPeriod, endOffset)) decoder.finishNestedTlvs(endOffset)
def wireDecode(self, wire): decoder = TlvDecoder(wire) beginOffset = decoder.getOffset() endOffset = decoder.readNestedTlvsStart(Tlv.SignatureInfo) signatureType = decoder.readNonNegativeIntegerTlv(Tlv.SignatureType) if signatureType != Tlv.SignatureType_Sha256WithAbsSignature: raise RuntimeError("Invalid SignatureType code: expected %d, got %d" % (Tlv.SignatureType_Sha256WithAbsSignature, signatureType)) keyLocator = KeyLocator() Tlv0_2WireFormat._decodeKeyLocator(Tlv.KeyLocator, keyLocator, decoder, True) self._keyLocator = ChangeCounter(keyLocator) # if decoder.peekType(Tlv.ValidityPeriod_ValidityPeriod, endOffset): # Tlv0_2WireFormat._decodeValidityPeriod( # signatureInfo.getValidityPeriod(), decoder) decoder.finishNestedTlvs(endOffset)
def wireDecode(self, input): """ Decode the input and update this Schedule object. :param input: The array with the bytes to decode. :type input: An array type with int elements :raises ValueError: For invalid encoding. """ # If input is a blob, get its buf(). decodeBuffer = input.buf() if isinstance(input, Blob) else input # 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. decoder = TlvDecoder(decodeBuffer) endOffset = decoder.readNestedTlvsStart(Tlv.Encrypt_Schedule) # Decode the whiteIntervalList. self._whiteIntervalList = [] listEndOffset = decoder.readNestedTlvsStart(Tlv.Encrypt_WhiteIntervalList) while decoder.getOffset() < listEndOffset: Schedule._sortedSetAdd(self._whiteIntervalList, Schedule._decodeRepetitiveInterval(decoder)) decoder.finishNestedTlvs(listEndOffset) # Decode the blackIntervalList. self._blackIntervalList = [] listEndOffset = decoder.readNestedTlvsStart(Tlv.Encrypt_BlackIntervalList) while decoder.getOffset() < listEndOffset: Schedule._sortedSetAdd(self._blackIntervalList, Schedule._decodeRepetitiveInterval(decoder)) decoder.finishNestedTlvs(listEndOffset) decoder.finishNestedTlvs(endOffset)
def wireDecode(self, input): """ Decode the input and update this Schedule object. :param input: The array with the bytes to decode. :type input: An array type with int elements :raises ValueError: For invalid encoding. """ # If input is a blob, get its buf(). decodeBuffer = input.buf() if isinstance(input, Blob) else input # 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. decoder = TlvDecoder(decodeBuffer) endOffset = decoder.readNestedTlvsStart(Tlv.Encrypt_Schedule) # Decode the whiteIntervalList. self._whiteIntervalList = [] listEndOffset = decoder.readNestedTlvsStart(Tlv.Encrypt_WhiteIntervalList) while decoder.getOffset() < listEndOffset: Schedule._sortedSetAdd( self._whiteIntervalList, Schedule._decodeRepetitiveInterval(decoder)) decoder.finishNestedTlvs(listEndOffset) # Decode the blackIntervalList. self._blackIntervalList = [] listEndOffset = decoder.readNestedTlvsStart(Tlv.Encrypt_BlackIntervalList) while decoder.getOffset() < listEndOffset: Schedule._sortedSetAdd( self._blackIntervalList, Schedule._decodeRepetitiveInterval(decoder)) decoder.finishNestedTlvs(listEndOffset) decoder.finishNestedTlvs(endOffset)
def wireDecode(self, input): """ Decode the input as an NDN-TLV PSyncContent and update this object. :param input: The array with the bytes to decode. :type input: Blob or an array type with int elements """ # If input is a blob, get its buf(). decodeBuffer = input.buf() if isinstance(input, Blob) else input self.clear() # Decode directly as TLV. We don't support the WireFormat abstraction # because this isn't meant to go directly on the wire. decoder = TlvDecoder(decodeBuffer) endOffset = decoder.readNestedTlvsStart(PSyncState.Tlv_PSyncContent) # Decode a sequence of Name. while decoder.getOffset() < len(decodeBuffer): name = Name() Tlv0_3WireFormat._decodeName(name, decoder, True) self._content.append(name) decoder.finishNestedTlvs(endOffset)
def decodeEncryptedContent(self, encryptedContent, input): """ Decode input as an EncryptedContent in NDN-TLV and set the fields of the encryptedContent object. :param EncryptedContent encryptedContent: The EncryptedContent object whose fields are updated. :param input: The array with the bytes to decode. :type input: An array type with int elements """ decoder = TlvDecoder(input) endOffset = decoder.readNestedTlvsStart(Tlv.Encrypt_EncryptedContent) Tlv0_1_1WireFormat._decodeKeyLocator( Tlv.KeyLocator, encryptedContent.getKeyLocator(), decoder) encryptedContent.setAlgorithmType( decoder.readNonNegativeIntegerTlv(Tlv.Encrypt_EncryptionAlgorithm)) encryptedContent.setInitialVector( Blob(decoder.readOptionalBlobTlv (Tlv.Encrypt_InitialVector, endOffset), True)) encryptedContent.setPayload( Blob(decoder.readBlobTlv(Tlv.Encrypt_EncryptedPayload), True)) decoder.finishNestedTlvs(endOffset)
def decodeControlParameters(self, controlParameters, input): """ Decode input as an NDN-TLV ControlParameters and set the fields of the controlParameters object. :param ControlParameters controlParameters: The ControlParameters object whose fields are updated. :param input: The array with the bytes to decode. :type input: An array type with int elements """ controlParameters.clear() decoder = TlvDecoder(input) endOffset = decoder.readNestedTlvsStart( Tlv.ControlParameters_ControlParameters) # decode name if decoder.peekType(Tlv.Name, endOffset): name = Name() self._decodeName(name, decoder) controlParameters.setName(name) # decode face ID controlParameters.setFaceId( decoder.readOptionalNonNegativeIntegerTlv( Tlv.ControlParameters_FaceId, endOffset)) # decode URI if decoder.peekType(Tlv.ControlParameters_Uri, endOffset): uri = Blob( decoder.readOptionalBlobTlv(Tlv.ControlParameters_Uri, endOffset), False) controlParameters.setUri(str(uri)) # decode integers controlParameters.setLocalControlFeature( decoder.readOptionalNonNegativeIntegerTlv( Tlv.ControlParameters_LocalControlFeature, endOffset)) controlParameters.setOrigin( decoder.readOptionalNonNegativeIntegerTlv( Tlv.ControlParameters_Origin, endOffset)) controlParameters.setCost( decoder.readOptionalNonNegativeIntegerTlv( Tlv.ControlParameters_Cost, endOffset)) # set forwarding flags if decoder.peekType(Tlv.ControlParameters_Flags, endOffset): flags = ForwardingFlags() flags.setNfdForwardingFlags( decoder.readNonNegativeIntegerTlv(Tlv.ControlParameters_Flags)) controlParameters.setForwardingFlags(flags) # decode strategy if decoder.peekType(Tlv.ControlParameters_Strategy, endOffset): strategyEndOffset = decoder.readNestedTlvsStart( Tlv.ControlParameters_Strategy) self._decodeName(controlParameters.getStrategy(), decoder) decoder.finishNestedTlvs(strategyEndOffset) # decode expiration period controlParameters.setExpirationPeriod( decoder.readOptionalNonNegativeIntegerTlv( Tlv.ControlParameters_ExpirationPeriod, endOffset)) decoder.finishNestedTlvs(endOffset)