16
16
import java .io .File ;
17
17
import java .io .FileInputStream ;
18
18
import java .io .IOException ;
19
+ import java .util .Collections ;
20
+ import java .util .HashMap ;
21
+ import java .util .Map ;
19
22
import java .util .Properties ;
20
23
21
24
import org .apache .commons .logging .Log ;
22
25
import org .apache .commons .logging .LogFactory ;
23
26
import org .apache .log4j .Level ;
27
+ import org .bouncycastle .jce .provider .BouncyCastleProvider ;
28
+
29
+ import static java .lang .String .format ;
24
30
25
31
/**
26
32
* Config allows for a global config of the toolkit. Central location for all
@@ -37,28 +43,40 @@ public class Config {
37
43
38
44
private static final String DEFAULT_CONFIG = "config.properties" ;
39
45
public static final String ORG_HYPERLEDGER_FABRIC_SDK_CONFIGURATION = "org.hyperledger.fabric.sdk.configuration" ;
40
- public static final String SECURITY_LEVEL = "org.hyperledger.fabric.sdk.security_level" ;
41
- public static final String HASH_ALGORITHM = "org.hyperledger.fabric.sdk.hash_algorithm" ;
46
+ /**
47
+ * Timeout settings
48
+ **/
42
49
public static final String PROPOSAL_WAIT_TIME = "org.hyperledger.fabric.sdk.proposal.wait.time" ;
43
50
public static final String CHANNEL_CONFIG_WAIT_TIME = "org.hyperledger.fabric.sdk.channelconfig.wait_time" ;
44
51
public static final String ORDERER_RETRY_WAIT_TIME = "org.hyperledger.fabric.sdk.orderer_retry.wait_time" ;
45
52
public static final String ORDERER_WAIT_TIME = "org.hyperledger.fabric.sdk.orderer.ordererWaitTimeMilliSecs" ;
46
53
public static final String EVENTHUB_CONNECTION_WAIT_TIME = "org.hyperledger.fabric.sdk.eventhub_connection.wait_time" ;
47
- public static final String PROPOSAL_CONSISTENCY_VALIDATION = "org.hyperledger.fabric.sdk.proposal.consistency_validation" ;
48
54
public static final String GENESISBLOCK_WAIT_TIME = "org.hyperledger.fabric.sdk.channel.genesisblock_wait_time" ;
55
+ /**
56
+ * Crypto configuration settings
57
+ **/
58
+ public static final String DEFAULT_CRYPTO_SUITE_FACTORY = "org.hyperledger.fabric.sdk.crypto.default_crypto_suite_factory" ;
59
+ public static final String SECURITY_LEVEL = "org.hyperledger.fabric.sdk.security_level" ;
60
+ public static final String SECURITY_PROVIDER_CLASS_NAME = "org.hyperledger.fabric.sdk.security_provider_class_name" ;
61
+ public static final String SECURITY_CURVE_MAPPING = "org.hyperledger.fabric.sdk.security_curve_mapping" ;
62
+ public static final String HASH_ALGORITHM = "org.hyperledger.fabric.sdk.hash_algorithm" ;
49
63
public static final String ASYMMETRIC_KEY_TYPE = "org.hyperledger.fabric.sdk.crypto.asymmetric_key_type" ;
50
- public static final String KEY_AGREEMENT_ALGORITHM = "org.hyperledger.fabric.sdk.crypto.key_agreement_algorithm" ;
51
- public static final String SYMMETRIC_KEY_TYPE = "org.hyperledger.fabric.sdk.crypto.symmetric_key_type" ;
52
- public static final String SYMMETRIC_KEY_BYTE_COUNT = "org.hyperledger.fabric.sdk.crypto.symmetric_key_byte_count" ;
53
- public static final String SYMMETRIC_ALGORITHM = "org.hyperledger.fabric.sdk.crypto.symmetric_algorithm" ;
54
- public static final String MAC_KEY_BYTE_COUNT = "org.hyperledger.fabric.sdk.crypto.mac_key_byte_count" ;
64
+
55
65
public static final String CERTIFICATE_FORMAT = "org.hyperledger.fabric.sdk.crypto.certificate_format" ;
56
66
public static final String SIGNATURE_ALGORITHM = "org.hyperledger.fabric.sdk.crypto.default_signature_algorithm" ;
67
+ /**
68
+ * Logging settings
69
+ **/
57
70
public static final String MAX_LOG_STRING_LENGTH = "org.hyperledger.fabric.sdk.log.stringlengthmax" ;
58
71
public static final String EXTRALOGLEVEL = "org.hyperledger.fabric.sdk.log.extraloglevel" ;
59
72
public static final String LOGGERLEVEL = "org.hyperledger.fabric.sdk.loglevel" ; // ORG_HYPERLEDGER_FABRIC_SDK_LOGLEVEL=TRACE,DEBUG
60
73
public static final String DIAGNOTISTIC_FILE_DIRECTORY = "org.hyperledger.fabric.sdk.diagnosticFileDir" ; //ORG_HYPERLEDGER_FABRIC_SDK_DIAGNOSTICFILEDIR
61
74
75
+ /**
76
+ * Miscellaneous settings
77
+ **/
78
+ public static final String PROPOSAL_CONSISTENCY_VALIDATION = "org.hyperledger.fabric.sdk.proposal.consistency_validation" ;
79
+
62
80
private static Config config ;
63
81
private static final Properties sdkProperties = new Properties ();
64
82
@@ -69,39 +87,51 @@ private Config() {
69
87
try {
70
88
loadFile = new File (System .getProperty (ORG_HYPERLEDGER_FABRIC_SDK_CONFIGURATION , DEFAULT_CONFIG ))
71
89
.getAbsoluteFile ();
72
- logger .debug (String . format ("Loading configuration from %s and it is present: %b" , loadFile .toString (),
90
+ logger .debug (format ("Loading configuration from %s and it is present: %b" , loadFile .toString (),
73
91
loadFile .exists ()));
74
92
configProps = new FileInputStream (loadFile );
75
93
sdkProperties .load (configProps );
76
94
77
95
} catch (IOException e ) {
78
- logger .warn (String . format ("Failed to load any configuration from: %s. Using toolkit defaults" ,
96
+ logger .warn (format ("Failed to load any configuration from: %s. Using toolkit defaults" ,
79
97
DEFAULT_CONFIG ));
80
98
} finally {
81
99
82
100
// Default values
101
+ /**
102
+ * Timeout settings
103
+ **/
104
+ defaultProperty (PROPOSAL_WAIT_TIME , "20000" );
105
+ defaultProperty (CHANNEL_CONFIG_WAIT_TIME , "15000" );
106
+ defaultProperty (ORDERER_RETRY_WAIT_TIME , "200" );
107
+ defaultProperty (ORDERER_WAIT_TIME , "3000" );
108
+ defaultProperty (EVENTHUB_CONNECTION_WAIT_TIME , "1000" );
109
+ defaultProperty (GENESISBLOCK_WAIT_TIME , "5000" );
110
+
111
+ /**
112
+ * Crypto configuration settings
113
+ **/
114
+ defaultProperty (DEFAULT_CRYPTO_SUITE_FACTORY , "org.hyperledger.fabric.sdk.security.HLSDKJCryptoSuiteFactory" );
115
+ defaultProperty (SECURITY_LEVEL , "256" );
116
+ defaultProperty (SECURITY_PROVIDER_CLASS_NAME , BouncyCastleProvider .class .getName ());
117
+ defaultProperty (SECURITY_CURVE_MAPPING , "256=secp256r1:384=secp384r1" );
118
+ defaultProperty (HASH_ALGORITHM , "SHA2" );
83
119
defaultProperty (ASYMMETRIC_KEY_TYPE , "EC" );
84
- defaultProperty (KEY_AGREEMENT_ALGORITHM , "ECDH" );
85
- defaultProperty (SYMMETRIC_KEY_TYPE , "AES" );
86
- defaultProperty (SYMMETRIC_KEY_BYTE_COUNT , "32" );
87
- defaultProperty (SYMMETRIC_ALGORITHM , "AES/CFB/NoPadding" );
88
- defaultProperty (MAC_KEY_BYTE_COUNT , "32" );
120
+
89
121
defaultProperty (CERTIFICATE_FORMAT , "X.509" );
90
122
defaultProperty (SIGNATURE_ALGORITHM , "SHA256withECDSA" );
91
- defaultProperty (SECURITY_LEVEL , "256" );
92
- defaultProperty (HASH_ALGORITHM , "SHA2" );
93
- defaultProperty (PROPOSAL_CONSISTENCY_VALIDATION , "true" );
94
123
95
- defaultProperty (PROPOSAL_WAIT_TIME , "20000" );
96
- defaultProperty (GENESISBLOCK_WAIT_TIME , "5000" );
124
+ /**
125
+ * Logging settings
126
+ **/
97
127
defaultProperty (MAX_LOG_STRING_LENGTH , "64" );
98
128
defaultProperty (EXTRALOGLEVEL , "0" );
99
129
defaultProperty (LOGGERLEVEL , null );
100
130
defaultProperty (DIAGNOTISTIC_FILE_DIRECTORY , null );
101
- defaultProperty ( CHANNEL_CONFIG_WAIT_TIME , "15000" );
102
- defaultProperty ( ORDERER_RETRY_WAIT_TIME , "200" );
103
- defaultProperty ( ORDERER_WAIT_TIME , "3000" );
104
- defaultProperty (EVENTHUB_CONNECTION_WAIT_TIME , "1000 " );
131
+ /**
132
+ * Miscellaneous settings
133
+ */
134
+ defaultProperty (PROPOSAL_CONSISTENCY_VALIDATION , "true " );
105
135
106
136
final String inLogLevel = sdkProperties .getProperty (LOGGERLEVEL );
107
137
@@ -171,7 +201,7 @@ private String getProperty(String property) {
171
201
String ret = sdkProperties .getProperty (property );
172
202
173
203
if (null == ret ) {
174
- logger .warn (String . format ("No configuration value found for '%s'" , property ));
204
+ logger .warn (format ("No configuration value found for '%s'" , property ));
175
205
}
176
206
return ret ;
177
207
}
@@ -207,6 +237,16 @@ public int getSecurityLevel() {
207
237
208
238
}
209
239
240
+ /**
241
+ * Get the configured security provider.
242
+ * This is the security provider used for the default SDK crypto suite factory.
243
+ *
244
+ * @return the security provider.
245
+ */
246
+ public String getSecurityProviderClassName () {
247
+ return getProperty (SECURITY_PROVIDER_CLASS_NAME );
248
+ }
249
+
210
250
/**
211
251
* Get the name of the configured hash algorithm, used for digital signatures.
212
252
*
@@ -217,6 +257,51 @@ public String getHashAlgorithm() {
217
257
218
258
}
219
259
260
+ private Map <Integer , String > curveMapping = null ;
261
+
262
+ /**
263
+ * Get a mapping from strength to curve desired.
264
+ *
265
+ * @return mapping from strength to curve name to use.
266
+ */
267
+ public Map <Integer , String > getSecurityCurveMapping () {
268
+
269
+ if (curveMapping == null ) {
270
+
271
+ curveMapping = parseSecurityCurveMappings (getProperty (SECURITY_CURVE_MAPPING ));
272
+ }
273
+
274
+ return Collections .unmodifiableMap (curveMapping );
275
+ }
276
+
277
+ public static Map <Integer , String > parseSecurityCurveMappings (final String property ) {
278
+ Map <Integer , String > lcurveMapping = new HashMap <>(8 );
279
+
280
+ if (property != null && !property .isEmpty ()) { //empty will be caught later.
281
+
282
+ String [] cmaps = property .split ("[ \t ]*:[ \t ]*" );
283
+ for (String mape : cmaps ) {
284
+
285
+ String [] ep = mape .split ("[ \t ]*=[ \t ]*" );
286
+ if (ep .length != 2 ) {
287
+ logger .warn (format ("Bad curve mapping for %s in property %s" , mape , SECURITY_CURVE_MAPPING ));
288
+ continue ;
289
+ }
290
+
291
+ try {
292
+ int parseInt = Integer .parseInt (ep [0 ]);
293
+ lcurveMapping .put (parseInt , ep [1 ]);
294
+ } catch (NumberFormatException e ) {
295
+ logger .warn (format ("Bad curve mapping. Integer needed for strength %s for %s in property %s" ,
296
+ ep [0 ], mape , SECURITY_CURVE_MAPPING ));
297
+ }
298
+
299
+ }
300
+
301
+ }
302
+ return lcurveMapping ;
303
+ }
304
+
220
305
/**
221
306
* Get the timeout for a single proposal request to endorser.
222
307
*
@@ -265,26 +350,6 @@ public String getAsymmetricKeyType() {
265
350
return getProperty (ASYMMETRIC_KEY_TYPE );
266
351
}
267
352
268
- public String getKeyAgreementAlgorithm () {
269
- return getProperty (KEY_AGREEMENT_ALGORITHM );
270
- }
271
-
272
- public String getSymmetricKeyType () {
273
- return getProperty (SYMMETRIC_KEY_TYPE );
274
- }
275
-
276
- public int getSymmetricKeyByteCount () {
277
- return Integer .parseInt (getProperty (SYMMETRIC_KEY_BYTE_COUNT ));
278
- }
279
-
280
- public String getSymmetricAlgorithm () {
281
- return getProperty (SYMMETRIC_ALGORITHM );
282
- }
283
-
284
- public int getMACKeyByteCount () {
285
- return Integer .parseInt (getProperty (MAC_KEY_BYTE_COUNT ));
286
- }
287
-
288
353
public String getCertificateFormat () {
289
354
return getProperty (CERTIFICATE_FORMAT );
290
355
}
@@ -293,6 +358,10 @@ public String getSignatureAlgorithm() {
293
358
return getProperty (SIGNATURE_ALGORITHM );
294
359
}
295
360
361
+ public String getDefaultCryptoSuiteFactory () {
362
+ return getProperty (DEFAULT_CRYPTO_SUITE_FACTORY );
363
+ }
364
+
296
365
public int maxLogStringLength () {
297
366
return Integer .parseInt (getProperty (MAX_LOG_STRING_LENGTH ));
298
367
}
0 commit comments