Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

573004 persist generated key pairs #751

Merged
merged 7 commits into from
Apr 24, 2021
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2020 ArSysOp
* Copyright (c) 2020, 2021 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 @@ -12,9 +12,12 @@
*******************************************************************************/
package org.eclipse.passage.lic.internal.api.io;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Path;
import java.util.Objects;

import org.eclipse.passage.lic.internal.api.LicensedProduct;
import org.eclipse.passage.lic.internal.api.LicensingException;
Expand All @@ -37,16 +40,17 @@ public interface StreamCodec extends Service<LicensedProduct> {
EncryptionKeySize keySize();

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

/**
* Encode {@code input} stream data with a private key retrieved form the given
Expand All @@ -57,7 +61,7 @@ public interface StreamCodec extends Service<LicensedProduct> {
* @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
* @param password of the private key owner password
* @throws LicensingException in case of any i/o misbehavior
*/
void encode(InputStream input, OutputStream output, InputStream key, String username, String password)
Expand All @@ -78,4 +82,68 @@ void encode(InputStream input, OutputStream output, InputStream key, String user
void decode(InputStream input, OutputStream output, InputStream key, DigestExpectation digest)
throws LicensingException;

public static final class Smart implements StreamCodec {
private final StreamCodec delegate;

public Smart(StreamCodec delegate) {
Objects.requireNonNull(delegate, "StreamCodec.Smart::delegate"); //$NON-NLS-1$
this.delegate = delegate;
}

@Override
public LicensedProduct id() {
return delegate.id();
}

@Override
public EncryptionAlgorithm algorithm() {
return delegate.algorithm();
}

@Override
public EncryptionKeySize keySize() {
return delegate.keySize();
}

@Override
public void createKeyPair(OutputStream publicKey, OutputStream privateKey, String username, String password)
throws LicensingException {
delegate.createKeyPair(publicKey, privateKey, username, password);
}

/**
* Create a public/private keys pair and store them to {@code publicKey} path
* and {@code privateKey} path respectively.
*
* @param publicKey file system path for <i>public key</i> to be generated
* @param privateKey file system path for <i>private key</i> to be generated
* @param username of the keys owner user
* @param password of the keys owner password
* @throws LicensingException in case of any i/o misbehavior
*/
public void createKeyPair(Path publicKey, Path privateKey, String username, String password)
throws LicensingException {
try (FileOutputStream pub = new FileOutputStream(publicKey.toFile()); //
FileOutputStream scr = new FileOutputStream(privateKey.toFile())) {
delegate.createKeyPair(pub, scr, username, password);
} catch (IOException e) {
throw new LicensingException("failed to create encryption keys", e); //$NON-NLS-1$
}

}

@Override
public void encode(InputStream input, OutputStream output, InputStream key, String username, String password)
throws LicensingException {
delegate.encode(input, output, key, username, password);
}

@Override
public void decode(InputStream input, OutputStream output, InputStream key, DigestExpectation digest)
throws LicensingException {
delegate.decode(input, output, key, digest);
}

}

}
2 changes: 1 addition & 1 deletion bundles/org.eclipse.passage.lic.bc/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Manifest-Version: 1.0
Automatic-Module-Name: org.eclipse.passage.lic.bc
Bundle-ManifestVersion: 2
Bundle-SymbolicName: org.eclipse.passage.lic.bc
Bundle-Version: 1.0.200.qualifier
Bundle-Version: 1.0.300.qualifier
Bundle-Name: %Bundle-Name
Bundle-Vendor: %Bundle-Vendor
Bundle-Copyright: %Bundle-Copyright
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2018, 2020 ArSysOp
* Copyright (c) 2018, 2021 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 @@ -13,8 +13,8 @@
package org.eclipse.passage.lic.internal.bc;

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Path;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
Expand Down Expand Up @@ -94,9 +94,8 @@ private PGPKeyRingGenerator keyRing(String username, String password) throws Lic
}
}

