From f89767ffe16e5a91f570e66b3d82df6907776202 Mon Sep 17 00:00:00 2001 From: Thomas Burnett Date: Tue, 26 Sep 2023 14:45:14 +0200 Subject: [PATCH] Flytter kode for prosesseringssteg til fellesmodul --- .../ebms/processing/PayloadProcessor.kt | 13 ++-- .../no/nav/emottak/util/SertifikatUtil.kt | 7 +- .../kotlin/no/nav/emottak/util/XMLUtil.kt | 8 +-- .../util/crypto/EncryptionException.kt | 4 ++ .../no/nav/emottak/util/crypto}/Kryptering.kt | 30 ++------ .../util/signatur}/SignaturVerifisering.kt | 29 ++------ .../util/signatur/SignatureException.kt | 4 ++ .../nav/emottak/util/signatur}/Signering.kt | 45 ++++-------- .../no/nav/emottak/melding/Processor.kt | 71 +++++++++++++++++-- .../no/nav/emottak/melding/model/Melding.kt | 13 ---- .../{melding/process => util}/GZipUtil.kt | 24 +------ .../melding/process/DekrypteringTest.kt | 7 +- .../emottak/melding/process/GZipUtilTest.kt | 8 ++- .../emottak/melding/process/KrypteringTest.kt | 6 +- .../emottak/melding/process/SigneringTest.kt | 4 +- 15 files changed, 124 insertions(+), 149 deletions(-) rename {payload-processor => felles}/src/main/kotlin/no/nav/emottak/util/SertifikatUtil.kt (68%) rename {payload-processor => felles}/src/main/kotlin/no/nav/emottak/util/XMLUtil.kt (89%) create mode 100644 felles/src/main/kotlin/no/nav/emottak/util/crypto/EncryptionException.kt rename {payload-processor/src/main/kotlin/no/nav/emottak/melding/process => felles/src/main/kotlin/no/nav/emottak/util/crypto}/Kryptering.kt (70%) rename {payload-processor/src/main/kotlin/no/nav/emottak/melding/process => felles/src/main/kotlin/no/nav/emottak/util/signatur}/SignaturVerifisering.kt (50%) create mode 100644 felles/src/main/kotlin/no/nav/emottak/util/signatur/SignatureException.kt rename {payload-processor/src/main/kotlin/no/nav/emottak/melding/process => felles/src/main/kotlin/no/nav/emottak/util/signatur}/Signering.kt (72%) rename payload-processor/src/main/kotlin/no/nav/emottak/{melding/process => util}/GZipUtil.kt (75%) diff --git a/ebms-provider/src/main/kotlin/no/nav/emottak/ebms/processing/PayloadProcessor.kt b/ebms-provider/src/main/kotlin/no/nav/emottak/ebms/processing/PayloadProcessor.kt index 6a1fb305..54c08a0a 100644 --- a/ebms-provider/src/main/kotlin/no/nav/emottak/ebms/processing/PayloadProcessor.kt +++ b/ebms-provider/src/main/kotlin/no/nav/emottak/ebms/processing/PayloadProcessor.kt @@ -1,5 +1,6 @@ package no.nav.emottak.ebms.processing +import io.ktor.server.plugins.BadRequestException import no.nav.emottak.ebms.model.EbMSMessage import no.nav.emottak.ebms.postPayloadRequest import no.nav.emottak.melding.model.Header @@ -26,22 +27,22 @@ class PayloadProcessor: Processor { fun MessageHeader.payloadRequestHeader(): Header { return Header( - messageId = this.id ?: throw RuntimeException("MessageID mangler fra header"), - cpaId = this.cpaId ?: throw RuntimeException("CPAID mangler fra header"), + messageId = this.id ?: throw BadRequestException("MessageID mangler fra header"), + cpaId = this.cpaId ?: throw BadRequestException("CPAID mangler fra header"), conversationId = this.conversationId, to = Party( herID = this.to.partyId.herID(), - role = this.to.role ?: throw RuntimeException("Melding mangler role for en eller flere parter") + role = this.to.role ?: throw BadRequestException("Melding mangler role for en eller flere parter") ), from = Party( herID = this.from.partyId.herID(), - role = this.from.role ?: throw RuntimeException("Melding mangler role for en eller flere parter") + role = this.from.role ?: throw BadRequestException("Melding mangler role for en eller flere parter") ), - service = this.service.value ?: throw RuntimeException("Service mangler fra header"), + service = this.service.value ?: throw BadRequestException("Service mangler fra header"), action = this.action ) } fun List.herID(): String { - return this.firstOrNull() { partyId -> partyId.type == "HER" }?.value ?: throw RuntimeException("Melding mangler HER-ID for en eller flere parter") + return this.firstOrNull() { partyId -> partyId.type == "HER" }?.value ?: throw BadRequestException("Melding mangler HER-ID for en eller flere parter") } diff --git a/payload-processor/src/main/kotlin/no/nav/emottak/util/SertifikatUtil.kt b/felles/src/main/kotlin/no/nav/emottak/util/SertifikatUtil.kt similarity index 68% rename from payload-processor/src/main/kotlin/no/nav/emottak/util/SertifikatUtil.kt rename to felles/src/main/kotlin/no/nav/emottak/util/SertifikatUtil.kt index 955b927e..9a80e0ad 100644 --- a/payload-processor/src/main/kotlin/no/nav/emottak/util/SertifikatUtil.kt +++ b/felles/src/main/kotlin/no/nav/emottak/util/SertifikatUtil.kt @@ -1,20 +1,19 @@ package no.nav.emottak.util -import io.ktor.server.plugins.BadRequestException import java.io.ByteArrayInputStream import java.security.cert.CertificateException import java.security.cert.CertificateFactory import java.security.cert.X509Certificate -internal fun isSelfSigned(certificate: X509Certificate) = +fun isSelfSigned(certificate: X509Certificate) = certificate.subjectX500Principal == certificate.issuerX500Principal -internal fun createX509Certificate(byteArray: ByteArray): X509Certificate { +fun createX509Certificate(byteArray: ByteArray): X509Certificate { val cf = CertificateFactory.getInstance("X.509") return try { cf.generateCertificate(ByteArrayInputStream(byteArray)) as X509Certificate } catch (e: CertificateException) { - throw BadRequestException("") + throw RuntimeException("Kunne ikke opprette X509Certificate fra ByteArray", e) } } \ No newline at end of file diff --git a/payload-processor/src/main/kotlin/no/nav/emottak/util/XMLUtil.kt b/felles/src/main/kotlin/no/nav/emottak/util/XMLUtil.kt similarity index 89% rename from payload-processor/src/main/kotlin/no/nav/emottak/util/XMLUtil.kt rename to felles/src/main/kotlin/no/nav/emottak/util/XMLUtil.kt index 4e4f9f42..043c9d28 100644 --- a/payload-processor/src/main/kotlin/no/nav/emottak/util/XMLUtil.kt +++ b/felles/src/main/kotlin/no/nav/emottak/util/XMLUtil.kt @@ -1,7 +1,7 @@ package no.nav.emottak.util -import io.ktor.server.plugins.BadRequestException import no.nav.emottak.util.signatur.KeyValueKeySelector +import no.nav.emottak.util.signatur.SignatureException import org.w3c.dom.Document import org.w3c.dom.Node import org.w3c.dom.NodeList @@ -31,7 +31,7 @@ internal fun retrieveXMLSignature(validateContext: DOMValidateContext): XMLSigna private fun retrieveSignatureElement(document: Document): Node { val nodeList: NodeList = document.getElementsByTagNameNS(XMLSignature.XMLNS, "Signature") - return nodeList.item(0) ?: throw BadRequestException("MANGLER_XML_SIGNATUR") + return nodeList.item(0) ?: throw SignatureException("Mangler xmldsig") } internal fun retrievePublicCertificateFromSignature(document: Document): X509Certificate { @@ -46,13 +46,13 @@ private fun retrievePublicCertificateFromSignature(signature: XMLSignature): X50 } -internal fun createDocument(inputstream: InputStream): Document { +fun createDocument(inputstream: InputStream): Document { val dbf = DocumentBuilderFactory.newInstance() dbf.isNamespaceAware = true return dbf.newDocumentBuilder().parse(inputstream) } -internal fun getByteArrayFromDocument(doc: Document): ByteArray { +fun getByteArrayFromDocument(doc: Document): ByteArray { val outputStream = ByteArrayOutputStream() val xmlSource = DOMSource(doc) val result = StreamResult(outputStream) diff --git a/felles/src/main/kotlin/no/nav/emottak/util/crypto/EncryptionException.kt b/felles/src/main/kotlin/no/nav/emottak/util/crypto/EncryptionException.kt new file mode 100644 index 00000000..c128b03a --- /dev/null +++ b/felles/src/main/kotlin/no/nav/emottak/util/crypto/EncryptionException.kt @@ -0,0 +1,4 @@ +package no.nav.emottak.util.crypto + +class EncryptionException(override val message: String, e: Exception? = null) : Exception(message, e) { +} \ No newline at end of file diff --git a/payload-processor/src/main/kotlin/no/nav/emottak/melding/process/Kryptering.kt b/felles/src/main/kotlin/no/nav/emottak/util/crypto/Kryptering.kt similarity index 70% rename from payload-processor/src/main/kotlin/no/nav/emottak/melding/process/Kryptering.kt rename to felles/src/main/kotlin/no/nav/emottak/util/crypto/Kryptering.kt index 7b87ff20..93c49fdf 100644 --- a/payload-processor/src/main/kotlin/no/nav/emottak/melding/process/Kryptering.kt +++ b/felles/src/main/kotlin/no/nav/emottak/util/crypto/Kryptering.kt @@ -1,10 +1,6 @@ -package no.nav.emottak.melding.process +package no.nav.emottak.util.crypto -import io.ktor.server.plugins.BadRequestException -import no.nav.emottak.melding.model.Header -import no.nav.emottak.melding.model.Melding import no.nav.emottak.util.createX509Certificate -import no.nav.emottak.util.hentKrypteringssertifikat import org.bouncycastle.asn1.ASN1ObjectIdentifier import org.bouncycastle.cms.CMSAlgorithm import org.bouncycastle.cms.CMSEnvelopedDataGenerator @@ -17,30 +13,14 @@ import org.bouncycastle.jce.provider.BouncyCastleProvider import java.security.cert.CertificateEncodingException import java.security.cert.X509Certificate -private val kryptering = Kryptering() - -fun krypter(byteArray: ByteArray, sertifikat: ByteArray) = kryptering.krypter(byteArray, sertifikat) - -fun Melding.krypter(): Melding { - return this.copy( - processedPayload = kryptering.krypter(this.processedPayload, this.header), - kryptert = true - ) -} - class Kryptering { - fun krypter(byteArray: ByteArray, header: Header): ByteArray { - val krypteringSertifikat = hentKrypteringssertifikat(header.cpaId, header.to.herID) - return krypter(byteArray, krypteringSertifikat) - } fun krypter(byteArray: ByteArray, krypteringSertifikat: ByteArray): ByteArray { if (byteArray.isEmpty()) { - throw BadRequestException("Meldingen er tom.") + throw EncryptionException("Meldingen er tom.") } val sertifikat = createX509Certificate(krypteringSertifikat) return krypterDokument(byteArray, sertifikat) - } } @@ -55,7 +35,7 @@ private fun krypterDokument(doc: ByteArray, certificate: X509Certificate): ByteA return try { krypterDokument(doc, listOf(certificate)) } catch (e: Exception) { - throw e + throw EncryptionException("Feil ved kryptering av dokument", e) } } @@ -75,8 +55,8 @@ private fun krypterDokument(input: ByteArray, certificates: List