Skip to content

Commit

Permalink
Merge pull request #275 from eclipse-passage/564819
Browse files Browse the repository at this point in the history
Bug 564819 - API revision | conditions | stream codecs review
  • Loading branch information
eparovyshnaya authored Jul 2, 2020
2 parents 4ebd01a + ef50662 commit 05db8f9
Show file tree
Hide file tree
Showing 20 changed files with 571 additions and 9 deletions.
1 change: 1 addition & 0 deletions bundles/org.eclipse.passage.lic.api/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Export-Package: org.eclipse.passage.lic.api,
org.eclipse.passage.lic.base",
org.eclipse.passage.lic.internal.api.conditions;x-friends:="org.eclipse.passage.lic.base",
org.eclipse.passage.lic.internal.api.conditions.mining;x-internal:=true,
org.eclipse.passage.lic.internal.api.io;x-internal:=true,
org.eclipse.passage.lic.internal.api.registry;x-friends:="org.eclipse.passage.lic.base",
org.eclipse.passage.lic.internal.api.requirements;x-friends:="org.eclipse.passage.lic.base",
org.eclipse.passage.lic.internal.api.restrictions;x-friends:="org.eclipse.passage.lic.base",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@

/**
* Coder and decoder for licensing data, used by {@link ConditionMiner}
*
*
* @deprecated use internal.StreamCodec
* @since 0.4.0
*/
@Deprecated
public interface StreamCodec {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
*******************************************************************************/
package org.eclipse.passage.lic.internal.api;

public interface LicensedProduct {
import org.eclipse.passage.lic.internal.api.registry.ServiceId;

public interface LicensedProduct extends ServiceId {

String identifier();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*******************************************************************************
* Copyright (c) 2020 ArSysOp
*
* 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:
* ArSysOp - initial API and implementation
*******************************************************************************/
package org.eclipse.passage.lic.internal.api.io;

public interface DigestExpectation {

/**
* @return is there is any expectation at all
*/
boolean expected();

/**
* <p>
* In the case we actually expect input digest to be verified, here actual
* digest expectation is supplied.
* </p>
* <p>
* Is not designed to be called if there are no expectations at all. Thus, is
* not designed to ever return {@code null}.
* </p>
*/
byte[] value();

public static final class None implements DigestExpectation {

@Override
public boolean expected() {
return false;
}

@Override
public byte[] value() {
throw new UnsupportedOperationException();
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*******************************************************************************
* Copyright (c) 2020 ArSysOp
*
* 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:
* ArSysOp - initial API and implementation
*******************************************************************************/
package org.eclipse.passage.lic.internal.api.io;

import java.util.Objects;

public abstract class EncryptionAlgorithm {

private final String name;

public EncryptionAlgorithm(String name) {
Objects.requireNonNull(name, "EncryptionAlgorithm::name"); //$NON-NLS-1$
this.name = name.toUpperCase();
}

@Override
public boolean equals(Object object) {
if (!getClass().isInstance(object)) {
return false;
}
return name.equals(((EncryptionAlgorithm) object).name());
}

public String name() {
return name;
}

@Override
public int hashCode() {
return name.hashCode();
}

@Override
public String toString() {
return name;
}

public static final class RSA extends EncryptionAlgorithm {

public RSA() {
super("RSA"); //$NON-NLS-1$
}

}

public static final class Default extends EncryptionAlgorithm {

public Default() {
super(new RSA().name());
}

}

public static final class Of extends EncryptionAlgorithm {

public Of(String name) {
super(name);
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*******************************************************************************
* Copyright (c) 2020 ArSysOp
*
* 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:
* ArSysOp - initial API and implementation
*******************************************************************************/
package org.eclipse.passage.lic.internal.api.io;

import java.util.Objects;

public abstract class EncryptionKeySize {

private final int size;

public EncryptionKeySize(int size) {
if (size <= 0) {
throw new IllegalArgumentException("EncryptionKeySize::size must be positiv"); //$NON-NLS-1$
}
this.size = size;
}

@Override
public boolean equals(Object object) {
if (!getClass().isInstance(object)) {
return false;
}
return size == ((EncryptionKeySize) object).size();
}

public int size() {
return size;
}

@Override
public int hashCode() {
return Objects.hash(size);
}

@Override
public String toString() {
return Integer.toString(size);
}

public static final class Default extends EncryptionKeySize {

public Default() {
super(1024);
}

}

public static final class Of extends EncryptionKeySize {

public Of(int size) {
super(size);
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*******************************************************************************
* Copyright (c) 2020 ArSysOp
*
* 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:
* ArSysOp - initial API and implementation
*******************************************************************************/
package org.eclipse.passage.lic.internal.api.io;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.eclipse.passage.lic.internal.api.LicensedProduct;
import org.eclipse.passage.lic.internal.api.conditions.mining.MinedConditions;
import org.eclipse.passage.lic.internal.api.registry.Service;

/**
* Coder and decoder for licensing data, used by {@link MinedConditions} to read
* licensing conditions data from encoded streams (like license text)
*/
public interface StreamCodec extends Service<LicensedProduct> {
/**
* Identifier of an encoding algorithm used by the codec.
*/
EncryptionAlgorithm algorithm();

/**
* Encoding key size.
*/
EncryptionKeySize keySize();

/**
* Create a public/private keys pair and store them to {@code publicKeyPath} and
* {@code privateKeyPath} respectively.
*
* @param publicKeyPath file system path for <i>public key</i> to be generated
* @param privateKeyPath file system path for <i>private key</i> to be generated
* @param username of the keys owner user
* @param password of the keys owner user
* @throws IOException in case of any i/o misbehavior
*/
void createKeyPair(String publicKeyPath, String privateKeyPath, String username, String password)
throws IOException;

/**
* Encode {@code input} stream data with a private key retrieved form the given
* {@code key} stream. Fill {@code output} stream with the encoded data.
*
* @param input source of data to be encoded
* @param output target stream to place encoded data into
* @param key source for a private key
* @param username of the private key owner user
* @param password of the private key owner user
* @throws IOException in case of any i/o misbehavior
*/
void encode(InputStream input, OutputStream output, InputStream key, String username, String password)
throws IOException;

/**
* Decode the {@code input} stream with the <i>public key</i> and store decoded
* data to {@code output} stream.
*
* @param input source stream with encoded data
* @param output target stream for decoded data
* @param key stream containing the <i>public key</i> for decoding
* @param digest expected digest for public key source stream {@code key} to be
* validated prior decoding
* @throws IOException in case of any i/o denial or misbehavior
*/
void decode(InputStream input, OutputStream output, InputStream key, DigestExpectation digest) throws IOException;

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@
import java.util.Locale;

import org.eclipse.passage.lic.internal.api.conditions.mining.ContentType;
import org.eclipse.passage.lic.internal.api.io.EncryptionAlgorithm;
import org.eclipse.passage.lic.internal.api.io.EncryptionKeySize;
import org.eclipse.passage.lic.internal.base.conditions.mining.LicensingContentType;
import org.eclipse.passage.lic.internal.base.conditions.mining.SecurityKeyAlgorithm;
import org.eclipse.passage.lic.internal.base.conditions.mining.SecurityKeySize;
import org.eclipse.passage.lic.internal.base.i18n.BaseMessages;

@SuppressWarnings("restriction")
Expand Down Expand Up @@ -116,10 +120,25 @@ public final class LicensingProperties {
@Deprecated
public static final String LICENSING_CONTENT_TYPE_XML = "application/xml"; //$NON-NLS-1$

/**
* @deprecated use {@link SecurityKeyAlgorithm}
*/
@Deprecated
public static final String LICENSING_SECURITY_KEY_ALGO = "licensing.security.key.algo"; //$NON-NLS-1$
/**
* @deprecated use {@link EncryptionAlgorithm.RSA}
*/
@Deprecated
public static final String LICENSING_SECURITY_KEY_ALGO_RSA = "RSA"; //$NON-NLS-1$

/**
* @deprecated use {@link SecurityKeySize}
*/
@Deprecated
public static final String LICENSING_SECURITY_KEY_SIZE = "licensing.security.key.size"; //$NON-NLS-1$
/**
* @deprecated use {@link EncryptionKeySize.Of}
*/
@Deprecated
public static final int LICENSING_SECURITY_KEY_SIZE_1024 = 1024;

public static final String LICENSING_SECURITY_HASH_ALGO = "licensing.security.hash.algo"; //$NON-NLS-1$
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*******************************************************************************
* Copyright (c) 2020 ArSysOp
*
* 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:
* ArSysOp - initial API and implementation
*******************************************************************************/
package org.eclipse.passage.lic.internal.base.conditions.mining;

import java.util.Map;
import java.util.Optional;
import java.util.function.Function;

import org.eclipse.passage.lic.internal.api.io.EncryptionAlgorithm;
import org.eclipse.passage.lic.internal.base.BaseNamedData;

@SuppressWarnings("restriction")
public final class SecurityKeyAlgorithm extends BaseNamedData<EncryptionAlgorithm> {

public SecurityKeyAlgorithm(Function<String, EncryptionAlgorithm> retrieve) {
super(retrieve);
}

public SecurityKeyAlgorithm(Map<String, Object> container) {
super(key -> Optional.ofNullable(container.get(key))//
.map(String::valueOf) //
.map(String::trim) //
.filter(value -> !value.isEmpty())//
.map(EncryptionAlgorithm.Of::new)//
.map(EncryptionAlgorithm.class::cast) // just for compiler
.orElseGet(EncryptionAlgorithm.Default::new));
}

public SecurityKeyAlgorithm(EncryptionAlgorithm value) {
super(key -> value);
}

public SecurityKeyAlgorithm(String algorithm) {
super(key -> new EncryptionAlgorithm.Of(algorithm));
}

@Override
public String key() {
return "licensing.security.key.algo"; //$NON-NLS-1$
}

}
Loading

0 comments on commit 05db8f9

Please sign in to comment.