How to interact with HSM using JAVA

Nov 13, 2014 at 2:07 PM
Hi,

I am new to Thales Simulator. I am trying to send a message to Thales Simulator console using my java code. Problem is it is connected and all but i am not getting any response back from Thales Simulator in my java code.

Do i need to do any additional configuration while setting up the Thales Simulator library?
Below is the code i have picked up from one of the discussion threads.

package thales;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.Socket;

public class TestHSMJava {
public static void main(String args[]) {
System.out.println("<<< Main Method Entry >>>");
String command = null;
Socket socket = null;
DataOutputStream out = null;
DataInputStream in = null;
byte[] b= new byte[100];
try {
    socket = new Socket("localhost", 9997);
    System.out.println("<<< Socket >>> :" + socket);
    if (socket != null) {
        System.out.println("<<< Connected to HSM  >>>:"
                + socket.isConnected());
        in = new DataInputStream (new BufferedInputStream(socket.getInputStream()));
        out = new DataOutputStream (new BufferedOutputStream(socket.getOutputStream()));
        command = "000b3030303041303030303255";
        out.writeUTF(command);
        System.out.println("Input to HSM : " +command);
        out.flush();
        String response = in.readUTF();
        System.out.println("Output from HSM : " +response);
        System.out.println("");
    }
}
catch(Exception exception){
    System.err.println("exception...exception : "+exception.getMessage());
}
finally{
    System.out.println("Complet..now get out");
}
}
}


Regards,
Amrutansu
Editor
Nov 14, 2014 at 9:17 AM
Hi!

Why do you need to send HOST command to HSM console? Use 9998 port to send it to host.

Regards,
Juris
Nov 15, 2014 at 12:35 PM
Are you able to connect your Hardware????

Your connection is OK. Verify your HSM server Ip/Port correctly and try to send Command in Uppercase.

__
HsmFirst
Nov 17, 2014 at 6:32 AM
I was just trying to connect to HSM by giving any dummy command. I had opened the console from Thales Simulator and was trying to connect to the console at the same time from java. The problem is resolved now.

Thanks,
Amrutansu
Editor
Nov 21, 2014 at 9:40 AM
Hi Amrutansu,

Did you managed to send command to simulator or you need future assistance?

Regards,
Juris
Nov 24, 2014 at 10:11 AM
Edited Nov 24, 2014 at 12:48 PM
Hi Juris,

I managed to send the commands to Simulator. But now i am unable to get any response from the simulator.
Everytime I am trying to execute a command, I am getting an EOF error. (Collections$RandomAccessList error)

For command HC, please find below the log:
Request: HC
Parsing header and code of message HC...
Exception while parsing message or creating implementor instance
System.ArgumentOutOfRangeException: Index and length must refer to a location within the string.
Parameter name: length
at System.String.InternalSubStringWithChecks(Int32 startIndex, Int32 length, Boolean fAlwaysCopy)
at System.String.Substring(Int32 startIndex, Int32 length)
at ThalesSim.Core.Message.Message.GetSubstring(Int32 length)
at ThalesSim.Core.ThalesMain.WCMessageArrived(WorkerClient sender, Byte[]& b, Int32 len)
Disconnecting client.
Client disconnected.

Regards,
Amrutansu
Editor
Nov 24, 2014 at 1:13 PM
Edited Nov 24, 2014 at 1:13 PM
Hi!

Are you sure you want to execute HC command instead of NC?

HC command generates and exports the key of type 002 under the key of the same type (TMK / TPK / PVK). The HC command, except the command code requires the TMK key.

So, before use HC first generate the key of type 002 using KG console command or simply try the exaple command below:
HCU3F731898ABAB31614E2B108E1B366461
It should work for you if you are using default LMK provided with simulator.

Regards,
Juris
Dec 29, 2014 at 2:45 PM
Hi Juris,

I appreciate your effort for the help.
Sadly I am still unable to proceed :(. The thing is, I am unable to run any command on HSM client.
Let's say a command event A2 - this is described to generate a random component and print it.

From java, if I want to run this command, I do not know in which format to send the command.

If write A2 as byte stream after opeing a TCP/IP port to HSM, my control never returns back. I have even tried converting the command to hexadecimal and writing it
to the output stream but no luck. If there is any online material I can refer to, please share it.

Below is the code

package thales;

import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.net.Socket;
public class ThalesSimulatorClient {
public static void main(String [] args){
    try{
        ThalesSimulatorClient simulatorclient = new ThalesSimulatorClient();

        Socket socket = new Socket("localhost",9998);
        System.out.println("Socket is connected : "+socket.isConnected());
        String command = simulatorclient.toHex("A2");
        byte[] bytes = command.getBytes();
        OutputStream outputStream = socket.getOutputStream();
        BufferedOutputStream bufferedOut = new BufferedOutputStream(outputStream, 1024);
        bufferedOut.write(bytes);
        bufferedOut.flush();
        InputStream in = socket.getInputStream();
        int result;
        while((result = in.read()) != -1){
                System.out.println((char)result);
        }
        socket.close();
    }
    catch(IOException ioException){
        System.err.println("unable to connect to socket 9997");
    }
}
public String toHex(String arg) {
        String hexString =  String.format("%016x", new BigInteger(1, arg.getBytes()));
    return hexString;
}
}

Thanks,
Amrutansu
Editor
Jan 19, 2015 at 2:08 AM
Hi Amrutansu,

Sorry for long silence,

You are forgetting to add software header (message length) to command, OutputStream do not add it itself.

If you want to use OutputStream just add the length of the command before it as 2 bytes, like in example below:
    private static byte[] length2byte (int len) {
        byte[] b = new byte[2];
        b[0] = (byte)(len / 256);
        b[1] = (byte)(len % 256);
        return b;
    }
The method abowe returns the 2 byes containing the length of message. For example, if you need to send to HSM 0001NC the commmand itself should look like \00\060001NC. With '\' I have escaped binary data.

That should work for you.

Regards,
Juris