]> git.saurik.com Git - apple/security.git/blob - SecureTransport/appleGlue.c
Security-30.1.tar.gz
[apple/security.git] / SecureTransport / appleGlue.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: appleGlue.c
21
22 Contains: Glue layer between Apple SecureTransport and
23 original SSLRef code.
24
25 Written by: Doug Mitchell, based on Netscape RSARef 3.0
26
27 Copyright: (c) 1999 by Apple Computer, Inc., all rights reserved.
28
29 */
30
31 #ifndef _SSL_H_
32 #include "ssl.h"
33 #endif
34
35 #ifndef _SSLCTX_H_
36 #include "sslctx.h"
37 #endif
38
39 #ifndef _SSLALLOC_H_
40 #include "sslalloc.h"
41 #endif
42
43 #ifndef _APPLE_GLUE_H_
44 #include "appleGlue.h"
45 #endif
46
47 #ifndef _SSL_DEBUG_H_
48 #include "sslDebug.h"
49 #endif
50
51 #include <CoreServices/../Frameworks/CarbonCore.framework/Headers/MacErrors.h>
52 #include <time.h>
53 #include <string.h>
54
55 /*
56 * Cruft used to map between private SSLErr's and the SSL-specific
57 * OSStatus values in SecureTransport.h. Eventually we should do
58 * away with SSLErr....
59 */
60 typedef struct {
61 SSLErr serr;
62 OSStatus oerr;
63 } _sslErrMap;
64
65 static const _sslErrMap sslErrMap[] = {
66 { SSLNoErr, noErr },
67 { SSLMemoryErr, memFullErr },
68 { SSLUnsupportedErr, unimpErr },
69 { SSLProtocolErr, errSSLProtocol },
70 { SSLNegotiationErr, errSSLNegotiation },
71 { SSLFatalAlert, errSSLFatalAlert },
72 { SSLWouldBlockErr, errSSLWouldBlock },
73 { SSLIOErr, ioErr },
74 { SSLSessionNotFoundErr, errSSLSessionNotFound },
75 { SSLConnectionClosedGraceful, errSSLClosedGraceful },
76 { SSLConnectionClosedError, errSSLClosedAbort },
77 { X509CertChainInvalidErr, errSSLXCertChainInvalid },
78 { SSLBadCert, errSSLBadCert },
79 { SSLCryptoError, errSSLCrypto },
80 { SSLInternalError, errSSLInternal },
81 { SSLDataOverflow, errSSLCrypto },
82 { SSLAttachFailure, errSSLModuleAttach },
83 { SSLUnknownRootCert, errSSLUnknownRootCert },
84 { SSLNoRootCert, errSSLNoRootCert },
85 { SSLCertExpired, errSSLCertExpired },
86 { SSLCertNotYetValid, errSSLCertNotYetValid },
87 { SSLBadStateErr, badReqErr },
88 { SSLConnectionClosedNoNotify, errSSLClosedNoNotify },
89 };
90
91 #define SIZEOF_ERR_MAP (sizeof(sslErrMap) / sizeof(_sslErrMap))
92
93 /*
94 * Functions to allow old code to use SSLBuffer-based I/O calls.
95 * We redirect the calls here to an SSL{Write,Read}Func.
96 * This is of course way inefficient due to an extra copy for
97 * each I/O, but let's do it this way until the port settles down.
98 */
99 SSLErr sslIoRead(
100 SSLBuffer buf,
101 UInt32 *actualLength,
102 SSLContext *ctx)
103 {
104 UInt32 dataLength = buf.length;
105 OSStatus ortn;
106
107 *actualLength = 0;
108 ortn = (ctx->ioCtx.read)(ctx->ioCtx.ioRef,
109 buf.data,
110 &dataLength);
111 *actualLength = dataLength;
112 return sslErrFromOsStatus(ortn);
113 }
114
115 SSLErr sslIoWrite(
116 SSLBuffer buf,
117 UInt32 *actualLength,
118 SSLContext *ctx)
119 {
120 UInt32 dataLength = buf.length;
121 OSStatus ortn;
122
123 *actualLength = 0;
124 ortn = (ctx->ioCtx.write)(ctx->ioCtx.ioRef,
125 buf.data,
126 &dataLength);
127 *actualLength = dataLength;
128 return sslErrFromOsStatus(ortn);
129 }
130
131 /*
132 * Convert between SSLErr and OSStatus.
133 * These will go away eventually.
134 */
135 SSLErr sslErrFromOsStatus(OSStatus o)
136 {
137 int i;
138 const _sslErrMap *emap = sslErrMap;
139
140 for(i=0; i<SIZEOF_ERR_MAP; i++) {
141 if(emap->oerr == o) {
142 return emap->serr;
143 }
144 emap++;
145 }
146 return SSLIOErr; /* normal: bad error */
147 }
148
149 OSStatus sslErrToOsStatus(SSLErr s)
150 {
151 int i;
152 const _sslErrMap *emap = sslErrMap;
153
154 for(i=0; i<SIZEOF_ERR_MAP; i++) {
155 if(emap->serr == s) {
156 return emap->oerr;
157 }
158 emap++;
159 }
160 CASSERT(0); /* Debug: panic */
161 return paramErr; /* normal: bad error */
162 }
163
164 /*
165 * Time functions - replaces SSLRef's SSLTimeFunc, SSLConvertTimeFunc
166 * Weird - this is just used to generate a random number in
167 * SSLEncodeRandom
168 */
169 SSLErr sslTime(UInt32 *tim)
170 {
171 time((time_t *)&tim);
172 return SSLNoErr;
173 }
174
175 #ifdef notdef
176 /* not used.... */
177 SSLErr sslConvertTime(UInt32 *time)
178 {
179 return SSLUnsupportedErr;
180 }
181 #endif