Search in sources :

Example 31 with KeyException

use of java.security.KeyException in project android by cSploit.

the class UpdateService method downloadFile.

/**
   * download mCurrentTask.url to mCurrentTask.path
   *
   * @throws KeyException when MD5 or SHA1 sum fails
   * @throws IOException when IOError occurs
   * @throws NoSuchAlgorithmException when required digest algorithm is not available
   * @throws CancellationException when user cancelled the download via notification
   */
private void downloadFile() throws SecurityException, KeyException, IOException, NoSuchAlgorithmException, CancellationException {
    if (mCurrentTask.url == null || mCurrentTask.path == null)
        return;
    File file = null;
    FileOutputStream writer = null;
    InputStream reader = null;
    HttpURLConnection connection = null;
    boolean exitForError = true;
    try {
        MessageDigest md5, sha1;
        URL url;
        byte[] buffer;
        int read;
        short percentage, previous_percentage;
        long downloaded, total;
        mBuilder.setContentTitle(getString(R.string.downloading_update)).setContentText(getString(R.string.connecting)).setSmallIcon(android.R.drawable.stat_sys_download).setProgress(100, 0, true);
        mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
        md5 = (mCurrentTask.md5 != null ? MessageDigest.getInstance("MD5") : null);
        sha1 = (mCurrentTask.sha1 != null ? MessageDigest.getInstance("SHA-1") : null);
        buffer = new byte[4096];
        file = new File(mCurrentTask.path);
        if (file.exists() && file.isFile())
            //noinspection ResultOfMethodCallIgnored
            file.delete();
        HttpURLConnection.setFollowRedirects(true);
        url = new URL(mCurrentTask.url);
        connection = (HttpURLConnection) url.openConnection();
        connection.connect();
        writer = new FileOutputStream(file);
        reader = connection.getInputStream();
        total = connection.getContentLength();
        read = connection.getResponseCode();
        if (read != 200)
            throw new IOException(String.format("cannot download '%s': responseCode: %d", mCurrentTask.url, read));
        downloaded = 0;
        previous_percentage = -1;
        mBuilder.setContentText(file.getName());
        mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
        Logger.info(String.format("downloading '%s' to '%s'", mCurrentTask.url, mCurrentTask.path));
        while (mRunning && (read = reader.read(buffer)) != -1) {
            writer.write(buffer, 0, read);
            if (md5 != null)
                md5.update(buffer, 0, read);
            if (sha1 != null)
                sha1.update(buffer, 0, read);
            if (total >= 0) {
                downloaded += read;
                percentage = (short) (((double) downloaded / total) * 100);
                if (percentage != previous_percentage) {
                    mBuilder.setProgress(100, percentage, false).setContentInfo(percentage + "%");
                    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
                    previous_percentage = percentage;
                }
            }
        }
        if (!mRunning)
            throw new CancellationException("download cancelled");
        Logger.info("download finished successfully");
        if (md5 != null || sha1 != null) {
            if (md5 != null && !mCurrentTask.md5.equals(digest2string(md5.digest()))) {
                throw new KeyException("wrong MD5");
            } else if (sha1 != null && !mCurrentTask.sha1.equals(digest2string(sha1.digest()))) {
                throw new KeyException("wrong SHA-1");
            }
        } else if (mCurrentTask.archiver != null) {
            verifyArchiveIntegrity();
        }
        exitForError = false;
    } finally {
        if (exitForError && file != null && file.exists() && !file.delete())
            Logger.error(String.format("cannot delete file '%s'", mCurrentTask.path));
        try {
            if (writer != null)
                writer.close();
            if (reader != null)
                reader.close();
            if (connection != null)
                connection.disconnect();
        } catch (IOException e) {
            System.errorLogging(e);
        }
    }
}
Also used : BufferedInputStream(java.io.BufferedInputStream) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) XZCompressorInputStream(org.apache.commons.compress.compressors.xz.XZCompressorInputStream) ArchiveInputStream(org.apache.commons.compress.archivers.ArchiveInputStream) BZip2CompressorInputStream(org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream) GzipCompressorInputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream) ZipArchiveInputStream(org.apache.commons.compress.archivers.zip.ZipArchiveInputStream) FileInputStream(java.io.FileInputStream) CountingInputStream(org.apache.commons.compress.utils.CountingInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) URL(java.net.URL) KeyException(java.security.KeyException) HttpURLConnection(java.net.HttpURLConnection) CancellationException(java.util.concurrent.CancellationException) FileOutputStream(java.io.FileOutputStream) MessageDigest(java.security.MessageDigest) File(java.io.File)

