Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use self signed certificate with cURL?

I have a flask application running using a self signed certificate. I'm able to send in a curl request using:

curl -v -k -H "Content-Type: application/json" -d '{"data":"value1","key":"value2"}' https://<server_ip>:<port> 

The verbose logs show that everything went alright.

I wanted to avoid using the -k (--insecure) option and instead specify a .pem file that curl could use. Looking at the curl man page I found that you could do this using the --cert option. So I created a .pem file using this:

openssl rsa -in server.key -text > private.pem 

CURL throws me this error when using the private.pem file:

curl: (58) unable to use client certificate (no key found or wrong pass phrase?) 

Any suggestions? - or is this only possible with a properly signed certificate?

Tnx

like image 768
wholly_cow Avatar asked Dec 22 '14 22:12

wholly_cow


People also ask

Does curl check SSL certificate?

libcurl performs peer SSL certificate verification by default. This is done by using a CA certificate store that the SSL library can use to make sure the peer's server certificate is valid.


2 Answers

This is just another version of this question: Using openssl to get the certificate from a server

Or put more bluntly:

Using curl --cert is wrong, it is for client certificates.

First, get the the certs your server is using:

$ echo quit | openssl s_client -showcerts -servername server -connect server:443 > cacert.pem 

(-servername is necessary for SNI so that you get the right virtual server's certificate back)

Then make your curl command line use that set to verify the server in subsequent operations:

$ curl --cacert cacert.pem https://server/ [and the rest] 
like image 166
Daniel Stenberg Avatar answered Sep 23 '22 21:09

Daniel Stenberg


To make request from https server through curl. I make use of below steps

  • Step1: Generate self signed certificate with below code at root of the project you want to make use of it.openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -nodes
  • Step2: Fill the prompt with required details but when you get to Common name input localhost e.g Common Name (eg, fully qualified host name) []:localhost
  • step3: When your openssl cert.pem & key.pem has being generated startup your server then in another terminal or command line run curl --cacert cert.pem https://localhost:443

Note: I use port 443 which is the default https port, you can make use of another port then make sure cert.pem file path is well referenced.

like image 40
Alabi Temitope Avatar answered Sep 25 '22 21:09

Alabi Temitope