Как настроить дескриптор httr cURL в R с помощью SSL - PullRequest
2 голосов
/ 24 января 2020

У меня есть успешный запрос SOAP в SOAPUI, который я пытаюсь преобразовать в код R с пакетом httr. В SOAPUI все, что мне нужно сделать для настроек SSL, это указать путь к файлу, который указывает на файл PKCS # 12 для хранилища ключей, а затем предоставить пароль в виде простого текста для файла PKCS # 12 в качестве пароля хранилища ключей. С этой настройкой все работает отлично.

В R, поскольку httr использует curl, я понимаю, что мне нужно извлечь клиентский SSL-сертификат и ключ SSL как два .pem файла из связанный файл PKCS # 12. Поэтому я извлек их с помощью следующих команд OpenSSL:

openssl pkcs12 -in "path to .p12 file" -passin pass:******** -clcerts -nokeys -out "path to new cert.pem"
openssl pkcs12 -in "path to .p12 file" -passin pass:******** -nodes -nocerts -out "path to new key.pem"

Затем в свой запрос httr::POST я включил эту опцию config, чтобы указывать на файлы .pem, чтобы дескриптор скручивания мог быть правильно определенным (я только временно установил ssl_verifypeer = F, чтобы исключить возможность получения ошибки из-за пакета CA):

config(ssl_verifypeer = F, sslcert = "path to new cert.pem", sslkey = "path to new key.pem")

Однако всякий раз, когда я запускаю запрос httr::POST, я получить следующую ошибку:

Error in curl::curl_fetch_memory(url, handle = handle) : 
schannel: next InitializeSecurityContext failed: SEC_E_ILLEGAL_MESSAGE (0x80090326) - This error usually occurs 
when a fatal SSL/TLS alert is received (e.g. handshake failed). More detail may be available in the Windows 
System event log.

Я не знаю, какую ошибку я здесь совершаю, но я боролся с ней в течение нескольких недель. Любая помощь здесь будет спасением.

1 Ответ

0 голосов
/ 02 февраля 2020

Вы можете попробовать что-то вроде этого:

# request made with certificate and key as plain text
res <- POST("the_url_goes_here",
            config = config(sslcert = "certificate_path", sslkey = "key_goes_here_as_plain_text"), 
            verbose(data_out = F, data_in = F, info = T, ssl = F)) # this is quite helpful to debug if something goes wrong

# you can also read in the certificate separately
cert <- openssl::read_p12(file = "cert_path", password = "key_goes_here")

# since there are different type of certificates that are handled differently by curl, this table of options might be helpful as well
# it shows what is the corresponding parameter in httr to the one in curl
httr::httr_options()

# here is what a curl command could look like
curl --data "@name_of_the_file_goes_here.json" --cert "name_of_the_certificate_goes_here.pfx" --key "password_goes_here" https://url.com

pause
...