aboutsummaryrefslogtreecommitdiffstats
path: root/openpgp-ktx/src/main/java/org/openintents/openpgp/OpenPgpSignatureResult.kt
blob: 78a2d741a21de7e547d5597c487563c380778066 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/*
 * Copyright © 2014-2021 The Android Password Store Authors. All Rights Reserved.
 * SPDX-License-Identifier: GPL-3.0-only
 */
@file:JvmName("OpenPgpSignatureResult")

package org.openintents.openpgp

import android.os.Parcel
import android.os.Parcelable
import android.os.Parcelable.Creator
import java.util.Date
import me.msfjarvis.openpgpktx.util.OpenPgpUtils

public class OpenPgpSignatureResult : Parcelable {

    private val result: Int
    private val keyId: Long
    private val primaryUserId: String?
    private val userIds: ArrayList<String>?
    private val confirmedUserIds: ArrayList<String>?
    private val senderStatusResult: SenderStatusResult?
    private val signatureTimestamp: Date?
    private val autocryptPeerentityResult: AutocryptPeerResult?

    private constructor(
        signatureStatus: Int,
        signatureUserId: String?,
        keyId: Long,
        userIds: ArrayList<String>?,
        confirmedUserIds: ArrayList<String>?,
        senderStatusResult: SenderStatusResult?,
        signatureOnly: Boolean?,
        signatureTimestamp: Date?,
        autocryptPeerentityResult: AutocryptPeerResult?
    ) {
        result = signatureStatus
        primaryUserId = signatureUserId
        this.keyId = keyId
        this.userIds = userIds
        this.confirmedUserIds = confirmedUserIds
        this.senderStatusResult = senderStatusResult
        this.signatureTimestamp = signatureTimestamp
        this.autocryptPeerentityResult = autocryptPeerentityResult
    }

    private constructor(source: Parcel, version: Int) {
        result = source.readInt()
        // we dropped support for signatureOnly, but need to skip the value for compatibility
        source.readByte()
        primaryUserId = source.readString()
        keyId = source.readLong()
        userIds = if (version > 1) {
            source.createStringArrayList()
        } else {
            null
        }
        // backward compatibility for this exact version
        if (version > 2) {
            senderStatusResult = readEnumWithNullAndFallback(
                source,
                SenderStatusResult.values(),
                SenderStatusResult.UNKNOWN
            )
            confirmedUserIds = source.createStringArrayList()
        } else {
            senderStatusResult = SenderStatusResult.UNKNOWN
            confirmedUserIds = null
        }
        signatureTimestamp = if (version > 3) {
            if (source.readInt() > 0) Date(source.readLong()) else null
        } else {
            null
        }
        autocryptPeerentityResult = if (version > 4) {
            readEnumWithNullAndFallback(
                source,
                AutocryptPeerResult.values(),
                null
            )
        } else {
            null
        }
    }

    public fun getUserIds(): List<String> {
        return (userIds ?: arrayListOf()).toList()
    }

    public fun getConfirmedUserIds(): List<String> {
        return (confirmedUserIds ?: arrayListOf()).toList()
    }

    override fun describeContents(): Int {
        return 0
    }

    override fun writeToParcel(dest: Parcel, flags: Int) {
        /**
         * NOTE: When adding fields in the process of updating this API, make sure to bump
         * [.PARCELABLE_VERSION].
         */
        dest.writeInt(PARCELABLE_VERSION)
        // Inject a placeholder that will store the parcel size from this point on
        // (not including the size itself).
        val sizePosition = dest.dataPosition()
        dest.writeInt(0)
        val startPosition = dest.dataPosition()
        // version 1
        dest.writeInt(result)
        // signatureOnly is deprecated since version 3. we pass a dummy value for compatibility
        dest.writeByte(0.toByte())
        dest.writeString(primaryUserId)
        dest.writeLong(keyId)
        // version 2
        dest.writeStringList(userIds)
        // version 3
        writeEnumWithNull(dest, senderStatusResult)
        dest.writeStringList(confirmedUserIds)
        // version 4
        if (signatureTimestamp != null) {
            dest.writeInt(1)
            dest.writeLong(signatureTimestamp.time)
        } else {
            dest.writeInt(0)
        }
        // version 5
        writeEnumWithNull(dest, autocryptPeerentityResult)
        // Go back and write the size
        val parcelableSize = dest.dataPosition() - startPosition
        dest.setDataPosition(sizePosition)
        dest.writeInt(parcelableSize)
        dest.setDataPosition(startPosition + parcelableSize)
    }

    override fun toString(): String {
        var out = "\nresult: $result"
        out += "\nprimaryUserId: $primaryUserId"
        out += "\nuserIds: $userIds"
        out += "\nkeyId: " + OpenPgpUtils.convertKeyIdToHex(keyId)
        return out
    }

