Skip to main content
lukin
Associate III
November 14, 2023
Solved

Decoder of ASN.1 format to read RSA public key

  • November 14, 2023
  • 4 replies
  • 4840 views

Hello,

I am using the STM32 Cryptolib. I need to get the modulus and exponent of RSA public key from a .pub or .pem files. After decoded from base64 I need to decode the DER format (in ASN.1 format). Has anyone any suggestion of which libray may I use? Cryptolib only accept modulus and exponent has two parameters. 

This topic has been closed for replies.
Best answer by lukin

Hello everybody,

I solve by using wolfSSL library, which is available in STM32CubeIDE already. It allows to read pem public key and certificates. It allows to check certificate chain as well.

4 replies

ST Employee
November 14, 2023

Hello @lukin ,

can you explain more what is the type of conversion you will need to do and how you generated the keys in the first place ?

meanwhile i suggest you do key formatting with a python script to give as an input for the cryptolib accepted format this is explained in this thread .

BR 

lukin
lukinAuthor
Associate III
November 14, 2023

Hi, I need to read PEM file, generated externally, which contain a RSA public key. I should extract the modulus and exponent in order to pass them to crypto library so to verify a signed file. I see openssl is a typical lib but size of the library is big. Any other way to retrieve this information?

Thank you 

ST Employee
November 15, 2023

Hello ,

do you need to do the extraction in the MCU or it can be done externally ? because if you can do this on your computer it can be done using python and then it can be feed to you code in C formatted table in hex representation .

BR

Visitor II
November 14, 2023
ST Employee
November 20, 2023

Hello @lukin ,

in this case i recommend you use opensll library to decode the RSA key stored into you SD card .

This can be done by :

  • reading the key stored in the SD card using FatFS 
  • decoding the buffer containing the RSA key with d2i_RSAPublicKey() function from openssl
  • use the RSA_get0_key() function to separate it to public and private exponents and the modulus 

you will find in the attachment and example if this use-case hope this solves you problem.

BR 

lukin
lukinAuthor
Associate III
November 22, 2023

Hi, thank you. I then preferred to use wolfSSL. It should be lighter than openssl. Isn't it?

 

ST Employee
November 22, 2023

Hello @lukin 

Indeed WolfSSL is lighter than OpenSSL  and it's more suited for your application if you are concerned about memory usage . this is explained in the following link . OpenSSL has the edge when it comes to the robustness of the solution 

because OpenSSL has a longer track record and a larger community of developers, which means it has been more extensively tested and audited.

The are basically the same in term of usage .

BR

lukin
lukinAuthorBest answer
Associate III
November 22, 2023

Hello everybody,

I solve by using wolfSSL library, which is available in STM32CubeIDE already. It allows to read pem public key and certificates. It allows to check certificate chain as well.