2024-01-17 18:32:44 +08:00
|
|
|
---
|
2024-02-28 18:28:10 +08:00
|
|
|
c: Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
|
2024-01-17 18:32:44 +08:00
|
|
|
SPDX-License-Identifier: curl
|
|
|
|
Title: CURLOPT_PINNEDPUBLICKEY
|
|
|
|
Section: 3
|
|
|
|
Source: libcurl
|
|
|
|
See-also:
|
|
|
|
- CURLOPT_CAINFO (3)
|
|
|
|
- CURLOPT_CAPATH (3)
|
|
|
|
- CURLOPT_SSL_VERIFYHOST (3)
|
|
|
|
- CURLOPT_SSL_VERIFYPEER (3)
|
2024-03-21 18:50:20 +08:00
|
|
|
Protocol:
|
|
|
|
- TLS
|
2024-03-21 22:46:32 +08:00
|
|
|
TLS-backend:
|
|
|
|
- OpenSSL
|
|
|
|
- GnuTLS
|
|
|
|
- wolfSSL
|
|
|
|
- mbedTLS
|
|
|
|
- Secure Transport
|
|
|
|
- Schannel
|
2024-01-17 18:32:44 +08:00
|
|
|
---
|
|
|
|
|
|
|
|
# NAME
|
|
|
|
|
|
|
|
CURLOPT_PINNEDPUBLICKEY - pinned public key
|
|
|
|
|
|
|
|
# SYNOPSIS
|
|
|
|
|
|
|
|
~~~c
|
2014-10-01 10:31:17 +08:00
|
|
|
#include <curl/curl.h>
|
|
|
|
|
2021-11-26 21:20:18 +08:00
|
|
|
CURLcode curl_easy_setopt(CURL *handle, CURLOPT_PINNEDPUBLICKEY,
|
|
|
|
char *pinnedpubkey);
|
2024-01-17 18:32:44 +08:00
|
|
|
~~~
|
|
|
|
|
|
|
|
# DESCRIPTION
|
|
|
|
|
2020-06-25 17:38:25 +08:00
|
|
|
Pass a pointer to a null-terminated string as parameter. The string can be the
|
2024-01-23 22:12:09 +08:00
|
|
|
filename of your pinned public key. The file format expected is "PEM" or
|
2024-01-17 18:32:44 +08:00
|
|
|
"DER". The string can also be any number of base64 encoded sha256 hashes
|
|
|
|
preceded by "sha256//" and separated by ";"
|
2014-10-01 10:31:17 +08:00
|
|
|
|
|
|
|
When negotiating a TLS or SSL connection, the server sends a certificate
|
2014-10-30 21:56:53 +08:00
|
|
|
indicating its identity. A public key is extracted from this certificate and
|
2023-08-22 23:40:39 +08:00
|
|
|
if it does not exactly match the public key provided to this option, curl
|
|
|
|
aborts the connection before sending or receiving any data.
|
2015-09-17 14:50:51 +08:00
|
|
|
|
2024-01-17 18:32:44 +08:00
|
|
|
This option is independent of option CURLOPT_SSL_VERIFYPEER(3). If you turn
|
|
|
|
off that option then the peer is still verified by public key.
|
2023-09-25 13:41:20 +08:00
|
|
|
|
2024-01-17 18:32:44 +08:00
|
|
|
On mismatch, *CURLE_SSL_PINNEDPUBKEYNOTMATCH* is returned.
|
2016-12-21 21:48:35 +08:00
|
|
|
|
|
|
|
The application does not have to keep the string around after setting this
|
|
|
|
option.
|
2024-01-17 18:32:44 +08:00
|
|
|
|
|
|
|
# DEFAULT
|
|
|
|
|
2014-10-01 10:31:17 +08:00
|
|
|
NULL
|
2024-01-17 18:32:44 +08:00
|
|
|
|
|
|
|
# EXAMPLE
|
|
|
|
|
|
|
|
~~~c
|
2023-12-04 17:50:42 +08:00
|
|
|
int main(void)
|
|
|
|
{
|
|
|
|
CURL *curl = curl_easy_init();
|
|
|
|
if(curl) {
|
|
|
|
curl_easy_setopt(curl, CURLOPT_URL, "https://example.com");
|
|
|
|
curl_easy_setopt(curl, CURLOPT_PINNEDPUBLICKEY, "/etc/publickey.der");
|
|
|
|
/* OR
|
|
|
|
curl_easy_setopt(curl, CURLOPT_PINNEDPUBLICKEY,
|
|
|
|
"sha256//YhKJKSzoTt2b5FP18fvpHo7fJYqQCjAa3HWY3"
|
|
|
|
"tvRMwE=;sha256//t62CeU2tQiqkexU74Gxa2eg7fRbEg"
|
|
|
|
"oChTociMee9wno=");
|
|
|
|
*/
|
2014-10-30 21:56:53 +08:00
|
|
|
|
2023-12-04 17:50:42 +08:00
|
|
|
/* Perform the request */
|
|
|
|
curl_easy_perform(curl);
|
|
|
|
}
|
2014-10-30 21:56:53 +08:00
|
|
|
}
|
2024-01-17 18:32:44 +08:00
|
|
|
~~~
|
|
|
|
|
|
|
|
# PUBLIC KEY EXTRACTION
|
|
|
|
|
2015-04-05 13:48:16 +08:00
|
|
|
If you do not have the server's public key file you can extract it from the
|
|
|
|
server's certificate.
|
2024-01-17 18:32:44 +08:00
|
|
|
~~~
|
2021-10-31 23:34:44 +08:00
|
|
|
# retrieve the server's certificate if you do not already have it
|
2015-09-14 15:16:04 +08:00
|
|
|
#
|
|
|
|
# be sure to examine the certificate to see if it is what you expected
|
|
|
|
#
|
|
|
|
# Windows-specific:
|
|
|
|
# - Use NUL instead of /dev/null.
|
|
|
|
# - OpenSSL may wait for input instead of disconnecting. Hit enter.
|
2021-10-31 23:34:44 +08:00
|
|
|
# - If you do not have sed, then just copy the certificate into a file:
|
2015-09-14 15:16:04 +08:00
|
|
|
# Lines from -----BEGIN CERTIFICATE----- to -----END CERTIFICATE-----.
|
|
|
|
#
|
2024-01-17 18:32:44 +08:00
|
|
|
openssl s_client -servername www.example.com -connect www.example.com:443 \
|
|
|
|
< /dev/null | sed -n "/-----BEGIN/,/-----END/p" > www.example.com.pem
|
2015-09-14 15:16:04 +08:00
|
|
|
|
2015-07-01 08:23:54 +08:00
|
|
|
# extract public key in pem format from certificate
|
2015-09-20 22:08:15 +08:00
|
|
|
openssl x509 -in www.example.com.pem -pubkey -noout > www.example.com.pubkey.pem
|
2015-09-14 15:16:04 +08:00
|
|
|
|
2015-07-01 08:23:54 +08:00
|
|
|
# convert public key from pem to der
|
2024-01-17 18:32:44 +08:00
|
|
|
openssl asn1parse -noout -inform pem -in www.example.com.pubkey.pem \
|
|
|
|
-out www.example.com.pubkey.der
|
2015-09-14 15:16:04 +08:00
|
|
|
|
2015-07-01 08:23:54 +08:00
|
|
|
# sha256 hash and base64 encode der to string for use
|
2015-09-20 22:08:15 +08:00
|
|
|
openssl dgst -sha256 -binary www.example.com.pubkey.der | openssl base64
|
2024-01-17 18:32:44 +08:00
|
|
|
~~~
|
|
|
|
|
2015-07-01 08:23:54 +08:00
|
|
|
The public key in PEM format contains a header, base64 data and a
|
2015-04-05 13:48:16 +08:00
|
|
|
footer:
|
2024-01-17 18:32:44 +08:00
|
|
|
~~~
|
2015-04-05 13:48:16 +08:00
|
|
|
-----BEGIN PUBLIC KEY-----
|
|
|
|
[BASE 64 DATA]
|
|
|
|
-----END PUBLIC KEY-----
|
2024-01-17 18:32:44 +08:00
|
|
|
~~~
|
|
|
|
|
|
|
|
# AVAILABILITY
|
2016-09-19 05:56:13 +08:00
|
|
|
|
2024-01-17 18:32:44 +08:00
|
|
|
## PEM/DER support
|
2018-01-26 05:47:49 +08:00
|
|
|
|
2024-01-17 18:32:44 +08:00
|
|
|
7.39.0: OpenSSL, GnuTLS
|
2016-09-19 05:56:13 +08:00
|
|
|
|
2024-01-17 18:32:44 +08:00
|
|
|
7.43.0: wolfSSL
|
2016-09-19 05:56:13 +08:00
|
|
|
|
2024-01-17 18:32:44 +08:00
|
|
|
7.47.0: mbedTLS
|
2017-04-18 07:47:51 +08:00
|
|
|
|
2024-01-17 18:32:44 +08:00
|
|
|
7.54.1: Secure Transport on macOS 10.7+/iOS 10+
|
2017-04-19 12:31:23 +08:00
|
|
|
|
2024-01-17 18:32:44 +08:00
|
|
|
7.58.1: Schannel
|
2016-09-19 05:56:13 +08:00
|
|
|
|
2024-01-17 18:32:44 +08:00
|
|
|
## sha256 support
|
2016-09-19 05:56:13 +08:00
|
|
|
|
2024-01-17 18:32:44 +08:00
|
|
|
7.44.0: OpenSSL, GnuTLS and wolfSSL
|
2016-09-19 05:56:13 +08:00
|
|
|
|
2024-01-17 18:32:44 +08:00
|
|
|
7.47.0: mbedTLS
|
2017-04-18 07:47:51 +08:00
|
|
|
|
2024-01-17 18:32:44 +08:00
|
|
|
7.54.1: Secure Transport on macOS 10.7+/iOS 10+
|
|
|
|
|
|
|
|
7.58.1: Schannel
|
2017-04-19 12:31:23 +08:00
|
|
|
|
2016-04-22 09:21:45 +08:00
|
|
|
Other SSL backends not supported.
|
2024-01-17 18:32:44 +08:00
|
|
|
|
|
|
|
# RETURN VALUE
|
|
|
|
|
2014-10-01 10:31:17 +08:00
|
|
|
Returns CURLE_OK if TLS enabled, CURLE_UNKNOWN_OPTION if not, or
|
|
|
|
CURLE_OUT_OF_MEMORY if there was insufficient heap space.
|