Click here to Skip to main content
15,915,160 members
Home / Discussions / Java
   

Java

 
AnswerRe: java programmimg Pin
Member 1494135617-Sep-20 19:39
Member 1494135617-Sep-20 19:39 
AnswerRe: java programmimg Pin
Sandeep Mewara17-Sep-20 20:23
mveSandeep Mewara17-Sep-20 20:23 
AnswerRe: java programmimg Pin
Gerry Schmitz18-Sep-20 0:44
mveGerry Schmitz18-Sep-20 0:44 
GeneralRe: java programmimg Pin
Dave Kreskowiak18-Sep-20 4:33
mveDave Kreskowiak18-Sep-20 4:33 
QuestionIntegrating keycloak with spring security Pin
arunken16-Sep-20 21:30
arunken16-Sep-20 21:30 
QuestionHelp me! Pin
Member 1492738231-Aug-20 8:30
Member 1492738231-Aug-20 8:30 
AnswerRe: Help me! Pin
ZurdoDev31-Aug-20 9:37
professionalZurdoDev31-Aug-20 9:37 
AnswerRe: Help me! Pin
ZurdoDev31-Aug-20 9:38
professionalZurdoDev31-Aug-20 9:38 
AnswerRe: Help me! Pin
Afzaal Ahmad Zeeshan31-Aug-20 17:52
professionalAfzaal Ahmad Zeeshan31-Aug-20 17:52 
AnswerRe: Help me! Pin
Richard MacCutchan31-Aug-20 21:11
mveRichard MacCutchan31-Aug-20 21:11 
QuestionHow can I solve the following error: javax.servlet.jsp.JspFactory.getJspApplicationContext(Ljavax/servlet/ServletContext;)Ljavax/servlet/jsp/JspApplicationContext; Pin
Member 1469623621-Jul-20 18:41
Member 1469623621-Jul-20 18:41 
AnswerRe: How can I solve the following error: javax.servlet.jsp.JspFactory.getJspApplicationContext(Ljavax/servlet/ServletContext;)Ljavax/servlet/jsp/JspApplicationContext; Pin
Richard MacCutchan21-Jul-20 21:41
mveRichard MacCutchan21-Jul-20 21:41 
AnswerRe: How can I solve the following error: javax.servlet.jsp.JspFactory.getJspApplicationContext(Ljavax/servlet/ServletContext;)Ljavax/servlet/jsp/JspApplicationContext; Pin
ZurdoDev31-Aug-20 9:37
professionalZurdoDev31-Aug-20 9:37 
QuestionBLOB TO MySQL Pin
Member 1489223717-Jul-20 6:28
Member 1489223717-Jul-20 6:28 
QuestionJava tuition/courses Pin
jopag80003-Jul-20 8:54
jopag80003-Jul-20 8:54 
AnswerRe: Java tuition/courses Pin
Richard MacCutchan10-Jul-20 22:08
mveRichard MacCutchan10-Jul-20 22:08 
QuestionSSLSocket.getInputStream() hangs when called Java 11 Pin
nrmad1-Jul-20 8:35
nrmad1-Jul-20 8:35 
I am trying to get a simple one way Java TLS connection using the bouncycastle provider. For a couple days I have had this issue where on calling the server side SSLSocket.getInputStream() the thread hangs whereas the client side SSLSocket.getOutputStream() method is seemingly successful. In my code I generate a self signed certificate using bouncycastle which is then used in initialising the server side SSLContext with a KeyManager and the client side with a TrustManager. I have tried explicitly starting the handshake with SSLSocket.startHandshake which itself then hangs. Additionally I have spent a good deal of time making my code as similar as possible to the examples given by the BCFips manual and the Java Cryptography: Tools and Techniques book but the problem persists.

This is the security class which has methods to create the V1 certificate, generate RSA keypairs and helper methods for the aforementioned:

private static final String ASYMMETRIC_KEY_ALG = "RSA";
    private static final String SYMMETRIC_KEY_ALG = "AES";
    private static final String SYMMETRIC_KEY_ALG_MODE_PAD = SYMMETRIC_KEY_ALG + "/ECB/PKCS7Padding";
    private static final String PROVIDER = "BC";
    private static final String HASH_DIGEST_ALG = "SHA3-512";
    private static final String CERT_FACTORY = "X.509";
    private static final String KEYSTORE_TYPE = "PKCS12";
    private static final String SIGNATURE_ALG = "SHA384with" + ASYMMETRIC_KEY_ALG;
    private static final String SECURE_RANDOM_ALG = "SHA1PRNG";
    private static final String AUTH_HASH_DIGEST_ALG = "PBKDF2WithHmacSHA512";
