ECDSA Verify Signature - Is R & S ok?

I have the following code to verify a signature:

#define CONTRACT_LEAF_CERT                                                     \
  0x30, 0x82, 0x02, 0x31, 0x30, 0x82, 0x01, 0xd7, 0xa0, 0x03, 0x02, 0x01,      \
      0x02, 0x02, 0x02, 0x30, 0x44, 0x30, 0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48,  \
      0xce, 0x3d, 0x04, 0x03, 0x02, 0x30, 0x45, 0x31, 0x11, 0x30, 0x0f, 0x06,  \
      0x03, 0x55, 0x04, 0x03, 0x0c, 0x08, 0x4d, 0x4f, 0x53, 0x75, 0x62, 0x43,  \
      0x41, 0x32, 0x31, 0x0f, 0x30, 0x0d, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c,  \
      0x06, 0x53, 0x77, 0x69, 0x74, 0x63, 0x68, 0x31, 0x0b, 0x30, 0x09, 0x06,  \
      0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x4b, 0x31, 0x12, 0x30, 0x10,  \
      0x06, 0x0a, 0x09, 0x92, 0x26, 0x89, 0x93, 0xf2, 0x2c, 0x64, 0x01, 0x19,  \
      0x16, 0x02, 0x4d, 0x4f, 0x30, 0x1e, 0x17, 0x0d, 0x32, 0x32, 0x30, 0x39,  \
      0x30, 0x39, 0x30, 0x36, 0x33, 0x35, 0x31, 0x33, 0x5a, 0x17, 0x0d, 0x32,  \
      0x34, 0x30, 0x39, 0x30, 0x38, 0x30, 0x36, 0x33, 0x35, 0x31, 0x33, 0x5a,  \
      0x30, 0x4c, 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c,  \
      0x0f, 0x55, 0x4b, 0x53, 0x57, 0x49, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36,  \
      0x37, 0x38, 0x39, 0x41, 0x31, 0x0f, 0x30, 0x0d, 0x06, 0x03, 0x55, 0x04,  \
      0x0a, 0x0c, 0x06, 0x53, 0x77, 0x69, 0x74, 0x63, 0x68, 0x31, 0x0b, 0x30,  \
      0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x4b, 0x31, 0x12,  \
      0x30, 0x10, 0x06, 0x0a, 0x09, 0x92, 0x26, 0x89, 0x93, 0xf2, 0x2c, 0x64,  \
      0x01, 0x19, 0x16, 0x02, 0x4d, 0x4f, 0x30, 0x59, 0x30, 0x13, 0x06, 0x07,  \
      0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a, 0x86, 0x48,  \
      0xce, 0x3d, 0x03, 0x01, 0x07, 0x03, 0x42, 0x00, 0x04, 0x1a, 0xaa, 0xa8,  \
      0x09, 0x93, 0x76, 0x49, 0xb7, 0x4f, 0x36, 0x5d, 0xbb, 0x0f, 0x8c, 0xb6,  \
      0x4d, 0xa3, 0x96, 0x72, 0x63, 0xf5, 0x82, 0xcb, 0x94, 0x5c, 0x24, 0xdd,  \
      0xea, 0x7b, 0x4e, 0x84, 0xee, 0xfd, 0x3d, 0xfd, 0x0a, 0x81, 0xce, 0xb5,  \
      0x5f, 0x2c, 0x34, 0xb0, 0xe8, 0x6a, 0xe6, 0x10, 0x65, 0xec, 0xea, 0x12,  \
      0x9f, 0xcd, 0xc7, 0x42, 0xca, 0x25, 0x62, 0x4f, 0x1d, 0xbc, 0x9a, 0x13,  \
      0x7e, 0xa3, 0x81, 0xaf, 0x30, 0x81, 0xac, 0x30, 0x0c, 0x06, 0x03, 0x55,  \
      0x1d, 0x13, 0x01, 0x01, 0xff, 0x04, 0x02, 0x30, 0x00, 0x30, 0x0e, 0x06,  \
      0x03, 0x55, 0x1d, 0x0f, 0x01, 0x01, 0xff, 0x04, 0x04, 0x03, 0x02, 0x03,  \
      0xe8, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x0e, 0x04, 0x16, 0x04, 0x14,  \
      0xab, 0xd6, 0x69, 0x70, 0x04, 0x53, 0x7d, 0x8d, 0x6a, 0x5d, 0xb2, 0xbd,  \
      0xfe, 0xe6, 0x9d, 0x2a, 0x57, 0xe6, 0x68, 0xe8, 0x30, 0x6d, 0x06, 0x08,  \
      0x2b, 0x06, 0x01, 0x05, 0x05, 0x07, 0x01, 0x01, 0x04, 0x61, 0x30, 0x5f,  \
      0x30, 0x24, 0x06, 0x08, 0x2b, 0x06, 0x01, 0x05, 0x05, 0x07, 0x30, 0x01,  \
      0x86, 0x18, 0x68, 0x74, 0x74, 0x70, 0x73, 0x3a, 0x2f, 0x2f, 0x77, 0x77,  \
      0x77, 0x2e, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e, 0x63, 0x6f,  \
      0x6d, 0x2f, 0x30, 0x37, 0x06, 0x08, 0x2b, 0x06, 0x01, 0x05, 0x05, 0x07,  \
      0x30, 0x02, 0x86, 0x2b, 0x68, 0x74, 0x74, 0x70, 0x73, 0x3a, 0x2f, 0x2f,  \
      0x77, 0x77, 0x77, 0x2e, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e,  \
      0x63, 0x6f, 0x6d, 0x2f, 0x49, 0x6e, 0x74, 0x65, 0x72, 0x6d, 0x65, 0x64,  \
      0x69, 0x61, 0x74, 0x65, 0x2d, 0x43, 0x41, 0x2e, 0x63, 0x65, 0x72, 0x30,  \
      0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x04, 0x03, 0x02, 0x03,  \
      0x48, 0x00, 0x30, 0x45, 0x02, 0x21, 0x00, 0xa8, 0x20, 0xe2, 0x52, 0xd8,  \
      0xd0, 0x9f, 0x37, 0xf2, 0x6c, 0xad, 0x1a, 0x85, 0xa0, 0x30, 0xc7, 0x94,  \
      0xa4, 0x65, 0xf0, 0x1f, 0xdd, 0x78, 0x52, 0xb7, 0x12, 0x89, 0x2c, 0x2a,  \
      0x0c, 0x3b, 0x33, 0x02, 0x20, 0x3d, 0xa7, 0x3b, 0x2b, 0x83, 0xd2, 0xab,  \
      0x43, 0x82, 0xb4, 0x5a, 0x33, 0x55, 0x81, 0x47, 0xae, 0x04, 0x0a, 0x72,  \
      0xb9, 0x45, 0xf5, 0xc6, 0x20, 0xb4, 0xa0, 0x0a, 0x91, 0x23, 0xdf, 0x77,  \
      0x50

