Page 1 of 1

Send data to socket from Java Card

Posted: Thu Dec 30, 2021 7:01 am
by vicoandrej
Trying to build solution that allows my Java Card send data via socket. Can Java Card communicate via TCP in general? I have function that can run in Java and trying to convert it to Java Card. I know String is not available in Java Card. What other things I should consider about?

Code: Select all

  public void sendSocket()
   {
        String hostname = "localhost";
        int port = 6789;
        
        Socket clientSocket = null;  
            DataOutputStream os = null;
            BufferedReader is = null;
            
            try {
                clientSocket = new Socket(hostname, port);
                os = new DataOutputStream(clientSocket.getOutputStream());
                is = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
            } catch (UnknownHostException e) {
                System.err.println("Don't know about host: " + hostname);
            } catch (IOException e) {
                System.err.println("Couldn't get I/O for the connection to: " + hostname);
            }
            
            
            if (clientSocket == null || os == null || is == null) {
            System.err.println( "Something is wrong. One variable is null." );
            return;
        }
        
        
         try {
            os.writeBytes(  "aaa" );
            os.close();
            is.close();
            clientSocket.close(); 
            
        } catch (UnknownHostException e) {
            System.err.println("Trying to connect to unknown host: " + e);
        } catch (IOException e) {
            System.err.println("IOException:  " + e);
        }
              
            
   }

Re: Send data to socket from Java Card

Posted: Sun Jan 16, 2022 2:10 pm
by mbsysde99
I also have same problem with you :roll:

Re: Send data to socket from Java Card

Posted: Fri Jan 21, 2022 12:03 pm
by vicoandrej
Looks like it is not possible from JavaCard. They definitely don't have String and looks are not able to communicate on network.

Re: Send data to socket from Java Card

Posted: Fri Jan 21, 2022 12:46 pm
by mbsysde99
As far as I know, you can send Apdu from the HOST to JCIDE via socket. The goal is to SIMULATE communication between the applet running on JCIDE (as virtual card) and the HOST (as reader). To debug the applet and host programs, and make sure that they both work the way you want, before you plug the applet into a real card, and the host connects to a real reader.

Look HOST at https://www.javacardos.com/javacardforu ... cide#p6038


But In REAL CARD, communication between CARD and READER is via CONTACT CARD (ICC/integrated circuit card) or CONTACTLESS CARD (NFC/Near Field Communication) with APDU command.

Re: Send data to socket from Java Card

Posted: Mon Jan 24, 2022 7:18 am
by PeteSmithSIMs
Hi.

The application that I'm working on does this, but it uses the mobile equipment to do the actual communication, and talks to it via PDUs.

The setup code that I've got is below. It's generic, and may not be valid Javacard code, but hopefully will give some pointers.

byte[] parameters = trCommsBuffer; // This buffer shouldn't be in use for anything at this stage
short idx = 0;
byte lgth;

parameters[idx++] = (short) 2; // default bearer

// APN1
short apnIdx = getIndexOfLV(BipConstants.LVIDX_APN1);
final short apnLength = apnName.length();
parameters[idx++] = (byte) apnLength;
idx = Util.arrayCopyNonAtomic(apnName, 0, parameters, idx, apnLength);

// Specify transport level
parameters[idx++] = (byte) 0x02; // TCP
parameters[idx++] = IDX_CFG_PORT_HI;
parameters[idx++] = IDX_CFG_PORT_LO;

// Specify destination address
short configIdx = getIndexOfLV(BipConstants.LVIDX_DEST);
lgth = destination.length()
parameters[idx++] = (byte) (lgth + 1);
parameters[idx++] = (lgth == (byte) 4) ? (byte) 0x21 : (byte) 0x57;
Util.arrayCopyNonAtomic(destination, 0, parameters, idx, lgth);

byte commandResult = ProactiveRequests.openChannel(parameters);

We then start off with

// Open a channel with details as 3== auto reconnect, immediate connect
ProactiveHandler proHdlr = proHandlerInit(
(byte) 0x40,
(byte) 0x01, // Immediate connect; no automatic reconnection; no background mode ETSI 102.223 8.6
(byte) ); // 0x82DEV_ID_TERMINAL

We then append TLV data defining bearer, buffer size, APN, transport level and destination host from the parameters in the 1st code block. The Proactive handler then sends this data to the handset to open a connection, and send the data

The way we send the code is (because String isn't a thing) is to have byte arrays...

private static final byte[] cs_http_10 = { ' ', 'H', 'T', 'T', 'P', '/', '1', '.', '0', 0x0d, 0x0a };

We then have an output buffer byte array, and copy the HTTP header in, along with the POST/GET (again, byte arrays),and then the information as a BASE64 encoded REST parameter.

Hope this is of some use. I can't give the full source code, as it's commercially sensitive, but this might be enough to point you in the right direction. I believe that the Proactive Handler Init with 0x40 is the most helpful part.

The problem I have currently is similar, but I need to send the TCP data to the handset itself, rather than an external host. I'm hoping I can run a local web-server, and just change the destination address to 127.0.0.1, and it'll just work.

Re: Send data to socket from Java Card

Posted: Tue Jan 25, 2022 12:24 pm
by vicoandrej
What is ProactiveRequests in this code?

Re: Send data to socket from Java Card

Posted: Tue Jan 25, 2022 12:25 pm
by vicoandrej
PeteSmithSIMs wrote:
Mon Jan 24, 2022 7:18 am
byte commandResult = ProactiveRequests.openChannel(parameters);

What is ProactiveRequests in this code?

Re: Send data to socket from Java Card

Posted: Tue Jan 25, 2022 4:56 pm
by PeteSmithSIMs
Hi.

Sorry, I didn't make it clear.

ProactiveRequests is one of our classes.

ProactiveRequest.open is the method which contains....

// Open a channel with details as 3== auto reconnect, immediate connect
ProactiveHandler proHdlr = proHandlerInit(
(byte) 0x40,
(byte) 0x01, // Immediate connect; no automatic reconnection; no background mode ETSI 102.223 8.6
(byte) ); // 0x82DEV_ID_TERMINAL

Hope this helps. If it doesn't, just reply and I'll help if I can.

Pete.