//    private static final File TRUSTSTORE_NAME = new File("/var/lib/secure-messenger-relay/truststore.p12");
    private static final File KEYSTORE_NAME = new File("/var/lib/secure-messenger-relay/keystore.p12");

    private static long serialNumberBase = System.currentTimeMillis();

    static{
        Security.addProvider(new BouncyCastleProvider());
        Security.addProvider(new BouncyCastleJsseProvider());
    }
    // APPARENTLY FUNCTIONALITY TO UPDATE KS PASSWORDS IS GOOD


    /**
     * Generate an RSA keypair
     * @return return the keypair
     * @throws GeneralSecurityException
     */
    public static KeyPair generateKeyPair()
            throws GeneralSecurityException
    {
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(ASYMMETRIC_KEY_ALG, PROVIDER);
        keyPairGenerator.initialize(new RSAKeyGenParameterSpec(3072, RSAKeyGenParameterSpec.F4));
        return keyPairGenerator.generateKeyPair();
    }



    /**
     * Generate a self signed V1 X509Certificate for use by the server to authenticate and sign new users into the network
     * it pertains to.
     * @param caPrivateKey The private key for use in signing
     * @param caPublicKey the public key of the certificate
     * @param name The name of the self signing party
     * @return The Certificate
     * @throws GeneralSecurityException
     * @throws OperatorCreationException
     */
    public static X509Certificate makeV1Certificate(PrivateKey caPrivateKey, PublicKey caPublicKey, String name)
            throws GeneralSecurityException, OperatorCreationException
    {

        X509v1CertificateBuilder v1CertBldr = new JcaX509v1CertificateBuilder(
                new X500Name("CN=" + name),
                calculateSerialNumber(),
                calculateDate(0),
                calculateDate(24 * 365 * 100),
                new X500Name("CN=" + name),
                caPublicKey);

        JcaContentSignerBuilder signerBuilder = new JcaContentSignerBuilder(SIGNATURE_ALG).setProvider(PROVIDER);
        return new JcaX509CertificateConverter().setProvider(PROVIDER).getCertificate(v1CertBldr.build(signerBuilder.build(caPrivateKey)));
    }


    /**
     * A date utilitiy for calculating how much time in the future a certificate will be valid for
     * @param hoursInFuture the number of hours you want the certificate to be valid for
     * @return the Date of that number of hours in the future from the current time
     */
    private static Date calculateDate(int hoursInFuture){

        long secs = System.currentTimeMillis() / 1000;

        return new Date((secs + (hoursInFuture * 60 * 60)) * 1000);
    }

    /**
     * A method for soliciting a distinct serial number for certificate generation for multiple threads
     * @return the SerialNumber
     */
    private static synchronized BigInteger calculateSerialNumber(){
        return BigInteger.valueOf(serialNumberBase++);
    }

    /**
     * produce a salt value for use in password hashing
     * @return salt
     * @throws NoSuchAlgorithmException missing boi
     */
    private static byte[] getSalt()
            throws NoSuchAlgorithmException
    {
        SecureRandom secureRandom = SecureRandom.getInstance(SECURE_RANDOM_ALG);
        byte[] salt = new byte[64];
        secureRandom.nextBytes(salt);
        return salt;
    }

}


This is the test class is where I set up the keystore and establish the connection between the server instance SSLServerSocket with accept() and the client SSLSocket instance. The code in the testSession() method is successful in calling the getOutputStream() method:

public class ReceiverClientThreadTest {

    // ADD REG AND A SINGLE NETWORK
    // ESTABLISH A TLS CONNECTION BETWEEN TWO POINTS WITH
    private final static String KEY_MANAGER = "SunX509";
    private final static String TLS_VERSION = "TLSv1.2";
//    private final static String RNG_ALGORITHM = "DEFAULT";
//    private final static String RNG_PROVIDER = "BC";
    private static final String PROVIDER = "BC";
    private static final String KEYSTORE_TYPE = "PKCS12";
    private static KeyStore keyStore1, keyStore2, trustStore2;
    private SSLSocket serverSocket;
    private SSLSocket clientSocket;

    @BeforeClass
    public static void setUp() throws GeneralSecurityException, OperatorCreationException, IOException {

        // CODE HERE RESPONSIBLE FOR GENERATING V1 CERT AND PUTTING IT IN BOTH KEYSTORE FOR SERVER AND
        // TRUSTSTORE FOR CLIENT ( HAVE TRIED WITH AND WITHOUT A PASSWORD FOR setKeyEntry() WITHOUT EFFECT

        String name1 = "localhost", name2 = "client";
        KeyPair kp1 = SecurityUtilities.generateKeyPair();
        X509Certificate cert1 = SecurityUtilities.makeV1Certificate(kp1.getPrivate(), kp1.getPublic(), name1);

        keyStore1 = KeyStore.getInstance(KEYSTORE_TYPE, PROVIDER);
        trustStore2 = KeyStore.getInstance(KEYSTORE_TYPE, PROVIDER);


        keyStore1.load(null, null);
        keyStore1.setKeyEntry(name1, kp1.getPrivate(), "relaypass".toCharArray(), new X509Certificate[]{cert1});


        trustStore2.load(null, null);
        trustStore2.setCertificateEntry(name2, cert1);

//        secureSocketManager = new SecureSocketManager(keyStore1, password);
    }




