CURLOPT_PINNEDPUBLICKEY.md (3692B)
1 --- 2 c: Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al. 3 SPDX-License-Identifier: curl 4 Title: CURLOPT_PINNEDPUBLICKEY 5 Section: 3 6 Source: libcurl 7 See-also: 8 - CURLOPT_CAINFO (3) 9 - CURLOPT_CAPATH (3) 10 - CURLOPT_SSL_VERIFYHOST (3) 11 - CURLOPT_SSL_VERIFYPEER (3) 12 Protocol: 13 - TLS 14 TLS-backend: 15 - OpenSSL 16 - GnuTLS 17 - wolfSSL 18 - mbedTLS 19 - Schannel 20 Added-in: 7.39.0 21 --- 22 23 # NAME 24 25 CURLOPT_PINNEDPUBLICKEY - pinned public key 26 27 # SYNOPSIS 28 29 ~~~c 30 #include <curl/curl.h> 31 32 CURLcode curl_easy_setopt(CURL *handle, CURLOPT_PINNEDPUBLICKEY, 33 char *pinnedpubkey); 34 ~~~ 35 36 # DESCRIPTION 37 38 Pass a pointer to a null-terminated string as parameter. The string can be the 39 filename of your pinned public key. The file format expected is "PEM" or 40 "DER". The string can also be any number of base64 encoded sha256 hashes 41 preceded by "sha256//" and separated by ";" 42 43 When negotiating a TLS or SSL connection, the server sends a certificate 44 indicating its identity. A public key is extracted from this certificate and 45 if it does not exactly match the public key provided to this option, curl 46 aborts the connection before sending or receiving any data. 47 48 This option is independent of option CURLOPT_SSL_VERIFYPEER(3). If you turn 49 off that option then the peer is still verified by public key. 50 51 On mismatch, *CURLE_SSL_PINNEDPUBKEYNOTMATCH* is returned. 52 53 The application does not have to keep the string around after setting this 54 option. 55 56 # DEFAULT 57 58 NULL 59 60 # %PROTOCOLS% 61 62 # EXAMPLE 63 64 ~~~c 65 int main(void) 66 { 67 CURL *curl = curl_easy_init(); 68 if(curl) { 69 curl_easy_setopt(curl, CURLOPT_URL, "https://example.com"); 70 curl_easy_setopt(curl, CURLOPT_PINNEDPUBLICKEY, "/etc/publickey.der"); 71 /* OR 72 curl_easy_setopt(curl, CURLOPT_PINNEDPUBLICKEY, 73 "sha256//YhKJKSzoTt2b5FP18fvpHo7fJYqQCjAa3HWY3" 74 "tvRMwE=;sha256//t62CeU2tQiqkexU74Gxa2eg7fRbEg" 75 "oChTociMee9wno="); 76 */ 77 78 /* Perform the request */ 79 curl_easy_perform(curl); 80 } 81 } 82 ~~~ 83 84 # PUBLIC KEY EXTRACTION 85 86 If you do not have the server's public key file you can extract it from the 87 server's certificate. 88 ~~~ 89 # retrieve the server's certificate if you do not already have it 90 # 91 # be sure to examine the certificate to see if it is what you expected 92 # 93 # Windows-specific: 94 # - Use NUL instead of /dev/null. 95 # - OpenSSL may wait for input instead of disconnecting. Hit enter. 96 # - If you do not have sed, then just copy the certificate into a file: 97 # Lines from -----BEGIN CERTIFICATE----- to -----END CERTIFICATE-----. 98 # 99 openssl s_client -servername www.example.com -connect www.example.com:443 \ 100 < /dev/null | sed -n "/-----BEGIN/,/-----END/p" > www.example.com.pem 101 102 # extract public key in pem format from certificate 103 openssl x509 -in www.example.com.pem -pubkey -noout > www.example.com.pubkey.pem 104 105 # convert public key from pem to der 106 openssl asn1parse -noout -inform pem -in www.example.com.pubkey.pem \ 107 -out www.example.com.pubkey.der 108 109 # sha256 hash and base64 encode der to string for use 110 openssl dgst -sha256 -binary www.example.com.pubkey.der | openssl base64 111 ~~~ 112 113 The public key in PEM format contains a header, base64 data and a 114 footer: 115 ~~~ 116 -----BEGIN PUBLIC KEY----- 117 [BASE 64 DATA] 118 -----END PUBLIC KEY----- 119 ~~~ 120 121 # HISTORY 122 123 ## PEM/DER support 124 125 7.39.0: OpenSSL, GnuTLS 126 127 7.43.0: wolfSSL 128 129 7.47.0: mbedTLS 130 131 7.58.1: Schannel 132 133 ## sha256 support 134 135 7.44.0: OpenSSL, GnuTLS and wolfSSL 136 137 7.47.0: mbedTLS 138 139 7.58.1: Schannel 140 141 Other SSL backends not supported. 142 143 # %AVAILABILITY% 144 145 # RETURN VALUE 146 147 curl_easy_setopt(3) returns a CURLcode indicating success or error. 148 149 CURLE_OK (0) means everything was OK, non-zero means an error occurred, see 150 libcurl-errors(3).