diff --git a/pom.xml b/pom.xml index 8981094..c132983 100644 --- a/pom.xml +++ b/pom.xml @@ -5,17 +5,20 @@ 4.0.0 - ro.kuberam.expath.exist - base - 1.6.0-SNAPSHOT + org.exist-db + exist-apps-parent + 1.9.0 - crypto - 0.8.0-SNAPSHOT + org.exist-db.xquery.extensions.expath + expath-crypto-module + 1.0.0-SNAPSHOT + eXist-db EXPath Cryptographic library eXist-db wrapper for EXPath Cryptographic Java library http://expath.org/spec/crypto 2016 + GNU Lesser General Public License version 2.1 @@ -24,6 +27,12 @@ + + https://github.com/exist-db/exist.git + scm:git:https://github.com/exist-db/exist.git + scm:git:https://github.com/exist-db/exist.git + + Kuberam http://kuberam.ro @@ -40,12 +49,17 @@ Adam Retter + adam@exist-db.org - 1.7.0-SNAPSHOT EXPath Cryptographic Module Implementation + 1.5 + 5.0.0-RC8 + http://expath.org/ns/crypto + crypto + ${crypto.module.ns} @@ -53,13 +67,14 @@ ro.kuberam.libs.java crypto ${crypto.java.lib.version} - provided org.exist-db exist-core + ${exist.version} provided + xml-apis xml-apis @@ -69,12 +84,14 @@ org.apache.logging.log4j log4j-api + 2.11.2 provided - + + + - - org.apache.maven.plugins - maven-enforcer-plugin - org.apache.maven.plugins maven-jar-plugin + 3.1.0 @@ -152,70 +167,35 @@ ro.kuberam.maven.plugins kuberam-expath-plugin - - - - org.apache.maven.plugins - maven-source-plugin - - - - true - true - - - ${build-tag} - ${build-commit} - ${build-commit-abbrev} - ${build-version} - ${build-tstamp} - ${project.scm.connection} - ${project.description} - ${project.url} - - - - attach-sources - verify + create-xar + package - jar + make-xar + + xar-assembly.xml + ${package-final-name} + - org.apache.maven.plugins - maven-javadoc-plugin + maven-gpg-plugin - - - true - true - - - ${build-tag} - ${build-commit} - ${build-commit-abbrev} - ${build-version} - ${build-tstamp} - ${project.scm.connection} - ${project.description} - ${project.url} - - + true + + + + org.apache.maven.plugins + maven-release-plugin + + forked-path + true + @{project.version} - - - attach-javadocs - verify - - jar - - - diff --git a/src/main/java/org/expath/exist/crypto/EXpathCryptoException.java b/src/main/java/org/expath/exist/crypto/EXpathCryptoException.java index 5b940b0..3be3ab7 100644 --- a/src/main/java/org/expath/exist/crypto/EXpathCryptoException.java +++ b/src/main/java/org/expath/exist/crypto/EXpathCryptoException.java @@ -30,7 +30,7 @@ public class EXpathCryptoException extends XPathException { private static final long serialVersionUID = -6789727720893604433L; public EXpathCryptoException(Expression expr, CryptoError cryptoError) { - super(expr, new ExpathCryptoErrorCode(cryptoError), cryptoError.getDescription()); + super(expr, new ExpathCryptoErrorCode(cryptoError), ExpathCryptoErrorCode.getDescription(cryptoError)); } public EXpathCryptoException(Expression expr, Exception exception) { diff --git a/src/main/java/org/expath/exist/crypto/ExpathCryptoErrorCode.java b/src/main/java/org/expath/exist/crypto/ExpathCryptoErrorCode.java index 6eddf21..a21fe0d 100644 --- a/src/main/java/org/expath/exist/crypto/ExpathCryptoErrorCode.java +++ b/src/main/java/org/expath/exist/crypto/ExpathCryptoErrorCode.java @@ -5,12 +5,24 @@ import ro.kuberam.libs.java.crypto.CryptoError; +import java.lang.reflect.Field; + public class ExpathCryptoErrorCode extends ErrorCode { public ExpathCryptoErrorCode(String code, String description) { super(new QName(code, ExistExpathCryptoModule.NAMESPACE_URI, ExistExpathCryptoModule.PREFIX), description); } public ExpathCryptoErrorCode(CryptoError cryptoError) { - super(new QName(cryptoError.getCode(), ExistExpathCryptoModule.NAMESPACE_URI, ExistExpathCryptoModule.PREFIX), cryptoError.getDescription()); + super(new QName(cryptoError.name(), ExistExpathCryptoModule.NAMESPACE_URI, ExistExpathCryptoModule.PREFIX), getDescription(cryptoError)); + } + + public static String getDescription(final CryptoError cryptoError) { + try { + final Field field = cryptoError.getClass().getDeclaredField("description"); + field.setAccessible(true); + return (String) field.get(cryptoError); + } catch (final NoSuchFieldException | IllegalAccessException e) { + return "UNKNOWN"; + } } } diff --git a/src/main/java/org/expath/exist/crypto/digitalSignature/GenerateSignatureFunction.java b/src/main/java/org/expath/exist/crypto/digitalSignature/GenerateSignatureFunction.java index 0846c0a..261e227 100644 --- a/src/main/java/org/expath/exist/crypto/digitalSignature/GenerateSignatureFunction.java +++ b/src/main/java/org/expath/exist/crypto/digitalSignature/GenerateSignatureFunction.java @@ -36,7 +36,7 @@ import org.exist.Namespaces; import org.exist.dom.memtree.SAXAdapter; import org.exist.dom.persistent.BinaryDocument; -import org.exist.dom.persistent.DocumentImpl; +import org.exist.dom.persistent.LockedDocument; import org.exist.security.PermissionDeniedException; import org.exist.storage.lock.Lock; import org.exist.storage.serializers.Serializer; @@ -51,6 +51,7 @@ import org.exist.xquery.value.Sequence; import org.exist.xquery.value.Type; import org.expath.exist.crypto.EXpathCryptoException; +import org.expath.exist.crypto.ExpathCryptoErrorCode; import org.w3c.dom.Document; import org.w3c.dom.Node; import org.w3c.dom.NodeList; @@ -129,7 +130,7 @@ public Sequence eval(final Sequence[] args, final Sequence contextSequence) thro NodeValue inputNode = (NodeValue) args[0].itemAt(0); Document inputDOMDoc; - try (InputStream inputNodeStream = new NodeInputStream(serializer, inputNode)) { + try (InputStream inputNodeStream = new NodeInputStream(context.getBroker().getBrokerPool(), serializer, inputNode)) { inputDOMDoc = inputStreamToDocument(inputNodeStream); } catch (IOException e) { throw new EXpathCryptoException(this, e); @@ -244,15 +245,12 @@ private String[] getDigitalCertificateDetails(final String[] certificateDetails, private InputStream getKeyStoreInputStream(final String keystoreURI) throws CryptoException { // get the keystore as InputStream try { - DocumentImpl keyStoreDoc = null; - try { - keyStoreDoc = context.getBroker().getXMLResource(XmldbURI.xmldbUriFor(keystoreURI), - Lock.LockMode.READ_LOCK); - if (keyStoreDoc == null) { + try(final LockedDocument lockedKeyStoreDoc = context.getBroker().getXMLResource(XmldbURI.xmldbUriFor(keystoreURI), Lock.LockMode.READ_LOCK)) { + if (lockedKeyStoreDoc == null) { throw new CryptoException(CryptoError.UNREADABLE_KEYSTORE); } - final BinaryDocument keyStoreBinaryDoc = (BinaryDocument) keyStoreDoc; + final BinaryDocument keyStoreBinaryDoc = (BinaryDocument) lockedKeyStoreDoc.getDocument(); try { return context.getBroker().getBinaryResource(keyStoreBinaryDoc); } catch (final IOException e) { @@ -260,13 +258,11 @@ private InputStream getKeyStoreInputStream(final String keystoreURI) throws Cryp } } catch (final PermissionDeniedException e) { - LOG.error(CryptoError.DENIED_KEYSTORE.getDescription()); + LOG.error(ExpathCryptoErrorCode.getDescription(CryptoError.DENIED_KEYSTORE)); return null; - } finally { - keyStoreDoc.getUpdateLock().release(Lock.LockMode.READ_LOCK); } } catch (final URISyntaxException e) { - LOG.error(CryptoError.KEYSTORE_URL.getDescription()); + LOG.error(ExpathCryptoErrorCode.getDescription(CryptoError.KEYSTORE_URL)); return null; } } diff --git a/src/main/java/org/expath/exist/crypto/encrypt/EncryptionFunctions.java b/src/main/java/org/expath/exist/crypto/encrypt/EncryptionFunctions.java index e62028c..872abaf 100644 --- a/src/main/java/org/expath/exist/crypto/encrypt/EncryptionFunctions.java +++ b/src/main/java/org/expath/exist/crypto/encrypt/EncryptionFunctions.java @@ -33,6 +33,7 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; +import org.exist.util.io.FastByteArrayInputStream; import org.exist.xquery.BasicFunction; import org.exist.xquery.FunctionSignature; import org.exist.xquery.XPathException; @@ -121,11 +122,13 @@ public Sequence eval(final Sequence[] args, final Sequence contextSequence) thro private Sequence encrypt(byte[] data, CryptType encryptType, String secretKey, String algorithm, @Nullable String iv, @Nullable String provider) throws XPathException { try { - byte[] resultBytes = null; + String resultBytes = null; switch (encryptType) { case SYMMETRIC: - resultBytes = SymmetricEncryption.encrypt(data, secretKey, algorithm, iv, provider); + try (final FastByteArrayInputStream is = new FastByteArrayInputStream(data)) { + resultBytes = SymmetricEncryption.encrypt(is, secretKey, algorithm, iv, provider); + } break; case ASYMMETRIC: @@ -135,7 +138,7 @@ private Sequence encrypt(byte[] data, CryptType encryptType, String secretKey, S default: throw new EXpathCryptoException(this, CryptoError.ENCRYPTION_TYPE); } - String result = Base64.getEncoder().encodeToString(resultBytes); + String result = Base64.getEncoder().encodeToString(resultBytes.getBytes()); LOG.debug("encrypt result = {}", () -> result); return new StringValue(result); @@ -151,11 +154,13 @@ private Sequence encrypt(byte[] data, CryptType encryptType, String secretKey, S private Sequence decrypt(byte[] data, CryptType decryptType, String secretKey, String algorithm, @Nullable String iv, @Nullable String provider) throws XPathException { try { - byte[] resultBytes = null; + String resultBytes = null; switch (decryptType) { case SYMMETRIC: - resultBytes = SymmetricEncryption.decrypt(data, secretKey, algorithm, iv, provider); + try (final FastByteArrayInputStream is = new FastByteArrayInputStream(data)) { + resultBytes = SymmetricEncryption.decrypt(is, secretKey, algorithm, iv, provider); + } break; case ASYMMETRIC: @@ -167,7 +172,7 @@ private Sequence decrypt(byte[] data, CryptType decryptType, String secretKey, S throw new EXpathCryptoException(this, CryptoError.DECRYPTION_TYPE); } - String result = new String(resultBytes, UTF_8); + String result = new String(resultBytes.getBytes(), UTF_8); LOG.debug("decrypt result = {}", () -> result); return new StringValue(result); diff --git a/build/xar-assembly.xml b/xar-assembly.xml similarity index 79% rename from build/xar-assembly.xml rename to xar-assembly.xml index 8969d12..1bbe1be 100644 --- a/build/xar-assembly.xml +++ b/xar-assembly.xml @@ -1,6 +1,6 @@ - - ${package.title} + ${package-title} Claudius Teodorescu ${project.url} GNU LGPL v2.1 @@ -13,26 +13,24 @@ exist Libraries eXist extensions - + ${project.groupId} ${project.artifactId} ${project.version} - ${project.artifactId}-exist-java-lib-${project.version}.jar - + ${project.artifactId}-exist-java-lib-${project.version}.jar ro.kuberam.libs.java crypto ${crypto.java.lib.version} - crypto-java-lib-${crypto.java.lib.version}.jar - + crypto-java-lib-${crypto.java.lib.version}.jar - http://expath.org/ns/crypto + ${crypto.module.ns} urn:java:class:org.expath.exist.crypto.ExistExpathCryptoModule