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
@@ -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;
@@ -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
@@ -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)
@@ -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);
}

}

}
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
@@ -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;
@@ -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$
@@ -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;
}
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
@@ -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;
@@ -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$