    @Deprecated("")
    public fun withSignatureOnlyFlag(signatureOnly: Boolean): OpenPgpSignatureResult {
        return OpenPgpSignatureResult(
            result, primaryUserId, keyId, userIds, confirmedUserIds,
            senderStatusResult, signatureOnly, signatureTimestamp, autocryptPeerentityResult
        )
    }

    public fun withAutocryptPeerResult(autocryptPeerentityResult: AutocryptPeerResult?): OpenPgpSignatureResult {
        return OpenPgpSignatureResult(
            result, primaryUserId, keyId, userIds, confirmedUserIds,
            senderStatusResult, null, signatureTimestamp, autocryptPeerentityResult
        )
    }

    public enum class SenderStatusResult {
        UNKNOWN, USER_ID_CONFIRMED, USER_ID_UNCONFIRMED, USER_ID_MISSING;
    }

    public enum class AutocryptPeerResult {
        OK, NEW, MISMATCH;
    }

    public companion object CREATOR : Creator<OpenPgpSignatureResult> {

        /**
         * Since there might be a case where new versions of the client using the library getting
         * old versions of the protocol (and thus old versions of this class), we need a versioning
         * system for the parcels sent between the clients and the providers.
         */
        private const val PARCELABLE_VERSION = 5

        // content not signed
        public const val RESULT_NO_SIGNATURE: Int = -1

        // invalid signature!
        public const val RESULT_INVALID_SIGNATURE: Int = 0

        // successfully verified signature, with confirmed key
        public const val RESULT_VALID_KEY_CONFIRMED: Int = 1

        // no key was found for this signature verification
        public const val RESULT_KEY_MISSING: Int = 2

        // successfully verified signature, but with unconfirmed key
        public const val RESULT_VALID_KEY_UNCONFIRMED: Int = 3

        // key has been revoked -> invalid signature!
        public const val RESULT_INVALID_KEY_REVOKED: Int = 4

        // key is expired -> invalid signature!
        public const val RESULT_INVALID_KEY_EXPIRED: Int = 5

        // insecure cryptographic algorithms/protocol -> invalid signature!
        public const val RESULT_INVALID_KEY_INSECURE: Int = 6

        override fun createFromParcel(source: Parcel): OpenPgpSignatureResult? {
            val version = source.readInt() // parcelableVersion
            val parcelableSize = source.readInt()
            val startPosition = source.dataPosition()
            val vr = OpenPgpSignatureResult(source, version)
            // skip over all fields added in future versions of this parcel
            source.setDataPosition(startPosition + parcelableSize)
            return vr
        }

        override fun newArray(size: Int): Array<OpenPgpSignatureResult?> {
            return arrayOfNulls(size)
        }

        public fun createWithValidSignature(
            signatureStatus: Int,
            primaryUserId: String?,
            keyId: Long,
            userIds: ArrayList<String>?,
            confirmedUserIds: ArrayList<String>?,
            senderStatusResult: SenderStatusResult?,
            signatureTimestamp: Date?
        ): OpenPgpSignatureResult {
            require(!(signatureStatus == RESULT_NO_SIGNATURE || signatureStatus == RESULT_KEY_MISSING || signatureStatus == RESULT_INVALID_SIGNATURE)) { "can only use this method for valid types of signatures" }
            return OpenPgpSignatureResult(
                signatureStatus, primaryUserId, keyId, userIds, confirmedUserIds,
                senderStatusResult, null, signatureTimestamp, null
            )
        }

        public fun createWithNoSignature(): OpenPgpSignatureResult {
            return OpenPgpSignatureResult(
                RESULT_NO_SIGNATURE,
                null,
                0L,
                null,
                null,
                null,
                null,
                null,
                null
            )
        }

        public fun createWithKeyMissing(keyId: Long, signatureTimestamp: Date?): OpenPgpSignatureResult {
            return OpenPgpSignatureResult(
                RESULT_KEY_MISSING,
                null,
                keyId,
                null,
                null,
                null,
                null,
                signatureTimestamp,
                null
            )
        }

        public fun createWithInvalidSignature(): OpenPgpSignatureResult {
            return OpenPgpSignatureResult(
                RESULT_INVALID_SIGNATURE,
                null,
                0L,
                null,
                null,
                null,
                null,
                null,
                null
            )
        }

        private fun <T : Enum<T>?> readEnumWithNullAndFallback(
            source: Parcel,
            enumValues: Array<T>,
            fallback: T?
        ): T? {
            val valueOrdinal = source.readInt()
            if (valueOrdinal == -1) {
                return null
            }
            return if (valueOrdinal >= enumValues.size) {
                fallback
            } else enumValues[valueOrdinal]
        }

        private fun writeEnumWithNull(dest: Parcel, enumValue: Enum<*>?) {
            if (enumValue == null) {
                dest.writeInt(-1)
                return
            }
            dest.writeInt(enumValue.ordinal)
        }
    }
}