Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .ci/ci_check_commit.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
set -e

scan_code_script="cobra/cobra.py -f json -o /tmp/report.json -t "
ignore_files=(sh crt key json toml SignatureTest.java Ok.java bin abi ChannelTest.java ParallelOkDemo.java PerformanceAmop.java DagPrecompiledDemo.java KeyManagerTest.java CryptoInterface.java AmopMsgHandlerTest.java TopicManager.java PrivateTopicVerifyTest.java AmopMsgBuilder.java TopicManagerTest.java AmopSubscribe.java AmopPublisher.java AmopPublisherPrivate.java AmopSubscribePrivate.java AmopPublisherFile.java AmopPublisherPrivateFile.java DemoAmopCallback.java FileToByteArrayHelper.java OkD.java TableTest.java PerformanceTable.java HelloWorld.java PerformanceRPC.java CodecTest.java ResponseTest.java ConfigTest.java)
ignore_files=(sh crt key json toml SignatureTest.java Ok.java bin abi ChannelTest.java ParallelOkDemo.java PerformanceAmop.java DagPrecompiledDemo.java KeyToolTest.java CryptoInterface.java AmopMsgHandlerTest.java TopicManager.java PrivateTopicVerifyTest.java AmopMsgBuilder.java TopicManagerTest.java AmopSubscribe.java AmopPublisher.java AmopPublisherPrivate.java AmopSubscribePrivate.java AmopPublisherFile.java AmopPublisherPrivateFile.java DemoAmopCallback.java FileToByteArrayHelper.java OkD.java TableTest.java PerformanceTable.java HelloWorld.java PerformanceRPC.java CodecTest.java ResponseTest.java ConfigTest.java)
commit_limit=6

