From b5b9305043a1f57bf0c73f0ac28ed906cb7ded32 Mon Sep 17 00:00:00 2001 From: alperozturk Date: Mon, 24 Feb 2025 11:53:34 +0100 Subject: [PATCH 1/6] add SignatureAlgorithm Signed-off-by: alperozturk --- .../lib/resources/e2ee/SendCSRRemoteOperationIT.kt | 2 +- .../android/lib/resources/e2ee/CsrHelper.kt | 14 +++++++++----- .../lib/resources/e2ee/SignatureAlgorithm.kt | 13 +++++++++++++ 3 files changed, 23 insertions(+), 6 deletions(-) create mode 100644 library/src/main/java/com/owncloud/android/lib/resources/e2ee/SignatureAlgorithm.kt diff --git a/library/src/androidTest/java/com/owncloud/android/lib/resources/e2ee/SendCSRRemoteOperationIT.kt b/library/src/androidTest/java/com/owncloud/android/lib/resources/e2ee/SendCSRRemoteOperationIT.kt index 1f62d9e5a2..dedf42ac88 100644 --- a/library/src/androidTest/java/com/owncloud/android/lib/resources/e2ee/SendCSRRemoteOperationIT.kt +++ b/library/src/androidTest/java/com/owncloud/android/lib/resources/e2ee/SendCSRRemoteOperationIT.kt @@ -35,7 +35,7 @@ class SendCSRRemoteOperationIT : AbstractIT() { val keyPair = keyGen.genKeyPair() // create CSR - val urlEncoded: String = CsrHelper().generateCsrPemEncodedString(keyPair, client.userId) + val urlEncoded: String = CsrHelper().generateCsrPemEncodedString(keyPair, client.userId, SignatureAlgorithm.SHA1) val operation = SendCSRRemoteOperation(urlEncoded) var result = operation.execute(nextcloudClient) diff --git a/library/src/main/java/com/owncloud/android/lib/resources/e2ee/CsrHelper.kt b/library/src/main/java/com/owncloud/android/lib/resources/e2ee/CsrHelper.kt index 979f20e6b0..710533af53 100644 --- a/library/src/main/java/com/owncloud/android/lib/resources/e2ee/CsrHelper.kt +++ b/library/src/main/java/com/owncloud/android/lib/resources/e2ee/CsrHelper.kt @@ -38,6 +38,7 @@ class CsrHelper { * * @param keyPair the KeyPair with private and public keys * @param userId userId of CSR owner + * @param algorithm represents supported signature algorithm * @return PEM encoded CSR string * @throws IOException thrown if key cannot be created * @throws OperatorCreationException thrown if contentSigner cannot be build @@ -45,9 +46,10 @@ class CsrHelper { @Throws(IOException::class, OperatorCreationException::class) fun generateCsrPemEncodedString( keyPair: KeyPair, - userId: String + userId: String, + algorithm: SignatureAlgorithm ): String { - val csr = generateCSR(keyPair, userId) + val csr = generateCSR(keyPair, userId, algorithm) val derCSR = csr.encoded return "-----BEGIN CERTIFICATE REQUEST-----\n" + Base64.encodeToString( @@ -61,6 +63,7 @@ class CsrHelper { * * @param keyPair the KeyPair with private and public keys * @param userId userId of CSR owner + * @param algorithm represents supported signature algorithm * @return PKCS10CertificationRequest with the certificate signing request (CSR) data * @throws IOException thrown if key cannot be created * @throws OperatorCreationException thrown if contentSigner cannot be build @@ -69,12 +72,13 @@ class CsrHelper { @Throws(IOException::class, OperatorCreationException::class) private fun generateCSR( keyPair: KeyPair, - userId: String + userId: String, + algorithm: SignatureAlgorithm ): PKCS10CertificationRequest { val principal = "CN=$userId" val privateKey = PrivateKeyFactory.createKey(keyPair.private.encoded) - val signatureAlgorithm = DefaultSignatureAlgorithmIdentifierFinder().find("SHA1WITHRSA") - val digestAlgorithm = DefaultDigestAlgorithmIdentifierFinder().find("SHA-1") + val signatureAlgorithm = DefaultSignatureAlgorithmIdentifierFinder().find(algorithm.signatureAlg) + val digestAlgorithm = DefaultDigestAlgorithmIdentifierFinder().find(algorithm.digestAlg) val signer = BcRSAContentSignerBuilder(signatureAlgorithm, digestAlgorithm).build(privateKey) val csrBuilder: PKCS10CertificationRequestBuilder = diff --git a/library/src/main/java/com/owncloud/android/lib/resources/e2ee/SignatureAlgorithm.kt b/library/src/main/java/com/owncloud/android/lib/resources/e2ee/SignatureAlgorithm.kt new file mode 100644 index 0000000000..363a5a63e0 --- /dev/null +++ b/library/src/main/java/com/owncloud/android/lib/resources/e2ee/SignatureAlgorithm.kt @@ -0,0 +1,13 @@ +/* + * Nextcloud Android Library + * + * SPDX-FileCopyrightText: 2025 Alper Ozturk + * SPDX-License-Identifier: MIT + */ + +package com.owncloud.android.lib.resources.e2ee + +enum class SignatureAlgorithm(val signatureAlg: String, val digestAlg: String) { + SHA1("SHA1WITHRSA", "SHA-1"), + SHA256("SHA256WITHRSA", "SHA-256") +} From ef00a8945116dd7d9eee3ef18ae674107ad1b231 Mon Sep 17 00:00:00 2001 From: alperozturk Date: Thu, 6 Mar 2025 11:35:29 +0100 Subject: [PATCH 2/6] SignatureAlgorithm.SHA256 Signed-off-by: alperozturk --- .../androidTest/java/com/owncloud/android/AbstractIT.java | 5 ----- .../android/lib/resources/e2ee/SendCSRRemoteOperationIT.kt | 3 ++- .../android/lib/resources/e2ee/SignatureAlgorithm.kt | 6 ++++-- 3 files changed, 6 insertions(+), 8 deletions(-) diff --git a/library/src/androidTest/java/com/owncloud/android/AbstractIT.java b/library/src/androidTest/java/com/owncloud/android/AbstractIT.java index 8e91093a2b..ac31b209d3 100644 --- a/library/src/androidTest/java/com/owncloud/android/AbstractIT.java +++ b/library/src/androidTest/java/com/owncloud/android/AbstractIT.java @@ -127,9 +127,6 @@ private static void waitForServer(OwnCloudClient client, Uri baseUrl) { i++; } - if (i == 3) { - fail("Server not ready!"); - } } catch (IOException e) { e.printStackTrace(); @@ -260,8 +257,6 @@ public static File extractAsset(String fileName, Context context) throws IOExcep @After public void after() { - removeOnClient(client); - removeOnClient(client2); } private void removeOnClient(OwnCloudClient client) { diff --git a/library/src/androidTest/java/com/owncloud/android/lib/resources/e2ee/SendCSRRemoteOperationIT.kt b/library/src/androidTest/java/com/owncloud/android/lib/resources/e2ee/SendCSRRemoteOperationIT.kt index dedf42ac88..1ca49f326a 100644 --- a/library/src/androidTest/java/com/owncloud/android/lib/resources/e2ee/SendCSRRemoteOperationIT.kt +++ b/library/src/androidTest/java/com/owncloud/android/lib/resources/e2ee/SendCSRRemoteOperationIT.kt @@ -35,7 +35,8 @@ class SendCSRRemoteOperationIT : AbstractIT() { val keyPair = keyGen.genKeyPair() // create CSR - val urlEncoded: String = CsrHelper().generateCsrPemEncodedString(keyPair, client.userId, SignatureAlgorithm.SHA1) + val urlEncoded: String = + CsrHelper().generateCsrPemEncodedString(keyPair, client.userId, SignatureAlgorithm.SHA256) val operation = SendCSRRemoteOperation(urlEncoded) var result = operation.execute(nextcloudClient) diff --git a/library/src/main/java/com/owncloud/android/lib/resources/e2ee/SignatureAlgorithm.kt b/library/src/main/java/com/owncloud/android/lib/resources/e2ee/SignatureAlgorithm.kt index 363a5a63e0..efe6f023cf 100644 --- a/library/src/main/java/com/owncloud/android/lib/resources/e2ee/SignatureAlgorithm.kt +++ b/library/src/main/java/com/owncloud/android/lib/resources/e2ee/SignatureAlgorithm.kt @@ -7,7 +7,9 @@ package com.owncloud.android.lib.resources.e2ee -enum class SignatureAlgorithm(val signatureAlg: String, val digestAlg: String) { - SHA1("SHA1WITHRSA", "SHA-1"), +enum class SignatureAlgorithm( + val signatureAlg: String, + val digestAlg: String +) { SHA256("SHA256WITHRSA", "SHA-256") } From 483a100e7e078b159a82b7706bba6e10c00a8478 Mon Sep 17 00:00:00 2001 From: alperozturk Date: Thu, 6 Mar 2025 11:37:43 +0100 Subject: [PATCH 3/6] revert Signed-off-by: alperozturk --- .../androidTest/java/com/owncloud/android/AbstractIT.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/library/src/androidTest/java/com/owncloud/android/AbstractIT.java b/library/src/androidTest/java/com/owncloud/android/AbstractIT.java index ac31b209d3..8e91093a2b 100644 --- a/library/src/androidTest/java/com/owncloud/android/AbstractIT.java +++ b/library/src/androidTest/java/com/owncloud/android/AbstractIT.java @@ -127,6 +127,9 @@ private static void waitForServer(OwnCloudClient client, Uri baseUrl) { i++; } + if (i == 3) { + fail("Server not ready!"); + } } catch (IOException e) { e.printStackTrace(); @@ -257,6 +260,8 @@ public static File extractAsset(String fileName, Context context) throws IOExcep @After public void after() { + removeOnClient(client); + removeOnClient(client2); } private void removeOnClient(OwnCloudClient client) { From 2297602d05bef034731880b08c7abf828af43ba1 Mon Sep 17 00:00:00 2001 From: alperozturk Date: Thu, 6 Mar 2025 12:25:55 +0100 Subject: [PATCH 4/6] update SignatureAlgorithm Signed-off-by: alperozturk --- .../android/lib/resources/e2ee/CsrHelper.kt | 13 +++++-------- .../lib/resources/e2ee/SignatureAlgorithm.kt | 2 +- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/library/src/main/java/com/owncloud/android/lib/resources/e2ee/CsrHelper.kt b/library/src/main/java/com/owncloud/android/lib/resources/e2ee/CsrHelper.kt index 710533af53..c3ad810df7 100644 --- a/library/src/main/java/com/owncloud/android/lib/resources/e2ee/CsrHelper.kt +++ b/library/src/main/java/com/owncloud/android/lib/resources/e2ee/CsrHelper.kt @@ -38,7 +38,6 @@ class CsrHelper { * * @param keyPair the KeyPair with private and public keys * @param userId userId of CSR owner - * @param algorithm represents supported signature algorithm * @return PEM encoded CSR string * @throws IOException thrown if key cannot be created * @throws OperatorCreationException thrown if contentSigner cannot be build @@ -46,10 +45,9 @@ class CsrHelper { @Throws(IOException::class, OperatorCreationException::class) fun generateCsrPemEncodedString( keyPair: KeyPair, - userId: String, - algorithm: SignatureAlgorithm + userId: String ): String { - val csr = generateCSR(keyPair, userId, algorithm) + val csr = generateCSR(keyPair, userId) val derCSR = csr.encoded return "-----BEGIN CERTIFICATE REQUEST-----\n" + Base64.encodeToString( @@ -63,7 +61,6 @@ class CsrHelper { * * @param keyPair the KeyPair with private and public keys * @param userId userId of CSR owner - * @param algorithm represents supported signature algorithm * @return PKCS10CertificationRequest with the certificate signing request (CSR) data * @throws IOException thrown if key cannot be created * @throws OperatorCreationException thrown if contentSigner cannot be build @@ -73,12 +70,12 @@ class CsrHelper { private fun generateCSR( keyPair: KeyPair, userId: String, - algorithm: SignatureAlgorithm ): PKCS10CertificationRequest { val principal = "CN=$userId" + val privateKey = PrivateKeyFactory.createKey(keyPair.private.encoded) - val signatureAlgorithm = DefaultSignatureAlgorithmIdentifierFinder().find(algorithm.signatureAlg) - val digestAlgorithm = DefaultDigestAlgorithmIdentifierFinder().find(algorithm.digestAlg) + val signatureAlgorithm = DefaultSignatureAlgorithmIdentifierFinder().find(SignatureAlgorithm.SHA256.signatureAlg) + val digestAlgorithm = DefaultDigestAlgorithmIdentifierFinder().find(SignatureAlgorithm.SHA256.digestAlg) val signer = BcRSAContentSignerBuilder(signatureAlgorithm, digestAlgorithm).build(privateKey) val csrBuilder: PKCS10CertificationRequestBuilder = diff --git a/library/src/main/java/com/owncloud/android/lib/resources/e2ee/SignatureAlgorithm.kt b/library/src/main/java/com/owncloud/android/lib/resources/e2ee/SignatureAlgorithm.kt index efe6f023cf..e08fbb6937 100644 --- a/library/src/main/java/com/owncloud/android/lib/resources/e2ee/SignatureAlgorithm.kt +++ b/library/src/main/java/com/owncloud/android/lib/resources/e2ee/SignatureAlgorithm.kt @@ -11,5 +11,5 @@ enum class SignatureAlgorithm( val signatureAlg: String, val digestAlg: String ) { - SHA256("SHA256WITHRSA", "SHA-256") + SHA256("SHA256WITHRSAENCRYPTION", "SHA-256") } From d9a065c5f2c93e92e759b0b40edce3b8dbaa0856 Mon Sep 17 00:00:00 2001 From: alperozturk Date: Thu, 6 Mar 2025 12:40:27 +0100 Subject: [PATCH 5/6] revert Signed-off-by: alperozturk --- .../android/lib/resources/e2ee/SendCSRRemoteOperationIT.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/src/androidTest/java/com/owncloud/android/lib/resources/e2ee/SendCSRRemoteOperationIT.kt b/library/src/androidTest/java/com/owncloud/android/lib/resources/e2ee/SendCSRRemoteOperationIT.kt index 1ca49f326a..fa0747b79c 100644 --- a/library/src/androidTest/java/com/owncloud/android/lib/resources/e2ee/SendCSRRemoteOperationIT.kt +++ b/library/src/androidTest/java/com/owncloud/android/lib/resources/e2ee/SendCSRRemoteOperationIT.kt @@ -36,7 +36,7 @@ class SendCSRRemoteOperationIT : AbstractIT() { // create CSR val urlEncoded: String = - CsrHelper().generateCsrPemEncodedString(keyPair, client.userId, SignatureAlgorithm.SHA256) + CsrHelper().generateCsrPemEncodedString(keyPair, client.userId) val operation = SendCSRRemoteOperation(urlEncoded) var result = operation.execute(nextcloudClient) From ab1d31693648417a9400f0df359b9971da4bfba0 Mon Sep 17 00:00:00 2001 From: alperozturk Date: Thu, 6 Mar 2025 13:53:07 +0100 Subject: [PATCH 6/6] fix code analytics Signed-off-by: alperozturk --- .../owncloud/android/lib/resources/e2ee/CsrHelper.kt | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/library/src/main/java/com/owncloud/android/lib/resources/e2ee/CsrHelper.kt b/library/src/main/java/com/owncloud/android/lib/resources/e2ee/CsrHelper.kt index c3ad810df7..ac82a9c493 100644 --- a/library/src/main/java/com/owncloud/android/lib/resources/e2ee/CsrHelper.kt +++ b/library/src/main/java/com/owncloud/android/lib/resources/e2ee/CsrHelper.kt @@ -69,15 +69,17 @@ class CsrHelper { @Throws(IOException::class, OperatorCreationException::class) private fun generateCSR( keyPair: KeyPair, - userId: String, + userId: String ): PKCS10CertificationRequest { val principal = "CN=$userId" val privateKey = PrivateKeyFactory.createKey(keyPair.private.encoded) - val signatureAlgorithm = DefaultSignatureAlgorithmIdentifierFinder().find(SignatureAlgorithm.SHA256.signatureAlg) - val digestAlgorithm = DefaultDigestAlgorithmIdentifierFinder().find(SignatureAlgorithm.SHA256.digestAlg) + val signatureAlgorithm = SignatureAlgorithm.SHA256 + val signatureAlgorithmIdentifier = + DefaultSignatureAlgorithmIdentifierFinder().find(signatureAlgorithm.signatureAlg) + val digestAlgorithmIdentifier = DefaultDigestAlgorithmIdentifierFinder().find(signatureAlgorithm.digestAlg) val signer = - BcRSAContentSignerBuilder(signatureAlgorithm, digestAlgorithm).build(privateKey) + BcRSAContentSignerBuilder(signatureAlgorithmIdentifier, digestAlgorithmIdentifier).build(privateKey) val csrBuilder: PKCS10CertificationRequestBuilder = JcaPKCS10CertificationRequestBuilder( X500Name(principal),