//2012-07-09 requires FileSystem
//TODO: Unit Tests
import java.io.File;
import java.io.IOException;
public class SSLUtility
{
private String location;
private String utility;
SSLUtility() throws IOException
{
FileSystem fs = new FileSystem();
location = "/usr/bin/";
location = fs.convertFileSystemPath( location );
utility = location + fs.getFileSeparator() + "openssl";
if( fs.isWindows() )
{ utility = utility + ".exe";
}
}
private boolean isValid()
{
boolean isValid = false;
File opensslFile = new File( utility );
if( opensslFile.exists() )
{ isValid = true;
}
return isValid;
}
//openssl.exe x509 -noout -modulus -in /var/lib/ssl/cert.crt | /usr/bin/openssl.exe md5
//openssl rsa -noout -modulus -in server.key | openssl md5
protected boolean isCertificateAndKeyValid( String certPathAndFilename , String keyPathAndFilename ) throws IOException
{
if( certPathAndFilename == null )
{
throw new IllegalArgumentException( "ERROR: certPathAndFilename cannot be null" );
}
if( certPathAndFilename.isEmpty() )
{
throw new IllegalArgumentException( "ERROR: certPathAndFilename cannot be empty" );
}
if( keyPathAndFilename == null )
{
throw new IllegalArgumentException( "ERROR: keyPathAndFilename cannot be null" );
}
if( keyPathAndFilename.isEmpty() )
{
throw new IllegalArgumentException( "ERROR: keyPathAndFilename cannot be empty" );
}
boolean result = false;
String certResult = "";
String keyResult = "";
String command = "";
FileSystem fs = new FileSystem();
SSLUtility util = new SSLUtility();
if( util.isValid() )
{
command = utility + " x509 -noout -modulus -in " + certPathAndFilename;
// System.out.println( "DEBUG: " + command );
certResult = fs.runSystemCommand( command );
command = utility + " rsa -noout -modulus -in " + keyPathAndFilename;
// System.out.println( "DEBUG: " + command );
keyResult = fs.runSystemCommand( command );
if( !keyResult.isEmpty() && !certResult.isEmpty() && keyResult.equals( certResult ) )
{ result = true;
}
}
return result;
}
protected String isCertificateAndIntermediateValid( String certPathAndFilename , String intermediatePathAndFilename ) throws IOException
{
String certResult = "";
String command = "";
FileSystem fs = new FileSystem();
SSLUtility util = new SSLUtility();
if( util.isValid() )
{
command = utility + " verify -purpose sslserver -CAfile " + intermediatePathAndFilename + " -verbose " + certPathAndFilename;
// System.out.println( "DEBUG: " + command );
certResult = fs.runSystemCommand( command );
}
return certResult;
}
} //end class