001/*
002 * Copyright 2018 Paul Schaub.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.pgpainless.key.protection;
017
018import javax.annotation.Nonnull;
019import javax.annotation.Nullable;
020import java.util.HashMap;
021import java.util.Map;
022
023import org.bouncycastle.openpgp.PGPException;
024import org.bouncycastle.openpgp.operator.PBESecretKeyDecryptor;
025import org.bouncycastle.openpgp.operator.PBESecretKeyEncryptor;
026import org.pgpainless.util.Passphrase;
027
028/**
029 * Implementation of the {@link SecretKeyRingProtector} which holds a map of key ids and their passwords.
030 * In case the needed passphrase is not contained in the map, the {@code missingPassphraseCallback} will be consulted,
031 * and the passphrase is added to the map.
032 */
033public class PassphraseMapKeyRingProtector implements SecretKeyRingProtector, SecretKeyPassphraseProvider {
034
035    private final Map<Long, Passphrase> cache = new HashMap<>();
036    private final SecretKeyRingProtector protector;
037    private final SecretKeyPassphraseProvider provider;
038
039    public PassphraseMapKeyRingProtector(@Nonnull Map<Long, Passphrase> passphrases,
040                                         @Nonnull KeyRingProtectionSettings protectionSettings,
041                                         @Nullable SecretKeyPassphraseProvider missingPassphraseCallback) {
042        this.cache.putAll(passphrases);
043        this.protector = new PasswordBasedSecretKeyRingProtector(protectionSettings, this);
044        this.provider = missingPassphraseCallback;
045    }
046
047    /**
048     * Add a passphrase to the cache.
049     *
050     * @param keyId id of the key
051     * @param passphrase passphrase
052     */
053    public void addPassphrase(@Nonnull Long keyId, @Nullable Passphrase passphrase) {
054        this.cache.put(keyId, passphrase);
055    }
056
057    /**
058     * Remove a passphrase from the cache.
059     * The passphrase will be cleared and then removed.
060     *
061     * @param keyId id of the key
062     */
063    public void forgetPassphrase(@Nonnull Long keyId) {
064        Passphrase passphrase = cache.get(keyId);
065        passphrase.clear();
066        cache.remove(keyId);
067    }
068
069    @Override
070    @Nullable
071    public Passphrase getPassphraseFor(@Nonnull Long keyId) {
072        Passphrase passphrase = cache.get(keyId);
073        if (passphrase == null || !passphrase.isValid()) {
074            passphrase = provider.getPassphraseFor(keyId);
075            if (passphrase != null) {
076                cache.put(keyId, passphrase);
077            }
078        }
079        return passphrase;
080    }
081
082    @Override
083    @Nullable
084    public PBESecretKeyDecryptor getDecryptor(@Nonnull Long keyId) {
085        return protector.getDecryptor(keyId);
086    }
087
088    @Override
089    @Nullable
090    public PBESecretKeyEncryptor getEncryptor(@Nonnull Long keyId) throws PGPException {
091        return protector.getEncryptor(keyId);
092    }
093}