Thales HSM Import Public Key (EO) error '04'

596 views Asked by At

I am sending this command with a DER encoded public RSA key.

HSM-EO023082018A02820181009B1EA09942164A293536A18E3881A981161BC9147548DBADFA7A3D6F7C0425331427E431A25A1EED0E8BE184AFBE0F1A0CF9F08F731F2148560ED2E9740EDF50CA823BC1A1210A82D71820E6C2DC39B871F7EE170D07CAAAC5CB7E074829B2D0AFB1AF5DE35222EE425CCF3122F5BAA0521FCEF97198102A02CAC7F85FE15785D6731674779E485448FCF934C331C5C6CDA70C31971E09D797EE340528896029AA8434100C0F732FFF7DF5DC7EED975D56E58E30A505D7B9C299B4109EC9D353D3234DA99C3DD4B5030A4F66290BAE13EA0618E11F43BF8B283C1B1A9865E551080E15F60642E15B452626ABBCB8B59E96471CFDD819D14C74C86DF3CD245EBE85D45A3EB19A1CA2CDEF133809A6079ED5C2DFD83F9419897D8064ABE119D4EC7BD01AC9DA0A8E18EADEA9AED624F63FB39DD167C4230C1745FB12A8117B2B9B630206FA7285CA804E28FBB47A013DC17880ACF58B6F3A1ADA6A7A5B3B47CD88C60C3DAB6122C1AB5571C2586BF93FAF96657FCDEDB3F766DB06E804F4FE8C890203010001~%#B00N00

his is just the header HSM-, the command EO and the 02 denoting "DER encoding for ASN.1 Public Key (INTEGER using unsigned representation)". Followed by the encoded DER itself and then the Key block header ~%#B00N00.

Error 04 means "Public key does not conform to encoding rules".

This is a example obtained via Thales ( HSM supplier) and the only diference is they are using hsm terminal, and my implemententation is via Java sending the message via Socket.

If anyone know something please help.

In advice, they inform me is the public Key is in ASCII and i have to convert to binary Data

1

There are 1 answers

2
Eduardo Faustino On

Had the a similar issue with some other commands. I had to convert all characters excluding the public key from ASCII to hex. Then from there I pulled it all together and did a conversion from hex to ASCII. So your command code EO looks like this 454f Encoding rule 02 looks like 3032

This little method helped me with the conversion

public static String hexToAscii(String input) {
    StringBuilder result = new StringBuilder();
    for (int i = 0; i < input.length(); i += 2) {
        String str = input.substring(i, i + 2);
        result.append((char) Integer.parseInt(str, 16));
    }
    return result.toString();
}

There is probably a smarter way of doing this but it worked for me.