forked from wolfSSL/wolfssl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathssl_sess.c
4542 lines (3975 loc) · 138 KB
/
ssl_sess.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* ssl_sess.c
*
* Copyright (C) 2006-2024 wolfSSL Inc.
*
* This file is part of wolfSSL.
*
* wolfSSL is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* wolfSSL is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <wolfssl/wolfcrypt/settings.h>
#if !defined(WOLFSSL_SSL_SESS_INCLUDED)
#ifndef WOLFSSL_IGNORE_FILE_WARN
#warning ssl_sess.c does not need to be compiled separately from ssl.c
#endif
#else
#ifndef NO_SESSION_CACHE
/* basic config gives a cache with 33 sessions, adequate for clients and
embedded servers
TITAN_SESSION_CACHE allows just over 2 million sessions, for servers
with titanic amounts of memory with long session ID timeouts and high
levels of traffic.
ENABLE_SESSION_CACHE_ROW_LOCK: Allows row level locking for increased
performance with large session caches
HUGE_SESSION_CACHE yields 65,791 sessions, for servers under heavy load,
allows over 13,000 new sessions per minute or over 200 new sessions per
second
BIG_SESSION_CACHE yields 20,027 sessions
MEDIUM_SESSION_CACHE allows 1055 sessions, adequate for servers that
aren't under heavy load, basically allows 200 new sessions per minute
SMALL_SESSION_CACHE only stores 6 sessions, good for embedded clients
or systems where the default of is too much RAM.
SessionCache takes about 2K, ClientCache takes about 3Kbytes
MICRO_SESSION_CACHE only stores 1 session, good for embedded clients
or systems where memory is at a premium.
SessionCache takes about 400 bytes, ClientCache takes 576 bytes
default SESSION_CACHE stores 33 sessions (no XXX_SESSION_CACHE defined)
SessionCache takes about 13K bytes, ClientCache takes 17K bytes
*/
#if defined(TITAN_SESSION_CACHE)
#define SESSIONS_PER_ROW 31
#define SESSION_ROWS 64937
#ifndef ENABLE_SESSION_CACHE_ROW_LOCK
#define ENABLE_SESSION_CACHE_ROW_LOCK
#endif
#elif defined(HUGE_SESSION_CACHE)
#define SESSIONS_PER_ROW 11
#define SESSION_ROWS 5981
#elif defined(BIG_SESSION_CACHE)
#define SESSIONS_PER_ROW 7
#define SESSION_ROWS 2861
#elif defined(MEDIUM_SESSION_CACHE)
#define SESSIONS_PER_ROW 5
#define SESSION_ROWS 211
#elif defined(SMALL_SESSION_CACHE)
#define SESSIONS_PER_ROW 2
#define SESSION_ROWS 3
#elif defined(MICRO_SESSION_CACHE)
#define SESSIONS_PER_ROW 1
#define SESSION_ROWS 1
#else
#define SESSIONS_PER_ROW 3
#define SESSION_ROWS 11
#endif
#define INVALID_SESSION_ROW (-1)
#ifdef NO_SESSION_CACHE_ROW_LOCK
#undef ENABLE_SESSION_CACHE_ROW_LOCK
#endif
typedef struct SessionRow {
int nextIdx; /* where to place next one */
int totalCount; /* sessions ever on this row */
#ifdef SESSION_CACHE_DYNAMIC_MEM
WOLFSSL_SESSION* Sessions[SESSIONS_PER_ROW];
void* heap;
#else
WOLFSSL_SESSION Sessions[SESSIONS_PER_ROW];
#endif
#ifdef ENABLE_SESSION_CACHE_ROW_LOCK
/* not included in import/export */
wolfSSL_RwLock row_lock;
int lock_valid;
#endif
} SessionRow;
#define SIZEOF_SESSION_ROW (sizeof(WOLFSSL_SESSION) + (sizeof(int) * 2))
static WOLFSSL_GLOBAL SessionRow SessionCache[SESSION_ROWS];
#if defined(WOLFSSL_SESSION_STATS) && defined(WOLFSSL_PEAK_SESSIONS)
static WOLFSSL_GLOBAL word32 PeakSessions;
#endif
#ifdef ENABLE_SESSION_CACHE_ROW_LOCK
#define SESSION_ROW_RD_LOCK(row) wc_LockRwLock_Rd(&(row)->row_lock)
#define SESSION_ROW_WR_LOCK(row) wc_LockRwLock_Wr(&(row)->row_lock)
#define SESSION_ROW_UNLOCK(row) wc_UnLockRwLock(&(row)->row_lock);
#else
static WOLFSSL_GLOBAL wolfSSL_RwLock session_lock; /* SessionCache lock */
static WOLFSSL_GLOBAL int session_lock_valid = 0;
#define SESSION_ROW_RD_LOCK(row) wc_LockRwLock_Rd(&session_lock)
#define SESSION_ROW_WR_LOCK(row) wc_LockRwLock_Wr(&session_lock)
#define SESSION_ROW_UNLOCK(row) wc_UnLockRwLock(&session_lock);
#endif
#if !defined(NO_SESSION_CACHE_REF) && defined(NO_CLIENT_CACHE)
#error ClientCache is required when not using NO_SESSION_CACHE_REF
#endif
#ifndef NO_CLIENT_CACHE
#ifndef CLIENT_SESSIONS_MULTIPLIER
#ifdef NO_SESSION_CACHE_REF
#define CLIENT_SESSIONS_MULTIPLIER 1
#else
/* ClientSession objects are lightweight (compared to
* WOLFSSL_SESSION) so to decrease chance that user will reuse
* the wrong session, increase the ClientCache size. This will
* make the entire ClientCache about the size of one
* WOLFSSL_SESSION object. */
#define CLIENT_SESSIONS_MULTIPLIER 8
#endif
#endif
#define CLIENT_SESSIONS_PER_ROW \
(SESSIONS_PER_ROW * CLIENT_SESSIONS_MULTIPLIER)
#define CLIENT_SESSION_ROWS (SESSION_ROWS * CLIENT_SESSIONS_MULTIPLIER)
#if CLIENT_SESSIONS_PER_ROW > 65535
#error CLIENT_SESSIONS_PER_ROW too big
#endif
#if CLIENT_SESSION_ROWS > 65535
#error CLIENT_SESSION_ROWS too big
#endif
struct ClientSession {
word16 serverRow; /* SessionCache Row id */
word16 serverIdx; /* SessionCache Idx (column) */
word32 sessionIDHash;
};
#ifndef WOLFSSL_CLIENT_SESSION_DEFINED
typedef struct ClientSession ClientSession;
#define WOLFSSL_CLIENT_SESSION_DEFINED
#endif
typedef struct ClientRow {
int nextIdx; /* where to place next one */
int totalCount; /* sessions ever on this row */
ClientSession Clients[CLIENT_SESSIONS_PER_ROW];
} ClientRow;
static WOLFSSL_GLOBAL ClientRow ClientCache[CLIENT_SESSION_ROWS];
/* Client Cache */
/* uses session mutex */
/* ClientCache mutex */
static WOLFSSL_GLOBAL wolfSSL_Mutex clisession_mutex
WOLFSSL_MUTEX_INITIALIZER_CLAUSE(clisession_mutex);
#ifndef WOLFSSL_MUTEX_INITIALIZER
static WOLFSSL_GLOBAL int clisession_mutex_valid = 0;
#endif
#endif /* !NO_CLIENT_CACHE */
void EvictSessionFromCache(WOLFSSL_SESSION* session)
{
#ifdef HAVE_EX_DATA
int save_ownExData = session->ownExData;
session->ownExData = 1; /* Make sure ex_data access doesn't lead back
* into the cache. */
#endif
#if defined(HAVE_EXT_CACHE) || defined(HAVE_EX_DATA)
if (session->rem_sess_cb != NULL) {
session->rem_sess_cb(NULL, session);
session->rem_sess_cb = NULL;
}
#endif
ForceZero(session->masterSecret, SECRET_LEN);
XMEMSET(session->sessionID, 0, ID_LEN);
session->sessionIDSz = 0;
#ifdef HAVE_SESSION_TICKET
if (session->ticketLenAlloc > 0) {
XFREE(session->ticket, NULL, DYNAMIC_TYPE_SESSION_TICK);
session->ticket = session->staticTicket;
session->ticketLen = 0;
session->ticketLenAlloc = 0;
}
#endif
#ifdef HAVE_EX_DATA
session->ownExData = save_ownExData;
#endif
}
WOLFSSL_ABI
WOLFSSL_SESSION* wolfSSL_get_session(WOLFSSL* ssl)
{
WOLFSSL_ENTER("wolfSSL_get_session");
if (ssl) {
#ifdef NO_SESSION_CACHE_REF
return ssl->session;
#else
if (ssl->options.side == WOLFSSL_CLIENT_END) {
/* On the client side we want to return a persistent reference for
* backwards compatibility. */
#ifndef NO_CLIENT_CACHE
if (ssl->clientSession) {
return (WOLFSSL_SESSION*)ssl->clientSession;
}
else {
/* Try to add a ClientCache entry to associate with the current
* session. Ignore any session cache options. */
int err;
const byte* id = ssl->session->sessionID;
byte idSz = ssl->session->sessionIDSz;
if (ssl->session->haveAltSessionID) {
id = ssl->session->altSessionID;
idSz = ID_LEN;
}
err = AddSessionToCache(ssl->ctx, ssl->session, id, idSz,
NULL, ssl->session->side,
#ifdef HAVE_SESSION_TICKET
ssl->session->ticketLen > 0,
#else
0,
#endif
&ssl->clientSession);
if (err == 0) {
return (WOLFSSL_SESSION*)ssl->clientSession;
}
}
#endif
}
else {
return ssl->session;
}
#endif
}
return NULL;
}
/* The get1 version requires caller to call SSL_SESSION_free */
WOLFSSL_SESSION* wolfSSL_get1_session(WOLFSSL* ssl)
{
WOLFSSL_SESSION* sess = NULL;
WOLFSSL_ENTER("wolfSSL_get1_session");
if (ssl != NULL) {
sess = ssl->session;
if (sess != NULL) {
/* increase reference count if allocated session */
if (sess->type == WOLFSSL_SESSION_TYPE_HEAP) {
if (wolfSSL_SESSION_up_ref(sess) != WOLFSSL_SUCCESS)
sess = NULL;
}
}
}
return sess;
}
/* session is a private struct, return if it is setup or not */
WOLFSSL_API int wolfSSL_SessionIsSetup(WOLFSSL_SESSION* session)
{
if (session != NULL)
return session->isSetup;
return 0;
}
/*
* Sets the session object to use when establishing a TLS/SSL session using
* the ssl object. Therefore, this function must be called before
* wolfSSL_connect. The session object to use can be obtained in a previous
* TLS/SSL connection using wolfSSL_get_session.
*
* This function rejects the session if it has been expired when this function
* is called. Note that this expiration check is wolfSSL specific and differs
* from OpenSSL return code behavior.
*
* By default, wolfSSL_set_session returns WOLFSSL_SUCCESS on successfully
* setting the session, WOLFSSL_FAILURE on failure due to the session cache
* being disabled, or the session has expired.
*
* To match OpenSSL return code behavior when session is expired, define
* OPENSSL_EXTRA and WOLFSSL_ERROR_CODE_OPENSSL. This behavior will return
* WOLFSSL_SUCCESS even when the session is expired and rejected.
*/
WOLFSSL_ABI
int wolfSSL_set_session(WOLFSSL* ssl, WOLFSSL_SESSION* session)
{
WOLFSSL_ENTER("wolfSSL_set_session");
if (session)
return wolfSSL_SetSession(ssl, session);
return WOLFSSL_FAILURE;
}
#ifndef NO_CLIENT_CACHE
/* Associate client session with serverID, find existing or store for saving
if newSession flag on, don't reuse existing session
WOLFSSL_SUCCESS on ok */
int wolfSSL_SetServerID(WOLFSSL* ssl, const byte* id, int len, int newSession)
{
WOLFSSL_SESSION* session = NULL;
byte idHash[SERVER_ID_LEN];
WOLFSSL_ENTER("wolfSSL_SetServerID");
if (ssl == NULL || id == NULL || len <= 0)
return BAD_FUNC_ARG;
if (len > SERVER_ID_LEN) {
#if defined(NO_SHA) && !defined(NO_SHA256)
if (wc_Sha256Hash(id, len, idHash) != 0)
return WOLFSSL_FAILURE;
#else
if (wc_ShaHash(id, (word32)len, idHash) != 0)
return WOLFSSL_FAILURE;
#endif
id = idHash;
len = SERVER_ID_LEN;
}
if (newSession == 0) {
session = wolfSSL_GetSessionClient(ssl, id, len);
if (session) {
if (wolfSSL_SetSession(ssl, session) != WOLFSSL_SUCCESS) {
#ifdef HAVE_EXT_CACHE
wolfSSL_FreeSession(ssl->ctx, session);
#endif
WOLFSSL_MSG("wolfSSL_SetSession failed");
session = NULL;
}
}
}
if (session == NULL) {
WOLFSSL_MSG("Valid ServerID not cached already");
ssl->session->idLen = (word16)len;
XMEMCPY(ssl->session->serverID, id, len);
}
#ifdef HAVE_EXT_CACHE
else {
wolfSSL_FreeSession(ssl->ctx, session);
}
#endif
return WOLFSSL_SUCCESS;
}
#endif /* !NO_CLIENT_CACHE */
/* TODO: Add SESSION_CACHE_DYNAMIC_MEM support for PERSIST_SESSION_CACHE.
* Need a count of current sessions to get an accurate memsize (totalCount is
* not decremented when sessions are removed).
* Need to determine ideal layout for mem/filesave.
* Also need mem/filesave checking to ensure not restoring non DYNAMIC_MEM
* cache.
*/
#if defined(PERSIST_SESSION_CACHE) && !defined(SESSION_CACHE_DYNAMIC_MEM)
/* for persistence, if changes to layout need to increment and modify
save_session_cache() and restore_session_cache and memory versions too */
#define WOLFSSL_CACHE_VERSION 2
/* Session Cache Header information */
typedef struct {
int version; /* cache layout version id */
int rows; /* session rows */
int columns; /* session columns */
int sessionSz; /* sizeof WOLFSSL_SESSION */
} cache_header_t;
/* current persistence layout is:
1) cache_header_t
2) SessionCache
3) ClientCache
update WOLFSSL_CACHE_VERSION if change layout for the following
PERSISTENT_SESSION_CACHE functions
*/
/* get how big the the session cache save buffer needs to be */
int wolfSSL_get_session_cache_memsize(void)
{
int sz = (int)(sizeof(SessionCache) + sizeof(cache_header_t));
#ifndef NO_CLIENT_CACHE
sz += (int)(sizeof(ClientCache));
#endif
return sz;
}
/* Persist session cache to memory */
int wolfSSL_memsave_session_cache(void* mem, int sz)
{
int i;
cache_header_t cache_header;
SessionRow* row = (SessionRow*)((byte*)mem + sizeof(cache_header));
WOLFSSL_ENTER("wolfSSL_memsave_session_cache");
if (sz < wolfSSL_get_session_cache_memsize()) {
WOLFSSL_MSG("Memory buffer too small");
return BUFFER_E;
}
cache_header.version = WOLFSSL_CACHE_VERSION;
cache_header.rows = SESSION_ROWS;
cache_header.columns = SESSIONS_PER_ROW;
cache_header.sessionSz = (int)sizeof(WOLFSSL_SESSION);
XMEMCPY(mem, &cache_header, sizeof(cache_header));
#ifndef ENABLE_SESSION_CACHE_ROW_LOCK
if (SESSION_ROW_RD_LOCK(row) != 0) {
WOLFSSL_MSG("Session cache mutex lock failed");
return BAD_MUTEX_E;
}
#endif
for (i = 0; i < cache_header.rows; ++i) {
#ifdef ENABLE_SESSION_CACHE_ROW_LOCK
if (SESSION_ROW_RD_LOCK(&SessionCache[i]) != 0) {
WOLFSSL_MSG("Session row cache mutex lock failed");
return BAD_MUTEX_E;
}
#endif
XMEMCPY(row++, &SessionCache[i], SIZEOF_SESSION_ROW);
#ifdef ENABLE_SESSION_CACHE_ROW_LOCK
SESSION_ROW_UNLOCK(&SessionCache[i]);
#endif
}
#ifndef ENABLE_SESSION_CACHE_ROW_LOCK
SESSION_ROW_UNLOCK(row);
#endif
#ifndef NO_CLIENT_CACHE
if (wc_LockMutex(&clisession_mutex) != 0) {
WOLFSSL_MSG("Client cache mutex lock failed");
return BAD_MUTEX_E;
}
XMEMCPY(row, ClientCache, sizeof(ClientCache));
wc_UnLockMutex(&clisession_mutex);
#endif
WOLFSSL_LEAVE("wolfSSL_memsave_session_cache", WOLFSSL_SUCCESS);
return WOLFSSL_SUCCESS;
}
/* Restore the persistent session cache from memory */
int wolfSSL_memrestore_session_cache(const void* mem, int sz)
{
int i;
cache_header_t cache_header;
SessionRow* row = (SessionRow*)((byte*)mem + sizeof(cache_header));
WOLFSSL_ENTER("wolfSSL_memrestore_session_cache");
if (sz < wolfSSL_get_session_cache_memsize()) {
WOLFSSL_MSG("Memory buffer too small");
return BUFFER_E;
}
XMEMCPY(&cache_header, mem, sizeof(cache_header));
if (cache_header.version != WOLFSSL_CACHE_VERSION ||
cache_header.rows != SESSION_ROWS ||
cache_header.columns != SESSIONS_PER_ROW ||
cache_header.sessionSz != (int)sizeof(WOLFSSL_SESSION)) {
WOLFSSL_MSG("Session cache header match failed");
return CACHE_MATCH_ERROR;
}
#ifndef ENABLE_SESSION_CACHE_ROW_LOCK
if (SESSION_ROW_WR_LOCK(&SessionCache[0]) != 0) {
WOLFSSL_MSG("Session cache mutex lock failed");
return BAD_MUTEX_E;
}
#endif
for (i = 0; i < cache_header.rows; ++i) {
#ifdef ENABLE_SESSION_CACHE_ROW_LOCK
if (SESSION_ROW_WR_LOCK(&SessionCache[i]) != 0) {
WOLFSSL_MSG("Session row cache mutex lock failed");
return BAD_MUTEX_E;
}
#endif
XMEMCPY(&SessionCache[i], row++, SIZEOF_SESSION_ROW);
#ifdef ENABLE_SESSION_CACHE_ROW_LOCK
SESSION_ROW_UNLOCK(&SessionCache[i]);
#endif
}
#ifndef ENABLE_SESSION_CACHE_ROW_LOCK
SESSION_ROW_UNLOCK(&SessionCache[0]);
#endif
#ifndef NO_CLIENT_CACHE
if (wc_LockMutex(&clisession_mutex) != 0) {
WOLFSSL_MSG("Client cache mutex lock failed");
return BAD_MUTEX_E;
}
XMEMCPY(ClientCache, row, sizeof(ClientCache));
wc_UnLockMutex(&clisession_mutex);
#endif
WOLFSSL_LEAVE("wolfSSL_memrestore_session_cache", WOLFSSL_SUCCESS);
return WOLFSSL_SUCCESS;
}
#if !defined(NO_FILESYSTEM)
/* Persist session cache to file */
/* doesn't use memsave because of additional memory use */
int wolfSSL_save_session_cache(const char *fname)
{
XFILE file;
int ret;
int rc = WOLFSSL_SUCCESS;
int i;
cache_header_t cache_header;
WOLFSSL_ENTER("wolfSSL_save_session_cache");
file = XFOPEN(fname, "w+b");
if (file == XBADFILE) {
WOLFSSL_MSG("Couldn't open session cache save file");
return WOLFSSL_BAD_FILE;
}
cache_header.version = WOLFSSL_CACHE_VERSION;
cache_header.rows = SESSION_ROWS;
cache_header.columns = SESSIONS_PER_ROW;
cache_header.sessionSz = (int)sizeof(WOLFSSL_SESSION);
/* cache header */
ret = (int)XFWRITE(&cache_header, sizeof cache_header, 1, file);
if (ret != 1) {
WOLFSSL_MSG("Session cache header file write failed");
XFCLOSE(file);
return FWRITE_ERROR;
}
#ifndef ENABLE_SESSION_CACHE_ROW_LOCK
if (SESSION_ROW_RD_LOCK(&SessionCache[0]) != 0) {
WOLFSSL_MSG("Session cache mutex lock failed");
XFCLOSE(file);
return BAD_MUTEX_E;
}
#endif
/* session cache */
for (i = 0; i < cache_header.rows; ++i) {
#ifdef ENABLE_SESSION_CACHE_ROW_LOCK
if (SESSION_ROW_RD_LOCK(&SessionCache[i]) != 0) {
WOLFSSL_MSG("Session row cache mutex lock failed");
XFCLOSE(file);
return BAD_MUTEX_E;
}
#endif
ret = (int)XFWRITE(&SessionCache[i], SIZEOF_SESSION_ROW, 1, file);
#ifdef ENABLE_SESSION_CACHE_ROW_LOCK
SESSION_ROW_UNLOCK(&SessionCache[i]);
#endif
if (ret != 1) {
WOLFSSL_MSG("Session cache member file write failed");
rc = FWRITE_ERROR;
break;
}
}
#ifndef ENABLE_SESSION_CACHE_ROW_LOCK
SESSION_ROW_UNLOCK(&SessionCache[0]);
#endif
#ifndef NO_CLIENT_CACHE
/* client cache */
if (wc_LockMutex(&clisession_mutex) != 0) {
WOLFSSL_MSG("Client cache mutex lock failed");
XFCLOSE(file);
return BAD_MUTEX_E;
}
ret = (int)XFWRITE(ClientCache, sizeof(ClientCache), 1, file);
if (ret != 1) {
WOLFSSL_MSG("Client cache member file write failed");
rc = FWRITE_ERROR;
}
wc_UnLockMutex(&clisession_mutex);
#endif /* !NO_CLIENT_CACHE */
XFCLOSE(file);
WOLFSSL_LEAVE("wolfSSL_save_session_cache", rc);
return rc;
}
/* Restore the persistent session cache from file */
/* doesn't use memstore because of additional memory use */
int wolfSSL_restore_session_cache(const char *fname)
{
XFILE file;
int rc = WOLFSSL_SUCCESS;
int ret;
int i;
cache_header_t cache_header;
WOLFSSL_ENTER("wolfSSL_restore_session_cache");
file = XFOPEN(fname, "rb");
if (file == XBADFILE) {
WOLFSSL_MSG("Couldn't open session cache save file");
return WOLFSSL_BAD_FILE;
}
/* cache header */
ret = (int)XFREAD(&cache_header, sizeof(cache_header), 1, file);
if (ret != 1) {
WOLFSSL_MSG("Session cache header file read failed");
XFCLOSE(file);
return FREAD_ERROR;
}
if (cache_header.version != WOLFSSL_CACHE_VERSION ||
cache_header.rows != SESSION_ROWS ||
cache_header.columns != SESSIONS_PER_ROW ||
cache_header.sessionSz != (int)sizeof(WOLFSSL_SESSION)) {
WOLFSSL_MSG("Session cache header match failed");
XFCLOSE(file);
return CACHE_MATCH_ERROR;
}
#ifndef ENABLE_SESSION_CACHE_ROW_LOCK
if (SESSION_ROW_WR_LOCK(&SessionCache[0]) != 0) {
WOLFSSL_MSG("Session cache mutex lock failed");
XFCLOSE(file);
return BAD_MUTEX_E;
}
#endif
/* session cache */
for (i = 0; i < cache_header.rows; ++i) {
#ifdef ENABLE_SESSION_CACHE_ROW_LOCK
if (SESSION_ROW_WR_LOCK(&SessionCache[i]) != 0) {
WOLFSSL_MSG("Session row cache mutex lock failed");
XFCLOSE(file);
return BAD_MUTEX_E;
}
#endif
ret = (int)XFREAD(&SessionCache[i], SIZEOF_SESSION_ROW, 1, file);
#ifdef ENABLE_SESSION_CACHE_ROW_LOCK
SESSION_ROW_UNLOCK(&SessionCache[i]);
#endif
if (ret != 1) {
WOLFSSL_MSG("Session cache member file read failed");
XMEMSET(SessionCache, 0, sizeof SessionCache);
rc = FREAD_ERROR;
break;
}
}
#ifndef ENABLE_SESSION_CACHE_ROW_LOCK
SESSION_ROW_UNLOCK(&SessionCache[0]);
#endif
#ifndef NO_CLIENT_CACHE
/* client cache */
if (wc_LockMutex(&clisession_mutex) != 0) {
WOLFSSL_MSG("Client cache mutex lock failed");
XFCLOSE(file);
return BAD_MUTEX_E;
}
ret = (int)XFREAD(ClientCache, sizeof(ClientCache), 1, file);
if (ret != 1) {
WOLFSSL_MSG("Client cache member file read failed");
XMEMSET(ClientCache, 0, sizeof ClientCache);
rc = FREAD_ERROR;
}
wc_UnLockMutex(&clisession_mutex);
#endif /* !NO_CLIENT_CACHE */
XFCLOSE(file);
WOLFSSL_LEAVE("wolfSSL_restore_session_cache", rc);
return rc;
}
#endif /* !NO_FILESYSTEM */
#endif /* PERSIST_SESSION_CACHE && !SESSION_CACHE_DYNAMIC_MEM */
/* on by default if built in but allow user to turn off */
WOLFSSL_ABI
long wolfSSL_CTX_set_session_cache_mode(WOLFSSL_CTX* ctx, long mode)
{
WOLFSSL_ENTER("wolfSSL_CTX_set_session_cache_mode");
if (ctx == NULL)
return WOLFSSL_FAILURE;
if (mode == WOLFSSL_SESS_CACHE_OFF) {
ctx->sessionCacheOff = 1;
#ifdef HAVE_EXT_CACHE
ctx->internalCacheOff = 1;
ctx->internalCacheLookupOff = 1;
#endif
}
if ((mode & WOLFSSL_SESS_CACHE_NO_AUTO_CLEAR) != 0)
ctx->sessionCacheFlushOff = 1;
#ifdef HAVE_EXT_CACHE
/* WOLFSSL_SESS_CACHE_NO_INTERNAL activates both if's */
if ((mode & WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE) != 0)
ctx->internalCacheOff = 1;
if ((mode & WOLFSSL_SESS_CACHE_NO_INTERNAL_LOOKUP) != 0)
ctx->internalCacheLookupOff = 1;
#endif
return WOLFSSL_SUCCESS;
}
#ifdef OPENSSL_EXTRA
/* Get the session cache mode for CTX
*
* ctx WOLFSSL_CTX struct to get cache mode from
*
* Returns a bit mask that has the session cache mode */
long wolfSSL_CTX_get_session_cache_mode(WOLFSSL_CTX* ctx)
{
long m = 0;
WOLFSSL_ENTER("wolfSSL_CTX_get_session_cache_mode");
if (ctx == NULL) {
return m;
}
if (ctx->sessionCacheOff != 1) {
m |= WOLFSSL_SESS_CACHE_SERVER;
}
if (ctx->sessionCacheFlushOff == 1) {
m |= WOLFSSL_SESS_CACHE_NO_AUTO_CLEAR;
}
#ifdef HAVE_EXT_CACHE
if (ctx->internalCacheOff == 1) {
m |= WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE;
}
if (ctx->internalCacheLookupOff == 1) {
m |= WOLFSSL_SESS_CACHE_NO_INTERNAL_LOOKUP;
}
#endif
return m;
}
#endif /* OPENSSL_EXTRA */
#endif /* !NO_SESSION_CACHE */
#ifndef NO_SESSION_CACHE
WOLFSSL_ABI
void wolfSSL_flush_sessions(WOLFSSL_CTX* ctx, long tm)
{
/* static table now, no flushing needed */
(void)ctx;
(void)tm;
}
void wolfSSL_CTX_flush_sessions(WOLFSSL_CTX* ctx, long tm)
{
int i, j;
byte id[ID_LEN];
(void)ctx;
XMEMSET(id, 0, ID_LEN);
WOLFSSL_ENTER("wolfSSL_flush_sessions");
for (i = 0; i < SESSION_ROWS; ++i) {
if (SESSION_ROW_WR_LOCK(&SessionCache[i]) != 0) {
WOLFSSL_MSG("Session cache mutex lock failed");
return;
}
for (j = 0; j < SESSIONS_PER_ROW; j++) {
#ifdef SESSION_CACHE_DYNAMIC_MEM
WOLFSSL_SESSION* s = SessionCache[i].Sessions[j];
#else
WOLFSSL_SESSION* s = &SessionCache[i].Sessions[j];
#endif
if (
#ifdef SESSION_CACHE_DYNAMIC_MEM
s != NULL &&
#endif
XMEMCMP(s->sessionID, id, ID_LEN) != 0 &&
s->bornOn + s->timeout < (word32)tm
)
{
EvictSessionFromCache(s);
#ifdef SESSION_CACHE_DYNAMIC_MEM
XFREE(s, s->heap, DYNAMIC_TYPE_SESSION);
SessionCache[i].Sessions[j] = NULL;
#endif
}
}
SESSION_ROW_UNLOCK(&SessionCache[i]);
}
}
/* set ssl session timeout in seconds */
WOLFSSL_ABI
int wolfSSL_set_timeout(WOLFSSL* ssl, unsigned int to)
{
if (ssl == NULL)
return BAD_FUNC_ARG;
if (to == 0)
to = WOLFSSL_SESSION_TIMEOUT;
ssl->timeout = to;
return WOLFSSL_SUCCESS;
}
/**
* Sets ctx session timeout in seconds.
* The timeout value set here should be reflected in the
* "session ticket lifetime hint" if this API works in the openssl compat-layer.
* Therefore wolfSSL_CTX_set_TicketHint is called internally.
* Arguments:
* - ctx WOLFSSL_CTX object which the timeout is set to
* - to timeout value in second
* Returns:
* WOLFSSL_SUCCESS on success, BAD_FUNC_ARG on failure.
* When WOLFSSL_ERROR_CODE_OPENSSL is defined, returns previous timeout value
* on success, BAD_FUNC_ARG on failure.
*/
WOLFSSL_ABI
int wolfSSL_CTX_set_timeout(WOLFSSL_CTX* ctx, unsigned int to)
{
#if defined(WOLFSSL_ERROR_CODE_OPENSSL)
word32 prev_timeout = 0;
#endif
int ret = WOLFSSL_SUCCESS;
(void)ret;
if (ctx == NULL)
ret = BAD_FUNC_ARG;
if (ret == WOLFSSL_SUCCESS) {
#if defined(WOLFSSL_ERROR_CODE_OPENSSL)
prev_timeout = ctx->timeout;
#endif
if (to == 0) {
ctx->timeout = WOLFSSL_SESSION_TIMEOUT;
}
else {
ctx->timeout = to;
}
}
#if defined(OPENSSL_EXTRA) && defined(HAVE_SESSION_TICKET) && \
!defined(NO_WOLFSSL_SERVER)
if (ret == WOLFSSL_SUCCESS) {
if (to == 0) {
ret = wolfSSL_CTX_set_TicketHint(ctx, SESSION_TICKET_HINT_DEFAULT);
}
else {
ret = wolfSSL_CTX_set_TicketHint(ctx, (int)to);
}
}
#endif /* OPENSSL_EXTRA && HAVE_SESSION_TICKET && !NO_WOLFSSL_SERVER */
#if defined(WOLFSSL_ERROR_CODE_OPENSSL)
if (ret == WOLFSSL_SUCCESS) {
return (int)prev_timeout;
}
else {
return ret;
}
#else
return ret;
#endif /* WOLFSSL_ERROR_CODE_OPENSSL */
}
#ifndef NO_CLIENT_CACHE
/* Get Session from Client cache based on id/len, return NULL on failure */
WOLFSSL_SESSION* wolfSSL_GetSessionClient(WOLFSSL* ssl, const byte* id, int len)
{
WOLFSSL_SESSION* ret = NULL;
word32 row;
int idx;
int count;
int error = 0;
ClientSession* clSess;
WOLFSSL_ENTER("wolfSSL_GetSessionClient");
if (ssl->ctx->sessionCacheOff) {
WOLFSSL_MSG("Session Cache off");
return NULL;
}
if (ssl->options.side == WOLFSSL_SERVER_END)
return NULL;
len = (int)min(SERVER_ID_LEN, (word32)len);
/* Do not access ssl->ctx->get_sess_cb from here. It is using a different
* set of ID's */
row = HashObject(id, (word32)len, &error) % CLIENT_SESSION_ROWS;
if (error != 0) {
WOLFSSL_MSG("Hash session failed");
return NULL;
}
if (wc_LockMutex(&clisession_mutex) != 0) {
WOLFSSL_MSG("Client cache mutex lock failed");
return NULL;
}
/* start from most recently used */
count = (int)min((word32)ClientCache[row].totalCount, CLIENT_SESSIONS_PER_ROW);
idx = ClientCache[row].nextIdx - 1;
if (idx < 0 || idx >= CLIENT_SESSIONS_PER_ROW) {
/* if back to front, the previous was end */
idx = CLIENT_SESSIONS_PER_ROW - 1;
}
clSess = ClientCache[row].Clients;
for (; count > 0; --count) {
WOLFSSL_SESSION* current;
SessionRow* sessRow;
if (clSess[idx].serverRow >= SESSION_ROWS) {
WOLFSSL_MSG("Client cache serverRow invalid");
break;
}
/* lock row */
sessRow = &SessionCache[clSess[idx].serverRow];
if (SESSION_ROW_RD_LOCK(sessRow) != 0) {
WOLFSSL_MSG("Session cache row lock failure");
break;
}
#ifdef SESSION_CACHE_DYNAMIC_MEM
current = sessRow->Sessions[clSess[idx].serverIdx];
#else
current = &sessRow->Sessions[clSess[idx].serverIdx];
#endif
if (current && XMEMCMP(current->serverID, id, len) == 0) {
WOLFSSL_MSG("Found a serverid match for client");
if (LowResTimer() < (current->bornOn + current->timeout)) {
WOLFSSL_MSG("Session valid");
ret = current;
SESSION_ROW_UNLOCK(sessRow);
break;
} else {
WOLFSSL_MSG("Session timed out"); /* could have more for id */
}
} else {
WOLFSSL_MSG("ServerID not a match from client table");
}
SESSION_ROW_UNLOCK(sessRow);
idx = idx > 0 ? idx - 1 : CLIENT_SESSIONS_PER_ROW - 1;
}
wc_UnLockMutex(&clisession_mutex);