Search in sources :

Example 6 with KeyStoreException

use of com.radixdlt.crypto.exception.KeyStoreException in project radixdlt by radixdlt.

the class RadixKeyStore method fromFile.

/**
 * Load a private key from file, and compute the public key.
 *
 * <p>Note that if {@code create} is set to {@code true}, then the file will be created if it does
 * not exist. If the file is created, then it's permissions will be set to just {@link
 * PosixFilePermission#OWNER_READ} and {@link PosixFilePermission#OWNER_WRITE} on Posix
 * filesystems.
 *
 * @param file The file to load the private key from
 * @param storePassword The password to use for securing the store. Set to {@code null} if a
 *     default password should be used. Note: using {@code null} effectively means there is
 *     <b><i>no security</i></b> on the underlying key store.
 * @param create Set to {@code true} if the file should be created if it doesn't exist.
 * @return A {@link RadixKeyStore}
 * @throws IOException If reading or writing the file fails
 * @throws KeyStoreException If the key read from the file is invalid
 */
public static RadixKeyStore fromFile(File file, char[] storePassword, boolean create) throws IOException, KeyStoreException {
    try {
        var ks = KeyStore.getInstance("pkcs12");
        var usedStorePassword = (storePassword == null || storePassword.length == 0) ? defaultKey : storePassword;
        initializeKeyStore(ks, file, usedStorePassword, create);
        return new RadixKeyStore(file, ks, usedStorePassword.clone());
    } catch (GeneralSecurityException ex) {
        throw new KeyStoreException("Can't load key store", ex);
    }
}
Also used : GeneralSecurityException(java.security.GeneralSecurityException) KeyStoreException(com.radixdlt.crypto.exception.KeyStoreException)

Example 7 with KeyStoreException

use of com.radixdlt.crypto.exception.KeyStoreException in project radixdlt by radixdlt.

the class RadixKeyStoreTest method testClose.

/**
 * Test method for {@link RadixKeyStore#close()}.
 */
@Test
public void testClose() throws IOException, KeyStoreException {
    File file = newFile(TEST_KS_FILENAME);
    @SuppressWarnings("resource") RadixKeyStore ks = RadixKeyStore.fromFile(file, "testpassword".toCharArray(), true);
    assertTrue(file.exists());
    ks.close();
    char[] pwd = ks.storePassword();
    assertEquals(12, pwd.length);
    assertTrue(IntStream.range(0, pwd.length).map(i -> pwd[i]).allMatch(i -> i == ' '));
}
Also used : IntStream(java.util.stream.IntStream) KeyPair(java.security.KeyPair) PrivateKeyException(com.radixdlt.crypto.exception.PrivateKeyException) BeforeClass(org.junit.BeforeClass) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) PublicKeyException(com.radixdlt.crypto.exception.PublicKeyException) KeyStoreException(com.radixdlt.crypto.exception.KeyStoreException) TestSetupUtils(com.radixdlt.TestSetupUtils) PKCS12Key(org.bouncycastle.jcajce.PKCS12Key) SecureRandom(java.security.SecureRandom) GeneralSecurityException(java.security.GeneralSecurityException) ECGenParameterSpec(java.security.spec.ECGenParameterSpec) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) Assert.assertArrayEquals(org.junit.Assert.assertArrayEquals) KeyPairGenerator(java.security.KeyPairGenerator) Files(java.nio.file.Files) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) KeyStore(java.security.KeyStore) Test(org.junit.Test) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) Certificate(java.security.cert.Certificate) PrivateKey(java.security.PrivateKey) Assert.assertFalse(org.junit.Assert.assertFalse) SecretKey(javax.crypto.SecretKey) Assert.assertEquals(org.junit.Assert.assertEquals) File(java.io.File) Test(org.junit.Test)

Aggregations

KeyStoreException (com.radixdlt.crypto.exception.KeyStoreException)7 GeneralSecurityException (java.security.GeneralSecurityException)4 KeyStore (java.security.KeyStore)3 IOException (java.io.IOException)2 KeyPair (java.security.KeyPair)2 PrivateKey (java.security.PrivateKey)2 Certificate (java.security.cert.Certificate)2 ECPrivateKey (org.bouncycastle.asn1.sec.ECPrivateKey)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 TestSetupUtils (com.radixdlt.TestSetupUtils)1 PrivateKeyException (com.radixdlt.crypto.exception.PrivateKeyException)1 PublicKeyException (com.radixdlt.crypto.exception.PublicKeyException)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 BigInteger (java.math.BigInteger)1 Files (java.nio.file.Files)1 KeyPairGenerator (java.security.KeyPairGenerator)1 SecureRandom (java.security.SecureRandom)1 CertificateException (java.security.cert.CertificateException)1 ECGenParameterSpec (java.security.spec.ECGenParameterSpec)1