erlang elixir nif char* data to binary fails for OCSP request

I am trying to create OCSP request to provider with data returned from nif I want make request in this way:

HTTPoison.post!(
    oscp_info[:access],
    :unicode.characters_to_binary(oscp_info[:data], utf8),
    [{"Content-Type", "application/ocsp-request"}],
    timeout: 1000
  )

I have block of code in C that form data from c to erlang:

static ERL_NIF_TERM CreateElixirBinary(ErlNifEnv *env, const char *str, int strLength)
{
  ERL_NIF_TERM databytes = enif_make_list(env, 0);

  for (int i=0; i < strLength; i++)
  {
    databytes = enif_make_list_cell(env, enif_make_uint(env, str[i]), databytes);
  }

  return databytes;
}

and form map keypair in C

enif_make_map_put(env, osp, enif_make_atom(env, "data"), CreateElixirBinary(env, baseValidationResult.certsCheckInfo[i].data, baseValidationResult.certsCheckInfo[i].dataLen),  &osp);

I have map oscp_info

`oscp_info = %{
  access: "http://acsk.any.provider.ua/services/ocsp/",
  crl: "http://acsk.any.provider.ua/crl/PB-S9.crl",
  data: [0, 113, 4294967247, 69, 0, 34, 4294967231, 4294967213, 0, 0, 0, 4,
   4294967272, 4294967169, 4294967187, 4294967227, 4294967201, 4294967277,
   4294967172, 13, 20, 2, 87, 85, 24, 93, 4294967256, 87, 17, 55, 4294967224, 1,
   74, 4294967238, 4294967172, 4294967239, 4294967256, 4294967236, 63,
   4294967173, 4294967186, 4294967212, 4294967208, 4294967184, 17, 4294967235,
   4294967272, ...],
  delta_crl: "http://acsk.any.provider.ua/crldelta/PB-Delta-S9.crl"
}`

If I use enif_make_int instead of enif_make_uint data looks like:

data: [0, 113, -49, 69, 0, 34, -65, -83, 0, 0, 0, 4, -24, -127, -109, -69,
   -95, -19, -124, 13, 20, 2, 87, 85, 24, 93, -40, 87, 17, 55, -72, 1, 74, -58,
   -124, -57, -40, -60, 63, -123, -110, -84, -88, -112, 17, -61, -24, ...]

The problem is data cannot be converted neither to unicode no to binary, so I can't make http request How should I return data (char*) from nif to be able make request?

1 answer

  • answered 2018-07-25 10:22 Maryna Krasnova

    The correct way to form erlang binary from c:

    ErlNifBinary derDataBin;
    
    enif_alloc_binary(baseValidationResult.certsCheckInfo[i].dataLen, &derDataBin);
    
    memcpy(derDataBin.data, baseValidationResult.certsCheckInfo[i].data, 
    
    baseValidationResult.certsCheckInfo[i].dataLen);
    
    ERL_NIF_TERM derData = enif_make_binary(env, &derDataBin);