Search in sources :

Example 6 with CryptoException

use of org.hyperledger.fabric.sdk.exception.CryptoException in project fabric-sdk-java by hyperledger.

the class PeerEventServiceClient method peerVent.

// =========================================================
// Peer eventing
void peerVent(TransactionContext transactionContext) throws TransactionException {
    final Envelope envelope;
    try {
        Ab.SeekPosition.Builder start = Ab.SeekPosition.newBuilder();
        if (null != peerOptions.getNewest()) {
            start.setNewest(Ab.SeekNewest.getDefaultInstance());
        } else if (peerOptions.getStartEvents() != null) {
            start.setSpecified(Ab.SeekSpecified.newBuilder().setNumber(peerOptions.getStartEvents()));
        } else {
            start.setNewest(Ab.SeekNewest.getDefaultInstance());
        }
        // properties.
        envelope = createSeekInfoEnvelope(transactionContext, start.build(), Ab.SeekPosition.newBuilder().setSpecified(Ab.SeekSpecified.newBuilder().setNumber(peerOptions.getStopEvents()).build()).build(), SeekInfo.SeekBehavior.BLOCK_UNTIL_READY, clientTLSCertificateDigest);
        connectEnvelope(envelope);
    } catch (CryptoException e) {
        throw new TransactionException(e);
    }
}
Also used : TransactionException(org.hyperledger.fabric.sdk.exception.TransactionException) ProtoUtils.createSeekInfoEnvelope(org.hyperledger.fabric.sdk.transaction.ProtoUtils.createSeekInfoEnvelope) Envelope(org.hyperledger.fabric.protos.common.Common.Envelope) CryptoException(org.hyperledger.fabric.sdk.exception.CryptoException)

Example 7 with CryptoException

use of org.hyperledger.fabric.sdk.exception.CryptoException in project fabric-sdk-java by hyperledger.

the class ProposalResponse method verify.

/*
     * Verifies that a Proposal response is properly signed. The payload is the
     * concatenation of the response payload byte string and the endorsement The
     * certificate (public key) is gotten from the Endorsement.Endorser.IdBytes
     * field
     *
     * @param crypto the CryptoPrimitives instance to be used for signing and
     * verification
     *
     * @return true/false depending on result of signature verification
     */
public boolean verify(CryptoSuite crypto) {
    if (isVerified()) {
        // check if this proposalResponse was already verified   by client code
        return isVerified();
    }
    if (isInvalid()) {
        this.isVerified = false;
    }
    FabricProposalResponse.Endorsement endorsement = this.proposalResponse.getEndorsement();
    ByteString sig = endorsement.getSignature();
    try {
        Identities.SerializedIdentity endorser = Identities.SerializedIdentity.parseFrom(endorsement.getEndorser());
        ByteString plainText = proposalResponse.getPayload().concat(endorsement.getEndorser());
        if (config.extraLogLevel(10)) {
            if (null != diagnosticFileDumper) {
                StringBuilder sb = new StringBuilder(10000);
                sb.append("payload TransactionBuilderbytes in hex: " + DatatypeConverter.printHexBinary(proposalResponse.getPayload().toByteArray()));
                sb.append("\n");
                sb.append("endorser bytes in hex: " + DatatypeConverter.printHexBinary(endorsement.getEndorser().toByteArray()));
                sb.append("\n");
                sb.append("plainText bytes in hex: " + DatatypeConverter.printHexBinary(plainText.toByteArray()));
                logger.trace("payload TransactionBuilderbytes:  " + diagnosticFileDumper.createDiagnosticFile(sb.toString()));
            }
        }
        this.isVerified = crypto.verify(endorser.getIdBytes().toByteArray(), config.getSignatureAlgorithm(), sig.toByteArray(), plainText.toByteArray());
    } catch (InvalidProtocolBufferException | CryptoException e) {
        logger.error("verify: Cannot retrieve peer identity from ProposalResponse. Error is: " + e.getMessage(), e);
        this.isVerified = false;
    }
    return this.isVerified;
}
Also used : ByteString(com.google.protobuf.ByteString) FabricProposalResponse(org.hyperledger.fabric.protos.peer.FabricProposalResponse) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) Identities(org.hyperledger.fabric.protos.msp.Identities) CryptoException(org.hyperledger.fabric.sdk.exception.CryptoException)

