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

564819 API revision | conditions | stream codecs review #278

Merged
merged 4 commits into from
Jul 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ final class BcDecodedStream {

void produce(InputStream publicKeyRing, DigestExpectation digest) throws LicensingException {
byte[] ring = ring(publicKeyRing, digest);
try (InputStream compressed = compressedData().getDataStream()) {
try (//
InputStream decodedInput = PGPUtil.getDecoderStream(input);
InputStream compressed = compressedData(decodedInput).getDataStream()) {
PGPObjectFactory factory = new JcaPGPObjectFactory(compressed);
PGPOnePassSignature signature = signature(factory);
try (InputStream literal = literalDataStream(factory);
Expand All @@ -61,6 +63,7 @@ void produce(InputStream publicKeyRing, DigestExpectation digest) throws Licensi
signature.init(new JcaPGPContentVerifierBuilderProvider(), decodeKey);
writeVerifiedDecodedOutput(literal, signature, factory);
}

} catch (Exception e) {
throw new LicensingException( //
String.format(BcMessages.getString("BcStreamCodec_deconde_error"), product), //$NON-NLS-1$ ,
Expand Down Expand Up @@ -102,16 +105,14 @@ private InputStream literalDataStream(PGPObjectFactory factory) throws IOExcepti
return ((PGPLiteralData) factory.nextObject()).getInputStream();
}

private PGPCompressedData compressedData() throws LicensingException, IOException {
try (InputStream decoder = PGPUtil.getDecoderStream(input)) {
PGPObjectFactory factory = new JcaPGPObjectFactory(decoder);
Optional<PGPCompressedData> compressed = Optional.ofNullable((PGPCompressedData) factory.nextObject());
if (!compressed.isPresent()) {
throw new LicensingException(//
String.format(BcMessages.getString("BcStreamCodec_encode_error_data"), product)); //$NON-NLS-1$
}
return compressed.get();
private PGPCompressedData compressedData(InputStream decoder) throws LicensingException, IOException {
PGPObjectFactory factory = new JcaPGPObjectFactory(decoder);
Optional<PGPCompressedData> compressed = Optional.ofNullable((PGPCompressedData) factory.nextObject());
if (!compressed.isPresent()) {
throw new LicensingException(//
String.format(BcMessages.getString("BcStreamCodec_encode_error_data"), product)); //$NON-NLS-1$
}
return compressed.get();
}

private byte[] ring(InputStream publicKeyRing, DigestExpectation digest) throws LicensingException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,17 @@
*******************************************************************************/
package org.eclipse.passage.lic.internal.bc;

import java.util.Objects;
import java.util.function.Supplier;

import org.bouncycastle.crypto.digests.SHA512Digest;

public final class BcDigest implements Supplier<byte[]> {
final class BcDigest implements Supplier<byte[]> {

private final byte[] source;

public BcDigest(byte[] source) {
BcDigest(byte[] source) {
Objects.requireNonNull(source, "BcDigest::source"); //$NON-NLS-1$
this.source = source;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.bouncycastle.openpgp.PGPKeyRing;
import org.bouncycastle.openpgp.PGPKeyRingGenerator;
import org.bouncycastle.openpgp.PGPPublicKey;
import org.bouncycastle.openpgp.PGPSecretKey;
import org.bouncycastle.openpgp.PGPSignature;
import org.bouncycastle.openpgp.PGPSignatureSubpacketGenerator;
import org.bouncycastle.openpgp.PGPSignatureSubpacketVector;
Expand All @@ -44,6 +45,21 @@
import org.eclipse.passage.lic.internal.api.io.EncryptionKeySize;
import org.eclipse.passage.lic.internal.bc.i18n.BcMessages;

/**
* <p>
* Generated a couple of PGP keys ({@linkplain PGPPublicKey} and pairing
* {@linkplain PGPSecretKey})
* </p>
* <ul>
* <li>into the files pointed in the given {@linkplain Targets}</li>
* <li>according to the configured {@linkplain EncryptionParameters}</li>
* <li>for an owner with the passed credentials</li>
* </ul>
* <p>
* Empowers
* {@linkplain BcStreamCodec#createKeyPair(Path, Path, String, String)}.
* </p>
*/
@SuppressWarnings("restriction")
final class BcKeyPair {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
import org.eclipse.passage.lic.internal.api.LicensingException;
import org.eclipse.passage.lic.internal.bc.i18n.BcMessages;

/**
* Look for a {@code secret key} in the given {@code residence} input stream,
* which is supposed to be begotten by a key ring file.
*/
@SuppressWarnings("restriction")
final class BcResidentSecretKey {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@
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;

import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.eclipse.passage.lic.internal.api.LicensedProduct;
import org.eclipse.passage.lic.internal.api.LicensingException;
import org.eclipse.passage.lic.internal.api.io.DigestExpectation;
Expand All @@ -27,6 +30,10 @@
@SuppressWarnings("restriction")
public final class BcStreamCodec implements StreamCodec {

static {
Security.addProvider(new BouncyCastleProvider());
}

private final Supplier<LicensedProduct> product;
private final EncryptionAlgorithm algorithm;
private final EncryptionKeySize keySize;
Expand Down Expand Up @@ -59,24 +66,38 @@ public EncryptionKeySize keySize() {
@Override
public void createKeyPair(Path publicKey, Path 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$
Objects.requireNonNull(username, "BcStreamCodec::createKeyPair::username"); //$NON-NLS-1$
Objects.requireNonNull(password, "BcStreamCodec::createKeyPair::password"); //$NON-NLS-1$
new BcKeyPair( //
new BcKeyPair.Targets(publicKey, privateKey), //
new BcKeyPair.EncryptionParameters(algorithm, keySize) //
).generate(username, password);
}

@SuppressWarnings("resource")
@Override
public void encode(InputStream input, OutputStream output, InputStream key, String username, String password)
throws LicensingException {
Objects.requireNonNull(input, "BcStreamCodec::encode::input"); //$NON-NLS-1$
Objects.requireNonNull(output, "BcStreamCodec::encode::output"); //$NON-NLS-1$
Objects.requireNonNull(key, "BcStreamCodec::encode::key"); //$NON-NLS-1$ ;
Objects.requireNonNull(username, "BcStreamCodec::encode::username"); //$NON-NLS-1$
Objects.requireNonNull(password, "BcStreamCodec::encode::password"); //$NON-NLS-1$
new BcEncodedStream(product.get(), input, output)//
.produce(new BcResidentSecretKey(key, username).get(), password);
}

@SuppressWarnings("resource")
@Override
public void decode(InputStream input, OutputStream output, InputStream key, DigestExpectation digest)
throws LicensingException {
// TODO Auto-generated method stub

Objects.requireNonNull(input, "BcStreamCodec::decode::input"); //$NON-NLS-1$
Objects.requireNonNull(output, "BcStreamCodec::decode::output"); //$NON-NLS-1$
Objects.requireNonNull(key, "BcStreamCodec::decode::key"); //$NON-NLS-1$ ;
Objects.requireNonNull(digest, "BcStreamCodec::decode::digest"); //$NON-NLS-1$
new BcDecodedStream(product.get(), input, output).produce(key, digest);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*******************************************************************************
* 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.bc.tests;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;

final class FileContent {

private final Path file;

FileContent(Path file) {
this.file = file;
}

byte[] get() throws IOException {
byte[] content = new byte[(int) Files.size(file)];
try (InputStream stream = new FileInputStream(file.toFile())) {
stream.read(content);
}
return content;
}
}
Loading