Search in sources :

Example 1 with CryptoPrimitives

use of org.hyperledger.fabric.sdk.security.CryptoPrimitives in project fabric-sdk-java by hyperledger.

the class HFCAClientTest method testSetCryptoSuite.

@Test
public void testSetCryptoSuite() throws Exception {
    HFCAClient client = HFCAClient.createNewInstance("client", "http://localhost:99", null);
    CryptoPrimitives testcrypt = new CryptoPrimitives();
    client.setCryptoSuite(testcrypt);
    Assert.assertEquals(testcrypt, client.getCryptoSuite());
}
Also used : CryptoPrimitives(org.hyperledger.fabric.sdk.security.CryptoPrimitives) Test(org.junit.Test)

Example 2 with CryptoPrimitives

use of org.hyperledger.fabric.sdk.security.CryptoPrimitives in project fabric-sdk-java by hyperledger.

the class HFCAClientTest method testRegisterNoServerResponse.

@Test
public void testRegisterNoServerResponse() throws Exception {
    thrown.expect(RegistrationException.class);
    thrown.expectMessage("Error while registering the user");
    Properties testProps = new Properties();
    HFCAClient client = HFCAClient.createNewInstance("client", "https://localhost:99", testProps);
    CryptoPrimitives testcrypt = new CryptoPrimitives();
    client.setCryptoSuite(testcrypt);
    RegistrationRequest regreq = new RegistrationRequest("name", "affiliation");
    client.register(regreq, admin);
}
Also used : Properties(java.util.Properties) CryptoPrimitives(org.hyperledger.fabric.sdk.security.CryptoPrimitives) Test(org.junit.Test)

Example 3 with CryptoPrimitives

use of org.hyperledger.fabric.sdk.security.CryptoPrimitives in project fabric-sdk-java by hyperledger.

the class HFCAClient method setUpSSL.

private void setUpSSL() throws InvalidArgumentException {
    if (cryptoPrimitives == null) {
        try {
            cryptoPrimitives = new CryptoPrimitives();
            cryptoPrimitives.init();
        } catch (Exception e) {
            throw new InvalidArgumentException(e);
        }
    }
    if (isSSL && null == registry) {
        if (properties.containsKey("pemBytes") && properties.containsKey("pemFile")) {
            throw new InvalidArgumentException("Properties can not have both \"pemBytes\" and \"pemFile\" specified. ");
        }
        try {
            if (properties.containsKey("pemBytes")) {
                byte[] pemBytes = (byte[]) properties.get("pemBytes");
                cryptoPrimitives.addCACertificateToTrustStore(pemBytes, pemBytes.toString());
            } else {
                String pemFile = properties.getProperty("pemFile");
                if (pemFile != null) {
                    cryptoPrimitives.addCACertificateToTrustStore(new File(pemFile), pemFile);
                }
            }
            SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(cryptoPrimitives.getTrustStore(), null).build();
            ConnectionSocketFactory sf;
            if (null != properties && "true".equals(properties.getProperty("allowAllHostNames"))) {
                AllHostsSSLSocketFactory msf = new AllHostsSSLSocketFactory(cryptoPrimitives.getTrustStore());
                msf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
                sf = msf;
            } else {
                sf = new SSLConnectionSocketFactory(sslContext);
            }
            registry = RegistryBuilder.<ConnectionSocketFactory>create().register("https", sf).register("http", new PlainConnectionSocketFactory()).build();
        } catch (Exception e) {
            logger.error(e);
            throw new InvalidArgumentException(e);
        }
    }
}
Also used : SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) ConnectionSocketFactory(org.apache.http.conn.socket.ConnectionSocketFactory) PlainConnectionSocketFactory(org.apache.http.conn.socket.PlainConnectionSocketFactory) InvalidArgumentException(org.hyperledger.fabric_ca.sdk.exception.InvalidArgumentException) ASN1OctetString(org.bouncycastle.asn1.ASN1OctetString) SSLContext(javax.net.ssl.SSLContext) PlainConnectionSocketFactory(org.apache.http.conn.socket.PlainConnectionSocketFactory) File(java.io.File) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) InvalidArgumentException(org.hyperledger.fabric_ca.sdk.exception.InvalidArgumentException) URISyntaxException(java.net.URISyntaxException) RegistrationException(org.hyperledger.fabric_ca.sdk.exception.RegistrationException) KeyStoreException(java.security.KeyStoreException) AffiliationException(org.hyperledger.fabric_ca.sdk.exception.AffiliationException) GenerateCRLException(org.hyperledger.fabric_ca.sdk.exception.GenerateCRLException) KeyManagementException(java.security.KeyManagementException) IdentityException(org.hyperledger.fabric_ca.sdk.exception.IdentityException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) EnrollmentException(org.hyperledger.fabric_ca.sdk.exception.EnrollmentException) UnrecoverableKeyException(java.security.UnrecoverableKeyException) RevocationException(org.hyperledger.fabric_ca.sdk.exception.RevocationException) ParseException(org.apache.http.ParseException) MalformedURLException(java.net.MalformedURLException) InfoException(org.hyperledger.fabric_ca.sdk.exception.InfoException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) HTTPException(org.hyperledger.fabric_ca.sdk.exception.HTTPException) CryptoPrimitives(org.hyperledger.fabric.sdk.security.CryptoPrimitives)

