Skip to content

Commit

Permalink
Harden KeyContent against different line-delimiters
Browse files Browse the repository at this point in the history
  • Loading branch information
HannesWell committed Jul 4, 2022
1 parent 5856284 commit e30c0fe
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@
*******************************************************************************/
package org.eclipse.passage.lic.base.io;

import java.io.ByteArrayOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.stream.Collectors;

import org.eclipse.passage.lic.api.LicensingException;
import org.eclipse.passage.lic.api.io.KeyKeeper;
Expand All @@ -31,13 +34,10 @@ public KeyContent(KeyKeeper keeper) {
}

public byte[] get() throws LicensingException {
try (InputStream in = keeper.productPublicKey(); ByteArrayOutputStream out = new ByteArrayOutputStream()) {
int acquired = -1;
byte[] buffer = new byte[1024];
while ((acquired = in.read(buffer)) != -1) {
out.write(buffer, 0, acquired);
}
return out.toByteArray();
try (InputStream in = keeper.productPublicKey();
BufferedReader reader = new BufferedReader(new InputStreamReader(in, StandardCharsets.US_ASCII))) {
String content = reader.lines().collect(Collectors.joining()); // filter line-delimiters out
return content.getBytes(StandardCharsets.US_ASCII);
} catch (IOException e) {
throw new LicensingException("Failed to read public key ", e); //$NON-NLS-1$
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ Bundle-Copyright: %Bundle-Copyright
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Fragment-Host: org.eclipse.passage.lic.net
Require-Bundle: org.junit;bundle-version="4.12.0"
Import-Package: org.junit.jupiter.api.function;version="5.8.1"

Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,67 @@
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import org.eclipse.passage.lic.api.LicensingException;
import org.eclipse.passage.lic.api.io.KeyKeeper;
import org.eclipse.passage.lic.base.io.FileKeyKeeper;
import org.eclipse.passage.lic.base.io.MD5Hashes;
import org.eclipse.passage.lic.internal.net.io.SafePayload;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.jupiter.api.function.ThrowingSupplier;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameter;
import org.junit.runners.Parameterized.Parameters;

@RunWith(Parameterized.class)
public final class SafePayloadTest {
@ClassRule
public static TemporaryFolder tempFolder = new TemporaryFolder();

@Parameter
public ThrowingSupplier<KeyKeeper> keyKeeper2;

@Parameters
public static List<ThrowingSupplier<KeyKeeper>> keyKeepers() {
return Arrays.asList(SafePayloadTest::keeper, SafePayloadTest::keeperWithDifferentLineDelimiter);
}

@Test
public void symmetric() {
public void symmetric() throws Throwable {
String original = "S0me sophisticäted Str!ng"; //$NON-NLS-1$
try {
byte[] encoded = new SafePayload(keerper(), new MD5Hashes()).encode(original.getBytes());
byte[] encoded = new SafePayload(keeper(), new MD5Hashes()).encode(original.getBytes());
assertTrue(encoded.length > 0);
System.out.println(new String(encoded));
byte[] decoded = new SafePayload(keerper(), new MD5Hashes()).decode(encoded);
byte[] decoded = new SafePayload(keyKeeper2.get(), new MD5Hashes()).decode(encoded);
assertTrue(decoded.length > 0);
assertEquals(original, new String(decoded));
} catch (LicensingException e) {
fail("Not intended to fail on valid data"); //$NON-NLS-1$
}
}

private KeyKeeper keerper() throws LicensingException {
private static KeyKeeper keeper() throws LicensingException {
return new FileKeyKeeper(Paths.get("resource").resolve("io").resolve("key.pub")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
}

private static KeyKeeper keeperWithDifferentLineDelimiter() throws LicensingException, IOException {
Path file = Paths.get("resource").resolve("io").resolve("key.pub"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
String otherLineSeparator = "\r\n".equals(System.lineSeparator()) ? "\n" : "\r\n"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
String keyContent = Files.lines(file, StandardCharsets.UTF_8).collect(Collectors.joining(otherLineSeparator));
Path keyFile = tempFolder.newFile("key.pub").toPath(); //$NON-NLS-1$
Files.write(keyFile, keyContent.getBytes(StandardCharsets.UTF_8));
return new FileKeyKeeper(keyFile);
}
}

0 comments on commit e30c0fe

Please sign in to comment.