2018-07-12 11:07:53 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2018 Paul Schaub.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
2018-07-18 18:23:06 +02:00
|
|
|
package org.pgpainless.key.collection;
|
2018-07-12 11:07:53 +02:00
|
|
|
|
2018-07-12 18:57:45 +02:00
|
|
|
import org.bouncycastle.openpgp.PGPPublicKey;
|
2018-07-12 11:07:53 +02:00
|
|
|
import org.bouncycastle.openpgp.PGPPublicKeyRing;
|
|
|
|
import org.bouncycastle.openpgp.PGPSecretKeyRing;
|
2018-07-18 18:23:06 +02:00
|
|
|
import org.pgpainless.key.OpenPgpV4Fingerprint;
|
2018-07-12 11:07:53 +02:00
|
|
|
|
|
|
|
public class PGPKeyRing {
|
|
|
|
|
|
|
|
private PGPPublicKeyRing publicKeys;
|
|
|
|
private PGPSecretKeyRing secretKeys;
|
|
|
|
|
|
|
|
public PGPKeyRing(PGPPublicKeyRing publicKeys, PGPSecretKeyRing secretKeys) {
|
2018-07-12 18:57:45 +02:00
|
|
|
if (secretKeys == null && publicKeys == null) {
|
|
|
|
throw new IllegalArgumentException("publicKeys and secretKeys MUST NOT be both null.");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (publicKeys != null && secretKeys != null) {
|
|
|
|
if (publicKeys.getPublicKey().getKeyID() != secretKeys.getPublicKey().getKeyID()) {
|
|
|
|
throw new IllegalArgumentException("publicKeys and secretKeys must have the same master key.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-12 11:07:53 +02:00
|
|
|
this.publicKeys = publicKeys;
|
|
|
|
this.secretKeys = secretKeys;
|
|
|
|
}
|
|
|
|
|
2018-07-12 18:57:45 +02:00
|
|
|
public long getKeyId() {
|
|
|
|
return getMasterKey().getKeyID();
|
|
|
|
}
|
|
|
|
|
|
|
|
public PGPPublicKey getMasterKey() {
|
|
|
|
PGPPublicKey publicKey = hasSecretKeys() ? secretKeys.getPublicKey() : publicKeys.getPublicKey();
|
|
|
|
if (!publicKey.isMasterKey()) {
|
|
|
|
throw new IllegalStateException("Expected master key is not a master key");
|
|
|
|
}
|
|
|
|
return publicKey;
|
|
|
|
}
|
|
|
|
|
|
|
|
public OpenPgpV4Fingerprint getV4Fingerprint() {
|
|
|
|
return new OpenPgpV4Fingerprint(getMasterKey());
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean hasSecretKeys() {
|
|
|
|
return secretKeys != null;
|
|
|
|
}
|
|
|
|
|
2018-07-12 11:07:53 +02:00
|
|
|
public PGPPublicKeyRing getPublicKeys() {
|
|
|
|
return publicKeys;
|
|
|
|
}
|
|
|
|
|
|
|
|
public PGPSecretKeyRing getSecretKeys() {
|
|
|
|
return secretKeys;
|
|
|
|
}
|
|
|
|
}
|