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