Search in sources :

Example 1 with BftsmartSessionCredential

use of com.jd.blockchain.consensus.bftsmart.BftsmartSessionCredential in project jdchain-core by blockchain-jd-com.

the class BftsmartClientAuthencationService method authencateIncoming.

@Override
public BftsmartClientIncomingSettings authencateIncoming(ClientCredential clientCredential) {
    if (!verify(clientCredential)) {
        return null;
    }
    BftsmartTopology topology = nodeServer.getTopology();
    if (topology == null) {
        throw new IllegalStateException("Topology of node[" + nodeServer.getId() + "] still not created !!!");
    }
    BftsmartClientIncomingConfig clientIncomingSettings = new BftsmartClientIncomingConfig();
    clientIncomingSettings.setTopology(BinarySerializeUtils.serialize(topology));
    clientIncomingSettings.setTomConfig(BinarySerializeUtils.serialize(nodeServer.getTomConfig()));
    clientIncomingSettings.setViewSettings(nodeServer.getConsensusSetting());
    clientIncomingSettings.setPubKey(clientCredential.getPubKey());
    BftsmartSessionCredential sessionCredential = (BftsmartSessionCredential) clientCredential.getSessionCredential();
    // 如果历史会话凭证的客户端ID是小于全局的最小客户端ID,则是无效的客户端ID,对其重新分配;
    // 注:忽略历史会话凭证的客户端ID不属于当前节点的分配空间的情形,此种情形是由于该客户端是从其它共识节点重定向过来的,
    // 应该继续维持该客户端的 ID 复用;
    int clientId = sessionCredential.getClientId();
    int clientIdRange = sessionCredential.getClientIdRange();
    if (clientIdRange < 1 || clientIdRange > POOL_SIZE_PEER_CLIENT) {
        clientIdRange = POOL_SIZE_PEER_CLIENT;
    }
    if (clientId < GLOBAL_MIN_CLIENT_ID) {
        // 重新分配
        clientId = allocateClientId(clientIdRange);
    }
    sessionCredential = new BftsmartSessionCredentialConfig(clientId, clientIdRange, System.currentTimeMillis());
    clientIncomingSettings.setSessionCredential(sessionCredential);
    return clientIncomingSettings;
}
Also used : BftsmartTopology(com.jd.blockchain.consensus.bftsmart.BftsmartTopology) BftsmartClientIncomingConfig(com.jd.blockchain.consensus.bftsmart.BftsmartClientIncomingConfig) BftsmartSessionCredential(com.jd.blockchain.consensus.bftsmart.BftsmartSessionCredential) BftsmartSessionCredentialConfig(com.jd.blockchain.consensus.bftsmart.client.BftsmartSessionCredentialConfig)

Example 2 with BftsmartSessionCredential

use of com.jd.blockchain.consensus.bftsmart.BftsmartSessionCredential in project jdchain-core by blockchain-jd-com.

the class BftsmartConsensusClientFactory method buildCredential.

@Override
public BftsmartClientAuthCredit buildCredential(SessionCredential sessionCredential, AsymmetricKeypair clientKeyPair, X509Certificate gatewayCertificate) {
    if (sessionCredential == null) {
        sessionCredential = BftsmartSessionCredentialConfig.createEmptyCredential();
    } else if (!(sessionCredential instanceof BftsmartSessionCredential)) {
        throw new IllegalArgumentException("Illegal credential info type! Requrie [" + BftsmartSessionCredential.class.getName() + "] but it is [" + sessionCredential.getClass().getName() + "]!");
    }
    PubKey pubKey = clientKeyPair.getPubKey();
    PrivKey privKey = clientKeyPair.getPrivKey();
    SignatureFunction signatureFunction = Crypto.getSignatureFunction(pubKey.getAlgorithm());
    byte[] credentialBytes = BinaryProtocol.encode(sessionCredential, BftsmartSessionCredential.class);
    SignatureDigest signatureDigest = signatureFunction.sign(privKey, credentialBytes);
    BftsmartClientAuthCredit bftsmartClientAuthCredential = new BftsmartClientAuthCredit();
    bftsmartClientAuthCredential.setSessionCredential((BftsmartSessionCredential) sessionCredential);
    bftsmartClientAuthCredential.setPubKey(pubKey);
    bftsmartClientAuthCredential.setSignatureDigest(signatureDigest);
    bftsmartClientAuthCredential.setCertificate(null != gatewayCertificate ? CertificateUtils.toPEMString(gatewayCertificate) : null);
    return bftsmartClientAuthCredential;
}
Also used : PubKey(com.jd.blockchain.crypto.PubKey) BftsmartClientAuthCredit(com.jd.blockchain.consensus.bftsmart.BftsmartClientAuthCredit) SignatureFunction(com.jd.blockchain.crypto.SignatureFunction) SignatureDigest(com.jd.blockchain.crypto.SignatureDigest) BftsmartSessionCredential(com.jd.blockchain.consensus.bftsmart.BftsmartSessionCredential) PrivKey(com.jd.blockchain.crypto.PrivKey)

Example 3 with BftsmartSessionCredential

use of com.jd.blockchain.consensus.bftsmart.BftsmartSessionCredential in project jdchain-core by blockchain-jd-com.

the class GatewayConsensusClientManager method isCredentialUpated.

private boolean isCredentialUpated(ConsensusClient client, SessionCredential sessionCredential) {
    if (client instanceof BftsmartConsensusClient && sessionCredential instanceof BftsmartSessionCredential) {
        BftsmartConsensusClient bftsmartClient = (BftsmartConsensusClient) client;
        BftsmartSessionCredential newCredential = (BftsmartSessionCredential) sessionCredential;
        BftsmartSessionCredential oldCredential = bftsmartClient.getSettings().getSessionCredential();
        // clientId 和 clientIdRange 任何一个有差异,都表示凭证已更新;
        return oldCredential.getClientId() != newCredential.getClientId() || oldCredential.getClientIdRange() != newCredential.getClientIdRange();
    } else if (client instanceof RaftConsensusClient) {
        // return !((RaftConsensusClient)client).isInit();
        return !((RaftConsensusClient) client).isConnected();
    }
    return true;
}
Also used : RaftConsensusClient(com.jd.blockchain.consensus.raft.client.RaftConsensusClient) BftsmartConsensusClient(com.jd.blockchain.consensus.bftsmart.client.BftsmartConsensusClient) BftsmartSessionCredential(com.jd.blockchain.consensus.bftsmart.BftsmartSessionCredential)

Aggregations

BftsmartSessionCredential (com.jd.blockchain.consensus.bftsmart.BftsmartSessionCredential)3 BftsmartClientAuthCredit (com.jd.blockchain.consensus.bftsmart.BftsmartClientAuthCredit)1 BftsmartClientIncomingConfig (com.jd.blockchain.consensus.bftsmart.BftsmartClientIncomingConfig)1 BftsmartTopology (com.jd.blockchain.consensus.bftsmart.BftsmartTopology)1 BftsmartConsensusClient (com.jd.blockchain.consensus.bftsmart.client.BftsmartConsensusClient)1 BftsmartSessionCredentialConfig (com.jd.blockchain.consensus.bftsmart.client.BftsmartSessionCredentialConfig)1 RaftConsensusClient (com.jd.blockchain.consensus.raft.client.RaftConsensusClient)1 PrivKey (com.jd.blockchain.crypto.PrivKey)1 PubKey (com.jd.blockchain.crypto.PubKey)1 SignatureDigest (com.jd.blockchain.crypto.SignatureDigest)1 SignatureFunction (com.jd.blockchain.crypto.SignatureFunction)1