Skip to content
Merged
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
114 changes: 47 additions & 67 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,20 @@
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>ro.kuberam.expath.exist</groupId>
<artifactId>base</artifactId>
<version>1.6.0-SNAPSHOT</version>
<groupId>org.exist-db</groupId>
<artifactId>exist-apps-parent</artifactId>
<version>1.9.0</version>
</parent>

<artifactId>crypto</artifactId>
<version>0.8.0-SNAPSHOT</version>
<groupId>org.exist-db.xquery.extensions.expath</groupId>
<artifactId>expath-crypto-module</artifactId>
<version>1.0.0-SNAPSHOT</version>

<name>eXist-db EXPath Cryptographic library</name>
<description>eXist-db wrapper for EXPath Cryptographic Java library</description>
<url>http://expath.org/spec/crypto</url>
<inceptionYear>2016</inceptionYear>

<licenses>
<license>
<name>GNU Lesser General Public License version 2.1</name>
Expand All @@ -24,6 +27,12 @@
</license>
</licenses>

<scm>
<url>https://github.com/exist-db/exist.git</url>
<connection>scm:git:https://github.com/exist-db/exist.git</connection>
<developerConnection>scm:git:https://github.com/exist-db/exist.git</developerConnection>
</scm>

<organization>
<name>Kuberam</name>
<url>http://kuberam.ro</url>
Expand All @@ -40,26 +49,32 @@
<contributors>
<contributor>
<name>Adam Retter</name>
<email>[email protected]</email>
</contributor>
</contributors>

<properties>
<crypto.java.lib.version>1.7.0-SNAPSHOT</crypto.java.lib.version>
<package.title>EXPath Cryptographic Module Implementation</package.title>
<crypto.java.lib.version>1.5</crypto.java.lib.version>
<exist.version>5.0.0-RC8</exist.version>
<crypto.module.ns>http://expath.org/ns/crypto</crypto.module.ns>
<package-abbrev>crypto</package-abbrev>
<package-name>${crypto.module.ns}</package-name>
</properties>

<dependencies>
<dependency>
<groupId>ro.kuberam.libs.java</groupId>
<artifactId>crypto</artifactId>
<version>${crypto.java.lib.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.exist-db</groupId>
<artifactId>exist-core</artifactId>
<version>${exist.version}</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>xml-apis</groupId>
<artifactId>xml-apis</artifactId>
Expand All @@ -69,31 +84,31 @@
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.11.2</version>
<scope>provided</scope>
</dependency>
<dependency>

<!-- dependency>
<groupId>com.evolvedbinary.j8fu</groupId>
<artifactId>j8fu</artifactId>
<version>1.23.0</version>
<version>1.22</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
<version>3.0.2</version>
<scope>provided</scope>
</dependency>
</dependency -->

</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<archive>
<manifest>
Expand Down Expand Up @@ -152,70 +167,35 @@
<plugin>
<groupId>ro.kuberam.maven.plugins</groupId>
<artifactId>kuberam-expath-plugin</artifactId>
</plugin>
<plugin>
<!-- Attach source jars -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
</manifest>
<manifestEntries>
<Build-Tag>${build-tag}</Build-Tag>
<Git-Commit>${build-commit}</Git-Commit>
<Git-Commit-Abbrev>${build-commit-abbrev}</Git-Commit-Abbrev>
<Build-Version>${build-version}</Build-Version>
<Build-Timestamp>${build-tstamp}</Build-Timestamp>
<Source-Repository>${project.scm.connection}</Source-Repository>
<Description>${project.description}</Description>
<Implementation-URL>${project.url}</Implementation-URL>
</manifestEntries>
</archive>
</configuration>
<executions>
<execution>
<id>attach-sources</id>
<phase>verify</phase>
<id>create-xar</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
<goal>make-xar</goal>
</goals>
<configuration>
<descriptor>xar-assembly.xml</descriptor>
<finalName>${package-final-name}</finalName>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<!-- Attach javadoc jars -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<artifactId>maven-gpg-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
</manifest>
<manifestEntries>
<Build-Tag>${build-tag}</Build-Tag>
<Git-Commit>${build-commit}</Git-Commit>
<Git-Commit-Abbrev>${build-commit-abbrev}</Git-Commit-Abbrev>
<Build-Version>${build-version}</Build-Version>
<Build-Timestamp>${build-tstamp}</Build-Timestamp>
<Source-Repository>${project.scm.connection}</Source-Repository>
<Description>${project.description}</Description>
<Implementation-URL>${project.url}</Implementation-URL>
</manifestEntries>
</archive>
<useAgent>true</useAgent>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<configuration>
<mavenExecutorId>forked-path</mavenExecutorId> <!-- avoid a bug with GPG plugin hanging http://jira.codehaus.org/browse/MGPG-9 -->
<autoVersionSubmodules>true</autoVersionSubmodules>
<tagNameFormat>@{project.version}</tagNameFormat>
</configuration>
<executions>
<execution>
<id>attach-javadocs</id>
<phase>verify</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
14 changes: 13 additions & 1 deletion src/main/java/org/expath/exist/crypto/ExpathCryptoErrorCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -244,29 +245,24 @@ 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) {
throw new CryptoException(CryptoError.UNREADABLE_KEYSTORE, e);
}

} 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;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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:
Expand All @@ -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);
Expand All @@ -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:
Expand All @@ -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);
Expand Down
14 changes: 6 additions & 8 deletions build/xar-assembly.xml → xar-assembly.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<package xmlns="http://expath.org/ns/pkg" name="${package.name}" abbrev="${package.abbrev}"
<package xmlns="http://expath.org/ns/pkg" name="${package-name}" abbrev="${package-abbrev}"
version="${project.version}" spec="1.0">
<title>${package.title}</title>
<title>${package-title}</title>
<author id="cteodorescu">Claudius Teodorescu</author>
<website>${project.url}</website>
<license>GNU LGPL v2.1</license>
Expand All @@ -13,26 +13,24 @@
<tag>exist</tag>
<category id="libs">Libraries</category>
<category id="exist">eXist extensions</category>
<dependency processor="http://exist-db.org" semver-min="4.4.0" />
<dependency processor="http://exist-db.org" semver-min="${exist.version}" />
<dependencySets>
<dependencySet>
<groupId>${project.groupId}</groupId>
<artifactId>${project.artifactId}</artifactId>
<version>${project.version}</version>
<outputFileNameMapping>${project.artifactId}-exist-java-lib-${project.version}.jar
</outputFileNameMapping>
<outputFileNameMapping>${project.artifactId}-exist-java-lib-${project.version}.jar</outputFileNameMapping>
</dependencySet>
<dependencySet>
<groupId>ro.kuberam.libs.java</groupId>
<artifactId>crypto</artifactId>
<version>${crypto.java.lib.version}</version>
<outputFileNameMapping>crypto-java-lib-${crypto.java.lib.version}.jar
</outputFileNameMapping>
<outputFileNameMapping>crypto-java-lib-${crypto.java.lib.version}.jar</outputFileNameMapping>
</dependencySet>
</dependencySets>
<components>
<resource>
<public-uri>http://expath.org/ns/crypto</public-uri>
<public-uri>${crypto.module.ns}</public-uri>
<file>urn:java:class:org.expath.exist.crypto.ExistExpathCryptoModule</file>
</resource>
</components>
Expand Down