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-03 09:55:10 -04:00
|
|
|
package org.sufficientlysecure.keychain.pgp;
|
|
|
|
|
2014-07-31 11:08:33 -04:00
|
|
|
import org.spongycastle.bcpg.S2K;
|
2014-05-03 13:04:23 -04:00
|
|
|
import org.spongycastle.openpgp.PGPException;
|
2014-05-28 15:52:45 -04:00
|
|
|
import org.spongycastle.openpgp.PGPKeyRing;
|
|
|
|
import org.spongycastle.openpgp.PGPObjectFactory;
|
2014-05-03 13:04:23 -04:00
|
|
|
import org.spongycastle.openpgp.PGPPrivateKey;
|
2014-06-04 14:27:25 -04:00
|
|
|
import org.spongycastle.openpgp.PGPPublicKey;
|
2014-05-03 09:55:10 -04:00
|
|
|
import org.spongycastle.openpgp.PGPSecretKey;
|
|
|
|
import org.spongycastle.openpgp.PGPSecretKeyRing;
|
2014-05-03 13:04:23 -04:00
|
|
|
import org.spongycastle.openpgp.operator.PBESecretKeyDecryptor;
|
|
|
|
import org.spongycastle.openpgp.operator.jcajce.JcePBESecretKeyDecryptorBuilder;
|
2014-05-03 16:29:09 -04:00
|
|
|
import org.sufficientlysecure.keychain.Constants;
|
2014-05-04 06:55:22 -04:00
|
|
|
import org.sufficientlysecure.keychain.pgp.exception.PgpGeneralException;
|
2014-10-02 13:23:08 -04:00
|
|
|
import org.sufficientlysecure.keychain.provider.KeychainContract;
|
|
|
|
import org.sufficientlysecure.keychain.provider.ProviderHelper;
|
2014-05-03 09:55:10 -04:00
|
|
|
import org.sufficientlysecure.keychain.util.IterableIterator;
|
2014-05-28 15:52:45 -04:00
|
|
|
import org.sufficientlysecure.keychain.util.Log;
|
2014-05-03 09:55:10 -04:00
|
|
|
|
2014-05-03 16:29:09 -04:00
|
|
|
import java.io.IOException;
|
2014-07-31 11:08:33 -04:00
|
|
|
import java.util.HashSet;
|
2014-05-03 13:04:23 -04:00
|
|
|
import java.util.Iterator;
|
|
|
|
|
2014-07-31 11:08:33 -04:00
|
|
|
public class CanonicalizedSecretKeyRing extends CanonicalizedKeyRing {
|
2014-05-03 09:55:10 -04:00
|
|
|
|
|
|
|
private PGPSecretKeyRing mRing;
|
|
|
|
|
2014-07-31 11:08:33 -04:00
|
|
|
CanonicalizedSecretKeyRing(PGPSecretKeyRing ring, int verified) {
|
|
|
|
super(verified);
|
|
|
|
mRing = ring;
|
|
|
|
}
|
|
|
|
|
|
|
|
public CanonicalizedSecretKeyRing(byte[] blob, boolean isRevoked, int verified)
|
2014-05-03 09:55:10 -04:00
|
|
|
{
|
2014-07-31 11:08:33 -04:00
|
|
|
super(verified);
|
2014-05-28 15:52:45 -04:00
|
|
|
PGPObjectFactory factory = new PGPObjectFactory(blob);
|
|
|
|
PGPKeyRing keyRing = null;
|
|
|
|
try {
|
|
|
|
if ((keyRing = (PGPKeyRing) factory.nextObject()) == null) {
|
|
|
|
Log.e(Constants.TAG, "No keys given!");
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
Log.e(Constants.TAG, "Error while converting to PGPKeyRing!", e);
|
|
|
|
}
|
|
|
|
|
|
|
|
mRing = (PGPSecretKeyRing) keyRing;
|
2014-05-03 09:55:10 -04:00
|
|
|
}
|
|
|
|
|
2014-05-03 16:29:09 -04:00
|
|
|
PGPSecretKeyRing getRing() {
|
|
|
|
return mRing;
|
|
|
|
}
|
|
|
|
|
2014-07-31 11:08:33 -04:00
|
|
|
public CanonicalizedSecretKey getSecretKey() {
|
|
|
|
return new CanonicalizedSecretKey(this, mRing.getSecretKey());
|
|
|
|
}
|
|
|
|
|
|
|
|
public CanonicalizedSecretKey getSecretKey(long id) {
|
|
|
|
return new CanonicalizedSecretKey(this, mRing.getSecretKey(id));
|
2014-05-03 12:32:20 -04:00
|
|
|
}
|
|
|
|
|
2014-10-02 13:23:08 -04:00
|
|
|
/** Returns the key id which should be used for signing.
|
|
|
|
*
|
|
|
|
* This method returns keys which are actually available (ie. secret available, and not stripped,
|
|
|
|
* revoked, or expired), hence only works on keyrings where a secret key is available!
|
|
|
|
*/
|
|
|
|
public long getSecretSignId() throws PgpGeneralException {
|
|
|
|
for(CanonicalizedSecretKey key : secretKeyIterator()) {
|
|
|
|
if (key.canSign() && key.isValid() && key.getSecretKeyType().isUsable()) {
|
|
|
|
return key.getKeyId();
|
2014-05-04 06:55:22 -04:00
|
|
|
}
|
2014-05-03 13:04:23 -04:00
|
|
|
}
|
2014-10-02 13:23:08 -04:00
|
|
|
throw new PgpGeneralException("no valid signing key available");
|
2014-05-03 13:04:23 -04:00
|
|
|
}
|
|
|
|
|
2014-07-31 11:08:33 -04:00
|
|
|
public IterableIterator<CanonicalizedSecretKey> secretKeyIterator() {
|
2014-05-04 06:55:22 -04:00
|
|
|
final Iterator<PGPSecretKey> it = mRing.getSecretKeys();
|
2014-07-31 11:08:33 -04:00
|
|
|
return new IterableIterator<CanonicalizedSecretKey>(new Iterator<CanonicalizedSecretKey>() {
|
2014-05-04 06:55:22 -04:00
|
|
|
@Override
|
|
|
|
public boolean hasNext() {
|
|
|
|
return it.hasNext();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-07-31 11:08:33 -04:00
|
|
|
public CanonicalizedSecretKey next() {
|
|
|
|
return new CanonicalizedSecretKey(CanonicalizedSecretKeyRing.this, it.next());
|
2014-05-04 06:55:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void remove() {
|
|
|
|
it.remove();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-07-31 11:08:33 -04:00
|
|
|
public IterableIterator<CanonicalizedPublicKey> publicKeyIterator() {
|
2014-06-04 14:27:25 -04:00
|
|
|
final Iterator<PGPPublicKey> it = getRing().getPublicKeys();
|
2014-07-31 11:08:33 -04:00
|
|
|
return new IterableIterator<CanonicalizedPublicKey>(new Iterator<CanonicalizedPublicKey>() {
|
2014-06-04 14:27:25 -04:00
|
|
|
@Override
|
|
|
|
public boolean hasNext() {
|
|
|
|
return it.hasNext();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-07-31 11:08:33 -04:00
|
|
|
public CanonicalizedPublicKey next() {
|
|
|
|
return new CanonicalizedPublicKey(CanonicalizedSecretKeyRing.this, it.next());
|
2014-06-04 14:27:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void remove() {
|
|
|
|
it.remove();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-05-03 09:55:10 -04:00
|
|
|
}
|