dsa and elgamal fix

This commit is contained in:
Dominik 2012-11-02 10:49:37 +01:00
parent 1ea00ef082
commit beb2ad0047
4 changed files with 22 additions and 8 deletions

View File

@ -260,7 +260,8 @@
<string name="error_wrongPassPhrase">wrong passphrase</string> <string name="error_wrongPassPhrase">wrong passphrase</string>
<string name="error_savingKeys">error saving some key(s)</string> <string name="error_savingKeys">error saving some key(s)</string>
<string name="error_couldNotExtractPrivateKey">could not extract private key</string> <string name="error_couldNotExtractPrivateKey">could not extract private key</string>
<string name="error_couldNotAddDSASubkey">Master key must be DSA to add DSA subkeys</string>
<!-- progress_lowerCase: lowercase, phrases, usually ending in '…' --> <!-- progress_lowerCase: lowercase, phrases, usually ending in '…' -->
<string name="progress_done">done.</string> <string name="progress_done">done.</string>
<string name="progress_initializing">initializing…</string> <string name="progress_initializing">initializing…</string>

View File

@ -22,6 +22,7 @@ import org.spongycastle.bcpg.ArmoredOutputStream;
import org.spongycastle.bcpg.BCPGOutputStream; import org.spongycastle.bcpg.BCPGOutputStream;
import org.spongycastle.bcpg.CompressionAlgorithmTags; import org.spongycastle.bcpg.CompressionAlgorithmTags;
import org.spongycastle.bcpg.HashAlgorithmTags; import org.spongycastle.bcpg.HashAlgorithmTags;
import org.spongycastle.bcpg.PublicKeyAlgorithmTags;
import org.spongycastle.bcpg.SymmetricKeyAlgorithmTags; import org.spongycastle.bcpg.SymmetricKeyAlgorithmTags;
import org.spongycastle.bcpg.sig.KeyFlags; import org.spongycastle.bcpg.sig.KeyFlags;
import org.spongycastle.jce.provider.BouncyCastleProvider; import org.spongycastle.jce.provider.BouncyCastleProvider;
@ -230,6 +231,11 @@ public class PGPMain {
switch (algorithmChoice) { switch (algorithmChoice) {
case Id.choice.algorithm.dsa: { case Id.choice.algorithm.dsa: {
if (masterSecretKey != null
&& masterSecretKey.getPublicKey().getAlgorithm() != PublicKeyAlgorithmTags.DSA) {
throw new ApgGeneralException(
context.getString(R.string.error_couldNotAddDSASubkey));
}
keyGen = KeyPairGenerator.getInstance("DSA", BOUNCY_CASTLE_PROVIDER_NAME); keyGen = KeyPairGenerator.getInstance("DSA", BOUNCY_CASTLE_PROVIDER_NAME);
keyGen.initialize(keySize, new SecureRandom()); keyGen.initialize(keySize, new SecureRandom());
algorithm = PGPPublicKey.DSA; algorithm = PGPPublicKey.DSA;
@ -241,7 +247,7 @@ public class PGPMain {
throw new ApgGeneralException( throw new ApgGeneralException(
context.getString(R.string.error_masterKeyMustNotBeElGamal)); context.getString(R.string.error_masterKeyMustNotBeElGamal));
} }
keyGen = KeyPairGenerator.getInstance("ELGAMAL", BOUNCY_CASTLE_PROVIDER_NAME); keyGen = KeyPairGenerator.getInstance("ElGamal", BOUNCY_CASTLE_PROVIDER_NAME);
BigInteger p = Primes.getBestPrime(keySize); BigInteger p = Primes.getBestPrime(keySize);
BigInteger g = new BigInteger("2"); BigInteger g = new BigInteger("2");
@ -271,8 +277,6 @@ public class PGPMain {
// define hashing and signing algos // define hashing and signing algos
PGPDigestCalculator sha1Calc = new JcaPGPDigestCalculatorProviderBuilder().build().get( PGPDigestCalculator sha1Calc = new JcaPGPDigestCalculatorProviderBuilder().build().get(
HashAlgorithmTags.SHA1); HashAlgorithmTags.SHA1);
PGPContentSignerBuilder certificationSignerBuilder = new JcaPGPContentSignerBuilder(keyPair
.getPublicKey().getAlgorithm(), HashAlgorithmTags.SHA1);
// Build key encrypter and decrypter based on passphrase // Build key encrypter and decrypter based on passphrase
PBESecretKeyEncryptor keyEncryptor = new JcePBESecretKeyEncryptorBuilder( PBESecretKeyEncryptor keyEncryptor = new JcePBESecretKeyEncryptorBuilder(
@ -282,17 +286,24 @@ public class PGPMain {
BOUNCY_CASTLE_PROVIDER_NAME).build(passPhrase.toCharArray()); BOUNCY_CASTLE_PROVIDER_NAME).build(passPhrase.toCharArray());
PGPKeyRingGenerator ringGen = null; PGPKeyRingGenerator ringGen = null;
PGPContentSignerBuilder certificationSignerBuilder = null;
if (masterSecretKey == null) { if (masterSecretKey == null) {
certificationSignerBuilder = new JcaPGPContentSignerBuilder(keyPair.getPublicKey()
.getAlgorithm(), HashAlgorithmTags.SHA1);
// build keyRing with only this one master key in it! // build keyRing with only this one master key in it!
ringGen = new PGPKeyRingGenerator(PGPSignature.DEFAULT_CERTIFICATION, keyPair, "", ringGen = new PGPKeyRingGenerator(PGPSignature.POSITIVE_CERTIFICATION, keyPair, "",
sha1Calc, null, null, certificationSignerBuilder, keyEncryptor); sha1Calc, null, null, certificationSignerBuilder, keyEncryptor);
} else { } else {
PGPPublicKey masterPublicKey = masterSecretKey.getPublicKey(); PGPPublicKey masterPublicKey = masterSecretKey.getPublicKey();
PGPPrivateKey masterPrivateKey = masterSecretKey.extractPrivateKey(keyDecryptor); PGPPrivateKey masterPrivateKey = masterSecretKey.extractPrivateKey(keyDecryptor);
PGPKeyPair masterKeyPair = new PGPKeyPair(masterPublicKey, masterPrivateKey); PGPKeyPair masterKeyPair = new PGPKeyPair(masterPublicKey, masterPrivateKey);
certificationSignerBuilder = new JcaPGPContentSignerBuilder(masterKeyPair
.getPublicKey().getAlgorithm(), HashAlgorithmTags.SHA1);
// build keyRing with master key and new key as subkey (certified by masterkey) // build keyRing with master key and new key as subkey (certified by masterkey)
ringGen = new PGPKeyRingGenerator(PGPSignature.DEFAULT_CERTIFICATION, masterKeyPair, ringGen = new PGPKeyRingGenerator(PGPSignature.POSITIVE_CERTIFICATION, masterKeyPair,
"", sha1Calc, null, null, certificationSignerBuilder, keyEncryptor); "", sha1Calc, null, null, certificationSignerBuilder, keyEncryptor);
ringGen.addSubKey(keyPair); ringGen.addSubKey(keyPair);

View File

@ -998,7 +998,7 @@ public class EncryptActivity extends SherlockFragmentActivity {
initialKeyIds[i] = keyIds.get(i); initialKeyIds[i] = keyIds.get(i);
} }
} }
intent.putExtra(SelectPublicKeyActivity.RESULT_EXTRA_MASTER_KEY_IDS, initialKeyIds); intent.putExtra(SelectPublicKeyActivity.EXTRA_SELECTED_MASTER_KEY_IDS, initialKeyIds);
startActivityForResult(intent, Id.request.public_keys); startActivityForResult(intent, Id.request.public_keys);
} }

View File

@ -35,6 +35,8 @@ public class SelectPublicKeyActivity extends SherlockFragmentActivity {
public static final String ACTION_SELECT_PUBLIC_KEYS = Constants.INTENT_PREFIX public static final String ACTION_SELECT_PUBLIC_KEYS = Constants.INTENT_PREFIX
+ "SELECT_PUBLIC_KEYS"; + "SELECT_PUBLIC_KEYS";
public static final String EXTRA_SELECTED_MASTER_KEY_IDS = "masterKeyIds";
public static final String RESULT_EXTRA_MASTER_KEY_IDS = "masterKeyIds"; public static final String RESULT_EXTRA_MASTER_KEY_IDS = "masterKeyIds";
public static final String RESULT_EXTRA_USER_IDS = "userIds"; public static final String RESULT_EXTRA_USER_IDS = "userIds";
@ -95,7 +97,7 @@ public class SelectPublicKeyActivity extends SherlockFragmentActivity {
// } // }
// preselected master keys // preselected master keys
selectedMasterKeyIds = intent.getLongArrayExtra(RESULT_EXTRA_MASTER_KEY_IDS); selectedMasterKeyIds = intent.getLongArrayExtra(EXTRA_SELECTED_MASTER_KEY_IDS);
} }
/** /**