Closed linhongz closed 7 months ago
Change BEGIN RSA PUBLIC KEY
to BEGIN PUBLIC KEY
and same with the END
.
There are two popular binary formats for RSA public keys: the PKCS1 format that's specifically about for keys, and the SubjectPublicKeyInfo format that can encode other key types such as ECC. Mbed TLS supports both transparently for DER. For PEM, you need to have the correct BEGIN/END descriptor.
This also happens with RSA private keys (PKCS1 vs PKCS8) and ECC private key (SEC1 vs PKCS8). Transparent for DER parsing, but a PEM file needs to have the correct header.
Did some popular tool produce that PEM encoding with a mismatched header? We could make the Mbed TLS parsing code sloppier, I don't think it would be harmful except for making the code messier, but I don't remember this being requested before.
Change
BEGIN RSA PUBLIC KEY
toBEGIN PUBLIC KEY
and same with theEND
.There are two popular binary formats for RSA public keys: the PKCS1 format that's specifically about for keys, and the SubjectPublicKeyInfo format that can encode other key types such as ECC. Mbed TLS supports both transparently for DER. For PEM, you need to have the correct BEGIN/END descriptor.
This also happens with RSA private keys (PKCS1 vs PKCS8) and ECC private key (SEC1 vs PKCS8). Transparent for DER parsing, but a PEM file needs to have the correct header.
Did some popular tool produce that PEM encoding with a mismatched header? We could make the Mbed TLS parsing code sloppier, I don't think it would be harmful except for making the code messier, but I don't remember this being requested before.
Sorry for the oversight. I manually edited the key header based on what I saw in the code. After changing it to the standard PEM format, importing it worked fine. Additionally, regarding storing PEM format in an array, are there any special requirements for newline characters like '\r', '\n' or spaces?
Summary
I stored the public key in PEM format inside an array in ROM, then attempted to parse it using
mbedtls_pk_parse_public_key
, but it failed.System information
Mbed TLS version: 3.1.0 Operating system and version: ARM Cortex-M4F bare metal Configuration:
Compiler and options: ARMCC
Steps to reproduce
The following is my minimal implementation:
mbedtls_pk_parse_public_key
-->>pk_get_rsapubkey
-->>Here is where the function ultimately fails to execute. Could it be that the format of the array where I stored the keys is incorrect? The key pair was generated by mbedTLS on the platform I'm using. I also tried using key pairs generated by OpenSSL, but encountered the same issue
Additional information
private key: