Search in sources :

Example 1 with SignatureException

use of de.flexiprovider.api.exceptions.SignatureException in project core by jcryptool.

the class SignatureEngine method perform.

@Override
public void perform(KeyObject usedKey) {
    if (initialized) {
        // $NON-NLS-1$
        LogUtil.logInfo("performing a signature");
        InputStream inputStream = initInput(operation.getInput());
        try {
            int i;
            while ((i = inputStream.read()) != -1) {
                signature.update((byte) i);
            }
            inputStream.close();
            if (operation.getOperation().equals(OperationType.SIGN)) {
                OutputStream outputStream = initOutput(operation.getSignature());
                byte[] signatureBytes = signature.sign();
                outputStream.write(signatureBytes);
                outputStream.close();
                try {
                    KeyStoreManager.getInstance().updateKeyPair((PrivateKey) usedKey.getKey(), usedKey.getPassword(), (KeyStoreAlias) operation.getKeyStoreAlias());
                } catch (UnrecoverableEntryException e) {
                    LogUtil.logError(FlexiProviderEnginesPlugin.PLUGIN_ID, e);
                } catch (java.security.NoSuchAlgorithmException e) {
                    LogUtil.logError(FlexiProviderEnginesPlugin.PLUGIN_ID, e);
                }
            // }
            } else {
                InputStream signatureInputStream = initInput(operation.getSignature());
                // rotten hack...
                List<Byte> signatureByteList = new ArrayList<Byte>();
                int s;
                while ((s = signatureInputStream.read()) != -1) {
                    signatureByteList.add((byte) s);
                }
                signatureInputStream.close();
                byte[] signatureBytes = new byte[signatureByteList.size()];
                for (int b = 0; b < signatureBytes.length; b++) {
                    signatureBytes[b] = signatureByteList.get(b);
                }
                boolean valid = signature.verify(signatureBytes);
                if (valid) {
                    MessageDialog.openInformation(shell, Messages.SignatureEngine_0, Messages.SignatureEngine_1);
                } else {
                    MessageDialog.openWarning(shell, Messages.SignatureEngine_0, Messages.SignatureEngine_2);
                }
            }
        } catch (SignatureException e) {
            LogUtil.logError(FlexiProviderEnginesPlugin.PLUGIN_ID, "SignatureException while performing a signature", e, // $NON-NLS-1$
            true);
        } catch (IOException e) {
            // $NON-NLS-1$
            LogUtil.logError(// $NON-NLS-1$
            FlexiProviderEnginesPlugin.PLUGIN_ID, // $NON-NLS-1$
            "IOException while performing a signature", // $NON-NLS-1$
            e, false);
        }
    }
}
Also used : InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) ArrayList(java.util.ArrayList) SignatureException(de.flexiprovider.api.exceptions.SignatureException) IOException(java.io.IOException) UnrecoverableEntryException(java.security.UnrecoverableEntryException)

Aggregations

SignatureException (de.flexiprovider.api.exceptions.SignatureException)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 UnrecoverableEntryException (java.security.UnrecoverableEntryException)1 ArrayList (java.util.ArrayList)1