Example #1
0
    def __init__(self,
                 name=None,
                 payload_type=None,
                 payload=None,
                 expiry_time=None):
        ccnpy.TlvType.__init__(self)
        if name is not None:
            if not isinstance(name, ccnpy.Name):
                raise TypeError("Name must be of type ccnpy.Name")

        if payload is not None:
            if not isinstance(payload, ccnpy.Payload):
                raise TypeError("Payload must be of type ccnpy.Payload")

        if payload_type is not None:
            if not isinstance(payload_type, ccnpy.PayloadType):
                raise TypeError(
                    "PayloadType must be of type ccnpy.PayloadType")

        self._name = name
        self._payload_type = payload_type
        self._payload = payload
        self._expiry_time = expiry_time
        self._tlv = ccnpy.Tlv(
            self.class_type(),
            [self._name, self._expiry_time, self._payload_type, self._payload])
Example #2
0
    def __init__(self, key_number, iv, mode):
        """

        :param key_number: An integer
        :param iv: A byte array
        :param mode: One of the allowed modes (use a class create_x method to create)
        """
        ccnpy.flic.SecurityCtx.__init__(self)
        self._key_number = key_number
        self._iv = iv
        self._mode = mode

        key_tlv = ccnpy.Tlv(self.__T_KEYNUM, ccnpy.Tlv.number_to_array(self._key_number))
        iv_tlv = ccnpy.Tlv(self.__T_IV, self._iv)
        mode_tlv = ccnpy.Tlv.create_uint8(self.__T_MODE, self._mode)

        self._tlv = ccnpy.Tlv(ccnpy.flic.SecurityCtx.class_type(),
                              ccnpy.Tlv(self.class_type(), [key_tlv, iv_tlv, mode_tlv]))
Example #3
0
    def __init__(self, digest):
        ccnpy.TlvType.__init__(self)

        if digest is None:
            raise ValueError("digest must not be None")
        if not isinstance(digest, ccnpy.HashValue):
            raise TypeError("digest must be ccnpy.HashValue")

        self._digest = digest
        self._tlv = ccnpy.Tlv(self.class_type(), self._digest)
Example #4
0
    def __init__(self, subtree_size=None, subtree_digest=None, leaf_size=None, locators=None):
        """

        :param subtree_size: Total application bytes in direct or indirect pointers
        :param subtree_digest:
        :param leaf_size: Total application bytes in direct pointers
        :param locators:
        """
        ccnpy.TlvType.__init__(self)

        if subtree_size is not None and not isinstance(subtree_size, ccnpy.flic.SubtreeSize):
            raise TypeError("subtree_size, if present, must be ccnpy.flic.SubtreeSize")

        if subtree_digest is not None and not isinstance(subtree_digest, ccnpy.HashValue):
            raise TypeError("subtree_digest, if present, must be ccnpy.HashValue")

        if leaf_size is not None:
            raise RuntimeError("Not Implemented")

        if locators is not None and not isinstance(locators, ccnpy.flic.LocatorList):
            raise TypeError("locators, if present, must be ccnpy.flic.LocatorList")

        self._subtree_size = subtree_size
        self._subtree_digest = subtree_digest
        self._locators = locators

        tlvs = []
        if self._subtree_size is not None:
            tlvs.append(subtree_size)

        if self._subtree_digest is not None:
            tlvs.append(ccnpy.Tlv(GroupData.__subtree_digest_type, self._subtree_digest))

        if self._locators is not None:
            tlvs.append(self._locators)

        self._tlv = ccnpy.Tlv(self.class_type(), tlvs)
Example #5
0
    def decrypt_node(self, security_ctx, encrypted_node, auth_tag):
        """
        Example:
            manifest = ccnpy.flic.Manifest.deserialize(payload.value())
            if isinstance(manifest.security_ctx(), ccnpy.flic.PresharedKeyCtx):
                # keystore is not necessarily provided
                key = keystore.get(manifest.security_ctx().key_number())
                psk = ccnpy.flic.PresharedKey(key)
                node = psk.decrypt_node(manifest.security_ctx(),
                                        manifest.node(),
                                        manifest.auth_tag())

        :param security_ctx: A ccnpy.flic.PresharedKeyCtx
        :param encrypted_node: A ccnpy.flic.EncryptedNode
        :param auth_tag: A ccnpy.flic.AuthTag
        :return: a ccnpy.flic.Node
        """

        if not isinstance(encrypted_node, ccnpy.flic.EncryptedNode):
            raise TypeError("encrypted_node must be ccnpy.flic.EncryptedNode")
        if security_ctx is None:
            raise ValueError("security context must not be None")
        if not isinstance(security_ctx,
                          ccnpy.flic.presharedkey.PresharedKeyCtx):
            raise TypeError(
                "security_ctx must be a ccnpy.flic.PresharedKeyCtx")
        if auth_tag is None:
            raise ValueError("auth_tag must not be None")
        if not isinstance(auth_tag, ccnpy.flic.AuthTag):
            raise TypeError("auth_tag must be ccnpy.flic.AuthTag")

        if security_ctx.key_number() != self._key_number:
            raise ValueError(
                "security_ctx.key_number %r != our key_number %r" %
                (security_ctx.key_number(), self._key_number))

        plaintext = self._key.decrypt(nonce=security_ctx.iv(),
                                      ciphertext=encrypted_node.value(),
                                      associated_data=security_ctx.serialize(),
                                      auth_tag=auth_tag.value())

        node_tlv = ccnpy.Tlv(ccnpy.flic.Node.class_type(), plaintext)
        node = ccnpy.flic.Node.parse(node_tlv)
        return node