Skip to content

Commit 2fcb24f

Browse files
authored
support hsm (#300)
* add hsm config methods and internal key support SDF SM2 keypair add 04 prefix in public key hex string use webank-blockchain-java-crypto instead of key-mini-toolkit * supply hsm config example
1 parent 657a378 commit 2fcb24f

File tree

26 files changed

+506
-51
lines changed

26 files changed

+506
-51
lines changed

.circleci/config.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ jobs:
1616
- run:
1717
name: Compile
1818
command: |
19+
bash gradlew --version
1920
bash gradlew build -x test -x integrationTest
2021
- run:
2122
name: Integration Test

build.gradle

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ ext {
2424
nettySMSSLContextVersion = "1.2.0"
2525
toml4jVersion = "0.7.2"
2626
bcprovJDK15onVersion = "1.60"
27-
keyMiniToolkit = "1.0.3"
27+
webankJavaCryptoVersion = "1.0.0-005-SNAPSHOT"
28+
webankHsmCryptoVersion = "1.0.0-008-SNAPSHOT"
2829

2930
slf4jVersion = "1.7.30"
3031
junitVersion = "4.12"
@@ -66,6 +67,7 @@ allprojects {
6667
maven { url "http://maven.aliyun.com/nexus/content/groups/public/" }
6768
maven { url "https://oss.sonatype.org/service/local/staging/deploy/maven2"}
6869
maven { url "https://oss.sonatype.org/content/repositories/snapshots" }
70+
maven {url "https://plugins.gradle.org/m2/"}
6971
}
7072

7173
dependencies {
@@ -193,9 +195,9 @@ dependencies {
193195
compile ("commons-io:commons-io:${commonsIOVersion}")
194196
compile ("com.squareup:javapoet:${javapoetVersion}")
195197
compile ("info.picocli:picocli:${picocliVersion}")
196-
compile ("com.webank:key-mini-toolkit:${keyMiniToolkit}")
198+
compile ("com.webank:webank-blockchain-java-crypto:${webankJavaCryptoVersion}")
197199
compile ("com.moandjiezana.toml:toml4j:${toml4jVersion}")
198-
200+
compile ("com.webank:webank-blockchain-hsm-crypto:${webankHsmCryptoVersion}")
199201
testCompile ("org.apache.commons:commons-collections4:${commonsCollections4Version}")
200202
testCompile ("com.google.guava:guava:${guavaVersion}")
201203
}

sdk-core/src/main/java/org/fisco/bcos/sdk/config/ConfigOption.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.fisco.bcos.sdk.config.model.AmopConfig;
2222
import org.fisco.bcos.sdk.config.model.ConfigProperty;
2323
import org.fisco.bcos.sdk.config.model.CryptoMaterialConfig;
24+
import org.fisco.bcos.sdk.config.model.CryptoProviderConfig;
2425
import org.fisco.bcos.sdk.config.model.NetworkConfig;
2526
import org.fisco.bcos.sdk.config.model.ThreadPoolConfig;
2627
import org.fisco.bcos.sdk.model.CryptoType;
@@ -37,6 +38,7 @@ public class ConfigOption {
3738
private AmopConfig amopConfig;
3839
private NetworkConfig networkConfig;
3940
private ThreadPoolConfig threadPoolConfig;
41+
private CryptoProviderConfig cryptoProviderConfig;
4042
private ConfigProperty configProperty;
4143

4244
public ConfigOption(ConfigProperty configProperty) throws ConfigException {
@@ -54,6 +56,8 @@ public ConfigOption(ConfigProperty configProperty, int cryptoType) throws Config
5456
networkConfig = new NetworkConfig(configProperty);
5557
// load threadPoolConfig
5658
threadPoolConfig = new ThreadPoolConfig(configProperty);
59+
// load cryptoProviderConfig
60+
cryptoProviderConfig = new CryptoProviderConfig(configProperty);
5761
// init configProperty
5862
this.configProperty = configProperty;
5963
}
@@ -101,4 +105,12 @@ public ThreadPoolConfig getThreadPoolConfig() {
101105
public void setThreadPoolConfig(ThreadPoolConfig threadPoolConfig) {
102106
this.threadPoolConfig = threadPoolConfig;
103107
}
108+
109+
public CryptoProviderConfig getCryptoProviderConfig() {
110+
return cryptoProviderConfig;
111+
}
112+
113+
public void setCryptoProviderConfig(CryptoProviderConfig cryptoProviderConfig) {
114+
this.cryptoProviderConfig = cryptoProviderConfig;
115+
}
104116
}

sdk-core/src/main/java/org/fisco/bcos/sdk/config/model/AccountConfig.java

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515

1616
package org.fisco.bcos.sdk.config.model;
1717

18+
import static org.fisco.bcos.sdk.model.CryptoProviderType.HSM;
19+
import static org.fisco.bcos.sdk.model.CryptoProviderType.SSM;
20+
21+
import java.util.Map;
1822
import java.util.Objects;
1923
import org.fisco.bcos.sdk.config.exceptions.ConfigException;
2024

@@ -25,6 +29,7 @@ public class AccountConfig {
2529
private String accountFileFormat;
2630
private String accountPassword;
2731
private String accountFilePath;
32+
private String accountKeyIndex;
2833

2934
public AccountConfig(ConfigProperty configProperty) throws ConfigException {
3035
this.keyStoreDir =
@@ -38,13 +43,25 @@ public AccountConfig(ConfigProperty configProperty) throws ConfigException {
3843
this.accountPassword = ConfigProperty.getValue(configProperty.getAccount(), "password", "");
3944
this.accountFilePath =
4045
ConfigProperty.getValue(configProperty.getAccount(), "accountFilePath", "");
46+
this.accountKeyIndex =
47+
ConfigProperty.getValue(configProperty.getAccount(), "accountKeyIndex", "");
4148
if (!this.accountFilePath.equals("")) {
4249
this.accountFilePath = ConfigProperty.getConfigFilePath(this.accountFilePath);
4350
}
44-
checkAccountConfig();
45-
}
46-
47-
private void checkAccountConfig() throws ConfigException {
51+
checkAccountConfig(configProperty);
52+
}
53+
54+
private void checkAccountConfig(ConfigProperty configProperty) throws ConfigException {
55+
Map<String, Object> cryptoProvider = configProperty.getCryptoProvider();
56+
if (cryptoProvider != null) {
57+
String cryptoType = ConfigProperty.getValue(cryptoProvider, "type", SSM);
58+
if (cryptoType != null && cryptoType.equals(HSM)) {
59+
if (!this.accountKeyIndex.equals("") && this.accountPassword.equals("")) {
60+
throw new ConfigException(
61+
"cannot load hsm inner key, please config the password");
62+
}
63+
}
64+
}
4865
if (this.accountAddress.equals("")) {
4966
return;
5067
}
@@ -97,6 +114,14 @@ public void setAccountPassword(String accountPassword) {
97114
this.accountPassword = accountPassword;
98115
}
99116

117+
public String getAccountKeyIndex() {
118+
return accountKeyIndex;
119+
}
120+
121+
public void setAccountKeyIndex(String accountKeyIndex) {
122+
this.accountKeyIndex = accountKeyIndex;
123+
}
124+
100125
@Override
101126
public String toString() {
102127
return "AccountConfig{"

sdk-core/src/main/java/org/fisco/bcos/sdk/config/model/ConfigProperty.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ public class ConfigProperty {
4141
public Map<String, Object> network;
4242
public List<AmopTopic> amop;
4343
public Map<String, Object> account;
44-
4544
public Map<String, Object> threadPool;
45+
public Map<String, Object> cryptoProvider;
4646

4747
public Map<String, Object> getCryptoMaterial() {
4848
return cryptoMaterial;
@@ -84,6 +84,14 @@ public void setThreadPool(Map<String, Object> threadPool) {
8484
this.threadPool = threadPool;
8585
}
8686

87+
public Map<String, Object> getCryptoProvider() {
88+
return cryptoProvider;
89+
}
90+
91+
public void setCryptoProvider(Map<String, Object> cryptoProvider) {
92+
this.cryptoProvider = cryptoProvider;
93+
}
94+
8795
public static String getValue(Map<String, Object> config, String key, String defaultValue) {
8896
if (config == null || config.get(key) == null) {
8997
return defaultValue;
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package org.fisco.bcos.sdk.config.model;
2+
3+
import static org.fisco.bcos.sdk.model.CryptoProviderType.SSM;
4+
5+
import java.util.Map;
6+
import org.slf4j.Logger;
7+
import org.slf4j.LoggerFactory;
8+
9+
public class CryptoProviderConfig {
10+
private static Logger logger = LoggerFactory.getLogger(CryptoProviderConfig.class);
11+
private String type;
12+
13+
protected CryptoProviderConfig() {}
14+
15+
public CryptoProviderConfig(ConfigProperty configProperty) {
16+
Map<String, Object> cryptoProvider = configProperty.getCryptoProvider();
17+
if (cryptoProvider != null) {
18+
this.type = ConfigProperty.getValue(cryptoProvider, "type", SSM);
19+
} else {
20+
type = SSM;
21+
}
22+
}
23+
24+
public String getType() {
25+
return type;
26+
}
27+
28+
public void setType(String type) {
29+
this.type = type;
30+
}
31+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package org.fisco.bcos.sdk.model;
2+
3+
public class CryptoProviderType {
4+
public static final String HSM = "hsm"; // Hardware secure module
5+
public static final String SSM = "ssm"; // Software secure module
6+
}

sdk-core/src/main/java/org/fisco/bcos/sdk/model/CryptoType.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,7 @@ public class CryptoType {
2222

2323
// vrf related crypto type(1000-1999)
2424
public static final int ED25519_VRF_TYPE = 1000;
25+
26+
// hardware secure module type (2000-2999)
27+
public static final int SM_HSM_TYPE = 2001;
2528
}

sdk-core/src/test/resources/config-example.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
[cryptoMaterial]
2-
32
certPath = "conf" # The certification path
43

54
# The following configurations take the certPath by default:

sdk-crypto/build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ plugins {
55
dependencies {
66
compile project(':sdk-core')
77
compile ("org.bouncycastle:bcprov-jdk15on:${bcprovJDK15onVersion}")
8-
compile ("com.webank:key-mini-toolkit:${keyMiniToolkit}")
8+
compile ("com.webank:webank-blockchain-java-crypto:${webankJavaCryptoVersion}")
9+
compile ("com.webank:webank-blockchain-hsm-crypto:${webankHsmCryptoVersion}")
910
}
1011

1112
task sourcesJar(type: Jar) {

0 commit comments

Comments
 (0)