LOG_ERROR() {
Expand Down
11 changes: 5 additions & 6 deletions sdk-amop/src/main/java/org/fisco/bcos/sdk/amop/Amop.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import org.fisco.bcos.sdk.channel.Channel;
import org.fisco.bcos.sdk.channel.ResponseCallback;
import org.fisco.bcos.sdk.config.ConfigOption;
import org.fisco.bcos.sdk.crypto.keystore.KeyManager;
import org.fisco.bcos.sdk.crypto.keystore.KeyTool;

/**
* AMOP module interface.
Expand Down Expand Up @@ -53,21 +53,20 @@ static Amop build(Channel channel, ConfigOption config) {
* Subscribe a private topic which need verify.
*
* @param topicName
* @param privateKeyManager the private key you used to prove your identity.
* @param privateKeyTool the private key you used to prove your identity.
* @param callback callback is called when receive a msg relate to this topic
*/
void subscribePrivateTopics(
String topicName, KeyManager privateKeyManager, AmopCallback callback);
void subscribePrivateTopics(String topicName, KeyTool privateKeyTool, AmopCallback callback);

/**
* Config a topic which is need verification, after that user can send message to verified
* subscriber.
*
* @param topicName
* @param publicKeyManagers the public keys of the target organizations that you want to
* @param publicKeyTools the public keys of the target organizations that you want to
* communicate with
*/
void publishPrivateTopic(String topicName, List<KeyManager> publicKeyManagers);
void publishPrivateTopic(String topicName, List<KeyTool> publicKeyTools);

/**
* Unsubscribe a topic.
Expand Down
26 changes: 13 additions & 13 deletions sdk-amop/src/main/java/org/fisco/bcos/sdk/amop/AmopImp.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import org.fisco.bcos.sdk.channel.model.Options;
import org.fisco.bcos.sdk.config.ConfigOption;
import org.fisco.bcos.sdk.config.model.AmopTopic;
import org.fisco.bcos.sdk.crypto.keystore.KeyManager;
import org.fisco.bcos.sdk.crypto.keystore.KeyTool;
import org.fisco.bcos.sdk.crypto.keystore.P12KeyStore;
import org.fisco.bcos.sdk.crypto.keystore.PEMKeyStore;
import org.fisco.bcos.sdk.model.AmopMsg;
Expand Down Expand Up @@ -74,17 +74,17 @@ public void subscribeTopic(String topicName, AmopCallback callback) {

@Override
public void subscribePrivateTopics(
String topicName, KeyManager privateKeyManager, AmopCallback callback) {
String topicName, KeyTool privateKeyTool, AmopCallback callback) {
logger.info("subscribe private topic, topic:{}", topicName);
topicManager.addPrivateTopicSubscribe(topicName, privateKeyManager, callback);
topicManager.addPrivateTopicSubscribe(topicName, privateKeyTool, callback);
sendSubscribe();
}

@Override
public void publishPrivateTopic(String topicName, List<KeyManager> publicKeyManagers) {
public void publishPrivateTopic(String topicName, List<KeyTool> publicKeyTools) {
logger.info(
"setup private topic, topic:{} pubKey len:{}", topicName, publicKeyManagers.size());
topicManager.addPrivateTopicSend(topicName, publicKeyManagers);
"setup private topic, topic:{} pubKey len:{}", topicName, publicKeyTools.size());
topicManager.addPrivateTopicSend(topicName, publicKeyTools);
sendSubscribe();
}

Expand Down Expand Up @@ -244,19 +244,19 @@ private void loadConfiguredTopics(ConfigOption config) throws AmopException {
for (AmopTopic topic : topics) {
if (null != topic.getPrivateKey()) {
String privKeyFile = topic.getPrivateKey();
KeyManager km;
KeyTool keyTool;

if (privKeyFile.endsWith("p12")) {
km = new P12KeyStore(privKeyFile, topic.getPassword());
keyTool = new P12KeyStore(privKeyFile, topic.getPassword());
} else {
km = new PEMKeyStore(privKeyFile);
keyTool = new PEMKeyStore(privKeyFile);
}
topicManager.addPrivateTopicSubscribe(topic.getTopicName(), km, null);
topicManager.addPrivateTopicSubscribe(topic.getTopicName(), keyTool, null);
} else if (null != topic.getPublicKeys()) {
List<KeyManager> pubList = new ArrayList<>();
List<KeyTool> pubList = new ArrayList<>();
for (String pubKey : topic.getPublicKeys()) {
KeyManager km = new PEMKeyStore(pubKey);
pubList.add(km);
KeyTool keyTool = new PEMKeyStore(pubKey);
pubList.add(keyTool);
}
topicManager.addPrivateTopicSend(topic.getTopicName(), pubList);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import org.fisco.bcos.sdk.channel.ResponseCallback;
import org.fisco.bcos.sdk.channel.model.Options;
import org.fisco.bcos.sdk.crypto.CryptoInterface;
import org.fisco.bcos.sdk.crypto.keystore.KeyManager;
import org.fisco.bcos.sdk.crypto.keystore.KeyTool;
import org.fisco.bcos.sdk.model.AmopMsg;
import org.fisco.bcos.sdk.model.CryptoType;
import org.fisco.bcos.sdk.model.Message;
Expand Down Expand Up @@ -216,13 +216,13 @@ public void responseVerifyRequest(ChannelHandlerContext ctx, Message msg) {
}

public int checkSignature(String topic, byte[] randomValue, byte[] signature) {
List<KeyManager> pubKeys = topicManager.getPublicKeysByTopic(topic);
Iterator<KeyManager> pks = pubKeys.iterator();
List<KeyTool> pubKeys = topicManager.getPublicKeysByTopic(topic);
Iterator<KeyTool> pks = pubKeys.iterator();
while (pks.hasNext()) {
KeyManager km = pks.next();
KeyTool keyTool = pks.next();
CryptoInterface cryptoInterface = new CryptoInterface(CryptoType.ECDSA_TYPE);
if (cryptoInterface.verify(
km,
keyTool,
Hex.toHexString(cryptoInterface.hash(randomValue)),
Hex.toHexString(signature))) {
return 0;
Expand Down Expand Up @@ -250,16 +250,17 @@ public void onPrivateTopicRandomValue(ChannelHandlerContext ctx, AmopMsg msg) {
new String(msg.getData()));
byte[] randValue = msg.getData();
String topic = msg.getTopic();
KeyManager km = topicManager.getPrivateKeyByTopic(getSimpleTopic(topic));
KeyTool keyTool = topicManager.getPrivateKeyByTopic(getSimpleTopic(topic));
String signature = "";
if (null == km) {
if (null == keyTool) {
logger.error("topic:{} not subscribed, reject message", getSimpleTopic(topic));
return;
} else {
CryptoInterface cryptoInterface = new CryptoInterface(CryptoType.ECDSA_TYPE);
try {
signature =
cryptoInterface.sign(km, Hex.toHexString(cryptoInterface.hash(randValue)));
cryptoInterface.sign(
keyTool, Hex.toHexString(cryptoInterface.hash(randValue)));
} catch (Exception e) {
logger.error(
"please check the public key of topic {} is correct configured, error {}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@
import java.util.concurrent.ConcurrentHashMap;
import org.fisco.bcos.sdk.amop.AmopCallback;
import org.fisco.bcos.sdk.amop.AmopMsgOut;
import org.fisco.bcos.sdk.crypto.keystore.KeyManager;
import org.fisco.bcos.sdk.crypto.keystore.KeyTool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class TopicManager {
private static Logger logger = LoggerFactory.getLogger(TopicManager.class);
private Map<String, KeyManager> topic2PrivateKey = new ConcurrentHashMap<>();
private Map<String, List<KeyManager>> topic2PublicKeys = new ConcurrentHashMap<>();
private Map<String, KeyTool> topic2PrivateKey = new ConcurrentHashMap<>();
private Map<String, List<KeyTool>> topic2PublicKeys = new ConcurrentHashMap<>();
private Map<String, String> topicName2FullName = new ConcurrentHashMap<>();
private Map<String, AmopCallback> topic2Callback = new ConcurrentHashMap<>();
private Set<String> topics = Collections.synchronizedSet(new HashSet<>());
Expand All @@ -51,7 +51,7 @@ public void addTopic(String topicString, AmopCallback callback) {
}

public void addPrivateTopicSubscribe(
String topicName, KeyManager privateKeyStore, AmopCallback callback) {
String topicName, KeyTool privateKeyStore, AmopCallback callback) {
String fullNameToSendToNode = makeVerifyChannelPrefixTopic(topicName);
logger.trace(
"add private topic subscribe, topic:{} full name:{}",
Expand All @@ -66,14 +66,14 @@ public void addPrivateTopicSubscribe(
}
}

public void addPrivateTopicSend(String topicName, List<KeyManager> publicKeyManagers) {
public void addPrivateTopicSend(String topicName, List<KeyTool> publicKeyTools) {
String fullNameToSendToNode = makePushChannelPrefixTopic(topicName);
logger.trace(
"add private topic to send, topic:{} full name:{}",
topicName,
fullNameToSendToNode);
topics.add(fullNameToSendToNode);
topic2PublicKeys.put(addNeedVerifyTopicPrefix(topicName), publicKeyManagers);
topic2PublicKeys.put(addNeedVerifyTopicPrefix(topicName), publicKeyTools);
topicName2FullName.put(topicName, fullNameToSendToNode);
}

Expand Down Expand Up @@ -156,11 +156,11 @@ public void setCallback(AmopCallback cb) {
this.callback = cb;
}

public List<KeyManager> getPublicKeysByTopic(String topic) {
public List<KeyTool> getPublicKeysByTopic(String topic) {
return topic2PublicKeys.get(topic);
}

public KeyManager getPrivateKeyByTopic(String topic) {
public KeyTool getPrivateKeyByTopic(String topic) {
return topic2PrivateKey.get(topic);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import org.fisco.bcos.sdk.amop.topic.TopicManager;
import org.fisco.bcos.sdk.channel.ResponseCallback;
import org.fisco.bcos.sdk.crypto.CryptoInterface;
import org.fisco.bcos.sdk.crypto.keystore.KeyManager;
import org.fisco.bcos.sdk.crypto.keystore.KeyTool;
import org.fisco.bcos.sdk.crypto.keystore.P12KeyStore;
import org.fisco.bcos.sdk.crypto.keystore.PEMKeyStore;
import org.fisco.bcos.sdk.model.AmopMsg;
Expand Down Expand Up @@ -104,7 +104,7 @@ public void testSignRandom() throws JsonProcessingException {
.getResource(
"keystore/ecdsa/0x45e14c53197adbcb719d915fb93342c25600faaf.public.pem")
.getPath();
KeyManager km = new PEMKeyStore(keyFile);
KeyTool km = new PEMKeyStore(keyFile);
Assert.assertTrue(
cryptoInterface.verify(
km,
Expand Down Expand Up @@ -170,7 +170,7 @@ private void senderGenRandom() throws JsonProcessingException {
private void initEnv() {
topicManagerSender = new TopicManager();
msgHandlerSender = new AmopMsgHandler(chSender, topicManagerSender);
List<KeyManager> list = new ArrayList<>();
List<KeyTool> list = new ArrayList<>();
String keyFile =
AmopMsgHandlerTest.class
.getClassLoader()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import java.util.Set;
import org.fisco.bcos.sdk.amop.topic.AmopMsgIn;
import org.fisco.bcos.sdk.amop.topic.TopicManager;
import org.fisco.bcos.sdk.crypto.keystore.KeyManager;
import org.fisco.bcos.sdk.crypto.keystore.KeyTool;
import org.fisco.bcos.sdk.crypto.keystore.PEMKeyStore;
import org.junit.Assert;
import org.junit.Test;
Expand All @@ -42,14 +42,14 @@ public void testAddTopic() {
.getResource(
"keystore/ecdsa/0x0fc3c4bb89bd90299db4c62be0174c4966286c00.pem")
.getPath();
KeyManager km = new PEMKeyStore(keyFile);
topicManager.addPrivateTopicSubscribe("priv", km, null);
KeyTool keyTool = new PEMKeyStore(keyFile);
topicManager.addPrivateTopicSubscribe("priv", keyTool, null);
Assert.assertEquals(
"#!$TopicNeedVerify_priv",
topicManager.getFullTopicString("priv").substring(17, 40));

List<KeyManager> list = new ArrayList<>();
list.add(km);
List<KeyTool> list = new ArrayList<>();
list.add(keyTool);
topicManager.addPrivateTopicSend("priv2", list);
Assert.assertEquals(
"#!$PushChannel_#!$TopicNeedVerify_priv2",
Expand Down Expand Up @@ -151,10 +151,10 @@ private TopicManager getTestTopicManager() {
.getResource(
"keystore/ecdsa/0x0fc3c4bb89bd90299db4c62be0174c4966286c00.pem")
.getPath();
KeyManager km = new PEMKeyStore(keyFile);
topicManager.addPrivateTopicSubscribe("priv", km, null);
List<KeyManager> list = new ArrayList<>();
list.add(km);
KeyTool keyTool = new PEMKeyStore(keyFile);
topicManager.addPrivateTopicSubscribe("priv", keyTool, null);
List<KeyTool> list = new ArrayList<>();
list.add(keyTool);
topicManager.addPrivateTopicSend("priv2", list);
topicManager.addPrivateTopicCallback("priv2", cb3);
return topicManager;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import org.fisco.bcos.sdk.crypto.keypair.CryptoKeyPair;
import org.fisco.bcos.sdk.crypto.keypair.ECDSAKeyPair;
import org.fisco.bcos.sdk.crypto.keypair.SM2KeyPair;
import org.fisco.bcos.sdk.crypto.keystore.KeyManager;
import org.fisco.bcos.sdk.crypto.keystore.KeyTool;
import org.fisco.bcos.sdk.crypto.keystore.P12KeyStore;
import org.fisco.bcos.sdk.crypto.keystore.PEMKeyStore;
import org.fisco.bcos.sdk.crypto.signature.ECDSASignature;
Expand Down Expand Up @@ -88,19 +88,19 @@ public CryptoInterface(int cryptoTypeConfig) {
}

public void loadAccount(String accountFileFormat, String accountFilePath, String password) {
KeyManager keyManager = null;
KeyTool keyTool = null;
if (accountFileFormat.compareToIgnoreCase("p12") == 0) {
keyManager = new P12KeyStore(accountFilePath, password);
keyTool = new P12KeyStore(accountFilePath, password);
} else if (accountFileFormat.compareToIgnoreCase("pem") == 0) {
keyManager = new PEMKeyStore(accountFilePath);
keyTool = new PEMKeyStore(accountFilePath);
} else {
throw new LoadKeyStoreException(
"unsupported account file format : "
+ accountFileFormat
+ ", current supported are p12 and pem");
}
logger.debug("Load account from {}", accountFilePath);
createKeyPair(keyManager.getKeyPair());
createKeyPair(keyTool.getKeyPair());
}

private void loadAccount(ConfigOption configOption) {
Expand Down Expand Up @@ -155,18 +155,18 @@ public SignatureResult sign(final String message, final CryptoKeyPair keyPair) {
}

// for AMOP topic verify, generate signature
public String sign(KeyManager keyManager, String message) {
CryptoKeyPair cryptoKeyPair = this.keyPairFactory.createKeyPair(keyManager.getKeyPair());
public String sign(KeyTool keyTool, String message) {
CryptoKeyPair cryptoKeyPair = this.keyPairFactory.createKeyPair(keyTool.getKeyPair());
return signatureImpl.signWithStringSignature(message, cryptoKeyPair);
}

// for AMOP topic verify, verify the signature
public boolean verify(KeyManager keyManager, String message, String signature) {
return verify(keyManager.getHexedPublicKey(), message, signature);
public boolean verify(KeyTool keyTool, String message, String signature) {
return verify(keyTool.getHexedPublicKey(), message, signature);
}

public boolean verify(KeyManager keyManager, byte[] message, byte[] signature) {
return verify(keyManager.getHexedPublicKey(), message, signature);
public boolean verify(KeyTool keyTool, byte[] message, byte[] signature) {
return verify(keyTool.getHexedPublicKey(), message, signature);
}

public boolean verify(final String publicKey, final String message, final String signature) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import org.fisco.bcos.sdk.config.ConfigOption;
import org.fisco.bcos.sdk.crypto.exceptions.KeyPairException;
import org.fisco.bcos.sdk.crypto.hash.Hash;
import org.fisco.bcos.sdk.crypto.keystore.KeyManager;
import org.fisco.bcos.sdk.crypto.keystore.KeyTool;
import org.fisco.bcos.sdk.crypto.keystore.P12KeyStore;
import org.fisco.bcos.sdk.crypto.keystore.PEMKeyStore;
import org.fisco.bcos.sdk.utils.Hex;
Expand Down Expand Up @@ -77,8 +77,8 @@ public CryptoKeyPair() {}
public CryptoKeyPair(KeyPair keyPair) {
this.keyPair = keyPair;
// init privateKey/publicKey from the keyPair
this.hexPrivateKey = KeyManager.getHexedPrivateKey(keyPair.getPrivate());
this.hexPublicKey = KeyManager.getHexedPublicKey(keyPair.getPublic());
this.hexPrivateKey = KeyTool.getHexedPrivateKey(keyPair.getPrivate());
this.hexPublicKey = KeyTool.getHexedPublicKey(keyPair.getPublic());
}
/**
* get CryptoKeyPair information from CryptoResult
Expand Down Expand Up @@ -116,16 +116,15 @@ public KeyPair getKeyPair() {
public abstract CryptoKeyPair createKeyPair(KeyPair keyPair);

public CryptoKeyPair createKeyPair(BigInteger privateKeyValue) {
PrivateKey privateKey =
KeyManager.convertHexedStringToPrivateKey(privateKeyValue, curveName);
PublicKey publicKey = KeyManager.getPublicKeyFromPrivateKey(privateKey);
PrivateKey privateKey = KeyTool.convertHexedStringToPrivateKey(privateKeyValue, curveName);
PublicKey publicKey = KeyTool.getPublicKeyFromPrivateKey(privateKey);
KeyPair keyPair = new KeyPair(publicKey, privateKey);
return createKeyPair(keyPair);
}

public CryptoKeyPair createKeyPair(String hexPrivateKey) {
PrivateKey privateKey = KeyManager.convertHexedStringToPrivateKey(hexPrivateKey, curveName);
PublicKey publicKey = KeyManager.getPublicKeyFromPrivateKey(privateKey);
PrivateKey privateKey = KeyTool.convertHexedStringToPrivateKey(hexPrivateKey, curveName);
PublicKey publicKey = KeyTool.getPublicKeyFromPrivateKey(privateKey);
KeyPair keyPair = new KeyPair(publicKey, privateKey);
return createKeyPair(keyPair);
}
Expand Down
Loading