use of org.hyperledger.fabric.sdk.exception.CryptoException in project fabric-sdk-java by hyperledger.
the class CryptoPrimitivesTest method testGetSetProperties.
@Test
public void testGetSetProperties() throws Exception {
Properties propsIn = new Properties();
try {
// use something different than default!
final String expectHash = "SHA3";
propsIn.setProperty(Config.SECURITY_LEVEL, "384");
propsIn.setProperty(Config.HASH_ALGORITHM, expectHash);
// testCrypto.setProperties(propsIn);
// testCrypto.init();
CryptoSuite testCrypto = CryptoSuiteFactory.getDefault().getCryptoSuite(propsIn);
// assertEquals(BouncyCastleProvider.class, getField(testCrypto, "SECURITY_PROVIDER").getClass());
String expectedCurve = config.getSecurityCurveMapping().get(384);
assertEquals("secp384r1", expectedCurve);
assertEquals(expectedCurve, getField(testCrypto, "curveName"));
assertEquals(384, getField(testCrypto, "securityLevel"));
Properties cryptoProps = ((CryptoPrimitives) testCrypto).getProperties();
assertEquals(cryptoProps.getProperty(Config.SECURITY_LEVEL), "384");
cryptoProps = testCrypto.getProperties();
assertEquals(cryptoProps.getProperty(Config.HASH_ALGORITHM), expectHash);
assertEquals(expectHash, getField(testCrypto, "hashAlgorithm"));
assertEquals(cryptoProps.getProperty(Config.SECURITY_LEVEL), "384");
// Should be exactly same instance as it has the same properties.
assertEquals(testCrypto, CryptoSuiteFactory.getDefault().getCryptoSuite(propsIn));
} catch (CryptoException | InvalidArgumentException e) {
fail("testGetSetProperties should not throw exception. Error: " + e.getMessage());
}
}
use of org.hyperledger.fabric.sdk.exception.CryptoException in project fabric-sdk-java by hyperledger.
the class CryptoPrimitives method resetConfiguration.
// /**
// * Shake256 hash the supplied byte data.
// *
// * @param in byte array to be hashed.
// * @param bitLength of the result.
// * @return the hashed byte data.
// */
// public byte[] shake256(byte[] in, int bitLength) {
//
// if (bitLength % 8 != 0) {
// throw new IllegalArgumentException("bit length not modulo 8");
//
// }
//
// final int byteLen = bitLength / 8;
//
// SHAKEDigest sd = new SHAKEDigest(256);
//
// sd.update(in, 0, in.length);
//
// byte[] out = new byte[byteLen];
//
// sd.doFinal(out, 0, byteLen);
//
// return out;
//
// }
/**
* Resets curve name, hash algorithm and cert factory. Call this method when a config value changes
*
* @throws CryptoException
* @throws InvalidArgumentException
*/
private void resetConfiguration() throws CryptoException, InvalidArgumentException {
setSecurityLevel(securityLevel);
setHashAlgorithm(hashAlgorithm);
try {
cf = CertificateFactory.getInstance(CERTIFICATE_FORMAT);
} catch (CertificateException e) {
CryptoException ex = new CryptoException("Cannot initialize " + CERTIFICATE_FORMAT + " certificate factory. Error = " + e.getMessage(), e);
logger.error(ex.getMessage(), ex);
throw ex;
}
}
use of org.hyperledger.fabric.sdk.exception.CryptoException in project fabric-sdk-java by hyperledger.
the class CryptoPrimitives method addCACertificateToTrustStore.
/**
* addCACertificateToTrustStore adds a CA cert to the set of certificates used for signature validation
*
* @param bytes an X.509 certificate in PEM format in bytes
* @param alias an alias associated with the certificate. Used as shorthand for the certificate during crypto operations
* @throws CryptoException
* @throws InvalidArgumentException
*/
public void addCACertificateToTrustStore(byte[] bytes, String alias) throws CryptoException, InvalidArgumentException {
if (bytes == null) {
throw new InvalidArgumentException("The certificate cannot be null");
}
if (alias == null || alias.isEmpty()) {
throw new InvalidArgumentException("You must assign an alias to a certificate when adding to the trust store");
}
BufferedInputStream bis;
try {
bis = new BufferedInputStream(new ByteArrayInputStream(bytes));
Certificate caCert = cf.generateCertificate(bis);
addCACertificateToTrustStore(caCert, alias);
} catch (CertificateException e) {
throw new CryptoException("Unable to add CA certificate to trust store. Error: " + e.getMessage(), e);
}
}
use of org.hyperledger.fabric.sdk.exception.CryptoException in project fabric-sdk-java by hyperledger.
the class CryptoPrimitives method bytesToPrivateKey.
/**
* Return PrivateKey from pem bytes.
*
* @param pemKey pem-encoded private key
* @return
*/
public PrivateKey bytesToPrivateKey(byte[] pemKey) throws CryptoException {
PrivateKey pk = null;
CryptoException ce = null;
try {
PemReader pr = new PemReader(new StringReader(new String(pemKey)));
PemObject po = pr.readPemObject();
PEMParser pem = new PEMParser(new StringReader(new String(pemKey)));
logger.debug("found private key with type " + po.getType());
if (po.getType().equals("PRIVATE KEY")) {
pk = new JcaPEMKeyConverter().getPrivateKey((PrivateKeyInfo) pem.readObject());
} else {
PEMKeyPair kp = (PEMKeyPair) pem.readObject();
pk = new JcaPEMKeyConverter().getPrivateKey(kp.getPrivateKeyInfo());
}
} catch (Exception e) {
throw new CryptoException("Failed to convert private key bytes", e);
}
return pk;
}
use of org.hyperledger.fabric.sdk.exception.CryptoException in project fabric-sdk-java by hyperledger.
the class CryptoPrimitives method generateKey.
private KeyPair generateKey(String encryptionName, String curveName) throws CryptoException {
try {
ECGenParameterSpec ecGenSpec = new ECGenParameterSpec(curveName);
KeyPairGenerator g = SECURITY_PROVIDER == null ? KeyPairGenerator.getInstance(encryptionName) : KeyPairGenerator.getInstance(encryptionName, SECURITY_PROVIDER);
g.initialize(ecGenSpec, new SecureRandom());
return g.generateKeyPair();
} catch (Exception exp) {
throw new CryptoException("Unable to generate key pair", exp);
}
}
Aggregations