source: UsbWattMeter/trunk/curl-7.47.1/lib/vtls/vtls.c@ 164

Last change on this file since 164 was 164, checked in by coas-nagasima, 6 years ago

TOPPERS/ECNLサンプルアプリ「USB充電器電力計」を追加

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
  • Property svn:mime-type set to text/x-csrc
File size: 25.7 KB
Line 
1/***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2015, 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.haxx.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 ***************************************************************************/
22
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
25 "backend" function.
26
27 SSL-functions in libcurl should call functions in this source file, and not
28 to any specific SSL-layer.
29
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
39
40 Note that this source code uses curlssl_* functions, and they are all
41 defines/macros #defined by the lib-specific header files.
42
43 "SSL/TLS Strong Encryption: An Introduction"
44 https://httpd.apache.org/docs/2.0/ssl/ssl_intro.html
45*/
46
47#include "curl_setup.h"
48
49#ifdef HAVE_SYS_TYPES_H
50#include <sys/types.h>
51#endif
52#ifdef HAVE_SYS_STAT_H
53#include <sys/stat.h>
54#endif
55#ifdef HAVE_FCNTL_H
56#include <fcntl.h>
57#endif
58
59#include "urldata.h"
60
61#include "vtls.h" /* generic SSL protos etc */
62#include "slist.h"
63#include "sendf.h"
64#include "rawstr.h"
65#include "url.h"
66#include "progress.h"
67#include "share.h"
68#include "timeval.h"
69#include "curl_md5.h"
70#include "warnless.h"
71#include "curl_base64.h"
72#include "curl_printf.h"
73
74/* The last #include files should be: */
75#include "curl_memory.h"
76#include "memdebug.h"
77
78/* convenience macro to check if this handle is using a shared SSL session */
79#define SSLSESSION_SHARED(data) (data->share && \
80 (data->share->specifier & \
81 (1<<CURL_LOCK_DATA_SSL_SESSION)))
82
83static bool safe_strequal(char* str1, char* str2)
84{
85 if(str1 && str2)
86 /* both pointers point to something then compare them */
87 return (0 != Curl_raw_equal(str1, str2)) ? TRUE : FALSE;
88 else
89 /* if both pointers are NULL then treat them as equal */
90 return (!str1 && !str2) ? TRUE : FALSE;
91}
92
93bool
94Curl_ssl_config_matches(struct ssl_config_data* data,
95 struct ssl_config_data* needle)
96{
97 if((data->version == needle->version) &&
98 (data->verifypeer == needle->verifypeer) &&
99 (data->verifyhost == needle->verifyhost) &&
100 safe_strequal(data->CApath, needle->CApath) &&
101 safe_strequal(data->CAfile, needle->CAfile) &&
102 safe_strequal(data->random_file, needle->random_file) &&
103 safe_strequal(data->egdsocket, needle->egdsocket) &&
104 safe_strequal(data->cipher_list, needle->cipher_list))
105 return TRUE;
106
107 return FALSE;
108}
109
110bool
111Curl_clone_ssl_config(struct ssl_config_data *source,
112 struct ssl_config_data *dest)
113{
114 dest->sessionid = source->sessionid;
115 dest->verifyhost = source->verifyhost;
116 dest->verifypeer = source->verifypeer;
117 dest->version = source->version;
118
119 if(source->CAfile) {
120 dest->CAfile = strdup(source->CAfile);
121 if(!dest->CAfile)
122 return FALSE;
123 }
124 else
125 dest->CAfile = NULL;
126
127 if(source->CApath) {
128 dest->CApath = strdup(source->CApath);
129 if(!dest->CApath)
130 return FALSE;
131 }
132 else
133 dest->CApath = NULL;
134
135 if(source->cipher_list) {
136 dest->cipher_list = strdup(source->cipher_list);
137 if(!dest->cipher_list)
138 return FALSE;
139 }
140 else
141 dest->cipher_list = NULL;
142
143 if(source->egdsocket) {
144 dest->egdsocket = strdup(source->egdsocket);
145 if(!dest->egdsocket)
146 return FALSE;
147 }
148 else
149 dest->egdsocket = NULL;
150
151 if(source->random_file) {
152 dest->random_file = strdup(source->random_file);
153 if(!dest->random_file)
154 return FALSE;
155 }
156 else
157 dest->random_file = NULL;
158
159 return TRUE;
160}
161
162void Curl_free_ssl_config(struct ssl_config_data* sslc)
163{
164 Curl_safefree(sslc->CAfile);
165 Curl_safefree(sslc->CApath);
166 Curl_safefree(sslc->cipher_list);
167 Curl_safefree(sslc->egdsocket);
168 Curl_safefree(sslc->random_file);
169}
170
171
172/*
173 * Curl_rand() returns a random unsigned integer, 32bit.
174 *
175 * This non-SSL function is put here only because this file is the only one
176 * with knowledge of what the underlying SSL libraries provide in terms of
177 * randomizers.
178 *
179 * NOTE: 'data' may be passed in as NULL when coming from external API without
180 * easy handle!
181 *
182 */
183
184unsigned int Curl_rand(struct SessionHandle *data)
185{
186 unsigned int r = 0;
187 static unsigned int randseed;
188 static bool seeded = FALSE;
189
190#ifdef CURLDEBUG
191 char *force_entropy = getenv("CURL_ENTROPY");
192 if(force_entropy) {
193 if(!seeded) {
194 size_t elen = strlen(force_entropy);
195 size_t clen = sizeof(randseed);
196 size_t min = elen < clen ? elen : clen;
197 memcpy((char *)&randseed, force_entropy, min);
198 seeded = TRUE;
199 }
200 else
201 randseed++;
202 return randseed;
203 }
204#endif
205
206 /* data may be NULL! */
207 if(!Curl_ssl_random(data, (unsigned char *)&r, sizeof(r)))
208 return r;
209
210 /* If Curl_ssl_random() returns non-zero it couldn't offer randomness and we
211 instead perform a "best effort" */
212
213#ifdef RANDOM_FILE
214 if(!seeded) {
215 /* if there's a random file to read a seed from, use it */
216 int fd = open(RANDOM_FILE, O_RDONLY);
217 if(fd > -1) {
218 /* read random data into the randseed variable */
219 ssize_t nread = read(fd, &randseed, sizeof(randseed));
220 if(nread == sizeof(randseed))
221 seeded = TRUE;
222 close(fd);
223 }
224 }
225#endif
226
227 if(!seeded) {
228 struct timeval now = curlx_tvnow();
229 infof(data, "WARNING: Using weak random seed\n");
230 randseed += (unsigned int)now.tv_usec + (unsigned int)now.tv_sec;
231 randseed = randseed * 1103515245 + 12345;
232 randseed = randseed * 1103515245 + 12345;
233 randseed = randseed * 1103515245 + 12345;
234 seeded = TRUE;
235 }
236
237 /* Return an unsigned 32-bit pseudo-random number. */
238 r = randseed = randseed * 1103515245 + 12345;
239 return (r << 16) | ((r >> 16) & 0xFFFF);
240}
241
242int Curl_ssl_backend(void)
243{
244 return (int)CURL_SSL_BACKEND;
245}
246
247#ifdef USE_SSL
248
249/* "global" init done? */
250static bool init_ssl=FALSE;
251
252/**
253 * Global SSL init
254 *
255 * @retval 0 error initializing SSL
256 * @retval 1 SSL initialized successfully
257 */
258int Curl_ssl_init(void)
259{
260 /* make sure this is only done once */
261 if(init_ssl)
262 return 1;
263 init_ssl = TRUE; /* never again */
264
265 return curlssl_init();
266}
267
268
269/* Global cleanup */
270void Curl_ssl_cleanup(void)
271{
272 if(init_ssl) {
273 /* only cleanup if we did a previous init */
274 curlssl_cleanup();
275 init_ssl = FALSE;
276 }
277}
278
279static bool ssl_prefs_check(struct SessionHandle *data)
280{
281 /* check for CURLOPT_SSLVERSION invalid parameter value */
282 if((data->set.ssl.version < 0)
283 || (data->set.ssl.version >= CURL_SSLVERSION_LAST)) {
284 failf(data, "Unrecognized parameter value passed via CURLOPT_SSLVERSION");
285 return FALSE;
286 }
287 return TRUE;
288}
289
290CURLcode
291Curl_ssl_connect(struct connectdata *conn, int sockindex)
292{
293 CURLcode result;
294
295 if(!ssl_prefs_check(conn->data))
296 return CURLE_SSL_CONNECT_ERROR;
297
298 /* mark this is being ssl-enabled from here on. */
299 conn->ssl[sockindex].use = TRUE;
300 conn->ssl[sockindex].state = ssl_connection_negotiating;
301
302 result = curlssl_connect(conn, sockindex);
303
304 if(!result)
305 Curl_pgrsTime(conn->data, TIMER_APPCONNECT); /* SSL is connected */
306
307 return result;
308}
309
310CURLcode
311Curl_ssl_connect_nonblocking(struct connectdata *conn, int sockindex,
312 bool *done)
313{
314 CURLcode result;
315
316 if(!ssl_prefs_check(conn->data))
317 return CURLE_SSL_CONNECT_ERROR;
318
319 /* mark this is being ssl requested from here on. */
320 conn->ssl[sockindex].use = TRUE;
321#ifdef curlssl_connect_nonblocking
322 result = curlssl_connect_nonblocking(conn, sockindex, done);
323#else
324 *done = TRUE; /* fallback to BLOCKING */
325 result = curlssl_connect(conn, sockindex);
326#endif /* non-blocking connect support */
327 if(!result && *done)
328 Curl_pgrsTime(conn->data, TIMER_APPCONNECT); /* SSL is connected */
329 return result;
330}
331
332/*
333 * Check if there's a session ID for the given connection in the cache, and if
334 * there's one suitable, it is provided. Returns TRUE when no entry matched.
335 */
336bool Curl_ssl_getsessionid(struct connectdata *conn,
337 void **ssl_sessionid,
338 size_t *idsize) /* set 0 if unknown */
339{
340 struct curl_ssl_session *check;
341 struct SessionHandle *data = conn->data;
342 size_t i;
343 long *general_age;
344 bool no_match = TRUE;
345
346 *ssl_sessionid = NULL;
347
348 if(!conn->ssl_config.sessionid)
349 /* session ID re-use is disabled */
350 return TRUE;
351
352 /* Lock if shared */
353 if(SSLSESSION_SHARED(data)) {
354 Curl_share_lock(data, CURL_LOCK_DATA_SSL_SESSION, CURL_LOCK_ACCESS_SINGLE);
355 general_age = &data->share->sessionage;
356 }
357 else
358 general_age = &data->state.sessionage;
359
360 for(i = 0; i < data->set.ssl.max_ssl_sessions; i++) {
361 check = &data->state.session[i];
362 if(!check->sessionid)
363 /* not session ID means blank entry */
364 continue;
365 if(Curl_raw_equal(conn->host.name, check->name) &&
366 (conn->remote_port == check->remote_port) &&
367 Curl_ssl_config_matches(&conn->ssl_config, &check->ssl_config)) {
368 /* yes, we have a session ID! */
369 (*general_age)++; /* increase general age */
370 check->age = *general_age; /* set this as used in this age */
371 *ssl_sessionid = check->sessionid;
372 if(idsize)
373 *idsize = check->idsize;
374 no_match = FALSE;
375 break;
376 }
377 }
378
379 /* Unlock */
380 if(SSLSESSION_SHARED(data))
381 Curl_share_unlock(data, CURL_LOCK_DATA_SSL_SESSION);
382
383 return no_match;
384}
385
386/*
387 * Kill a single session ID entry in the cache.
388 */
389void Curl_ssl_kill_session(struct curl_ssl_session *session)
390{
391 if(session->sessionid) {
392 /* defensive check */
393
394 /* free the ID the SSL-layer specific way */
395 curlssl_session_free(session->sessionid);
396
397 session->sessionid = NULL;
398 session->age = 0; /* fresh */
399
400 Curl_free_ssl_config(&session->ssl_config);
401
402 Curl_safefree(session->name);
403 }
404}
405
406/*
407 * Delete the given session ID from the cache.
408 */
409void Curl_ssl_delsessionid(struct connectdata *conn, void *ssl_sessionid)
410{
411 size_t i;
412 struct SessionHandle *data=conn->data;
413
414 if(SSLSESSION_SHARED(data))
415 Curl_share_lock(data, CURL_LOCK_DATA_SSL_SESSION, CURL_LOCK_ACCESS_SINGLE);
416
417 for(i = 0; i < data->set.ssl.max_ssl_sessions; i++) {
418 struct curl_ssl_session *check = &data->state.session[i];
419
420 if(check->sessionid == ssl_sessionid) {
421 Curl_ssl_kill_session(check);
422 break;
423 }
424 }
425
426 if(SSLSESSION_SHARED(data))
427 Curl_share_unlock(data, CURL_LOCK_DATA_SSL_SESSION);
428}
429
430/*
431 * Store session id in the session cache. The ID passed on to this function
432 * must already have been extracted and allocated the proper way for the SSL
433 * layer. Curl_XXXX_session_free() will be called to free/kill the session ID
434 * later on.
435 */
436CURLcode Curl_ssl_addsessionid(struct connectdata *conn,
437 void *ssl_sessionid,
438 size_t idsize)
439{
440 size_t i;
441 struct SessionHandle *data=conn->data; /* the mother of all structs */
442 struct curl_ssl_session *store = &data->state.session[0];
443 long oldest_age=data->state.session[0].age; /* zero if unused */
444 char *clone_host;
445 long *general_age;
446
447 /* Even though session ID re-use might be disabled, that only disables USING
448 IT. We still store it here in case the re-using is again enabled for an
449 upcoming transfer */
450
451 clone_host = strdup(conn->host.name);
452 if(!clone_host)
453 return CURLE_OUT_OF_MEMORY; /* bail out */
454
455 /* Now we should add the session ID and the host name to the cache, (remove
456 the oldest if necessary) */
457
458 /* If using shared SSL session, lock! */
459 if(SSLSESSION_SHARED(data)) {
460 Curl_share_lock(data, CURL_LOCK_DATA_SSL_SESSION, CURL_LOCK_ACCESS_SINGLE);
461 general_age = &data->share->sessionage;
462 }
463 else {
464 general_age = &data->state.sessionage;
465 }
466
467 /* find an empty slot for us, or find the oldest */
468 for(i = 1; (i < data->set.ssl.max_ssl_sessions) &&
469 data->state.session[i].sessionid; i++) {
470 if(data->state.session[i].age < oldest_age) {
471 oldest_age = data->state.session[i].age;
472 store = &data->state.session[i];
473 }
474 }
475 if(i == data->set.ssl.max_ssl_sessions)
476 /* cache is full, we must "kill" the oldest entry! */
477 Curl_ssl_kill_session(store);
478 else
479 store = &data->state.session[i]; /* use this slot */
480
481 /* now init the session struct wisely */
482 store->sessionid = ssl_sessionid;
483 store->idsize = idsize;
484 store->age = *general_age; /* set current age */
485 /* free it if there's one already present */
486 free(store->name);
487 store->name = clone_host; /* clone host name */
488 store->remote_port = conn->remote_port; /* port number */
489
490
491 /* Unlock */
492 if(SSLSESSION_SHARED(data))
493 Curl_share_unlock(data, CURL_LOCK_DATA_SSL_SESSION);
494
495 if(!Curl_clone_ssl_config(&conn->ssl_config, &store->ssl_config)) {
496 store->sessionid = NULL; /* let caller free sessionid */
497 free(clone_host);
498 return CURLE_OUT_OF_MEMORY;
499 }
500
501 return CURLE_OK;
502}
503
504
505void Curl_ssl_close_all(struct SessionHandle *data)
506{
507 size_t i;
508 /* kill the session ID cache if not shared */
509 if(data->state.session && !SSLSESSION_SHARED(data)) {
510 for(i = 0; i < data->set.ssl.max_ssl_sessions; i++)
511 /* the single-killer function handles empty table slots */
512 Curl_ssl_kill_session(&data->state.session[i]);
513
514 /* free the cache data */
515 Curl_safefree(data->state.session);
516 }
517
518 curlssl_close_all(data);
519}
520
521void Curl_ssl_close(struct connectdata *conn, int sockindex)
522{
523 DEBUGASSERT((sockindex <= 1) && (sockindex >= -1));
524 curlssl_close(conn, sockindex);
525}
526
527CURLcode Curl_ssl_shutdown(struct connectdata *conn, int sockindex)
528{
529 if(curlssl_shutdown(conn, sockindex))
530 return CURLE_SSL_SHUTDOWN_FAILED;
531
532 conn->ssl[sockindex].use = FALSE; /* get back to ordinary socket usage */
533 conn->ssl[sockindex].state = ssl_connection_none;
534
535 conn->recv[sockindex] = Curl_recv_plain;
536 conn->send[sockindex] = Curl_send_plain;
537
538 return CURLE_OK;
539}
540
541/* Selects an SSL crypto engine
542 */
543CURLcode Curl_ssl_set_engine(struct SessionHandle *data, const char *engine)
544{
545 return curlssl_set_engine(data, engine);
546}
547
548/* Selects the default SSL crypto engine
549 */
550CURLcode Curl_ssl_set_engine_default(struct SessionHandle *data)
551{
552 return curlssl_set_engine_default(data);
553}
554
555/* Return list of OpenSSL crypto engine names. */
556struct curl_slist *Curl_ssl_engines_list(struct SessionHandle *data)
557{
558 return curlssl_engines_list(data);
559}
560
561/*
562 * This sets up a session ID cache to the specified size. Make sure this code
563 * is agnostic to what underlying SSL technology we use.
564 */
565CURLcode Curl_ssl_initsessions(struct SessionHandle *data, size_t amount)
566{
567 struct curl_ssl_session *session;
568
569 if(data->state.session)
570 /* this is just a precaution to prevent multiple inits */
571 return CURLE_OK;
572
573 session = calloc(amount, sizeof(struct curl_ssl_session));
574 if(!session)
575 return CURLE_OUT_OF_MEMORY;
576
577 /* store the info in the SSL section */
578 data->set.ssl.max_ssl_sessions = amount;
579 data->state.session = session;
580 data->state.sessionage = 1; /* this is brand new */
581 return CURLE_OK;
582}
583
584size_t Curl_ssl_version(char *buffer, size_t size)
585{
586 return curlssl_version(buffer, size);
587}
588
589/*
590 * This function tries to determine connection status.
591 *
592 * Return codes:
593 * 1 means the connection is still in place
594 * 0 means the connection has been closed
595 * -1 means the connection status is unknown
596 */
597int Curl_ssl_check_cxn(struct connectdata *conn)
598{
599 return curlssl_check_cxn(conn);
600}
601
602bool Curl_ssl_data_pending(const struct connectdata *conn,
603 int connindex)
604{
605 return curlssl_data_pending(conn, connindex);
606}
607
608void Curl_ssl_free_certinfo(struct SessionHandle *data)
609{
610 int i;
611 struct curl_certinfo *ci = &data->info.certs;
612
613 if(ci->num_of_certs) {
614 /* free all individual lists used */
615 for(i=0; i<ci->num_of_certs; i++) {
616 curl_slist_free_all(ci->certinfo[i]);
617 ci->certinfo[i] = NULL;
618 }
619
620 free(ci->certinfo); /* free the actual array too */
621 ci->certinfo = NULL;
622 ci->num_of_certs = 0;
623 }
624}
625
626CURLcode Curl_ssl_init_certinfo(struct SessionHandle *data, int num)
627{
628 struct curl_certinfo *ci = &data->info.certs;
629 struct curl_slist **table;
630
631 /* Free any previous certificate information structures */
632 Curl_ssl_free_certinfo(data);
633
634 /* Allocate the required certificate information structures */
635 table = calloc((size_t) num, sizeof(struct curl_slist *));
636 if(!table)
637 return CURLE_OUT_OF_MEMORY;
638
639 ci->num_of_certs = num;
640 ci->certinfo = table;
641
642 return CURLE_OK;
643}
644
645/*
646 * 'value' is NOT a zero terminated string
647 */
648CURLcode Curl_ssl_push_certinfo_len(struct SessionHandle *data,
649 int certnum,
650 const char *label,
651 const char *value,
652 size_t valuelen)
653{
654 struct curl_certinfo * ci = &data->info.certs;
655 char * output;
656 struct curl_slist * nl;
657 CURLcode result = CURLE_OK;
658 size_t labellen = strlen(label);
659 size_t outlen = labellen + 1 + valuelen + 1; /* label:value\0 */
660
661 output = malloc(outlen);
662 if(!output)
663 return CURLE_OUT_OF_MEMORY;
664
665 /* sprintf the label and colon */
666 snprintf(output, outlen, "%s:", label);
667
668 /* memcpy the value (it might not be zero terminated) */
669 memcpy(&output[labellen+1], value, valuelen);
670
671 /* zero terminate the output */
672 output[labellen + 1 + valuelen] = 0;
673
674 nl = Curl_slist_append_nodup(ci->certinfo[certnum], output);
675 if(!nl) {
676 free(output);
677 curl_slist_free_all(ci->certinfo[certnum]);
678 result = CURLE_OUT_OF_MEMORY;
679 }
680
681 ci->certinfo[certnum] = nl;
682 return result;
683}
684
685/*
686 * This is a convenience function for push_certinfo_len that takes a zero
687 * terminated value.
688 */
689CURLcode Curl_ssl_push_certinfo(struct SessionHandle *data,
690 int certnum,
691 const char *label,
692 const char *value)
693{
694 size_t valuelen = strlen(value);
695
696 return Curl_ssl_push_certinfo_len(data, certnum, label, value, valuelen);
697}
698
699int Curl_ssl_random(struct SessionHandle *data,
700 unsigned char *entropy,
701 size_t length)
702{
703 return curlssl_random(data, entropy, length);
704}
705
706/*
707 * Public key pem to der conversion
708 */
709
710static CURLcode pubkey_pem_to_der(const char *pem,
711 unsigned char **der, size_t *der_len)
712{
713 char *stripped_pem, *begin_pos, *end_pos;
714 size_t pem_count, stripped_pem_count = 0, pem_len;
715 CURLcode result;
716
717 /* if no pem, exit. */
718 if(!pem)
719 return CURLE_BAD_CONTENT_ENCODING;
720
721 begin_pos = strstr(pem, "-----BEGIN PUBLIC KEY-----");
722 if(!begin_pos)
723 return CURLE_BAD_CONTENT_ENCODING;
724
725 pem_count = begin_pos - pem;
726 /* Invalid if not at beginning AND not directly following \n */
727 if(0 != pem_count && '\n' != pem[pem_count - 1])
728 return CURLE_BAD_CONTENT_ENCODING;
729
730 /* 26 is length of "-----BEGIN PUBLIC KEY-----" */
731 pem_count += 26;
732
733 /* Invalid if not directly following \n */
734 end_pos = strstr(pem + pem_count, "\n-----END PUBLIC KEY-----");
735 if(!end_pos)
736 return CURLE_BAD_CONTENT_ENCODING;
737
738 pem_len = end_pos - pem;
739
740 stripped_pem = malloc(pem_len - pem_count + 1);
741 if(!stripped_pem)
742 return CURLE_OUT_OF_MEMORY;
743
744 /*
745 * Here we loop through the pem array one character at a time between the
746 * correct indices, and place each character that is not '\n' or '\r'
747 * into the stripped_pem array, which should represent the raw base64 string
748 */
749 while(pem_count < pem_len) {
750 if('\n' != pem[pem_count] && '\r' != pem[pem_count])
751 stripped_pem[stripped_pem_count++] = pem[pem_count];
752 ++pem_count;
753 }
754 /* Place the null terminator in the correct place */
755 stripped_pem[stripped_pem_count] = '\0';
756
757 result = Curl_base64_decode(stripped_pem, der, der_len);
758
759 Curl_safefree(stripped_pem);
760
761 return result;
762}
763
764/*
765 * Generic pinned public key check.
766 */
767
768CURLcode Curl_pin_peer_pubkey(struct SessionHandle *data,
769 const char *pinnedpubkey,
770 const unsigned char *pubkey, size_t pubkeylen)
771{
772 FILE *fp;
773 unsigned char *buf = NULL, *pem_ptr = NULL;
774 long filesize;
775 size_t size, pem_len;
776 CURLcode pem_read;
777 CURLcode result = CURLE_SSL_PINNEDPUBKEYNOTMATCH;
778#ifdef curlssl_sha256sum
779 CURLcode encode;
780 size_t encodedlen, pinkeylen;
781 char *encoded, *pinkeycopy, *begin_pos, *end_pos;
782 unsigned char *sha256sumdigest = NULL;
783#endif
784
785 /* if a path wasn't specified, don't pin */
786 if(!pinnedpubkey)
787 return CURLE_OK;
788 if(!pubkey || !pubkeylen)
789 return result;
790
791#ifdef curlssl_sha256sum
792 /* only do this if pinnedpubkey starts with "sha256//", length 8 */
793 if(strncmp(pinnedpubkey, "sha256//", 8) == 0) {
794 /* compute sha256sum of public key */
795 sha256sumdigest = malloc(SHA256_DIGEST_LENGTH);
796 if(!sha256sumdigest)
797 return CURLE_OUT_OF_MEMORY;
798 curlssl_sha256sum(pubkey, pubkeylen,
799 sha256sumdigest, SHA256_DIGEST_LENGTH);
800 encode = Curl_base64_encode(data, (char *)sha256sumdigest,
801 SHA256_DIGEST_LENGTH, &encoded, &encodedlen);
802 Curl_safefree(sha256sumdigest);
803
804 if(encode)
805 return encode;
806
807 infof(data, "\t public key hash: sha256//%s\n", encoded);
808
809 /* it starts with sha256//, copy so we can modify it */
810 pinkeylen = strlen(pinnedpubkey) + 1;
811 pinkeycopy = malloc(pinkeylen);
812 if(!pinkeycopy) {
813 Curl_safefree(encoded);
814 return CURLE_OUT_OF_MEMORY;
815 }
816 memcpy(pinkeycopy, pinnedpubkey, pinkeylen);
817 /* point begin_pos to the copy, and start extracting keys */
818 begin_pos = pinkeycopy;
819 do {
820 end_pos = strstr(begin_pos, ";sha256//");
821 /*
822 * if there is an end_pos, null terminate,
823 * otherwise it'll go to the end of the original string
824 */
825 if(end_pos)
826 end_pos[0] = '\0';
827
828 /* compare base64 sha256 digests, 8 is the length of "sha256//" */
829 if(encodedlen == strlen(begin_pos + 8) &&
830 !memcmp(encoded, begin_pos + 8, encodedlen)) {
831 result = CURLE_OK;
832 break;
833 }
834
835 /*
836 * change back the null-terminator we changed earlier,
837 * and look for next begin
838 */
839 if(end_pos) {
840 end_pos[0] = ';';
841 begin_pos = strstr(end_pos, "sha256//");
842 }
843 } while(end_pos && begin_pos);
844 Curl_safefree(encoded);
845 Curl_safefree(pinkeycopy);
846 return result;
847 }
848#else
849 (void)data;
850#endif
851
852 fp = fopen(pinnedpubkey, "rb");
853 if(!fp)
854 return result;
855
856 do {
857 /* Determine the file's size */
858 if(fseek(fp, 0, SEEK_END))
859 break;
860 filesize = ftell(fp);
861 if(fseek(fp, 0, SEEK_SET))
862 break;
863 if(filesize < 0 || filesize > MAX_PINNED_PUBKEY_SIZE)
864 break;
865
866 /*
867 * if the size of our certificate is bigger than the file
868 * size then it can't match
869 */
870 size = curlx_sotouz((curl_off_t) filesize);
871 if(pubkeylen > size)
872 break;
873
874 /*
875 * Allocate buffer for the pinned key
876 * With 1 additional byte for null terminator in case of PEM key
877 */
878 buf = malloc(size + 1);
879 if(!buf)
880 break;
881
882 /* Returns number of elements read, which should be 1 */
883 if((int) fread(buf, size, 1, fp) != 1)
884 break;
885
886 /* If the sizes are the same, it can't be base64 encoded, must be der */
887 if(pubkeylen == size) {
888 if(!memcmp(pubkey, buf, pubkeylen))
889 result = CURLE_OK;
890 break;
891 }
892
893 /*
894 * Otherwise we will assume it's PEM and try to decode it
895 * after placing null terminator
896 */
897 buf[size] = '\0';
898 pem_read = pubkey_pem_to_der((const char *)buf, &pem_ptr, &pem_len);
899 /* if it wasn't read successfully, exit */
900 if(pem_read)
901 break;
902
903 /*
904 * if the size of our certificate doesn't match the size of
905 * the decoded file, they can't be the same, otherwise compare
906 */
907 if(pubkeylen == pem_len && !memcmp(pubkey, pem_ptr, pubkeylen))
908 result = CURLE_OK;
909 } while(0);
910
911 Curl_safefree(buf);
912 Curl_safefree(pem_ptr);
913 fclose(fp);
914
915 return result;
916}
917
918#ifndef CURL_DISABLE_CRYPTO_AUTH
919CURLcode Curl_ssl_md5sum(unsigned char *tmp, /* input */
920 size_t tmplen,
921 unsigned char *md5sum, /* output */
922 size_t md5len)
923{
924#ifdef curlssl_md5sum
925 curlssl_md5sum(tmp, tmplen, md5sum, md5len);
926#else
927 MD5_context *MD5pw;
928
929 (void) md5len;
930
931 MD5pw = Curl_MD5_init(Curl_DIGEST_MD5);
932 if(!MD5pw)
933 return CURLE_OUT_OF_MEMORY;
934 Curl_MD5_update(MD5pw, tmp, curlx_uztoui(tmplen));
935 Curl_MD5_final(MD5pw, md5sum);
936#endif
937 return CURLE_OK;
938}
939#endif
940
941/*
942 * Check whether the SSL backend supports the status_request extension.
943 */
944bool Curl_ssl_cert_status_request(void)
945{
946#ifdef curlssl_cert_status_request
947 return curlssl_cert_status_request();
948#else
949 return FALSE;
950#endif
951}
952
953/*
954 * Check whether the SSL backend supports false start.
955 */
956bool Curl_ssl_false_start(void)
957{
958#ifdef curlssl_false_start
959 return curlssl_false_start();
960#else
961 return FALSE;
962#endif
963}
964
965#endif /* USE_SSL */
Note: See TracBrowser for help on using the repository browser.