S/MIME: improved get recipient info

master
M66B 2 weeks ago
parent 00da519046
commit 03d3291f3b

@ -177,6 +177,7 @@ import org.bouncycastle.cms.KeyAgreeRecipientInformation;
import org.bouncycastle.cms.KeyTransRecipientInformation; import org.bouncycastle.cms.KeyTransRecipientInformation;
import org.bouncycastle.cms.PKIXRecipientId; import org.bouncycastle.cms.PKIXRecipientId;
import org.bouncycastle.cms.Recipient; import org.bouncycastle.cms.Recipient;
import org.bouncycastle.cms.RecipientId;
import org.bouncycastle.cms.RecipientInformation; import org.bouncycastle.cms.RecipientInformation;
import org.bouncycastle.cms.RecipientInformationStore; import org.bouncycastle.cms.RecipientInformationStore;
import org.bouncycastle.cms.SignerId; import org.bouncycastle.cms.SignerId;
@ -186,9 +187,11 @@ import org.bouncycastle.cms.SignerInformationVerifier;
import org.bouncycastle.cms.jcajce.JcaSimpleSignerInfoVerifierBuilder; import org.bouncycastle.cms.jcajce.JcaSimpleSignerInfoVerifierBuilder;
import org.bouncycastle.cms.jcajce.JceKeyAgreeAuthEnvelopedRecipient; import org.bouncycastle.cms.jcajce.JceKeyAgreeAuthEnvelopedRecipient;
import org.bouncycastle.cms.jcajce.JceKeyAgreeEnvelopedRecipient; import org.bouncycastle.cms.jcajce.JceKeyAgreeEnvelopedRecipient;
import org.bouncycastle.cms.jcajce.JceKeyAgreeRecipientId;
import org.bouncycastle.cms.jcajce.JceKeyTransAuthEnvelopedRecipient; import org.bouncycastle.cms.jcajce.JceKeyTransAuthEnvelopedRecipient;
import org.bouncycastle.cms.jcajce.JceKeyTransEnvelopedRecipient; import org.bouncycastle.cms.jcajce.JceKeyTransEnvelopedRecipient;
import org.bouncycastle.cms.jcajce.JceKeyTransRecipient; import org.bouncycastle.cms.jcajce.JceKeyTransRecipient;
import org.bouncycastle.cms.jcajce.JceKeyTransRecipientId;
import org.bouncycastle.operator.DefaultAlgorithmNameFinder; import org.bouncycastle.operator.DefaultAlgorithmNameFinder;
import org.bouncycastle.util.Store; import org.bouncycastle.util.Store;
import org.json.JSONException; import org.json.JSONException;
@ -10666,7 +10669,7 @@ public class FragmentMessages extends FragmentBase
// Get recipient info // Get recipient info
Collection<RecipientInformation> recipients = Collection<RecipientInformation> recipients =
recipientStore.getRecipients(); // KeyTransRecipientInformation recipientStore.getRecipients(); // KeyTrans, KeyAgree, or another recipient type
EntityLog.log(context, "s/mime private key" + EntityLog.log(context, "s/mime private key" +
" algo=" + privkey.getAlgorithm() + " algo=" + privkey.getAlgorithm() +
@ -10680,12 +10683,16 @@ public class FragmentMessages extends FragmentBase
Log.e(ex); Log.e(ex);
algo = recipientInfo.getKeyEncryptionAlgOID(); algo = recipientInfo.getKeyEncryptionAlgOID();
} }
String serial; String serial = null;
try { try {
PKIXRecipientId recipientId = (PKIXRecipientId) recipientInfo.getRID(); RecipientId rid = recipientInfo.getRID();
serial = recipientId.getSerialNumber().toString(); if (rid instanceof PKIXRecipientId) {
BigInteger value = ((PKIXRecipientId) rid).getSerialNumber();
if (value != null)
serial = value.toString();
}
} catch (Throwable ex) { } catch (Throwable ex) {
serial = null; Log.e(ex);
} }
EntityLog.log(context, "s/mime recipient" + EntityLog.log(context, "s/mime recipient" +
" algo=" + algo + " algo=" + algo +
@ -10708,15 +10715,15 @@ public class FragmentMessages extends FragmentBase
// Find recipient // Find recipient
if (count < 0) { if (count < 0) {
BigInteger serialno = chain[0].getSerialNumber(); RecipientInformation recipientInfo = recipientStore.get(new JceKeyTransRecipientId(chain[0]));
for (RecipientInformation recipientInfo : recipients) { if (recipientInfo == null)
// KeyTransRecipientId or KeyAgreeRecipientId recipientInfo = recipientStore.get(new JceKeyAgreeRecipientId(chain[0]));
PKIXRecipientId recipientId = (PKIXRecipientId) recipientInfo.getRID(); if (recipientInfo != null) {
if (serialno != null && serialno.equals(recipientId.getSerialNumber())) {
try { try {
Recipient recipient = createRecipient(recipientInfo, authEnveloped, privkey); Recipient recipient = createRecipient(recipientInfo, authEnveloped, privkey);
InputStream is = recipientInfo.getContentStream(recipient).getContentStream(); try (InputStream is = recipientInfo.getContentStream(recipient).getContentStream()) {
decodeMessage(context, is, message, args); decodeMessage(context, is, message, args);
}
decoded = true; decoded = true;
Log.i("Encryption algo=" + malgo); Log.i("Encryption algo=" + malgo);
args.putString("algo", malgo); args.putString("algo", malgo);
@ -10727,8 +10734,6 @@ public class FragmentMessages extends FragmentBase
Log.e(ex); Log.e(ex);
last = ex; last = ex;
} }
break; // only one try
}
} }
} else { } else {
List<RecipientInformation> list = new ArrayList<>(recipients); List<RecipientInformation> list = new ArrayList<>(recipients);
@ -10736,10 +10741,10 @@ public class FragmentMessages extends FragmentBase
RecipientInformation recipientInfo = list.get(count); RecipientInformation recipientInfo = list.get(count);
try { try {
Recipient recipient = createRecipient(recipientInfo, authEnveloped, privkey); Recipient recipient = createRecipient(recipientInfo, authEnveloped, privkey);
InputStream is = recipientInfo.getContentStream(recipient).getContentStream(); try (InputStream is = recipientInfo.getContentStream(recipient).getContentStream()) {
decodeMessage(context, is, message, args); decodeMessage(context, is, message, args);
}
decoded = true; decoded = true;
break;
} catch (CMSException ex) { } catch (CMSException ex) {
Log.w(ex); Log.w(ex);
last = ex; last = ex;

Loading…
Cancel
Save