Switched to CMSEnvelopedDataParser

This commit is contained in:
M66B 2020-04-19 14:16:43 +02:00
parent e178a39762
commit 4493d77d33
1 changed files with 45 additions and 31 deletions

View File

@ -126,7 +126,7 @@ import org.bouncycastle.asn1.cms.CMSAttributes;
import org.bouncycastle.asn1.cms.Time; import org.bouncycastle.asn1.cms.Time;
import org.bouncycastle.cert.X509CertificateHolder; import org.bouncycastle.cert.X509CertificateHolder;
import org.bouncycastle.cert.jcajce.JcaX509CertificateConverter; import org.bouncycastle.cert.jcajce.JcaX509CertificateConverter;
import org.bouncycastle.cms.CMSEnvelopedData; import org.bouncycastle.cms.CMSEnvelopedDataParser;
import org.bouncycastle.cms.CMSException; import org.bouncycastle.cms.CMSException;
import org.bouncycastle.cms.CMSProcessable; import org.bouncycastle.cms.CMSProcessable;
import org.bouncycastle.cms.CMSProcessableFile; import org.bouncycastle.cms.CMSProcessableFile;
@ -159,6 +159,7 @@ import java.io.FileOutputStream;
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.math.BigInteger;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.security.KeyStore; import java.security.KeyStore;
import java.security.PrivateKey; import java.security.PrivateKey;
@ -5452,6 +5453,7 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences.
InputStream is = null; InputStream is = null;
FileInputStream fis = new FileInputStream(signature); FileInputStream fis = new FileInputStream(signature);
CMSSignedData signedData; CMSSignedData signedData;
// TODO: CMSSignedDataParser
if (sdata) { if (sdata) {
signedData = new CMSSignedData(fis); signedData = new CMSSignedData(fis);
@ -5669,45 +5671,57 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences.
if (input == null) if (input == null)
throw new IllegalArgumentException("Encrypted message missing"); throw new IllegalArgumentException("Encrypted message missing");
// Build enveloped data int count = -1;
CMSEnvelopedData envelopedData; boolean decoded = false;
try (FileInputStream fis = new FileInputStream(input)) { while (!decoded)
envelopedData = new CMSEnvelopedData(fis); try (FileInputStream fis = new FileInputStream(input)) {
} // Create parser
CMSEnvelopedDataParser envelopedData = new CMSEnvelopedDataParser(fis);
// Get recipient info // Get recipient info
JceKeyTransRecipient recipient = new JceKeyTransEnvelopedRecipient(privkey); JceKeyTransRecipient recipient = new JceKeyTransEnvelopedRecipient(privkey);
Collection<RecipientInformation> recipients = envelopedData.getRecipientInfos().getRecipients(); // KeyTransRecipientInformation Collection<RecipientInformation> recipients = envelopedData.getRecipientInfos().getRecipients(); // KeyTransRecipientInformation
// Find recipient // Find recipient
InputStream is = null; if (count < 0) {
if (chain[0].getSerialNumber() != null) BigInteger serialno = chain[0].getSerialNumber();
for (RecipientInformation recipientInfo : recipients) { for (RecipientInformation recipientInfo : recipients) {
KeyTransRecipientId recipientId = (KeyTransRecipientId) recipientInfo.getRID(); KeyTransRecipientId recipientId = (KeyTransRecipientId) recipientInfo.getRID();
if (chain[0].getSerialNumber().equals(recipientId.getSerialNumber())) if (serialno != null && serialno.equals(recipientId.getSerialNumber())) {
try { try {
is = recipientInfo.getContentStream(recipient).getContentStream(); InputStream is = recipientInfo.getContentStream(recipient).getContentStream();
} catch (CMSException ex) { decodeMessage(context, is, message, args);
Log.w(ex); decoded = true;
} catch (CMSException ex) {
Log.w(ex);
}
break; // only one try
}
} }
} } else {
List<RecipientInformation> list = new ArrayList<>(recipients);
// Fallback: try all recipients if (count < list.size()) {
if (is == null) RecipientInformation recipientInfo = list.get(count);
for (RecipientInformation recipientInfo : recipients) try {
try { InputStream is = recipientInfo.getContentStream(recipient).getContentStream();
is = recipientInfo.getContentStream(recipient).getContentStream(); decodeMessage(context, is, message, args);
} catch (CMSException ex) { decoded = true;
Log.w(ex); break;
} catch (CMSException ex) {
Log.w(ex);
}
} else
break; // out of recipients
} }
if (is == null) { count++;
}
if (!decoded) {
if (message.identity != null) if (message.identity != null)
db.identity().setIdentitySignKeyAlias(message.identity, null); db.identity().setIdentitySignKeyAlias(message.identity, null);
throw new IllegalArgumentException(context.getString(R.string.title_unknown_key)); throw new IllegalArgumentException(context.getString(R.string.title_unknown_key));
} }
decodeMessage(context, is, message, args);
} }
return result; return result;