generic UncachedKeyRing fixes

This commit is contained in:
Vincent Breitmoser 2014-07-26 15:51:35 +02:00
parent cf6e4254c7
commit 236a502ea7

View File

@ -91,7 +91,7 @@ public class UncachedKeyRing {
final Iterator<PGPPublicKey> it = mRing.getPublicKeys(); final Iterator<PGPPublicKey> it = mRing.getPublicKeys();
return new Iterator<UncachedPublicKey>() { return new Iterator<UncachedPublicKey>() {
public void remove() { public void remove() {
it.remove(); throw new UnsupportedOperationException();
} }
public UncachedPublicKey next() { public UncachedPublicKey next() {
return new UncachedPublicKey(it.next()); return new UncachedPublicKey(it.next());
@ -121,42 +121,41 @@ public class UncachedKeyRing {
public static UncachedKeyRing decodeFromData(byte[] data) public static UncachedKeyRing decodeFromData(byte[] data)
throws PgpGeneralException, IOException { throws PgpGeneralException, IOException {
BufferedInputStream bufferedInput =
new BufferedInputStream(new ByteArrayInputStream(data));
if (bufferedInput.available() > 0) {
InputStream in = PGPUtil.getDecoderStream(bufferedInput);
PGPObjectFactory objectFactory = new PGPObjectFactory(in);
// get first object in block List<UncachedKeyRing> parsed = fromStream(new ByteArrayInputStream(data));
Object obj;
if ((obj = objectFactory.nextObject()) != null && obj instanceof PGPKeyRing) { if (parsed.isEmpty()) {
return new UncachedKeyRing((PGPKeyRing) obj);
} else {
throw new PgpGeneralException("Object not recognized as PGPKeyRing!"); throw new PgpGeneralException("Object not recognized as PGPKeyRing!");
} }
} else { if (parsed.size() > 1) {
throw new PgpGeneralException("Object not recognized as PGPKeyRing!"); throw new PgpGeneralException(
"Expected single keyring in stream, found " + parsed.size());
} }
return parsed.get(0);
} }
public static List<UncachedKeyRing> fromStream(InputStream stream) public static List<UncachedKeyRing> fromStream(InputStream stream)
throws PgpGeneralException, IOException { throws PgpGeneralException, IOException {
PGPObjectFactory objectFactory = new PGPObjectFactory(PGPUtil.getDecoderStream(stream));
List<UncachedKeyRing> result = new Vector<UncachedKeyRing>(); List<UncachedKeyRing> result = new Vector<UncachedKeyRing>();
while(stream.available() > 0) {
PGPObjectFactory objectFactory = new PGPObjectFactory(PGPUtil.getDecoderStream(stream));
// go through all objects in this block // go through all objects in this block
Object obj; Object obj;
while ((obj = objectFactory.nextObject()) != null) { while ((obj = objectFactory.nextObject()) != null) {
Log.d(Constants.TAG, "Found class: " + obj.getClass()); Log.d(Constants.TAG, "Found class: " + obj.getClass());
if (!(obj instanceof PGPKeyRing)) {
if (obj instanceof PGPKeyRing) { throw new PgpGeneralException(
"Bad object of type " + obj.getClass().getName() + " in stream!");
}
result.add(new UncachedKeyRing((PGPKeyRing) obj)); result.add(new UncachedKeyRing((PGPKeyRing) obj));
} else {
Log.e(Constants.TAG, "Object not recognized as PGPKeyRing!");
} }
} }
return result; return result;
} }
@ -327,7 +326,7 @@ public class UncachedKeyRing {
if (cert.getCreationTime().after(now)) { if (cert.getCreationTime().after(now)) {
// Creation date in the future? No way! // Creation date in the future? No way!
log.add(LogLevel.WARN, LogType.MSG_KC_REVOKE_BAD_TIME, indent); log.add(LogLevel.WARN, LogType.MSG_KC_UID_BAD_TIME, indent);
modified = PGPPublicKey.removeCertification(modified, zert); modified = PGPPublicKey.removeCertification(modified, zert);
badCerts += 1; badCerts += 1;
continue; continue;
@ -335,7 +334,7 @@ public class UncachedKeyRing {
if (cert.isLocal()) { if (cert.isLocal()) {
// Creation date in the future? No way! // Creation date in the future? No way!
log.add(LogLevel.WARN, LogType.MSG_KC_REVOKE_BAD_LOCAL, indent); log.add(LogLevel.WARN, LogType.MSG_KC_UID_BAD_LOCAL, indent);
modified = PGPPublicKey.removeCertification(modified, zert); modified = PGPPublicKey.removeCertification(modified, zert);
badCerts += 1; badCerts += 1;
continue; continue;