master
1/***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
9 *
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at https://curl.se/docs/copyright.html.
13 *
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
17 *
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
20 *
21 * SPDX-License-Identifier: curl
22 *
23 ***************************************************************************/
24/* <DESC>
25 * Use in-memory user certificate and private key and retrieve an HTTPS page.
26 * </DESC>
27 */
29/* This example uses the legacy and discouraged method. It also disables
30 server certificate verification; real applications must verify the peer.
31 Use CURLOPT_SSLCERT_BLOB instead, which is TLS backend agnostic. See the
32 clientcert-blob.c example. */
34/* Requires: USE_OPENSSL */
36#include <openssl/ssl.h>
38#include <stdio.h>
40#include <curl/curl.h>
42static size_t write_cb(char *ptr, size_t size, size_t nmemb, void *stream)
43{
44 fwrite(ptr, size, nmemb, (FILE *)stream);
45 return nmemb * size;
46}
48static CURLcode sslctx_function(CURL *curl, void *sslctx, void *pointer)
49{
50 /** This example uses a (fake) certificate and private key **/
51 /* replace the XXX with the actual client/user certificate */
52 static const char mypem[] =
53 "-----BEGIN CERTIFICATE-----\n"
54 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"
55 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"
56 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"
57 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"
58 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"
59 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"
60 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"
61 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"
62 "-----END CERTIFICATE-----\n";
64 /* replace the XXX with the actual private key */
65 static const char mykey[] =
66 "-----BEGIN PRIVATE KEY-----\n"
67 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"
68 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"
69 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"
70 "-----END PRIVATE KEY-----\n";
72 CURLcode result = CURLE_ABORTED_BY_CALLBACK;
73 X509 *cert = NULL;
74 BIO *bio = NULL;
75 BIO *kbio = NULL;
76 EVP_PKEY *pkey = NULL;
77 int ret;
79 (void)curl;
80 (void)pointer;
82 /* get a BIO */
83 bio = BIO_new_mem_buf(mypem, sizeof(mypem) - 1);
84 if(!bio) {
85 printf("BIO_new_mem_buf() failed\n");
86 goto out;
87 }
89 /* use it to read the PEM formatted certificate from memory into an X509
90 structure that SSL can use. */
91 cert = PEM_read_bio_X509(bio, NULL, 0, NULL);
92 if(!cert) {
93 printf("PEM_read_bio_X509() failed\n");
94 goto out;
95 }
97 /* tell SSL to use the X509 certificate */
98 ret = SSL_CTX_use_certificate((SSL_CTX *)sslctx, cert);
99 if(ret != 1) {
100 printf("SSL_CTX_use_certificate() failed\n");
101 goto out;
102 }
104 /* create a bio for the private key */
105 kbio = BIO_new_mem_buf(mykey, sizeof(mykey) - 1);
106 if(!kbio) {
107 printf("BIO_new_mem_buf() failed\n");
108 goto out;
109 }
111 pkey = PEM_read_bio_PrivateKey(kbio, NULL, NULL, NULL);
112 if(!pkey) {
113 printf("PEM_read_bio_PrivateKey() failed\n");
114 goto out;
115 }
117 /* tell SSL to use the private key from memory */
118 ret = SSL_CTX_use_PrivateKey((SSL_CTX *)sslctx, pkey);
119 if(ret != 1) {
120 printf("SSL_CTX_use_PrivateKey() failed\n");
121 goto out;
122 }
124 result = CURLE_OK;
126out:
127 /* free resources that have been allocated by OpenSSL functions */
128 if(bio)
129 BIO_free(bio);
131 if(kbio)
132 BIO_free(kbio);
134 if(pkey)
135 EVP_PKEY_free(pkey);
137 if(cert)
138 X509_free(cert);
140 /* all set to go */
141 return result;
142}
144int main(void)
145{
146 CURL *curl;
148 CURLcode result = curl_global_init(CURL_GLOBAL_ALL);
149 if(result != CURLE_OK)
150 return (int)result;
152 curl = curl_easy_init();
153 if(curl) {
154 curl_easy_setopt(curl, CURLOPT_VERBOSE, 0L);
155 curl_easy_setopt(curl, CURLOPT_HEADER, 0L);
156 curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1L);
157 curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1L);
158 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_cb);
159 curl_easy_setopt(curl, CURLOPT_WRITEDATA, stdout);
160 curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, write_cb);
161 curl_easy_setopt(curl, CURLOPT_HEADERDATA, stderr);
162 curl_easy_setopt(curl, CURLOPT_SSLCERTTYPE, "PEM");
164 /* both VERIFYPEER and VERIFYHOST are set to 0 in this case because there
165 is no CA certificate */
166 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
167 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
169 curl_easy_setopt(curl, CURLOPT_URL, "https://www.example.com/");
170 curl_easy_setopt(curl, CURLOPT_SSLKEYTYPE, "PEM");
172 /* first try: retrieve page without user certificate and key -> fails */
173 result = curl_easy_perform(curl);
174 if(result == CURLE_OK)
175 printf("*** transfer succeeded ***\n");
176 else
177 printf("*** transfer failed ***\n");
179 /* second try: retrieve page using user certificate and key -> succeeds to
180 * load the certificate and key by installing a function doing the
181 * necessary "modifications" to the SSL CONTEXT before link init
182 */
183 curl_easy_setopt(curl, CURLOPT_SSL_CTX_FUNCTION, sslctx_function);
184 result = curl_easy_perform(curl);
185 if(result == CURLE_OK)
186 printf("*** transfer succeeded ***\n");
187 else
188 printf("*** transfer failed ***\n");
190 curl_easy_cleanup(curl);
191 }
192 curl_global_cleanup();
193 return (int)result;
194}
Diagnostics4 git calls · 27 ms
CallStatusSizeTime027 ms
discover200152 B3 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=3 git.response.bytes=152 git.server.agent=gitty/1.0 git.server.object_format=sha1 git.server.caps=agent ls-refs fetch object-format git.server.fetch=shallow filter sideband-all git.protocol.version=2 git.pkt.frames=8 git.pkt.data=6 git.pkt.flush=2 git.pkt.delim=0 git.pkt.bytes=152 git.pkt.payload.max=34
ls-refs200149 B · hit3 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=3 git.response.bytes=149 git.response.cache=HIT git.response.duration_ms=3 git.protocol.version=2 git.pkt.frames=3 git.pkt.data=2 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=149 git.pkt.payload.max=78 git.ls_refs.count=2 git.ls_refs.head=refs/heads/master
fetchblob:none · deepen 10200287 KiB · hit4 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=4 git.response.bytes=293470 git.response.cache=HIT git.response.duration_ms=4 git.fetch.filter=blob:none git.fetch.filter.requested=tree:0 git.fetch.filter.fallback=blob:none git.fetch.filter.learned=false git.fetch.wants=31c911efe5652725e930f4d2e60c9060e5f6646d git.fetch.haves.count=0 git.fetch.deepen=10 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=10 git.pkt.data=8 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=293470 git.pkt.payload.max=65516 git.sideband.band1.bytes=293354 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=87 git.pack.bytes=293354 git.pack.ofs_delta=0 git.pack.ref_delta=1 git.pack.chain.max=1 git.pack.trailer=6a646bcda6740bab42e321b51998b52ea15146c0 git.pack.full=86Open pack map
fetch2002.0 KiB · hit20 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=20 git.response.bytes=2095 git.response.cache=HIT git.response.duration_ms=20 git.fetch.wants=d9e8e40b553252a2a49c979b3bb0679e2e8950e6 git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=3 git.pkt.data=2 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=2095 git.pkt.payload.max=2074 git.sideband.band1.bytes=2073 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0
Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.
- Remote
- https://5ad2236b39de4c8bd8f6270ea63f3ea9.artifacts.cloudflare.net/git/public-v0/curl.git
- Ref
- refs/heads/master
- Commit
- 31c911efe5652725e930f4d2e60c9060e5f6646d
- cf-ray
- a5e2c2ee1d15aedd
- Request
- e032ec03…
- Protocol
- v2 · sha1
- Agent
- gitty/1.0
- Filter
- advertised
- Fetch
- shallow filter sideband-all
- Capabilities
- agent ls-refs fetch object-format