HSM Communication - FSDMSG

564 views
Skip to first unread message

Foly

unread,
May 25, 2021, 3:13:11 PM5/25/21
to jPOS Users
I implemented some code snippets to connect to Thales HSM to encrypt a clear Pin ("BA") and supply the resultant value to generate a pin offset ("DE").

However, the HSM responds without any error code or command code error but no Offset is returned in the response.
I will be glad to have some clarity on what can be wrong as I have gone through the HSM programming guide and a few other materials with no solution.
Implementation details can be found below:

*********************************
Implementation Trace
********************************
Connected to HSM
command being executed  == BA
Message sent to HSM<fsdmsg schema='file:/usr/app/testinghsm/hsm-base'>
Message sent to HSM  command: 'BA'
Message sent to HSM  PIN: '1234'
Message sent to HSM  Account-Number: '010020886367'
Message sent to HSM</fsdmsg>
Diconnected to HSM
BA Command Dump -  <fsdmsg schema='file:/usr/app/testinghsm/hsm-resp-base'>
BA Command Dump -    response: 'BB'
BA Command Dump -    command: 'BA'
BA Command Dump -    Command code: '  '
BA Command Dump -    PIN: '1215'
BA Command Dump -    EOF: 'true'
BA Command Dump -  </fsdmsg>
Base path in req object::: file:/usr/app/testinghsm/hsm-
My Original buffer string == DE
Response Message Path == file:/usr/app/testinghsm/hsm-resp-
Inside command method >>>>
command being executed  == DE
My buffer string == DF
IsConnected == false
Connected to HSM
command being executed  == DE
Message sent to HSM<fsdmsg schema='file:/usr/app/testinghsm/hsm-base'>
Message sent to HSM  command: 'DE'
Message sent to HSM  PVK: '7674DF79DFB7FDFB'
Message sent to HSM  PIN under LMK: '1215'
Message sent to HSM  Check Length: '04'
Message sent to HSM  Account Number: '010020886367'
Message sent to HSM  Decimalisation Table: '0123456789012345'
Message sent to HSM  PIN Validation Data: '******0208N6'
Message sent to HSM</fsdmsg>
Diconnected to HSM
MasterCard PinOffset Calculation - <fsdmsg schema='file:/usr/app/testinghsm/hsm-resp-base'>
MasterCard PinOffset Calculation -   response: 'DF'
MasterCard PinOffset Calculation -   command: 'DE'
MasterCard PinOffset Calculation -   Command code: '  '
MasterCard PinOffset Calculation -   EOF: 'true'
MasterCard PinOffset Calculation - </fsdmsg>

************************************
ThalesAdapter.java
************************************

package com.hsmtest.hsm.operations;


import java.io.IOException;


import org.jpos.core.SimpleConfiguration;

import org.jpos.iso.FSDISOMsg;

import org.jpos.iso.ISOUtil;

import org.jpos.iso.channel.FSDChannel;

import org.jpos.iso.packager.DummyPackager;

import org.jpos.util.FSDMsg;

import org.jpos.util.LogEvent;

import org.jpos.util.Logger;


/**

 * @author Foly

 *

 */

public class ThalesAdapter {



    private static final String PREFIX = "src/test/resources/";

    

    /*Assign the class name to the logger */

    static Logger log = Logger.getLogger("ThalesAdapter.class");

    

private boolean trace  = true;

FSDChannel channel = null;

    

/**

*/

public ThalesAdapter(String host, int port) {


channel = new FSDChannel();

channel.setHost(host);

channel.setPort(port);

channel.setPackager(new DummyPackager());

        

    }

public FSDMsg diagnostics () {

    return command(createRequest ("NC"));

    }

public FSDMsg encryptPin (String pin, String acctNo) {

FSDMsg req = createRequest("BA");

req .set("PIN", pin);

req.set("Account Number", acctNo); /*The rightmost 12 digits excluding check digit */


System.out.printf ("pn %s and pan %s == " , pin, acctNo);

System.out.println();

    return command(req);

    }

public FSDMsg calculateVisaPvv (String pvk, String pin, String acctNo, String pvki) {


FSDMsg req = createRequest("DG");

    req.set("PVK Pair", pvk);

    req.set("PIN", pin); /* Encrypted Under LMK */

    req.set("Account Number", acctNo);  /*The rightmost 12 digits excluding check digit */

    req.set("PVKI", pvki);

    return command(req);

}


public FSDMsg calculateMasterCardPinOffset (String pvk, String pin, String acctNo, String chklen, String dlTable, String pinVlData) {

    FSDMsg req = createRequest("DE");

    req.set("PVK", pvk);

    req.set("PIN under LMK", pin); /* Encrypted Under LMK */

    req.set("Check Length", chklen);

    req.set("Account Number", acctNo); /*The rightmost 12 digits excluding check digit */

    req.set("Decimalisation Table",  dlTable);

    req.set("PIN Validation Data", pinVlData);

    return command(req);

}  

    

public FSDMsg createRequest (String command) {

    FSDMsg req = new FSDMsg ("file:" + PREFIX + "hsm-");

    if (command != null) {

    req.set ("command", command);

    req.setCharset(ISOUtil.CHARSET);

    }

    return req;

    }

    

    

