1

I'm trying to make an API request and am passing my SSL cert to the config() parameter of GET. I initially got this working for a few weeks but then had to reinstall R. I did a clean install, deleting all folders, installing R, RTools, RStudio. In this new instance of R the same script no longer works. I've uninstalled/reinstalled HTTR, curl, openssl and no luck still (I've also reinstalled R multiple times).

This is the error I get:

Error in curl::curl_fetch_memory(url, handle = handle) : 
could not load PEM client certificate, OpenSSL error error:02001002:system library:fopen:No such file or directory, (no key found, wrong pass phrase, or wrong file format?)

This is the get request code:

conn <- GET(url = "testurl",
               add_headers(header2),
               config(sslcert =  "my_cert.pem", sslkey = "my_key.pem"),
               content_type_json(),
               accept_json())

Where my_cert.pem and my_key.pem were parsed from openssl by:

cert <- openssl::read_p12(file = "Data/certificate.pfx", password = "somepassword")

my_cert.pem <- write_pem(cert$cert)

my_key.pem <-  write_pem(cert$key)

Any help with this would be much appreciated.

Thank you

tpscp
  • 49
  • 6
  • You are passing in a literal string to sslcert. Is that the name of the file in your current working directory? I don’t see how the pem variables you created are related to your GET call. Please include the versions numbers for R and all your packages to make it clear what your current set up is. Are you sure this exact code worked before, or are you recreating it from memory? – MrFlick Nov 24 '22 at 01:06
  • @MrFlick I've updated the post with the name of file in my current working directory. I got the information for inserting the .pem variables from the below link and the httr documentation. [https://stackoverflow.com/questions/37273819/how-to-specify-certificate-key-and-root-certificate-with-httr-for-certificate-b] R version: 4.2.2, - RStudio:: 2022.07.2 - 576 - rtools42-5355-5357 - openssl 2.0.4 - curl: 4.3.3 -httr: 1.4.4 I'm certain it worked as I used it everyday, it only stopped working when I reinstalled R. Thank you for your help! – tpscp Nov 24 '22 at 14:36
  • I should also mention I have this set in my .renviron file 'CURL_SSL_BACKEND="openssl"' though I'm not quite sure why I have to set it. – tpscp Nov 24 '22 at 15:48
  • Try writing these files to disk rather than saving them as variables: `write_pem(cert$cert, "my_cert.pem")` and `write_pem(cert$key, "my_key.pem")`. Do this before you call the `GET()` function – MrFlick Nov 28 '22 at 18:50
  • Thank you @MrFlick this resolved the issue. Exactly why did this work? In any case, this has given me back 1 hour of my day, much appreciated! – tpscp Nov 30 '22 at 07:15
  • The values you pass to `config(sslcert = "my_cert.pem", sslkey = "my_key.pem")` need to be file names that exist in disk; they can't be R variables. That's just how the wrapper to the CURL library was set up unfortunately. – MrFlick Nov 30 '22 at 14:20

1 Answers1

0

Following the answer provided by @MrFlick I was able to resolve the issue.

Writing the files to disk before the get() request like so:

write_pem(cert$cert, "my_cert.pem") 
write_pem(cert$key, "my_key.pem")

Resulted in a successful response and I no longer received the error of:

Error in curl::curl_fetch_memory(url, handle = handle) : could not load PEM client certificate, OpenSSL error error:02001002:system library:fopen:No such file or directory, (no key found, wrong pass phrase, or wrong file format?)
tpscp
  • 49
  • 6