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 RSARef 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
);
98 SSLEncodeServerHello(SSLRecord
*serverHello
, SSLContext
*ctx
)
104 if (ctx
->sessionID
.data
!= 0)
105 sessionIDLen
= (UInt8
)ctx
->sessionID
.length
;
107 dprintf0("===SSL3 server: sending SSL_Version_3_0\n");
109 serverHello
->protocolVersion
= SSL_Version_3_0
;
110 serverHello
->contentType
= SSL_handshake
;
111 if ((err
= SSLAllocBuffer(&serverHello
->contents
, 42 + sessionIDLen
, &ctx
->sysCtx
)) != 0)
114 progress
= serverHello
->contents
.data
;
115 *progress
++ = SSL_server_hello
;
116 progress
= SSLEncodeInt(progress
, 38 + sessionIDLen
, 3);
117 progress
= SSLEncodeInt(progress
, SSL_Version_3_0
, 2);
118 if ((err
= SSLEncodeRandom(progress
, ctx
)) != 0)
120 memcpy(ctx
->serverRandom
, progress
, 32);
122 *(progress
++) = (UInt8
)sessionIDLen
;
123 if (sessionIDLen
> 0)
124 memcpy(progress
, ctx
->sessionID
.data
, sessionIDLen
);
125 progress
+= sessionIDLen
;
126 progress
= SSLEncodeInt(progress
, ctx
->selectedCipher
, 2);
127 *(progress
++) = 0; /* Null compression */
129 dprintf1("ssl3: server specifying cipherSuite 0x%lx\n", (UInt32
)ctx
->selectedCipher
);
131 CASSERT(progress
== serverHello
->contents
.data
+ serverHello
->contents
.length
);
137 SSLProcessServerHello(SSLBuffer message
, SSLContext
*ctx
)
139 SSLProtocolVersion protocolVersion
;
140 unsigned int sessionIDLen
;
143 CASSERT(ctx
->protocolSide
== SSL_ClientSide
);
145 if (message
.length
< 38 || message
.length
> 70) {
146 errorLog0("SSLProcessServerHello: msg len error\n");
147 return SSLProtocolErr
;
151 protocolVersion
= (SSLProtocolVersion
)SSLDecodeInt(p
, 2);
153 if (protocolVersion
!= SSL_Version_3_0
)
154 return SSLUnsupportedErr
;
155 ctx
->negProtocolVersion
= protocolVersion
;
157 dprintf0("===SSL3 client: negVersion is 3_0\n");
160 memcpy(ctx
->serverRandom
, p
, 32);
164 if (message
.length
!= 38 + sessionIDLen
) {
165 errorLog0("SSLProcessServerHello: msg len error 2\n");
166 return SSLProtocolErr
;
168 if (sessionIDLen
> 0 && ctx
->peerID
.data
!= 0)
169 { /* Don't die on error; just treat it as an uncached session */
170 err
= SSLAllocBuffer(&ctx
->sessionID
, sessionIDLen
, &ctx
->sysCtx
);
172 memcpy(ctx
->sessionID
.data
, p
, sessionIDLen
);
176 ctx
->selectedCipher
= (UInt16
)SSLDecodeInt(p
,2);
178 dprintf1("===ssl3: server requests cipherKind 0x%x\n",
179 (UInt32
)ctx
->selectedCipher
);
182 if ((err
= FindCipherSpec(ctx
)) != 0) {
186 if (*p
++ != 0) /* Compression */
187 return SSLUnsupportedErr
;
189 CASSERT(p
== message
.data
+ message
.length
);
194 SSLEncodeClientHello(SSLRecord
*clientHello
, SSLContext
*ctx
)
198 SSLBuffer sessionIdentifier
;
201 CASSERT(ctx
->protocolSide
== SSL_ClientSide
);
204 if (ctx
->resumableSession
.data
!= 0)
205 { if (ERR(err
= SSLRetrieveSessionIDIdentifier(ctx
->resumableSession
, &sessionIdentifier
, ctx
)) != 0)
208 sessionIDLen
= sessionIdentifier
.length
;
211 length
= 39 + 2*(ctx
->numValidCipherSpecs
) + sessionIDLen
;
213 clientHello
->protocolVersion
= SSL_Version_3_0
;
214 clientHello
->contentType
= SSL_handshake
;
215 if ((err
= SSLAllocBuffer(&clientHello
->contents
, length
+ 4, &ctx
->sysCtx
)) != 0)
218 p
= clientHello
->contents
.data
;
219 *p
++ = SSL_client_hello
;
220 p
= SSLEncodeInt(p
, length
, 3);
221 p
= SSLEncodeInt(p
, SSL_Version_3_0
, 2);
223 dprintf0("===SSL3 client: proclaiming Version_3_0 capable ONLY\n");
225 if ((err
= SSLEncodeRandom(p
, ctx
)) != 0)
226 { SSLFreeBuffer(&clientHello
->contents
, &ctx
->sysCtx
);
229 memcpy(ctx
->clientRandom
, p
, 32);
231 *p
++ = sessionIDLen
; /* 1 byte vector length */
232 if (sessionIDLen
> 0)
233 { memcpy(p
, sessionIdentifier
.data
, sessionIDLen
);
234 if ((err
= SSLFreeBuffer(&sessionIdentifier
, &ctx
->sysCtx
)) != 0)
238 p
= SSLEncodeInt(p
, 2*(ctx
->numValidCipherSpecs
), 2); /* 2 byte long vector length */
239 for (i
= 0; i
<ctx
->numValidCipherSpecs
; ++i
)
240 p
= SSLEncodeInt(p
, ctx
->validCipherSpecs
[i
].cipherSpec
, 2);
241 *p
++ = 1; /* 1 byte long vector */
242 *p
++ = 0; /* null compression */
244 CASSERT(p
== clientHello
->contents
.data
+ clientHello
->contents
.length
);
246 if ((err
= SSLInitMessageHashes(ctx
)) != 0)
253 SSLProcessClientHello(SSLBuffer message
, SSLContext
*ctx
)
255 SSLProtocolVersion clientVersion
;
256 UInt16 cipherListLen
, cipherCount
, desiredSpec
, cipherSpec
;
257 UInt8 sessionIDLen
, compressionCount
;
261 if (message
.length
< 41) {
262 errorLog0("SSLProcessClientHello: msg len error 1\n");
263 return SSLProtocolErr
;
265 progress
= message
.data
;
266 clientVersion
= (SSLProtocolVersion
)SSLDecodeInt(progress
, 2);
268 if (clientVersion
< SSL_Version_3_0
) {
270 dprintf1("===SSL3 server: clientVersion %s rejected\n", clientVersion
);
272 return SSLUnsupportedErr
;
274 ctx
->negProtocolVersion
= SSL_Version_3_0
;
276 dprintf0("===SSL3 server: negVersion is 3_0\n");
279 memcpy(ctx
->clientRandom
, progress
, 32);
281 sessionIDLen
= *(progress
++);
282 if (message
.length
< 41 + sessionIDLen
) {
283 errorLog0("SSLProcessClientHello: msg len error 2\n");
284 return SSLProtocolErr
;
286 if (sessionIDLen
> 0 && ctx
->peerID
.data
!= 0)
287 { /* Don't die on error; just treat it as an uncacheable session */
288 err
= SSLAllocBuffer(&ctx
->sessionID
, sessionIDLen
, &ctx
->sysCtx
);
290 memcpy(ctx
->sessionID
.data
, progress
, sessionIDLen
);
292 progress
+= sessionIDLen
;
294 cipherListLen
= (UInt16
)SSLDecodeInt(progress
, 2); /* Count of cipherSpecs, must be even & >= 2 */
296 if ((cipherListLen
& 1) || cipherListLen
< 2 || message
.length
< 39 + sessionIDLen
+ cipherListLen
) {
297 errorLog0("SSLProcessClientHello: msg len error 3\n");
298 return SSLProtocolErr
;
300 cipherCount
= cipherListLen
/2;
301 cipherSpec
= 0xFFFF; /* No match marker */
302 while (cipherSpec
== 0xFFFF && cipherCount
--)
303 { desiredSpec
= (UInt16
)SSLDecodeInt(progress
, 2);
305 for (i
= 0; i
<ctx
->numValidCipherSpecs
; i
++)
306 { if (ctx
->validCipherSpecs
[i
].cipherSpec
== desiredSpec
)
307 { cipherSpec
= desiredSpec
;
313 if (cipherSpec
== 0xFFFF)
314 return SSLNegotiationErr
;
315 progress
+= 2 * cipherCount
; /* Advance past unchecked cipherCounts */
316 ctx
->selectedCipher
= cipherSpec
;
317 if ((err
= FindCipherSpec(ctx
)) != 0) {
321 dprintf1("ssl3 server: selecting cipherKind 0x%x\n", (UInt32
)ctx
->selectedCipher
);
324 compressionCount
= *(progress
++);
325 /* message.length restriction relaxed to allow too-long messages for future expansion
326 following recommendation of TLS meeting 5/29/96 */
327 if (compressionCount
< 1 || message
.length
< 38 + sessionIDLen
+ cipherListLen
+ compressionCount
) {
328 errorLog0("SSLProcessClientHello: msg len error 4\n");
329 return SSLProtocolErr
;
331 /* Ignore list; we're doing null */
333 if ((err
= SSLInitMessageHashes(ctx
)) != 0)
340 SSLEncodeRandom(unsigned char *p
, SSLContext
*ctx
)
341 { SSLBuffer randomData
;
346 if ((err
= sslTime(&time
)) != 0)
348 if ((err
= ctx
->sysCtx
.time(&time
, ctx
->sysCtx
.timeRef
)) != 0)
351 SSLEncodeInt(p
, time
, 4);
352 randomData
.data
= p
+4;
353 randomData
.length
= 28;
355 if((err
= sslRand(ctx
, &randomData
)) != 0)
357 if ((err
= ctx
->sysCtx
.random(randomData
, ctx
->sysCtx
.randomRef
)) != 0)
364 SSLInitMessageHashes(SSLContext
*ctx
)
366 if ((err
= SSLFreeBuffer(&ctx
->shaState
, &ctx
->sysCtx
)) != 0)
368 if ((err
= SSLFreeBuffer(&ctx
->md5State
, &ctx
->sysCtx
)) != 0)
370 if ((err
= ReadyHash(&SSLHashSHA1
, &ctx
->shaState
, ctx
)) != 0)
372 if ((err
= ReadyHash(&SSLHashMD5
, &ctx
->md5State
, ctx
)) != 0)