-
Notifications
You must be signed in to change notification settings - Fork 2
/
cryptses.c
executable file
·706 lines (614 loc) · 23.4 KB
/
cryptses.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
/****************************************************************************
* *
* cryptlib Secure Session Routines *
* Copyright Peter Gutmann 1998-2005 *
* *
****************************************************************************/
#include <stdio.h>
#include <stdarg.h>
#include "crypt.h"
#ifdef INC_ALL
#include "asn1.h"
#include "stream.h"
#include "session.h"
#include "scorebrd.h"
#else
#include "enc_dec/asn1.h"
#include "io/stream.h"
#include "session/session.h"
#include "session/scorebrd.h"
#endif /* Compiler-specific includes */
#ifdef USE_SESSIONS
/* The number of entries in the SSL session cache. Note that when increasing
the SESSIONCACHE_SIZE value to more than about 256 you need to also change
MAX_ALLOC_SIZE in kernel/sec_mem.c to allow the allocation of such large
amounts of secure memory */
#if defined( CONFIG_CONSERVE_MEMORY )
#define SESSIONCACHE_SIZE 8
#else
#define SESSIONCACHE_SIZE 64
#endif /* CONFIG_CONSERVE_MEMORY */
static SCOREBOARD_STATE scoreboardInfo;
/****************************************************************************
* *
* Session Message Handler *
* *
****************************************************************************/
/* Handle a message sent to a session object */
CHECK_RETVAL STDC_NONNULL_ARG( ( 1 ) ) \
static int sessionMessageFunction( INOUT TYPECAST( CONTEXT_INFO * ) \
void *objectInfoPtr,
IN_MESSAGE const MESSAGE_TYPE message,
void *messageDataPtr,
IN_INT_Z const int messageValue )
{
SESSION_INFO *sessionInfoPtr = ( SESSION_INFO * ) objectInfoPtr;
int status;
assert( isWritePtr( objectInfoPtr, sizeof( SESSION_INFO ) ) );
REQUIRES( message > MESSAGE_NONE && message < MESSAGE_LAST );
REQUIRES( messageValue >= 0 && messageValue < MAX_INTLENGTH );
/* Process destroy object messages */
if( message == MESSAGE_DESTROY )
{
const SES_SHUTDOWN_FUNCTION shutdownFunction = \
FNPTR_GET( sessionInfoPtr->shutdownFunction );
REQUIRES( shutdownFunction != NULL );
/* Shut down the session if required. Nemo nisi mors */
if( sessionInfoPtr->flags & \
( SESSION_ISOPEN | SESSION_PARTIALOPEN ) )
{
sessionInfoPtr->flags |= SESSION_ISCLOSINGDOWN;
shutdownFunction( sessionInfoPtr );
}
/* Clear and free session state information if necessary */
if( sessionInfoPtr->sendBuffer != NULL )
{
zeroise( sessionInfoPtr->sendBuffer,
sessionInfoPtr->sendBufSize );
clFree( "sessionMessageFunction", sessionInfoPtr->sendBuffer );
}
if( sessionInfoPtr->receiveBuffer != NULL )
{
zeroise( sessionInfoPtr->receiveBuffer,
sessionInfoPtr->receiveBufSize );
clFree( "sessionMessageFunction", sessionInfoPtr->receiveBuffer );
}
/* Clear session attributes if necessary */
if( sessionInfoPtr->attributeList != NULL )
deleteSessionInfoAll( &sessionInfoPtr->attributeList,
&sessionInfoPtr->attributeListCurrent );
/* Clean up any session-related objects if necessary */
if( sessionInfoPtr->iKeyexCryptContext != CRYPT_ERROR )
krnlSendNotifier( sessionInfoPtr->iKeyexCryptContext,
IMESSAGE_DECREFCOUNT );
if( sessionInfoPtr->iKeyexAuthContext != CRYPT_ERROR )
krnlSendNotifier( sessionInfoPtr->iKeyexAuthContext,
IMESSAGE_DECREFCOUNT );
if( sessionInfoPtr->iCryptInContext != CRYPT_ERROR )
krnlSendNotifier( sessionInfoPtr->iCryptInContext,
IMESSAGE_DECREFCOUNT );
if( sessionInfoPtr->iCryptOutContext != CRYPT_ERROR )
krnlSendNotifier( sessionInfoPtr->iCryptOutContext,
IMESSAGE_DECREFCOUNT );
if( sessionInfoPtr->iAuthInContext != CRYPT_ERROR )
krnlSendNotifier( sessionInfoPtr->iAuthInContext,
IMESSAGE_DECREFCOUNT );
if( sessionInfoPtr->iAuthOutContext != CRYPT_ERROR )
krnlSendNotifier( sessionInfoPtr->iAuthOutContext,
IMESSAGE_DECREFCOUNT );
if( sessionInfoPtr->iCertRequest != CRYPT_ERROR )
krnlSendNotifier( sessionInfoPtr->iCertRequest,
IMESSAGE_DECREFCOUNT );
if( sessionInfoPtr->iCertResponse != CRYPT_ERROR )
krnlSendNotifier( sessionInfoPtr->iCertResponse,
IMESSAGE_DECREFCOUNT );
if( sessionInfoPtr->privateKey != CRYPT_ERROR )
krnlSendNotifier( sessionInfoPtr->privateKey,
IMESSAGE_DECREFCOUNT );
if( sessionInfoPtr->cryptKeyset != CRYPT_ERROR )
krnlSendNotifier( sessionInfoPtr->cryptKeyset,
IMESSAGE_DECREFCOUNT );
if( sessionInfoPtr->privKeyset != CRYPT_ERROR )
krnlSendNotifier( sessionInfoPtr->privKeyset,
IMESSAGE_DECREFCOUNT );
if( sessionInfoPtr->transportSession != CRYPT_ERROR )
krnlSendNotifier( sessionInfoPtr->transportSession,
IMESSAGE_DECREFCOUNT );
return( CRYPT_OK );
}
/* Process attribute get/set/delete messages */
if( isAttributeMessage( message ) )
{
/* If it's a protocol-specific attribute, forward it directly to
the low-level code */
if( message != MESSAGE_DELETEATTRIBUTE && \
( ( messageValue >= CRYPT_SESSINFO_FIRST_SPECIFIC && \
messageValue <= CRYPT_SESSINFO_LAST_SPECIFIC ) || \
messageValue == CRYPT_IATTRIBUTE_ENC_TIMESTAMP ) )
{
if( message == MESSAGE_SETATTRIBUTE || \
message == MESSAGE_SETATTRIBUTE_S )
{
const SES_SETATTRIBUTE_FUNCTION setAttributeFunction = \
FNPTR_GET( sessionInfoPtr->setAttributeFunction );
REQUIRES( setAttributeFunction != NULL );
/* Perform any protocol-specific additional checks if
necessary */
if( FNPTR_ISSET( sessionInfoPtr->checkAttributeFunction ) )
{
const SES_CHECKATTRIBUTE_FUNCTION checkAttributeFunction = \
FNPTR_GET( sessionInfoPtr->checkAttributeFunction );
REQUIRES( checkAttributeFunction != NULL );
status = checkAttributeFunction( sessionInfoPtr,
messageDataPtr, messageValue );
if( cryptStatusError( status ) )
return( status );
}
status = setAttributeFunction( sessionInfoPtr, messageDataPtr,
messageValue );
if( status == CRYPT_ERROR_INITED )
{
setErrorInfo( sessionInfoPtr, messageValue,
CRYPT_ERRTYPE_ATTR_PRESENT );
return( CRYPT_ERROR_INITED );
}
}
else
{
const SES_GETATTRIBUTE_FUNCTION getAttributeFunction = \
FNPTR_GET( sessionInfoPtr->getAttributeFunction );
REQUIRES( message == MESSAGE_GETATTRIBUTE || \
message == MESSAGE_GETATTRIBUTE_S );
REQUIRES( getAttributeFunction != NULL );
status = getAttributeFunction( sessionInfoPtr, messageDataPtr,
messageValue );
if( status == CRYPT_ERROR_NOTFOUND )
{
setErrorInfo( sessionInfoPtr, messageValue,
CRYPT_ERRTYPE_ATTR_ABSENT );
return( CRYPT_ERROR_NOTFOUND );
}
}
return( status );
}
if( message == MESSAGE_GETATTRIBUTE )
return( getSessionAttribute( sessionInfoPtr,
( int * ) messageDataPtr,
messageValue ) );
if( message == MESSAGE_GETATTRIBUTE_S )
return( getSessionAttributeS( sessionInfoPtr,
( MESSAGE_DATA * ) messageDataPtr,
messageValue ) );
if( message == MESSAGE_SETATTRIBUTE )
{
/* CRYPT_IATTRIBUTE_INITIALISED is purely a notification message
with no parameters so we don't pass it down to the attribute-
handling code */
if( messageValue == CRYPT_IATTRIBUTE_INITIALISED )
return( CRYPT_OK );
return( setSessionAttribute( sessionInfoPtr,
*( ( int * ) messageDataPtr ),
messageValue ) );
}
if( message == MESSAGE_SETATTRIBUTE_S )
{
const MESSAGE_DATA *msgData = ( MESSAGE_DATA * ) messageDataPtr;
return( setSessionAttributeS( sessionInfoPtr, msgData->data,
msgData->length, messageValue ) );
}
if( message == MESSAGE_DELETEATTRIBUTE )
return( deleteSessionAttribute( sessionInfoPtr, messageValue ) );
retIntError();
}
/* Process object-specific messages */
if( message == MESSAGE_ENV_PUSHDATA )
{
MESSAGE_DATA *msgData = ( MESSAGE_DATA * ) messageDataPtr;
const int length = msgData->length;
int bytesCopied;
/* Unless we're told otherwise, we've copied zero bytes */
msgData->length = 0;
/* If the session isn't open yet, perform an implicit open */
if( !( sessionInfoPtr->flags & SESSION_ISOPEN ) )
{
status = krnlSendMessage( sessionInfoPtr->objectHandle,
IMESSAGE_SETATTRIBUTE, MESSAGE_VALUE_TRUE,
CRYPT_SESSINFO_ACTIVE );
if( cryptStatusError( status ) )
return( status );
/* The session is ready to process data, move it into the high
state */
status = krnlSendMessage( sessionInfoPtr->objectHandle,
IMESSAGE_SETATTRIBUTE,
MESSAGE_VALUE_UNUSED,
CRYPT_IATTRIBUTE_INITIALISED );
if( cryptStatusError( status ) )
return( status );
}
ENSURES( sessionInfoPtr->flags & SESSION_ISOPEN );
ENSURES( sessionInfoPtr->sendBuffer != NULL );
/* Make sure that everything is in order */
if( sessionInfoPtr->flags & SESSION_SENDCLOSED )
{
/* If the other side has closed its receive channel (which is
our send channel), we can't send any more data, although we
can still get data on our receive channel if we haven't closed
it as well. The closing of the other side's send channel is
detected during a read and isn't a write error but a normal
state change in the channel so we don't treat it as an error
when it's seen at the read stage until the caller actually
tries to write data to the closed channel */
sessionInfoPtr->writeErrorState = CRYPT_ERROR_COMPLETE;
}
if( sessionInfoPtr->writeErrorState != CRYPT_OK )
return( sessionInfoPtr->writeErrorState );
/* Write the data */
clearErrorInfo( sessionInfoPtr );
status = putSessionData( sessionInfoPtr, msgData->data, length,
&bytesCopied );
if( cryptStatusOK( status ) )
msgData->length = bytesCopied;
ENSURES( ( cryptStatusError( status ) && bytesCopied == 0 ) || \
( cryptStatusOK( status ) && bytesCopied >= 0 ) );
return( status );
}
if( message == MESSAGE_ENV_POPDATA )
{
MESSAGE_DATA *msgData = ( MESSAGE_DATA * ) messageDataPtr;
const int length = msgData->length;
int bytesCopied;
/* Unless we're told otherwise, we've copied zero bytes */
msgData->length = 0;
/* If the session isn't open, there's nothing to pop */
if( !( sessionInfoPtr->flags & SESSION_ISOPEN ) )
return( CRYPT_ERROR_NOTINITED );
ENSURES( sessionInfoPtr->flags & SESSION_ISOPEN );
ENSURES( sessionInfoPtr->receiveBuffer != NULL );
/* Make sure that everything is in order */
if( sessionInfoPtr->readErrorState != CRYPT_OK )
return( sessionInfoPtr->readErrorState );
/* Read the data */
clearErrorInfo( sessionInfoPtr );
status = getSessionData( sessionInfoPtr, msgData->data, length,
&bytesCopied );
if( cryptStatusOK( status ) )
msgData->length = bytesCopied;
ENSURES( ( cryptStatusError( status ) && bytesCopied == 0 ) || \
( cryptStatusOK( status ) && bytesCopied >= 0 ) );
return( status );
}
retIntError();
}
/* Open a session. This is a low-level function encapsulated by createSession()
and used to manage error exits */
CHECK_RETVAL STDC_NONNULL_ARG( ( 1, 4 ) ) \
static int openSession( OUT_HANDLE_OPT CRYPT_SESSION *iCryptSession,
IN_HANDLE const CRYPT_USER iCryptOwner,
IN_ENUM( CRYPT_SESSION ) \
const CRYPT_SESSION_TYPE sessionType,
OUT_PTR_OPT SESSION_INFO **sessionInfoPtrPtr )
{
CRYPT_SESSION_TYPE sessionBaseType;
SESSION_INFO *sessionInfoPtr;
const PROTOCOL_INFO *protocolInfoPtr;
static const MAP_TABLE subtypeMapTbl[] = {
{ CRYPT_SESSION_SSH, SUBTYPE_SESSION_SSH },
{ CRYPT_SESSION_SSH_SERVER, SUBTYPE_SESSION_SSH_SVR },
{ CRYPT_SESSION_SSL, SUBTYPE_SESSION_SSL },
{ CRYPT_SESSION_SSL_SERVER, SUBTYPE_SESSION_SSL_SVR },
{ CRYPT_SESSION_RTCS, SUBTYPE_SESSION_RTCS },
{ CRYPT_SESSION_RTCS_SERVER, SUBTYPE_SESSION_RTCS_SVR },
{ CRYPT_SESSION_OCSP, SUBTYPE_SESSION_OCSP },
{ CRYPT_SESSION_OCSP_SERVER, SUBTYPE_SESSION_OCSP_SVR },
{ CRYPT_SESSION_TSP, SUBTYPE_SESSION_TSP },
{ CRYPT_SESSION_TSP_SERVER, SUBTYPE_SESSION_TSP_SVR },
{ CRYPT_SESSION_CMP, SUBTYPE_SESSION_CMP },
{ CRYPT_SESSION_CMP_SERVER, SUBTYPE_SESSION_CMP_SVR },
{ CRYPT_SESSION_SCEP, SUBTYPE_SESSION_SCEP },
{ CRYPT_SESSION_SCEP_SERVER, SUBTYPE_SESSION_SCEP_SVR },
{ CRYPT_SESSION_CERTSTORE_SERVER, SUBTYPE_SESSION_CERT_SVR },
{ CRYPT_ERROR, CRYPT_ERROR }, { CRYPT_ERROR, CRYPT_ERROR }
};
static const MAP_TABLE basetypeMapTbl[] = {
{ CRYPT_SESSION_SSH, CRYPT_SESSION_SSH },
{ CRYPT_SESSION_SSH_SERVER, CRYPT_SESSION_SSH },
{ CRYPT_SESSION_SSL, CRYPT_SESSION_SSL },
{ CRYPT_SESSION_SSL_SERVER, CRYPT_SESSION_SSL },
{ CRYPT_SESSION_RTCS, CRYPT_SESSION_RTCS },
{ CRYPT_SESSION_RTCS_SERVER, CRYPT_SESSION_RTCS },
{ CRYPT_SESSION_OCSP, CRYPT_SESSION_OCSP },
{ CRYPT_SESSION_OCSP_SERVER, CRYPT_SESSION_OCSP },
{ CRYPT_SESSION_TSP, CRYPT_SESSION_TSP },
{ CRYPT_SESSION_TSP_SERVER, CRYPT_SESSION_TSP },
{ CRYPT_SESSION_CMP, CRYPT_SESSION_CMP },
{ CRYPT_SESSION_CMP_SERVER, CRYPT_SESSION_CMP },
{ CRYPT_SESSION_SCEP, CRYPT_SESSION_SCEP },
{ CRYPT_SESSION_SCEP_SERVER, CRYPT_SESSION_SCEP },
{ CRYPT_SESSION_CERTSTORE_SERVER, CRYPT_SESSION_CERTSTORE_SERVER },
{ CRYPT_ERROR, CRYPT_ERROR }, { CRYPT_ERROR, CRYPT_ERROR }
};
OBJECT_SUBTYPE subType;
int value, storageSize, status;
assert( isWritePtr( iCryptSession, sizeof( CRYPT_SESSION * ) ) );
assert( isWritePtr( sessionInfoPtrPtr, sizeof( SESSION_INFO * ) ) );
REQUIRES( ( iCryptOwner == DEFAULTUSER_OBJECT_HANDLE ) || \
isHandleRangeValid( iCryptOwner ) );
REQUIRES( sessionType > CRYPT_SESSION_NONE && \
sessionType < CRYPT_SESSION_LAST );
/* Clear return values */
*iCryptSession = CRYPT_ERROR;
*sessionInfoPtrPtr = NULL;
/* Map the external session type to a base type and internal object
subtype */
status = mapValue( sessionType, &value, subtypeMapTbl,
FAILSAFE_ARRAYSIZE( subtypeMapTbl, MAP_TABLE ) );
ENSURES( cryptStatusOK( status ) );
subType = value;
status = mapValue( sessionType, &value, basetypeMapTbl,
FAILSAFE_ARRAYSIZE( basetypeMapTbl, MAP_TABLE ) );
ENSURES( cryptStatusOK( status ) );
sessionBaseType = value;
/* Set up subtype-specific information */
switch( sessionBaseType )
{
case CRYPT_SESSION_SSH:
storageSize = sizeof( SSH_INFO );
break;
case CRYPT_SESSION_SSL:
storageSize = sizeof( SSL_INFO );
break;
case CRYPT_SESSION_TSP:
storageSize = sizeof( TSP_INFO );
break;
case CRYPT_SESSION_CMP:
storageSize = sizeof( CMP_INFO );
break;
case CRYPT_SESSION_SCEP:
storageSize = sizeof( SCEP_INFO );
break;
case CRYPT_SESSION_RTCS:
case CRYPT_SESSION_OCSP:
case CRYPT_SESSION_CERTSTORE_SERVER:
storageSize = 0;
break;
default:
retIntError();
}
/* Create the session object */
status = krnlCreateObject( iCryptSession, ( void ** ) &sessionInfoPtr,
sizeof( SESSION_INFO ) + storageSize,
OBJECT_TYPE_SESSION, subType,
CREATEOBJECT_FLAG_NONE, iCryptOwner,
ACTION_PERM_NONE_ALL, sessionMessageFunction );
if( cryptStatusError( status ) )
return( status );
ANALYSER_HINT( sessionInfoPtr != NULL );
*sessionInfoPtrPtr = sessionInfoPtr;
sessionInfoPtr->objectHandle = *iCryptSession;
sessionInfoPtr->ownerHandle = iCryptOwner;
sessionInfoPtr->type = sessionBaseType;
if( storageSize > 0 )
{
switch( sessionBaseType )
{
case CRYPT_SESSION_SSH:
sessionInfoPtr->sessionSSH = \
( SSH_INFO * ) sessionInfoPtr->storage;
break;
case CRYPT_SESSION_SSL:
sessionInfoPtr->sessionSSL = \
( SSL_INFO * ) sessionInfoPtr->storage;
break;
case CRYPT_SESSION_TSP:
sessionInfoPtr->sessionTSP = \
( TSP_INFO * ) sessionInfoPtr->storage;
break;
case CRYPT_SESSION_CMP:
sessionInfoPtr->sessionCMP = \
( CMP_INFO * ) sessionInfoPtr->storage;
break;
case CRYPT_SESSION_SCEP:
sessionInfoPtr->sessionSCEP = \
( SCEP_INFO * ) sessionInfoPtr->storage;
break;
default:
retIntError();
}
}
sessionInfoPtr->storageSize = storageSize;
/* If it's a server session, mark it as such. An HTTP certstore session
is a special case in that it's always a server session */
if( ( sessionType != sessionBaseType ) || \
( sessionType == CRYPT_SESSION_CERTSTORE_SERVER ) )
sessionInfoPtr->flags = SESSION_ISSERVER;
/* Set up any internal objects to contain invalid handles */
sessionInfoPtr->iKeyexCryptContext = \
sessionInfoPtr->iKeyexAuthContext = CRYPT_ERROR;
sessionInfoPtr->iCryptInContext = \
sessionInfoPtr->iCryptOutContext = CRYPT_ERROR;
sessionInfoPtr->iAuthInContext = \
sessionInfoPtr->iAuthOutContext = CRYPT_ERROR;
sessionInfoPtr->iCertRequest = \
sessionInfoPtr->iCertResponse = CRYPT_ERROR;
sessionInfoPtr->privateKey = CRYPT_ERROR;
sessionInfoPtr->cryptKeyset = CRYPT_ERROR;
sessionInfoPtr->privKeyset = CRYPT_ERROR;
sessionInfoPtr->transportSession = CRYPT_ERROR;
sessionInfoPtr->networkSocket = CRYPT_ERROR;
sessionInfoPtr->readTimeout = \
sessionInfoPtr->writeTimeout = \
sessionInfoPtr->connectTimeout = CRYPT_ERROR;
/* Set up the access information for the session and initialise it */
switch( sessionBaseType )
{
case CRYPT_SESSION_CERTSTORE_SERVER:
status = setAccessMethodCertstore( sessionInfoPtr );
break;
case CRYPT_SESSION_CMP:
status = setAccessMethodCMP( sessionInfoPtr );
break;
case CRYPT_SESSION_RTCS:
status = setAccessMethodRTCS( sessionInfoPtr );
break;
case CRYPT_SESSION_OCSP:
status = setAccessMethodOCSP( sessionInfoPtr );
break;
case CRYPT_SESSION_SCEP:
status = setAccessMethodSCEP( sessionInfoPtr );
break;
case CRYPT_SESSION_SSH:
status = setAccessMethodSSH( sessionInfoPtr );
break;
case CRYPT_SESSION_SSL:
status = setAccessMethodSSL( sessionInfoPtr );
break;
case CRYPT_SESSION_TSP:
status = setAccessMethodTSP( sessionInfoPtr );
break;
default:
retIntError();
}
if( cryptStatusError( status ) )
return( status );
/* If it's a session type that uses the scoreboard, set up the
scoreboard information for the session */
if( sessionType == CRYPT_SESSION_SSL || \
sessionType == CRYPT_SESSION_SSL_SERVER )
sessionInfoPtr->sessionSSL->scoreboardInfoPtr = &scoreboardInfo;
/* Check that the protocol info is OK */
protocolInfoPtr = sessionInfoPtr->protocolInfo;
ENSURES( ( protocolInfoPtr->isReqResp && \
protocolInfoPtr->bufSize == 0 && \
protocolInfoPtr->sendBufStartOfs == 0 && \
protocolInfoPtr->maxPacketSize == 0 ) ||
( !protocolInfoPtr->isReqResp && \
protocolInfoPtr->bufSize >= MIN_BUFFER_SIZE && \
protocolInfoPtr->bufSize < MAX_BUFFER_SIZE && \
protocolInfoPtr->sendBufStartOfs >= 5 &&
protocolInfoPtr->sendBufStartOfs < protocolInfoPtr->maxPacketSize && \
protocolInfoPtr->maxPacketSize <= protocolInfoPtr->bufSize ) );
ENSURES( ( ( protocolInfoPtr->flags & SESSION_ISHTTPTRANSPORT ) && \
protocolInfoPtr->port == 80 ) || \
( protocolInfoPtr->port != 80 ) );
ENSURES( protocolInfoPtr->port > 21 && protocolInfoPtr->port <= 65534L );
ENSURES( protocolInfoPtr->version >= 0 && protocolInfoPtr->version < 5 );
/* Copy mutable protocol-specific information into the session info */
sessionInfoPtr->flags |= protocolInfoPtr->flags;
sessionInfoPtr->clientReqAttrFlags = protocolInfoPtr->clientReqAttrFlags;
sessionInfoPtr->serverReqAttrFlags = protocolInfoPtr->serverReqAttrFlags;
sessionInfoPtr->version = protocolInfoPtr->version;
if( protocolInfoPtr->isReqResp )
{
sessionInfoPtr->sendBufSize = CRYPT_UNUSED;
sessionInfoPtr->receiveBufSize = MIN_BUFFER_SIZE;
}
else
{
sessionInfoPtr->sendBufSize = sessionInfoPtr->receiveBufSize = \
protocolInfoPtr->bufSize;
sessionInfoPtr->sendBufStartOfs = sessionInfoPtr->receiveBufStartOfs = \
protocolInfoPtr->sendBufStartOfs;
sessionInfoPtr->maxPacketSize = protocolInfoPtr->maxPacketSize;
}
/* Install default handlers if no session-specific ones are provided */
status = initSessionIO( sessionInfoPtr );
if( cryptStatusError( status ) )
return( status );
/* Check that the handlers are all OK */
ENSURES( FNPTR_GET( sessionInfoPtr->connectFunction ) != NULL );
ENSURES( FNPTR_GET( sessionInfoPtr->transactFunction ) != NULL );
ENSURES( ( protocolInfoPtr->isReqResp && \
FNPTR_GET( sessionInfoPtr->readHeaderFunction ) == NULL && \
FNPTR_GET( sessionInfoPtr->processBodyFunction ) == NULL && \
FNPTR_GET( sessionInfoPtr->preparePacketFunction ) == NULL ) || \
( !protocolInfoPtr->isReqResp && \
FNPTR_GET( sessionInfoPtr->readHeaderFunction ) != NULL && \
FNPTR_GET( sessionInfoPtr->processBodyFunction ) != NULL && \
FNPTR_GET( sessionInfoPtr->preparePacketFunction ) != NULL ) );
return( CRYPT_OK );
}
CHECK_RETVAL STDC_NONNULL_ARG( ( 1 ) ) \
int createSession( INOUT MESSAGE_CREATEOBJECT_INFO *createInfo,
STDC_UNUSED const void *auxDataPtr,
STDC_UNUSED const int auxValue )
{
CRYPT_SESSION iCryptSession;
SESSION_INFO *sessionInfoPtr = NULL;
int initStatus, status;
assert( isWritePtr( createInfo, sizeof( MESSAGE_CREATEOBJECT_INFO ) ) );
REQUIRES( auxDataPtr == NULL && auxValue == 0 );
REQUIRES( createInfo->arg1 > CRYPT_SESSION_NONE && \
createInfo->arg1 < CRYPT_SESSION_LAST );
/* Pass the call on to the lower-level open function */
initStatus = openSession( &iCryptSession, createInfo->cryptOwner,
createInfo->arg1, &sessionInfoPtr );
if( cryptStatusError( initStatus ) )
{
/* If the create object failed, return immediately */
if( sessionInfoPtr == NULL )
return( initStatus );
/* The init failed, make sure that the object gets destroyed when we
notify the kernel that the setup process is complete */
krnlSendNotifier( iCryptSession, IMESSAGE_DESTROY );
}
/* We've finished setting up the object-type-specific info, tell the
kernel that the object is ready for use */
status = krnlSendMessage( iCryptSession, IMESSAGE_SETATTRIBUTE,
MESSAGE_VALUE_OK, CRYPT_IATTRIBUTE_STATUS );
if( cryptStatusError( initStatus ) || cryptStatusError( status ) )
return( cryptStatusError( initStatus ) ? initStatus : status );
createInfo->cryptHandle = iCryptSession;
return( CRYPT_OK );
}
/* Generic management function for this class of object */
CHECK_RETVAL \
int sessionManagementFunction( IN_ENUM( MANAGEMENT_ACTION ) \
const MANAGEMENT_ACTION_TYPE action )
{
static int initLevel = 0;
int status;
REQUIRES( action == MANAGEMENT_ACTION_INIT || \
action == MANAGEMENT_ACTION_PRE_SHUTDOWN || \
action == MANAGEMENT_ACTION_SHUTDOWN );
switch( action )
{
case MANAGEMENT_ACTION_INIT:
status = netInitTCP();
if( cryptStatusOK( status ) )
{
initLevel++;
if( krnlIsExiting() )
{
/* The kernel is shutting down, exit */
return( CRYPT_ERROR_PERMISSION );
}
status = initScoreboard( &scoreboardInfo,
SESSIONCACHE_SIZE );
}
if( cryptStatusOK( status ) )
initLevel++;
return( status );
case MANAGEMENT_ACTION_PRE_SHUTDOWN:
/* We have to wait for the driver binding to complete before we
can start the shutdown process */
if( !krnlWaitSemaphore( SEMAPHORE_DRIVERBIND ) )
{
/* The kernel is shutting down, bail out */
DEBUG_DIAG(( "Exiting due to kernel shutdown" ));
assert( DEBUG_WARN );
return( CRYPT_ERROR_PERMISSION );
}
if( initLevel > 0 )
netSignalShutdown();
return( CRYPT_OK );
case MANAGEMENT_ACTION_SHUTDOWN:
if( initLevel > 1 )
endScoreboard( &scoreboardInfo );
if( initLevel > 0 )
netEndTCP();
initLevel = 0;
return( CRYPT_OK );
}
retIntError();
}
#endif /* USE_SESSIONS */