Skip to content

Commit

Permalink
methods set flexiprovider or sun Providers with preference ordering
Browse files Browse the repository at this point in the history
  • Loading branch information
simlei committed Aug 25, 2020
1 parent 0e5b753 commit a6cf43f
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 23 deletions.
106 changes: 85 additions & 21 deletions de.flexiprovider/src/de/flexiprovider/FlexiProviderController.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.security.Provider;
import java.security.Security;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import org.jcryptool.core.logging.utils.LogUtil;
Expand All @@ -35,12 +36,82 @@
*/
public class FlexiProviderController extends AbstractProviderController {

public static final FlexiNFProvider FLEXI_NF_PROVIDER = new FlexiNFProvider();
public static final FlexiPQCProvider FLEXI_PQC_PROVIDER = new FlexiPQCProvider();
public static final FlexiECProvider FLEXI_EC_PROVIDER = new FlexiECProvider();
public static final FlexiCoreProvider FLEXICORE_PROVIDER = new FlexiCoreProvider();
private static final Provider[] FLEXI_PROVIDERS = new Provider[] {FlexiProviderController.FLEXICORE_PROVIDER, FLEXI_EC_PROVIDER, FLEXI_PQC_PROVIDER, FLEXI_NF_PROVIDER};

static {
FLEXICORE_PROVIDER.remove("SecureRandom.BBS");
FLEXICORE_PROVIDER.remove("SecureRandom.BBSRandom");
FLEXICORE_PROVIDER.remove("Alg.Alias.SecureRandom.BBSRandom");
FLEXI_EC_PROVIDER.remove("SecureRandom.ECPRNG");
}

/**
* Empty no-args constructor.
*/
public FlexiProviderController() {
}

// these get cached before any of JCT's providers are set, so they can be repositioned (before or after) according to the current needs
private List<Provider> defaultProviders = null;

private void cacheDefaultProviders() {
if (defaultProviders == null) {
defaultProviders = new LinkedList<>();
for (Provider p : Security.getProviders()) {
defaultProviders.add(p);
}
}
}



@Override
public void setProviders__sunPromoted() {
System.err.println("promoting sun security providers in FlexiProviderController");
cacheDefaultProviders();
for(Provider p: Security.getProviders()) {
Security.removeProvider(p.getName());
}
if (Security.getProviders().length == 0) {
System.err.println("the providers have been cleared.");
}
// add the sun providers first
for (Provider provider: defaultProviders) {
System.err.println("adding Provider: " + provider.getName());
Security.addProvider(provider);
}
// add the Flexiproviders after
for (Provider provider : FLEXI_PROVIDERS) {
System.err.println("adding Provider: " + provider.getName());
Security.addProvider(provider);
}
}
@Override
public void setProviders__flexiPromoted() {
System.err.println("promoting flexi security providers in FlexiProviderController");
cacheDefaultProviders();
if (Security.getProviders().length == 0) {
System.err.println("the providers have been cleared.");
}
for(Provider p: Security.getProviders()) {
Security.removeProvider(p.getName());
}
// add the Flexiproviders first
for (Provider provider : FLEXI_PROVIDERS) {
System.err.println("adding Provider: " + provider.getName());
Security.addProvider(provider);
}
// add the sun providers after
for (Provider provider: defaultProviders) {
System.err.println("adding Provider: " + provider.getName());
Security.addProvider(provider);
}
}

/**
* Adds the four distinctive FlexiProvider cryptographic providers as the top priority providers for the platform.
*
Expand All @@ -50,31 +121,24 @@ public FlexiProviderController() {
public List<String> addProviders() {
List<String> providers = new ArrayList<String>(4);

Provider flexiCore = new FlexiCoreProvider();
flexiCore.remove("SecureRandom.BBS");
flexiCore.remove("SecureRandom.BBSRandom");
flexiCore.remove("Alg.Alias.SecureRandom.BBSRandom");
Security.addProvider(flexiCore);
providers.add(flexiCore.getName() + AbstractProviderController.SEPARATOR + flexiCore.getInfo());
LogUtil.logInfo("Security Provider '" + flexiCore.getName() + "' added.");
providers.add(FlexiProviderController.FLEXICORE_PROVIDER.getName() + AbstractProviderController.SEPARATOR + FlexiProviderController.FLEXICORE_PROVIDER.getInfo());
providers.add(FLEXI_EC_PROVIDER.getName() + AbstractProviderController.SEPARATOR + FLEXI_EC_PROVIDER.getInfo());
providers.add(FLEXI_PQC_PROVIDER.getName() + AbstractProviderController.SEPARATOR + FLEXI_PQC_PROVIDER.getInfo());
providers.add(FLEXI_NF_PROVIDER.getName() + AbstractProviderController.SEPARATOR + FLEXI_NF_PROVIDER.getInfo());

Provider flexiEC = new FlexiECProvider();
flexiEC.remove("SecureRandom.ECPRNG");
Security.addProvider(flexiEC);
providers.add(flexiEC.getName() + AbstractProviderController.SEPARATOR + flexiEC.getInfo());
LogUtil.logInfo("Security Provider '" + flexiEC.getName() + "' added.");
setProviders__flexiPromoted();
System.err.println("CURRENT PROVIDERS: ----");
for (Provider p: Security.getProviders()) {
System.err.println("- " + p.getName());
}
System.err.println("END: CURRENT PROVIDERS: ----");
return providers;
}

Provider flexiPQC = new FlexiPQCProvider();
Security.addProvider(flexiPQC);
providers.add(flexiPQC.getName() + AbstractProviderController.SEPARATOR + flexiPQC.getInfo());
LogUtil.logInfo("Security Provider '" + flexiPQC.getName() + "' added.");

Provider flexiNF = new FlexiNFProvider();
Security.addProvider(flexiNF);
providers.add(flexiNF.getName() + AbstractProviderController.SEPARATOR + flexiNF.getInfo());
LogUtil.logInfo("Security Provider '" + flexiNF.getName() + "' added.");
public static void reset_crypto_providers_generic() {
// TODO Auto-generated method stub

return providers;
}

}
10 changes: 10 additions & 0 deletions org.bouncycastle/src/org/bouncycastle/BouncyCastleController.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,14 @@ public List<String> addProviders() {
return providers;
}

@Override
public void setProviders__sunPromoted() {
// System.err.println("promoting sun security providers in BouncyCastleController (NOP)");
}

@Override
public void setProviders__flexiPromoted() {
// System.err.println("promoting flexi security providers in BouncyCastleController (NOP)");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@
// -----END DISCLAIMER-----
package org.jcryptool.core.operations.providers;

import java.security.Provider;
import java.util.List;

public abstract class AbstractProviderController {

public static final String SEPARATOR = ";"; //$NON-NLS-1$

public abstract List<String> addProviders();

public abstract void setProviders__sunPromoted();
public abstract void setProviders__flexiPromoted();

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.security.Security;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import org.eclipse.core.runtime.CoreException;
Expand All @@ -34,6 +35,8 @@ public class ProviderManager2 {
/* */
private List<String> availableProviders = new ArrayList<String>();

public List<AbstractProviderController> controllers = new LinkedList<>();

private ProviderManager2() {
loadProviders();
// establish order
Expand Down Expand Up @@ -67,6 +70,7 @@ private void loadProviders() {
}
AbstractProviderController controller = (AbstractProviderController) configElements[j]
.createExecutableExtension("providerController"); //$NON-NLS-1$
this.controllers.add(controller);
addProviders(controller.addProviders());
} catch (CoreException e) {
LogUtil.logError(OperationsPlugin.PLUGIN_ID, "CoreException while accessing a provider controller", //$NON-NLS-1$
Expand Down Expand Up @@ -101,4 +105,12 @@ public Provider getFactoryDefaultProvider() {
return Security.getProvider(factoryDefaultProvider);
}

public void setProviders__flexiPromoted() {
this.controllers.forEach(c -> c.setProviders__flexiPromoted());
}

public void setProviders__sunPromoted() {
this.controllers.forEach(c -> c.setProviders__sunPromoted());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,15 @@
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.ui.PlatformUI;
import org.jcryptool.core.logging.utils.LogUtil;
import org.jcryptool.core.operations.providers.AbstractProviderController;
import org.jcryptool.core.operations.providers.ProviderManager2;
import org.jcryptool.core.util.directories.DirectoryService;
import org.jcryptool.crypto.keystore.KeyStorePlugin;
import org.jcryptool.crypto.keystore.keys.IKeyStoreAlias;
import org.jcryptool.crypto.keystore.keys.KeyType;
import org.jcryptool.crypto.keystore.ui.views.nodes.ContactManager;

import de.flexiprovider.FlexiProviderController;
import de.flexiprovider.api.keys.Key;

/**
Expand Down Expand Up @@ -96,9 +98,13 @@ public class KeyStoreManager {
private KeyStoreManager() {
ProviderManager2.getInstance();
try {
// keyStore = KeyStore.getInstance("JCEKS"); //$NON-NLS-1$
ProviderManager2.getInstance().setProviders__flexiPromoted();
keyStore = KeyStore.getInstance("JCEKS"); //$NON-NLS-1$
} catch (KeyStoreException ex) {
LogUtil.logError(KeyStorePlugin.PLUGIN_ID, Messages.KeyStoreManager_0, ex, true);
} finally {
ProviderManager2.getInstance().setProviders__sunPromoted();
}

platformKeystore = EFS.getLocalFileSystem().fromLocalFile(new File(KEYSTORE_FILE));
Expand Down Expand Up @@ -141,6 +147,7 @@ private void createDefaultKeystore() {
BufferedInputStream is = null;

try {
ProviderManager2.getInstance().setProviders__flexiPromoted();
File flexiProvider = new File(DirectoryService.getWorkspaceDir(), FLEXIPROVIDER_FOLDER);
if (!flexiProvider.exists()) {
flexiProvider.mkdir();
Expand All @@ -157,6 +164,7 @@ private void createDefaultKeystore() {
} catch (Exception ex) {
LogUtil.logError(KeyStorePlugin.PLUGIN_ID, ex);
} finally {
ProviderManager2.getInstance().setProviders__sunPromoted();
if (is != null) {
try {
is.close();
Expand Down Expand Up @@ -197,11 +205,14 @@ private void loadKeystore() {
private void saveKeystore() {
OutputStream os = null;
try {
ProviderManager2.getInstance().setProviders__flexiPromoted();

os = new BufferedOutputStream(platformKeystore.openOutputStream(EFS.NONE, null));
keyStore.store(os, KEYSTORE_PASSWORD);
} catch (Exception ex) {
LogUtil.logError(KeyStorePlugin.PLUGIN_ID, Messages.KeyStoreManager_1, ex, true);
} finally {
ProviderManager2.getInstance().setProviders__sunPromoted();
if (os != null) {
try {
os.close();
Expand All @@ -214,6 +225,7 @@ private void saveKeystore() {

public void backupKeystore(String pathToFile) {
try {
ProviderManager2.getInstance().setProviders__flexiPromoted();
File backupFile = new File(pathToFile);
URI uri = backupFile.toURI();
IFileStore backupKeystore = EFS.getLocalFileSystem().getStore(uri);
Expand All @@ -229,11 +241,14 @@ public void backupKeystore(String pathToFile) {
}
} catch (Exception ex) {
LogUtil.logError(KeyStorePlugin.PLUGIN_ID, ex);
}
} finally {
ProviderManager2.getInstance().setProviders__sunPromoted();
}
}

public void restoreKeystore(String pathToFile) {
try {
ProviderManager2.getInstance().setProviders__flexiPromoted();
File flexiProvider = new File(DirectoryService.getWorkspaceDir(), FLEXIPROVIDER_FOLDER);
if (!flexiProvider.exists()) {
flexiProvider.mkdir();
Expand All @@ -254,7 +269,9 @@ public void restoreKeystore(String pathToFile) {
}
} catch (Exception ex) {
LogUtil.logError(KeyStorePlugin.PLUGIN_ID, ex);
}
} finally {
ProviderManager2.getInstance().setProviders__sunPromoted();
}
}

/**
Expand Down

0 comments on commit a6cf43f

Please sign in to comment.