RSA key pair generation || variable length

254 views
Skip to first unread message

Raman Verma

unread,
Aug 25, 2023, 3:37:01 AM8/25/23
to jPOS Users
Hi
I am trying to generate the RSA public/Private key using EI Thales HSM command. 

I am getting a successful response from HSM but the length of the fields is a variable length and it is not getting mapped correctly. 

The response has 3 fields as below
Public key                - nB
Private key length   -N4
private key                nB

My XML Configurations. Pls, let me know if I am doing anything wrong here. Would be a great help.
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generate an RSA Public/Private Key Pair -->
<schema id='EI'>
<field id="key-type-indicator" type="N" length="1" >4</field>
<field id="key-length" type="N" length="4" >0512</field>
<field id="public-key-encoding" type="N" length="2" >01</field>
<field id="delimiter" type="K" length="1">%</field>
<field id="lmk-identifier" type="K" length="2">00</field>
</schema>

<?xml version="1.0" encoding="UTF-8"?>
<schema id='EJ'>
<field id="public-key" type="BFS" length="512" />
<field id="private-key-length" type="N" length="4" />
<field id="private-key" type="BFS" length="2048" />
</schema>

Screenshot 2023-08-25 at 1.03.05 PM.png

Mark Salter

unread,
Aug 25, 2023, 3:41:43 AM8/25/23
to jpos-...@googlegroups.com

If you include a delimiter in the EI request, then the HSM will reply with a delimiter in EJ which you can probably use.


--

Mark

--
--
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/73dedeaa-e05a-4465-b346-ae9df3ae5515n%40googlegroups.com.
signature.asc

Raman Verma

unread,
Aug 25, 2023, 3:56:04 AM8/25/23
to jPOS Users
Adding a delimiter in the request will not work as HSM will through the error of invalid input data. I just tested this and got the invalid input error.
Maybe I am doing it wrong can you pls specify where to put the delimiter? 

<?xml version="1.0" encoding="UTF-8"?>
<!-- Generate an RSA Public/Private Key Pair -->
<schema id='EI'>
<field id="key-type-indicator" type="N" length="1" >4</field>
<field id="key-length" type="N" length="4" >0512</field>
<field id="public-key-encoding" type="N" length="2" >01</field>
<field id="delimiter" type="K" length="1">%</field>
<field id="lmk-identifier" type="K" length="2">00</field>
</schema>

Mark Salter

unread,
Aug 25, 2023, 4:00:50 AM8/25/23
to jpos-...@googlegroups.com

You need to use the right value as a delimiter!

I have not looked at or really used FSDMsg in a while, I would have code to pull these non FSD forms apart instead - sorry.

--

Mark

signature.asc

Raman Verma

unread,
Aug 25, 2023, 5:03:09 AM8/25/23
to jPOS Users
Is there anyone else who can help me here with this? 

Raman Verma

unread,
Aug 25, 2023, 7:30:59 AM8/25/23
to jPOS Users
I have tried all available ways to parse the EJ response using fsdMsg but still, it is not able to parse the response. Is there anyone who has done the implementation of EI/EJ command using fsdMsg pls revert.

Alejandro Revilla

unread,
Aug 25, 2023, 11:11:11 PM8/25/23
to jpos-...@googlegroups.com
Instead of:

<field id="private-key-length" type="N" length="4" />
<field id="private-key" type="BFS" length="2048" />

You can use just:

<field id="private-key" type="LLLLBFS" length="2048" />

The 'LLLL' here should handle the variable length field.

Please let us know if this works for you.




--

Raman Verma

unread,
Aug 28, 2023, 6:11:04 AM8/28/23
to jPOS Users
Hi Alejandro

The problem is parsing the public key for which no length is mentioned. If the public key gets parsed correctly then private key length and private key will get easily parsed. I am stuck in parsing the public key only. 

Mark Salter

unread,
Aug 28, 2023, 8:19:59 AM8/28/23
to jpos-...@googlegroups.com

Is the length of the public key fixed for a given set of inputs (length and encoding option)?

Why not share your input and output - make two runs;  I am thinking the public length you need is static - dependant on request and key length.

--

Mark

signature.asc

Raman Verma

