generate public keyring from secret if not available

This commit is contained in:
Vincent Breitmoser 2014-06-18 20:55:44 +02:00
parent f8d895dea4
commit 3bffe4da55
5 changed files with 41 additions and 23 deletions

View File

@ -27,6 +27,7 @@ import java.io.ByteArrayInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Comparator; import java.util.Comparator;
import java.util.Date; import java.util.Date;
import java.util.HashSet; import java.util.HashSet;
@ -759,6 +760,21 @@ public class UncachedKeyRing {
} }
public UncachedKeyRing extractPublicKeyRing() {
if(!isSecret()) {
throw new RuntimeException("Tried to extract public keyring from non-secret keyring. " +
"This is a programming error and should never happen!");
}
ArrayList<PGPPublicKey> keys = new ArrayList();
Iterator<PGPPublicKey> it = mRing.getPublicKeys();
while (it.hasNext()) {
keys.add(it.next());
}
return new UncachedKeyRing(new PGPPublicKeyRing(keys));
}
/** This method replaces a public key in a keyring. /** This method replaces a public key in a keyring.
* *
* This method essentially wraps PGP*KeyRing.insertPublicKey, where the keyring may be of either * This method essentially wraps PGP*KeyRing.insertPublicKey, where the keyring may be of either

View File

@ -804,37 +804,37 @@ public class ProviderHelper {
} }
// Merge new data into public keyring as well, if there is any // Merge new data into public keyring as well, if there is any
UncachedKeyRing publicRing;
try { try {
UncachedKeyRing oldPublicRing = getWrappedPublicKeyRing(masterKeyId).getUncached(); UncachedKeyRing oldPublicRing = getWrappedPublicKeyRing(masterKeyId).getUncached();
// Merge data from new public ring into secret one // Merge data from new public ring into secret one
UncachedKeyRing publicRing = oldPublicRing.merge(secretRing, mLog, mIndent); publicRing = oldPublicRing.merge(secretRing, mLog, mIndent);
if (publicRing == null) { if (publicRing == null) {
return new SaveKeyringResult(SaveKeyringResult.RESULT_ERROR, mLog); return new SaveKeyringResult(SaveKeyringResult.RESULT_ERROR, mLog);
} }
// If anything changed, reinsert // If nothing changed, never mind
if (Arrays.hashCode(publicRing.getEncoded()) if (Arrays.hashCode(publicRing.getEncoded())
!= Arrays.hashCode(oldPublicRing.getEncoded())) { == Arrays.hashCode(oldPublicRing.getEncoded())) {
publicRing = null;
log(LogLevel.OK, LogType.MSG_IS,
new String[]{ PgpKeyHelper.convertKeyIdToHex(masterKeyId) });
publicRing = publicRing.canonicalize(mLog, mIndent);
if (publicRing == null) {
return new SaveKeyringResult(SaveKeyringResult.RESULT_ERROR, mLog);
}
int result = internalSavePublicKeyRing(publicRing, progress, true);
if ((result & SaveKeyringResult.RESULT_ERROR) == SaveKeyringResult.RESULT_ERROR) {
return new SaveKeyringResult(SaveKeyringResult.RESULT_ERROR, mLog);
}
} }
} catch (NotFoundException e) { } catch (NotFoundException e) {
// TODO, this WILL error out later because secret rings cannot be inserted without log(LogLevel.DEBUG, LogType.MSG_IS_PUBRING_GENERATE, null);
// public ones publicRing = secretRing.extractPublicKeyRing();
}
if (publicRing != null) {
publicRing = publicRing.canonicalize(mLog, mIndent);
if (publicRing == null) {
return new SaveKeyringResult(SaveKeyringResult.RESULT_ERROR, mLog);
}
int result = internalSavePublicKeyRing(publicRing, progress, true);
if ((result & SaveKeyringResult.RESULT_ERROR) == SaveKeyringResult.RESULT_ERROR) {
return new SaveKeyringResult(SaveKeyringResult.RESULT_ERROR, mLog);
}
} }
progress.setProgress(LogType.MSG_IP_REINSERT_SECRET.getMsgId(), 90, 100); progress.setProgress(LogType.MSG_IP_REINSERT_SECRET.getMsgId(), 90, 100);

View File

@ -174,13 +174,14 @@ public class OperationResultParcel implements Parcelable {
MSG_IS(R.string.msg_is), MSG_IS(R.string.msg_is),
MSG_IS_BAD_TYPE_PUBLIC (R.string.msg_is_bad_type_public), MSG_IS_BAD_TYPE_PUBLIC (R.string.msg_is_bad_type_public),
MSG_IS_DB_EXCEPTION (R.string.msg_is_db_exception), MSG_IS_DB_EXCEPTION (R.string.msg_is_db_exception),
MSG_IS_IMPORTING_SUBKEYS (R.string.msg_is_importing_subkeys),
MSG_IS_FAIL_IO_EXC (R.string.msg_is_io_exc), MSG_IS_FAIL_IO_EXC (R.string.msg_is_io_exc),
MSG_IS_IMPORTING_SUBKEYS (R.string.msg_is_importing_subkeys),
MSG_IS_PUBRING_GENERATE (R.string.msg_is_pubring_generate),
MSG_IS_SUBKEY_NONEXISTENT (R.string.msg_is_subkey_nonexistent), MSG_IS_SUBKEY_NONEXISTENT (R.string.msg_is_subkey_nonexistent),
MSG_IS_SUBKEY_OK (R.string.msg_is_subkey_ok), MSG_IS_SUBKEY_OK (R.string.msg_is_subkey_ok),
MSG_IS_SUBKEY_STRIPPED (R.string.msg_is_subkey_stripped), MSG_IS_SUBKEY_STRIPPED (R.string.msg_is_subkey_stripped),
MSG_IS_SUCCESS (R.string.msg_is_success),
MSG_IS_SUCCESS_IDENTICAL (R.string.msg_is_success_identical), MSG_IS_SUCCESS_IDENTICAL (R.string.msg_is_success_identical),
MSG_IS_SUCCESS (R.string.msg_is_success),
// keyring canonicalization // keyring canonicalization
MSG_KC_PUBLIC (R.string.msg_kc_public), MSG_KC_PUBLIC (R.string.msg_kc_public),

View File

@ -553,11 +553,12 @@
<string name="msg_is_db_exception">Database error!</string> <string name="msg_is_db_exception">Database error!</string>
<string name="msg_is_importing_subkeys">Processing secret subkeys</string> <string name="msg_is_importing_subkeys">Processing secret subkeys</string>
<string name="msg_is_io_exc">Error encoding keyring</string> <string name="msg_is_io_exc">Error encoding keyring</string>
<string name="msg_is_pubring_generate">Generating public keyring from secret keyring</string>
<string name="msg_is_subkey_nonexistent">Subkey %s unavailable in public key</string> <string name="msg_is_subkey_nonexistent">Subkey %s unavailable in public key</string>
<string name="msg_is_subkey_ok">Marked %s as available</string> <string name="msg_is_subkey_ok">Marked %s as available</string>
<string name="msg_is_subkey_stripped">Marked %s as stripped</string> <string name="msg_is_subkey_stripped">Marked %s as stripped</string>
<string name="msg_is_success">Successfully imported secret keyring</string>
<string name="msg_is_success_identical">Keyring contains no new data, nothing to do</string> <string name="msg_is_success_identical">Keyring contains no new data, nothing to do</string>
<string name="msg_is_success">Successfully imported secret keyring</string>
<!-- Keyring Canonicalization log entries --> <!-- Keyring Canonicalization log entries -->
<string name="msg_kc_public">Canonicalizing public keyring %s</string> <string name="msg_kc_public">Canonicalizing public keyring %s</string>

2
extern/spongycastle vendored

@ -1 +1 @@
Subproject commit 2c47e5fca2a820a4fd584066871bed993f1c3919 Subproject commit 09d85b7d7a64b3003210d065c4210ff7fb7a8c6d