Skip to content

Commit b1ebbc3

Browse files
committed
wip
1 parent 5803a9e commit b1ebbc3

40 files changed

Lines changed: 645 additions & 715 deletions

FlowCrypt/src/main/java/com/flowcrypt/email/api/email/EmailUtil.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,9 +201,9 @@ class EmailUtil {
201201
* @param pgpKeyDetails The key details
202202
* @return A generated [AttachmentInfo].
203203
*/
204-
fun genAttInfoFromPubKey(pgpKeyDetails: PgpKeyDetails?): AttachmentInfo? {
204+
fun genAttInfoFromPubKey(pgpKeyDetails: PgpKeyDetails?, email: String): AttachmentInfo? {
205205
if (pgpKeyDetails != null) {
206-
val fileName = "0x" + pgpKeyDetails.fingerprint.toUpperCase(Locale.getDefault()) + ".asc"
206+
val fileName = "0x" + pgpKeyDetails.fingerprint.uppercase() + ".asc"
207207

208208
return if (!TextUtils.isEmpty(pgpKeyDetails.publicKey)) {
209209
val attachmentInfo = AttachmentInfo()
@@ -212,7 +212,7 @@ class EmailUtil {
212212
attachmentInfo.encodedSize = pgpKeyDetails.publicKey.length.toLong()
213213
attachmentInfo.rawData = pgpKeyDetails.publicKey.toByteArray()
214214
attachmentInfo.type = Constants.MIME_TYPE_PGP_KEY
215-
attachmentInfo.email = pgpKeyDetails.primaryPgpContact.email
215+
attachmentInfo.email = email
216216
attachmentInfo.id = generateContentId()
217217
attachmentInfo.isEncryptionAllowed = false
218218

FlowCrypt/src/main/java/com/flowcrypt/email/api/retrofit/response/model/PublicKeyMsgBlock.kt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ package com.flowcrypt.email.api.retrofit.response.model
77

88
import android.os.Parcel
99
import android.os.Parcelable
10-
import com.flowcrypt.email.model.PgpContact
10+
import com.flowcrypt.email.database.entity.relation.RecipientWithPubKeys
11+
1112
import com.flowcrypt.email.security.model.PgpKeyDetails
1213

1314
import com.google.gson.annotations.Expose
@@ -29,15 +30,16 @@ data class PublicKeyMsgBlock constructor(
2930
@Expose
3031
override val type: MsgBlock.Type = MsgBlock.Type.PUBLIC_KEY
3132

32-
var existingPgpContact: PgpContact? = null
33+
var existingRecipientWithPubKeys: RecipientWithPubKeys? = null
3334

3435
constructor(parcel: Parcel) : this(
3536
parcel.readString(),
3637
parcel.readByte() != 0.toByte(),
3738
parcel.readParcelable(PgpKeyDetails::class.java.classLoader),
3839
parcel.readParcelable(MsgBlockError::class.java.classLoader),
3940
) {
40-
existingPgpContact = parcel.readParcelable(PgpContact::class.java.classLoader)
41+
existingRecipientWithPubKeys =
42+
parcel.readParcelable(RecipientWithPubKeys::class.java.classLoader)
4143
}
4244

4345
override fun writeToParcel(parcel: Parcel, flags: Int) =
@@ -47,7 +49,7 @@ data class PublicKeyMsgBlock constructor(
4749
writeInt((if (complete) 1 else 0))
4850
writeParcelable(keyDetails, flags)
4951
writeParcelable(error, flags)
50-
writeParcelable(existingPgpContact, flags)
52+
writeParcelable(existingRecipientWithPubKeys, flags)
5153
}
5254

5355
override fun describeContents(): Int = 0

FlowCrypt/src/main/java/com/flowcrypt/email/database/dao/PubKeysDao.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,10 @@ interface PubKeysDao : BaseDao<PublicKeyEntity> {
2929

3030
@Query("SELECT * FROM public_keys WHERE fingerprint = :fingerprint")
3131
suspend fun getPublicKeysByFingerprint(fingerprint: String): List<PublicKeyEntity>
32+
33+
@Query("SELECT * FROM public_keys WHERE recipient = :recipient AND fingerprint = :fingerprint")
34+
suspend fun getPublicKeysByRecipientAndFingerprint(
35+
recipient: String,
36+
fingerprint: String
37+
): PublicKeyEntity?
3238
}

FlowCrypt/src/main/java/com/flowcrypt/email/database/dao/RecipientDao.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,9 @@ interface RecipientDao : BaseDao<RecipientEntity> {
5050
@Query("SELECT * FROM recipients WHERE email = :email")
5151
fun getRecipientByEmail(email: String): RecipientEntity?
5252

53+
@Transaction
5354
@Query("SELECT * FROM recipients WHERE email = :email")
54-
fun getRecipientByEmailLD(email: String): LiveData<RecipientEntity?>
55+
fun getRecipientsWithPubKeysByEmailsLD(email: String): LiveData<RecipientWithPubKeys?>
5556

5657
//fixed
5758
@Transaction
@@ -68,5 +69,5 @@ interface RecipientDao : BaseDao<RecipientEntity> {
6869

6970
@Transaction
7071
@Query("SELECT * FROM recipients WHERE email = :email")
71-
fun getRecipientWithPubKeysByEmail(email: String): RecipientWithPubKeys?
72+
suspend fun getRecipientWithPubKeysByEmail(email: String): RecipientWithPubKeys?
7273
}

FlowCrypt/src/main/java/com/flowcrypt/email/database/entity/PublicKeyEntity.kt

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ import android.provider.BaseColumns
1111
import androidx.room.ColumnInfo
1212
import androidx.room.Entity
1313
import androidx.room.ForeignKey
14+
import androidx.room.Ignore
1415
import androidx.room.Index
1516
import androidx.room.PrimaryKey
17+
import com.flowcrypt.email.security.model.PgpKeyDetails
1618

1719
/**
1820
* @author Denis Bondarenko
@@ -49,18 +51,31 @@ data class PublicKeyEntity(
4951
@ColumnInfo(name = "fingerprint") val fingerprint: String,
5052
@ColumnInfo(name = "public_key") val publicKey: ByteArray
5153
) : Parcelable {
54+
55+
@Ignore
56+
var pgpKeyDetails: PgpKeyDetails? = null
57+
58+
@Ignore
59+
var isNotUsable: Boolean? = null
60+
5261
constructor(parcel: Parcel) : this(
5362
parcel.readValue(Long::class.java.classLoader) as? Long,
5463
requireNotNull(parcel.readString()),
5564
requireNotNull(parcel.readString()),
5665
requireNotNull(parcel.createByteArray())
57-
)
66+
) {
67+
pgpKeyDetails = parcel.readParcelable(PgpKeyDetails::class.java.classLoader)
68+
isNotUsable = parcel.readValue(Boolean::class.java.classLoader) as? Boolean
69+
}
70+
5871

5972
override fun writeToParcel(parcel: Parcel, flags: Int) {
6073
parcel.writeValue(id)
6174
parcel.writeString(recipient)
6275
parcel.writeString(fingerprint)
6376
parcel.writeByteArray(publicKey)
77+
parcel.writeParcelable(pgpKeyDetails, flags)
78+
parcel.writeValue(isNotUsable)
6479
}
6580

6681
override fun describeContents(): Int {
@@ -77,6 +92,8 @@ data class PublicKeyEntity(
7792
if (recipient != other.recipient) return false
7893
if (fingerprint != other.fingerprint) return false
7994
if (!publicKey.contentEquals(other.publicKey)) return false
95+
if (pgpKeyDetails != other.pgpKeyDetails) return false
96+
if (isNotUsable != other.isNotUsable) return false
8097

8198
return true
8299
}
@@ -86,6 +103,8 @@ data class PublicKeyEntity(
86103
result = 31 * result + recipient.hashCode()
87104
result = 31 * result + fingerprint.hashCode()
88105
result = 31 * result + publicKey.contentHashCode()
106+
result = 31 * result + (pgpKeyDetails?.hashCode() ?: 0)
107+
result = 31 * result + (isNotUsable?.hashCode() ?: 0)
89108
return result
90109
}
91110

FlowCrypt/src/main/java/com/flowcrypt/email/database/entity/RecipientEntity.kt

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,8 @@ import android.os.Parcelable
1010
import android.provider.BaseColumns
1111
import androidx.room.ColumnInfo
1212
import androidx.room.Entity
13-
import androidx.room.Ignore
1413
import androidx.room.Index
1514
import androidx.room.PrimaryKey
16-
import com.flowcrypt.email.model.PgpContact
17-
import com.flowcrypt.email.security.model.PgpKeyDetails
1815

1916
/**
2017
* @author Denis Bondarenko
@@ -37,26 +34,12 @@ data class RecipientEntity(
3734
@ColumnInfo(name = "last_use", defaultValue = "0") val lastUse: Long = 0
3835
) : Parcelable {
3936

40-
@Ignore
41-
var pgpKeyDetails: PgpKeyDetails? = null
42-
4337
constructor(parcel: Parcel) : this(
4438
parcel.readValue(Long::class.java.classLoader) as? Long,
4539
requireNotNull(parcel.readString()),
4640
parcel.readString(),
4741
parcel.readLong()
48-
) {
49-
pgpKeyDetails = parcel.readParcelable(PgpKeyDetails::class.java.classLoader)
50-
}
51-
52-
fun toPgpContact(): PgpContact {
53-
return PgpContact(
54-
email = email,
55-
name = name,
56-
lastUse = lastUse,
57-
pgpKeyDetails = pgpKeyDetails
58-
)
59-
}
42+
)
6043

6144
//todo-denbond7 need to think about this class.
6245
enum class Type {
@@ -68,7 +51,6 @@ data class RecipientEntity(
6851
parcel.writeString(email)
6952
parcel.writeString(name)
7053
parcel.writeLong(lastUse)
71-
parcel.writeParcelable(pgpKeyDetails, flags)
7254
}
7355

7456
override fun describeContents(): Int {

FlowCrypt/src/main/java/com/flowcrypt/email/database/entity/relation/RecipientWithPubKeys.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,18 @@ data class RecipientWithPubKeys(
4242
return 0
4343
}
4444

45+
fun hasPgp(): Boolean {
46+
return publicKeys.isNotEmpty()
47+
}
48+
49+
fun hasNotExpiredKeys(): Boolean {
50+
return publicKeys.any { it.pgpKeyDetails?.isExpired?.not() ?: false }
51+
}
52+
53+
fun hasUsableKeys(): Boolean {
54+
return publicKeys.any { if (it.isNotUsable != null) it.isNotUsable?.not() ?: false else true }
55+
}
56+
4557
companion object CREATOR : Parcelable.Creator<RecipientWithPubKeys> {
4658
override fun createFromParcel(parcel: Parcel) = RecipientWithPubKeys(parcel)
4759
override fun newArray(size: Int): Array<RecipientWithPubKeys?> = arrayOfNulls(size)

FlowCrypt/src/main/java/com/flowcrypt/email/extensions/org/bouncycastle/openpgp/PGPKeyRingExt.kt

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
package com.flowcrypt.email.extensions.org.bouncycastle.openpgp
77

88
import androidx.annotation.WorkerThread
9-
import com.flowcrypt.email.model.PgpContact
109
import com.flowcrypt.email.security.model.Algo
1110
import com.flowcrypt.email.security.model.KeyId
1211
import com.flowcrypt.email.security.model.PgpKeyDetails
@@ -85,11 +84,6 @@ fun PGPKeyRing.toPgpKeyDetails(): PgpKeyDetails {
8584
)
8685
}
8786

88-
fun PGPKeyRing.pgpContacts(): List<PgpContact> {
89-
val list = publicKey.userIDs.iterator().asSequence().toList()
90-
return PgpContact.determinePgpContacts(list)
91-
}
92-
9387
@Throws(IOException::class)
9488
fun PGPKeyRing.armor(headers: List<Pair<String, String>>? = PgpArmor.FLOWCRYPT_HEADERS): String {
9589
ByteArrayOutputStream().use { out ->

FlowCrypt/src/main/java/com/flowcrypt/email/jetpack/viewmodel/AccountViewModel.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import com.flowcrypt.email.R
1717
import com.flowcrypt.email.api.email.model.AuthCredentials
1818
import com.flowcrypt.email.api.retrofit.response.base.Result
1919
import com.flowcrypt.email.database.entity.AccountEntity
20-
import com.flowcrypt.email.jetpack.workmanager.sync.LoadContactsWorker
20+
import com.flowcrypt.email.jetpack.workmanager.sync.LoadRecipientsWorker
2121
import com.flowcrypt.email.security.KeyStoreCryptoManager
2222
import com.flowcrypt.email.service.IdleService
2323
import kotlinx.coroutines.Dispatchers
@@ -84,7 +84,7 @@ open class AccountViewModel(application: Application) : RoomBasicViewModel(appli
8484
)
8585
}
8686

87-
LoadContactsWorker.enqueue(getApplication())
87+
LoadRecipientsWorker.enqueue(getApplication())
8888

8989
addNewAccountLiveData.value = Result.success(true)
9090
} catch (e: Exception) {

FlowCrypt/src/main/java/com/flowcrypt/email/jetpack/viewmodel/MsgDetailsViewModel.kt

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -411,22 +411,22 @@ class MsgDetailsViewModel(
411411

412412
private suspend fun preResultsProcessing(blocks: List<MsgBlock>) {
413413
for (block in blocks) {
414-
if (block is PublicKeyMsgBlock) {
415-
val keyDetails = block.keyDetails ?: continue
416-
val pgpContact = keyDetails.primaryPgpContact
417-
//todo-denbond7 need to rework on this code
418-
val recipientEntity =
419-
roomDatabase.recipientDao().getRecipientByEmailSuspend(pgpContact.email)
420-
//block.existingPgpContact = recipientEntity?.toPgpContact()
421-
}
414+
when (block) {
415+
is PublicKeyMsgBlock -> {
416+
val keyDetails = block.keyDetails ?: continue
417+
val recipient = keyDetails.mimeAddresses.firstOrNull()?.address ?: continue
418+
block.existingRecipientWithPubKeys =
419+
roomDatabase.recipientDao().getRecipientWithPubKeysByEmail(recipient)
420+
}
422421

423-
if (block is DecryptErrorMsgBlock) {
424-
if (block.decryptErr?.details?.type == PgpDecrypt.DecryptionErrorType.NEED_PASSPHRASE) {
425-
val fingerprints = block.decryptErr.fingerprints ?: emptyList()
426-
if (fingerprints.isEmpty()) {
427-
ExceptionUtil.handleError(IllegalStateException("Fingerprints were not provided"))
428-
} else {
429-
passphraseNeededLiveData.postValue(fingerprints)
422+
is DecryptErrorMsgBlock -> {
423+
if (block.decryptErr?.details?.type == PgpDecrypt.DecryptionErrorType.NEED_PASSPHRASE) {
424+
val fingerprints = block.decryptErr.fingerprints ?: emptyList()
425+
if (fingerprints.isEmpty()) {
426+
ExceptionUtil.handleError(IllegalStateException("Fingerprints were not provided"))
427+
} else {
428+
passphraseNeededLiveData.postValue(fingerprints)
429+
}
430430
}
431431
}
432432
}

0 commit comments

Comments
 (0)