Search in sources :

Example 1 with KeyException

use of java.security.KeyException in project hbase by apache.

the class TestEncryptionUtil method testKeyWrapping.

// There does not seem to be a ready way to test either getKeyFromBytesOrMasterKey
// or createEncryptionContext, and the existing code under MobUtils appeared to be
// untested.  Not ideal!
@Test
public void testKeyWrapping() throws Exception {
    // set up the key provider for testing to resolve a key for our test subject
    // we don't need HBaseConfiguration for this
    Configuration conf = new Configuration();
    conf.set(HConstants.CRYPTO_KEYPROVIDER_CONF_KEY, KeyProviderForTesting.class.getName());
    // generate a test key
    byte[] keyBytes = new byte[AES.KEY_LENGTH];
    new SecureRandom().nextBytes(keyBytes);
    String algorithm = conf.get(HConstants.CRYPTO_KEY_ALGORITHM_CONF_KEY, HConstants.CIPHER_AES);
    Key key = new SecretKeySpec(keyBytes, algorithm);
    // wrap the test key
    byte[] wrappedKeyBytes = EncryptionUtil.wrapKey(conf, "hbase", key);
    assertNotNull(wrappedKeyBytes);
    // unwrap
    Key unwrappedKey = EncryptionUtil.unwrapKey(conf, "hbase", wrappedKeyBytes);
    assertNotNull(unwrappedKey);
    // only secretkeyspec supported for now
    assertTrue(unwrappedKey instanceof SecretKeySpec);
    // did we get back what we wrapped?
    assertTrue("Unwrapped key bytes do not match original", Bytes.equals(keyBytes, unwrappedKey.getEncoded()));
    // unwrap with an incorrect key
    try {
        EncryptionUtil.unwrapKey(conf, "other", wrappedKeyBytes);
        fail("Unwrap with incorrect key did not throw KeyException");
    } catch (KeyException e) {
    // expected
    }
}
Also used : Configuration(org.apache.hadoop.conf.Configuration) SecretKeySpec(javax.crypto.spec.SecretKeySpec) SecureRandom(java.security.SecureRandom) KeyProviderForTesting(org.apache.hadoop.hbase.io.crypto.KeyProviderForTesting) Key(java.security.Key) KeyException(java.security.KeyException) Test(org.junit.Test)

Example 2 with KeyException

use of java.security.KeyException in project hbase by apache.

the class EncryptionUtil method unwrapKey.

/**
   * Helper for {@link #unwrapKey(Configuration, String, byte[])} which automatically uses the
   * configured master and alternative keys, rather than having to specify a key type to unwrap
   * with.
   *
   * The configuration must be set up correctly for key alias resolution.
   *
   * @param conf the current configuration
   * @param keyBytes the key encrypted by master (or alternative) to unwrap
   * @return the key bytes, decrypted
   * @throws IOException if the key cannot be unwrapped
   */
public static Key unwrapKey(Configuration conf, byte[] keyBytes) throws IOException {
    Key key;
    String masterKeyName = conf.get(HConstants.CRYPTO_MASTERKEY_NAME_CONF_KEY, User.getCurrent().getShortName());
    try {
        // First try the master key
        key = unwrapKey(conf, masterKeyName, keyBytes);
    } catch (KeyException e) {
        // one is configured
        if (LOG.isDebugEnabled()) {
            LOG.debug("Unable to unwrap key with current master key '" + masterKeyName + "'");
        }
        String alternateKeyName = conf.get(HConstants.CRYPTO_MASTERKEY_ALTERNATE_NAME_CONF_KEY);
        if (alternateKeyName != null) {
            try {
                key = unwrapKey(conf, alternateKeyName, keyBytes);
            } catch (KeyException ex) {
                throw new IOException(ex);
            }
        } else {
            throw new IOException(e);
        }
    }
    return key;
}
Also used : IOException(java.io.IOException) Key(java.security.Key) KeyException(java.security.KeyException)

Example 3 with KeyException

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

the class PemReader method readPrivateKey.

static ByteBuf readPrivateKey(InputStream in) throws KeyException {
    String content;
    try {
        content = readContent(in);
    } catch (IOException e) {
        throw new KeyException("failed to read key input stream", e);
    }
    Matcher m = KEY_PATTERN.matcher(content);
    if (!m.find()) {
        throw new KeyException("could not find a PKCS #8 private key in input stream" + " (see http://netty.io/wiki/sslcontextbuilder-and-private-key.html for more information)");
    }
    ByteBuf base64 = Unpooled.copiedBuffer(m.group(1), CharsetUtil.US_ASCII);
    ByteBuf der = Base64.decode(base64);
    base64.release();
    return der;
}
Also used : Matcher(java.util.regex.Matcher) IOException(java.io.IOException) ByteBuf(io.netty.buffer.ByteBuf) KeyException(java.security.KeyException)

Example 4 with KeyException

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

the class KeyExceptionTest method testKeyException09.

/**
     * Test for <code>KeyException(String, Throwable)</code> constructor
     * Assertion: constructs KeyException when <code>cause</code> is not null
     * <code>msg</code> is not null
     */
public void testKeyException09() {
    KeyException tE;
    for (int i = 0; i < msgs.length; i++) {
        tE = new KeyException(msgs[i], tCause);
        String getM = tE.getMessage();
        String toS = tCause.toString();
        if (msgs[i].length() > 0) {
            assertTrue("getMessage() must contain ".concat(msgs[i]), getM.indexOf(msgs[i]) != -1);
            if (!getM.equals(msgs[i])) {
                assertTrue("getMessage() should contain ".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 5 with KeyException

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

the class KeyExceptionTest method testKeyException06.

/**
     * Test for <code>KeyException(String, Throwable)</code> constructor
     * Assertion: constructs KeyException when <code>cause</code> is null
     * <code>msg</code> is null
     */
public void testKeyException06() {
    KeyException tE = new KeyException(null, null);
    assertNull("getMessage() must return null", tE.getMessage());
    assertNull("getCause() must return null", tE.getCause());
}
Also used : KeyException(java.security.KeyException)

Aggregations

KeyException (java.security.KeyException)21 IOException (java.io.IOException)7 Key (java.security.Key)4 File (java.io.File)3 FileInputStream (java.io.FileInputStream)3 CancellationException (java.util.concurrent.CancellationException)3 ArchiveInputStream (org.apache.commons.compress.archivers.ArchiveInputStream)3 TarArchiveInputStream (org.apache.commons.compress.archivers.tar.TarArchiveInputStream)3 ZipArchiveInputStream (org.apache.commons.compress.archivers.zip.ZipArchiveInputStream)3 CountingInputStream (org.apache.commons.compress.utils.CountingInputStream)3 BufferedInputStream (java.io.BufferedInputStream)2 FileNotFoundException (java.io.FileNotFoundException)2 InputStream (java.io.InputStream)2 BigInteger (java.math.BigInteger)2 MessageDigest (java.security.MessageDigest)2 ProviderException (java.security.ProviderException)2 Matcher (java.util.regex.Matcher)2 SecretKeySpec (javax.crypto.spec.SecretKeySpec)2 BZip2CompressorInputStream (org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream)2 GzipCompressorInputStream (org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream)2