CURLOPT_PINNEDPUBLICKEY.3 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. .\" generated by cd2nroff 0.1 from CURLOPT_PINNEDPUBLICKEY.md
  2. .TH CURLOPT_PINNEDPUBLICKEY 3 "2025-01-17" libcurl
  3. .SH NAME
  4. CURLOPT_PINNEDPUBLICKEY \- pinned public key
  5. .SH SYNOPSIS
  6. .nf
  7. #include <curl/curl.h>
  8. CURLcode curl_easy_setopt(CURL *handle, CURLOPT_PINNEDPUBLICKEY,
  9. char *pinnedpubkey);
  10. .fi
  11. .SH DESCRIPTION
  12. Pass a pointer to a null\-terminated string as parameter. The string can be the
  13. filename of your pinned public key. The file format expected is "PEM" or
  14. \&"DER". The string can also be any number of base64 encoded sha256 hashes
  15. preceded by "sha256//" and separated by ";"
  16. When negotiating a TLS or SSL connection, the server sends a certificate
  17. indicating its identity. A public key is extracted from this certificate and
  18. if it does not exactly match the public key provided to this option, curl
  19. aborts the connection before sending or receiving any data.
  20. This option is independent of option \fICURLOPT_SSL_VERIFYPEER(3)\fP. If you turn
  21. off that option then the peer is still verified by public key.
  22. On mismatch, \fICURLE_SSL_PINNEDPUBKEYNOTMATCH\fP is returned.
  23. The application does not have to keep the string around after setting this
  24. option.
  25. .SH DEFAULT
  26. NULL
  27. .SH PROTOCOLS
  28. This functionality affects all TLS based protocols: HTTPS, FTPS, IMAPS, POP3S, SMTPS etc.
  29. This option works only with the following TLS backends:
  30. GnuTLS, OpenSSL, Schannel, Secure Transport, mbedTLS and wolfSSL
  31. .SH EXAMPLE
  32. .nf
  33. int main(void)
  34. {
  35. CURL *curl = curl_easy_init();
  36. if(curl) {
  37. curl_easy_setopt(curl, CURLOPT_URL, "https://example.com");
  38. curl_easy_setopt(curl, CURLOPT_PINNEDPUBLICKEY, "/etc/publickey.der");
  39. /* OR
  40. curl_easy_setopt(curl, CURLOPT_PINNEDPUBLICKEY,
  41. "sha256//YhKJKSzoTt2b5FP18fvpHo7fJYqQCjAa3HWY3"
  42. "tvRMwE=;sha256//t62CeU2tQiqkexU74Gxa2eg7fRbEg"
  43. "oChTociMee9wno=");
  44. */
  45. /* Perform the request */
  46. curl_easy_perform(curl);
  47. }
  48. }
  49. .fi
  50. .SH PUBLIC KEY EXTRACTION
  51. If you do not have the server\(aqs public key file you can extract it from the
  52. server\(aqs certificate.
  53. .nf
  54. # retrieve the server's certificate if you do not already have it
  55. #
  56. # be sure to examine the certificate to see if it is what you expected
  57. #
  58. # Windows-specific:
  59. # - Use NUL instead of /dev/null.
  60. # - OpenSSL may wait for input instead of disconnecting. Hit enter.
  61. # - If you do not have sed, then just copy the certificate into a file:
  62. # Lines from -----BEGIN CERTIFICATE----- to -----END CERTIFICATE-----.
  63. #
  64. openssl s_client -servername www.example.com -connect www.example.com:443 \\
  65. < /dev/null | sed -n "/-----BEGIN/,/-----END/p" > www.example.com.pem
  66. # extract public key in pem format from certificate
  67. openssl x509 -in www.example.com.pem -pubkey -noout > www.example.com.pubkey.pem
  68. # convert public key from pem to der
  69. openssl asn1parse -noout -inform pem -in www.example.com.pubkey.pem \\
  70. -out www.example.com.pubkey.der
  71. # sha256 hash and base64 encode der to string for use
  72. openssl dgst -sha256 -binary www.example.com.pubkey.der | openssl base64
  73. .fi
  74. The public key in PEM format contains a header, base64 data and a
  75. footer:
  76. .nf
  77. -----BEGIN PUBLIC KEY-----
  78. [BASE 64 DATA]
  79. -----END PUBLIC KEY-----
  80. .fi
  81. .SH HISTORY
  82. .IP "PEM/DER support"
  83. 7.39.0: OpenSSL, GnuTLS
  84. 7.43.0: wolfSSL
  85. 7.47.0: mbedTLS
  86. 7.54.1: Secure Transport on macOS 10.7+/iOS 10+
  87. 7.58.1: Schannel
  88. .IP "sha256 support"
  89. 7.44.0: OpenSSL, GnuTLS and wolfSSL
  90. 7.47.0: mbedTLS
  91. 7.54.1: Secure Transport on macOS 10.7+/iOS 10+
  92. 7.58.1: Schannel
  93. Other SSL backends not supported.
  94. .SH AVAILABILITY
  95. Added in curl 7.39.0
  96. .SH RETURN VALUE
  97. Returns CURLE_OK if TLS enabled, CURLE_UNKNOWN_OPTION if not, or
  98. CURLE_OUT_OF_MEMORY if there was insufficient heap space.
  99. .SH SEE ALSO
  100. .BR CURLOPT_CAINFO (3),
  101. .BR CURLOPT_CAPATH (3),
  102. .BR CURLOPT_SSL_VERIFYHOST (3),
  103. .BR CURLOPT_SSL_VERIFYPEER (3)