Parse the Subject name of a X.509 certificate to a buffer

358 views Asked by At

i'm trying to parse the subject name of a X.509 certificate into a buffer , but i'm receiving negative value. What could have been gone wrong?

   int32_t ret;
   size_t n = 0;
   uint8_t *p = NULL;
   mbedtls_x509_crt testcert;
   mbedtls_x509_crt_init(&testcert);
   if (ret = mbedtls_x509_crt_parse_file(&testcert, "testcert.pem"))
   {
     printf(" failed\n  !  mbedtls_x509_crt_parse_file returned -0x%04x\n\n", -ret);
     return ret;
   }
   ret = mbedtls_x509_dn_gets(p, n, &testcert.subject);
   if (ret <= 0)
   {
      printf("mbedtls_x509_dn_gets returned %02x", -ret);
      return ret;
   }
1

There are 1 answers

1
LIJIN T V On

I got the mistake i made in the above code snippet.

  1. I was not assigning memory for pointer p
  2. "n" is supposed to be the Maximum size of buffer

After correcting these two things i was able to get the CN parsed to a buffer.

Thank you