0

I created client and server program to exchange the data. Client and server uses tls to pass the message securely. Used openssl to make the connection between server and client. Now i have the ssl handle. Is there any way to extract server write key, server random, client random,client write key, master key.

in the below code sample Servlet method will connect to the client do the handshake get the session keys. i am trying to read the tls packet from the port after tls handshake completed and pass the packet to another module. To check the for malformed or invalid packets i need to decrypt the packet to inspect the payload where i required the session keys.

Is there any way to extract server write key, server random, client random,client write key, master key.

code snippet:

     main(){
      // Initialize the SSL library
    SSL_library_init();

    portnum = Argc[1];
    ctx = InitServerCTX();        /* initialize SSL */
    LoadCertificates(ctx, "mycert.pem", "mycert.pem"); /* load certs */
    server = OpenListener(atoi(portnum));    /* create server socket */
    while (1)
    {   struct sockaddr_in addr;
        socklen_t len = sizeof(addr);
        SSL *ssl;

        int client = accept(server, (struct sockaddr*)&addr, &len);  /* accept connection as usual */
        printf("Connection: %s:%d\n",inet_ntoa(addr.sin_addr), ntohs(addr.sin_port));
        ssl = SSL_new(ctx);              /* get new SSL state with context */
        SSL_set_fd(ssl, client);      /* set connection socket to SSL state */
        Servlet(ssl);         /* service connection */
    }
    close(server);          /* close server socket */
    SSL_CTX_free(ctx);         /* release context */

}

   

     Servlet()
       {
        if ( SSL_accept(ssl) == FAIL )     /* do SSL-protocol accept */
            ERR_print_errors_fp(stderr);
        else
        {
            unsigned char key[100];
    
            //SSL_SESSION_get_master_key(ssl,key,100);
            //printf("masterkey:%s", key);
            ShowCerts(ssl);        /* get any certificates */
            bytes = SSL_read(ssl, buf, sizeof(buf)); /* get request */
            buf[bytes] = '\0';
    
            printf("Client msg: \"%s\"\n", buf);
    
            if ( bytes > 0 )
            {
         if(strcmp(cpValidMessage,buf) == 0)
          {
             SSL_write(ssl, ServerResponse, strlen(ServerResponse)); /* 
           send reply */
           }
user1897937
  • 379
  • 1
  • 4
  • 9

0 Answers0