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

More srtp factory improvements #26

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
88 changes: 65 additions & 23 deletions src/main/java/org/jitsi/srtp/crypto/Aes.java
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ public static synchronized void setFactoryClassName(String name)
{
FACTORY_CLASS_NAME = name;
factoryClass = null;
fastestFactories.clear();
factories = null;
}

private static abstract class BenchmarkOperation
Expand Down Expand Up @@ -861,6 +863,26 @@ public static class SunPKCS11CipherFactory
*/
private static boolean useProvider = true;

/**
* Get the configuration string we want to use for SunPKCS11.
*/
private static String getConfiguration()
{
// The SunPKCS11 Config name should be unique in order
// to avoid repeated initialization exceptions.
String name = null;
Package pkg = Aes.class.getPackage();

if (pkg != null)
name = pkg.getName();
if (name == null || name.length() == 0)
name = "org.jitsi.srtp";

return "--name=" + name + "\\n"
+ "nssDbMode=noDb\\n"
+ "attributes=compatibility";
}

/**
* Gets the {@code java.security.Provider} instance (to be) employed
* for an (optimized) AES implementation.
Expand All @@ -877,31 +899,51 @@ public static synchronized Provider getProvider()
{
try
{
Class<?> clazz
= Class.forName("sun.security.pkcs11.SunPKCS11");

if (Provider.class.isAssignableFrom(clazz))
try
{
Constructor<?> contructor
= clazz.getConstructor(String.class);

// The SunPKCS11 Config name should be unique in order
// to avoid repeated initialization exceptions.
String name = null;
Package pkg = Aes.class.getPackage();

if (pkg != null)
name = pkg.getName();
if (name == null || name.length() == 0)
name = "org.jitsi.srtp";

provider
= (Provider)
contructor.newInstance(
"--name=" + name + "\\n"
+ "nssDbMode=noDb\\n"
+ "attributes=compatibility");
/* Java 9+. PKCS11 configuration is set using the
* configure method.
* Use reflection so we can build with JDK 8. */
Method configureMethod =
Provider.class.getMethod("configure", String.class);

Provider prototype =
Security.getProvider("SunPKCS11");
if (prototype != null)
{
provider
= (Provider)
configureMethod.invoke(prototype, getConfiguration());
}
}
catch (Exception e)
{
logger.debug("Unable to construct PKCS11 provider: " + e.getMessage());
}
finally
{
/* Java 8. PKCS11 configuration is passed as a constructor parameter. */
if (provider == null)
{
Class<?> clazz
= Class.forName("sun.security.pkcs11.SunPKCS11");

if (Provider.class.isAssignableFrom(clazz))
{
Constructor<?> contructor
= clazz.getConstructor(String.class);

provider
= (Provider)
contructor
.newInstance(getConfiguration());
}
}
}
}
catch (Exception e)
{
logger.debug("Unable to construct PKCS11 provider: " + e.getMessage());
}
finally
{
Expand Down