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.collection; 017 018import javax.annotation.Nonnull; 019import java.io.File; 020import java.io.FileInputStream; 021import java.io.IOException; 022import java.io.InputStream; 023import java.util.logging.Level; 024import java.util.logging.Logger; 025 026import org.bouncycastle.openpgp.PGPException; 027import org.bouncycastle.openpgp.PGPPublicKeyRing; 028import org.bouncycastle.openpgp.PGPPublicKeyRingCollection; 029import org.bouncycastle.openpgp.PGPSecretKeyRing; 030import org.bouncycastle.openpgp.PGPSecretKeyRingCollection; 031import org.pgpainless.PGPainless; 032 033public class KeyRingCollection { 034 035 private static final Logger LOGGER = Logger.getLogger(KeyRingCollection.class.getName()); 036 037 private PGPPublicKeyRingCollection publicKeys; 038 private PGPSecretKeyRingCollection secretKeys; 039 040 public KeyRingCollection(@Nonnull PGPPublicKeyRingCollection publicKeyRings, @Nonnull PGPSecretKeyRingCollection secretKeyRings) { 041 this.publicKeys = publicKeyRings; 042 this.secretKeys = secretKeyRings; 043 } 044 045 public KeyRingCollection(File pubRingFile, File secRingFile) throws IOException, PGPException { 046 047 if (pubRingFile == null && secRingFile == null) { 048 throw new NullPointerException("pubRingFile and secRingFile cannot BOTH be null."); 049 } 050 051 if (pubRingFile != null) { 052 InputStream pubRingIn = new FileInputStream(pubRingFile); 053 this.publicKeys = PGPainless.readKeyRing().publicKeyRingCollection(pubRingIn); 054 pubRingIn.close(); 055 } 056 057 if (secRingFile != null) { 058 InputStream secRingIn = new FileInputStream(secRingFile); 059 this.secretKeys = PGPainless.readKeyRing().secretKeyRingCollection(secRingIn); 060 secRingIn.close(); 061 } 062 } 063 064 public KeyRingCollection(@Nonnull PGPPublicKeyRingCollection publicKeyRings) { 065 this.publicKeys = publicKeyRings; 066 } 067 068 public KeyRingCollection(@Nonnull PGPSecretKeyRingCollection secretKeyRings) { 069 this.secretKeys = secretKeyRings; 070 } 071 072 public void importPublicKeys(@Nonnull PGPPublicKeyRingCollection publicKeyRings) { 073 if (this.publicKeys == null) { 074 this.publicKeys = publicKeyRings; 075 return; 076 } 077 078 for (PGPPublicKeyRing keyRing : publicKeyRings) { 079 try { 080 this.publicKeys = PGPPublicKeyRingCollection.addPublicKeyRing(this.publicKeys, keyRing); 081 } catch (IllegalArgumentException e) { 082 // TODO: merge key rings. 083 LOGGER.log(Level.FINE, "Keyring " + Long.toHexString(keyRing.getPublicKey().getKeyID()) + 084 " is already included in the collection. Skip!"); 085 } 086 } 087 } 088 089 public void importSecretKeys(@Nonnull PGPSecretKeyRingCollection secretKeyRings) { 090 if (this.secretKeys == null) { 091 this.secretKeys = secretKeyRings; 092 return; 093 } 094 095 for (PGPSecretKeyRing keyRing : secretKeyRings) { 096 try { 097 this.secretKeys = PGPSecretKeyRingCollection.addSecretKeyRing(this.secretKeys, keyRing); 098 } catch (IllegalArgumentException e) { 099 // TODO: merge key rings. 100 LOGGER.log(Level.FINE, "Keyring " + Long.toHexString(keyRing.getPublicKey().getKeyID()) + 101 " is already included in the collection. Skip!"); 102 } 103 } 104 } 105}