Example 32 with KeyException

use of java.security.KeyException in project graylog2-server by Graylog2.

the class PemReader method readPrivateKey.

static byte[] readPrivateKey(Path path) throws KeyException {
    final byte[] bytes;
    try {
        bytes = Files.readAllBytes(path);
    } catch (IOException e) {
        throw new KeyException("Couldn't read private key from file: " + path, e);
    }
    final String content = new String(bytes, StandardCharsets.US_ASCII);
    final Matcher m = KEY_PATTERN.matcher(content);
    if (!m.find()) {
        throw new KeyException("No private key found in file: " + path);
    }
    final String s = CharMatcher.breakingWhitespace().removeFrom(m.group(1));
    byte[] base64 = s.getBytes(StandardCharsets.US_ASCII);
    return Base64.getDecoder().decode(base64);
}
Also used : CharMatcher(com.google.common.base.CharMatcher) Matcher(java.util.regex.Matcher) IOException(java.io.IOException) KeyException(java.security.KeyException)

Example 33 with KeyException

use of java.security.KeyException in project robovm by robovm.

the class KeyExceptionTest method testKeyException01.

/**
     * Test for <code>KeyException()</code> constructor Assertion: constructs
     * KeyException with no detail message
     */
public void testKeyException01() {
    KeyException tE = new KeyException();
    assertNull("getMessage() must return null.", tE.getMessage());
    assertNull("getCause() must return null", tE.getCause());
}
Also used : KeyException(java.security.KeyException)

Example 34 with KeyException

use of java.security.KeyException in project robovm by robovm.

the class KeyExceptionTest method testKeyException08.

/**
     * Test for <code>KeyException(String, Throwable)</code> constructor
     * Assertion: constructs KeyException when <code>cause</code> is not null
     * <code>msg</code> is null
     */
public void testKeyException08() {
    KeyException tE = new KeyException(null, tCause);
    if (tE.getMessage() != null) {
        String toS = tCause.toString();
        String getM = tE.getMessage();
        assertTrue("getMessage() must should ".concat(toS), (getM.indexOf(toS) != -1));
    }
    assertNotNull("getCause() must not return null", tE.getCause());
    assertEquals("getCause() must return ".concat(tCause.toString()), tE.getCause(), tCause);
}
Also used : KeyException(java.security.KeyException)

Example 35 with KeyException

use of java.security.KeyException in project robovm by robovm.

the class KeyExceptionTest method testKeyException02.

/**
     * Test for <code>KeyException(String)</code> constructor Assertion:
     * constructs KeyException with detail message msg. Parameter
     * <code>msg</code> is not null.
     */
public void testKeyException02() {
    KeyException tE;
    for (int i = 0; i < msgs.length; i++) {
        tE = new KeyException(msgs[i]);
        assertEquals("getMessage() must return: ".concat(msgs[i]), tE.getMessage(), msgs[i]);
        assertNull("getCause() must return null", tE.getCause());
    }
}
Also used : KeyException(java.security.KeyException)

Aggregations

KeyException (java.security.KeyException)59 IOException (java.io.IOException)22 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)14 File (java.io.File)10 PublicKey (java.security.PublicKey)8 FileInputStream (java.io.FileInputStream)7 Cipher (javax.crypto.Cipher)6 Throwables.getStackTraceAsString (com.google.common.base.Throwables.getStackTraceAsString)5 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)5 LoginException (javax.security.auth.login.LoginException)5 Credentials (org.ow2.proactive.authentication.crypto.Credentials)5 FileNotFoundException (java.io.FileNotFoundException)4 PrivateKey (java.security.PrivateKey)4 CredData (org.ow2.proactive.authentication.crypto.CredData)4 RMException (org.ow2.proactive.resourcemanager.exception.RMException)4 CommandLineBuilder (org.ow2.proactive.resourcemanager.utils.CommandLineBuilder)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 DataInputStream (java.io.DataInputStream)3 InputStream (java.io.InputStream)3 Key (java.security.Key)3