json - How to send the content of SSL-related files to the API call | PHP | cUrl

I have two files for SSL ssl_crt and ssl_key. I want to read the content of these files and send it to the APIs as a JSON field. But the file_get_contents actually convert the single slash to double so the API return the following error.

The private key format is invalid.

The certificate format is invalid.

I am actually using run cloud service where I want to send the content of these files to one of their API

https://runcloud.io/docs/api/web-application#ssl-basic-install-ssl

As you can see in the above URL, they need the content in the proper format.

So I tried the following.

$curl = curl_init();

$ssl_crt = file_get_contents(selectServer()->ssl_crt_path);
$ssl_key = file_get_contents(selectServer()->ssl_key_path);

$data = [
    'provider' => 'custom',
    'enableHttp' => true,
    'enableHsts' => false,
    'certificate' => $ssl_crt,
    'privateKey' => $ssl_key,
];

curl_setopt_array($curl, array(
    CURLOPT_URL => 'https://manage.runcloud.io/api/v2/servers/{id}/webapps/{webapp_id}/ssl',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => '',
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 0,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => 'POST',
    CURLOPT_POSTFIELDS => json_encode($data, JSON_UNESCAPED_SLASHES),
    CURLOPT_HTTPHEADER => array(
        'Accept: application/json',
        'Content-Type: application/json',
        'Authorization: Basic {token}'
    ),
));

$response = curl_exec($curl);

curl_close($curl);
echo $response;

If I manually write the content of the certificate(or key) to the data array then it works fine. i.e

$data = [
    // ...
    'certificate' => '-----BEGIN CERTIFICATE-----\nblah\nblah==\n-----END CERTIFICATE-----,
    'privateKey' => '-----BEGIN PRIVATE KEY-----\nblah\nblah==\n-----END PRIVATE KEY-----,
];

The actual issue with the format. the "\n" becomes "\n" and the main content of the certificate or key can have "\n".

Can someone help me out with this?

Answer

Solution:

the post-mortem here is that the on-disk file was corrupted, the on-disk file had all newlines converted to literal-slash-followed-by-n, which PHP's double-quote operator was fixing, thus it worked when you hardcoded the file (with php's double-quote operator. it would not have worked with php's single-quote operator), and didn't work when you got it from file_get_contents() - code used to fix the on-disk file was

<?php
$real_crt=file_get_contents("ssl_crt");
$fixed_crt = strtr($real_crt,array(
    "\\n"=>"\n",
    "\\r"=>"\r",
));
file_put_contents("ssl_crt_fixed",$fixed_crt);

i only figured this out after you sent me the ssl_cert file on facebook messenger, though, and i'm pretty sure the ssl_cert file was required to solve this mystery, so i have voted to close this question as requires the shortest code necessary to reproduce the problem. ie, it needs the ssl_cert file to be any kind of reproducible

Source