    public FSDMsg createResponse (String response) {

    FSDMsg resp = new FSDMsg ("file:" + PREFIX + "hsm-");

    

    if (response != null)

            resp.set("response", response);


        return resp;

    }

  

    

    public synchronized FSDMsg command(FSDMsg request) {

    LogEvent evt =new LogEvent();

    FSDMsg resp;

    

    StringBuffer sbuffer = new StringBuffer(request.get("command"));

    //System.out.println("My Original buffer string == " + sbuffer.toString());

        sbuffer.setCharAt(1, (char) (sbuffer.charAt(1) + 1));


        

        resp = createResponse(sbuffer.toString());

        System.out.println("Response Message Path == " + resp.getBasePath());

        System.out.println("command being executed  == " + request.get("command"));


         

    try {

    String s = request.pack();

    if (s != null) {

    /* Check if channel is not connected. If no, connect */

         if (!isConnected()) connect();


         FSDISOMsg isomsg = new FSDISOMsg(request);

                 isomsg.setHeader(new byte[] { (byte) 0, (byte) 0, (byte) 0, (byte) 0});

                 

         evt.addMessage ("Sending Message to HSM >>> ");

         

         System.out.println("command being executed  == " + request.get("command"));

         

                 SimpleConfiguration cfg = new SimpleConfiguration();

                 cfg.put("schema", request.getBasePath());

     channel.setConfiguration(cfg);

     

         channel.send(isomsg);

         FSDISOMsg respIsomsg = (FSDISOMsg) channel.receive();

         

         

         evt.addMessage ("****** Message received from HSM before merge operation ******");

         evt.addMessage (respIsomsg);

         evt.addMessage ("************************** End  *******************************");

         

         resp.merge(respIsomsg.getFSDMsg());


    if (trace)

     evt.addMessage (request);

     } else {

                    if (trace)

                        evt.addMessage ("*****Request is null- Unable to pack request.*****");

                    evt.addMessage (request);

             }

    disconnect(); /* Disconnect from channel */

    } catch (Exception e) {

    evt.addMessage(e.getStackTrace());

    } finally {

    Logger.log (evt);

    }

    return resp;

    }



    public void connect() throws IOException {

        channel.connect();

    }



    public boolean isConnected()  {

        return channel.isConnected();


    }


    public void disconnect() throws IOException {

        channel.disconnect();     

    }


}

*************************************

**************************************

package com.hsmtest.hsm.operations;


import org.jpos.util.FSDMsg;

import org.jpos.util.LogEvent;

import org.jpos.util.Logger;


/**

 * @author Foly

 *

 */

public class HsmComPvv {

private static final String PREFIX = "src/test/resources/";

   

    /*Assign the class name to the logger */

    static Logger log = Logger.getLogger("HsmComPvv.class");

   

private static boolean trace  = true;

   

       

/**

* @param args

* @throws InterruptedException 

*/

public static void main(String[] args) throws InterruptedException {


testCalculatePVVviaHSM ();

}



    