#define SIGNATURE_3 \
 0xa9, 0xa5, 0x51, 0x0c, 0x48, 0xee, 0x57, 0x2f, 0x49, 0x80, 0xfe, 0xe4, 0xb9, 0x5f, 0x2b, 0xf0, 0x51, 0x1c, 0x7b, 0x38, 0xe2, 0xc8, 0xc0, 0xb9, 0xbb, 0xb2, 0x26, 0xd3, 0x81, 0x28, 0x55, 0x77, 0x22, 0x72, 0x4f, 0x6c, 0x3b, 0x10, 0xd5, 0x64, 0x88, 0x43, 0x6f, 0x05, 0x6a, 0xae, 0x8b, 0xa6, 0xbd, 0x7d, 0x55, 0xf7, 0x42, 0x38, 0x54, 0xaa, 0x33, 0x2b, 0xfd, 0x8c, 0x50, 0x4e, 0xfa, 0x25

#define hash_sha256 \
  0x2a, 0xd5, 0xbb, 0x7d, 0x37, 0x9f, 0x0b, 0x4f, 0x09, 0x49, 0xd0, 0x9d, 0x5a, 0xb3, 0x4a, 0x1b, 0xb6, 0x0a, 0xe1, 0x15, 0xca, 0x30, 0x76, 0xbd, 0x8e, 0xae, 0xe5, 0xb8, 0x78, 0xc2, 0x8f, 0x6c

void verifySignature() {
  int ret = 0;
  unsigned char cert[] = {CONTRACT_LEAF_CERT};
  unsigned char hash[] = {hash_sha256};
  unsigned char sign[] = {SIGNATURE_3};
  size_t cert_size = sizeof(cert);
  size_t hash_size = sizeof(hash);
  size_t sign_size = sizeof(sign);

  mbedtls_x509_crt crt;
  mbedtls_x509_crt_init(&crt);
  ret = mbedtls_x509_crt_parse_der(&crt, cert, cert_size);
  ERR("ERR mbedtls_x509_crt_parse_der", ret)

  mbedtls_mpi r;
  mbedtls_mpi s;
  mbedtls_mpi_init(&r);
  mbedtls_mpi_init(&s);

  ret = mbedtls_mpi_read_binary(&r, (const unsigned  char*)sign, sign_size / 2);
  ERR("mbedtls_mpi_read_binary", ret)
  ret = mbedtls_mpi_read_binary(&s, (const unsigned  char*)sign + sign_size / 2, sign_size / 2);
  ERR("mbedtls_mpi_read_binary II", ret)

  mbedtls_ecp_group *grp = &mbedtls_pk_ec(crt.pk)->MBEDTLS_PRIVATE(grp);
  mbedtls_ecp_point *Q = &mbedtls_pk_ec(crt.pk)->MBEDTLS_PRIVATE(Q);
  ret = mbedtls_ecdsa_verify(grp, hash, sizeof(hash), Q, &r, &s);
  ERR("mbedtls_ecdsa_verify", ret)

  printf("Success!"); 
}

It is failing on the last step, mbedtls_ecdsa_verify(), with a result code 0x4E00 (The signature is not valid)

I suspect that the way of calculating R and S is not correct, I don’t understand very well these two values. I do not discard that the input data (cert, sign and hash) are not correct, but I have some certainty that they are valid.

This function is based on a message from this forum, but when loading R and S I don’t know exactly what those variables (signaturePtr and Security_Signature512_t) are.

I have spent a good number of hours on this and would greatly appreciate any help.