    @Before
    public void init() throws IOException, GeneralSecurityException, InterruptedException, ExecutionException {
        
        // THIS CODE BLOCK CALLS getSSLServerSocket() and getSSLSocketFactory() AND USES THEM WITH TWO CALLABLE THREADS
        // TO ESTABLISH A CONNECTION 
        SSLServerSocket sslServerSocket = getSSLServerSocket();
        SSLSocketFactory sslSocketFactory = getSSLSocketFactory();
        ExecutorService pool = Executors.newFixedThreadPool(2);

        Callable<SSLSocket> c1 = () -> {
            return (SSLSocket) sslServerSocket.accept();
        };

        Callable<SSLSocket> c2 = () -> {
            return (SSLSocket) sslSocketFactory.createSocket("localhost", 2048);
        };

        Future<SSLSocket> server = pool.submit(c1);
        Thread.sleep(1000);
        Future<SSLSocket> client = pool.submit(c2);
        Thread.sleep(1000);
        serverSocket = server.get();
        clientSocket = client.get();
    }

    @After
    public void tearDown(){
        serverSocket = null;
        clientSocket = null;
    }

    @org.junit.Test
    public void testSession(){
        
        // SUCCESSFULLY RETURNS FROM getOutputStream()
            Thread test = new Thread(new ReceiverClientThread(serverSocket));

            test.start();
            try (ObjectOutputStream output = new ObjectOutputStream(new BufferedOutputStream(clientSocket.getOutputStream()))) {
                System.out.println("here");
            }catch (IOException e){
                fail();
            }

    }

    private SSLServerSocket getSSLServerSocket() throws GeneralSecurityException, IOException {
        char[] entryPassword = "relaypass".toCharArray();
        // COULD ADD PROVIDER IN THESE FOR CONSISTENCY
        KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance("PKIX", "BCJSSE");
        keyManagerFactory.init(keyStore1, entryPassword);

        // specify TLS version e.g. TLSv1.3
        SSLContext sslContext = SSLContext.getInstance(TLS_VERSION, "BCJSSE");
        sslContext.init(keyManagerFactory.getKeyManagers(),null, null);

        SSLServerSocketFactory fact = sslContext.getServerSocketFactory();
        return (SSLServerSocket) fact.createServerSocket(2048 );
    }

    private SSLSocketFactory getSSLSocketFactory() throws GeneralSecurityException{

        char[] entryPassword = "relaypass".toCharArray();
        // COULD ADD PROVIDER IN THESE FOR CONSISTENCY
//        KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KEY_MANAGER, "BCJSSE");
//        keyManagerFactory.init(keyStore1, entryPassword);

        TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance("PKIX", "BCJSSE");
        trustManagerFactory.init(trustStore2);
        // specify TLS version e.g. TLSv1.3
        SSLContext sslContext = SSLContext.getInstance(TLS_VERSION, "BCJSSE");
        sslContext.init(null,trustManagerFactory.getTrustManagers(), null);
       return  sslContext.getSocketFactory();
    }


}


This thread which is passed the SSLSocket of the client hangs on the call to SSLSocket.getInputStream():

public class ReceiverClientThread implements Runnable {

    private final SSLSocket sslSocket;


    public ReceiverClientThread(SSLSocket sslSocket) {
        this.sslSocket = sslSocket;

    }

    public void run() {

            try (ObjectInputStream input = new ObjectInputStream(new BufferedInputStream(sslSocket.getInputStream()))) {

                System.out.println("here");
            } catch (IOException e) {
            }
        }

}



What could I be doing wrong as I have gone through two manuals and done my best to copy the code to the letter. I would suspect foul play in regards to the connection being over localhost but surely the fact that the Callable threads return successfully means that a connection was established and there is an issue with the handshake? Any help would be appreciated. Sigh | :sigh: Sigh | :sigh: Frown | :(
AnswerMessage Closed Pin
16-Feb-22 23:05
Weent1916-Feb-22 23:05 
AnswerMessage Closed Pin
16-Feb-22 23:05
Weent1916-Feb-22 23:05 
QuestionIntegration Pin
GauravSahu911-Jul-20 0:39
GauravSahu911-Jul-20 0:39 
AnswerRe: Integration Pin
OriginalGriff1-Jul-20 0:41
mveOriginalGriff1-Jul-20 0:41 
QuestionHow to dynamically RDF resources in java ? Pin
Member 1487416726-Jun-20 8:26
Member 1487416726-Jun-20 8:26 
AnswerRe: How to dynamically RDF resources in java ? Pin
Richard MacCutchan26-Jun-20 22:24
mveRichard MacCutchan26-Jun-20 22:24 
QuestionNeed help to simplify the solution Pin
User-862169523-Jun-20 18:02
User-862169523-Jun-20 18:02 
AnswerRe: Need help to simplify the solution Pin
Richard MacCutchan23-Jun-20 21:53
mveRichard MacCutchan23-Jun-20 21:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.