    public static void testCalculatePVVviaHSM () {

   

    LogEvent evt =new LogEvent(); 

    try {

   

    /* Extract the rightmost 12 digits of the PAN excluding check digit */

    String mpan = "5*****0208863679";

    String pinValidationData = mpan.substring(0, 10) + "N" + (mpan.length()-10);

    mpan = mpan.substring(0, mpan.length()-1);

    int startIndex = mpan.length() - 12;

    String mAccountNumber = mpan.substring(startIndex, mpan.length());


   

    System.out.println("12 Digit MasterCard Account Number >>>  " + mAccountNumber);

   

    /* Extract the rightmost 12 digits of the PAN excluding check digit */

    String vpan = "4*****0220759492";

    vpan = vpan.substring(0, vpan.length()-1);

    startIndex = vpan.length() - 12;

    String vAccountNumber = vpan.substring(startIndex, vpan.length());

   

    System.out.println("12 Digit Visa Account Number >>>  " + vAccountNumber);

   

    FSDMsg respGetFileds = new FSDMsg ("file:" + PREFIX + "hsm-");

    

    /* Ping Thales HSM with "NC" command and record response 

    * before making actual HSM function 

    * call to calculate PVV

    */

   

    ThalesAdapter thales = new ThalesAdapter("10.4.139.9", 9990);

    /* Perform HSM diagnostic test to return LMK check digit & HSM firmware number */

    FSDMsg responseMsg = thales.diagnostics();

    if (trace) evt.addMessage (responseMsg); 

   

    responseMsg.dump(System.out, "NC Command Dump -  ");

   

    System.out.println ("responseMsg Path +++++++ " + responseMsg.getBasePath());

   

    /*respGetFileds.unpack(responseMsg.pack().getBytes());

      System.out.println ("Hmmm....");

          System.out.println ("  123&&&&&&&&  " + respGetFileds.get("response"));*/

         

          responseMsg.dump(System.out, "NC Command Dump -  ");

   

   

    /* Next Step: Call HSM to Encrypt MasterCard PIN */

FSDMsg resp = thales.encryptPin ("1234", mAccountNumber);

if (trace) evt.addMessage (resp);


/* Retrieve error code from response received. 

* If "00", then all is ok. Retrieve encrypted pin*/

         

          resp.dump(System.out, "MC BA Command Dump -  ");

         


    /* Next Step: Call HSM to Encrypt Visa PIN */

resp = thales.encryptPin ("1234", vAccountNumber);

if (trace) evt.addMessage (resp);

/* Retrieve error code from response received. 

* If "00", then all is ok. Retrieve encrypted pin*/

resp.dump(System.out, "Visa BA Command Dump ---   "); 

 

/* Next Step: Call HSM to generate VISA card PVV */

    resp = thales.calculateVisaPvv ("0D0A7C61FB7E7987" + "0A2992B6DBE1A7ED", "6224", vAccountNumber, "1");

    if (trace) evt.addMessage (resp);

   

    /* Retrieve error code from response received. If "00", then all is ok */

    resp.dump(System.out, "Visa Calculation - ");

   

    /* Next Step: Call HSM to generate MasterCard PVV */

    resp = thales.calculateMasterCardPinOffset("7674DF79DFB7FDFB", "1234", mAccountNumber, "04", "0123456789012345", pinValidationData);

    if (trace) evt.addMessage (resp);

   

   

    /* Retrieve error code from response received. If "00", then all is ok */

    resp.dump(System.out, "MasterCard PinOffset Calculation - ");

   

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

} finally {

Logger.log (evt);

}

   

    }


}


******************************

BA, BB, DE & DF XML files

*******************************

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

<schema id='BA'>

<field id="Command code" type="A" length="2" />

<field id="PIN" type="H" length="4" />

<field id="Account Number" type="N" length="12" />

</schema>


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

<schema id='BB'>

<field id="Response code" type="A" length="2" />

<field id="Error code" type="N" length="2" />

<field id="PIN" type="N" length="16" />

<!-- LN for PIN / -->

</schema>


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

<schema id='DE'>

<field id="Command code" type="A" length="2" />

<!-- field id="Tpk" / -->

<field id="PVK" type="A" length="16" />

<!-- field id="PIN" type="H" length="8"  LN or LH/ -->

<field id="PIN-Under-LMK" type="N" length="4" />

<field id="Check length" type="N" length="2" />

<field id="Account Number" type="N" length="12" />

<field id="Decimalisation Table" type="N" length="16" />

<field id="PIN validation data" type="A" length="12" />

</schema>


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

<schema id='DF'>

<field id="Response code" type="A" length="2" />

<field id="Error code" type="N" length="2" />

<field id="Offset" type="N" length="12" />

</schema>



Mark Salter

