예제 #1
0
파일: member.py 프로젝트: csko/Tribler
    def __init__(self, public_key, ec=None, sync_with_database=True):
        """
        Create a new Member instance.  Member instances must be reated or retrieved using
        has_instance or get_instance.

        PUBLIC_KEY must be a string giving the public EC key in DER format.  EC is an optional EC
        object (given when created from private key).
        """
        assert isinstance(public_key, str)
        assert not public_key.startswith("-----BEGIN")
        assert isinstance(sync_with_database, bool)
        self._public_key = public_key
        if ec is None:
            self._ec = ec_from_public_bin(public_key)
        else:
            self._ec = ec

        self._signature_length = ec_signature_length(self._ec)
        self._mid = sha1(public_key).digest()

        self._database_id = -1
        self._address = ("", -1)
        self._tags = []

        # sync with database
        if sync_with_database:
            if not self.update():
                database = DispersyDatabase.get_instance()
                database.execute(u"INSERT INTO user(mid, public_key) VALUES(?, ?)", (buffer(self._mid), buffer(self._public_key)))
                self._database_id = database.last_insert_rowid
예제 #2
0
def GetKey(name):
	''' Get the associated key for this user '''
	for x,y in m.keytable.items():
		if x == name:
			key = y
			z = m.keypair
			biny = binascii.unhexlify(y)
			newy = crypto.ec_from_public_bin(biny)
			z_shared = z.compute_dh_key(newy)
			return z_shared
	return None
예제 #3
0
 def __init__(self, public_key, ec = None, sync_with_database = True):
     self._public_key = public_key
     if ec is None:
         self._ec = ec_from_public_bin(public_key)
     else:
         self._ec = ec
     self._signature_length = ec_signature_length(self._ec)
     self._mid = sha1(public_key).digest()
     self._database_id = -1
     self._address = ('', -1)
     self._tags = []
     if sync_with_database:
         if not self.update():
             database = DispersyDatabase.get_instance()
             database.execute(u'INSERT INTO user(mid, public_key) VALUES(?, ?)', (buffer(self._mid), buffer(self._public_key)))
             self._database_id = database.last_insert_rowid
예제 #4
0
 def __init__(self, public_key, ec = None, sync_with_database = True):
     self._public_key = public_key
     if ec is None:
         self._ec = ec_from_public_bin(public_key)
     else:
         self._ec = ec
     self._signature_length = ec_signature_length(self._ec)
     self._mid = sha1(public_key).digest()
     self._database_id = -1
     self._address = ('', -1)
     self._tags = []
     if sync_with_database:
         if not self.update():
             database = DispersyDatabase.get_instance()
             database.execute(u'INSERT INTO user(mid, public_key) VALUES(?, ?)', (buffer(self._mid), buffer(self._public_key)))
             self._database_id = database.last_insert_rowid
예제 #5
0
파일: member.py 프로젝트: duy/dispersy
    def __init__(self, public_key, private_key=""):
        """
        Create a new Member instance.
        """
        assert isinstance(public_key, str)
        assert isinstance(private_key, str)
        assert ec_check_public_bin(public_key), public_key.encode("HEX")
        assert private_key == "" or ec_check_private_bin(private_key), private_key.encode("HEX")

        if not hasattr(self, "_public_key"):
            assert DispersyDatabase.has_instance(), "DispersyDatabase has not yet been created"
            database = DispersyDatabase.get_instance()

            try:
                database_id, mid, tags, private_key_from_db = database.execute(
                    u"SELECT m.id, m.mid, m.tags, p.private_key FROM member AS m LEFT OUTER JOIN private_key AS p ON p.member = m.id WHERE m.public_key = ? LIMIT 1",
                    (buffer(public_key),),
                ).next()

            except StopIteration:
                mid = sha1(public_key).digest()
                private_key_from_db = None
                try:
                    database_id, tags = database.execute(
                        u"SELECT id, tags FROM member WHERE mid = ? LIMIT 1", (buffer(mid),)
                    ).next()

                except StopIteration:
                    database.execute(
                        u"INSERT INTO member (mid, public_key) VALUES (?, ?)", (buffer(mid), buffer(public_key))
                    )
                    database_id = database.last_insert_rowid
                    tags = u""

                else:
                    database.execute(
                        u"UPDATE member SET public_key = ? WHERE id = ?", (buffer(public_key), database_id)
                    )

            else:
                mid = str(mid)
                private_key_from_db = str(private_key_from_db) if private_key_from_db else ""
                assert private_key_from_db == "" or ec_check_private_bin(
                    private_key_from_db
                ), private_key_from_db.encode("HEX")

            if private_key_from_db:
                private_key = private_key_from_db
            elif private_key:
                database.execute(
                    u"INSERT INTO private_key (member, private_key) VALUES (?, ?)", (database_id, buffer(private_key))
                )

            self._database = database
            self._database_id = database_id
            self._mid = mid
            self._public_key = public_key
            self._private_key = private_key
            self._ec = ec_from_private_bin(private_key) if private_key else ec_from_public_bin(public_key)
            self._signature_length = ec_signature_length(self._ec)
            self._tags = [tag for tag in tags.split(",") if tag]
            self._has_identity = set()

            if __debug__:
                assert len(set(self._tags)) == len(self._tags), ("there are duplicate tags", self._tags)
                for tag in self._tags:
                    assert tag in (u"store", u"ignore", u"blacklist"), tag

            if __debug__:
                dprint(
                    "mid:",
                    self._mid.encode("HEX"),
                    " db:",
                    self._database_id,
                    " public:",
                    bool(self._public_key),
                    " private:",
                    bool(self._private_key),
                )
