Search in sources :

Example 6 with AssertionException

use of org.eclipse.scout.rt.platform.util.Assertions.AssertionException in project scout.rt by eclipse.

the class SunSecurityProvider method createMac.

@Override
public byte[] createMac(byte[] password, InputStream data) {
    Assertions.assertGreater(Assertions.assertNotNull(password, "no password provided").length, 0, "empty password not allowed");
    if (data == null) {
        throw new AssertionException("no data provided");
    }
    try {
        String algorithm = getMacAlgorithm();
        SecretKeySpec key = new SecretKeySpec(password, 0, password.length, algorithm);
        Mac mac = Mac.getInstance(algorithm, getMacAlgorithmProvider());
        mac.init(key);
        int n;
        byte[] buf = new byte[BUF_SIZE];
        while ((n = data.read(buf)) >= 0) {
            mac.update(buf, 0, n);
        }
        return mac.doFinal();
    } catch (NoSuchAlgorithmException | InvalidKeyException | IllegalStateException | IOException | NoSuchProviderException e) {
        throw new ProcessingException("unable to create signature.", e);
    }
}
Also used : AssertionException(org.eclipse.scout.rt.platform.util.Assertions.AssertionException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) InvalidKeyException(java.security.InvalidKeyException) Mac(javax.crypto.Mac) SecretKeySpec(javax.crypto.spec.SecretKeySpec) NoSuchProviderException(java.security.NoSuchProviderException) ProcessingException(org.eclipse.scout.rt.platform.exception.ProcessingException)

Example 7 with AssertionException

use of org.eclipse.scout.rt.platform.util.Assertions.AssertionException in project scout.rt by eclipse.

the class SunSecurityProvider method verifySignature.

@Override
public boolean verifySignature(byte[] publicKey, InputStream data, byte[] signatureToVerify) {
    Assertions.assertGreater(Assertions.assertNotNull(publicKey, "no public key provided").length, 0, "empty public key not allowed");
    Assertions.assertGreater(Assertions.assertNotNull(signatureToVerify, "no signature provided").length, 0, "empty signature not allowed");
    if (data == null) {
        throw new AssertionException("no data provided");
    }
    try {
        // create public key from bytes
        KeyFactory keyFactory = KeyFactory.getInstance(getKeyPairGenerationAlgorithm(), getSignatureProvider());
        X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(publicKey);
        PublicKey pubKey = keyFactory.generatePublic(pubKeySpec);
        // verify signature
        Signature sig = Signature.getInstance(getSignatureAlgorithm(), getSignatureProvider());
        sig.initVerify(pubKey);
        int n;
        byte[] buf = new byte[BUF_SIZE];
        while ((n = data.read(buf)) >= 0) {
            sig.update(buf, 0, n);
        }
        return sig.verify(signatureToVerify);
    } catch (NoSuchAlgorithmException | NoSuchProviderException | InvalidKeySpecException | InvalidKeyException | SignatureException | IOException t) {
        throw new ProcessingException("unable to verify signature", t);
    }
}
Also used : PublicKey(java.security.PublicKey) AssertionException(org.eclipse.scout.rt.platform.util.Assertions.AssertionException) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) SignatureException(java.security.SignatureException) IOException(java.io.IOException) InvalidKeyException(java.security.InvalidKeyException) Signature(java.security.Signature) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) NoSuchProviderException(java.security.NoSuchProviderException) SecretKeyFactory(javax.crypto.SecretKeyFactory) KeyFactory(java.security.KeyFactory) ProcessingException(org.eclipse.scout.rt.platform.exception.ProcessingException)

Example 8 with AssertionException

use of org.eclipse.scout.rt.platform.util.Assertions.AssertionException in project scout.rt by eclipse.

the class SunSecurityProvider method doCrypt.

protected void doCrypt(InputStream input, OutputStream output, EncryptionKey key, int mode) {
    Assertions.assertNotNull(key, "key must not be null.");
    if (input == null) {
        throw new AssertionException("input must not be null.");
    }
    if (output == null) {
        throw new AssertionException("output must not be null.");
    }
    try {
        Cipher cipher = Cipher.getInstance(getCipherAlgorithm() + "/" + getCipherAlgorithmMode() + "/" + getCipherAlgorithmPadding(), getCipherAlgorithmProvider());
        cipher.init(mode, key.get(), key.params());
        try (OutputStream out = new CipherOutputStream(output, cipher)) {
            int n;
            byte[] buf = new byte[BUF_SIZE];
            while ((n = input.read(buf)) >= 0) {
                out.write(buf, 0, n);
            }
        }
    } catch (NoSuchAlgorithmException e) {
        throw new ProcessingException("Unable to crypt data. Algorithm could not be found. Make sure to use JRE 1.8 or newer.", e);
    } catch (NoSuchPaddingException | InvalidKeyException | InvalidAlgorithmParameterException | NoSuchProviderException | IOException e) {
        throw new ProcessingException("Unable to crypt data.", e);
    }
}
Also used : CipherOutputStream(javax.crypto.CipherOutputStream) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) AssertionException(org.eclipse.scout.rt.platform.util.Assertions.AssertionException) CipherOutputStream(javax.crypto.CipherOutputStream) OutputStream(java.io.OutputStream) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) InvalidKeyException(java.security.InvalidKeyException) Cipher(javax.crypto.Cipher) NoSuchProviderException(java.security.NoSuchProviderException) ProcessingException(org.eclipse.scout.rt.platform.exception.ProcessingException)

