add SSLSocketFactory getDefaultCipherSuites(), getSupportedCipherSuites()

pull/23/head
Chris Conlon 2019-03-06 13:20:14 -08:00
parent d12a5b4bb8
commit 9e94f49d4d
6 changed files with 212 additions and 4 deletions

View File

@ -536,6 +536,23 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_WolfSSL_getHmacMaxSize
return MAX_DIGEST_SIZE;
}
JNIEXPORT jstring JNICALL Java_com_wolfssl_WolfSSL_getEnabledCipherSuites
(JNIEnv* jenv, jclass jcl)
{
int ret;
char ciphers[4096];
jstring retString;
ret = wolfSSL_get_ciphers(ciphers, sizeof(ciphers));
if (ret != WOLFSSL_SUCCESS) {
return NULL;
}
retString = (*jenv)->NewStringUTF(jenv, ciphers);
return retString;
}
JNIEXPORT jint JNICALL Java_com_wolfssl_WolfSSL_isEnabledCRL
(JNIEnv* jenv, jclass jcl)
{

View File

@ -287,6 +287,14 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_WolfSSL_getBulkCipherAlgorithmEnumHC128
JNIEXPORT jint JNICALL Java_com_wolfssl_WolfSSL_getBulkCipherAlgorithmEnumRABBIT
(JNIEnv *, jclass);
/*
* Class: com_wolfssl_WolfSSL
* Method: getEnabledCipherSuites
* Signature: ()Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_com_wolfssl_WolfSSL_getEnabledCipherSuites
(JNIEnv *, jclass);
/*
* Class: com_wolfssl_WolfSSL
* Method: TLSv1Enabled

View File

@ -261,6 +261,8 @@ public class WolfSSL {
static native int getBulkCipherAlgorithmEnumHC128();
static native int getBulkCipherAlgorithmEnumRABBIT();
static native String getEnabledCipherSuites();
/* ------------------------- Java methods --------------------------- */
/**
@ -670,6 +672,17 @@ public class WolfSSL {
*/
public static native int getHmacMaxSize();
public static String[] getCiphers() {
String cipherSuites = getEnabledCipherSuites();
if (cipherSuites == null)
return null;
String[] suiteArray = cipherSuites.split(":");
return suiteArray;
}
/* ------------------------- isEnabled methods -------------------------- */
/**

View File

@ -173,14 +173,12 @@ public class WolfSSLSocketFactory extends SSLSocketFactory {
@Override
public String[] getDefaultCipherSuites() {
/* TODO */
return null;
return WolfSSL.getCiphers();
}
@Override
public String[] getSupportedCipherSuites() {
/* TODO */
return null;
return getDefaultCipherSuites();
}
@Override

View File

@ -27,6 +27,7 @@ import org.junit.runners.Suite;
@RunWith(Suite.class)
@Suite.SuiteClasses({
WolfSSLContextTest.class,
WolfSSLSocketFactoryTest.class,
})

View File

@ -0,0 +1,171 @@
/* WolfSSLContextTest.java
*
* Copyright (C) 2006-2018 wolfSSL Inc.
*
* This file is part of wolfSSL.
*
* wolfSSL is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* wolfSSL is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
package com.wolfssl.provider.jsse.test;
import org.junit.Test;
import org.junit.BeforeClass;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import static org.junit.Assert.*;
import java.util.ArrayList;
import com.wolfssl.provider.jsse.WolfSSLSocketFactory;
import java.io.FileInputStream;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.TrustManagerFactory;
import java.security.Security;
import java.security.Provider;
import java.security.KeyStore;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.security.KeyStoreException;
import java.security.KeyManagementException;
import java.security.NoSuchProviderException;
import java.security.NoSuchAlgorithmException;
import com.wolfssl.provider.jsse.WolfSSLProvider;
public class WolfSSLSocketFactoryTest {
public final static String clientJKS = "./examples/provider/client.jks";
public final static char[] jksPass = "wolfSSL test".toCharArray();
private static String allProtocols[] = {
"TLSV1",
"TLSV1.1",
"TLSV1.2",
"TLS"
};
private static ArrayList<String> enabledProtocols =
new ArrayList<String>();
/* list of SSLSocketFactories for each protocol supported */
private static ArrayList<SSLSocketFactory> sockFactories =
new ArrayList<SSLSocketFactory>();
@BeforeClass
public static void testSetupSocketFactory() throws NoSuchProviderException,
NoSuchAlgorithmException, IllegalStateException,
KeyManagementException, Exception {
SSLContext ctx;
KeyManagerFactory km;
TrustManagerFactory tm;
KeyStore pKey, cert;
System.out.println("WolfSSLSocketFactory Class");
/* install wolfJSSE provider at runtime */
Security.addProvider(new WolfSSLProvider());
Provider p = Security.getProvider("wolfJSSE");
assertNotNull(p);
/* populate enabledProtocols */
for (int i = 0; i < allProtocols.length; i++) {
try {
ctx = SSLContext.getInstance(allProtocols[i], "wolfJSSE");
enabledProtocols.add(allProtocols[i]);
} catch (NoSuchAlgorithmException e) {
/* protocol not enabled */
}
}
try {
/* set up KeyStore */
pKey = KeyStore.getInstance("JKS");
pKey.load(new FileInputStream(clientJKS), jksPass);
cert = KeyStore.getInstance("JKS");
cert.load(new FileInputStream(clientJKS), jksPass);
/* trust manager (certificates) */
tm = TrustManagerFactory.getInstance("SunX509");
tm.init(cert);
/* load private key */
km = KeyManagerFactory.getInstance("SunX509");
km.init(pKey, jksPass);
} catch (KeyStoreException kse) {
throw new Exception(kse);
} catch (FileNotFoundException fnfe) {
throw new Exception(fnfe);
} catch (IOException ioe) {
throw new Exception(ioe);
}
for (int i = 0; i < enabledProtocols.size(); i++) {
ctx = SSLContext.getInstance(enabledProtocols.get(i), "wolfJSSE");
ctx.init(km.getKeyManagers(), tm.getTrustManagers(), null);
SSLSocketFactory sf = ctx.getSocketFactory();
sockFactories.add(sf);
}
}
@Test
public void testGetDefaultCipherSuites()
throws NoSuchProviderException, NoSuchAlgorithmException {
System.out.print("\tgetDefaultCipherSuites()");
for (int i = 0; i < sockFactories.size(); i++) {
SSLSocketFactory sf = sockFactories.get(i);
String[] cipherSuites = sf.getDefaultCipherSuites();
if (cipherSuites == null) {
System.out.println("\t... failed");
fail("SSLSocketFactory.getDefaultCipherSuites() failed");
}
}
System.out.println("\t... passed");
}
@Test
public void testGetSupportedCipherSuites()
throws NoSuchProviderException, NoSuchAlgorithmException {
System.out.print("\tgetSupportedCipherSuites()");
for (int i = 0; i < sockFactories.size(); i++) {
SSLSocketFactory sf = sockFactories.get(i);
String[] cipherSuites = sf.getSupportedCipherSuites();
if (cipherSuites == null) {
System.out.println("\t... failed");
fail("SSLSocketFactory.getSupportedCipherSuites() failed");
}
}
System.out.println("\t... passed");
}
}