unread,
Aug 30, 2023, 6:07:29 AM8/30/23
to jPOS Users
Hi Mark

It seems parsing this kind of field is not possible at the moment using FSDMsg. What I have done is, read all the bytes in a single field and then do the parsing as per the DER ANS.1 encoding. This seems the only way to read the fields as of now. 

Mark Salter

unread,
Aug 30, 2023, 8:10:28 AM8/30/23
to jpos-...@googlegroups.com
A quick confirmation if I may...
... is the public key fixed length or do you need to scan for marker bytes such as Manuel describe on another reply?

If you are happy to share the options on your EI, I would like to take a look; it seems like Thales might be missing a length otherwise.

-- 
Mark


Sent from Proton Mail mobile



-------- Original Message --------
To view this discussion on the web visit https://groups.google.com/d/msgid/jpos-users/86474d3a-f9d5-4ae2-b926-92627b5b3cbfn%40googlegroups.com.
signature.asc

Raman Verma

unread,
Aug 31, 2023, 1:29:58 AM8/31/23
to jPOS Users
The public key length depends on the length provided in the request and the encoding used. If I put a length of 2048 bits then 270 bytes will be the length of the public key in response. Also, the public key can be extracted using the Manuel method by looking for the public key exponent and then parse accordingly.

Request input for EI command goes as below

Key Type Indicator               1 N              Defines the intended usage of the key pair: 
                                                                             '5': PIN encryption/decryption 
Key Length                            4 N               The length of the RSA modulus, in bits: '0320' ... '2048' 
Public Key Encoding            2 N               Encoding rules for the Public Key
                                                                              '02': DER encoded ASN.1 RSA Public Key (INTEGER uses 2's complement representation). 
Public Exponent Length      4 N               Optional. Must be present if a public exponent is supplied. Indicates the length (in bits) of the public exponent. 
Public Exponent                   n B                Optional. Must be an odd value. If not supplied, a default exponent of 65537 is assumed. 
Delimiter                                1 A                Value '%'. 
LMK Identifier                       2 N                 LMK identifier; min value = '00'; max value is defined by license; must be present if the above Delimiter is present.

Mark Salter

unread,
Aug 31, 2023, 2:29:33 AM8/31/23
to jpos-...@googlegroups.com
So, then, for a given set of input options and a length of Key, the output length is known, and why there is no need for a length of data value back on the EJ?

I was interested in your EI content - if you were happy to share, if not, no matter.



-- 
Mark


Sent from Proton Mail mobile



-------- Original Message --------
To view this discussion on the web visit https://groups.google.com/d/msgid/jpos-users/4c01d47f-72d1-4a38-8a9b-6259722aabdfn%40googlegroups.com.
signature.asc

Raman Verma

unread,
Aug 31, 2023, 2:47:07 AM8/31/23
to jPOS Users
I am not getting what you mean by EI content. If you are asking for the EI message format then it is already added in the first message and I have added the EI command format in the last message. 
Below I am adding the Host command that is getting triggered.

EI5204802%00

Mark Salter

unread,
Aug 31, 2023, 3:05:09 AM8/31/23
to jpos-...@googlegroups.com
I was after your whole command data that went tonthe hsm so I could use and see what you were seeing.

Not to worry, you seem happy to use Manuel's method, so all good.



-- 
Mark


Sent from Proton Mail mobile



-------- Original Message --------
To view this discussion on the web visit https://groups.google.com/d/msgid/jpos-users/71d65bc9-041f-45ad-9888-6b7bce968e95n%40googlegroups.com.
signature.asc

Walid Zohair

unread,
Aug 31, 2023, 3:12:36 AM8/31/23
to jpos-...@googlegroups.com

Mark Salter

unread,
Aug 31, 2023, 3:15:31 AM8/31/23
to jpos-...@googlegroups.com
You are replying to my reply in a Google Groups group for jPos you have subscribed to?

--
M



-- 
Mark


Sent from Proton Mail mobile



-------- Original Message --------
To view this discussion on the web visit https://groups.google.com/d/msgid/jpos-users/CAECO%3D-QFgnRLj0AX61xUV2mtzC1K53vQvxjJkSP9%2BtgpwkxtXA%40mail.gmail.com.
signature.asc
Reply all
Reply to author
Forward
0 new messages