All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.schlichtherle.truezip.crypto.raes.param.KeyManagerRaesParameters Maven / Gradle / Ivy

/*
 * Copyright (C) 2005-2013 Schlichtherle IT Services.
 * All rights reserved. Use is subject to license terms.
 */
package de.schlichtherle.truezip.crypto.raes.param;

import de.schlichtherle.truezip.crypto.raes.RaesKeyException;
import de.schlichtherle.truezip.crypto.raes.RaesParameters;
import de.schlichtherle.truezip.crypto.raes.RaesParametersProvider;
import de.schlichtherle.truezip.crypto.raes.Type0RaesParameters;
import de.schlichtherle.truezip.crypto.raes.Type0RaesParameters.KeyStrength;
import de.schlichtherle.truezip.key.KeyManager;
import de.schlichtherle.truezip.key.KeyManagerProvider;
import de.schlichtherle.truezip.key.KeyProvider;
import de.schlichtherle.truezip.key.UnknownKeyException;
import java.net.URI;
import javax.annotation.concurrent.ThreadSafe;

/**
 * An adapter which provides {@link RaesParameters} by using a
 * {@link KeyManager} for {@link AesCipherParameters}.
 * 

* The current implementation supports only {@link Type0RaesParameters}. * * @author Christian Schlichtherle */ @ThreadSafe public class KeyManagerRaesParameters implements RaesParametersProvider { /** The key manager for accessing RAES encrypted data. */ protected final KeyManager manager; /** The resource URI of the RAES file. */ protected final URI raes; /** * Constructs RAES parameters using the given key manager provider. * * @param provider the provider for the key manager for accessing RAES * encrypted data. * @param raes the absolute URI of the RAES file. */ public KeyManagerRaesParameters( final KeyManagerProvider provider, final URI raes) { this(provider.get(AesCipherParameters.class), raes); } /** * Constructs new RAES parameters. * * @param manager the key manager for accessing RAES encrypted data. * @param raes the resource URI of the RAES file. */ public KeyManagerRaesParameters( final KeyManager manager, final URI raes) { if (null == manager || null == raes) throw new NullPointerException(); this.manager = manager; this.raes = raes; } /** * {@inheritDoc} *

* If {@code type} is assignable from {@link Type0RaesParameters}, then the * {@link KeyManager} for {@link AesCipherParameters} will get used which * has been provided to the constructor. *

* Otherwise, {@code null} gets returned. */ @Override public

P get(Class

type) { if (type.isAssignableFrom(Type0RaesParameters.class)) return type.cast(new Type0()); return null; } /** * Adapts a {@code KeyProvider} for {@link AesCipherParameters} obtained * from the {@link #manager} to {@code Type0RaesParameters}. */ private class Type0 implements Type0RaesParameters { @Override public char[] getWritePassword() throws RaesKeyException { final KeyProvider provider = manager.getKeyProvider(raes); try { return provider.getWriteKey().getPassword(); } catch (UnknownKeyException ex) { throw new RaesKeyException(ex); } } @Override public char[] getReadPassword(final boolean invalid) throws RaesKeyException { final KeyProvider provider = manager.getKeyProvider(raes); try { return provider.getReadKey(invalid).getPassword(); } catch (UnknownKeyException ex) { throw new RaesKeyException(ex); } } @Override public KeyStrength getKeyStrength() throws RaesKeyException { final KeyProvider provider = manager.getKeyProvider(raes); try { return provider.getWriteKey().getKeyStrength(); } catch (UnknownKeyException ex) { throw new RaesKeyException(ex); } } @Override public void setKeyStrength(final KeyStrength keyStrength) throws RaesKeyException { final KeyProvider provider = manager.getKeyProvider(raes); final AesCipherParameters param; try { param = provider.getReadKey(false); } catch (UnknownKeyException ex) { throw new RaesKeyException(ex); } param.setKeyStrength(keyStrength); provider.setKey(param); } } // Type0 }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy