Пример #1
0
    def test_01_key_compare(self):
        print("\n-----", sys._getframe().f_code.co_name, "-----")

        keypair1 = bbclib.KeyPair()
        keypair1.generate()

        pem1 = keypair1.get_private_key_in_pem()
        der1 = keypair1.get_private_key_in_der()

        keypair2 = bbclib.KeyPair()
        keypair2.mk_keyobj_from_private_key_pem(pem1)

        keypair3 = bbclib.KeyPair()
        keypair3.mk_keyobj_from_private_key_der(der1)

        privkey1 = keypair1.private_key
        pubkey1 = keypair1.public_key
        privkey2 = keypair2.private_key
        pubkey2 = keypair2.public_key
        privkey3 = keypair3.private_key
        pubkey3 = keypair3.public_key

        if isinstance(privkey1, bytes):
            assert privkey1 == privkey2
            assert privkey2 == privkey3
            assert pubkey1 == pubkey2
            assert pubkey2 == pubkey3
        else:
            assert bytes(privkey1) == bytes(privkey2)
            assert bytes(privkey2) == bytes(privkey3)
            assert bytes(pubkey1) == bytes(pubkey2)
            assert bytes(pubkey2) == bytes(pubkey3)
Пример #2
0
def init_keypair():
    keypair1 = bbclib.KeyPair()
    keypair2 = bbclib.KeyPair()
    if os.path.exists(os.path.join(this_directory, "../db/", "user1")):
        read_privatekey("user1", keypair1)
    else:
        create_privatekey("user1", keypair1)
    if os.path.exists(os.path.join(this_directory, "../db/", "user2")):
        read_privatekey("user2", keypair2)
    else:
        create_privatekey("user2", keypair2)
    return keypair1, keypair2
Пример #3
0
    def test_02_pem(self):
        print("\n-----", sys._getframe().f_code.co_name, "-----")
        pem1 = "-----BEGIN EC PRIVATE KEY-----\nMHcCAQEEIDSt1IOhS5ZmY6nkX/Wh7pT+Y45TmYxrwoc1pG72v387oAoGCCqGSM49\nAwEHoUQDQgAEdEsjD2i2LytHOjNxxc9PbFeqQ89aMLOfmdBbEoSOhZBukJ52EqQM\nhOdgHqyqD4hEyYxgDu3uIbKat+lEZEhb3Q==\n-----END EC PRIVATE KEY-----"
        keypair1 = bbclib.KeyPair()
        keypair1.mk_keyobj_from_private_key_pem(pem1)
        privkey1 = keypair1.private_key
        pem1_1 = keypair1.get_private_key_in_pem()
        pem1_1 = pem1_1.decode().rstrip()
        assert pem1 == pem1_1

        keypair2 = bbclib.KeyPair()
        keypair2.mk_keyobj_from_private_key(privkey1)
        pem2 = keypair2.get_private_key_in_pem()
        pem2 = pem2.decode().rstrip()
        assert pem1 == pem2
Пример #4
0
    def get_domain_keypair(self, domain_id):
        """Get domain_keys (private key and public key)

        Args:
            domain_id (bytes): target domain_id
        """
        keyconfig = self.config.get_config().get('domain_key', None)
        if keyconfig is None:
            self.domains[domain_id]['keypair'] = None
            return
        if 'use' not in keyconfig or not keyconfig['use']:
            return
        if 'directory' not in keyconfig or not os.path.exists(keyconfig['directory']):
            self.domains[domain_id]['keypair'] = None
            return
        domain_id_str = domain_id.hex()
        keypair = bbclib.KeyPair()
        try:
            with open(os.path.join(keyconfig['directory'], domain_id_str+".pem"), "r") as f:
                keypair.mk_keyobj_from_private_key_pem(f.read())
        except:
            self.domains[domain_id]['keypair'] = None
            return
        self.domains[domain_id].setdefault('keypair', dict())
        self.domains[domain_id]['keypair'].setdefault('keys', list())
        self.domains[domain_id]['keypair']['keys'].insert(0, keypair)
        timer = self.domains[domain_id]['keypair'].setdefault('timer', None)
        if timer is None or not timer.active:
            self.domains[domain_id]['keypair']['timer'] = query_management.QueryEntry(
                expire_after=keyconfig['obsolete_timeout'],
                data={KeyType.domain_id: domain_id}, callback_expire=self._delete_obsoleted_domain_keys)
        else:
            timer.update_expiration_time(keyconfig['obsolete_timeout'])
        self.domains[domain_id]['keypair']['keys'].insert(0, keypair)