Example 9 with AssertionException

use of org.eclipse.scout.rt.platform.util.Assertions.AssertionException in project scout.rt by eclipse.

the class SunSecurityProvider method createSignature.

@Override
public byte[] createSignature(byte[] privateKey, InputStream data) {
    Assertions.assertGreater(Assertions.assertNotNull(privateKey, "no private key provided").length, 0, "empty private key not allowed");
    if (data == null) {
        throw new AssertionException("no data provided");
    }
    try {
        // create private key from bytes
        KeyFactory keyFactory = KeyFactory.getInstance(getKeyPairGenerationAlgorithm(), getSignatureProvider());
        PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(privateKey);
        PrivateKey priv = keyFactory.generatePrivate(privateKeySpec);
        // create signature
        Signature sig = Signature.getInstance(getSignatureAlgorithm(), getSignatureProvider());
        sig.initSign(priv, createSecureRandom());
        int n;
        byte[] buf = new byte[BUF_SIZE];
        while ((n = data.read(buf)) >= 0) {
            sig.update(buf, 0, n);
        }
        return sig.sign();
    } catch (NoSuchProviderException | NoSuchAlgorithmException | InvalidKeySpecException | InvalidKeyException | SignatureException | IOException e) {
        throw new ProcessingException("unable to create signature.", e);
    }
}
Also used : PrivateKey(java.security.PrivateKey) AssertionException(org.eclipse.scout.rt.platform.util.Assertions.AssertionException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) SignatureException(java.security.SignatureException) IOException(java.io.IOException) InvalidKeyException(java.security.InvalidKeyException) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) Signature(java.security.Signature) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) NoSuchProviderException(java.security.NoSuchProviderException) SecretKeyFactory(javax.crypto.SecretKeyFactory) KeyFactory(java.security.KeyFactory) ProcessingException(org.eclipse.scout.rt.platform.exception.ProcessingException)

Example 10 with AssertionException

use of org.eclipse.scout.rt.platform.util.Assertions.AssertionException in project scout.rt by eclipse.

the class MutualExclusionTest method testAcquisition2.

/**
 * Tests blocking mutex acquisition.
 */
@Test(timeout = 1000)
public void testAcquisition2() {
    ExecutionSemaphore mutex = (ExecutionSemaphore) m_task1.getExecutionSemaphore();
    assertEquals(0, mutex.getCompetitorCount());
    // Make task1 to acquire the mutex.
    final AtomicReference<Thread> thread = new AtomicReference<>();
    assertTrue(mutex.compete(m_task1, QueuePosition.HEAD, new IPermitAcquiredCallback() {

        @Override
        public void onPermitAcquired() {
            thread.set(Thread.currentThread());
        }
    }));
    assertSame(Thread.currentThread(), thread.get());
    assertTrue(mutex.isPermitOwner(m_task1));
    assertEquals(1, mutex.getCompetitorCount());
    // Wrong mutex release.
    try {
        mutex.release(m_task2);
        fail();
    } catch (AssertionException e) {
        assertTrue(mutex.isPermitOwner(m_task1));
        assertEquals(1, mutex.getCompetitorCount());
    }
    // Task1 releases the mutex.
    mutex.release(m_task1);
    assertFalse(mutex.isPermitOwner(m_task1));
    assertEquals(0, mutex.getCompetitorCount());
}
Also used : IExecutionSemaphore(org.eclipse.scout.rt.platform.job.IExecutionSemaphore) AssertionException(org.eclipse.scout.rt.platform.util.Assertions.AssertionException) IPermitAcquiredCallback(org.eclipse.scout.rt.platform.job.internal.ExecutionSemaphore.IPermitAcquiredCallback) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.Test)

Aggregations

AssertionException (org.eclipse.scout.rt.platform.util.Assertions.AssertionException)13 ProcessingException (org.eclipse.scout.rt.platform.exception.ProcessingException)8 Test (org.junit.Test)7 IOException (java.io.IOException)5 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)5 NoSuchProviderException (java.security.NoSuchProviderException)5 IRunnable (org.eclipse.scout.rt.platform.util.concurrent.IRunnable)5 InvalidKeyException (java.security.InvalidKeyException)4 IExecutionSemaphore (org.eclipse.scout.rt.platform.job.IExecutionSemaphore)3 KeyFactory (java.security.KeyFactory)2 Signature (java.security.Signature)2 SignatureException (java.security.SignatureException)2 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)2 SecretKeyFactory (javax.crypto.SecretKeyFactory)2 BlockingCountDownLatch (org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch)2 OutputStream (java.io.OutputStream)1 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)1 MessageDigest (java.security.MessageDigest)1 PrivateKey (java.security.PrivateKey)1 PublicKey (java.security.PublicKey)1