2014-08-04 10:46:23 -04:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2014 Dominik Schürmann <dominik@dominikschuermann.de>
|
2014-08-20 18:02:27 -04:00
|
|
|
* Copyright (C) 2014 Vincent Breitmoser <v.breitmoser@mugenguild.com>
|
2014-08-04 10:46:23 -04:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2014-05-21 15:07:32 -04:00
|
|
|
package org.sufficientlysecure.keychain.pgp;
|
|
|
|
|
|
|
|
import org.spongycastle.openpgp.PGPKeyRing;
|
2014-10-08 18:59:45 -04:00
|
|
|
import org.sufficientlysecure.keychain.pgp.exception.PgpGeneralException;
|
2014-10-08 19:37:44 -04:00
|
|
|
import org.sufficientlysecure.keychain.pgp.exception.PgpKeyNotFoundException;
|
2014-05-21 15:07:32 -04:00
|
|
|
import org.sufficientlysecure.keychain.util.IterableIterator;
|
|
|
|
|
2014-05-14 09:37:55 -04:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.OutputStream;
|
2014-09-07 14:09:36 -04:00
|
|
|
import java.util.ArrayList;
|
2014-10-08 12:31:31 -04:00
|
|
|
import java.util.Date;
|
2014-05-14 09:37:55 -04:00
|
|
|
|
2014-05-31 07:10:41 -04:00
|
|
|
/** A generic wrapped PGPKeyRing object.
|
|
|
|
*
|
|
|
|
* This class provides implementations for all basic getters which both
|
|
|
|
* PublicKeyRing and SecretKeyRing have in common. To make the wrapped keyring
|
|
|
|
* class typesafe in implementing subclasses, the field is stored in the
|
|
|
|
* implementing class, providing properly typed access through the getRing
|
|
|
|
* getter method.
|
|
|
|
*
|
|
|
|
*/
|
2014-07-31 11:08:33 -04:00
|
|
|
public abstract class CanonicalizedKeyRing extends KeyRing {
|
2014-05-21 15:07:32 -04:00
|
|
|
|
|
|
|
private final int mVerified;
|
|
|
|
|
2014-07-31 11:08:33 -04:00
|
|
|
CanonicalizedKeyRing(int verified) {
|
2014-05-21 15:07:32 -04:00
|
|
|
mVerified = verified;
|
|
|
|
}
|
|
|
|
|
|
|
|
public long getMasterKeyId() {
|
|
|
|
return getRing().getPublicKey().getKeyID();
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getVerified() {
|
|
|
|
return mVerified;
|
|
|
|
}
|
|
|
|
|
2014-10-04 08:11:51 -04:00
|
|
|
public byte[] getFingerprint() {
|
|
|
|
return getRing().getPublicKey().getFingerprint();
|
|
|
|
}
|
|
|
|
|
2014-10-08 19:37:44 -04:00
|
|
|
public String getPrimaryUserId() throws PgpKeyNotFoundException {
|
2014-07-15 13:47:40 -04:00
|
|
|
return getPublicKey().getPrimaryUserId();
|
2014-07-15 18:22:45 -04:00
|
|
|
}
|
|
|
|
|
2014-10-08 19:37:44 -04:00
|
|
|
public String getPrimaryUserIdWithFallback() throws PgpKeyNotFoundException {
|
2014-07-15 18:22:45 -04:00
|
|
|
return getPublicKey().getPrimaryUserIdWithFallback();
|
|
|
|
}
|
2014-05-21 15:07:32 -04:00
|
|
|
|
2014-09-13 13:05:53 -04:00
|
|
|
public ArrayList<byte[]> getUnorderedRawUserIds() {
|
|
|
|
return getPublicKey().getUnorderedRawUserIds();
|
|
|
|
}
|
|
|
|
|
2014-09-07 14:09:36 -04:00
|
|
|
public ArrayList<String> getUnorderedUserIds() {
|
|
|
|
return getPublicKey().getUnorderedUserIds();
|
|
|
|
}
|
|
|
|
|
2014-10-08 12:33:01 -04:00
|
|
|
public boolean isRevoked() {
|
2014-05-21 15:07:32 -04:00
|
|
|
// Is the master key revoked?
|
|
|
|
return getRing().getPublicKey().isRevoked();
|
|
|
|
}
|
|
|
|
|
2014-10-08 12:33:01 -04:00
|
|
|
public boolean isExpired() {
|
2014-10-08 12:31:31 -04:00
|
|
|
// Is the master key expired?
|
|
|
|
Date creationDate = getRing().getPublicKey().getCreationTime();
|
|
|
|
Date expiryDate = getRing().getPublicKey().getValidSeconds() > 0
|
|
|
|
? new Date(creationDate.getTime() + getRing().getPublicKey().getValidSeconds() * 1000) : null;
|
|
|
|
|
|
|
|
Date now = new Date();
|
|
|
|
return creationDate.after(now) || (expiryDate != null && expiryDate.before(now));
|
|
|
|
}
|
|
|
|
|
2014-10-08 19:37:44 -04:00
|
|
|
public boolean canCertify() throws PgpKeyNotFoundException {
|
2014-05-21 15:07:32 -04:00
|
|
|
return getRing().getPublicKey().isEncryptionKey();
|
|
|
|
}
|
|
|
|
|
2014-10-08 19:37:44 -04:00
|
|
|
public long getEncryptId() throws PgpKeyNotFoundException {
|
2014-07-31 11:08:33 -04:00
|
|
|
for(CanonicalizedPublicKey key : publicKeyIterator()) {
|
2014-10-02 13:23:08 -04:00
|
|
|
if (key.canEncrypt() && key.isValid()) {
|
2014-06-04 14:27:25 -04:00
|
|
|
return key.getKeyId();
|
2014-05-21 15:07:32 -04:00
|
|
|
}
|
|
|
|
}
|
2014-10-08 19:37:44 -04:00
|
|
|
throw new PgpKeyNotFoundException("No valid encryption key found!");
|
2014-05-21 15:07:32 -04:00
|
|
|
}
|
|
|
|
|
2014-10-08 19:37:44 -04:00
|
|
|
public boolean hasEncrypt() throws PgpKeyNotFoundException {
|
2014-05-21 15:07:32 -04:00
|
|
|
try {
|
|
|
|
getEncryptId();
|
|
|
|
return true;
|
2014-10-08 19:37:44 -04:00
|
|
|
} catch(PgpKeyNotFoundException e) {
|
2014-05-21 15:07:32 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-14 09:37:55 -04:00
|
|
|
public void encode(OutputStream stream) throws IOException {
|
|
|
|
getRing().encode(stream);
|
|
|
|
}
|
|
|
|
|
2014-06-16 18:12:06 -04:00
|
|
|
/** Returns an UncachedKeyRing which wraps the same data as this ring. This method should
|
|
|
|
* only be used */
|
|
|
|
public UncachedKeyRing getUncachedKeyRing() {
|
|
|
|
return new UncachedKeyRing(getRing());
|
|
|
|
}
|
|
|
|
|
2014-05-21 15:07:32 -04:00
|
|
|
abstract PGPKeyRing getRing();
|
|
|
|
|
2014-07-31 11:08:33 -04:00
|
|
|
abstract public IterableIterator<CanonicalizedPublicKey> publicKeyIterator();
|
2014-06-04 14:27:25 -04:00
|
|
|
|
2014-07-31 11:08:33 -04:00
|
|
|
public CanonicalizedPublicKey getPublicKey() {
|
|
|
|
return new CanonicalizedPublicKey(this, getRing().getPublicKey());
|
2014-07-15 13:47:40 -04:00
|
|
|
}
|
|
|
|
|
2014-07-31 11:08:33 -04:00
|
|
|
public CanonicalizedPublicKey getPublicKey(long id) {
|
|
|
|
return new CanonicalizedPublicKey(this, getRing().getPublicKey(id));
|
2014-07-15 13:47:40 -04:00
|
|
|
}
|
|
|
|
|
2014-07-09 19:38:57 -04:00
|
|
|
public byte[] getEncoded() throws IOException {
|
|
|
|
return getRing().getEncoded();
|
|
|
|
}
|
|
|
|
|
2014-05-21 15:07:32 -04:00
|
|
|
}
|