Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,8 @@ class EncryptRecipientViewModel
_errorState.postValue(R.string.crypto_encrypt_data_files_empty_error)
} catch (_: RecipientsEmptyException) {
_errorState.postValue(R.string.crypto_encrypt_recipients_empty_error)
} catch (_: Exception) {
} catch (e: Exception) {
errorLog(logTag, "Unable to encrypt container", e)
_errorState.postValue(R.string.crypto_encrypt_error)
}
} else {
Expand Down
2 changes: 0 additions & 2 deletions crypto-lib/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,7 @@ dependencies {
implementation(libs.bouncy.castle)
api(libs.guava)
implementation(libs.unboundid.ldapsdk)
implementation(libs.cdoc4j)
implementation(libs.preferencex)
implementation(libs.stax.api)

testImplementation(libs.junit)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,9 @@ class CryptoContainerTest {

assertNotNull(result)
assertEquals(containerCDOC1.name, result.name)
assertEquals(1, cryptoContainer.getDataFiles().size)
assertEquals("soe_30-04-2025_uus-sadama-16-3.jpeg", cryptoContainer.getDataFiles().first().name)
assertEquals(1, cryptoContainer.getRecipients().size)
}

@Test
Expand Down Expand Up @@ -443,6 +446,7 @@ class CryptoContainerTest {

assertNotNull(result)
assertEquals(containerRIACDOC1.name, result.name)
assertEquals(3, cryptoContainer.getRecipients().size)
}

@Test
Expand Down
15 changes: 11 additions & 4 deletions crypto-lib/src/main/kotlin/ee/ria/DigiDoc/cryptolib/Addressee.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

package ee.ria.DigiDoc.cryptolib

import ee.ria.DigiDoc.utilsLib.logging.LoggingUtil.Companion.errorLog
import ee.ria.cdoc.Recipient.parseLabel
import org.bouncycastle.asn1.ASN1InputStream
import org.bouncycastle.asn1.ASN1OctetString
Expand All @@ -35,6 +36,8 @@ import java.security.cert.CertificateFactory
import java.security.cert.X509Certificate
import java.util.Date

private const val LOG_TAG = "Addressee"

class Addressee(
var data: ByteArray,
var identifier: String,
Expand Down Expand Up @@ -144,7 +147,8 @@ class Addressee(
} else {
""
}
} catch (_: Exception) {
} catch (e: Exception) {
errorLog(LOG_TAG, "Unable to extract CN from certificate", e)
""
}

Expand All @@ -169,7 +173,8 @@ class Addressee(
} else {
""
}
} catch (_: Exception) {
} catch (e: Exception) {
errorLog(LOG_TAG, "Unable to extract serial number from certificate", e)
""
}

Expand Down Expand Up @@ -197,7 +202,8 @@ class Addressee(
}
}
CertType.UnknownType
} catch (_: Exception) {
} catch (e: Exception) {
errorLog(LOG_TAG, "Unable to extract certificate type", e)
CertType.UnknownType
}
}
Expand All @@ -209,7 +215,8 @@ class Addressee(
.getInstance("X.509")
.generateCertificate(cert.inputStream()) as X509Certificate
certificate.notAfter
} catch (_: Exception) {
} catch (e: Exception) {
errorLog(LOG_TAG, "Unable to extract validTo from certificate", e)
null
}
}
Expand Down
81 changes: 81 additions & 0 deletions crypto-lib/src/main/kotlin/ee/ria/DigiDoc/cryptolib/Cdoc1Parser.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Copyright 2017 - 2026 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.util.Xml
import ee.ria.DigiDoc.utilsLib.logging.LoggingUtil.Companion.debugLog
import ee.ria.DigiDoc.utilsLib.logging.LoggingUtil.Companion.errorLog
import org.xmlpull.v1.XmlPullParser
import java.io.InputStream
import java.util.Base64

private const val LOG_TAG = "Cdoc1Parser"
private const val X509_CERTIFICATE = "X509Certificate"
private const val ENCRYPTION_PROPERTY = "EncryptionProperty"
private const val NAME_ATTRIBUTE = "Name"
private const val ORIG_FILE = "orig_file"

data class Cdoc1Content(
val dataFileNames: List<String>,
val recipientCertificates: List<ByteArray>,
)

object Cdoc1Parser {
fun parse(inputStream: InputStream): Cdoc1Content {
debugLog(LOG_TAG, "Parsing CDOC1 XML stream")
val parser = Xml.newPullParser().apply { setInput(inputStream, null) }
val dataFileNames = mutableListOf<String>()
val recipientCertificates = mutableListOf<ByteArray>()
while (parser.next() != XmlPullParser.END_DOCUMENT) {
if (parser.eventType != XmlPullParser.START_TAG) {
continue
}
when (parser.localName) {
X509_CERTIFICATE -> certificateOf(parser.nextText())?.let(recipientCertificates::add)
ENCRYPTION_PROPERTY ->
if (parser.isOrigFile()) {
fileNameOf(parser.nextText())?.let(dataFileNames::add)
}
}
}
debugLog(
LOG_TAG,
"Parsed CDOC1: ${dataFileNames.size} data file name(s), " +
"${recipientCertificates.size} recipient certificate(s)",
)
return Cdoc1Content(dataFileNames, recipientCertificates)
}
}

private val XmlPullParser.localName: String
get() = name.substringAfterLast(':')

private fun XmlPullParser.isOrigFile(): Boolean = getAttributeValue(null, NAME_ATTRIBUTE) == ORIG_FILE

private fun fileNameOf(origFileProperty: String): String? =
origFileProperty.substringBefore('|').trim().ifEmpty { null }

private fun certificateOf(base64: String): ByteArray? =
runCatching { Base64.getMimeDecoder().decode(base64) }
.onFailure { errorLog(LOG_TAG, "Unable to decode recipient certificate", it) }
.getOrNull()
?.takeIf { it.isNotEmpty() }
Loading
Loading