2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
22 Contains: Support for client hello and server hello messages.
24 Written by: Doug Mitchell, based on Netscape SSLRef 3.0
26 Copyright: (c) 1999 by Apple Computer, Inc., all rights reserved.
29 /* *********************************************************************
32 SSLRef 3.0 Final -- 11/19/96
34 Copyright (c)1996 by Netscape Communications Corp.
36 By retrieving this software you are bound by the licensing terms
37 disclosed in the file "LICENSE.txt". Please read it, and if you don't
38 accept the terms, delete this software.
40 SSLRef 3.0 was developed by Netscape Communications Corp. of Mountain
41 View, California <http://home.netscape.com/> and Consensus Development
42 Corporation of Berkeley, California <http://www.consensus.com/>.
44 *********************************************************************
46 File: hdskhelo.c Support for client hello and server hello messages
48 Also, encoding of Random structures and initializing the message
49 hashes used for calculating finished and certificate verify messages.
51 ****************************************************************** */
77 #ifndef _APPLE_GLUE_H_
78 #include "appleGlue.h"
81 #ifndef _APPLE_CDSA_H_
82 #include "appleCdsa.h"
89 #ifndef _CIPHER_SPECS_H_
90 #include "cipherSpecs.h"
95 static SSLErr
SSLEncodeRandom(unsigned char *p
, SSLContext
*ctx
);
97 /* IE treats null session id as valid; two consecutive sessions with NULL ID
98 * are considered a match. Workaround: when resumable sessions are disabled,
99 * send a random session ID. */
100 #define SSL_IE_NULL_RESUME_BUG 1
101 #if SSL_IE_NULL_RESUME_BUG
102 #define SSL_NULL_ID_LEN 32 /* length of bogus session ID */
106 SSLEncodeServerHello(SSLRecord
*serverHello
, SSLContext
*ctx
)
112 if (ctx
->sessionID
.data
!= 0)
113 sessionIDLen
= (UInt8
)ctx
->sessionID
.length
;
114 #if SSL_IE_NULL_RESUME_BUG
115 if(sessionIDLen
== 0) {
116 sessionIDLen
= SSL_NULL_ID_LEN
;
118 #endif /* SSL_IE_NULL_RESUME_BUG */
121 dprintf2("===SSL3 server: sending version %d_%d\n",
122 ctx
->negProtocolVersion
>> 8, ctx
->negProtocolVersion
& 0xff);
123 dprintf1("...sessionIDLen = %d\n", sessionIDLen
);
125 serverHello
->protocolVersion
= ctx
->negProtocolVersion
;
126 serverHello
->contentType
= SSL_handshake
;
127 if ((err
= SSLAllocBuffer(&serverHello
->contents
, 42 + sessionIDLen
, &ctx
->sysCtx
)) != 0)
130 progress
= serverHello
->contents
.data
;
131 *progress
++ = SSL_server_hello
;
132 progress
= SSLEncodeInt(progress
, 38 + sessionIDLen
, 3);
133 progress
= SSLEncodeInt(progress
, serverHello
->protocolVersion
, 2);
134 if ((err
= SSLEncodeRandom(progress
, ctx
)) != 0)
136 memcpy(ctx
->serverRandom
, progress
, SSL_CLIENT_SRVR_RAND_SIZE
);
137 progress
+= SSL_CLIENT_SRVR_RAND_SIZE
;
138 *(progress
++) = (UInt8
)sessionIDLen
;
139 #if SSL_IE_NULL_RESUME_BUG
140 if(ctx
->sessionID
.data
!= NULL
) {
141 /* normal path for enabled resumable session */
142 memcpy(progress
, ctx
->sessionID
.data
, sessionIDLen
);
148 rb
.length
= SSL_NULL_ID_LEN
;
152 if (sessionIDLen
> 0)
153 memcpy(progress
, ctx
->sessionID
.data
, sessionIDLen
);
154 #endif /* SSL_IE_NULL_RESUME_BUG */
155 progress
+= sessionIDLen
;
156 progress
= SSLEncodeInt(progress
, ctx
->selectedCipher
, 2);
157 *(progress
++) = 0; /* Null compression */
160 dprintf1("ssl3: server specifying cipherSuite 0x%lx\n", (UInt32
)ctx
->selectedCipher
);
163 CASSERT(progress
== serverHello
->contents
.data
+ serverHello
->contents
.length
);
169 SSLProcessServerHello(SSLBuffer message
, SSLContext
*ctx
)
171 SSLProtocolVersion protocolVersion
;
172 unsigned int sessionIDLen
;
175 CASSERT(ctx
->protocolSide
== SSL_ClientSide
);
177 if (message
.length
< 38 || message
.length
> 70) {
178 errorLog0("SSLProcessServerHello: msg len error\n");
179 return SSLProtocolErr
;
183 protocolVersion
= (SSLProtocolVersion
)SSLDecodeInt(p
, 2);
185 if (protocolVersion
> ctx
->maxProtocolVersion
) {
186 return SSLNegotiationErr
;
188 ctx
->negProtocolVersion
= protocolVersion
;
189 switch(protocolVersion
) {
190 case SSL_Version_3_0
:
191 ctx
->sslTslCalls
= &Ssl3Callouts
;
193 case TLS_Version_1_0
:
194 ctx
->sslTslCalls
= &Tls1Callouts
;
197 return SSLNegotiationErr
;
200 dprintf2("===SSL3 client: negVersion is %d_%d\n",
201 (protocolVersion
>> 8) & 0xff, protocolVersion
& 0xff);
204 memcpy(ctx
->serverRandom
, p
, 32);
208 if (message
.length
!= 38 + sessionIDLen
) {
209 errorLog0("SSLProcessServerHello: msg len error 2\n");
210 return SSLProtocolErr
;
212 if (sessionIDLen
> 0 && ctx
->peerID
.data
!= 0)
213 { /* Don't die on error; just treat it as an uncached session */
214 err
= SSLAllocBuffer(&ctx
->sessionID
, sessionIDLen
, &ctx
->sysCtx
);
216 memcpy(ctx
->sessionID
.data
, p
, sessionIDLen
);
220 ctx
->selectedCipher
= (UInt16
)SSLDecodeInt(p
,2);
222 dprintf1("===ssl3: server requests cipherKind %d\n",
223 (unsigned)ctx
->selectedCipher
);
226 if ((err
= FindCipherSpec(ctx
)) != 0) {
230 if (*p
++ != 0) /* Compression */
231 return SSLUnsupportedErr
;
233 CASSERT(p
== message
.data
+ message
.length
);
238 SSLEncodeClientHello(SSLRecord
*clientHello
, SSLContext
*ctx
)
242 SSLBuffer sessionIdentifier
;
245 CASSERT(ctx
->protocolSide
== SSL_ClientSide
);
248 if (ctx
->resumableSession
.data
!= 0)
249 { if (ERR(err
= SSLRetrieveSessionID(ctx
->resumableSession
, &sessionIdentifier
, ctx
)) != 0)
252 sessionIDLen
= sessionIdentifier
.length
;
255 length
= 39 + 2*(ctx
->numValidCipherSpecs
) + sessionIDLen
;
257 clientHello
->protocolVersion
= ctx
->maxProtocolVersion
;
258 clientHello
->contentType
= SSL_handshake
;
259 if ((err
= SSLAllocBuffer(&clientHello
->contents
, length
+ 4, &ctx
->sysCtx
)) != 0)
262 p
= clientHello
->contents
.data
;
263 *p
++ = SSL_client_hello
;
264 p
= SSLEncodeInt(p
, length
, 3);
265 p
= SSLEncodeInt(p
, ctx
->maxProtocolVersion
, 2);
267 dprintf2("===SSL3 client: proclaiming max protocol %d_%d capable ONLY\n",
268 ctx
->maxProtocolVersion
>> 8, ctx
->maxProtocolVersion
& 0xff);
270 if ((err
= SSLEncodeRandom(p
, ctx
)) != 0)
271 { SSLFreeBuffer(&clientHello
->contents
, &ctx
->sysCtx
);
274 memcpy(ctx
->clientRandom
, p
, SSL_CLIENT_SRVR_RAND_SIZE
);
276 *p
++ = sessionIDLen
; /* 1 byte vector length */
277 if (sessionIDLen
> 0)
278 { memcpy(p
, sessionIdentifier
.data
, sessionIDLen
);
279 if ((err
= SSLFreeBuffer(&sessionIdentifier
, &ctx
->sysCtx
)) != 0)
283 p
= SSLEncodeInt(p
, 2*(ctx
->numValidCipherSpecs
), 2); /* 2 byte long vector length */
284 for (i
= 0; i
<ctx
->numValidCipherSpecs
; ++i
)
285 p
= SSLEncodeInt(p
, ctx
->validCipherSpecs
[i
].cipherSpec
, 2);
286 *p
++ = 1; /* 1 byte long vector */
287 *p
++ = 0; /* null compression */
289 CASSERT(p
== clientHello
->contents
.data
+ clientHello
->contents
.length
);
291 if ((err
= SSLInitMessageHashes(ctx
)) != 0)
298 SSLProcessClientHello(SSLBuffer message
, SSLContext
*ctx
)
300 SSLProtocolVersion clientVersion
;
301 UInt16 cipherListLen
, cipherCount
, desiredSpec
, cipherSpec
;
302 UInt8 sessionIDLen
, compressionCount
;
306 if (message
.length
< 41) {
307 errorLog0("SSLProcessClientHello: msg len error 1\n");
308 return SSLProtocolErr
;
310 progress
= message
.data
;
311 clientVersion
= (SSLProtocolVersion
)SSLDecodeInt(progress
, 2);
314 /* tested, works with SSLv3 */
315 if (clientVersion
< SSL_Version_3_0
) {
317 dprintf1("===SSL3 server: clientVersion %s rejected\n", clientVersion
);
319 return SSLUnsupportedErr
;
321 ctx
->negProtocolVersion
= SSL_Version_3_0
;
323 /* Untested, for TLS */
324 if(clientVersion
> ctx
->maxProtocolVersion
) {
325 clientVersion
= ctx
->maxProtocolVersion
;
327 switch(clientVersion
) {
328 case SSL_Version_3_0
:
329 ctx
->sslTslCalls
= &Ssl3Callouts
;
331 case TLS_Version_1_0
:
332 ctx
->sslTslCalls
= &Tls1Callouts
;
335 return SSLNegotiationErr
;
337 ctx
->negProtocolVersion
= clientVersion
;
340 dprintf2("===SSL3 server: negVersion is %d_%d\n",
341 clientVersion
>> 8, clientVersion
& 0xff);
344 memcpy(ctx
->clientRandom
, progress
, SSL_CLIENT_SRVR_RAND_SIZE
);
346 sessionIDLen
= *(progress
++);
347 if (message
.length
< 41 + sessionIDLen
) {
348 errorLog0("SSLProcessClientHello: msg len error 2\n");
349 return SSLProtocolErr
;
351 if (sessionIDLen
> 0 && ctx
->peerID
.data
!= 0)
352 { /* Don't die on error; just treat it as an uncacheable session */
353 err
= SSLAllocBuffer(&ctx
->sessionID
, sessionIDLen
, &ctx
->sysCtx
);
355 memcpy(ctx
->sessionID
.data
, progress
, sessionIDLen
);
357 progress
+= sessionIDLen
;
359 cipherListLen
= (UInt16
)SSLDecodeInt(progress
, 2); /* Count of cipherSpecs, must be even & >= 2 */
361 if ((cipherListLen
& 1) || cipherListLen
< 2 || message
.length
< 39 + sessionIDLen
+ cipherListLen
) {
362 errorLog0("SSLProcessClientHello: msg len error 3\n");
363 return SSLProtocolErr
;
365 cipherCount
= cipherListLen
/2;
366 cipherSpec
= 0xFFFF; /* No match marker */
367 while (cipherSpec
== 0xFFFF && cipherCount
--)
368 { desiredSpec
= (UInt16
)SSLDecodeInt(progress
, 2);
370 for (i
= 0; i
<ctx
->numValidCipherSpecs
; i
++)
371 { if (ctx
->validCipherSpecs
[i
].cipherSpec
== desiredSpec
)
372 { cipherSpec
= desiredSpec
;
378 if (cipherSpec
== 0xFFFF)
379 return SSLNegotiationErr
;
380 progress
+= 2 * cipherCount
; /* Advance past unchecked cipherCounts */
381 ctx
->selectedCipher
= cipherSpec
;
382 if ((err
= FindCipherSpec(ctx
)) != 0) {
386 dprintf1("ssl3 server: selecting cipherKind 0x%x\n", (unsigned)ctx
->selectedCipher
);
389 compressionCount
= *(progress
++);
390 /* message.length restriction relaxed to allow too-long messages for future expansion
391 following recommendation of TLS meeting 5/29/96 */
392 if (compressionCount
< 1 || message
.length
< 38 + sessionIDLen
+ cipherListLen
+ compressionCount
) {
393 errorLog0("SSLProcessClientHello: msg len error 4\n");
394 return SSLProtocolErr
;
396 /* Ignore list; we're doing null */
398 if ((err
= SSLInitMessageHashes(ctx
)) != 0)
405 SSLEncodeRandom(unsigned char *p
, SSLContext
*ctx
)
406 { SSLBuffer randomData
;
410 if ((err
= sslTime(&time
)) != 0)
412 SSLEncodeInt(p
, time
, 4);
413 randomData
.data
= p
+4;
414 randomData
.length
= 28;
415 if((err
= sslRand(ctx
, &randomData
)) != 0)
421 SSLInitMessageHashes(SSLContext
*ctx
)
424 if ((err
= CloseHash(&SSLHashSHA1
, &ctx
->shaState
, ctx
)) != 0)
426 if ((err
= CloseHash(&SSLHashMD5
, &ctx
->md5State
, ctx
)) != 0)
428 if ((err
= ReadyHash(&SSLHashSHA1
, &ctx
->shaState
, ctx
)) != 0)
430 if ((err
= ReadyHash(&SSLHashMD5
, &ctx
->md5State
, ctx
)) != 0)