Пример #5
0
    def test_03_keyid(self):
        print("\n-----", sys._getframe().f_code.co_name, "-----")
        pem1 = "-----BEGIN EC PRIVATE KEY-----\nMHcCAQEEIDSt1IOhS5ZmY6nkX/Wh7pT+Y45TmYxrwoc1pG72v387oAoGCCqGSM49\nAwEHoUQDQgAEdEsjD2i2LytHOjNxxc9PbFeqQ89aMLOfmdBbEoSOhZBukJ52EqQM\nhOdgHqyqD4hEyYxgDu3uIbKat+lEZEhb3Q==\n-----END EC PRIVATE KEY-----"
        keypair1 = bbclib.KeyPair()
        keypair1.mk_keyobj_from_private_key_pem(pem1)
        keyid1 = keypair1.get_key_id()

        pubkey = keypair1.get_public_key_in_pem()
        obj = JsonWebKey.import_key(pubkey, {'kty': 'EC'})
        obj["kty"] = "EC"
        json_obj = json.dumps(obj, separators=(',', ':'), sort_keys=True)
        keyid2 = hashlib.sha256(json_obj.encode()).digest()
        assert keyid1 == keyid2
Пример #6
0
    def set_node_key(self, pem_file=None):
        """Set node_key to this client

        Args:
            pem_file (str): path string for the pem file
        """
        if pem_file is None:
            self.node_keypair = None
        try:
            self.node_keypair = bbclib.KeyPair()
            with open(pem_file, "r") as f:
                self.node_keypair.mk_keyobj_from_private_key_pem(f.read())
        except:
            return
Пример #7
0
# See the License for the specific language governing permissions and
# limitations under the License.
#

import bbclib
import os
import common
this_directory = os.path.dirname(os.path.abspath(__file__))

user_id1 = common.user_id1
user_id2 = common.user_id2
asset_group_id1 = common.asset_group_id1
asset_group_id2 = common.asset_group_id2
domain_id = common.domain_id

keypair1 = bbclib.KeyPair()
keypair2 = bbclib.KeyPair()


def make_transactions(id_len_conf=None, idlen=None, no_pubkey=False):
    transactions = list()
    if id_len_conf is not None:
        bbclib.configure_id_length(id_len_conf)
    elif idlen is not None:
        bbclib.configure_id_length_all(idlen)

    transactions.append(bbclib.make_transaction(relation_num=1, event_num=1, witness=True, version=2))
    bbclib.add_relation_asset(transactions[0], relation_idx=0, asset_group_id=asset_group_id1,
                              user_id=user_id1, asset_body=b'relation:asset_0-0')
    bbclib.add_event_asset(transactions[0], event_idx=0, asset_group_id=asset_group_id1,
                           user_id=user_id1, asset_body=b'event:asset_0-0')
