ProductKeyChain.java

  1. /*
  2.  * ProductPublicKey
  3.  */
  4. package gov.usgs.earthquake.distribution;

  5. import gov.usgs.earthquake.product.ProductId;
  6. import gov.usgs.util.Config;
  7. import gov.usgs.util.StringUtils;

  8. import java.security.PublicKey;

  9. import java.util.Iterator;
  10. import java.util.List;
  11. import java.util.LinkedList;
  12. import java.util.logging.Logger;

  13. /**
  14.  * A group of keys that can be used to verify product signatures.
  15.  */
  16. public class ProductKeyChain {

  17.     /** Logging object. */
  18.     private static final Logger LOGGER = Logger.getLogger(ProductKeyChain.class
  19.             .getName());

  20.     /** List of candidate keys. */
  21.     private List<ProductKey> keychain = new LinkedList<ProductKey>();

  22.     /** Empty constructor */
  23.     public ProductKeyChain() {
  24.     }

  25.     /**
  26.      * Constructor for a string of keys
  27.      * @param keys String of keys, separated by commas
  28.      * @param config Config file
  29.      * @throws Exception if error occurs
  30.      */
  31.     public ProductKeyChain(final String keys, final Config config)
  32.             throws Exception {
  33.         this(StringUtils.split(keys, ","), config);
  34.     }

  35.     /**
  36.      * Constructor for list of keys
  37.      * @param keys String list of keys
  38.      * @param config Config file
  39.      * @throws Exception if error occurs
  40.      */
  41.     public ProductKeyChain(final List<String> keys, final Config config)
  42.             throws Exception {
  43.         Iterator<String> iter = keys.iterator();
  44.         while (iter.hasNext()) {
  45.             String keyName = iter.next();
  46.             LOGGER.config("Loading key '" + keyName + "'");
  47.             ProductKey key = (ProductKey) Config.getConfig().getObject(keyName);
  48.             if (key != null) {
  49.                 keychain.add(key);
  50.             }
  51.         }
  52.     }

  53.     /**
  54.      * @return the keys
  55.      */
  56.     public List<ProductKey> getKeychain() {
  57.         return keychain;
  58.     }

  59.     /**
  60.      * Find public keys based on configured Keys.
  61.      *
  62.      * @param id ID of product
  63.      * @return an array of candidate keys used to verify a signature.
  64.      */
  65.     public PublicKey[] getProductKeys(final ProductId id) {
  66.         LinkedList<PublicKey> publicKeys = new LinkedList<PublicKey>();
  67.         Iterator<ProductKey> iter = keychain.iterator();
  68.         while (iter.hasNext()) {
  69.             ProductKey key = iter.next();
  70.             if (key.isForProduct(id)) {
  71.                 publicKeys.add(key.getKey());
  72.             }
  73.         }
  74.         return publicKeys.toArray(new PublicKey[0]);
  75.     }

  76. }