private void persist(PGPKeyRing key, Path target, String error) throws LicensingException {
try (FileOutputStream fos = new FileOutputStream(target.toFile());
ArmoredOutputStream output = new ArmoredOutputStream(new BufferedOutputStream(fos))) {
private void persist(PGPKeyRing key, OutputStream target, String error) throws LicensingException {
try (ArmoredOutputStream output = new ArmoredOutputStream(new BufferedOutputStream(target))) {
key.encode(output);
} catch (IOException e) {
throw new LicensingException(BcMessages.getString(error), e); // $NON-NLS-1$
Expand Down Expand Up @@ -128,10 +127,10 @@ private JcaPGPContentSignerBuilder signer(PGPKeyPair pair) {

static final class Targets {

private final Path publicPath;
private final Path privatePath;
private final OutputStream publicPath;
private final OutputStream privatePath;

Targets(Path publicPath, Path privatePath) {
Targets(OutputStream publicPath, OutputStream privatePath) {
this.publicPath = publicPath;
this.privatePath = privatePath;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2020 ArSysOp
* Copyright (c) 2020, 2021 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 @@ -14,7 +14,6 @@

import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Path;
import java.security.Security;
import java.util.Objects;
import java.util.function.Supplier;
Expand Down Expand Up @@ -62,8 +61,9 @@ public EncryptionKeySize keySize() {
return keySize;
}

@SuppressWarnings("resource")
@Override
public void createKeyPair(Path publicKey, Path privateKey, String username, String password)
public void createKeyPair(OutputStream publicKey, OutputStream privateKey, String username, String password)
throws LicensingException {
Objects.requireNonNull(publicKey, "BcStreamCodec::createKeyPair::publicKey"); //$NON-NLS-1$
Objects.requireNonNull(privateKey, "BcStreamCodec::createKeyPair::privateKey"); //$NON-NLS-1$
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2018, 2020 ArSysOp
* Copyright (c) 2018, 2021 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 Down Expand Up @@ -34,6 +34,7 @@ public interface EditingDomainRegistry<I> {

String getFileExtension();

// TODO: is not used
EClass getContentClassifier();

EStructuralFeature getContentIdentifierAttribute();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ private IStatus createKeyPair(ProductVersionDescriptor target, LicensedProduct p
Path destination = new UserHomeProductResidence(product).get();
Path open = open(product, destination);
Path secret = secret(product, destination);
codec.createKeyPair(open, secret, product.identifier(), new ProductVersionPassword(target).get());
new StreamCodec.Smart(codec).createKeyPair(open, secret, product.identifier(),
new ProductVersionPassword(target).get());
notify.accept(open, secret);
// TODO: store .keys_xmi under workspace
return created(open, secret);
Expand Down
2 changes: 1 addition & 1 deletion features/org.eclipse.passage.lic.bc.feature/feature.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<feature
id="org.eclipse.passage.lic.bc.feature"
label="%featureName"
version="0.6.200.qualifier"
version="0.6.300.qualifier"
provider-name="%providerName"
plugin="org.eclipse.passage.lic.bc"
license-feature="org.eclipse.license"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2020 ArSysOp
* Copyright (c) 2020, 2021 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 @@ -18,7 +18,6 @@
import org.eclipse.passage.lic.internal.api.LicensingException;
import org.eclipse.passage.lic.internal.api.io.KeyKeeper;

@SuppressWarnings("restriction")
public final class FakeKeyKeeper implements KeyKeeper, LicensedProduct {

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2020 ArSysOp
* Copyright (c) 2020, 2021 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 @@ -14,7 +14,6 @@

import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Path;

import org.eclipse.passage.lic.internal.api.LicensedProduct;
import org.eclipse.passage.lic.internal.api.LicensingException;
Expand All @@ -23,7 +22,6 @@
import org.eclipse.passage.lic.internal.api.io.EncryptionKeySize;
import org.eclipse.passage.lic.internal.api.io.StreamCodec;

@SuppressWarnings("restriction")
public final class FakeStreamCodec implements StreamCodec, LicensedProduct {

@Override
Expand All @@ -42,7 +40,7 @@ public EncryptionKeySize keySize() {
}

@Override
public void createKeyPair(Path publicKey, Path privateKey, String username, String password)
public void createKeyPair(OutputStream publicKey, OutputStream privateKey, String username, String password)
throws LicensingException {
throw new UnsupportedOperationException();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2020 ArSysOp
* Copyright (c) 2020, 2021 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 @@ -18,7 +18,6 @@
import java.io.LineNumberReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.nio.file.Path;

import org.eclipse.passage.lic.internal.api.LicensedProduct;
import org.eclipse.passage.lic.internal.api.LicensingException;
Expand Down Expand Up @@ -54,7 +53,7 @@ public EncryptionKeySize keySize() {
}

@Override
public void createKeyPair(Path publicKey, Path privateKey, String username, String password)
public void createKeyPair(OutputStream publicKey, OutputStream privateKey, String username, String password)
throws LicensingException {
throw new UnsupportedOperationException();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Manifest-Version: 1.0
Automatic-Module-Name: org.eclipse.passage.lic.bc.tests
Bundle-ManifestVersion: 2
Bundle-SymbolicName: org.eclipse.passage.lic.bc.tests
Bundle-Version: 0.5.0.qualifier
Bundle-Version: 0.5.100.qualifier
Bundle-Name: %Bundle-Name
Bundle-Vendor: %Bundle-Vendor
Bundle-Copyright: %Bundle-Copyright
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2020 ArSysOp
* Copyright (c) 2020, 2021 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 @@ -25,6 +25,7 @@

import org.eclipse.passage.lic.internal.api.LicensedProduct;
import org.eclipse.passage.lic.internal.api.LicensingException;
import org.eclipse.passage.lic.internal.api.io.StreamCodec;
import org.eclipse.passage.lic.internal.base.BaseLicensedProduct;
import org.eclipse.passage.lic.internal.base.io.PassageFileExtension;
import org.eclipse.passage.lic.internal.bc.BcStreamCodec;
Expand All @@ -49,7 +50,7 @@ protected final PairInfo<Path> pair(String user, String pass) throws IOException
protected final <I> PairInfo<I> pair(ThrowingCtor<I> ctor, String user, String pass) throws IOException {
Path pub = new TmpFile(root).keyFile(new PassageFileExtension.PublicKey());
Path secret = new TmpFile(root).keyFile(new PassageFileExtension.PrivateKey());
BcStreamCodec codec = new BcStreamCodec(this::product);
StreamCodec.Smart codec = new StreamCodec.Smart(new BcStreamCodec(this::product));
try {
codec.createKeyPair(pub, secret, user, pass);
} catch (LicensingException e) {
Expand Down
Loading