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,44 @@ 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 =
108
+ Integer .parseInt (this .config .getCryptoMaterialConfig ().getHsmKeyIndex ());
109
+ String hsmPassword = this .config .getCryptoMaterialConfig ().getHsmPassword ();
110
+
111
+ HsmSM2Signature hsmSM2Signature = new HsmSM2Signature ();
112
+ hsmSM2Signature .setHsmLibPath (hsmLibPath );
113
+ this .signatureImpl = hsmSM2Signature ;
114
+ this .hashImpl = new SM3Hash ();
115
+ this .keyPair = new HsmSM2KeyPair (hsmLibPath , hsmKeyIndex , hsmPassword );
116
+ HsmSM2KeyPair hsmKeyPair = (HsmSM2KeyPair ) this .keyPair ;
117
+ this .cryptoKeyPair = hsmKeyPair .useKeyPair ();
95
118
} else {
96
119
throw new UnsupportedCryptoTypeException (
97
120
"only support "
98
121
+ CryptoType .ECDSA_TYPE
99
122
+ "/"
100
123
+ CryptoType .SM_TYPE
124
+ + "/"
125
+ + CryptoType .HSM_TYPE
101
126
+ " crypto type" );
102
127
}
103
- // create keyPair randomly
104
- this .generateRandomKeyPair ();
105
128
}
106
129
107
130
/**
@@ -115,16 +138,19 @@ public void loadAccount(String accountFileFormat, String accountFilePath, String
115
138
KeyTool keyTool = null ;
116
139
if (accountFileFormat .compareToIgnoreCase ("p12" ) == 0 ) {
117
140
keyTool = new P12KeyStore (accountFilePath , password );
141
+ this .loadKeyPair (keyTool .getKeyPair ());
118
142
} else if (accountFileFormat .compareToIgnoreCase ("pem" ) == 0 ) {
119
143
keyTool = new PEMKeyStore (accountFilePath );
144
+ this .loadKeyPair (keyTool .getKeyPair ());
145
+ } else if (accountFileFormat .compareToIgnoreCase ("HSM" ) == 0 ) {
146
+ this .loadHsmKeyPair ();
120
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,12 +164,10 @@ 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 (
142
- accountConfig .getAccountAddress ());
167
+ this .keyPair .getP12KeyStoreFilePath (accountConfig .getAccountAddress ());
143
168
} else if (accountConfig .getAccountFileFormat ().compareToIgnoreCase ("pem" ) == 0 ) {
144
169
accountFilePath =
145
- this .keyPairFactory .getPemKeyStoreFilePath (
146
- accountConfig .getAccountAddress ());
170
+ this .keyPair .getPemKeyStoreFilePath (accountConfig .getAccountAddress ());
147
171
}
148
172
}
149
173
this .loadAccount (
@@ -159,7 +183,6 @@ private void loadAccount(ConfigOption configOption) {
159
183
*/
160
184
public void setConfig (ConfigOption config ) {
161
185
this .config = config ;
162
- this .keyPairFactory .setConfig (config );
163
186
}
164
187
165
188
public int getCryptoTypeConfig () {
@@ -231,7 +254,7 @@ public SignatureResult sign(final String message, final CryptoKeyPair keyPair) {
231
254
* @return the string type signature
232
255
*/
233
256
public String sign (KeyTool keyTool , String message ) {
234
- CryptoKeyPair cryptoKeyPair = this .keyPairFactory .createKeyPair (keyTool .getKeyPair ());
257
+ CryptoKeyPair cryptoKeyPair = this .keyPair .createKeyPair (keyTool .getKeyPair ());
235
258
return this .signatureImpl .signWithStringSignature (message , cryptoKeyPair );
236
259
}
237
260
@@ -289,7 +312,7 @@ public boolean verify(final String publicKey, final byte[] message, final byte[]
289
312
* @return a generated key pair
290
313
*/
291
314
public CryptoKeyPair generateRandomKeyPair () {
292
- this .cryptoKeyPair = this .keyPairFactory .generateKeyPair ();
315
+ this .cryptoKeyPair = this .keyPair .generateKeyPair ();
293
316
this .cryptoKeyPair .setConfig (this .config );
294
317
return this .cryptoKeyPair ;
295
318
}
@@ -301,7 +324,7 @@ public CryptoKeyPair generateRandomKeyPair() {
301
324
* @return CryptoKeyPair type key pair
302
325
*/
303
326
public CryptoKeyPair loadKeyPair (KeyPair keyPair ) {
304
- this .cryptoKeyPair = this .keyPairFactory .createKeyPair (keyPair );
327
+ this .cryptoKeyPair = this .keyPair .createKeyPair (keyPair );
305
328
this .cryptoKeyPair .setConfig (this .config );
306
329
return this .cryptoKeyPair ;
307
330
}
@@ -313,11 +336,23 @@ public CryptoKeyPair loadKeyPair(KeyPair keyPair) {
313
336
* @return CryptoKeyPair type key pair
314
337
*/
315
338
public CryptoKeyPair loadKeyPair (String hexedPrivateKey ) {
316
- this .cryptoKeyPair = this .keyPairFactory .createKeyPair (hexedPrivateKey );
339
+ this .cryptoKeyPair = this .keyPair .createKeyPair (hexedPrivateKey );
317
340
this .cryptoKeyPair .setConfig (this .config );
318
341
return this .cryptoKeyPair ;
319
342
}
320
343
344
+ /**
345
+ * Create key pair from a private key string
346
+ *
347
+ * @param hexedPrivateKey a hex string of private key
348
+ * @return CryptoKeyPair type key pair
349
+ */
350
+ public CryptoKeyPair loadHsmKeyPair () {
351
+ HsmSM2KeyPair hsmSM2KeyPair = (HsmSM2KeyPair ) this .keyPair ;
352
+ this .cryptoKeyPair = hsmSM2KeyPair .useKeyPair ();
353
+ return this .cryptoKeyPair ;
354
+ }
355
+
321
356
/**
322
357
* Set the key pair in CryptoSuite
323
358
*
@@ -352,7 +387,7 @@ public ConfigOption getConfig() {
352
387
* @return CryptoKeyPair
353
388
*/
354
389
public CryptoKeyPair getKeyPairFactory () {
355
- return this .keyPairFactory ;
390
+ return this .keyPair ;
356
391
}
357
392
358
393
public void destroy () {
0 commit comments