]> git.saurik.com Git - apple/security.git/blob - SecureTransport/hdskchgc.c
Security-29.tar.gz
[apple/security.git] / SecureTransport / hdskchgc.c
1 /*
2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
3 *
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
8 * using this file.
9 *
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.
16 */
17
18
19 /*
20 File: hdskchgc.c
21
22 Contains: support for change cipher spec messages
23
24 Written by: Doug Mitchell, based on Netscape SSLRef 3.0
25
26 Copyright: (c) 1999 by Apple Computer, Inc., all rights reserved.
27
28 */
29 /* *********************************************************************
30 File: hdskchgc.c
31
32 SSLRef 3.0 Final -- 11/19/96
33
34 Copyright (c)1996 by Netscape Communications Corp.
35
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.
39
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/>.
43
44 *********************************************************************
45
46 File: hdskchgc.c Contains support for change cipher spec messages
47
48 Simple support for encoding and decoding change cipher spec messages;
49 the decode message also installs the pending read cipher (if it is
50 ready).
51
52 ****************************************************************** */
53
54 #ifndef _SSLCTX_H_
55 #include "sslctx.h"
56 #endif
57
58 #ifndef _SSLHDSHK_H_
59 #include "sslhdshk.h"
60 #endif
61
62 #ifndef _SSLALLOC_H_
63 #include "sslalloc.h"
64 #endif
65
66 #ifndef _SSLALERT_H_
67 #include "sslalert.h"
68 #endif
69
70 #ifndef _SSL_DEBUG_H_
71 #include "sslDebug.h"
72 #endif
73
74 #include <string.h>
75
76 SSLErr
77 SSLEncodeChangeCipherSpec(SSLRecord *rec, SSLContext *ctx)
78 { SSLErr err;
79
80 CASSERT(ctx->writePending.ready);
81
82 #if LOG_NEGOTIATE
83 dprintf0("===Sending changeCipherSpec msg\n");
84 #endif
85 rec->contentType = SSL_change_cipher_spec;
86 rec->protocolVersion = SSL_Version_3_0;
87 rec->contents.length = 1;
88 if ((err = SSLAllocBuffer(&rec->contents, 1, &ctx->sysCtx)) != 0)
89 return err;
90 rec->contents.data[0] = 1;
91
92 return SSLNoErr;
93 }
94
95 SSLErr
96 SSLProcessChangeCipherSpec(SSLRecord rec, SSLContext *ctx)
97 { SSLErr err;
98
99 if (rec.contents.length != 1 || rec.contents.data[0] != 1)
100 { SSLFatalSessionAlert(alert_unexpected_message, ctx);
101 errorLog2("***bad changeCipherSpec msg: length %d data 0x%x\n",
102 (unsigned)rec.contents.length, (unsigned)rec.contents.data[0]);
103 return SSLProtocolErr;
104 }
105
106 if (!ctx->readPending.ready || ctx->state != HandshakeChangeCipherSpec)
107 { SSLFatalSessionAlert(alert_unexpected_message, ctx);
108 errorLog2("***bad changeCipherSpec msg: readPending.ready %d state %d\n",
109 (unsigned)ctx->readPending.ready, (unsigned)ctx->state);
110 return SSLProtocolErr;
111 }
112
113 #if LOG_NEGOTIATE
114 dprintf0("===Processing changeCipherSpec msg\n");
115 #endif
116
117 /* Install new cipher spec on read side */
118 if ((err = SSLDisposeCipherSuite(&ctx->readCipher, ctx)) != 0)
119 { SSLFatalSessionAlert(alert_close_notify, ctx);
120 return err;
121 }
122 ctx->readCipher = ctx->readPending;
123 ctx->readCipher.ready = 0; /* Can't send data until Finished is sent */
124 SSLChangeHdskState(ctx, HandshakeFinished);
125 memset(&ctx->readPending, 0, sizeof(CipherContext)); /* Zero out old data */
126 return SSLNoErr;
127 }
128
129 SSLErr
130 SSLDisposeCipherSuite(CipherContext *cipher, SSLContext *ctx)
131 { SSLErr err;
132
133 if (cipher->symKey)
134 { if ((err = cipher->symCipher->finish(cipher, ctx)) != 0)
135 return err;
136 cipher->symKey = 0;
137 }
138
139 return SSLNoErr;
140 }