1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2014, Daniel Stenberg, <daniel@haxx.se>, et al.
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 http://curl.haxx.se/docs/copyright.html.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
23 /* This file is for implementing all "generic" SSL functions that all libcurl
24 internals should use. It is then responsible for calling the proper
27 SSL-functions in libcurl should call functions in this source file, and not
28 to any specific SSL-layer.
30 Curl_ssl_ - prefix for generic ones
31 Curl_ossl_ - prefix for OpenSSL ones
32 Curl_gtls_ - prefix for GnuTLS ones
33 Curl_nss_ - prefix for NSS ones
34 Curl_gskit_ - prefix for GSKit ones
35 Curl_polarssl_ - prefix for PolarSSL ones
36 Curl_cyassl_ - prefix for CyaSSL ones
37 Curl_schannel_ - prefix for Schannel SSPI ones
38 Curl_darwinssl_ - prefix for SecureTransport (Darwin) ones
40 Note that this source code uses curlssl_* functions, and they are all
41 defines/macros #defined by the lib-specific header files.
43 "SSL/TLS Strong Encryption: An Introduction"
44 http://httpd.apache.org/docs-2.0/ssl/ssl_intro.html
47 #include "curl_setup.h"
49 #ifdef HAVE_SYS_TYPES_H
50 #include <sys/types.h>
52 #ifdef HAVE_SYS_STAT_H
61 #include "vtls.h" /* generic SSL protos etc */
66 #include "curl_memory.h"
72 #include "curl_base64.h"
74 #define _MPRINTF_REPLACE /* use our functions only */
75 #include <curl/mprintf.h>
77 /* The last #include file should be: */
80 /* convenience macro to check if this handle is using a shared SSL session */
81 #define SSLSESSION_SHARED(data) (data->share && \
82 (data->share->specifier & \
83 (1<<CURL_LOCK_DATA_SSL_SESSION)))
85 static bool safe_strequal(char* str1, char* str2)
88 /* both pointers point to something then compare them */
89 return (0 != Curl_raw_equal(str1, str2)) ? TRUE : FALSE;
91 /* if both pointers are NULL then treat them as equal */
92 return (!str1 && !str2) ? TRUE : FALSE;
96 Curl_ssl_config_matches(struct ssl_config_data* data,
97 struct ssl_config_data* needle)
99 if((data->version == needle->version) &&
100 (data->verifypeer == needle->verifypeer) &&
101 (data->verifyhost == needle->verifyhost) &&
102 safe_strequal(data->CApath, needle->CApath) &&
103 safe_strequal(data->CAfile, needle->CAfile) &&
104 safe_strequal(data->random_file, needle->random_file) &&
105 safe_strequal(data->egdsocket, needle->egdsocket) &&
106 safe_strequal(data->cipher_list, needle->cipher_list))
113 Curl_clone_ssl_config(struct ssl_config_data *source,
114 struct ssl_config_data *dest)
116 dest->sessionid = source->sessionid;
117 dest->verifyhost = source->verifyhost;
118 dest->verifypeer = source->verifypeer;
119 dest->version = source->version;
122 dest->CAfile = strdup(source->CAfile);
130 dest->CApath = strdup(source->CApath);
137 if(source->cipher_list) {
138 dest->cipher_list = strdup(source->cipher_list);
139 if(!dest->cipher_list)
143 dest->cipher_list = NULL;
145 if(source->egdsocket) {
146 dest->egdsocket = strdup(source->egdsocket);
151 dest->egdsocket = NULL;
153 if(source->random_file) {
154 dest->random_file = strdup(source->random_file);
155 if(!dest->random_file)
159 dest->random_file = NULL;
164 void Curl_free_ssl_config(struct ssl_config_data* sslc)
166 Curl_safefree(sslc->CAfile);
167 Curl_safefree(sslc->CApath);
168 Curl_safefree(sslc->cipher_list);
169 Curl_safefree(sslc->egdsocket);
170 Curl_safefree(sslc->random_file);
175 * Curl_rand() returns a random unsigned integer, 32bit.
177 * This non-SSL function is put here only because this file is the only one
178 * with knowledge of what the underlying SSL libraries provide in terms of
181 * NOTE: 'data' may be passed in as NULL when coming from external API without
186 unsigned int Curl_rand(struct SessionHandle *data)
189 static unsigned int randseed;
190 static bool seeded = FALSE;
193 char *force_entropy = getenv("CURL_ENTROPY");
196 size_t elen = strlen(force_entropy);
197 size_t clen = sizeof(randseed);
198 size_t min = elen < clen ? elen : clen;
199 memcpy((char *)&randseed, force_entropy, min);
208 /* data may be NULL! */
209 if(!Curl_ssl_random(data, (unsigned char *)&r, sizeof(r)))
212 /* If Curl_ssl_random() returns non-zero it couldn't offer randomness and we
213 instead perform a "best effort" */
217 /* if there's a random file to read a seed from, use it */
218 int fd = open(RANDOM_FILE, O_RDONLY);
220 /* read random data into the randseed variable */
221 ssize_t nread = read(fd, &randseed, sizeof(randseed));
222 if(nread == sizeof(randseed))
230 struct timeval now = curlx_tvnow();
231 infof(data, "WARNING: Using weak random seed\n");
232 randseed += (unsigned int)now.tv_usec + (unsigned int)now.tv_sec;
233 randseed = randseed * 1103515245 + 12345;
234 randseed = randseed * 1103515245 + 12345;
235 randseed = randseed * 1103515245 + 12345;
239 /* Return an unsigned 32-bit pseudo-random number. */
240 r = randseed = randseed * 1103515245 + 12345;
241 return (r << 16) | ((r >> 16) & 0xFFFF);
244 int Curl_ssl_backend(void)
246 return (int)CURL_SSL_BACKEND;
251 /* "global" init done? */
252 static bool init_ssl=FALSE;
257 * @retval 0 error initializing SSL
258 * @retval 1 SSL initialized successfully
260 int Curl_ssl_init(void)
262 /* make sure this is only done once */
265 init_ssl = TRUE; /* never again */
267 return curlssl_init();
272 void Curl_ssl_cleanup(void)
275 /* only cleanup if we did a previous init */
282 Curl_ssl_connect(struct connectdata *conn, int sockindex)
285 /* mark this is being ssl-enabled from here on. */
286 conn->ssl[sockindex].use = TRUE;
287 conn->ssl[sockindex].state = ssl_connection_negotiating;
289 result = curlssl_connect(conn, sockindex);
292 Curl_pgrsTime(conn->data, TIMER_APPCONNECT); /* SSL is connected */
298 Curl_ssl_connect_nonblocking(struct connectdata *conn, int sockindex,
302 /* mark this is being ssl requested from here on. */
303 conn->ssl[sockindex].use = TRUE;
304 #ifdef curlssl_connect_nonblocking
305 result = curlssl_connect_nonblocking(conn, sockindex, done);
307 *done = TRUE; /* fallback to BLOCKING */
308 result = curlssl_connect(conn, sockindex);
309 #endif /* non-blocking connect support */
311 Curl_pgrsTime(conn->data, TIMER_APPCONNECT); /* SSL is connected */
316 * Check if there's a session ID for the given connection in the cache, and if
317 * there's one suitable, it is provided. Returns TRUE when no entry matched.
319 bool Curl_ssl_getsessionid(struct connectdata *conn,
320 void **ssl_sessionid,
321 size_t *idsize) /* set 0 if unknown */
323 struct curl_ssl_session *check;
324 struct SessionHandle *data = conn->data;
327 bool no_match = TRUE;
329 *ssl_sessionid = NULL;
331 if(!conn->ssl_config.sessionid)
332 /* session ID re-use is disabled */
336 if(SSLSESSION_SHARED(data)) {
337 Curl_share_lock(data, CURL_LOCK_DATA_SSL_SESSION, CURL_LOCK_ACCESS_SINGLE);
338 general_age = &data->share->sessionage;
341 general_age = &data->state.sessionage;
343 for(i = 0; i < data->set.ssl.max_ssl_sessions; i++) {
344 check = &data->state.session[i];
345 if(!check->sessionid)
346 /* not session ID means blank entry */
348 if(Curl_raw_equal(conn->host.name, check->name) &&
349 (conn->remote_port == check->remote_port) &&
350 Curl_ssl_config_matches(&conn->ssl_config, &check->ssl_config)) {
351 /* yes, we have a session ID! */
352 (*general_age)++; /* increase general age */
353 check->age = *general_age; /* set this as used in this age */
354 *ssl_sessionid = check->sessionid;
356 *idsize = check->idsize;
363 if(SSLSESSION_SHARED(data))
364 Curl_share_unlock(data, CURL_LOCK_DATA_SSL_SESSION);
370 * Kill a single session ID entry in the cache.
372 void Curl_ssl_kill_session(struct curl_ssl_session *session)
374 if(session->sessionid) {
375 /* defensive check */
377 /* free the ID the SSL-layer specific way */
378 curlssl_session_free(session->sessionid);
380 session->sessionid = NULL;
381 session->age = 0; /* fresh */
383 Curl_free_ssl_config(&session->ssl_config);
385 Curl_safefree(session->name);
390 * Delete the given session ID from the cache.
392 void Curl_ssl_delsessionid(struct connectdata *conn, void *ssl_sessionid)
395 struct SessionHandle *data=conn->data;
397 if(SSLSESSION_SHARED(data))
398 Curl_share_lock(data, CURL_LOCK_DATA_SSL_SESSION, CURL_LOCK_ACCESS_SINGLE);
400 for(i = 0; i < data->set.ssl.max_ssl_sessions; i++) {
401 struct curl_ssl_session *check = &data->state.session[i];
403 if(check->sessionid == ssl_sessionid) {
404 Curl_ssl_kill_session(check);
409 if(SSLSESSION_SHARED(data))
410 Curl_share_unlock(data, CURL_LOCK_DATA_SSL_SESSION);
414 * Store session id in the session cache. The ID passed on to this function
415 * must already have been extracted and allocated the proper way for the SSL
416 * layer. Curl_XXXX_session_free() will be called to free/kill the session ID
419 CURLcode Curl_ssl_addsessionid(struct connectdata *conn,
424 struct SessionHandle *data=conn->data; /* the mother of all structs */
425 struct curl_ssl_session *store = &data->state.session[0];
426 long oldest_age=data->state.session[0].age; /* zero if unused */
430 /* Even though session ID re-use might be disabled, that only disables USING
431 IT. We still store it here in case the re-using is again enabled for an
434 clone_host = strdup(conn->host.name);
436 return CURLE_OUT_OF_MEMORY; /* bail out */
438 /* Now we should add the session ID and the host name to the cache, (remove
439 the oldest if necessary) */
441 /* If using shared SSL session, lock! */
442 if(SSLSESSION_SHARED(data)) {
443 Curl_share_lock(data, CURL_LOCK_DATA_SSL_SESSION, CURL_LOCK_ACCESS_SINGLE);
444 general_age = &data->share->sessionage;
447 general_age = &data->state.sessionage;
450 /* find an empty slot for us, or find the oldest */
451 for(i = 1; (i < data->set.ssl.max_ssl_sessions) &&
452 data->state.session[i].sessionid; i++) {
453 if(data->state.session[i].age < oldest_age) {
454 oldest_age = data->state.session[i].age;
455 store = &data->state.session[i];
458 if(i == data->set.ssl.max_ssl_sessions)
459 /* cache is full, we must "kill" the oldest entry! */
460 Curl_ssl_kill_session(store);
462 store = &data->state.session[i]; /* use this slot */
464 /* now init the session struct wisely */
465 store->sessionid = ssl_sessionid;
466 store->idsize = idsize;
467 store->age = *general_age; /* set current age */
469 /* free it if there's one already present */
471 store->name = clone_host; /* clone host name */
472 store->remote_port = conn->remote_port; /* port number */
476 if(SSLSESSION_SHARED(data))
477 Curl_share_unlock(data, CURL_LOCK_DATA_SSL_SESSION);
479 if(!Curl_clone_ssl_config(&conn->ssl_config, &store->ssl_config)) {
480 store->sessionid = NULL; /* let caller free sessionid */
482 return CURLE_OUT_OF_MEMORY;
489 void Curl_ssl_close_all(struct SessionHandle *data)
492 /* kill the session ID cache if not shared */
493 if(data->state.session && !SSLSESSION_SHARED(data)) {
494 for(i = 0; i < data->set.ssl.max_ssl_sessions; i++)
495 /* the single-killer function handles empty table slots */
496 Curl_ssl_kill_session(&data->state.session[i]);
498 /* free the cache data */
499 Curl_safefree(data->state.session);
502 curlssl_close_all(data);
505 void Curl_ssl_close(struct connectdata *conn, int sockindex)
507 DEBUGASSERT((sockindex <= 1) && (sockindex >= -1));
508 curlssl_close(conn, sockindex);
511 CURLcode Curl_ssl_shutdown(struct connectdata *conn, int sockindex)
513 if(curlssl_shutdown(conn, sockindex))
514 return CURLE_SSL_SHUTDOWN_FAILED;
516 conn->ssl[sockindex].use = FALSE; /* get back to ordinary socket usage */
517 conn->ssl[sockindex].state = ssl_connection_none;
519 conn->recv[sockindex] = Curl_recv_plain;
520 conn->send[sockindex] = Curl_send_plain;
525 /* Selects an SSL crypto engine
527 CURLcode Curl_ssl_set_engine(struct SessionHandle *data, const char *engine)
529 return curlssl_set_engine(data, engine);
532 /* Selects the default SSL crypto engine
534 CURLcode Curl_ssl_set_engine_default(struct SessionHandle *data)
536 return curlssl_set_engine_default(data);
539 /* Return list of OpenSSL crypto engine names. */
540 struct curl_slist *Curl_ssl_engines_list(struct SessionHandle *data)
542 return curlssl_engines_list(data);
546 * This sets up a session ID cache to the specified size. Make sure this code
547 * is agnostic to what underlying SSL technology we use.
549 CURLcode Curl_ssl_initsessions(struct SessionHandle *data, size_t amount)
551 struct curl_ssl_session *session;
553 if(data->state.session)
554 /* this is just a precaution to prevent multiple inits */
557 session = calloc(amount, sizeof(struct curl_ssl_session));
559 return CURLE_OUT_OF_MEMORY;
561 /* store the info in the SSL section */
562 data->set.ssl.max_ssl_sessions = amount;
563 data->state.session = session;
564 data->state.sessionage = 1; /* this is brand new */
568 size_t Curl_ssl_version(char *buffer, size_t size)
570 return curlssl_version(buffer, size);
574 * This function tries to determine connection status.
577 * 1 means the connection is still in place
578 * 0 means the connection has been closed
579 * -1 means the connection status is unknown
581 int Curl_ssl_check_cxn(struct connectdata *conn)
583 return curlssl_check_cxn(conn);
586 bool Curl_ssl_data_pending(const struct connectdata *conn,
589 return curlssl_data_pending(conn, connindex);
592 void Curl_ssl_free_certinfo(struct SessionHandle *data)
595 struct curl_certinfo *ci = &data->info.certs;
597 if(ci->num_of_certs) {
598 /* free all individual lists used */
599 for(i=0; i<ci->num_of_certs; i++) {
600 curl_slist_free_all(ci->certinfo[i]);
601 ci->certinfo[i] = NULL;
604 free(ci->certinfo); /* free the actual array too */
606 ci->num_of_certs = 0;
610 CURLcode Curl_ssl_init_certinfo(struct SessionHandle *data, int num)
612 struct curl_certinfo *ci = &data->info.certs;
613 struct curl_slist **table;
615 /* Free any previous certificate information structures */
616 Curl_ssl_free_certinfo(data);
618 /* Allocate the required certificate information structures */
619 table = calloc((size_t) num, sizeof(struct curl_slist *));
621 return CURLE_OUT_OF_MEMORY;
623 ci->num_of_certs = num;
624 ci->certinfo = table;
630 * 'value' is NOT a zero terminated string
632 CURLcode Curl_ssl_push_certinfo_len(struct SessionHandle *data,
638 struct curl_certinfo * ci = &data->info.certs;
640 struct curl_slist * nl;
641 CURLcode result = CURLE_OK;
642 size_t labellen = strlen(label);
643 size_t outlen = labellen + 1 + valuelen + 1; /* label:value\0 */
645 output = malloc(outlen);
647 return CURLE_OUT_OF_MEMORY;
649 /* sprintf the label and colon */
650 snprintf(output, outlen, "%s:", label);
652 /* memcpy the value (it might not be zero terminated) */
653 memcpy(&output[labellen+1], value, valuelen);
655 /* zero terminate the output */
656 output[labellen + 1 + valuelen] = 0;
658 nl = Curl_slist_append_nodup(ci->certinfo[certnum], output);
661 curl_slist_free_all(ci->certinfo[certnum]);
662 result = CURLE_OUT_OF_MEMORY;
665 ci->certinfo[certnum] = nl;
670 * This is a convenience function for push_certinfo_len that takes a zero
673 CURLcode Curl_ssl_push_certinfo(struct SessionHandle *data,
678 size_t valuelen = strlen(value);
680 return Curl_ssl_push_certinfo_len(data, certnum, label, value, valuelen);
683 int Curl_ssl_random(struct SessionHandle *data,
684 unsigned char *entropy,
687 return curlssl_random(data, entropy, length);
691 * Public key pem to der conversion
694 static CURLcode pubkey_pem_to_der(const char *pem,
695 unsigned char **der, size_t *der_len)
697 char *stripped_pem, *begin_pos, *end_pos;
698 size_t pem_count, stripped_pem_count = 0, pem_len;
701 /* if no pem, exit. */
703 return CURLE_BAD_CONTENT_ENCODING;
705 begin_pos = strstr(pem, "-----BEGIN PUBLIC KEY-----");
707 return CURLE_BAD_CONTENT_ENCODING;
709 pem_count = begin_pos - pem;
710 /* Invalid if not at beginning AND not directly following \n */
711 if(0 != pem_count && '\n' != pem[pem_count - 1])
712 return CURLE_BAD_CONTENT_ENCODING;
714 /* 26 is length of "-----BEGIN PUBLIC KEY-----" */
717 /* Invalid if not directly following \n */
718 end_pos = strstr(pem + pem_count, "\n-----END PUBLIC KEY-----");
720 return CURLE_BAD_CONTENT_ENCODING;
722 pem_len = end_pos - pem;
724 stripped_pem = malloc(pem_len - pem_count + 1);
726 return CURLE_OUT_OF_MEMORY;
729 * Here we loop through the pem array one character at a time between the
730 * correct indices, and place each character that is not '\n' or '\r'
731 * into the stripped_pem array, which should represent the raw base64 string
733 while(pem_count < pem_len) {
734 if('\n' != pem[pem_count] && '\r' != pem[pem_count])
735 stripped_pem[stripped_pem_count++] = pem[pem_count];
738 /* Place the null terminator in the correct place */
739 stripped_pem[stripped_pem_count] = '\0';
741 result = Curl_base64_decode(stripped_pem, der, der_len);
743 Curl_safefree(stripped_pem);
749 * Generic pinned public key check.
752 CURLcode Curl_pin_peer_pubkey(const char *pinnedpubkey,
753 const unsigned char *pubkey, size_t pubkeylen)
756 unsigned char *buf = NULL, *pem_ptr = NULL;
758 size_t size, pem_len;
760 CURLcode result = CURLE_SSL_PINNEDPUBKEYNOTMATCH;
762 /* if a path wasn't specified, don't pin */
765 if(!pubkey || !pubkeylen)
767 fp = fopen(pinnedpubkey, "rb");
772 /* Determine the file's size */
773 if(fseek(fp, 0, SEEK_END))
775 filesize = ftell(fp);
776 if(fseek(fp, 0, SEEK_SET))
778 if(filesize < 0 || filesize > MAX_PINNED_PUBKEY_SIZE)
782 * if the size of our certificate is bigger than the file
783 * size then it can't match
785 size = curlx_sotouz((curl_off_t) filesize);
790 * Allocate buffer for the pinned key
791 * With 1 additional byte for null terminator in case of PEM key
793 buf = malloc(size + 1);
797 /* Returns number of elements read, which should be 1 */
798 if((int) fread(buf, size, 1, fp) != 1)
801 /* If the sizes are the same, it can't be base64 encoded, must be der */
802 if(pubkeylen == size) {
803 if(!memcmp(pubkey, buf, pubkeylen))
809 * Otherwise we will assume it's PEM and try to decode it
810 * after placing null terminator
813 pem_read = pubkey_pem_to_der((const char *)buf, &pem_ptr, &pem_len);
814 /* if it wasn't read successfully, exit */
819 * if the size of our certificate doesn't match the size of
820 * the decoded file, they can't be the same, otherwise compare
822 if(pubkeylen == pem_len && !memcmp(pubkey, pem_ptr, pubkeylen))
827 Curl_safefree(pem_ptr);
833 void Curl_ssl_md5sum(unsigned char *tmp, /* input */
835 unsigned char *md5sum, /* output */
838 #ifdef curlssl_md5sum
839 curlssl_md5sum(tmp, tmplen, md5sum, md5len);
845 MD5pw = Curl_MD5_init(Curl_DIGEST_MD5);
846 Curl_MD5_update(MD5pw, tmp, curlx_uztoui(tmplen));
847 Curl_MD5_final(MD5pw, md5sum);
852 * Check whether the SSL backend supports the status_request extension.
854 bool Curl_ssl_cert_status_request(void)
856 #ifdef curlssl_cert_status_request
857 return curlssl_cert_status_request();