]> git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_ssl/lib/sslContext.h
Security-57740.51.3.tar.gz
[apple/security.git] / OSX / libsecurity_ssl / lib / sslContext.h
1 /*
2 * Copyright (c) 1999-2001,2005-2014 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 /*
25 * sslContext.h - Private SSL typedefs: SSLContext and its components
26 */
27
28 #ifndef _SSLCONTEXT_H_
29 #define _SSLCONTEXT_H_ 1
30
31 #include "SecureTransport.h"
32 #include "sslBuildFlags.h"
33
34 #include <tls_handshake.h>
35 #include <tls_record.h>
36 #include <tls_stream_parser.h>
37 #include <tls_cache.h>
38
39
40 #if TARGET_OS_IPHONE
41 #include <Security/SecDH.h>
42 #include <Security/SecKeyInternal.h>
43 #else
44 #include "../sec/Security/SecDH.h" // hack to get SecDH.
45 // typedef struct OpaqueSecDHContext *SecDHContext;
46 #endif
47 #include <corecrypto/ccec.h>
48
49 #include <CoreFoundation/CFRuntime.h>
50 #include <AssertMacros.h>
51
52 #include "sslPriv.h"
53 #include "sslRecord.h"
54 #include "cipherSpecs.h"
55
56 #include <dispatch/dispatch.h>
57
58 #ifdef __cplusplus
59 extern "C" {
60 #endif
61
62 typedef struct
63 { SSLReadFunc read;
64 SSLWriteFunc write;
65 SSLConnectionRef ioRef;
66 } IOContext;
67
68 //FIXME should not need this.
69 typedef enum
70 {
71 SSL_HdskStateUninit = 0, /* No Handshake yet */
72 SSL_HdskStatePending, /* Handshake in Progress */
73 SSL_HdskStateReady, /* Handshake is done */
74 SSL_HdskStateGracefulClose,
75 SSL_HdskStateErrorClose,
76 SSL_HdskStateNoNotifyClose, /* server disconnected with no
77 * notify msg */
78 } SSLHandshakeState;
79
80 #define SSLChangeHdskState(ctx, newState) { ctx->state=newState; }
81
82 struct SSLContext
83 {
84 CFRuntimeBase _base;
85 IOContext ioCtx;
86
87 const struct SSLRecordFuncs *recFuncs;
88 SSLRecordContextRef recCtx;
89
90 tls_handshake_t hdsk;
91 tls_cache_t cache;
92 int readCipher_ready;
93 int writeCipher_ready;
94
95 SSLHandshakeState state;
96
97 /*
98 * Prior to successful protocol negotiation, negProtocolVersion
99 * is SSL_Version_Undetermined. Subsequent to successful
100 * negotiation, negProtocolVersion contains the actual over-the-wire
101 * protocol value.
102 *
103 * The Boolean versionEnable flags are set by
104 * SSLSetProtocolVersionEnabled or SSLSetProtocolVersion and
105 * remain invariant once negotiation has started. If there
106 * were a large number of these and/or we were adding new
107 * protocol versions on a regular basis, we'd probably want
108 * to implement these as a word of flags. For now, in the
109 * real world, this is the most straightforward implementation.
110 */
111 tls_protocol_version negProtocolVersion; /* negotiated */
112 tls_protocol_version clientReqProtocol; /* requested by client in hello msg */
113 tls_protocol_version minProtocolVersion;
114 tls_protocol_version maxProtocolVersion;
115 Boolean isDTLS; /* if this is a Datagram Context */
116 SSLProtocolSide protocolSide; /* ConnectionEnd enum { server, client } in rfc5246. */
117
118 SSLBuffer dtlsCookie; /* DTLS ClientHello cookie */
119
120
121 uint16_t selectedCipher; /* currently selected */
122
123 /* Server DH Parameters */
124 SSLBuffer dhParamsEncoded; /* PKCS3 encoded blob - prime + generator */
125
126 /*
127 * The arrays we are given via SSLSetCertificate() and SSLSetEncryptionCertificate().
128 * We keep them here, refcounted, solely for the associated getter.
129 */
130 CFArrayRef localCertArray;
131 CFArrayRef encryptCertArray;
132
133 /* peer certs as SecTrustRef */
134 SecTrustRef peerSecTrust;
135
136 CFMutableArrayRef trustedCerts;
137 Boolean trustedCertsOnly;
138
139 #if !TARGET_OS_IPHONE
140 /*
141 * trusted leaf certs as specified in SSLSetTrustedLeafCertificates()
142 */
143 CFArrayRef trustedLeafCerts;
144 #endif
145
146 Boolean allowExpiredCerts;
147 Boolean allowExpiredRoots;
148 Boolean enableCertVerify;
149
150 SSLBuffer sessionID;
151 SSLBuffer peerID;
152 SSLBuffer resumableSession; /* We keep a copy for now - but eventually this should go away if we get refcounted SSLBuffers */
153
154 uint16_t *ecdhCurves;
155 unsigned ecdhNumCurves;
156
157 /* server-side only */
158 SSLAuthenticate clientAuth; /* kNeverAuthenticate, etc. */
159
160 /* client and server */
161 SSLClientCertificateState clientCertState;
162
163 DNListElem *acceptableDNList; /* client and server */
164 CFMutableArrayRef acceptableCAs; /* server only - SecCertificateRefs */
165
166 bool certRequested;
167 bool certSent;
168 bool certReceived;
169 bool x509Requested;
170
171 unsigned sessionMatch;
172
173
174 /* Transport layer fields */
175 SSLBuffer receivedDataBuffer;
176 size_t receivedDataPos;
177
178 Boolean allowAnyRoot; // don't require known roots
179 Boolean sentFatalAlert; // this session terminated by fatal alert
180 Boolean rsaBlindingEnable;
181 Boolean oneByteRecordEnable; /* enable 1/n-1 data splitting for TLSv1 and SSLv3 */
182
183 /* optional session cache timeout (in seconds) override - 0 means default */
184 uint32_t sessionCacheTimeout;
185
186 /* optional SessionTicket */
187 SSLBuffer sessionTicket;
188
189 /* optional callback to obtain master secret, with its opaque arg */
190 SSLInternalMasterSecretFunction masterSecretCallback;
191 const void *masterSecretArg;
192
193 #if SSL_PAC_SERVER_ENABLE
194 /* server PAC resume sets serverRandom early to allow for secret acquisition */
195 uint8_t serverRandomValid;
196 #endif
197
198 Boolean anonCipherEnable;
199
200 /* optional switches to enable additional returns from SSLHandshake */
201 Boolean breakOnServerAuth;
202 Boolean breakOnCertRequest;
203 Boolean breakOnClientAuth;
204 Boolean signalServerAuth;
205 Boolean signalCertRequest;
206 Boolean signalClientAuth;
207 Boolean breakOnClientHello;
208
209 /* List of peer-specified supported_signature_algorithms */
210 unsigned numPeerSigAlgs;
211 const tls_signature_and_hash_algorithm *peerSigAlgs;
212
213 /* List of server-specified client auth types */
214 unsigned numAuthTypes;
215 const tls_client_auth_type *clientAuthTypes;
216
217 /* Timeout for DTLS retransmit */
218 CFAbsoluteTime timeout_deadline;
219 CFAbsoluteTime timeout_duration;
220 size_t mtu;
221
222 /* RFC 5746: Secure renegotiation */
223 Boolean secure_renegotiation;
224 Boolean secure_renegotiation_received;
225 SSLBuffer ownVerifyData;
226 SSLBuffer peerVerifyData;
227
228 /* RFC 4279: TLS PSK */
229 SSLBuffer pskSharedSecret;
230 SSLBuffer pskIdentity;
231
232 /* TLS False Start */
233 Boolean falseStartEnabled; //FalseStart enabled (by API call)
234 /* Fallback behavior */
235 Boolean fallbackEnabled; // Fallback behavior enabled.
236 /* NPN */
237 SSLNPNFunc npnFunc;
238 void *npnFuncInfo;
239
240 /* ALPN */
241 SSLALPNFunc alpnFunc;
242 void *alpnFuncInfo;
243
244 /* Enable DHE or not */
245 bool dheEnabled;
246
247 /* For early failure reporting */
248 bool serverHelloReceived;
249 };
250
251 OSStatus SSLUpdateNegotiatedClientAuthType(SSLContextRef ctx);
252
253 Boolean sslIsSessionActive(const SSLContext *ctx);
254
255 static inline bool sslVersionIsLikeTls12(SSLContext *ctx)
256 {
257 check(ctx->negProtocolVersion!=SSL_Version_Undetermined);
258 return ctx->isDTLS ? ctx->negProtocolVersion > DTLS_Version_1_0 : ctx->negProtocolVersion >= TLS_Version_1_2;
259 }
260
261 /* This is implemented in tls_callbacks.c */
262 int sslGetSessionID(SSLContext *myCtx, SSLBuffer *sessionID);
263
264 #ifdef __cplusplus
265 }
266 #endif
267
268 #endif /* _SSLCONTEXT_H_ */