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);
}
}
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;
}
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) {
}
}
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());
}
}
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());
}
}
Aggregations