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 16, 2022
1 parent 7232d34 commit 120feef
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 19 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,11 @@ 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))) {
return reader.lines() //
.collect(Collectors.joining()) // filter out line-delimiters
.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
@@ -0,0 +1,44 @@
/*******************************************************************************
* Copyright (c) 2022 IILS mbH
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* https://www.eclipse.org/legal/epl-2.0/.
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* IILS mbH (Hannes Wellmann) - initial API and implementation
*******************************************************************************/
package org.eclipse.passage.lic.net.tests.io;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
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;

final class KeyKeeperWithOppositeLineDelimiterFactory extends TestKeyKeeper {

@Override
public final KeyKeeper get() throws LicensingException, IOException {
Path file = SafePayloadTest.folder.newFile("key.pub").toPath(); //$NON-NLS-1$
byte[] content = publicKeyContent();
Files.write(file, content);
return new FileKeyKeeper(file);
}

private byte[] publicKeyContent() throws IOException {
return Files.lines(publicKey()) //
.collect(Collectors.joining(oppositeLineSeparator())) //
.getBytes(StandardCharsets.UTF_8);
}

private String oppositeLineSeparator() {
return "\r\n".equals(System.lineSeparator()) ? "\n" : "\r\n"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2021 ArSysOp
* Copyright (c) 2021, 2022 ArSysOp
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
Expand All @@ -9,40 +9,54 @@
*
* Contributors:
* ArSysOp - initial API and implementation
* IILS mbH (Hannes Wellmann) - Harden KeyContent against different line-delimiters
*******************************************************************************/
package org.eclipse.passage.lic.net.tests.io;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;

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.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 folder = new TemporaryFolder();

@Parameter
public TestKeyKeeper factory;

@Parameters
public static List<TestKeyKeeper> keyKeepers() {
return Arrays.asList(new SimpleKeyKeeperFactory(), new KeyKeeperWithOppositeLineDelimiterFactory());
}

@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());
KeyKeeper keeper = new SimpleKeyKeeperFactory().get();
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(this.factory.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 {
return new FileKeyKeeper(Paths.get("resource").resolve("io").resolve("key.pub")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*******************************************************************************
* Copyright (c) 2022 IILS mbH
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* https://www.eclipse.org/legal/epl-2.0/.
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* IILS mbH (Hannes Wellmann) - initial API and implementation
*******************************************************************************/
package org.eclipse.passage.lic.net.tests.io;

import java.io.IOException;

import org.eclipse.passage.lic.api.LicensingException;
import org.eclipse.passage.lic.api.io.KeyKeeper;
import org.eclipse.passage.lic.base.io.FileKeyKeeper;

final class SimpleKeyKeeperFactory extends TestKeyKeeper {

@Override
public final KeyKeeper get() throws LicensingException, IOException {
return new FileKeyKeeper(publicKey());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*******************************************************************************
* Copyright (c) 2022 IILS mbH
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* https://www.eclipse.org/legal/epl-2.0/.
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* IILS mbH (Hannes Wellmann) - initial API and implementation
*******************************************************************************/
package org.eclipse.passage.lic.net.tests.io;

import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;

import org.eclipse.passage.lic.api.LicensingException;
import org.eclipse.passage.lic.api.io.KeyKeeper;

abstract class TestKeyKeeper {

protected abstract KeyKeeper get() throws LicensingException, IOException;

protected final Path publicKey() {
return Paths.get("resource").resolve("io").resolve("key.pub"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
}
}

0 comments on commit 120feef

Please sign in to comment.