Example 4 with CryptoPrimitives

use of org.hyperledger.fabric.sdk.security.CryptoPrimitives in project fabric-sdk-java by hyperledger.

the class ProtoUtils method getSignatureHeaderAsByteString.

public static ByteString getSignatureHeaderAsByteString(User user, TransactionContext transactionContext) {
    final Identities.SerializedIdentity identity = ProtoUtils.createSerializedIdentity(user);
    if (isDebugLevel) {
        String cert = user.getEnrollment().getCert();
        if (null == suite) {
            try {
                suite = CryptoSuite.Factory.getCryptoSuite();
            } catch (Exception e) {
            // best try.
            }
        }
        if (null != suite && suite instanceof CryptoPrimitives) {
            CryptoPrimitives cp = (CryptoPrimitives) suite;
            byte[] der = cp.certificateToDER(cert);
            if (null != der && der.length > 0) {
                cert = toHexString(suite.hash(der));
            }
        }
        logger.debug(format("SignatureHeader: nonce: %s, User:%s, MSPID: %s, idBytes: %s", toHexString(transactionContext.getNonce()), user.getName(), identity.getMspid(), cert));
    }
    return SignatureHeader.newBuilder().setCreator(identity.toByteString()).setNonce(transactionContext.getNonce()).build().toByteString();
}
Also used : ByteString(com.google.protobuf.ByteString) Utils.logString(org.hyperledger.fabric.sdk.helper.Utils.logString) Utils.toHexString(org.hyperledger.fabric.sdk.helper.Utils.toHexString) Identities(org.hyperledger.fabric.protos.msp.Identities) CryptoException(org.hyperledger.fabric.sdk.exception.CryptoException) CryptoPrimitives(org.hyperledger.fabric.sdk.security.CryptoPrimitives)

Example 5 with CryptoPrimitives

use of org.hyperledger.fabric.sdk.security.CryptoPrimitives in project fabric-sdk-java by hyperledger.

the class HFCAAffiliationTest method setupBeforeClass.

@BeforeClass
public static void setupBeforeClass() {
    try {
        crypto = new CryptoPrimitives();
        crypto.init();
    } catch (Exception e) {
        throw new RuntimeException("HFCAAffiliationTest.setupBeforeClass failed!", e);
    }
}
Also used : InvalidArgumentException(org.hyperledger.fabric_ca.sdk.exception.InvalidArgumentException) MalformedURLException(java.net.MalformedURLException) CryptoException(org.hyperledger.fabric.sdk.exception.CryptoException) EnrollmentException(org.hyperledger.fabric_ca.sdk.exception.EnrollmentException) AffiliationException(org.hyperledger.fabric_ca.sdk.exception.AffiliationException) ExpectedException(org.junit.rules.ExpectedException) CryptoPrimitives(org.hyperledger.fabric.sdk.security.CryptoPrimitives) BeforeClass(org.junit.BeforeClass)

Aggregations

CryptoPrimitives (org.hyperledger.fabric.sdk.security.CryptoPrimitives)9 MalformedURLException (java.net.MalformedURLException)4 CryptoException (org.hyperledger.fabric.sdk.exception.CryptoException)4 EnrollmentException (org.hyperledger.fabric_ca.sdk.exception.EnrollmentException)4 InvalidArgumentException (org.hyperledger.fabric_ca.sdk.exception.InvalidArgumentException)4 Test (org.junit.Test)4 Properties (java.util.Properties)3 BeforeClass (org.junit.BeforeClass)3 ExpectedException (org.junit.rules.ExpectedException)3 AffiliationException (org.hyperledger.fabric_ca.sdk.exception.AffiliationException)2 IdentityException (org.hyperledger.fabric_ca.sdk.exception.IdentityException)2 RegistrationException (org.hyperledger.fabric_ca.sdk.exception.RegistrationException)2 RevocationException (org.hyperledger.fabric_ca.sdk.exception.RevocationException)2 ByteString (com.google.protobuf.ByteString)1 File (java.io.File)1 IOException (java.io.IOException)1 URISyntaxException (java.net.URISyntaxException)1 KeyManagementException (java.security.KeyManagementException)1 KeyStoreException (java.security.KeyStoreException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1