Cannot add SSL certificate with Grizzly and Glassfish tyrus on WebSocket server

52 views Asked by At

Currently, I am running a WS (unsecure websocket) server using Grizzly and Tyrus for my messaging app. I have been trying for a while to switch to WSS (websocket secure) but I haven't seen anything mentioning server-side SSL configuration in the documentation.

Code:

App.java

//App.java

package crowchat_server.crowchat_server;

import java.io.IOException;
import java.security.KeyManagementException;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Scanner;

import javax.net.ssl.SSLContext;
import javax.net.websocket.HandshakeRequest;
import javax.net.websocket.HandshakeResponse;
import javax.websocket.ClientEndpointConfig.Configurator;
import javax.websocket.DeploymentException;
import javax.websocket.server.ServerEndpointConfig;

import org.glassfish.grizzly.GrizzlyFuture;
import org.glassfish.grizzly.http.server.HttpServer;
import org.glassfish.grizzly.http.server.NetworkListener;
import org.glassfish.grizzly.ssl.SSLContextConfigurator;
import org.glassfish.tyrus.client.SslContextConfigurator;
import org.glassfish.tyrus.client.SslEngineConfigurator;
import org.glassfish.tyrus.container.grizzly.server.GrizzlyServerContainer;
import org.glassfish.tyrus.server.Server;
import org.glassfish.tyrus.spi.ServerContainer;

import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;



public class App 
{
    //if false, use normal ip, if true use localhost
    static boolean typeServer = true;
    
    static String conip;
    static int port;
    static String endpoint = "/test";
    public static void main(String[] args) throws ClassNotFoundException, IOException  {
        BanManager bm = new BanManager();
        ModerationManager mm = new ModerationManager();
        
        
        Scanner sc = new Scanner(System.in);
        System.out.println("Type 1 to use IP, type 2 to use localhost, type 3 to custom");
        String choice = sc.nextLine();
        if(choice.equals("1")) {typeServer = false;} else if(choice.equals("2")){typeServer = true;} else if(choice.equals("3")){
            System.out.println("Enter an instruction (admin/ban - add/remove - user)");
            String banus = sc.nextLine();
            String[] parts = banus.split("-");
            String type = parts[0];
            String instr = parts[1];
            String user = parts[2]; 
            
            if(type.equals("admin")) {
                
                if(instr.equals("add")){
                    mm.addAdmin(user, true);
                } else
                {
                    mm.removeAdmin(user, true);
                }
            } else
            {
                if(instr.equals("add")){
                    bm.addBan(user, true);
                } else
                {
                    bm.removeBan(user, true);
                }
            }
            
            
            
            System.out.println("Success. Type 1 to use IP, type 2 to use localhost.");
            choice = sc.nextLine();
            if(choice.equals("1")) {typeServer = false;} else {typeServer = true;}
            
            
            
        }
        System.out.print("---CrowMessage SERVER---\nBans: ");
        bm.readBans();
        System.out.print("\nAdmins: ");
        mm.readAdmins();
        
        System.out.println("\nEnter a password for admin panel.");
        WSS.password = sc.nextLine();
        
        
        startServer("/certificate/keystore.jks","testpassword"); //starting the actual server
        
        
    }

    
    
    public static void startServer(String keystorePath, String keystorePassword) {
          try {
              if(typeServer) {conip = "localhost";} else {conip = "45.8.133.57";}
              
                SSLContext sslContext = createSSLContext(keystorePath, keystorePassword);


                Server server = new Server(conip, 8082, endpoint, null, WSS.class); //the constructor doesn't seem to have any way of adding SSL certificates to it.

                
                System.out.println("Websocket ip: wss or ws://" + conip + ":8082" + endpoint);
                System.out.println("WebSocket Server started on IP " + conip + ". Press Ctrl+C to stop.");
                server.start();
                
                Thread.currentThread().join(); // keep thread alive
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    

     private static SSLContext createSSLContext(String keystorePath, String keystorePassword)
                throws Exception {
            SslEngineConfigurator sslConfigurator = new SslEngineConfigurator(
                    new SslContextConfigurator()
                            .setKeyPassword(keystorePassword)
                            .setKeyStorePassword(keystorePassword)
                            .setTrustStorePassword(keystorePassword)
                            .setTrustStoreFile(keystorePath)
                            .setKeyStoreFile(keystorePath)
                            .createSSLContext())
                    .setClientMode(false)
                    .setNeedClientAuth(false);

            return sslConfigurator.getSslContext();
        }

pom.xml

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>crowchat_server</groupId>
  <artifactId>crowchat_server</artifactId>
  <version>0.0.1-SNAPSHOT</version>

  <name>crowchat_server</name>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>javax.websocket</groupId>
        <artifactId>javax.websocket-api</artifactId>
        <version>1.1</version>
    </dependency>

    <dependency>
        <groupId>org.glassfish.tyrus</groupId>
        <artifactId>tyrus-core</artifactId>
        <version>1.21</version>
        
    </dependency>

    <dependency>
        <groupId>org.glassfish.tyrus</groupId>
        <artifactId>tyrus-container-grizzly-server</artifactId>
        <version>1.21</version>
    </dependency>

    <dependency>
        <groupId>org.glassfish.tyrus</groupId>
        <artifactId>tyrus-container-glassfish-ejb</artifactId>
        <version>1.21</version>
        
    </dependency>

    <dependency>
        <groupId>org.glassfish.tyrus</groupId>
        <artifactId>tyrus-server</artifactId>
        <version>1.21</version> <!--old version 1.17 -->
        
    </dependency> 
    
    <dependency>
    <groupId>org.glassfish.tyrus</groupId>
    <artifactId>websocket-provider-grizzly</artifactId>
    <version>1.0-b06</version>
</dependency>    
</dependencies>

  <build>
    <pluginManagement>
      <plugins>
        
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
       
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-jar-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
       
        <plugin>
          <artifactId>maven-site-plugin</artifactId>
          <version>3.7.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-project-info-reports-plugin</artifactId>
          <version>3.0.0</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

As seen in the App class, I have tried creating a SSLContext, but I haven't found a way of implementing it.

(ChatGPT or any other AI tools haven't helped)

Additional info: WSS.class is the endpoint for the server.

Many thanks in advance.

I have tried modifying the Server constructor, but it doesn't seem to support arguments other than (String, int, String, null, Class)

I have already made a keystore.jks and a password for it but couldn't find a way of implementing it into the actual server.

0

There are 0 answers