imapext-2007

annotate src/osdep/nt/ssl_old.c @ 0:ada5e610ab86

imap-2007e
author yuuji@gentei.org
date Mon, 14 Sep 2009 15:17:45 +0900
parents
children
rev   line source
yuuji@0 1 /* ========================================================================
yuuji@0 2 * Copyright 1988-2008 University of Washington
yuuji@0 3 *
yuuji@0 4 * Licensed under the Apache License, Version 2.0 (the "License");
yuuji@0 5 * you may not use this file except in compliance with the License.
yuuji@0 6 * You may obtain a copy of the License at
yuuji@0 7 *
yuuji@0 8 * http://www.apache.org/licenses/LICENSE-2.0
yuuji@0 9 *
yuuji@0 10 *
yuuji@0 11 * ========================================================================
yuuji@0 12 */
yuuji@0 13
yuuji@0 14 /*
yuuji@0 15 * Program: SSL authentication/encryption module for Windows 9x and NT
yuuji@0 16 *
yuuji@0 17 * Author: Mark Crispin
yuuji@0 18 * Networks and Distributed Computing
yuuji@0 19 * Computing & Communications
yuuji@0 20 * University of Washington
yuuji@0 21 * Administration Building, AG-44
yuuji@0 22 * Seattle, WA 98195
yuuji@0 23 * Internet: MRC@CAC.Washington.EDU
yuuji@0 24 *
yuuji@0 25 * Date: 22 September 1998
yuuji@0 26 * Last Edited: 13 January 2008
yuuji@0 27 */
yuuji@0 28
yuuji@0 29 #define SECURITY_WIN32
yuuji@0 30 #include <sspi.h>
yuuji@0 31 #if(_WIN32_WINNT < 0x0400)
yuuji@0 32 typedef unsigned int ALG_ID;
yuuji@0 33 #else
yuuji@0 34 #include <wincrypt.h>
yuuji@0 35 ALGIDDEF
yuuji@0 36 #endif
yuuji@0 37 #include <schnlsp.h>
yuuji@0 38 #include <issperr.h>
yuuji@0 39
yuuji@0 40 /* in case a binary runs on Windows 2000 */
yuuji@0 41 #ifndef ISC_REQ_MANUAL_CRED_VALIDATION
yuuji@0 42 #define ISC_REQ_MANUAL_CRED_VALIDATION 0x00080000
yuuji@0 43 #endif
yuuji@0 44 #ifndef SEC_E_UNTRUSTED_ROOT
yuuji@0 45 #define SEC_E_UNTRUSTED_ROOT ((HRESULT) 0x80090325L)
yuuji@0 46 #endif
yuuji@0 47 #ifndef SEC_E_CERT_EXPIRED
yuuji@0 48 #define SEC_E_CERT_EXPIRED ((HRESULT) 0x80090328L)
yuuji@0 49 #endif
yuuji@0 50
yuuji@0 51
yuuji@0 52 #define SSLBUFLEN 8192
yuuji@0 53
yuuji@0 54
yuuji@0 55 /* SSL I/O stream */
yuuji@0 56
yuuji@0 57 typedef struct ssl_stream {
yuuji@0 58 TCPSTREAM *tcpstream; /* TCP stream */
yuuji@0 59 CredHandle cred; /* SSL credentials */
yuuji@0 60 CtxtHandle context; /* SSL context */
yuuji@0 61 /* stream encryption sizes */
yuuji@0 62 SecPkgContext_StreamSizes sizes;
yuuji@0 63 size_t bufsize;
yuuji@0 64 int ictr; /* input counter */
yuuji@0 65 char *iptr; /* input pointer */
yuuji@0 66 int iextractr; /* extra input counter */
yuuji@0 67 char *iextraptr; /* extra input pointer */
yuuji@0 68 char *ibuf; /* input buffer */
yuuji@0 69 char *obuf; /* output buffer */
yuuji@0 70 } SSLSTREAM;
yuuji@0 71
yuuji@0 72 #include "sslio.h"
yuuji@0 73
yuuji@0 74
yuuji@0 75 /* Function prototypes */
yuuji@0 76
yuuji@0 77 static SSLSTREAM *ssl_start(TCPSTREAM *tstream,char *host,unsigned long flags);
yuuji@0 78 static char *ssl_analyze_status (SECURITY_STATUS err,char *buf);
yuuji@0 79 static char *ssl_getline_work (SSLSTREAM *stream,unsigned long *size,
yuuji@0 80 long *contd);
yuuji@0 81 static long ssl_abort (SSLSTREAM *stream);
yuuji@0 82
yuuji@0 83 /* Secure Sockets Layer network driver dispatch */
yuuji@0 84
yuuji@0 85 static struct ssl_driver ssldriver = {
yuuji@0 86 ssl_open, /* open connection */
yuuji@0 87 ssl_aopen, /* open preauthenticated connection */
yuuji@0 88 ssl_getline, /* get a line */
yuuji@0 89 ssl_getbuffer, /* get a buffer */
yuuji@0 90 ssl_soutr, /* output pushed data */
yuuji@0 91 ssl_sout, /* output string */
yuuji@0 92 ssl_close, /* close connection */
yuuji@0 93 ssl_host, /* return host name */
yuuji@0 94 ssl_remotehost, /* return remote host name */
yuuji@0 95 ssl_port, /* return port number */
yuuji@0 96 ssl_localhost /* return local host name */
yuuji@0 97 };
yuuji@0 98
yuuji@0 99 /* security function table */
yuuji@0 100 static SecurityFunctionTable *sft = NIL;
yuuji@0 101 static unsigned long ssltsz = 0;/* SSL maximum token length */
yuuji@0 102
yuuji@0 103 /* One-time SSL initialization */
yuuji@0 104
yuuji@0 105 static int sslonceonly = 0;
yuuji@0 106
yuuji@0 107 void ssl_onceonlyinit (void)
yuuji@0 108 {
yuuji@0 109 if (!sslonceonly++) { /* only need to call it once */
yuuji@0 110 HINSTANCE lib;
yuuji@0 111 FARPROC pi;
yuuji@0 112 ULONG np;
yuuji@0 113 SecPkgInfo *pp;
yuuji@0 114 int i;
yuuji@0 115 /* get security library */
yuuji@0 116 if (((lib = LoadLibrary ("schannel.dll")) ||
yuuji@0 117 (lib = LoadLibrary ("security.dll"))) &&
yuuji@0 118 (pi = GetProcAddress (lib,SECURITY_ENTRYPOINT)) &&
yuuji@0 119 (sft = (SecurityFunctionTable *) pi ()) &&
yuuji@0 120 !(sft->EnumerateSecurityPackages (&np,&pp))) {
yuuji@0 121 /* look for an SSL package */
yuuji@0 122 for (i = 0; (i < (int) np); i++) if (!strcmp (pp[i].Name,UNISP_NAME)) {
yuuji@0 123 /* note maximum token size and name */
yuuji@0 124 ssltsz = pp[i].cbMaxToken;
yuuji@0 125 /* apply runtime linkage */
yuuji@0 126 mail_parameters (NIL,SET_SSLDRIVER,(void *) &ssldriver);
yuuji@0 127 mail_parameters (NIL,SET_SSLSTART,(void *) ssl_start);
yuuji@0 128 return; /* all done */
yuuji@0 129 }
yuuji@0 130 }
yuuji@0 131 }
yuuji@0 132 }
yuuji@0 133
yuuji@0 134 /* SSL open
yuuji@0 135 * Accepts: host name
yuuji@0 136 * contact service name
yuuji@0 137 * contact port number
yuuji@0 138 * Returns: SSL stream if success else NIL
yuuji@0 139 */
yuuji@0 140
yuuji@0 141 SSLSTREAM *ssl_open (char *host,char *service,unsigned long port)
yuuji@0 142 {
yuuji@0 143 TCPSTREAM *stream = tcp_open (host,service,port);
yuuji@0 144 return stream ? ssl_start (stream,host,port) : NIL;
yuuji@0 145 }
yuuji@0 146
yuuji@0 147
yuuji@0 148 /* SSL authenticated open
yuuji@0 149 * Accepts: host name
yuuji@0 150 * service name
yuuji@0 151 * returned user name buffer
yuuji@0 152 * Returns: SSL stream if success else NIL
yuuji@0 153 */
yuuji@0 154
yuuji@0 155 SSLSTREAM *ssl_aopen (NETMBX *mb,char *service,char *usrbuf)
yuuji@0 156 {
yuuji@0 157 return NIL; /* don't use this mechanism with SSL */
yuuji@0 158 }
yuuji@0 159
yuuji@0 160 /* Start SSL/TLS negotiations
yuuji@0 161 * Accepts: open TCP stream of session
yuuji@0 162 * user's host name
yuuji@0 163 * flags
yuuji@0 164 * Returns: SSL stream if success else NIL
yuuji@0 165 */
yuuji@0 166
yuuji@0 167 static SSLSTREAM *ssl_start (TCPSTREAM *tstream,char *host,unsigned long flags)
yuuji@0 168 {
yuuji@0 169 SECURITY_STATUS e;
yuuji@0 170 ULONG a;
yuuji@0 171 TimeStamp t;
yuuji@0 172 SecBuffer ibuf[2],obuf[1];
yuuji@0 173 SecBufferDesc ibufs,obufs;
yuuji@0 174 char tmp[MAILTMPLEN];
yuuji@0 175 char *reason = NIL;
yuuji@0 176 ULONG req = ISC_REQ_REPLAY_DETECT | ISC_REQ_SEQUENCE_DETECT |
yuuji@0 177 ISC_REQ_CONFIDENTIALITY | ISC_REQ_USE_SESSION_KEY |
yuuji@0 178 ISC_REQ_ALLOCATE_MEMORY | ISC_REQ_STREAM | ISC_REQ_EXTENDED_ERROR +
yuuji@0 179 ((flags & NET_NOVALIDATECERT) ? ISC_REQ_MANUAL_CRED_VALIDATION :
yuuji@0 180 ISC_REQ_MUTUAL_AUTH);
yuuji@0 181 SCHANNEL_CRED tlscred;
yuuji@0 182 char *buf = (char *) fs_get (ssltsz);
yuuji@0 183 unsigned long size = 0;
yuuji@0 184 sslfailure_t sf = (sslfailure_t) mail_parameters (NIL,GET_SSLFAILURE,NIL);
yuuji@0 185 SSLSTREAM *stream = (SSLSTREAM *) memset (fs_get (sizeof (SSLSTREAM)),0,
yuuji@0 186 sizeof (SSLSTREAM));
yuuji@0 187 stream->tcpstream = tstream; /* bind TCP stream */
yuuji@0 188 /* initialize TLS credential */
yuuji@0 189 memset (&tlscred,0,sizeof (SCHANNEL_CRED));
yuuji@0 190 tlscred.dwVersion = SCHANNEL_CRED_VERSION;
yuuji@0 191 tlscred.grbitEnabledProtocols = SP_PROT_TLS1;
yuuji@0 192
yuuji@0 193 /* acquire credentials */
yuuji@0 194 if (sft->AcquireCredentialsHandle
yuuji@0 195 (NIL,UNISP_NAME,SECPKG_CRED_OUTBOUND,NIL,(flags & NET_TLSCLIENT) ?
yuuji@0 196 &tlscred : NIL,NIL,NIL,&stream->cred,&t)
yuuji@0 197 != SEC_E_OK) reason = "Acquire credentials handle failed";
yuuji@0 198 else while (!reason) { /* negotiate security context */
yuuji@0 199 /* initialize buffers */
yuuji@0 200 ibuf[0].cbBuffer = size; ibuf[0].pvBuffer = buf;
yuuji@0 201 ibuf[1].cbBuffer = 0; ibuf[1].pvBuffer = NIL;
yuuji@0 202 obuf[0].cbBuffer = 0; obuf[0].pvBuffer = NIL;
yuuji@0 203 ibuf[0].BufferType = obuf[0].BufferType = SECBUFFER_TOKEN;
yuuji@0 204 ibuf[1].BufferType = SECBUFFER_EMPTY;
yuuji@0 205 /* initialize buffer descriptors */
yuuji@0 206 ibufs.ulVersion = obufs.ulVersion = SECBUFFER_VERSION;
yuuji@0 207 ibufs.cBuffers = 2; obufs.cBuffers = 1;
yuuji@0 208 ibufs.pBuffers = ibuf; obufs.pBuffers = obuf;
yuuji@0 209 /* negotiate security */
yuuji@0 210 e = sft->InitializeSecurityContext
yuuji@0 211 (&stream->cred,size ? &stream->context : NIL,host,req,0,
yuuji@0 212 SECURITY_NETWORK_DREP,size? &ibufs:NIL,0,&stream->context,&obufs,&a,&t);
yuuji@0 213 /* have an output buffer we need to send? */
yuuji@0 214 if (obuf[0].pvBuffer && obuf[0].cbBuffer) {
yuuji@0 215 if (!tcp_sout (stream->tcpstream,obuf[0].pvBuffer,obuf[0].cbBuffer))
yuuji@0 216 reason = "Unexpected TCP output disconnect";
yuuji@0 217 /* free the buffer */
yuuji@0 218 sft->FreeContextBuffer (obuf[0].pvBuffer);
yuuji@0 219 }
yuuji@0 220 if (!reason) switch (e) { /* negotiation state */
yuuji@0 221 case SEC_I_INCOMPLETE_CREDENTIALS:
yuuji@0 222 break; /* server wants client auth */
yuuji@0 223 case SEC_I_CONTINUE_NEEDED:
yuuji@0 224 if (size) { /* continue, read any data? */
yuuji@0 225 /* yes, anything regurgiated back to us? */
yuuji@0 226 if (ibuf[1].BufferType == SECBUFFER_EXTRA) {
yuuji@0 227 /* yes, set this as the new data */
yuuji@0 228 memmove (buf,buf + size - ibuf[1].cbBuffer,ibuf[1].cbBuffer);
yuuji@0 229 size = ibuf[1].cbBuffer;
yuuji@0 230 break;
yuuji@0 231 }
yuuji@0 232 size = 0; /* otherwise, read more stuff from server */
yuuji@0 233 }
yuuji@0 234 case SEC_E_INCOMPLETE_MESSAGE:
yuuji@0 235 /* need to read more data from server */
yuuji@0 236 if (!tcp_getdata (stream->tcpstream))
yuuji@0 237 reason = "Unexpected TCP input disconnect";
yuuji@0 238 else {
yuuji@0 239 memcpy (buf+size,stream->tcpstream->iptr,stream->tcpstream->ictr);
yuuji@0 240 size += stream->tcpstream->ictr;
yuuji@0 241 /* empty it from TCP's buffers */
yuuji@0 242 stream->tcpstream->iptr += stream->tcpstream->ictr;
yuuji@0 243 stream->tcpstream->ictr = 0;
yuuji@0 244 }
yuuji@0 245 break;
yuuji@0 246
yuuji@0 247 case SEC_E_OK: /* success, any data to be regurgitated? */
yuuji@0 248 if (ibuf[1].BufferType == SECBUFFER_EXTRA) {
yuuji@0 249 /* yes, set this as the new data */
yuuji@0 250 memmove (stream->tcpstream->iptr = stream->tcpstream->ibuf,
yuuji@0 251 buf + size - ibuf[1].cbBuffer,ibuf[1].cbBuffer);
yuuji@0 252 stream->tcpstream->ictr = ibuf[1].cbBuffer;
yuuji@0 253 }
yuuji@0 254 if (reason = ssl_analyze_status
yuuji@0 255 (sft->QueryContextAttributes
yuuji@0 256 (&stream->context,SECPKG_ATTR_STREAM_SIZES,&stream->sizes),buf))
yuuji@0 257 break; /* error getting sizes */
yuuji@0 258 fs_give ((void **) &buf); /* flush temporary buffer */
yuuji@0 259 /* make maximum-sized buffers */
yuuji@0 260 stream->bufsize = stream->sizes.cbHeader +
yuuji@0 261 stream->sizes.cbMaximumMessage + stream->sizes.cbTrailer;
yuuji@0 262 if (stream->sizes.cbMaximumMessage < SSLBUFLEN)
yuuji@0 263 fatal ("cbMaximumMessage is less than SSLBUFLEN!");
yuuji@0 264 else if (stream->sizes.cbMaximumMessage < 16384) {
yuuji@0 265 sprintf (tmp,"WINDOWS BUG: cbMaximumMessage = %ld, should be 16384",
yuuji@0 266 (long) stream->sizes.cbMaximumMessage);
yuuji@0 267 mm_log (tmp,NIL);
yuuji@0 268 }
yuuji@0 269 stream->ibuf = (char *) fs_get (stream->bufsize);
yuuji@0 270 stream->obuf = (char *) fs_get (stream->bufsize);
yuuji@0 271 return stream;
yuuji@0 272 default:
yuuji@0 273 reason = ssl_analyze_status (e,buf);
yuuji@0 274 }
yuuji@0 275 }
yuuji@0 276 ssl_close (stream); /* failed to do SSL */
yuuji@0 277 stream = NIL; /* no stream returned */
yuuji@0 278 fs_give ((void **) &buf); /* flush temporary buffer */
yuuji@0 279 switch (*reason) { /* analyze reason */
yuuji@0 280 case '*': /* certificate failure */
yuuji@0 281 ++reason; /* skip over certificate failure indication */
yuuji@0 282 /* pass to error callback */
yuuji@0 283 if (sf) (*sf) (host,reason,flags);
yuuji@0 284 else { /* no error callback, build error message */
yuuji@0 285 sprintf (tmp,"Certificate failure for %.80s: %.512s",host,reason);
yuuji@0 286 mm_log (tmp,ERROR);
yuuji@0 287 }
yuuji@0 288 case '\0': /* user answered no to certificate callback */
yuuji@0 289 if (flags & NET_TRYSSL) /* return dummy stream to stop tryssl */
yuuji@0 290 stream = (SSLSTREAM *) memset (fs_get (sizeof (SSLSTREAM)),0,
yuuji@0 291 sizeof (SSLSTREAM));
yuuji@0 292 break;
yuuji@0 293 default: /* non-certificate failure */
yuuji@0 294 if (flags & NET_TRYSSL); /* no error output if tryssl */
yuuji@0 295 /* pass to error callback */
yuuji@0 296 else if (sf) (*sf) (host,reason,flags);
yuuji@0 297 else { /* no error callback, build error message */
yuuji@0 298 sprintf (tmp,"TLS/SSL failure for %.80s: %.512s",host,reason);
yuuji@0 299 mm_log (tmp,ERROR);
yuuji@0 300 }
yuuji@0 301 break;
yuuji@0 302 }
yuuji@0 303 return stream;
yuuji@0 304 }
yuuji@0 305
yuuji@0 306 /* Generate error text from SSL error code
yuuji@0 307 * Accepts: SSL status
yuuji@0 308 * scratch buffer
yuuji@0 309 * Returns: text if error status, else NIL
yuuji@0 310 */
yuuji@0 311
yuuji@0 312 static char *ssl_analyze_status (SECURITY_STATUS err,char *buf)
yuuji@0 313 {
yuuji@0 314 switch (err) {
yuuji@0 315 case SEC_E_OK: /* no error */
yuuji@0 316 case SEC_I_CONTINUE_NEEDED:
yuuji@0 317 case SEC_I_INCOMPLETE_CREDENTIALS:
yuuji@0 318 case SEC_E_INCOMPLETE_MESSAGE:
yuuji@0 319 return NIL;
yuuji@0 320 case SEC_E_NO_AUTHENTICATING_AUTHORITY:
yuuji@0 321 return "*No authority could be contacted for authentication";
yuuji@0 322 case SEC_E_WRONG_PRINCIPAL:
yuuji@0 323 return "*Server name does not match certificate";
yuuji@0 324 case SEC_E_UNTRUSTED_ROOT:
yuuji@0 325 return "*Self-signed certificate or untrusted authority";
yuuji@0 326 case SEC_E_CERT_EXPIRED:
yuuji@0 327 return "*Certificate has expired";
yuuji@0 328 case SEC_E_INVALID_TOKEN:
yuuji@0 329 return "Invalid token, probably not an SSL server";
yuuji@0 330 case SEC_E_UNSUPPORTED_FUNCTION:
yuuji@0 331 return "SSL not supported on this machine - upgrade your system software";
yuuji@0 332 }
yuuji@0 333 sprintf (buf,"Unexpected SChannel error %lx - report this",err);
yuuji@0 334 return buf;
yuuji@0 335 }
yuuji@0 336
yuuji@0 337 /* SSL receive line
yuuji@0 338 * Accepts: SSL stream
yuuji@0 339 * Returns: text line string or NIL if failure
yuuji@0 340 */
yuuji@0 341
yuuji@0 342 char *ssl_getline (SSLSTREAM *stream)
yuuji@0 343 {
yuuji@0 344 unsigned long n,contd;
yuuji@0 345 char *ret = ssl_getline_work (stream,&n,&contd);
yuuji@0 346 if (ret && contd) { /* got a line needing continuation? */
yuuji@0 347 STRINGLIST *stl = mail_newstringlist ();
yuuji@0 348 STRINGLIST *stc = stl;
yuuji@0 349 do { /* collect additional lines */
yuuji@0 350 stc->text.data = (unsigned char *) ret;
yuuji@0 351 stc->text.size = n;
yuuji@0 352 stc = stc->next = mail_newstringlist ();
yuuji@0 353 ret = ssl_getline_work (stream,&n,&contd);
yuuji@0 354 } while (ret && contd);
yuuji@0 355 if (ret) { /* stash final part of line on list */
yuuji@0 356 stc->text.data = (unsigned char *) ret;
yuuji@0 357 stc->text.size = n;
yuuji@0 358 /* determine how large a buffer we need */
yuuji@0 359 for (n = 0, stc = stl; stc; stc = stc->next) n += stc->text.size;
yuuji@0 360 ret = fs_get (n + 1); /* copy parts into buffer */
yuuji@0 361 for (n = 0, stc = stl; stc; n += stc->text.size, stc = stc->next)
yuuji@0 362 memcpy (ret + n,stc->text.data,stc->text.size);
yuuji@0 363 ret[n] = '\0';
yuuji@0 364 }
yuuji@0 365 mail_free_stringlist (&stl);/* either way, done with list */
yuuji@0 366 }
yuuji@0 367 return ret;
yuuji@0 368 }
yuuji@0 369
yuuji@0 370 /* SSL receive line or partial line
yuuji@0 371 * Accepts: SSL stream
yuuji@0 372 * pointer to return size
yuuji@0 373 * pointer to return continuation flag
yuuji@0 374 * Returns: text line string, size and continuation flag, or NIL if failure
yuuji@0 375 */
yuuji@0 376
yuuji@0 377 static char *ssl_getline_work (SSLSTREAM *stream,unsigned long *size,
yuuji@0 378 long *contd)
yuuji@0 379 {
yuuji@0 380 unsigned long n;
yuuji@0 381 char *s,*ret,c,d;
yuuji@0 382 *contd = NIL; /* assume no continuation */
yuuji@0 383 /* make sure have data */
yuuji@0 384 if (!ssl_getdata (stream)) return NIL;
yuuji@0 385 for (s = stream->iptr, n = 0, c = '\0'; stream->ictr--; n++, c = d) {
yuuji@0 386 d = *stream->iptr++; /* slurp another character */
yuuji@0 387 if ((c == '\015') && (d == '\012')) {
yuuji@0 388 ret = (char *) fs_get (n--);
yuuji@0 389 memcpy (ret,s,*size = n); /* copy into a free storage string */
yuuji@0 390 ret[n] = '\0'; /* tie off string with null */
yuuji@0 391 return ret;
yuuji@0 392 }
yuuji@0 393 }
yuuji@0 394 /* copy partial string from buffer */
yuuji@0 395 memcpy ((ret = (char *) fs_get (n)),s,*size = n);
yuuji@0 396 /* get more data from the net */
yuuji@0 397 if (!ssl_getdata (stream)) fs_give ((void **) &ret);
yuuji@0 398 /* special case of newline broken by buffer */
yuuji@0 399 else if ((c == '\015') && (*stream->iptr == '\012')) {
yuuji@0 400 stream->iptr++; /* eat the line feed */
yuuji@0 401 stream->ictr--;
yuuji@0 402 ret[*size = --n] = '\0'; /* tie off string with null */
yuuji@0 403 }
yuuji@0 404 else *contd = LONGT; /* continuation needed */
yuuji@0 405 return ret;
yuuji@0 406 }
yuuji@0 407
yuuji@0 408 /* SSL receive buffer
yuuji@0 409 * Accepts: SSL stream
yuuji@0 410 * size in bytes
yuuji@0 411 * buffer to read into
yuuji@0 412 * Returns: T if success, NIL otherwise
yuuji@0 413 */
yuuji@0 414
yuuji@0 415 long ssl_getbuffer (SSLSTREAM *stream,unsigned long size,char *buffer)
yuuji@0 416 {
yuuji@0 417 unsigned long n;
yuuji@0 418 while (size > 0) { /* until request satisfied */
yuuji@0 419 if (!ssl_getdata (stream)) return NIL;
yuuji@0 420 n = min (size,stream->ictr);/* number of bytes to transfer */
yuuji@0 421 /* do the copy */
yuuji@0 422 memcpy (buffer,stream->iptr,n);
yuuji@0 423 buffer += n; /* update pointer */
yuuji@0 424 stream->iptr += n;
yuuji@0 425 size -= n; /* update # of bytes to do */
yuuji@0 426 stream->ictr -= n;
yuuji@0 427 }
yuuji@0 428 buffer[0] = '\0'; /* tie off string */
yuuji@0 429 return T;
yuuji@0 430 }
yuuji@0 431
yuuji@0 432 /* SSL receive data
yuuji@0 433 * Accepts: TCP/IP stream
yuuji@0 434 * Returns: T if success, NIL otherwise
yuuji@0 435 */
yuuji@0 436
yuuji@0 437 long ssl_getdata (SSLSTREAM *stream)
yuuji@0 438 {
yuuji@0 439 while (stream->ictr < 1) { /* decrypted buffer empty? */
yuuji@0 440 SECURITY_STATUS status;
yuuji@0 441 SecBuffer buf[4];
yuuji@0 442 SecBufferDesc msg;
yuuji@0 443 size_t i;
yuuji@0 444 size_t n = 0; /* initially no bytes to decrypt */
yuuji@0 445 do { /* yes, make sure have data from TCP */
yuuji@0 446 if (stream->iextractr) { /* have previous unread data? */
yuuji@0 447 memcpy (stream->ibuf + n,stream->iextraptr,stream->iextractr);
yuuji@0 448 n += stream->iextractr; /* update number of bytes read */
yuuji@0 449 stream->iextractr = 0; /* no more extra data */
yuuji@0 450 }
yuuji@0 451 else { /* read from TCP */
yuuji@0 452 if (!tcp_getdata (stream->tcpstream)) return ssl_abort (stream);
yuuji@0 453 /* maximum amount of data to copy */
yuuji@0 454 if (!(i = min (stream->bufsize - n,stream->tcpstream->ictr)))
yuuji@0 455 fatal ("incomplete SecBuffer exceeds maximum buffer size");
yuuji@0 456 /* do the copy */
yuuji@0 457 memcpy (stream->ibuf + n,stream->tcpstream->iptr,i);
yuuji@0 458 stream->tcpstream->iptr += i;
yuuji@0 459 stream->tcpstream->ictr -= i;
yuuji@0 460 n += i; /* update number of bytes to decrypt */
yuuji@0 461 }
yuuji@0 462 buf[0].cbBuffer = n; /* first SecBuffer gets data */
yuuji@0 463 buf[0].pvBuffer = stream->ibuf;
yuuji@0 464 buf[0].BufferType = SECBUFFER_DATA;
yuuji@0 465 /* subsequent ones are for spares */
yuuji@0 466 buf[1].BufferType = buf[2].BufferType = buf[3].BufferType =
yuuji@0 467 SECBUFFER_EMPTY;
yuuji@0 468 msg.ulVersion = SECBUFFER_VERSION;
yuuji@0 469 msg.cBuffers = 4; /* number of SecBuffers */
yuuji@0 470 msg.pBuffers = buf; /* first SecBuffer */
yuuji@0 471
yuuji@0 472 } while ((status = ((DECRYPT_MESSAGE_FN) sft->Reserved4)
yuuji@0 473 (&stream->context,&msg,0,NIL)) == SEC_E_INCOMPLETE_MESSAGE);
yuuji@0 474 switch (status) {
yuuji@0 475 case SEC_E_OK: /* won */
yuuji@0 476 case SEC_I_RENEGOTIATE: /* won but lost it after this buffer */
yuuji@0 477 /* hunt for a buffer */
yuuji@0 478 for (i = 0; (i < 4) && (buf[i].BufferType != SECBUFFER_DATA) ; i++);
yuuji@0 479 if (i < 4) { /* found a buffer? */
yuuji@0 480 /* yes, set up pointer and counter */
yuuji@0 481 stream->iptr = buf[i].pvBuffer;
yuuji@0 482 stream->ictr = buf[i].cbBuffer;
yuuji@0 483 /* any unprocessed data? */
yuuji@0 484 while (++i < 4) if (buf[i].BufferType == SECBUFFER_EXTRA) {
yuuji@0 485 /* yes, note for next time around */
yuuji@0 486 stream->iextraptr = buf[i].pvBuffer;
yuuji@0 487 stream->iextractr = buf[i].cbBuffer;
yuuji@0 488 }
yuuji@0 489 }
yuuji@0 490 break;
yuuji@0 491 default: /* anything else means we've lost */
yuuji@0 492 return ssl_abort (stream);
yuuji@0 493 }
yuuji@0 494 }
yuuji@0 495 return LONGT;
yuuji@0 496 }
yuuji@0 497
yuuji@0 498 /* SSL send string as record
yuuji@0 499 * Accepts: SSL stream
yuuji@0 500 * string pointer
yuuji@0 501 * Returns: T if success else NIL
yuuji@0 502 */
yuuji@0 503
yuuji@0 504 long ssl_soutr (SSLSTREAM *stream,char *string)
yuuji@0 505 {
yuuji@0 506 return ssl_sout (stream,string,(unsigned long) strlen (string));
yuuji@0 507 }
yuuji@0 508
yuuji@0 509
yuuji@0 510 /* SSL send string
yuuji@0 511 * Accepts: SSL stream
yuuji@0 512 * string pointer
yuuji@0 513 * byte count
yuuji@0 514 * Returns: T if success else NIL
yuuji@0 515 */
yuuji@0 516
yuuji@0 517 long ssl_sout (SSLSTREAM *stream,char *string,unsigned long size)
yuuji@0 518 {
yuuji@0 519 SecBuffer buf[4];
yuuji@0 520 SecBufferDesc msg;
yuuji@0 521 char *s = stream->ibuf;
yuuji@0 522 size_t n = 0;
yuuji@0 523 while (size) { /* until satisfied request */
yuuji@0 524 /* header */
yuuji@0 525 buf[0].BufferType = SECBUFFER_STREAM_HEADER;
yuuji@0 526 memset (buf[0].pvBuffer = stream->obuf,0,
yuuji@0 527 buf[0].cbBuffer = stream->sizes.cbHeader);
yuuji@0 528 /* message (up to maximum size) */
yuuji@0 529 buf[1].BufferType = SECBUFFER_DATA;
yuuji@0 530 memcpy (buf[1].pvBuffer = stream->obuf + stream->sizes.cbHeader,string,
yuuji@0 531 buf[1].cbBuffer = min (size,SSLBUFLEN));
yuuji@0 532 /* trailer */
yuuji@0 533 buf[2].BufferType = SECBUFFER_STREAM_TRAILER;
yuuji@0 534 memset (buf[2].pvBuffer = ((char *) buf[1].pvBuffer) + buf[1].cbBuffer,0,
yuuji@0 535 buf[2].cbBuffer = stream->sizes.cbTrailer);
yuuji@0 536 /* spare */
yuuji@0 537 buf[3].BufferType = SECBUFFER_EMPTY;
yuuji@0 538 msg.ulVersion = SECBUFFER_VERSION;
yuuji@0 539 msg.cBuffers = 4; /* number of SecBuffers */
yuuji@0 540 msg.pBuffers = buf; /* first SecBuffer */
yuuji@0 541 string += buf[1].cbBuffer;
yuuji@0 542 size -= buf[1].cbBuffer; /* this many bytes processed */
yuuji@0 543 /* encrypt and send message */
yuuji@0 544 if ((((ENCRYPT_MESSAGE_FN) sft->Reserved3)
yuuji@0 545 (&stream->context,0,&msg,NIL) != SEC_E_OK) ||
yuuji@0 546 !tcp_sout (stream->tcpstream,stream->obuf,
yuuji@0 547 buf[0].cbBuffer + buf[1].cbBuffer + buf[2].cbBuffer))
yuuji@0 548 return ssl_abort (stream);/* encryption or sending failed */
yuuji@0 549 }
yuuji@0 550 return LONGT;
yuuji@0 551 }
yuuji@0 552
yuuji@0 553 /* SSL close
yuuji@0 554 * Accepts: SSL stream
yuuji@0 555 */
yuuji@0 556
yuuji@0 557 void ssl_close (SSLSTREAM *stream)
yuuji@0 558 {
yuuji@0 559 ssl_abort (stream); /* nuke the stream */
yuuji@0 560 fs_give ((void **) &stream); /* flush the stream */
yuuji@0 561 }
yuuji@0 562
yuuji@0 563
yuuji@0 564 /* SSL abort stream
yuuji@0 565 * Accepts: SSL stream
yuuji@0 566 * Returns: NIL always
yuuji@0 567 */
yuuji@0 568
yuuji@0 569 static long ssl_abort (SSLSTREAM *stream)
yuuji@0 570 {
yuuji@0 571 if (stream->tcpstream) { /* close TCP stream */
yuuji@0 572 sft->DeleteSecurityContext (&stream->context);
yuuji@0 573 sft->FreeCredentialHandle (&stream->cred);
yuuji@0 574 tcp_close (stream->tcpstream);
yuuji@0 575 stream->tcpstream = NIL;
yuuji@0 576 }
yuuji@0 577 if (stream->ibuf) fs_give ((void **) &stream->ibuf);
yuuji@0 578 if (stream->obuf) fs_give ((void **) &stream->obuf);
yuuji@0 579 return NIL;
yuuji@0 580 }
yuuji@0 581
yuuji@0 582 /* SSL get host name
yuuji@0 583 * Accepts: SSL stream
yuuji@0 584 * Returns: host name for this stream
yuuji@0 585 */
yuuji@0 586
yuuji@0 587 char *ssl_host (SSLSTREAM *stream)
yuuji@0 588 {
yuuji@0 589 return tcp_host (stream->tcpstream);
yuuji@0 590 }
yuuji@0 591
yuuji@0 592
yuuji@0 593 /* SSL get remote host name
yuuji@0 594 * Accepts: SSL stream
yuuji@0 595 * Returns: host name for this stream
yuuji@0 596 */
yuuji@0 597
yuuji@0 598 char *ssl_remotehost (SSLSTREAM *stream)
yuuji@0 599 {
yuuji@0 600 return tcp_remotehost (stream->tcpstream);
yuuji@0 601 }
yuuji@0 602
yuuji@0 603
yuuji@0 604 /* SSL return port for this stream
yuuji@0 605 * Accepts: SSL stream
yuuji@0 606 * Returns: port number for this stream
yuuji@0 607 */
yuuji@0 608
yuuji@0 609 unsigned long ssl_port (SSLSTREAM *stream)
yuuji@0 610 {
yuuji@0 611 return tcp_port (stream->tcpstream);
yuuji@0 612 }
yuuji@0 613
yuuji@0 614
yuuji@0 615 /* SSL get local host name
yuuji@0 616 * Accepts: SSL stream
yuuji@0 617 * Returns: local host name
yuuji@0 618 */
yuuji@0 619
yuuji@0 620 char *ssl_localhost (SSLSTREAM *stream)
yuuji@0 621 {
yuuji@0 622 return tcp_localhost (stream->tcpstream);
yuuji@0 623 }
yuuji@0 624
yuuji@0 625 #include "ssl_none.c" /* currently no server support */

UW-IMAP'd extensions by yuuji