-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCryptoContainer.kt
More file actions
607 lines (542 loc) · 23.4 KB
/
CryptoContainer.kt
File metadata and controls
607 lines (542 loc) · 23.4 KB
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
/*
* Copyright 2017 - 2025 Riigi Infosüsteemi Amet
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
@file:Suppress("PackageName")
package ee.ria.DigiDoc.cryptolib
import android.content.Context
import dagger.hilt.android.qualifiers.ApplicationContext
import ee.ria.DigiDoc.common.Constant.CDOC1_EXTENSION
import ee.ria.DigiDoc.common.Constant.CDOC2_EXTENSION
import ee.ria.DigiDoc.common.Constant.CONTAINER_MIME_TYPE
import ee.ria.DigiDoc.common.container.Container
import ee.ria.DigiDoc.configuration.provider.ConfigurationProvider
import ee.ria.DigiDoc.configuration.repository.ConfigurationRepository
import ee.ria.DigiDoc.cryptolib.exception.ContainerDataFilesEmptyException
import ee.ria.DigiDoc.cryptolib.exception.CryptoException
import ee.ria.DigiDoc.cryptolib.exception.DataFilesEmptyException
import ee.ria.DigiDoc.cryptolib.exception.RecipientsEmptyException
import ee.ria.DigiDoc.idcard.Token
import ee.ria.DigiDoc.network.utils.ProxyUtil
import ee.ria.DigiDoc.smartcardreader.SmartCardReaderException
import ee.ria.DigiDoc.utilsLib.container.ContainerUtil
import ee.ria.DigiDoc.utilsLib.extensions.isCryptoContainer
import ee.ria.DigiDoc.utilsLib.extensions.saveAs
import ee.ria.DigiDoc.utilsLib.file.FileUtil.sanitizeString
import ee.ria.DigiDoc.utilsLib.logging.LoggingUtil.Companion.debugLog
import ee.ria.DigiDoc.utilsLib.logging.LoggingUtil.Companion.errorLog
import ee.ria.cdoc.CDoc
import ee.ria.cdoc.CDocException
import ee.ria.cdoc.CDocReader
import ee.ria.cdoc.CDocWriter
import ee.ria.cdoc.CertificateList
import ee.ria.cdoc.Configuration
import ee.ria.cdoc.CryptoBackend
import ee.ria.cdoc.DataBuffer
import ee.ria.cdoc.FileInfo
import ee.ria.cdoc.ILogger
import ee.ria.cdoc.Lock
import ee.ria.cdoc.NetworkBackend
import ee.ria.cdoc.Recipient
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.withContext
import org.apache.commons.io.FilenameUtils
import org.openeid.cdoc4j.CDOCParser
import java.io.File
import java.io.FileInputStream
import java.io.FileOutputStream
import java.io.IOException
import java.io.InputStream
import java.io.OutputStream
import java.util.Base64
import javax.inject.Inject
import javax.inject.Singleton
private const val LOG_TAG = "CryptoContainer"
@Singleton
class CryptoContainer
@Inject
constructor(
val context: Context,
var file: File,
val dataFiles: ArrayList<File>,
val recipients: ArrayList<Addressee>,
val decrypted: Boolean,
val encrypted: Boolean,
) : Container {
fun containerMimetype(): String = CONTAINER_MIME_TYPE
fun getName(): String = file.name
suspend fun setName(filename: String) {
val name = sanitizeString(filename, "")
val containerName = ContainerUtil.addExtensionToContainerFilename(name)
val newFile = File(file.parent, containerName)
withContext(IO) {
file.renameTo(newFile)
file = newFile
}
}
fun addDataFiles(filesToAdd: List<File>) {
dataFiles.addAll(filesToAdd)
}
fun addRecipients(recipientsToAdd: List<Addressee>) {
recipients.addAll(recipientsToAdd)
}
fun getDataFiles(): List<File> =
dataFiles
.toList()
fun getRecipients(): List<Addressee> =
recipients
.toList()
fun hasRecipients(): Boolean = recipients.isNotEmpty()
@Throws(Exception::class)
fun getDataFile(
dataFile: File,
directory: File,
): File {
val tmp = sanitizeString(dataFile.name, "")
val file = File(directory, tmp)
for (containerDataFile in dataFiles) {
if (dataFile.name == containerDataFile.name) {
if (!file.exists()) {
containerDataFile.saveAs(file.absolutePath)
}
return file
}
}
throw IllegalArgumentException("Could not find file ${dataFile.name} in container ${file.name}")
}
@Throws(Exception::class)
suspend fun removeDataFile(dataFile: File) {
if (dataFiles.size == 1) {
throw ContainerDataFilesEmptyException()
}
withContext(IO) {
val index = dataFiles.indexOfFirst { it.name == dataFile.name }
if (index != -1) {
dataFiles.removeAt(index)
}
}
}
@Throws(Exception::class)
suspend fun removeRecipient(recipient: Addressee) {
if (recipients.isNotEmpty()) {
withContext(IO) {
val index = recipients.indexOfFirst { it.identifier == recipient.identifier }
if (index != -1) {
recipients.removeAt(index)
}
}
}
}
companion object {
val logger = JavaLogger()
var loggingIsSet = false
@Throws(CryptoException::class)
private suspend fun open(
context: Context,
file: File,
): CryptoContainer {
val dataFiles = ArrayList<File>()
var recipients = ArrayList<Addressee>()
if (file.extension == CDOC1_EXTENSION) {
val cdoc1Container = openCDOC1(context, file)
dataFiles.addAll(cdoc1Container.getDataFiles())
recipients.addAll(cdoc1Container.getRecipients())
}
val addressees = ArrayList<Addressee>()
val cdocReader = CDocReader.createReader(file.path, null, null, null)
debugLog(LOG_TAG, "Reader created: (version ${cdocReader.version})")
withContext(IO) {
cdocReader.locks.forEach { lock ->
if (lock.isCertificate) {
var concatKDFAlgorithmURI = ""
if (!lock.isRSA) {
concatKDFAlgorithmURI = lock.getString(Lock.Params.CONCAT_DIGEST)
}
addressees.add(
Addressee(lock.label, lock.getBytes(Lock.Params.CERT), concatKDFAlgorithmURI),
)
} else if (lock.isPKI) {
addressees.add(
Addressee(lock.label, lock.getBytes(Lock.Params.RCPT_KEY), ""),
)
} else if (lock.isSymmetric) {
addressees.add(
Addressee(lock.label, "", CertType.UnknownType, null, ByteArray(0)),
)
} else {
addressees.add(Addressee("Unknown capsule", ByteArray(0), ""))
}
}
cdocReader.delete()
}
if (!recipients.isEmpty()) {
addressees.forEach { addressee ->
recipients.forEach { recipient ->
if (addressee.data.contentEquals(recipient.data)) {
recipient.concatKDFAlgorithmURI = addressee.concatKDFAlgorithmURI
}
}
}
} else {
recipients = addressees
}
return create(
context,
file,
dataFiles,
recipients,
decrypted = false,
encrypted = true,
)
}
@Throws(CryptoException::class)
suspend fun openCDOC1(
context: Context,
file: File,
): CryptoContainer {
val dataFiles = ArrayList<File>()
val recipients = ArrayList<Addressee>()
withContext(IO) {
try {
FileInputStream(file).use { dataFilesStream ->
CDOCParser.getDataFileNames(dataFilesStream).forEach { dataFileName ->
dataFiles.add(File(dataFileName))
}
}
FileInputStream(file).use { recipientsStream ->
CDOCParser.getRecipients(recipientsStream).forEach { recipient ->
val addressee = Addressee(recipient.certificate.encoded)
recipients.add(addressee)
}
}
} catch (e: Exception) {
throw CryptoException("Can't open crypto container", e)
}
}
return create(
context,
file,
dataFiles,
recipients,
decrypted = false,
encrypted = true,
)
}
@Throws(CryptoException::class, SmartCardReaderException::class)
fun decrypt(
context: Context,
file: File,
recipients: List<Addressee>,
authCert: ByteArray?,
pin: ByteArray,
smartToken: Token,
cdoc2Settings: CDOC2Settings,
configurationRepository: ConfigurationRepository,
): CryptoContainer {
val token = SmartCardTokenWrapper(pin, smartToken)
val conf = CryptoLibConf(cdoc2Settings)
val configurationProvider = configurationRepository.getConfiguration()
if (authCert == null || authCert.isEmpty()) {
throw CryptoException("Failed to get auth certificate")
}
val network = CryptoLibNetworkBackend(cdoc2Settings, configurationProvider, context, authCert, token)
val dataFiles = ArrayList<File>()
val cdocReader = CDocReader.createReader(file.path, conf, token, network)
debugLog(LOG_TAG, "Reader created: (version ${cdocReader.version})")
val idx = cdocReader.getLockForCert(authCert)
if (idx < 0) {
throw CryptoException("Failed to get lock for certificate")
}
val fmk = cdocReader.getFMK(idx.toInt())
if (token.lastError != null) {
throw token.lastError as Throwable
}
if (fmk.isEmpty()) {
throw CryptoException("Failed to get FMK")
}
if (cdocReader.beginDecryption(fmk) != 0L) {
throw CryptoException("Failed to begin decryption")
}
val fi = FileInfo()
var result: Long = cdocReader.nextFile(fi)
try {
while (result == CDoc.OK.toLong()) {
val ofile = File(fi.name)
val dir =
ContainerUtil.getContainerDataFilesDir(
context,
file,
)
val tmp = sanitizeString(ofile.name, "")
val fileToSave = File(dir, tmp)
val ofs: OutputStream = FileOutputStream(fileToSave)
cdocReader.readFile(ofs)
dataFiles.add(fileToSave)
ofs.close()
result = cdocReader.nextFile(fi)
}
} catch (exc: IOException) {
throw CryptoException("IO Exception: ${exc.message}", exc)
}
if (cdocReader.finishDecryption() != 0L) {
throw CryptoException("Failed to finish decryption")
}
return create(
context,
file,
dataFiles,
recipients,
decrypted = true,
encrypted = false,
)
}
@Throws(CryptoException::class)
suspend fun encrypt(
context: Context,
file: File,
dataFiles: List<File>,
recipients: List<Addressee>,
cdoc2Settings: CDOC2Settings,
configurationRepository: ConfigurationRepository,
): CryptoContainer {
if (dataFiles.isEmpty()) {
throw DataFilesEmptyException("Cannot create an empty crypto container")
}
if (recipients.isEmpty()) {
throw RecipientsEmptyException("Cannot create crypto container without recipients")
}
val configurationProvider = configurationRepository.getConfiguration()
val conf = CryptoLibConf(cdoc2Settings)
val network = Network(cdoc2Settings, configurationProvider, context)
val version =
if (file.extension == CDOC2_EXTENSION) {
2
} else {
1
}
val cdocWriter = CDocWriter.createWriter(version, file.path, conf, null, network)
try {
withContext(IO) {
if (version == 2 && cdoc2Settings.getUseOnlineEncryption()) {
val serverId = cdoc2Settings.getCDOC2UUID()
recipients.forEach { addressee ->
val recipient = Recipient.makeServer("", addressee.data, serverId)
if (cdocWriter.addRecipient(recipient) != 0L) {
throw CryptoException("Failed to add recipient")
}
}
} else {
recipients.forEach { addressee ->
val recipient = Recipient.makeCertificate("", addressee.data)
if (cdocWriter.addRecipient(recipient) != 0L) {
throw CryptoException("Failed to add recipient")
}
}
}
}
if (cdocWriter.beginEncryption() != 0L) {
throw CryptoException("Failed to begin encryption")
}
withContext(IO) {
dataFiles.forEach { dataFile ->
val ifs: InputStream = FileInputStream(dataFile)
val bytes = ifs.readBytes()
if (cdocWriter.addFile(dataFile.name, bytes.size.toLong()) != 0L) {
throw CryptoException("Failed to add file")
}
if (cdocWriter.writeData(bytes) != 0L) {
throw CryptoException("Failed to write data")
}
ifs.close()
}
}
if (cdocWriter.finishEncryption() != 0L) {
throw CryptoException("Failed to finish encryption")
}
} catch (exc: IOException) {
errorLog(LOG_TAG, "IO Exception: ${exc.message}", exc)
throw CryptoException("IO Exception: ${exc.message}", exc)
} catch (exc: CDocException) {
errorLog(LOG_TAG, "CDoc Exception ${exc.code}: ${exc.message}", exc)
throw CryptoException("CDoc Exception ${exc.code}: ${exc.message}", exc)
} finally {
cdocWriter.delete()
}
return open(context, file)
}
@Throws(CryptoException::class)
private fun create(
context: Context,
file: File,
dataFiles: List<File>,
recipients: List<Addressee>,
decrypted: Boolean,
encrypted: Boolean,
): CryptoContainer =
CryptoContainer(
context,
file,
ArrayList(dataFiles),
ArrayList(recipients),
decrypted,
encrypted,
)
@Throws(Exception::class)
suspend fun openOrCreate(
@ApplicationContext context: Context,
file: File,
dataFiles: List<File>,
cdoc2Settings: CDOC2Settings,
forceCreate: Boolean = false,
): CryptoContainer {
if (dataFiles.isEmpty()) {
throw DataFilesEmptyException("Cannot create an empty crypto container")
}
val isFirstDataFileContainer =
dataFiles.firstOrNull()?.run {
isCryptoContainer()
} == true
var containerFileWithExtension = file
if (!isFirstDataFileContainer) {
val defaultExtension =
if (cdoc2Settings.getUseEncryption()) {
CDOC2_EXTENSION
} else {
CDOC1_EXTENSION
}
containerFileWithExtension =
File(
FilenameUtils.removeExtension(file.path) + ".$defaultExtension",
)
containerFileWithExtension.parentFile?.mkdirs()
if (!containerFileWithExtension.exists()) {
containerFileWithExtension.createNewFile()
}
}
return if (!forceCreate && dataFiles.size == 1 && isFirstDataFileContainer) {
open(context, containerFileWithExtension)
} else {
create(
context,
containerFileWithExtension,
dataFiles,
listOf(),
decrypted = false,
encrypted = false,
)
}
}
fun setLogging(isLoggingEnabled: Boolean) {
if (isLoggingEnabled) {
logger.SetMinLogLevel(ILogger.LogLevel.LEVEL_TRACE)
if (!loggingIsSet) {
ILogger.addLogger(logger)
loggingIsSet = true
}
val lgr = ILogger.getLogger()
lgr.LogMessage(
ILogger.LogLevel.LEVEL_DEBUG,
"CryptoContainer",
450,
"Set libcdoc logging: true",
)
}
}
class JavaLogger : ILogger() {
override fun LogMessage(
level: LogLevel?,
file: String?,
line: Int,
message: String?,
) {
debugLog("CryptoContainer", String.format("%s:%s %s %s\n", file, line, level, message))
}
}
private class CryptoLibConf(
private val cdoc2Settings: CDOC2Settings,
) : Configuration() {
override fun getValue(
domain: String,
param: String,
): String =
when (param) {
KEYSERVER_FETCH_URL -> cdoc2Settings.getCDOC2FetchURL(domain)
KEYSERVER_SEND_URL -> cdoc2Settings.getCDOC2PostURL(domain)
else -> ""
}
}
private open class Network(
private val cdoc2Settings: CDOC2Settings,
private val configurationProvider: ConfigurationProvider?,
private val context: Context,
) : NetworkBackend() {
override fun getPeerTLSCertificates(
dst: CertificateList?,
url: String?,
): Long {
val certs = configurationProvider?.certBundle ?: listOf()
dst?.clear()
for (cert in certs) {
val certBytes = Base64.getDecoder().decode(cert)
dst?.addCertificate(certBytes)
}
val certFromSettings = cdoc2Settings.getCDOC2Cert()
if (certFromSettings != null) {
val certBytes =
Base64.getDecoder().decode(certFromSettings)
dst?.addCertificate(certBytes)
}
return CDoc.OK.toLong()
}
override fun getProxyCredentials(cred: ProxyCredentials?): Long {
val proxyValues =
ProxyUtil.getProxyValues(
ProxyUtil.getProxySetting(context),
ProxyUtil.getManualProxySettings(context),
)
if (proxyValues != null) {
cred?.host = proxyValues.host
cred?.port = proxyValues.port
cred?.username = proxyValues.username
cred?.password = proxyValues.password
}
return CDoc.OK.toLong()
}
}
private class CryptoLibNetworkBackend(
cdoc2Settings: CDOC2Settings,
configurationProvider: ConfigurationProvider?,
context: Context,
private var cert: ByteArray,
private var token: SmartCardTokenWrapper,
) : Network(cdoc2Settings, configurationProvider, context) {
override fun getClientTLSCertificate(dst: DataBuffer?): Long {
dst?.data = cert
return if (dst == null || dst.data == null || dst.data.isEmpty()) {
CDoc.IO_ERROR.toLong()
} else {
CDoc.OK.toLong()
}
}
override fun signTLS(
dst: DataBuffer,
algorithm: CryptoBackend.HashAlgorithm,
digest: ByteArray,
): Long = token.sign(dst, algorithm, digest, 0)
}
}
}