예제 #6
0
    def __init__(self, public_key, private_key="", sync_with_database=True, public_key_available=True):
        """
        Create a new Member instance.  Member instances must be reated or retrieved using
        has_instance or get_instance.

        To create a Member instance we either need a public_key or a mid.  If only the mid is
        available it must be given as the public_key parameter and public_key_available must be
        False.  In this case the member will be unable to verify or sign data.

        Also not that it is possible, however unlikely, that someone is able to find another member
        with the same mid.  This will cause conflicts until the public_key is available.
        """
        assert isinstance(public_key, str)
        assert isinstance(private_key, str)
        assert isinstance(sync_with_database, bool)
        assert isinstance(public_key_available, bool)
        assert (public_key_available and len(public_key) > 0 and not public_key.startswith("-----BEGIN")) or \
               (not public_key_available and len(public_key) == 20), (len(public_key), public_key_available, public_key.encode("HEX"))
        assert (public_key_available and len(private_key) > 0 and not private_key.startswith("-----BEGIN")) or len(private_key) == 0

        assert DispersyDatabase.has_instance(), "DispersyDatabase has not yet been created"
        database = DispersyDatabase.get_instance()

        if hasattr(self, "_database_id"):
            #
            # singleton already exists.  we may have received a public or private key now, so update
            # those in the database is needed
            #
            if __debug__: dprint("continue with existing singleton", force=1)

            if public_key_available:
                assert self._public_key == "" or self._public_key == public_key
                assert self._private_key == "" or self._private_key == private_key

                if not self._public_key:
                    assert public_key
                    assert ec_check_public_bin(public_key), public_key.encode("HEX")
                    self._public_key = public_key
                    if sync_with_database:
                        database.execute(u"UPDATE member SET public_key = ? WHERE id = ?", (buffer(public_key), self._database_id))

                if not self._private_key and private_key:
                    assert private_key
                    assert ec_check_private_bin(private_key), private_key.encode("HEX")
                    self._private_key = private_key
                    if sync_with_database:
                        database.execute(u"UPDATE private_key SET private_key = ? WHERE member = ?", (buffer(private_key), self._database_id))

                self._ec = ec_from_private_bin(self._private_key) if self._private_key else ec_from_public_bin(self._public_key)
                self._signature_length = ec_signature_length(self._ec)

            else:
                # we have nothing new
                pass

        else:
            #
            # singleton did not exist.  we make a new one
            #
            if public_key_available:
                assert public_key
                assert ec_check_public_bin(public_key), public_key.encode("HEX")
                assert not private_key or ec_check_private_bin(private_key), private_key.encode("HEX")
                self._public_key = public_key
                self._private_key = private_key
                self._mid = sha1(public_key).digest()
                self._database_id = -1
                self._communities = set()
                self._tags = []

                if sync_with_database:
                    try:
                        self._database_id, private_key = database.execute(u"SELECT m.id, p.private_key FROM member AS m LEFT OUTER JOIN private_key AS p ON p.member = m.id WHERE m.public_key = ? LIMIT 1",
                                                                 (buffer(self._public_key),)).next()
                    except StopIteration:
                        # TODO check if there is a member already in the database where we only had
                        # the MID

                        database.execute(u"INSERT INTO member(mid, public_key) VALUES(?, ?)", (buffer(self._mid), buffer(self._public_key)))
                        self._database_id = database.last_insert_rowid

                        if self._private_key:
                            database.execute(u"INSERT INTO private_key(member, private_key) VALUES(?, ?)", (self._database_id, buffer(self._private_key)))

                    else:
                        if not self._private_key and private_key:
                            self._private_key = str(private_key)

                self._ec = ec_from_private_bin(self._private_key) if self._private_key else ec_from_public_bin(self._public_key)
                self._signature_length = ec_signature_length(self._ec)

            else:
                assert len(public_key) == 20, public_key.encode("HEX")
                self._public_key = ""
                self._private_key = ""
                self._mid = public_key
                self._database_id = -1
                self._communities = set()
                self._tags = []

                if sync_with_database:
                    try:
                        # # TODO do something smart to select the right mid (multiple can exist...)
                        self._database_id, = database.execute(u"SELECT id FROM member WHERE mid = ? LIMIT 1",
                                                              (buffer(self._mid),)).next()
                    except StopIteration:
                        database.execute(u"INSERT INTO member(mid) VALUES(?)", (buffer(self._mid),))
                        self._database_id = database.last_insert_rowid

                self._ec = None
                self._signature_length = 0

        if sync_with_database:
            self.update()

        if __debug__: dprint("mid:", self._mid.encode("HEX"), " db:", self._database_id, " public:", bool(self._public_key), " private:", bool(self._private_key), " from-public:", public_key_available)
