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 decodeSignatureInfoAndValue(self, signatureInfo, signatureValue): """ Decode signatureInfo as a signature info and signatureValue as the related SignatureValue, and return a new object which is a subclass of Signature. :param signatureInfo: The array with the signature info input buffer to decode. :type signatureInfo: An array type with int elements :param signatureValue: The array with the signature value input buffer to decode. :type signatureValue: An array type with int elements :return: A new object which is a subclass of Signature. :rtype: a subclass of Signature """ # Use a SignatureHolder to imitate a Data object for _decodeSignatureInfo. signatureHolder = self.SignatureHolder() decoder = TlvDecoder(signatureInfo) self._decodeSignatureInfo(signatureHolder, decoder) decoder = TlvDecoder(signatureValue) signatureHolder.getSignature().setSignature( Blob(decoder.readBlobTlv(Tlv.SignatureValue))) return signatureHolder.getSignature()
def decodeSignatureInfoAndValue(self, signatureInfo, signatureValue): """ Decode signatureInfo as a signature info and signatureValue as the related SignatureValue, and return a new object which is a subclass of Signature. :param signatureInfo: The array with the signature info input buffer to decode. :type signatureInfo: An array type with int elements :param signatureValue: The array with the signature value input buffer to decode. :type signatureValue: An array type with int elements :return: A new object which is a subclass of Signature. :rtype: a subclass of Signature """ # Use a SignatureHolder to imitate a Data object for _decodeSignatureInfo. signatureHolder = self.SignatureHolder() decoder = TlvDecoder(signatureInfo) self._decodeSignatureInfo(signatureHolder, decoder) decoder = TlvDecoder(signatureValue) # TODO: The library needs to handle other signature types than # SignatureSha256WithRsa. signatureHolder.getSignature().setSignature( Blob(decoder.readBlobTlv(Tlv.SignatureValue))) return signatureHolder.getSignature()
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 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 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 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 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 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)