Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
make compatible with exist 5.3.0
- change to UnsynchronizedByteArrayOutputStream
- reapply changes from previous commits that got lost in the merge
  • Loading branch information
Olaf Schreck authored and line-o committed Jun 3, 2021
commit 87241f05e9e0d9cc048e8f2d0b975f4e5d723d44
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), ExpathCryptoErrorCode.getDescription(cryptoError));
super(expr, new ExpathCryptoErrorCode(cryptoError), cryptoError.getDescription());
}

public EXpathCryptoException(Expression expr, Exception exception) {
Expand Down
14 changes: 1 addition & 13 deletions src/main/java/org/expath/exist/crypto/ExpathCryptoErrorCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,12 @@

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.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";
}
super(new QName(cryptoError.getCode(), ExistExpathCryptoModule.NAMESPACE_URI, ExistExpathCryptoModule.PREFIX), cryptoError.getDescription());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@
import org.exist.xquery.value.StringValue;
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 @@ -307,7 +306,7 @@ private InputStream getKeyStoreInputStream(final String keystoreURI) throws Cryp
return is;
}
} catch (final URISyntaxException e) {
LOG.error(ExpathCryptoErrorCode.getDescription(CryptoError.KEYSTORE_URL));
LOG.error(CryptoError.KEYSTORE_URL.getDescription());
return null;
} catch (final IOException | TransactionException e) {
throw new CryptoException(CryptoError.UNREADABLE_KEYSTORE, e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@

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 @@ -122,13 +121,11 @@ 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 {
String resultBytes = null;
byte[] resultBytes = null;

switch (encryptType) {
case SYMMETRIC:
try (final FastByteArrayInputStream is = new FastByteArrayInputStream(data)) {
resultBytes = SymmetricEncryption.encrypt(is, secretKey, algorithm, iv, provider);
}
resultBytes = SymmetricEncryption.encrypt(data, secretKey, algorithm, iv, provider);
break;

case ASYMMETRIC:
Expand All @@ -138,7 +135,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.getBytes());
String result = Base64.getEncoder().encodeToString(resultBytes);
LOG.debug("encrypt result = {}", () -> result);

return new StringValue(result);
Expand All @@ -154,13 +151,11 @@ 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 {
String resultBytes = null;
byte[] resultBytes = null;

switch (decryptType) {
case SYMMETRIC:
try (final FastByteArrayInputStream is = new FastByteArrayInputStream(data)) {
resultBytes = SymmetricEncryption.decrypt(is, secretKey, algorithm, iv, provider);
}
resultBytes = SymmetricEncryption.decrypt(data, secretKey, algorithm, iv, provider);
break;

case ASYMMETRIC:
Expand All @@ -172,7 +167,7 @@ private Sequence decrypt(byte[] data, CryptType decryptType, String secretKey, S
throw new EXpathCryptoException(this, CryptoError.DECRYPTION_TYPE);
}

String result = new String(resultBytes.getBytes(), UTF_8);
String result = new String(resultBytes, UTF_8);
LOG.debug("decrypt result = {}", () -> result);

return new StringValue(result);
Expand Down
9 changes: 5 additions & 4 deletions src/main/java/org/expath/exist/crypto/utils/Conversion.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.exist.util.io.FastByteArrayOutputStream;
import org.exist.xquery.XPathException;
import org.exist.xquery.value.BinaryValue;
import org.exist.xquery.value.IntegerValue;
Expand All @@ -18,6 +17,8 @@
import org.exist.xquery.value.Type;
import org.exist.xquery.value.ValueSequence;

import org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream;

import com.evolvedbinary.j8fu.Either;

import ro.kuberam.libs.java.crypto.utils.Buffer;
Expand All @@ -36,7 +37,7 @@ public static byte[] toByteArray(@Nullable final Either<InputStream, byte[]> dat
return data.right().get();
} else {
try (final InputStream is = data.left().get();
final FastByteArrayOutputStream baos = new FastByteArrayOutputStream()) {
final UnsynchronizedByteArrayOutputStream baos = new UnsynchronizedByteArrayOutputStream()) {

final byte[] buf = new byte[Buffer.TRANSFER_SIZE];
int read = -1;
Expand Down Expand Up @@ -77,11 +78,11 @@ public static Either<InputStream, byte[]> sequence2javaTypes(final Sequence sequ
return null;
}
} else {
final FastByteArrayOutputStream baos = new FastByteArrayOutputStream();
final UnsynchronizedByteArrayOutputStream baos = new UnsynchronizedByteArrayOutputStream();
for (final SequenceIterator iterator = sequence.iterate(); iterator.hasNext();) {
baos.write(((NumericValue) iterator.nextItem()).getInt());
}
return Either.Left(baos.toFastByteInputStream());
return Either.Left(baos.toInputStream());
}
}

Expand Down