Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wine-mirror
GitHub Repository: wine-mirror/wine
Path: blob/master/libs/ldap/libldap/tls_w.c
4394 views
1
/* tls_w.c - Handle tls/ssl using Windows SSPI SChannel provider. */
2
/* $OpenLDAP$ */
3
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4
*
5
* Copyright 2008-2022 The OpenLDAP Foundation.
6
* All rights reserved.
7
*
8
* Redistribution and use in source and binary forms, with or without
9
* modification, are permitted only as authorized by the OpenLDAP
10
* Public License.
11
*
12
* A copy of this license is available in the file LICENSE in the
13
* top-level directory of the distribution or, alternatively, at
14
* <http://www.OpenLDAP.org/license.html>.
15
*/
16
17
#include "portable.h"
18
#include "ldap_config.h"
19
20
#include <stdio.h>
21
22
#include <ac/stdlib.h>
23
#include <ac/errno.h>
24
#include <ac/socket.h>
25
#include <ac/string.h>
26
#include <ac/ctype.h>
27
#include <ac/time.h>
28
#include <ac/unistd.h>
29
#include <ac/param.h>
30
#include <ac/dirent.h>
31
#include <sys/stat.h>
32
#include <fcntl.h>
33
34
#include "ldap-int.h"
35
#include "ldap-tls.h"
36
37
#include <sspi.h>
38
#include <schannel.h>
39
40
typedef struct tlsw_ctx {
41
int refcount;
42
int reqcert;
43
#ifdef LDAP_R_COMPILE
44
ldap_pvt_thread_mutex_t ref_mutex;
45
#endif
46
} tlsw_ctx;
47
48
typedef struct tlsw_session {
49
CtxtHandle ctxt_handle; /* ctxt_handle must be the first field */
50
CredHandle cred_handle;
51
Sockbuf_IO_Desc *sbiod;
52
struct berval peer_der_dn;
53
} tlsw_session;
54
55
#ifdef LDAP_R_COMPILE
56
57
static void
58
tlsw_thr_init( void )
59
{
60
/* do nothing */
61
}
62
#endif /* LDAP_R_COMPILE */
63
64
/*
65
* Initialize TLS subsystem. Should be called only once.
66
*/
67
static int
68
tlsw_init( void )
69
{
70
struct ldapoptions *lo = LDAP_INT_GLOBAL_OPT();
71
lo->ldo_debug = -1;
72
/* do nothing */
73
return 0;
74
}
75
76
/*
77
* Tear down the TLS subsystem. Should only be called once.
78
*/
79
static void
80
tlsw_destroy( void )
81
{
82
/* do nothing */
83
}
84
85
static tls_ctx *
86
tlsw_ctx_new( struct ldapoptions *lo )
87
{
88
tlsw_ctx *ctx;
89
90
ctx = ber_memcalloc ( 1, sizeof (*ctx) );
91
if ( ctx ) {
92
ctx->refcount = 1;
93
#ifdef LDAP_R_COMPILE
94
ldap_pvt_thread_mutex_init( &ctx->ref_mutex );
95
#endif
96
}
97
return (tls_ctx *)ctx;
98
}
99
100
static void
101
tlsw_ctx_ref( tls_ctx *ctx )
102
{
103
tlsw_ctx *c = (tlsw_ctx *)ctx;
104
LDAP_MUTEX_LOCK( &c->ref_mutex );
105
c->refcount++;
106
LDAP_MUTEX_UNLOCK( &c->ref_mutex );
107
}
108
109
static void
110
tlsw_ctx_free( tls_ctx *ctx )
111
{
112
tlsw_ctx *c = (tlsw_ctx *)ctx;
113
int refcount;
114
115
if ( !c ) return;
116
117
LDAP_MUTEX_LOCK( &c->ref_mutex );
118
refcount = --c->refcount;
119
LDAP_MUTEX_UNLOCK( &c->ref_mutex );
120
if ( refcount )
121
return;
122
ber_memfree ( c );
123
}
124
125
/*
126
* initialize a new TLS context
127
*/
128
static int
129
tlsw_ctx_init( struct ldapoptions *lo, struct ldaptls *lt, int is_server )
130
{
131
tlsw_ctx *ctx = lo->ldo_tls_ctx;
132
133
ctx->reqcert = lo->ldo_tls_require_cert;
134
return 0;
135
}
136
137
static tls_session *
138
tlsw_session_new( tls_ctx * ctx, int is_server )
139
{
140
tlsw_session *session;
141
SCHANNEL_CRED cred;
142
143
session = ber_memcalloc ( 1, sizeof (*session) );
144
if ( !session )
145
return NULL;
146
147
memset( &cred, 0, sizeof(cred) );
148
cred.dwVersion = SCHANNEL_CRED_VERSION; /* FIXME set other fields */
149
if ( AcquireCredentialsHandleA( NULL, (SEC_CHAR *)UNISP_NAME_A,
150
is_server ? SECPKG_CRED_INBOUND : SECPKG_CRED_OUTBOUND, NULL,
151
&cred, NULL, NULL, &session->cred_handle, NULL )) {
152
ber_memfree( session );
153
return NULL;
154
}
155
return (tls_session *)session;
156
}
157
158
static int
159
tlsw_session_accept( tls_session *session )
160
{
161
return -1;
162
}
163
164
static ssize_t
165
tlsw_recv( Sockbuf_IO_Desc *sbiod, void *buf, size_t len )
166
{
167
tlsw_session *session;
168
169
if ( sbiod == NULL || buf == NULL || len <= 0 ) return 0;
170
171
return LBER_SBIOD_READ_NEXT( sbiod, buf, len );
172
}
173
174
static ssize_t
175
tlsw_send( Sockbuf_IO_Desc *sbiod, const void *buf, size_t len )
176
{
177
tlsw_session *session;
178
179
if ( sbiod == NULL || buf == NULL || len <= 0 ) return 0;
180
181
return LBER_SBIOD_WRITE_NEXT( sbiod, (char *)buf, len );
182
}
183
184
#define TLS_HEADER_SIZE 5
185
static int
186
tlsw_session_connect( LDAP *ld, tls_session *session, const char *name_in )
187
{
188
tlsw_session *s = (tlsw_session *)session;
189
SecBuffer in_bufs[] = { { 0, SECBUFFER_TOKEN, NULL }, { 0, SECBUFFER_EMPTY, NULL } };
190
SecBuffer out_bufs[] = { { 0, SECBUFFER_TOKEN, NULL }, { 0, SECBUFFER_ALERT, NULL } };
191
SecBufferDesc in_buf_desc = { SECBUFFER_VERSION, ARRAYSIZE(in_bufs), in_bufs };
192
SecBufferDesc out_buf_desc = { SECBUFFER_VERSION, ARRAYSIZE(out_bufs), out_bufs };
193
ULONG attrs, flags = ISC_REQ_CONFIDENTIALITY | ISC_REQ_STREAM |
194
ISC_REQ_USE_SUPPLIED_CREDS | ISC_REQ_ALLOCATE_MEMORY;
195
SECURITY_STATUS status;
196
ssize_t size, max_token, recv_offset = 0, expected = TLS_HEADER_SIZE;
197
SecPkgInfoA *info;
198
199
status = QuerySecurityPackageInfoA( UNISP_NAME_A, &info );
200
if ( status != SEC_E_OK )
201
return -1;
202
expected = max_token = info->cbMaxToken;
203
FreeContextBuffer( info );
204
205
in_bufs[0].cbBuffer = max_token;
206
in_bufs[0].pvBuffer = ber_memalloc( in_bufs[0].cbBuffer );
207
if ( !in_bufs[0].pvBuffer )
208
return -1;
209
210
status = InitializeSecurityContextA( &s->cred_handle, NULL, (SEC_CHAR *)name_in,
211
flags, 0, 0, NULL, 0, &s->ctxt_handle, &out_buf_desc, &attrs, NULL );
212
while ( status == SEC_I_CONTINUE_NEEDED || status == SEC_E_INCOMPLETE_MESSAGE )
213
{
214
if ( status == SEC_I_CONTINUE_NEEDED ) {
215
size = tlsw_send( s->sbiod, out_bufs[0].pvBuffer, out_bufs[0].cbBuffer );
216
if ( size < 0 )
217
break;
218
expected = TLS_HEADER_SIZE;
219
in_bufs[0].cbBuffer = recv_offset = 0;
220
}
221
222
while ( expected > 0 ) {
223
size = tlsw_recv( s->sbiod, (char *)in_bufs[0].pvBuffer + recv_offset, expected );
224
if ( size <= 0 )
225
goto done;
226
in_bufs[0].cbBuffer += size;
227
recv_offset += size;
228
expected -= size;
229
}
230
231
FreeContextBuffer( out_bufs[0].pvBuffer );
232
out_bufs[0].pvBuffer = NULL;
233
234
status = InitializeSecurityContextA( &s->cred_handle, &s->ctxt_handle, (SEC_CHAR *)name_in,
235
flags, 0, 0, &in_buf_desc, 0, NULL, &out_buf_desc, &attrs, NULL );
236
if ( status == SEC_E_INCOMPLETE_MESSAGE ) {
237
assert( in_bufs[1].BufferType == SECBUFFER_MISSING );
238
expected = in_bufs[1].cbBuffer;
239
in_bufs[1].BufferType = SECBUFFER_EMPTY;
240
in_bufs[1].cbBuffer = 0;
241
}
242
}
243
244
done:
245
ber_memfree( in_bufs[0].pvBuffer );
246
FreeContextBuffer( out_bufs[0].pvBuffer );
247
return status == SEC_E_OK ? 0 : -1;
248
}
249
250
static int
251
tlsw_session_upflags( Sockbuf *sb, tls_session *session, int rc )
252
{
253
return 0;
254
}
255
256
static char *
257
tlsw_session_errmsg( tls_session *session, int rc, char *buf, size_t len )
258
{
259
return NULL;
260
}
261
262
static void
263
tlsw_x509_cert_dn( struct berval *cert, struct berval *dn, int get_subject )
264
{
265
BerElementBuffer berbuf;
266
BerElement *ber = (BerElement *)&berbuf;
267
ber_tag_t tag;
268
ber_len_t len;
269
ber_int_t i;
270
271
ber_init2( ber, cert, LBER_USE_DER );
272
tag = ber_skip_tag( ber, &len ); /* Sequence */
273
tag = ber_skip_tag( ber, &len ); /* Sequence */
274
tag = ber_peek_tag( ber, &len ); /* Context + Constructed (version) */
275
if ( tag == 0xa0 ) { /* Version is optional */
276
tag = ber_skip_tag( ber, &len );
277
tag = ber_get_int( ber, &i ); /* Int: Version */
278
}
279
tag = ber_skip_tag( ber, &len ); /* Int: Serial (can be longer than ber_int_t) */
280
ber_skip_data( ber, len );
281
tag = ber_skip_tag( ber, &len ); /* Sequence: Signature */
282
ber_skip_data( ber, len );
283
if ( !get_subject ) {
284
tag = ber_peek_tag( ber, &len ); /* Sequence: Issuer DN */
285
} else {
286
tag = ber_skip_tag( ber, &len );
287
ber_skip_data( ber, len );
288
tag = ber_skip_tag( ber, &len ); /* Sequence: Validity */
289
ber_skip_data( ber, len );
290
tag = ber_peek_tag( ber, &len ); /* Sequence: Subject DN */
291
}
292
len = ber_ptrlen( ber );
293
dn->bv_val = cert->bv_val + len;
294
dn->bv_len = cert->bv_len - len;
295
}
296
297
static int
298
tlsw_session_my_dn( tls_session *session, struct berval *der_dn )
299
{
300
return -1;
301
}
302
303
static int
304
tlsw_session_peer_dn( tls_session *session, struct berval *der_dn )
305
{
306
return -1;
307
}
308
309
static int
310
tlsw_session_chkhost( LDAP *ld, tls_session *session, const char *name_in )
311
{
312
return 0;
313
}
314
315
static int
316
tlsw_session_strength( tls_session *session )
317
{
318
return 0;
319
}
320
321
static int
322
tlsw_session_unique( tls_session *session, struct berval *buf, int is_server )
323
{
324
return -1;
325
}
326
327
static int
328
tlsw_session_endpoint( tls_session *session, struct berval *buf, int is_server )
329
{
330
return 0;
331
}
332
333
static const char *
334
tlsw_session_version( tls_session *session )
335
{
336
return NULL;
337
}
338
339
static const char *
340
tlsw_session_cipher( tls_session *session )
341
{
342
return NULL;
343
}
344
345
static int
346
tlsw_session_peercert( tls_session *session, struct berval *der )
347
{
348
return -1;
349
}
350
351
static int
352
tlsw_session_pinning( LDAP *ld, tls_session *session, char *hashalg, struct berval *hash )
353
{
354
return -1;
355
}
356
357
/*
358
* TLS support for LBER Sockbufs
359
*/
360
361
struct buffer
362
{
363
char *buf;
364
size_t size;
365
size_t offset;
366
};
367
368
struct tls_data {
369
tlsw_session *session;
370
struct buffer header;
371
struct buffer data;
372
struct buffer trailer;
373
struct buffer recv;
374
};
375
376
static int
377
tlsw_sb_setup( Sockbuf_IO_Desc *sbiod, void *arg )
378
{
379
struct tls_data *tls;
380
tlsw_session *session = arg;
381
SECURITY_STATUS status;
382
383
assert( sbiod != NULL );
384
385
tls = ber_memcalloc( 1, sizeof( *tls ) );
386
if ( tls == NULL ) {
387
return -1;
388
}
389
390
tls->session = session;
391
sbiod->sbiod_pvt = tls;
392
session->sbiod = sbiod;
393
return 0;
394
}
395
396
static int
397
tlsw_sb_remove( Sockbuf_IO_Desc *sbiod )
398
{
399
struct tls_data *tls;
400
401
assert( sbiod != NULL );
402
assert( sbiod->sbiod_pvt != NULL );
403
404
tls = (struct tls_data *)sbiod->sbiod_pvt;
405
ber_memfree( tls->header.buf );
406
ber_memfree( tls->data.buf );
407
ber_memfree( tls->trailer.buf );
408
ber_memfree( tls->recv.buf );
409
DeleteSecurityContext( &tls->session->ctxt_handle );
410
FreeCredentialsHandle( &tls->session->cred_handle );
411
ber_memfree( tls->session );
412
ber_memfree( sbiod->sbiod_pvt );
413
sbiod->sbiod_pvt = NULL;
414
return 0;
415
}
416
417
static int
418
tlsw_sb_close( Sockbuf_IO_Desc *sbiod )
419
{
420
/* FIXME: send close_notify message */
421
return 0;
422
}
423
424
static int
425
tlsw_sb_ctrl( Sockbuf_IO_Desc *sbiod, int opt, void *arg )
426
{
427
struct tls_data *tls;
428
429
assert( sbiod != NULL );
430
assert( sbiod->sbiod_pvt != NULL );
431
432
tls = (struct tls_data *)sbiod->sbiod_pvt;
433
434
if ( opt == LBER_SB_OPT_GET_SSL ) {
435
*((tlsw_session **)arg) = tls->session;
436
return 1;
437
}
438
else if ( opt == LBER_SB_OPT_DATA_READY ) {
439
if ( tls->data.offset > 0 || tls->recv.offset > 0 ) {
440
return 1;
441
}
442
}
443
444
return LBER_SBIOD_CTRL_NEXT( sbiod, opt, arg );
445
}
446
447
static int alloc_buffer( struct buffer *buf, size_t size )
448
{
449
buf->size = size;
450
buf->offset = 0;
451
buf->buf = ber_memalloc( buf->size );
452
return buf->buf != NULL;
453
}
454
455
static int setup_buffers( struct tls_data *tls )
456
{
457
SecPkgContext_StreamSizes sizes;
458
SECURITY_STATUS status;
459
460
if ( tls->header.buf )
461
return 0;
462
463
status = QueryContextAttributesA( &tls->session->ctxt_handle, SECPKG_ATTR_STREAM_SIZES, &sizes );
464
if ( status != SEC_E_OK )
465
return -1;
466
467
if ( !alloc_buffer( &tls->header, sizes.cbHeader ) )
468
return -1;
469
470
if ( !alloc_buffer( &tls->data, sizes.cbMaximumMessage ) ) {
471
ber_memfree( tls->header.buf );
472
return -1;
473
}
474
475
if ( !alloc_buffer( &tls->trailer, sizes.cbTrailer ) ) {
476
ber_memfree( tls->header.buf );
477
ber_memfree( tls->data.buf );
478
return -1;
479
}
480
481
if ( !alloc_buffer( &tls->recv, sizes.cbMaximumMessage ) ) {
482
ber_memfree( tls->header.buf );
483
ber_memfree( tls->data.buf );
484
ber_memfree( tls->trailer.buf );
485
return -1;
486
}
487
488
return 0;
489
}
490
491
static void init_secbuf( SecBuffer *secbuf, DWORD type, void *buf, DWORD size ) {
492
secbuf->BufferType = type;
493
secbuf->cbBuffer = size;
494
secbuf->pvBuffer = buf;
495
}
496
497
static ber_len_t remove_data( struct tls_data *tls, ber_len_t len )
498
{
499
tls->data.offset -= len;
500
memmove( tls->data.buf, tls->data.buf + len, tls->data.offset );
501
return len;
502
}
503
504
static int grow_buffer( struct buffer *buf, size_t min_size )
505
{
506
size_t size = max( min_size, buf->size * 2 );
507
char *tmp;
508
509
if ( buf->size >= min_size )
510
return 0;
511
512
tmp = ber_memrealloc( buf->buf, size );
513
if ( !tmp )
514
return -1;
515
buf->buf = tmp;
516
buf->size = size;
517
return 0;
518
}
519
520
static ber_slen_t
521
tlsw_sb_read( Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len)
522
{
523
struct tls_data *tls = sbiod->sbiod_pvt;
524
SecBuffer bufs[4];
525
SecBufferDesc buf_desc = { SECBUFFER_VERSION, 4, bufs };
526
SECURITY_STATUS status = SEC_E_OK;
527
ssize_t size, expected = max( len, tls->header.size ), ret = -1;
528
529
if ( !len )
530
return 0;
531
if ( setup_buffers( tls ) < 0 )
532
return -1;
533
534
if ( tls->data.offset >= len ) {
535
memcpy( buf, tls->data.buf, len );
536
return remove_data( tls, len );
537
}
538
539
restart:
540
if ( grow_buffer( &tls->recv, expected ) < 0 )
541
return -1;
542
while ( tls->recv.offset < expected ) {
543
size = tlsw_recv( sbiod, tls->recv.buf + tls->recv.offset, expected - tls->recv.offset );
544
if ( size < 0 )
545
return size;
546
tls->recv.offset += size;
547
}
548
549
if ( grow_buffer( &tls->data, tls->data.offset + tls->recv.offset ) < 0 )
550
return -1;
551
memcpy( tls->data.buf + tls->data.offset, tls->recv.buf, tls->recv.offset );
552
553
init_secbuf( &bufs[0], SECBUFFER_DATA, tls->data.buf + tls->data.offset, tls->recv.offset );
554
init_secbuf( &bufs[1], SECBUFFER_EMPTY, NULL, 0 );
555
init_secbuf( &bufs[2], SECBUFFER_EMPTY, NULL, 0 );
556
init_secbuf( &bufs[3], SECBUFFER_EMPTY, NULL, 0 );
557
558
status = DecryptMessage( &tls->session->ctxt_handle, &buf_desc, 0, NULL );
559
if ( status == SEC_E_OK ) {
560
assert( bufs[0].BufferType == SECBUFFER_STREAM_HEADER );
561
assert( bufs[1].BufferType == SECBUFFER_DATA );
562
assert( (char *)bufs[1].pvBuffer == (char *)bufs[0].pvBuffer + tls->header.size );
563
564
tls->recv.offset = 0;
565
memmove( tls->data.buf, tls->data.buf + tls->header.size, bufs[1].cbBuffer );
566
tls->data.offset += bufs[1].cbBuffer;
567
if ( tls->data.offset >= len ) {
568
memcpy( buf, tls->data.buf, len );
569
ret = remove_data( tls, len );
570
}
571
}
572
else if ( status == SEC_E_INCOMPLETE_MESSAGE ) {
573
assert( bufs[1].BufferType == SECBUFFER_MISSING );
574
expected = bufs[1].cbBuffer;
575
goto restart;
576
}
577
578
return ret;
579
}
580
581
static ber_slen_t
582
tlsw_sb_write( Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len)
583
{
584
struct tls_data *tls = sbiod->sbiod_pvt;
585
SecBuffer bufs[3];
586
SecBufferDesc buf_desc = { SECBUFFER_VERSION, 3, bufs };
587
SECURITY_STATUS status;
588
ssize_t size, ret = len;
589
unsigned int i;
590
591
if ( !len )
592
return 0;
593
if ( setup_buffers( tls ) < 0 )
594
return -1;
595
596
init_secbuf( &bufs[0], SECBUFFER_STREAM_HEADER, tls->header.buf, tls->header.size );
597
init_secbuf( &bufs[1], SECBUFFER_DATA, buf, len );
598
init_secbuf( &bufs[2], SECBUFFER_STREAM_TRAILER, tls->trailer.buf, tls->trailer.size );
599
600
status = EncryptMessage( &tls->session->ctxt_handle, 0, &buf_desc, 0 );
601
if ( status != SEC_E_OK )
602
ret = -1;
603
else {
604
for ( i = 0; i < 3; i++ ) {
605
size = tlsw_send( sbiod, bufs[i].pvBuffer, bufs[i].cbBuffer );
606
if ( size < 0 ) {
607
ret = -1;
608
break;
609
}
610
}
611
}
612
613
return ret;
614
}
615
616
static Sockbuf_IO tlsw_sbio =
617
{
618
tlsw_sb_setup, /* sbi_setup */
619
tlsw_sb_remove, /* sbi_remove */
620
tlsw_sb_ctrl, /* sbi_ctrl */
621
tlsw_sb_read, /* sbi_read */
622
tlsw_sb_write, /* sbi_write */
623
tlsw_sb_close /* sbi_close */
624
};
625
626
tls_impl ldap_int_tls_impl = {
627
"Windows SSPI SChannel",
628
629
tlsw_init,
630
tlsw_destroy,
631
632
tlsw_ctx_new,
633
tlsw_ctx_ref,
634
tlsw_ctx_free,
635
tlsw_ctx_init,
636
637
tlsw_session_new,
638
tlsw_session_connect,
639
tlsw_session_accept,
640
tlsw_session_upflags,
641
tlsw_session_errmsg,
642
tlsw_session_my_dn,
643
tlsw_session_peer_dn,
644
tlsw_session_chkhost,
645
tlsw_session_strength,
646
tlsw_session_unique,
647
tlsw_session_endpoint,
648
tlsw_session_version,
649
tlsw_session_cipher,
650
tlsw_session_peercert,
651
tlsw_session_pinning,
652
653
&tlsw_sbio,
654
655
#ifdef LDAP_R_COMPILE
656
tlsw_thr_init,
657
#else
658
NULL,
659
#endif
660
661
0
662
};
663
664