unread,
May 25, 2021, 3:26:29 PM5/25/21
to jpos-...@googlegroups.com
I don't have the Thales manual to hand, but you need to check it.

What do expect to encrypt the Pin under, I don't see any key in your request?

--
Mark


Sent from ProtonMail mobile



-------- Original Message --------
--
--
jPOS is licensed under AGPL - free for community usage for your open-source project. Licenses are also available for commercial usage. Please support jPOS, contact: sa...@jpos.org
---
You received this message because you are subscribed to the Google Groups "jPOS Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jpos-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jpos-users/1a901f5b-694d-4b78-9d39-99e263b0e7aen%40googlegroups.com.
publickey - EmailAddress(s=marksalter@pm.me) - 0x929AF469.asc
signature.asc

Foly

unread,
May 25, 2021, 4:05:22 PM5/25/21
to jPOS Users
Encrypted value Under LMK and I get a response for the value under LMK as shown below:

command being executed == BA
Message sent to HSM<fsdmsg schema='file:/usr/app/testinghsm/hsm-base'>
Message sent to HSM command: 'BA'
Message sent to HSM PIN: '1234'
Message sent to HSM Account-Number: '010020886367'
Message sent to HSM</fsdmsg>
Response received from HSM ***********<fsdmsg schema='file:/usr/app/testinghsm/hsm-base'>
Response received from HSM *********** command: 'BA'
Response received from HSM *********** Command code: ' '
Response received from HSM *********** PIN: '1215'
Response received from HSM *********** EOF: 'true'
Response received from HSM ***********</fsdmsg>

Manuel Alcala

unread,
May 25, 2021, 6:29:43 PM5/25/21
to jpos-...@googlegroups.com
Hi 

Is your command configured with the correct header length?

Regards



--
Manuel Alcalá Contreras


Autodesk Inventor Certified Professional
Instructor Autorizado ATC
Inventor / Revit RST / Revit MEP / AutoCAD Plant 3D / AutoCAD P&ID / Navisworks
CODIGO INSTRUCTOR ATC: 30240
CELULAR: 999899215
RPM: #999899215



Foly

unread,
May 26, 2021, 8:33:02 AM5/26/21
to jPOS Users
I have the Thales manual and went through it.
To Encrypt a clear pin under the LMK you need no key.
The clear pin Encrypt worked as shown in the trace below. It returned the encrypted Pin under the LMK.
It's the "DE" request to get the Pin Offset that did not return the offset nor show any error. That is exactly my issue
See the trace below:
On Tuesday, 25 May 2021 at 20:26:29 UTC+1 marks...@pm.me wrote:

Foly

unread,
May 26, 2021, 8:35:12 AM5/26/21
to jPOS Users
Yes. See the implementation as part of the code sample I sent across.

isomsg.setHeader(new byte[] { (byte) 0, (byte) 0, (byte) 0, (byte) 0});


Foly

unread,
Jun 1, 2021, 5:43:08 PM6/1/21
to jPOS Users
I figured out some occurrence with my implementation as follows:
1) The HSM expects the 'Msg Body' to be set with the entire Hex String of the command request. Please see sample 'NC' request and 'ND' response below.
2) For the NC command, when I dumped the message received on the channel, the message body Hex string is not in the message dump. I also attempted to add 'Msg body' to my hsm-base.xml and hsm-resp-base.xml but it did not work.
3) I noticed that all input parameters set in the 'BA' command were not sent/received by the HSM interface as it expects them as Hex String in 'Msg Body'  to be set for the HSM. Please see sample 'BA' request && BB response below with HSM responding with error 15 (Input error). 
4) My Request/Clarification
 Is there a way to get the 'Msg body' Hex String passed in my request as expected by the HSM from within FSD Channel implementation?