Example 8 with CryptoException

use of org.hyperledger.fabric.sdk.exception.CryptoException in project fabric-sdk-java by hyperledger.

the class CryptoPrimitivesTest method testSignNullData.

@Test
public void testSignNullData() {
    PrivateKey key;
    try {
        key = (PrivateKey) crypto.getTrustStore().getKey("key", "123456".toCharArray());
        crypto.sign(key, null);
        Assert.fail("sign() should have thrown an exception");
    } catch (UnrecoverableKeyException | KeyStoreException | NoSuchAlgorithmException e) {
        Assert.fail("Could not create private key. Error: " + e.getMessage());
    } catch (CryptoException e) {
    }
}
Also used : PrivateKey(java.security.PrivateKey) UnrecoverableKeyException(java.security.UnrecoverableKeyException) KeyStoreException(java.security.KeyStoreException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) CryptoException(org.hyperledger.fabric.sdk.exception.CryptoException) Test(org.junit.Test)

Example 9 with CryptoException

use of org.hyperledger.fabric.sdk.exception.CryptoException in project fabric-sdk-java by hyperledger.

the class CryptoPrimitivesTest method testSetTrustStore.

@Test
public void testSetTrustStore() throws Throwable {
    try {
        CryptoPrimitives myCrypto = new CryptoPrimitives();
        KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
        keyStore.load(null, null);
        // myCrypto.setTrustStore(keyStore);
        TestUtils.invokeMethod(myCrypto, "setTrustStore", keyStore);
        assertSame(keyStore, myCrypto.getTrustStore());
    } catch (CryptoException | KeyStoreException | NoSuchAlgorithmException | CertificateException | IOException e) {
        fail("testSetTrustStore() should not have thrown Exception. Error: " + e.getMessage());
    }
}
Also used : CertificateException(java.security.cert.CertificateException) KeyStoreException(java.security.KeyStoreException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) CryptoException(org.hyperledger.fabric.sdk.exception.CryptoException) KeyStore(java.security.KeyStore) Test(org.junit.Test)

Example 10 with CryptoException

use of org.hyperledger.fabric.sdk.exception.CryptoException in project fabric-sdk-java by hyperledger.

the class CryptoPrimitivesTest method testSign.

@Test
@Ignore
public // TODO need to regen key now that we're using CryptoSuite
void testSign() {
    byte[] plainText = "123456".getBytes(UTF_8);
    byte[] signature;
    try {
        PrivateKey key = (PrivateKey) crypto.getTrustStore().getKey("key", "123456".toCharArray());
        signature = crypto.sign(key, plainText);
        BufferedInputStream bis = new BufferedInputStream(this.getClass().getResourceAsStream("/keypair-signed.crt"));
        byte[] cert = IOUtils.toByteArray(bis);
        bis.close();
        assertTrue(crypto.verify(cert, SIGNING_ALGORITHM, signature, plainText));
    } catch (KeyStoreException | CryptoException | IOException | UnrecoverableKeyException | NoSuchAlgorithmException e) {
        fail("Could not verify signature. Error: " + e.getMessage());
    }
}
Also used : PrivateKey(java.security.PrivateKey) UnrecoverableKeyException(java.security.UnrecoverableKeyException) BufferedInputStream(java.io.BufferedInputStream) KeyStoreException(java.security.KeyStoreException) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) CryptoException(org.hyperledger.fabric.sdk.exception.CryptoException) Ignore(org.junit.Ignore) Test(org.junit.Test)

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