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.selection.keyring.impl; 017 018import java.util.Map; 019import java.util.Set; 020 021import org.bouncycastle.openpgp.PGPPublicKeyRing; 022import org.bouncycastle.openpgp.PGPSecretKeyRing; 023import org.pgpainless.key.selection.keyring.PublicKeyRingSelectionStrategy; 024import org.pgpainless.key.selection.keyring.SecretKeyRingSelectionStrategy; 025import org.pgpainless.util.MultiMap; 026 027public class Whitelist { 028 029 public static class PubRingSelectionStrategy<O> extends PublicKeyRingSelectionStrategy<O> { 030 031 private final MultiMap<O, Long> whitelist; 032 033 public PubRingSelectionStrategy(MultiMap<O, Long> whitelist) { 034 this.whitelist = whitelist; 035 } 036 037 public PubRingSelectionStrategy(Map<O, Set<Long>> whitelist) { 038 this.whitelist = new MultiMap<>(whitelist); 039 } 040 041 @Override 042 public boolean accept(O identifier, PGPPublicKeyRing keyRing) { 043 Set<Long> whitelistedKeyIds = whitelist.get(identifier); 044 045 if (whitelistedKeyIds == null) { 046 return false; 047 } 048 049 return whitelistedKeyIds.contains(keyRing.getPublicKey().getKeyID()); 050 } 051 } 052 053 public static class SecRingSelectionStrategy<O> extends SecretKeyRingSelectionStrategy<O> { 054 055 private final MultiMap<O, Long> whitelist; 056 057 public SecRingSelectionStrategy(MultiMap<O, Long> whitelist) { 058 this.whitelist = whitelist; 059 } 060 061 public SecRingSelectionStrategy(Map<O, Set<Long>> whitelist) { 062 this.whitelist = new MultiMap<>(whitelist); 063 } 064 065 @Override 066 public boolean accept(O identifier, PGPSecretKeyRing keyRing) { 067 Set<Long> whitelistedKeyIds = whitelist.get(identifier); 068 069 if (whitelistedKeyIds == null) { 070 return false; 071 } 072 073 return whitelistedKeyIds.contains(keyRing.getPublicKey().getKeyID()); 074 } 075 076 } 077}