*******************************************************************
***** My JPOS Code Trace for NC Command *********************
*******************************************************************
Message sent to HSM<fsdmsg schema='file:/usr/app/testinghsm/hsm-base'>
Message sent to HSM  command: 'NC'
Message sent to HSM</fsdmsg>
Response received from HSM ***********<fsdmsg schema='file:/usr/app/testinghsm/hsm-resp-base'>
Response received from HSM ***********  header: '00000000'
Response received from HSM ***********  response: 'ND'
Response received from HSM ***********  error: '00'
Response received from HSM ***********</fsdmsg>
*******************************************************************
********* HSM Interface Trace for NC  & ND Response ***********
*******************************************************************
[Jun 01 18h38:25.906] - [NC] Tx Request to: Test HSM at 10.4.139.9,9990 
[None       an  008 M] : 'Message Header' = [28110000]
[None       an  002 M] : 'Command Code' = [NC]
[VAR:..ETX  b   096 O] : 'Msg body' = <not set>
binary data 
0000(0000)  32 38 31 31 30 30 30 30  4e 43                     28110000NC
--------------------------------------------------------------------------------
[Jun 01 18h38:25.922] - [ND] Rx Response from: Test HSM at 10.4.139.9,9990 
[None       an  008 M] : 'Message Header' = [28110000]
[None       an  002 M] : 'Response Code' = [ND]
[None       an  002 M] : 'Error Code' = [00]
[VAR:..ETX  b   096 O] : 'Msg body' = *[37333433353630303030303030303030313236372D30393038]
binary data 
0000(0000)  32 38 31 31 30 30 30 30  4e 44 30 30 37 33 34 33   28110000ND007343
0016(0010)  35 36 30 30 30 30 30 30  30 30 30 30 31 32 36 37   5600000000001267
0032(0020)  2d 30 39 30 38                                     -0908

***************************************************************************
***** My JPOS Code Trace for BA Command ***********
***************************************************************************
Message sent to HSM<fsdmsg schema='file:/usr/app/testinghsm/hsm-base'>
Message sent to HSM  command: 'BA'
Message sent to HSM  pin: '1234FFFFFFFFF'
Message sent to HSM  account-number: '010020886367'
Message sent to HSM</fsdmsg>
Response received from HSM ***********<fsdmsg schema='file:/usr/app/testinghsm/hsm-resp-base'>
Response received from HSM ***********  header: '00000000'
Response received from HSM ***********  response: 'BB'
Response received from HSM ***********  error: '15'
Response received from HSM ***********</fsdmsg>
*******************************************************************
********* HSM Interface Trace for BA  & BB Response ***********
*******************************************************************
[Jun 01 18h38:25.938] - [BA] Tx Request to: Test HSM at 10.4.139.9,9990 
[None       an  008 M] : 'Message Header' = [28120000]
[None       an  002 M] : 'Command Code' = [BA]
[VAR:..ETX  b   096 O] : 'Msg body' = <not set>
binary data 
0000(0000)  32 38 31 32 30 30 30 30  42 41                     28120000BA
--------------------------------------------------------------------------------
[Jun 01 18h38:25.938] - [BB] Rx Response from: Test HSM at 10.4.139.9,9990 
[None       an  008 M] : 'Message Header' = [28120000]
[None       an  002 M] : 'Response Code' = [BB]
[None       an  002 M] : 'Error Code' = [15]
binary data 
0000(0000)  32 38 31 32 30 30 30 30  42 42 31 35               28120000BB15

Mark Salter

unread,
Jun 1, 2021, 6:30:15 PM6/1/21
to jpos-...@googlegroups.com
I think you are missing some fundamentals, but it is hard to tell with the level of information you are sharing.

You need to build and set the right fields or fields to match your field definitions.

If you don't set a field to a value, it won't contain anything - as appears to be the case here.

Is your code setting a field value that is not making it out into the Message sent to the HSM?

