Skip to content

Commit 0a7580e

Browse files
committed
Changed the encryption logic. | Close #106.
1 parent babbc7d commit 0a7580e

File tree

3 files changed

+56
-33
lines changed

3 files changed

+56
-33
lines changed

FlowCrypt/src/main/java/com/flowcrypt/email/api/email/sync/tasks/SendMessageSyncTask.java

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public void runSMTPAction(AccountDao accountDao, Session session, Store store, S
103103

104104
updateContactsLastUseDateTime(context);
105105

106-
MimeMessage mimeMessage = createMimeMessage(session, context, pgpCacheDirectory);
106+
MimeMessage mimeMessage = createMimeMessage(session, context, accountDao, pgpCacheDirectory);
107107

108108
Transport transport = prepareTransportForSmtp(syncListener.getContext(), session, accountDao);
109109
transport.sendMessage(mimeMessage, mimeMessage.getAllRecipients());
@@ -155,16 +155,16 @@ private void saveCopyOfSentMessage(AccountDao accountDao, Store store, Context c
155155
*
156156
* @param session Will be used to create {@link MimeMessage}
157157
* @param context Interface to global information about an application environment.
158-
* @param pgpCacheDirectory The cache directory which contains temp files.
159-
* @return {@link MimeMessage}
158+
* @param accountDao The {@link AccountDao} which contains information about account.
159+
* @param pgpCacheDirectory The cache directory which contains temp files. @return {@link MimeMessage}
160160
* @throws IOException
161161
* @throws MessagingException
162162
*/
163163
@NonNull
164-
private MimeMessage createMimeMessage(Session session, Context context, File pgpCacheDirectory)
165-
throws IOException, MessagingException {
164+
private MimeMessage createMimeMessage(Session session, Context context, AccountDao accountDao,
165+
File pgpCacheDirectory) throws IOException, MessagingException {
166166
Js js = new Js(context, new SecurityStorageConnector(context));
167-
String[] pubKeys = getPubKeys(js, context);
167+
String[] pubKeys = getPubKeys(context, js, accountDao);
168168

169169
String rawMessage = generateRawMessageWithoutAttachments(js, pubKeys);
170170

@@ -288,42 +288,59 @@ private void updateContactsLastUseDateTime(Context context) {
288288
/**
289289
* Get public keys for recipients + keys of the sender;
290290
*
291-
* @param js - {@link Js} util class.
292-
* @param context - Interface to global information about an application environment.
291+
* @param context Interface to global information about an application environment.
292+
* @param accountDao The {@link AccountDao} which contains information about account.
293+
* @param js - {@link Js} util class.
293294
* @return <tt>String[]</tt> An array of public keys.
294295
*/
295-
private String[] getPubKeys(Js js, Context context) {
296+
private String[] getPubKeys(Context context, Js js, AccountDao accountDao) {
296297
ArrayList<String> publicKeys = new ArrayList<>();
297298
for (PgpContact pgpContact : outgoingMessageInfo.getToPgpContacts()) {
298299
if (!TextUtils.isEmpty(pgpContact.getPubkey())) {
299300
publicKeys.add(pgpContact.getPubkey());
300301
}
301302
}
302303

303-
publicKeys.addAll(generateOwnPublicKeys(js, context));
304+
publicKeys.add(getAccountPublicKey(context, js, accountDao));
304305

305306
return publicKeys.toArray(new String[0]);
306307
}
307308

308309
/**
309-
* Get public keys of the sender;
310+
* Get a public key of the sender;
310311
*
311-
* @param js - {@link Js} util class.
312-
* @param context - Interface to global information about an application environment.
313-
* @return <tt>String[]</tt> An array of the sender public keys.
312+
* @param context Interface to global information about an application environment.
313+
* @param js {@link Js} util class.
314+
* @param accountDao The {@link AccountDao} which contains information about account.
315+
* @return <tt>String</tt> The sender public key.
314316
*/
315-
private ArrayList<String> generateOwnPublicKeys(Js js, Context context) {
316-
ArrayList<String> publicKeys = new ArrayList<>();
317+
private String getAccountPublicKey(Context context, Js js, AccountDao accountDao) {
318+
PgpContact pgpContact = new ContactsDaoSource().getPgpContact(context, accountDao.getEmail());
317319

318-
SecurityStorageConnector securityStorageConnector = new SecurityStorageConnector(context);
319-
PgpKeyInfo[] pgpKeyInfoArray = securityStorageConnector.getAllPgpPrivateKeys();
320+
if (pgpContact != null && !TextUtils.isEmpty(pgpContact.getPubkey())) {
321+
return pgpContact.getPubkey();
322+
}
320323

324+
PgpKeyInfo[] pgpKeyInfoArray = new SecurityStorageConnector(context).getAllPgpPrivateKeys();
321325
for (PgpKeyInfo pgpKeyInfo : pgpKeyInfoArray) {
322326
PgpKey pgpKey = js.crypto_key_read(pgpKeyInfo.getArmored());
323-
publicKeys.add(pgpKey.toPublic().armor());
327+
if (pgpKey != null) {
328+
PgpKey publicKey = pgpKey.toPublic();
329+
if (publicKey != null) {
330+
PgpContact primaryUserId = pgpKey.getPrimaryUserId();
331+
if (primaryUserId != null) {
332+
if (!TextUtils.isEmpty(publicKey.armor())) {
333+
primaryUserId.setPubkey(publicKey.armor());
334+
new ContactsDaoSource().addRow(context, primaryUserId);
335+
return primaryUserId.getPubkey();
336+
}
337+
break;
338+
}
339+
}
340+
}
324341
}
325342

326-
return publicKeys;
343+
throw new IllegalArgumentException("The sender doesn't have a public key");
327344
}
328345

329346
/**

FlowCrypt/src/main/java/com/flowcrypt/email/ui/activity/fragment/base/CreateMessageFragment.java

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -439,12 +439,10 @@ private OutgoingMessageInfo getOutgoingMessageInfo() {
439439

440440
}
441441

442+
List<String> contacts = editTextRecipients.getChipValues();
442443
if (onChangeMessageEncryptedTypeListener.getMessageEncryptionType() == MessageEncryptionType.ENCRYPTED) {
443-
pgpContacts = contactsDaoSource.getPgpContactsListFromDatabase(getContext(),
444-
editTextRecipients.getChipValues());
444+
pgpContacts = contactsDaoSource.getPgpContactsListFromDatabase(getContext(), contacts);
445445
} else {
446-
List<String> contacts = editTextRecipients.getChipValues();
447-
448446
for (String s : contacts) {
449447
pgpContacts.add(new PgpContact(s, null));
450448
}
@@ -499,7 +497,7 @@ private void updateChips() {
499497
}
500498

501499
private void initChipsView(View view) {
502-
editTextRecipients = (NachoTextView) view.findViewById(R.id.editTextRecipient);
500+
editTextRecipients = view.findViewById(R.id.editTextRecipient);
503501
editTextRecipients.setNachoValidator(new ChipifyingNachoValidator());
504502
editTextRecipients.setIllegalCharacters(',');
505503
editTextRecipients.setChipTokenizer(new SingleCharacterSpanChipTokenizer(getContext(),
@@ -585,12 +583,12 @@ private void removePgpContactFromRecipientsField(PgpContact deleteCandidatePgpCo
585583
* @param view The root fragment view.
586584
*/
587585
private void initViews(View view) {
588-
layoutAttachments = (ViewGroup) view.findViewById(R.id.layoutAttachments);
586+
layoutAttachments = view.findViewById(R.id.layoutAttachments);
589587
initChipsView(view);
590588

591-
editTextEmailSubject = (EditText) view.findViewById(R.id.editTextEmailSubject);
592-
editTextEmailMessage = (EditText) view.findViewById(R.id.editTextEmailMessage);
593-
textInputLayoutEmailMessage = (TextInputLayout) view.findViewById(R.id.textInputLayoutEmailMessage);
589+
editTextEmailSubject = view.findViewById(R.id.editTextEmailSubject);
590+
editTextEmailMessage = view.findViewById(R.id.editTextEmailMessage);
591+
textInputLayoutEmailMessage = view.findViewById(R.id.textInputLayoutEmailMessage);
594592

595593
layoutContent = view.findViewById(R.id.scrollView);
596594
progressBarCheckContactsDetails = view.findViewById(R.id.progressBarCheckContactsDetails);
@@ -754,10 +752,10 @@ private void showAttachments() {
754752
for (final AttachmentInfo attachmentInfo : attachmentInfoList) {
755753
final View rootView = layoutInflater.inflate(R.layout.attachment_item, layoutAttachments, false);
756754

757-
TextView textViewAttachmentName = (TextView) rootView.findViewById(R.id.textViewAttchmentName);
755+
TextView textViewAttachmentName = rootView.findViewById(R.id.textViewAttchmentName);
758756
textViewAttachmentName.setText(attachmentInfo.getName());
759757

760-
TextView textViewAttachmentSize = (TextView) rootView.findViewById(R.id.textViewAttachmentSize);
758+
TextView textViewAttachmentSize = rootView.findViewById(R.id.textViewAttachmentSize);
761759
textViewAttachmentSize.setText(Formatter.formatFileSize(getContext(), attachmentInfo.getEncodedSize()));
762760

763761
View imageButtonDownloadAttachment = rootView.findViewById(R.id.imageButtonDownloadAttachment);

FlowCrypt/src/main/java/com/flowcrypt/email/ui/loader/EncryptAndSavePrivateKeysAsyncTaskLoader.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@
1414
import com.eclipsesource.v8.V8Object;
1515
import com.flowcrypt.email.R;
1616
import com.flowcrypt.email.database.dao.KeysDao;
17+
import com.flowcrypt.email.database.dao.source.ContactsDaoSource;
1718
import com.flowcrypt.email.database.dao.source.KeysDaoSource;
1819
import com.flowcrypt.email.js.Js;
20+
import com.flowcrypt.email.js.PgpContact;
1921
import com.flowcrypt.email.js.PgpKey;
2022
import com.flowcrypt.email.model.KeyDetails;
2123
import com.flowcrypt.email.model.results.LoaderResult;
@@ -93,6 +95,12 @@ public LoaderResult loadInBackground() {
9395
if (!keysDaoSource.isKeyExist(getContext(), pgpKey.getLongid())) {
9496
Uri uri = saveKeyToDatabase(keyStoreCryptoManager, keyDetails,
9597
pgpKey, passphrase);
98+
PgpContact pgpContact = pgpKey.getPrimaryUserId();
99+
PgpKey publicKey = pgpKey.toPublic();
100+
if (pgpContact != null) {
101+
pgpContact.setPubkey(publicKey.armor());
102+
new ContactsDaoSource().addRow(getContext(), pgpContact);
103+
}
96104
isOneOrMoreKeySaved = uri != null;
97105
} else if (isThrowErrorIfDuplicateFound) {
98106
return new LoaderResult(null, new Exception(getContext().getString(R
@@ -126,8 +134,8 @@ public void onStopLoading() {
126134
* for generate an algorithm parameter spec String.
127135
*
128136
* @param keyStoreCryptoManager A {@link KeyStoreCryptoManager} which will bu used to encrypt
129-
* information about a key;
130-
* @param keyDetails The private key details
137+
* information about a key;
138+
* @param keyDetails The private key details
131139
* @param pgpKey A normalized key;
132140
* @param passphrase A passphrase which user entered;
133141
*/

0 commit comments

Comments
 (0)