Skip to content

Commit 2eb1e80

Browse files
kyonRayLucasLi1024
authored andcommitted
<fix>(crud): crud adapt new condition data structure. (FISCO-BCOS#709)
1 parent 695e1c2 commit 2eb1e80

File tree

6 files changed

+272
-12
lines changed

6 files changed

+272
-12
lines changed

src/main/java/org/fisco/bcos/sdk/v3/config/model/CryptoMaterialConfig.java

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,21 @@
1616
package org.fisco.bcos.sdk.v3.config.model;
1717

1818
import java.util.Map;
19+
1920
import org.fisco.bcos.sdk.v3.config.exceptions.ConfigException;
2021
import org.fisco.bcos.sdk.v3.model.CryptoType;
2122
import org.slf4j.Logger;
2223
import org.slf4j.LoggerFactory;
2324

24-
/** Crypto material configuration, include certs and keys */
25+
/**
26+
* Crypto material configuration, include certs and keys
27+
*/
2528
public class CryptoMaterialConfig {
2629
private static final Logger logger = LoggerFactory.getLogger(CryptoMaterialConfig.class);
2730

2831
private Boolean useSmCrypto = false;
2932
private Boolean disableSsl = false;
33+
private Boolean hsmEnable = false;
3034
private String certPath = "conf";
3135

3236
private String caCertPath;
@@ -41,23 +45,36 @@ public class CryptoMaterialConfig {
4145
private String enSdkCert;
4246
private String enSdkPrivateKey;
4347

44-
public CryptoMaterialConfig() {}
48+
private String hsmLibPath;
49+
private String hsmKeyIndex;
50+
private String hsmPassword;
51+
52+
public CryptoMaterialConfig() {
53+
}
4554

4655
public CryptoMaterialConfig(ConfigProperty configProperty) throws ConfigException {
4756

4857
Map<String, Object> cryptoMaterialProperty = configProperty.getCryptoMaterial();
4958
String useSMCrypto = (String) cryptoMaterialProperty.get("useSMCrypto");
5059
String disableSsl = (String) cryptoMaterialProperty.get("disableSsl");
60+
String hsmEnable = (String) cryptoMaterialProperty.get("hsmEnable");
5161

5262
this.useSmCrypto = Boolean.valueOf(useSMCrypto);
5363
this.disableSsl = Boolean.valueOf(disableSsl);
64+
this.hsmEnable = Boolean.valueOf(hsmEnable);
5465

5566
if (this.disableSsl) {
5667
logger.info("Load cryptoMaterial, disableSsl has been set");
5768
return;
5869
}
5970

60-
int cryptoType = this.useSmCrypto ? CryptoType.SM_TYPE : CryptoType.ECDSA_TYPE;
71+
if (this.hsmEnable) {
72+
this.hsmLibPath = (String) cryptoMaterialProperty.get("hsmLibPath");
73+
this.hsmKeyIndex = (String) cryptoMaterialProperty.get("hsmKeyIndex");
74+
this.hsmPassword = (String) cryptoMaterialProperty.get("hsmPassword");
75+
}
76+
77+
int cryptoType = this.useSmCrypto ? (this.hsmEnable ? CryptoType.HSM_TYPE : CryptoType.SM_TYPE) : CryptoType.ECDSA_TYPE;
6178
this.certPath =
6279
ConfigProperty.getConfigFilePath(
6380
ConfigProperty.getValue(cryptoMaterialProperty, "certPath", this.certPath));
@@ -134,7 +151,7 @@ public CryptoMaterialConfig getDefaultCaCertPath(int cryptoType, String certPath
134151
cryptoMaterialConfig.setCaCertPath(certPath + "/" + "ca.crt");
135152
cryptoMaterialConfig.setSdkCertPath(certPath + "/" + "sdk.crt");
136153
cryptoMaterialConfig.setSdkPrivateKeyPath(certPath + "/" + "sdk.key");
137-
} else if (cryptoType == CryptoType.SM_TYPE) {
154+
} else if (cryptoType == CryptoType.SM_TYPE | cryptoType == CryptoType.HSM_TYPE) {
138155
cryptoMaterialConfig.setCaCertPath(certPath + "/" + "sm_ca.crt");
139156
cryptoMaterialConfig.setSdkCertPath(certPath + "/" + "sm_sdk.crt");
140157
cryptoMaterialConfig.setSdkPrivateKeyPath(certPath + "/" + "sm_sdk.key");
@@ -220,6 +237,38 @@ public boolean isUseSmCrypto() {
220237
return useSmCrypto;
221238
}
222239

240+
public Boolean getHsmEnable() {
241+
return hsmEnable;
242+
}
243+
244+
public void setHsmEnable(Boolean hsmEnable) {
245+
this.hsmEnable = hsmEnable;
246+
}
247+
248+
public String getHsmLibPath() {
249+
return hsmLibPath;
250+
}
251+
252+
public void setHsmLibPath(String hsmLibPath) {
253+
this.hsmLibPath = hsmLibPath;
254+
}
255+
256+
public String getHsmKeyIndex() {
257+
return hsmKeyIndex;
258+
}
259+
260+
public void setHsmKeyIndex(String hsmKeyIndex) {
261+
this.hsmKeyIndex = hsmKeyIndex;
262+
}
263+
264+
public String getHsmPassword() {
265+
return hsmPassword;
266+
}
267+
268+
public void setHsmPassword(String hsmPassword) {
269+
this.hsmPassword = hsmPassword;
270+
}
271+
223272
public String getCaCertPath() {
224273
return caCertPath;
225274
}

src/main/java/org/fisco/bcos/sdk/v3/crypto/CryptoSuite.java

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,13 @@
2323
import org.fisco.bcos.sdk.v3.crypto.hash.SM3Hash;
2424
import org.fisco.bcos.sdk.v3.crypto.keypair.CryptoKeyPair;
2525
import org.fisco.bcos.sdk.v3.crypto.keypair.ECDSAKeyPair;
26+
import org.fisco.bcos.sdk.v3.crypto.keypair.HsmSM2KeyPair;
2627
import org.fisco.bcos.sdk.v3.crypto.keypair.SM2KeyPair;
2728
import org.fisco.bcos.sdk.v3.crypto.keystore.KeyTool;
2829
import org.fisco.bcos.sdk.v3.crypto.keystore.P12KeyStore;
2930
import org.fisco.bcos.sdk.v3.crypto.keystore.PEMKeyStore;
3031
import org.fisco.bcos.sdk.v3.crypto.signature.ECDSASignature;
32+
import org.fisco.bcos.sdk.v3.crypto.signature.HsmSM2Signature;
3133
import org.fisco.bcos.sdk.v3.crypto.signature.SM2Signature;
3234
import org.fisco.bcos.sdk.v3.crypto.signature.Signature;
3335
import org.fisco.bcos.sdk.v3.crypto.signature.SignatureResult;
@@ -39,11 +41,10 @@ public class CryptoSuite {
3941

4042
private static final Logger logger = LoggerFactory.getLogger(CryptoSuite.class);
4143

42-
public final int cryptoTypeConfig;
43-
44-
public final Signature signatureImpl;
45-
public final Hash hashImpl;
46-
private final CryptoKeyPair keyPairFactory;
44+
public int cryptoTypeConfig;
45+
public Signature signatureImpl;
46+
public Hash hashImpl;
47+
private CryptoKeyPair keyPairFactory;
4748
private CryptoKeyPair cryptoKeyPair;
4849
private ConfigOption config;
4950

@@ -64,9 +65,9 @@ public CryptoSuite(int cryptoTypeConfig, String hexedPrivateKey) {
6465
* @param configOption the configuration of account.
6566
*/
6667
public CryptoSuite(int cryptoTypeConfig, ConfigOption configOption) {
67-
this(cryptoTypeConfig);
6868
logger.info("init CryptoSuite, cryptoType: {}", cryptoTypeConfig);
6969
this.setConfig(configOption);
70+
this.initCryptoSuite(cryptoTypeConfig);
7071
// doesn't set the account name, generate the keyPair randomly
7172
if (!configOption.getAccountConfig().isAccountConfigured()) {
7273
this.generateRandomKeyPair();
@@ -81,6 +82,10 @@ public CryptoSuite(int cryptoTypeConfig, ConfigOption configOption) {
8182
* @param cryptoTypeConfig the crypto type config number
8283
*/
8384
public CryptoSuite(int cryptoTypeConfig) {
85+
initCryptoSuite(cryptoTypeConfig);
86+
}
87+
88+
public void initCryptoSuite(int cryptoTypeConfig) {
8489
this.cryptoTypeConfig = cryptoTypeConfig;
8590
if (this.cryptoTypeConfig == CryptoType.ECDSA_TYPE) {
8691
this.signatureImpl = new ECDSASignature();
@@ -92,12 +97,20 @@ public CryptoSuite(int cryptoTypeConfig) {
9297
this.hashImpl = new SM3Hash();
9398
this.keyPairFactory = new SM2KeyPair();
9499

100+
} else if(this.cryptoTypeConfig == CryptoType.HSM_TYPE) {
101+
HsmSM2Signature hsmSM2Signature = new HsmSM2Signature();
102+
hsmSM2Signature.setHsmLibPath(this.config.getCryptoMaterialConfig().getHsmLibPath());
103+
this.signatureImpl = hsmSM2Signature;
104+
this.hashImpl = new SM3Hash();
105+
this.keyPairFactory = new HsmSM2KeyPair(this.config.getCryptoMaterialConfig().getHsmLibPath());
95106
} else {
96107
throw new UnsupportedCryptoTypeException(
97108
"only support "
98109
+ CryptoType.ECDSA_TYPE
99110
+ "/"
100111
+ CryptoType.SM_TYPE
112+
+ "/"
113+
+ CryptoType.HSM_TYPE
101114
+ " crypto type");
102115
}
103116
// create keyPair randomly
@@ -159,7 +172,6 @@ private void loadAccount(ConfigOption configOption) {
159172
*/
160173
public void setConfig(ConfigOption config) {
161174
this.config = config;
162-
this.keyPairFactory.setConfig(config);
163175
}
164176

165177
public int getCryptoTypeConfig() {
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/**
2+
* Copyright 2014-2020 [fisco-dev]
3+
*
4+
* <p>Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5+
* except in compliance with the License. You may obtain a copy of the License at
6+
*
7+
* <p>http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* <p>Unless required by applicable law or agreed to in writing, software distributed under the
10+
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
11+
* express or implied. See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
package org.fisco.bcos.sdk.v3.crypto.keypair;
15+
16+
import org.fisco.bcos.sdk.jni.common.JniException;
17+
import org.fisco.bcos.sdk.jni.utilities.keypair.KeyPairJniObj;
18+
import org.fisco.bcos.sdk.v3.crypto.hash.Hash;
19+
import org.fisco.bcos.sdk.v3.crypto.hash.SM3Hash;
20+
import org.slf4j.Logger;
21+
import org.slf4j.LoggerFactory;
22+
23+
import java.security.KeyPair;
24+
25+
public class HsmSM2KeyPair extends CryptoKeyPair {
26+
private static final Logger logger = LoggerFactory.getLogger(HsmSM2KeyPair.class);
27+
28+
public static Hash DefaultHashAlgorithm = new SM3Hash();
29+
private String hsmLibPath;
30+
31+
public HsmSM2KeyPair(String hsmLibPath) {
32+
this.hsmLibPath = hsmLibPath;
33+
initHsmSM2KeyPairObject();
34+
}
35+
36+
public HsmSM2KeyPair(String hsmLibPath, KeyPair javaKeyPair) {
37+
super(javaKeyPair);
38+
this.hsmLibPath = hsmLibPath;
39+
initHsmSM2KeyPairObject();
40+
}
41+
42+
private void initHsmSM2KeyPairObject() {
43+
this.keyStoreSubDir = GM_ACCOUNT_SUBDIR;
44+
this.hashImpl = new SM3Hash();
45+
this.curveName = SM2_CURVE_NAME;
46+
this.signatureAlgorithm = SM_SIGNATURE_ALGORITHM;
47+
}
48+
49+
/**
50+
* generate keyPair randomly
51+
*
52+
* @return the generated keyPair
53+
*/
54+
@Override
55+
public CryptoKeyPair generateKeyPair() {
56+
try {
57+
HsmSM2KeyPair hsmSM2KeyPair = new HsmSM2KeyPair(this.hsmLibPath);
58+
hsmSM2KeyPair.jniKeyPair = KeyPairJniObj.createHsmKeyPair(this.hsmLibPath);
59+
return hsmSM2KeyPair;
60+
} catch (JniException e) {
61+
// TODO: handle jni exception
62+
logger.error("hsm generateKeyPair exception, jni e: ", e);
63+
return null;
64+
}
65+
}
66+
67+
@Override
68+
public CryptoKeyPair createKeyPair(KeyPair javaKeyPair) {
69+
try {
70+
HsmSM2KeyPair hsmSM2KeyPair = new HsmSM2KeyPair(this.hsmLibPath, javaKeyPair);
71+
hsmSM2KeyPair.jniKeyPair = KeyPairJniObj.createHsmKeyPair(javaKeyPair.getPrivate().getEncoded(), this.hsmLibPath);
72+
return hsmSM2KeyPair;
73+
74+
} catch (JniException e) {
75+
// TODO: handle jni exception
76+
logger.error("hsm createKeyPair exception, jni e: ", e);
77+
return null;
78+
}
79+
}
80+
81+
public CryptoKeyPair useKeyPair(int keyIndex, String password) {
82+
try {
83+
HsmSM2KeyPair hsmSM2KeyPair = new HsmSM2KeyPair(this.hsmLibPath);
84+
hsmSM2KeyPair.jniKeyPair = KeyPairJniObj.useHsmKeyPair(keyIndex, password, this.hsmLibPath);
85+
return hsmSM2KeyPair;
86+
} catch (JniException e) {
87+
// TODO: handle jni exception
88+
logger.error("hsm useKeyPair exception, jni e: ", e);
89+
return null;
90+
}
91+
}
92+
93+
public String getHsmLibPath() {
94+
return hsmLibPath;
95+
}
96+
97+
public void setHsmLibPath(String hsmLibPath) {
98+
this.hsmLibPath = hsmLibPath;
99+
}
100+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/**
2+
* Copyright 2014-2020 [fisco-dev]
3+
*
4+
* <p>Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5+
* except in compliance with the License. You may obtain a copy of the License at
6+
*
7+
* <p>http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* <p>Unless required by applicable law or agreed to in writing, software distributed under the
10+
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
11+
* express or implied. See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
package org.fisco.bcos.sdk.v3.crypto.signature;
15+
16+
import org.fisco.bcos.sdk.jni.common.JniException;
17+
import org.fisco.bcos.sdk.jni.utilities.signature.SignatureJniObj;
18+
import org.fisco.bcos.sdk.v3.crypto.keypair.HsmSM2KeyPair;
19+
import org.fisco.bcos.sdk.v3.crypto.exceptions.SignatureException;
20+
import org.fisco.bcos.sdk.v3.crypto.keypair.CryptoKeyPair;
21+
import org.fisco.bcos.sdk.v3.model.CryptoType;
22+
import org.fisco.bcos.sdk.v3.utils.Hex;
23+
import org.fisco.bcos.sdk.v3.utils.Numeric;
24+
import org.slf4j.Logger;
25+
import org.slf4j.LoggerFactory;
26+
27+
public class HsmSM2Signature implements Signature {
28+
private static final Logger logger = LoggerFactory.getLogger(HsmSM2Signature.class);
29+
30+
private String hsmLibPath;
31+
32+
public String getHsmLibPath() {
33+
return hsmLibPath;
34+
}
35+
36+
public void setHsmLibPath(String hsmLibPath) {
37+
this.hsmLibPath = hsmLibPath;
38+
}
39+
40+
@Override
41+
public SignatureResult sign(final String message, final CryptoKeyPair keyPair) {
42+
return new SM2SignatureResult(
43+
keyPair.getHexPublicKey(), signWithStringSignature(message, keyPair));
44+
}
45+
46+
@Override
47+
public SignatureResult sign(final byte[] message, final CryptoKeyPair keyPair) {
48+
return sign(Hex.toHexString(message), keyPair);
49+
}
50+
51+
@Override
52+
public String signWithStringSignature(final String message, final CryptoKeyPair keyPair) {
53+
return signMessage(message, keyPair);
54+
}
55+
56+
public String signMessage(String message, CryptoKeyPair keyPair) {
57+
if (!keyPair.getCurveName().equals(CryptoKeyPair.SM2_CURVE_NAME)) {
58+
throw new SignatureException("hsm sm2 sign with " + keyPair.getCurveName() + " keypair");
59+
}
60+
61+
HsmSM2KeyPair hsmSM2KeyPair = (HsmSM2KeyPair) keyPair;
62+
try {
63+
return SignatureJniObj.sign(
64+
hsmSM2KeyPair.getJniKeyPair(),
65+
Numeric.cleanHexPrefix(message),
66+
hsmSM2KeyPair.getHsmLibPath());
67+
} catch (JniException e) {
68+
// TODO: handle jni exception
69+
logger.error("Sign with hsm sm2 failed, jni e: ", e);
70+
return null;
71+
}
72+
}
73+
74+
@Override
75+
public boolean verify(final String publicKey, final String message, final String signature) {
76+
return verifyMessage(publicKey, message, signature);
77+
}
78+
79+
@Override
80+
public boolean verify(final String publicKey, final byte[] message, final byte[] signature) {
81+
return verify(publicKey, Hex.toHexString(message), Hex.toHexString(signature));
82+
}
83+
84+
public boolean verifyMessage(String publicKey, String message, String signature) {
85+
try {
86+
return SignatureJniObj.verify(
87+
CryptoType.HSM_TYPE,
88+
Hex.decode(publicKey),
89+
Numeric.cleanHexPrefix(message),
90+
Numeric.cleanHexPrefix(signature),
91+
this.getHsmLibPath());
92+
} catch (JniException e) {
93+
// TODO: handle jni exception
94+
logger.error("Verify with hsm sm2 failed, jni e: ", e);
95+
return false;
96+
}
97+
}
98+
}

src/main/java/org/fisco/bcos/sdk/v3/model/CryptoType.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@ public class CryptoType {
1919
public static final int ECDSA_TYPE = 0;
2020
public static final int SM_TYPE = 1;
2121
public static final int ED25519_VRF_TYPE = 2;
22+
public static final int HSM_TYPE = 3;
2223
}

0 commit comments

Comments
 (0)