Search in sources :

Example 11 with CryptoException

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());
    }
}
Also used : InvalidArgumentException(org.hyperledger.fabric.sdk.exception.InvalidArgumentException) Properties(java.util.Properties) CryptoException(org.hyperledger.fabric.sdk.exception.CryptoException) Test(org.junit.Test)

Example 12 with CryptoException

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;
    }
}
Also used : CertificateException(java.security.cert.CertificateException) CryptoException(org.hyperledger.fabric.sdk.exception.CryptoException)

Example 13 with CryptoException

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);
    }
}
Also used : InvalidArgumentException(org.hyperledger.fabric.sdk.exception.InvalidArgumentException) BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) CertificateException(java.security.cert.CertificateException) CryptoException(org.hyperledger.fabric.sdk.exception.CryptoException) X509Certificate(java.security.cert.X509Certificate) Certificate(java.security.cert.Certificate)

Example 14 with CryptoException

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;
}
Also used : PemReader(org.bouncycastle.util.io.pem.PemReader) PemObject(org.bouncycastle.util.io.pem.PemObject) PrivateKey(java.security.PrivateKey) ECPrivateKey(java.security.interfaces.ECPrivateKey) PEMParser(org.bouncycastle.openssl.PEMParser) StringReader(java.io.StringReader) JcaPEMKeyConverter(org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter) PEMKeyPair(org.bouncycastle.openssl.PEMKeyPair) CryptoException(org.hyperledger.fabric.sdk.exception.CryptoException) PrivateKeyInfo(org.bouncycastle.asn1.pkcs.PrivateKeyInfo) OperatorCreationException(org.bouncycastle.operator.OperatorCreationException) KeyStoreException(java.security.KeyStoreException) CertPathValidatorException(java.security.cert.CertPathValidatorException) InvalidArgumentException(org.hyperledger.fabric.sdk.exception.InvalidArgumentException) SignatureException(java.security.SignatureException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) CryptoException(org.hyperledger.fabric.sdk.exception.CryptoException)

Example 15 with CryptoException

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);
    }
}
Also used : ECGenParameterSpec(java.security.spec.ECGenParameterSpec) SecureRandom(java.security.SecureRandom) KeyPairGenerator(java.security.KeyPairGenerator) CryptoException(org.hyperledger.fabric.sdk.exception.CryptoException) OperatorCreationException(org.bouncycastle.operator.OperatorCreationException) KeyStoreException(java.security.KeyStoreException) CertPathValidatorException(java.security.cert.CertPathValidatorException) InvalidArgumentException(org.hyperledger.fabric.sdk.exception.InvalidArgumentException) SignatureException(java.security.SignatureException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) CryptoException(org.hyperledger.fabric.sdk.exception.CryptoException)

Aggregations

CryptoException (org.hyperledger.fabric.sdk.exception.CryptoException)20 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)11 CertificateException (java.security.cert.CertificateException)11 InvalidArgumentException (org.hyperledger.fabric.sdk.exception.InvalidArgumentException)11 KeyStoreException (java.security.KeyStoreException)10 IOException (java.io.IOException)9 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)6 InvalidKeyException (java.security.InvalidKeyException)6 SignatureException (java.security.SignatureException)6 CertPathValidatorException (java.security.cert.CertPathValidatorException)6 X509Certificate (java.security.cert.X509Certificate)5 OperatorCreationException (org.bouncycastle.operator.OperatorCreationException)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 Certificate (java.security.cert.Certificate)4 Test (org.junit.Test)4 BufferedInputStream (java.io.BufferedInputStream)3 KeyStore (java.security.KeyStore)3 PrivateKey (java.security.PrivateKey)3 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)2 BigInteger (java.math.BigInteger)2