23
23
import org .fisco .bcos .sdk .v3 .crypto .hash .SM3Hash ;
24
24
import org .fisco .bcos .sdk .v3 .crypto .keypair .CryptoKeyPair ;
25
25
import org .fisco .bcos .sdk .v3 .crypto .keypair .ECDSAKeyPair ;
26
+ import org .fisco .bcos .sdk .v3 .crypto .keypair .HsmSM2KeyPair ;
26
27
import org .fisco .bcos .sdk .v3 .crypto .keypair .SM2KeyPair ;
27
28
import org .fisco .bcos .sdk .v3 .crypto .keystore .KeyTool ;
28
29
import org .fisco .bcos .sdk .v3 .crypto .keystore .P12KeyStore ;
29
30
import org .fisco .bcos .sdk .v3 .crypto .keystore .PEMKeyStore ;
30
31
import org .fisco .bcos .sdk .v3 .crypto .signature .ECDSASignature ;
32
+ import org .fisco .bcos .sdk .v3 .crypto .signature .HsmSM2Signature ;
31
33
import org .fisco .bcos .sdk .v3 .crypto .signature .SM2Signature ;
32
34
import org .fisco .bcos .sdk .v3 .crypto .signature .Signature ;
33
35
import org .fisco .bcos .sdk .v3 .crypto .signature .SignatureResult ;
@@ -39,11 +41,10 @@ public class CryptoSuite {
39
41
40
42
private static final Logger logger = LoggerFactory .getLogger (CryptoSuite .class );
41
43
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 keyPair ;
47
48
private CryptoKeyPair cryptoKeyPair ;
48
49
private ConfigOption config ;
49
50
@@ -54,7 +55,7 @@ public CryptoSuite(int cryptoTypeConfig, CryptoKeyPair cryptoKeyPair) {
54
55
55
56
public CryptoSuite (int cryptoTypeConfig , String hexedPrivateKey ) {
56
57
this (cryptoTypeConfig );
57
- this .cryptoKeyPair = this .keyPairFactory .createKeyPair (hexedPrivateKey );
58
+ this .cryptoKeyPair = this .keyPair .createKeyPair (hexedPrivateKey );
58
59
}
59
60
60
61
/**
@@ -64,12 +65,17 @@ public CryptoSuite(int cryptoTypeConfig, String hexedPrivateKey) {
64
65
* @param configOption the configuration of account.
65
66
*/
66
67
public CryptoSuite (int cryptoTypeConfig , ConfigOption configOption ) {
67
- this (cryptoTypeConfig );
68
68
logger .info ("init CryptoSuite, cryptoType: {}" , cryptoTypeConfig );
69
69
this .setConfig (configOption );
70
+ this .initCryptoSuite (cryptoTypeConfig );
70
71
// doesn't set the account name, generate the keyPair randomly
71
72
if (!configOption .getAccountConfig ().isAccountConfigured ()) {
72
- this .generateRandomKeyPair ();
73
+ if (configOption .getCryptoMaterialConfig ().getEnableHsm ()) {
74
+ HsmSM2KeyPair hsmKeyPair = (HsmSM2KeyPair )this .keyPair ;
75
+ this .cryptoKeyPair = hsmKeyPair .useKeyPair ();
76
+ } else {
77
+ this .generateRandomKeyPair ();
78
+ }
73
79
return ;
74
80
}
75
81
this .loadAccount (configOption );
@@ -81,27 +87,43 @@ public CryptoSuite(int cryptoTypeConfig, ConfigOption configOption) {
81
87
* @param cryptoTypeConfig the crypto type config number
82
88
*/
83
89
public CryptoSuite (int cryptoTypeConfig ) {
90
+ initCryptoSuite (cryptoTypeConfig );
91
+ }
92
+
93
+ public void initCryptoSuite (int cryptoTypeConfig ) {
84
94
this .cryptoTypeConfig = cryptoTypeConfig ;
85
95
if (this .cryptoTypeConfig == CryptoType .ECDSA_TYPE ) {
86
96
this .signatureImpl = new ECDSASignature ();
87
97
this .hashImpl = new Keccak256 ();
88
- this .keyPairFactory = new ECDSAKeyPair ();
89
-
98
+ this .keyPair = new ECDSAKeyPair ();
99
+ this . generateRandomKeyPair ();
90
100
} else if (this .cryptoTypeConfig == CryptoType .SM_TYPE ) {
91
101
this .signatureImpl = new SM2Signature ();
92
102
this .hashImpl = new SM3Hash ();
93
- this .keyPairFactory = new SM2KeyPair ();
94
-
103
+ this .keyPair = new SM2KeyPair ();
104
+ this .generateRandomKeyPair ();
105
+ } else if (this .cryptoTypeConfig == CryptoType .HSM_TYPE ) {
106
+ String hsmLibPath = this .config .getCryptoMaterialConfig ().getHsmLibPath ();
107
+ int hsmKeyIndex = Integer .parseInt (this .config .getCryptoMaterialConfig ().getHsmKeyIndex ());
108
+ String hsmPassword = this .config .getCryptoMaterialConfig ().getHsmPassword ();
109
+
110
+ HsmSM2Signature hsmSM2Signature = new HsmSM2Signature ();
111
+ hsmSM2Signature .setHsmLibPath (hsmLibPath );
112
+ this .signatureImpl = hsmSM2Signature ;
113
+ this .hashImpl = new SM3Hash ();
114
+ this .keyPair = new HsmSM2KeyPair (hsmLibPath , hsmKeyIndex , hsmPassword );
115
+ HsmSM2KeyPair hsmKeyPair = (HsmSM2KeyPair )this .keyPair ;
116
+ this .cryptoKeyPair = hsmKeyPair .useKeyPair ();
95
117
} else {
96
118
throw new UnsupportedCryptoTypeException (
97
119
"only support "
98
120
+ CryptoType .ECDSA_TYPE
99
121
+ "/"
100
122
+ CryptoType .SM_TYPE
123
+ + "/"
124
+ + CryptoType .HSM_TYPE
101
125
+ " crypto type" );
102
126
}
103
- // create keyPair randomly
104
- this .generateRandomKeyPair ();
105
127
}
106
128
107
129
/**
@@ -115,16 +137,20 @@ public void loadAccount(String accountFileFormat, String accountFilePath, String
115
137
KeyTool keyTool = null ;
116
138
if (accountFileFormat .compareToIgnoreCase ("p12" ) == 0 ) {
117
139
keyTool = new P12KeyStore (accountFilePath , password );
140
+ this .loadKeyPair (keyTool .getKeyPair ());
118
141
} else if (accountFileFormat .compareToIgnoreCase ("pem" ) == 0 ) {
119
142
keyTool = new PEMKeyStore (accountFilePath );
120
- } else {
143
+ this .loadKeyPair (keyTool .getKeyPair ());
144
+ } else if (accountFileFormat .compareToIgnoreCase ("HSM" ) == 0 ) {
145
+ this .loadHsmKeyPair ();
146
+ }
147
+ else {
121
148
throw new LoadKeyStoreException (
122
149
"unsupported account file format : "
123
150
+ accountFileFormat
124
151
+ ", current supported are p12 and pem" );
125
152
}
126
153
logger .debug ("Load account from {}" , accountFilePath );
127
- this .loadKeyPair (keyTool .getKeyPair ());
128
154
}
129
155
130
156
/**
@@ -138,11 +164,11 @@ private void loadAccount(ConfigOption configOption) {
138
164
if (accountFilePath == null || accountFilePath .equals ("" )) {
139
165
if (accountConfig .getAccountFileFormat ().compareToIgnoreCase ("p12" ) == 0 ) {
140
166
accountFilePath =
141
- this .keyPairFactory .getP12KeyStoreFilePath (
167
+ this .keyPair .getP12KeyStoreFilePath (
142
168
accountConfig .getAccountAddress ());
143
169
} else if (accountConfig .getAccountFileFormat ().compareToIgnoreCase ("pem" ) == 0 ) {
144
170
accountFilePath =
145
- this .keyPairFactory .getPemKeyStoreFilePath (
171
+ this .keyPair .getPemKeyStoreFilePath (
146
172
accountConfig .getAccountAddress ());
147
173
}
148
174
}
@@ -159,7 +185,6 @@ private void loadAccount(ConfigOption configOption) {
159
185
*/
160
186
public void setConfig (ConfigOption config ) {
161
187
this .config = config ;
162
- this .keyPairFactory .setConfig (config );
163
188
}
164
189
165
190
public int getCryptoTypeConfig () {
@@ -231,7 +256,7 @@ public SignatureResult sign(final String message, final CryptoKeyPair keyPair) {
231
256
* @return the string type signature
232
257
*/
233
258
public String sign (KeyTool keyTool , String message ) {
234
- CryptoKeyPair cryptoKeyPair = this .keyPairFactory .createKeyPair (keyTool .getKeyPair ());
259
+ CryptoKeyPair cryptoKeyPair = this .keyPair .createKeyPair (keyTool .getKeyPair ());
235
260
return this .signatureImpl .signWithStringSignature (message , cryptoKeyPair );
236
261
}
237
262
@@ -289,7 +314,7 @@ public boolean verify(final String publicKey, final byte[] message, final byte[]
289
314
* @return a generated key pair
290
315
*/
291
316
public CryptoKeyPair generateRandomKeyPair () {
292
- this .cryptoKeyPair = this .keyPairFactory .generateKeyPair ();
317
+ this .cryptoKeyPair = this .keyPair .generateKeyPair ();
293
318
this .cryptoKeyPair .setConfig (this .config );
294
319
return this .cryptoKeyPair ;
295
320
}
@@ -301,7 +326,7 @@ public CryptoKeyPair generateRandomKeyPair() {
301
326
* @return CryptoKeyPair type key pair
302
327
*/
303
328
public CryptoKeyPair loadKeyPair (KeyPair keyPair ) {
304
- this .cryptoKeyPair = this .keyPairFactory .createKeyPair (keyPair );
329
+ this .cryptoKeyPair = this .keyPair .createKeyPair (keyPair );
305
330
this .cryptoKeyPair .setConfig (this .config );
306
331
return this .cryptoKeyPair ;
307
332
}
@@ -313,11 +338,23 @@ public CryptoKeyPair loadKeyPair(KeyPair keyPair) {
313
338
* @return CryptoKeyPair type key pair
314
339
*/
315
340
public CryptoKeyPair loadKeyPair (String hexedPrivateKey ) {
316
- this .cryptoKeyPair = this .keyPairFactory .createKeyPair (hexedPrivateKey );
341
+ this .cryptoKeyPair = this .keyPair .createKeyPair (hexedPrivateKey );
317
342
this .cryptoKeyPair .setConfig (this .config );
318
343
return this .cryptoKeyPair ;
319
344
}
320
345
346
+ /**
347
+ * Create key pair from a private key string
348
+ *
349
+ * @param hexedPrivateKey a hex string of private key
350
+ * @return CryptoKeyPair type key pair
351
+ */
352
+ public CryptoKeyPair loadHsmKeyPair () {
353
+ HsmSM2KeyPair hsmSM2KeyPair = (HsmSM2KeyPair )this .keyPair ;
354
+ this .cryptoKeyPair = hsmSM2KeyPair .useKeyPair ();
355
+ return this .cryptoKeyPair ;
356
+ }
357
+
321
358
/**
322
359
* Set the key pair in CryptoSuite
323
360
*
@@ -352,7 +389,7 @@ public ConfigOption getConfig() {
352
389
* @return CryptoKeyPair
353
390
*/
354
391
public CryptoKeyPair getKeyPairFactory () {
355
- return this .keyPairFactory ;
392
+ return this .keyPair ;
356
393
}
357
394
358
395
public void destroy () {
0 commit comments