예제 #7
0
    def __init__(self, public_key, private_key=""):
        """
        Create a new Member instance.
        """
        assert isinstance(public_key, str)
        assert isinstance(private_key, str)
        assert ec_check_public_bin(public_key), public_key.encode("HEX")
        assert private_key == "" or ec_check_private_bin(
            private_key), private_key.encode("HEX")

        if not hasattr(self, "_public_key"):
            assert DispersyDatabase.has_instance(
            ), "DispersyDatabase has not yet been created"
            database = DispersyDatabase.get_instance()

            try:
                database_id, mid, tags, private_key_from_db = database.execute(
                    u"SELECT m.id, m.mid, m.tags, p.private_key FROM member AS m LEFT OUTER JOIN private_key AS p ON p.member = m.id WHERE m.public_key = ? LIMIT 1",
                    (buffer(public_key), )).next()

            except StopIteration:
                mid = sha1(public_key).digest()
                private_key_from_db = None
                try:
                    database_id, tags = database.execute(
                        u"SELECT id, tags FROM member WHERE mid = ? LIMIT 1",
                        (buffer(mid), )).next()

                except StopIteration:
                    database.execute(
                        u"INSERT INTO member (mid, public_key) VALUES (?, ?)",
                        (buffer(mid), buffer(public_key)))
                    database_id = database.last_insert_rowid
                    tags = u""

                else:
                    database.execute(
                        u"UPDATE member SET public_key = ? WHERE id = ?",
                        (buffer(public_key), database_id))

            else:
                mid = str(mid)
                private_key_from_db = str(
                    private_key_from_db) if private_key_from_db else ""
                assert private_key_from_db == "" or ec_check_private_bin(
                    private_key_from_db), private_key_from_db.encode("HEX")

            if private_key_from_db:
                private_key = private_key_from_db
            elif private_key:
                database.execute(
                    u"INSERT INTO private_key (member, private_key) VALUES (?, ?)",
                    (database_id, buffer(private_key)))

            self._database = database
            self._database_id = database_id
            self._mid = mid
            self._public_key = public_key
            self._private_key = private_key
            self._ec = ec_from_private_bin(
                private_key) if private_key else ec_from_public_bin(public_key)
            self._signature_length = ec_signature_length(self._ec)
            self._tags = [tag for tag in tags.split(",") if tag]
            self._has_identity = set()

            if __debug__:
                assert len(set(self._tags)) == len(
                    self._tags), ("there are duplicate tags", self._tags)
                for tag in self._tags:
                    assert tag in (u"store", u"ignore", u"blacklist"), tag

            if __debug__:
                dprint("mid:", self._mid.encode("HEX"),
                       " db:", self._database_id, " public:",
                       bool(self._public_key), " private:",
                       bool(self._private_key))