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-2011 Schlichtherle IT Services
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 */
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 edu.umd.cs.findbugs.annotations.DefaultAnnotation;
import edu.umd.cs.findbugs.annotations.NonNull;
import java.net.URI;
import net.jcip.annotations.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 * @version $Id$ */ @ThreadSafe @DefaultAnnotation(NonNull.class) 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 @SuppressWarnings("unchecked") public

P get(Class

type) { if (type.isAssignableFrom(Type0RaesParameters.class)) return (P) 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 - 2025 Weber Informatics LLC | Privacy Policy