Пример #8
0
    def test_02_verify(self):
        print("\n-----", sys._getframe().f_code.co_name, "-----")

        keypair1 = bbclib.KeyPair()
        keypair1.mk_keyobj_from_private_key_pem(pem1)
        keypair2 = bbclib.KeyPair()
        keypair2.mk_keyobj_from_private_key_pem(pem2)

        bbclib.configure_id_length_all(32)
        transactions = list()
        txids = list()
        for txdata in txdata_list:
            txobj, _ = bbclib.deserialize(txdata)
            transactions.append(txobj)
            txids.append(txobj.transaction_id)

        for idx in range(len(transactions)):
            txobj = transactions[idx]
            if idx % 20 == 0:
                assert len(txobj.transaction_id) == id_conf["transaction_id"]
                assert len(txobj.relations) == 1
                assert len(txobj.events) == 1
                assert txobj.transaction_id == txids[idx]
                assert txobj.relations[0].asset_group_id == asset_group_id1[:id_conf["asset_group_id"]]
                assert txobj.relations[0].asset.user_id == user_id1[:id_conf["user_id"]]
                assert txobj.relations[0].asset.asset_body == b'relation:asset_0-0'
                assert len(txobj.relations[0].asset.nonce) == id_conf["nonce"]
                assert txobj.events[0].asset_group_id == asset_group_id1[:id_conf["asset_group_id"]]
                assert txobj.events[0].mandatory_approvers[0] == user_id1[:id_conf["user_id"]]
                assert txobj.events[0].asset.user_id == user_id1[:id_conf["user_id"]]
                assert txobj.events[0].asset.asset_body == b'event:asset_0-0'
                assert len(txobj.events[0].asset.nonce) == id_conf["nonce"]
                assert len(txobj.witness.user_ids) == 1
                assert len(txobj.witness.sig_indices) == 1
                assert len(txobj.signatures) == 1
                if idx < 20:
                    assert txobj.signatures[0].verify(txobj.digest())
                else:
                    #print("no pubkey")
                    pass

            else:
                assert len(txobj.transaction_id) == id_conf["transaction_id"]
                assert len(txobj.relations) == 4
                assert len(txobj.relations[0].pointers) == 1
                assert len(txobj.relations[1].pointers) == 2
                assert len(txobj.events) == 1
                assert len(txobj.references) == 1
                assert txobj.relations[0].asset_group_id == asset_group_id1[:id_conf["asset_group_id"]]
                assert txobj.relations[0].asset.user_id == user_id1[:id_conf["user_id"]]
                assert txobj.relations[0].asset.asset_body == b'relation:asset_1-%d' % (idx % 20)
                assert len(txobj.relations[0].asset.nonce) == id_conf["nonce"]
                assert len(txobj.relations[0].pointers[0].transaction_id) == id_conf["transaction_id"]
                assert len(txobj.relations[0].pointers[0].asset_id) == id_conf["asset_id"]
                assert txobj.relations[0].pointers[0].transaction_id == transactions[idx-1].transaction_id
                assert txobj.relations[0].pointers[0].asset_id == transactions[idx-1].relations[0].asset.asset_id
                assert txobj.relations[1].asset_group_id == asset_group_id2[:id_conf["asset_group_id"]]
                assert txobj.relations[1].asset.user_id == user_id2[:id_conf["user_id"]]
                assert txobj.relations[1].asset.asset_body == b'relation:asset_2-%d' % (idx % 20)
                assert len(txobj.relations[1].asset.nonce) == id_conf["nonce"]
                assert len(txobj.relations[1].pointers[0].transaction_id) == id_conf["transaction_id"]
                assert len(txobj.relations[1].pointers[0].asset_id) == id_conf["asset_id"]
                assert txobj.relations[1].pointers[0].transaction_id == transactions[idx-1].transaction_id
                assert txobj.relations[1].pointers[0].asset_id == transactions[idx-1].relations[0].asset.asset_id
                assert len(txobj.relations[1].pointers[1].transaction_id) == id_conf["transaction_id"]
                assert len(txobj.relations[1].pointers[1].asset_id) == id_conf["asset_id"]

                assert txobj.relations[2].asset_group_id == asset_group_id1[:id_conf["asset_group_id"]]
                assert txobj.relations[2].asset_raw.asset_body == b'relation:asset_4-%d' % (idx % 20)
                assert len(txobj.relations[2].pointers[0].transaction_id) == id_conf["transaction_id"]
                assert len(txobj.relations[2].pointers[0].asset_id) == id_conf["asset_id"]
                assert len(txobj.relations[2].pointers[1].transaction_id) == id_conf["transaction_id"]
                assert txobj.relations[2].pointers[1].asset_id is None

                assert txobj.relations[3].asset_group_id == asset_group_id2[:id_conf["asset_group_id"]]
                assert len(txobj.relations[3].asset_hash.asset_ids) == 4
                assert len(txobj.relations[3].pointers[0].transaction_id) == id_conf["transaction_id"]
                assert len(txobj.relations[3].pointers[0].asset_id) == id_conf["asset_id"]

                if idx < 20:
                    assert txobj.relations[1].pointers[1].transaction_id == transactions[0].transaction_id
                    assert txobj.relations[1].pointers[1].asset_id == transactions[0].relations[0].asset.asset_id
                    assert txobj.relations[2].pointers[0].transaction_id == transactions[0].transaction_id
                    assert txobj.relations[2].pointers[0].asset_id == transactions[0].relations[0].asset.asset_id
                    assert txobj.relations[2].pointers[1].transaction_id == transactions[0].transaction_id
                    assert txobj.relations[3].pointers[0].transaction_id == transactions[0].transaction_id
                    assert txobj.relations[3].pointers[0].asset_id == transactions[0].relations[0].asset.asset_id
                else:
                    assert txobj.relations[1].pointers[1].transaction_id == transactions[20].transaction_id
                    assert txobj.relations[1].pointers[1].asset_id == transactions[20].relations[0].asset.asset_id
                    assert txobj.relations[2].pointers[0].transaction_id == transactions[20].transaction_id
                    assert txobj.relations[2].pointers[0].asset_id == transactions[20].relations[0].asset.asset_id
                    assert txobj.relations[2].pointers[1].transaction_id == transactions[20].transaction_id
                    assert txobj.relations[3].pointers[0].transaction_id == transactions[20].transaction_id
                    assert txobj.relations[3].pointers[0].asset_id == transactions[20].relations[0].asset.asset_id

                assert txobj.events[0].asset_group_id == asset_group_id1[:id_conf["asset_group_id"]]
                assert txobj.events[0].mandatory_approvers[0] == user_id1[:id_conf["user_id"]]
                assert txobj.events[0].asset.user_id == user_id2[:id_conf["user_id"]]
                assert txobj.events[0].asset.asset_body == b'event:asset_3-%d' % (idx % 20)
                assert len(txobj.events[0].asset.nonce) == id_conf["nonce"]
                assert txobj.references[0].asset_group_id == asset_group_id1[:id_conf["asset_group_id"]]
                assert txobj.references[0].event_index_in_ref == 0
                assert len(txobj.references[0].sig_indices) == 1
                assert txobj.cross_ref.domain_id == domain_id
                if idx < 20:
                    assert txobj.cross_ref.transaction_id == transactions[0].transaction_id
                else:
                    assert txobj.cross_ref.transaction_id == transactions[20].transaction_id
                assert len(txobj.witness.user_ids) == 2
                assert len(txobj.witness.sig_indices) == 2
                assert len(txobj.signatures) == 2
                if txobj.signatures[0].pubkey is None:
                    pass
                else:
                    assert txobj.signatures[0].verify(txobj.digest())
                if txobj.signatures[1].pubkey is None:
                    pass
                else:
                    assert txobj.signatures[1].verify(txobj.digest())