Reputation: 47
Could anyone help me find out why I get a -16000 (bad input data) on attempt to parse a public/private key from a unsigned char*?
Here's my code (edited for brevity):
DataPoint* GetPublicKey(mbedtls_pk_context* pppctx)
{
unsigned char* PKey = new unsigned char[16000];
if (mbedtls_pk_write_pubkey_pem(pppctx, PKey, 16000) != 0)
{
delete[] PKey;
return NULL;
}
DataPoint* Out = new DataPoint(strlen((char*)PKey) + 1); //Initializes an internal unsigned char* and size_t with the length of the key and the null byte
memcpy(Out->Data, PKey, Out->Length);
delete[] PKey;
return Out;
}
void GenRSA(mbedtls_rsa_context* rs)
{
mbedtls_rsa_gen_key(rs, mbedtls_ctr_drbg_random, &dctx, 2048, 65537);
}
int main()
{
mbedtls_pk_context pctx;
mbedtls_pk_init(&pctx);
mbedtls_pk_setup(&pctx, mbedtls_pk_info_from_type(MBEDTLS_PK_RSA));
DataPoint* Key = GetPublicKey(&some_context_with_GenRSA_called);
cout << mbedtls_pk_parse_public_key(&pctx, Key->Data, Key->Length) << endl; //Returns -16000
return 0
}
And the same thing with the private key, what am I doing wrong?
Upvotes: 0
Views: 630
Reputation: 1614
The docs for mbedtls_pk_parse_public_key say:
On entry, ctx must be empty, either freshly initialised with mbedtls_pk_init() or reset with mbedtls_pk_free().
Your pseudo-code calls mbedtls_pk_setup
on pctx
. Perhaps this is the problem?
Can you check with other converters such as https://superdry.apphb.com/tools/online-rsa-key-converter to see if they can parse your PEM?
Upvotes: 1