If you share all the details in a smart question (Google 'how to ask a smart question to form one), I am sure your problem will be obvious to you.

--
Mark



-------- Original Message --------
To view this discussion on the web visit https://groups.google.com/d/msgid/jpos-users/c9de8cf4-7849-4029-8005-efe4661f4116n%40googlegroups.com.
publickey - EmailAddress(s=marksalter@pm.me) - 0x929AF469.asc
signature.asc

Foly

unread,
Jun 2, 2021, 12:26:34 AM6/2/21
to jPOS Users
Hello Mark,
     Thank you very much for your response.
Please moderate your comments if you really want to offer support.
You don't have to respond if you feel my questions are not smart. 
It's a group forum and others can assist where possible.

Meanwhile, all the fields are set and I shared the entire codebase at the beginning of the thread. I may have missed something out but can't seem to place my hands on it.
All looks ok from the review I did but can't be 100% sure and that's I am reaching out.
I am dumping the   FSDISOMsg object before sending it to the HSM just a  line before I call channel.send(isomsg) in the codebase below; and it shows all fields populated.
I also dump the response received from the channel and it does not contain the LMK check value and Firmware sent by the HSM in 'Msg body'.  That's when I became confused and also posted dump of the HSM to explain what I had noticed.
Just be clear on what you want me to provide and be sure I will do so.

****** My dump of FSDISOMsg isomsg object a line before calling channel.send(isomsg); *********
*****************************************************************************************************
Message sent to HSM<fsdmsg schema='file:/usr/app/testinghsm/hsm-base'>
Message sent to HSM  command: 'BA'
Message sent to HSM  pin: '1234FFFFFFFFF'
Message sent to HSM  account-number: '010020886367'
Message sent to HSM</fsdmsg>

Full implementation shared below again:
******** hsm-BA.xml ***********

<?xml version="1.0" encoding="UTF-8"?>
<schema id='BA'>

<field id="pin" type="H" length="5" />
<field id="account-number" type="N" length="12" />
</schema>

******** hsm-resp-BB.xml ***********

<?xml version="1.0" encoding="UTF-8"?>
<schema id='BB'>

<field id="PIN" type="N" length="5" />
</schema>

******** hsm-NC.xml ***********


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

<schema id='NC'>
</schema>

******** hsm-resp-ND.xml ***********


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

<schema id='ND'>
<field id="lmk-check-value" type="A" length="16" />
<field id="firmware-number" type="A" length="9" />
</schema>

********. Thales Adaptor Class *****


import java.io.IOException;

import org.jpos.core.SimpleConfiguration;

import org.jpos.iso.FSDISOMsg;

import org.jpos.iso.ISOUtil;

import org.jpos.iso.channel.FSDChannel;

import org.jpos.iso.packager.DummyPackager;

import org.jpos.util.FSDMsg;

import org.jpos.util.LogEvent;

import org.jpos.util.Logger;


public class ThalesAdapter {

    private static final String PREFIX = "usr/app/testinghsm/";

        /*Assign the class name to the logger */

    static Logger log = Logger.getLogger("ThalesAdapter.class");

    

private boolean trace  = true;

FSDChannel channel = null;


public ThalesAdapter(String host, int port) {


channel = new FSDChannel();

channel.setHost(host);

channel.setPort(port);

channel.setPackager(new DummyPackager());  

    }

public FSDMsg diagnostics () {

    return command(createRequest ("NC"));

    }

public FSDMsg encryptPin (String pin, String acctNo) {

FSDMsg req = createRequest("BA");

req .set("pin", pin);

req.set("account-number", acctNo); /*The rightmost 12 digits excluding check digit */

    return command(req);

    }

public FSDMsg calculateVisaPvv (String pvk, String pin, String acctNo, String pvki) {


FSDMsg req = createRequest("DG");

    req.set("PVK-Pair", pvk);

    req.set("PIN", pin); /* Encrypted Under LMK */

    req.set("Account-Number", acctNo);  /*The rightmost 12 digits excluding check digit */

    req.set("PVKI", pvki);

    return command(req);

}


public FSDMsg calculateMasterCardPinOffset (String pvk, String pin, String acctNo, String chklen, String dlTable, String pinVlData) {

    FSDMsg req = createRequest("DE");

    req.set("PVK", pvk);

    req.set("PIN", pin); /* Encrypted Under LMK */

    req.set("Check-Length", chklen);

    req.set("Account-Number", acctNo); /*The rightmost 12 digits excluding check digit */

    req.set("Decimalisation-Table",  dlTable);

    req.set("PIN-Validation-Data", pinVlData);

    return command(req);

}  

    

public FSDMsg createRequest (String command) {

    FSDMsg req = new FSDMsg ("file:" + PREFIX + "hsm-");

    if (command != null) {

    req.set ("command", command);

    req.setCharset(ISOUtil.CHARSET);

    }

    return req;

    }

 

    public FSDMsg createResponse (String response) {

    FSDMsg resp = new FSDMsg ("file:" + PREFIX + "hsm-resp-");

    if (response != null) resp.set("response", response);

        return resp;

    }


    public synchronized FSDMsg command(FSDMsg request) {

    LogEvent evt =new LogEvent();

    FSDMsg resp = null;

    StringBuffer sbuffer = new StringBuffer(request.get("command"));

        sbuffer.setCharAt(1, (char) (sbuffer.charAt(1) + 1));

        resp = createResponse(sbuffer.toString());

        System.out.println("command being executed  == " + request.get("command"));

    try {

    if (request.pack() != null) {

    /* Check if channel is not connected. If no, connect */

         if (!isConnected()) connect();

         FSDISOMsg isomsg = new FSDISOMsg(request);

         evt.addMessage ("Sending Message to HSM >>> ");   

         System.out.println("command being executed  == " + request.get("command"));

         

                 SimpleConfiguration cfg = new SimpleConfiguration();

                 cfg.put("schema", resp.getBasePath());

     channel.setConfiguration(cfg);

      isomsg.dump(System.out, "Message sennt to HSM");

         channel.send(isomsg);

         FSDISOMsg respIsomsg = (FSDISOMsg) channel.receive();

         respIsomsg.dump(System.out, "Response Received from HSM  -  ");


         evt.addMessage ("****** Message received from HSM before merge operation ******");

         evt.addMessage (respIsomsg);

         evt.addMessage ("************************** End  *******************************");


         if (trace) evt.addMessage (request);

         resp.merge(respIsomsg.getFSDMsg());


     } else {

                    if (trace)

                        evt.addMessage ("*****Request is null- Unable to pack request.*****");

                    evt.addMessage (request);

             }

    disconnect(); /* Disconnect from channel */

    } catch (Exception e) {

    evt.addMessage(e.getStackTrace());

    } finally {

    Logger.log (evt);

    }

    return resp;

    }

    public void connect() throws IOException {

        channel.connect();

    }

    public boolean isConnected()  {

        return channel.isConnected();

    }

    public void disconnect() throws IOException {

        channel.disconnect();     

    }

}


******* callHsm Call ***********

import org.jpos.util.FSDMsg;

import org.jpos.util.LogEvent;

import org.jpos.util.Logger;

public class  callHsm {

/*Assign the class name to the logger */

    static Logger log = Logger.getLogger("callHsm.class");

     private static boolean trace  = true;

/**

* @param args

* @throws InterruptedException 

*/

public static void main(String[] args) throws InterruptedException {

testCalculatePVVviaHSM ();

}    

    public static void testCalculatePVVviaHSM () {

    LogEvent evt =new LogEvent(); 

    try {

    /* Extract the rightmost 12 digits of the PAN excluding check digit */

    String mpan = "5973100208863679";

    String pinValidationData = mpan.substring(0, 10) + "N" + (mpan.length()-10);

    mpan = mpan.substring(0, mpan.length()-1);

    int startIndex = mpan.length() - 12;

    String mAccountNumber = mpan.substring(startIndex, mpan.length());

    System.out.println("12 Digit MasterCard Account Number >>>  " + mAccountNumber);


    /* Ping Thales HSM with "NC" command and record response 

    * before making actual HSM function 

    * call to calculate PVV

    */

    ThalesAdapter thales = new ThalesAdapter("10.4.139.9", 9990);

    /* Perform HSM diagnostic test to return LMK check digit & HSM firmware number */

    FSDMsg responseMsg = thales.diagnostics();

    if (trace) evt.addMessage (responseMsg); 


    //System.out.println ("responseMsg Path +++++++ " + responseMsg.getBasePath());

          responseMsg.dump(System.out, "NC Command Dump -  ");


    /* Next Step: Call HSM to Encrypt MasterCard PIN */

FSDMsg resp = thales.encryptPin ("1234F", mAccountNumber);

if (trace) evt.addMessage (resp);

/* Retrieve error code from response received. 

* If "00", then all is ok. Retrieve encrypted pin*/

          resp.dump(System.out, "MC BA Command Dump -  ");


Mark Salter

unread,
Jun 2, 2021, 2:00:46 AM6/2/21
to jpos-...@googlegroups.com
So I asked two question you didn't read or decided not to answer and guided you to some background reading that I believe will help you...

- gather the details needed to help you help yourself
- focus your question on the issue and detail needed so we can try and help

If you had tried to do as I asked, we might have moved forward, since you have not we won't for now.

Have fun.


--
Mark


Sent from ProtonMail mobile



-------- Original Message --------
To view this discussion on the web visit https://groups.google.com/d/msgid/jpos-users/49c66891-62eb-4a69-86f4-d1366db771dbn%40googlegroups.com.
publickey - EmailAddress(s=marksalter@pm.me) - 0x929AF469.asc
signature.asc
Reply all
Reply to author
Forward
0 new messages