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: Finished and server hello done 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: hdskfini.c Finished and server hello done messages
48 Support for encoding and decoding finished and server hello done
49 messgages. Also includes the necessary calculations for the Finished
50 message; note that the same function is used to calculate certificate
51 verify message hashes (without the 'SRVR' or 'CLNT' protocol side
54 ****************************************************************** */
84 SSLEncodeFinishedMessage(SSLRecord
*finished
, SSLContext
*ctx
)
86 SSLBuffer finishedMsg
, shaMsgState
, md5MsgState
;
88 unsigned finishedSize
;
93 /* size and version depend on negotiatedProtocol */
94 switch(ctx
->negProtocolVersion
) {
96 finished
->protocolVersion
= SSL_Version_3_0
;
100 finished
->protocolVersion
= TLS_Version_1_0
;
105 return SSLInternalError
;
107 finished
->contentType
= SSL_handshake
;
108 /* msg = type + 3 bytes len + finishedSize */
109 if ((err
= SSLAllocBuffer(&finished
->contents
, finishedSize
+ 4,
113 finished
->contents
.data
[0] = SSL_finished
;
114 SSLEncodeInt(finished
->contents
.data
+ 1, finishedSize
, 3);
116 finishedMsg
.data
= finished
->contents
.data
+ 4;
117 finishedMsg
.length
= finishedSize
;
119 if ((err
= CloneHashState(&SSLHashSHA1
, ctx
->shaState
, &shaMsgState
, ctx
)) != 0)
121 if ((err
= CloneHashState(&SSLHashMD5
, ctx
->md5State
, &md5MsgState
, ctx
)) != 0)
123 isServerMsg
= (ctx
->protocolSide
== SSL_ServerSide
) ? true : false;
124 if ((err
= ctx
->sslTslCalls
->computeFinishedMac(ctx
, finishedMsg
,
125 shaMsgState
, md5MsgState
, isServerMsg
)) != 0)
129 SSLFreeBuffer(&shaMsgState
, &ctx
->sysCtx
);
130 SSLFreeBuffer(&md5MsgState
, &ctx
->sysCtx
);
135 SSLProcessFinished(SSLBuffer message
, SSLContext
*ctx
)
137 SSLBuffer expectedFinished
, shaMsgState
, md5MsgState
;
139 unsigned finishedSize
;
141 switch(ctx
->negProtocolVersion
) {
142 case SSL_Version_3_0
:
145 case TLS_Version_1_0
:
150 return SSLInternalError
;
152 if (message
.length
!= finishedSize
) {
153 errorLog0("SSLProcessFinished: msg len error 1\n");
154 return SSLProtocolErr
;
156 expectedFinished
.data
= 0;
157 if ((err
= SSLAllocBuffer(&expectedFinished
, finishedSize
, &ctx
->sysCtx
)) != 0)
159 shaMsgState
.data
= 0;
160 if ((err
= CloneHashState(&SSLHashSHA1
, ctx
->shaState
, &shaMsgState
, ctx
)) != 0)
162 md5MsgState
.data
= 0;
163 if ((err
= CloneHashState(&SSLHashMD5
, ctx
->md5State
, &md5MsgState
, ctx
)) != 0)
165 isServerMsg
= (ctx
->protocolSide
== SSL_ServerSide
) ? false : true;
166 if ((err
= ctx
->sslTslCalls
->computeFinishedMac(ctx
, expectedFinished
,
167 shaMsgState
, md5MsgState
, isServerMsg
)) != 0)
170 if (memcmp(expectedFinished
.data
, message
.data
, finishedSize
) != 0)
172 errorLog0("SSLProcessFinished: memcmp failure\n");
173 err
= SSLProtocolErr
;
178 SSLFreeBuffer(&expectedFinished
, &ctx
->sysCtx
);
179 SSLFreeBuffer(&shaMsgState
, &ctx
->sysCtx
);
180 SSLFreeBuffer(&md5MsgState
, &ctx
->sysCtx
);
185 SSLEncodeServerHelloDone(SSLRecord
*helloDone
, SSLContext
*ctx
)
188 helloDone
->contentType
= SSL_handshake
;
189 assert((ctx
->negProtocolVersion
== SSL_Version_3_0
) ||
190 (ctx
->negProtocolVersion
== TLS_Version_1_0
));
191 helloDone
->protocolVersion
= ctx
->negProtocolVersion
;
192 if ((err
= SSLAllocBuffer(&helloDone
->contents
, 4, &ctx
->sysCtx
)) != 0)
194 helloDone
->contents
.data
[0] = SSL_server_hello_done
;
195 SSLEncodeInt(helloDone
->contents
.data
+1, 0, 3); /* Message has 0 length */
200 SSLProcessServerHelloDone(SSLBuffer message
, SSLContext
*ctx
)
201 { CASSERT(ctx
->protocolSide
== SSL_ClientSide
);
202 if (message
.length
!= 0) {
203 errorLog0("SSLProcessServerHelloDone: nonzero msg len\n");
204 return SSLProtocolErr
;