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

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

1234EO013082010a02820101ec7b6d6be7d0603e3f247c22dd0ae533f02f1216fd9099d6ec5c596eb92c95e8ee87e3437af53ecf9ef4ccd27b1acf28b41ff77b86d028d10c560265d8b2e61b3e57eb8a0c2593eea0815ab82871c1232b03f677342ac19c570caa8abe4f68cb35666e6fa50fef30d11ef50aadd52dd01b9c902c162e3e44b685ae4ff01802b3ce1741e5efc28f654e8fb231d06b6d028cecc51f789350bb456d9265eae82c25d987de44f747bf4312bfde87c06538ad76a6ec606f19436e67fcdb82014d3300fbd0b6bc3288d8cef6765bc82d0bb5b05b0c1f28a074003c94afd5fd5d404faf1b7ba4dc6cbd694e2fd2708b09aec36142817a1e5ee9a6d8122ff734861e54f30203010001~#N00N00

This is just the header 1234, the command EO and the 01 denoting "DER encoding for ASN.1 Public Key (INTEGER using unsigned representation)". Followed by the encoded DER itself and then the Key block header ~#N00N00.

Error 04 means "Public key does not conform to encoding rules". And below is the code used to generate the RSA keypair and then DER encoded public key:

rsa_private_key = buildRsaKeyPair()
rsa_public_key = rsa_private_key.public_key()

der = rsa_public_key.public_bytes(
   encoding=serialization.Encoding.DER,
   format=serialization.PublicFormat.PKCS1
)

encoded = der.hex()[:16] + der.hex()[18:]

encoded is what is built into the command. Can anyone see what could be wrong with the public key encoding I'm getting with the cryptography library in Python?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source