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: interface between SSL and CDSA
24 Written by: Doug Mitchell, based on Netscape SSLRef 3.0
26 Copyright: (c) 1999 by Apple Computer, Inc., all rights reserved.
30 #ifndef _APPLE_CDSA_H_
31 #define _APPLE_CDSA_H_ 1
37 #include <Security/cssmtype.h>
44 extern void stPrintCdsaError(const char *op
, CSSM_RETURN crtn
);
45 extern char *stCssmErrToStr(CSSM_RETURN err
);
47 #define stPrintCdsaError(o, cr)
50 extern SSLErr
sslSetUpSymmKey(
53 CSSM_KEYUSE keyUse
, // CSSM_KEYUSE_ENCRYPT, etc.
54 CSSM_BOOL copyKey
, // true: copy keyData false: set by reference
56 uint32 keyDataLen
); // in bytes
58 extern SSLErr
sslFreeKey(CSSM_CSP_HANDLE cspHand
,
60 #if ST_KEYCHAIN_ENABLE && ST_KC_KEYS_NEED_REF
61 SecKeychainRef
*kcItem
);
62 #else /* !ST_KEYCHAIN_ENABLE */
64 #endif /* ST_KEYCHAIN_ENABLE*/
66 extern SSLErr
attachToCsp(SSLContext
*ctx
);
67 extern SSLErr
attachToCl(SSLContext
*ctx
);
68 extern SSLErr
attachToTp(SSLContext
*ctx
);
69 extern SSLErr
attachToAll(SSLContext
*ctx
);
70 extern SSLErr
detachFromAll(SSLContext
*ctx
);
72 extern CSSM_DATA_PTR
stMallocCssmData(uint32 size
);
73 extern void stFreeCssmData(CSSM_DATA_PTR data
, CSSM_BOOL freeStruct
);
74 extern SSLErr
stSetUpCssmData(CSSM_DATA_PTR data
, uint32 length
);
78 * Common RNG function; replaces SSLRef's SSLRandomFunc
80 extern SSLErr
sslRand(
85 * Given a DER-encoded cert, obtain its public key as a CSSM_KEY_PTR.
87 extern SSLErr
sslPubKeyFromCert(
89 const SSLBuffer
*derCert
,
90 CSSM_KEY_PTR
*pubKey
, // RETURNED
91 CSSM_CSP_HANDLE
*cspHand
); // RETURNED
94 * Verify a cert chain.
96 extern SSLErr
sslVerifyCertChain(
98 const SSLCertificate
*certChain
);
101 * Raw RSA sign/verify.
103 SSLErr
sslRsaRawSign(
105 const CSSM_KEY
*privKey
,
106 CSSM_CSP_HANDLE cspHand
,
107 const UInt8
*plainText
,
109 UInt8
*sig
, // mallocd by caller; RETURNED
110 UInt32 sigLen
, // available
111 UInt32
*actualBytes
); // RETURNED
113 SSLErr
sslRsaRawVerify(
115 const CSSM_KEY
*pubKey
,
116 CSSM_CSP_HANDLE cspHand
,
117 const UInt8
*plainText
,
120 UInt32 sigLen
); // available
125 SSLErr
sslRsaEncrypt(
127 const CSSM_KEY
*pubKey
,
128 CSSM_CSP_HANDLE cspHand
,
129 const UInt8
*plainText
,
131 UInt8
*cipherText
, // mallocd by caller; RETURNED
132 UInt32 cipherTextLen
, // available
133 UInt32
*actualBytes
); // RETURNED
134 SSLErr
sslRsaDecrypt(
136 const CSSM_KEY
*privKey
,
137 CSSM_CSP_HANDLE cspHand
,
138 const UInt8
*cipherText
,
139 UInt32 cipherTextLen
,
140 UInt8
*plainText
, // mallocd by caller; RETURNED
141 UInt32 plainTextLen
, // available
142 UInt32
*actualBytes
); // RETURNED
145 * Obtain size of key in bytes.
147 extern UInt32
sslKeyLengthInBytes(
148 const CSSM_KEY
*key
);
151 * Get raw key bits from an RSA public key.
153 SSLErr
sslGetPubKeyBits(
155 const CSSM_KEY
*pubKey
,
156 CSSM_CSP_HANDLE cspHand
,
157 SSLBuffer
*modulus
, // data mallocd and RETURNED
158 SSLBuffer
*exponent
); // data mallocd and RETURNED
161 * Given raw RSA key bits, cook up a CSSM_KEY_PTR. Used in
162 * Server-initiated key exchange.
164 SSLErr
sslGetPubKeyFromBits(
166 const SSLBuffer
*modulus
,
167 const SSLBuffer
*exponent
,
168 CSSM_KEY_PTR
*pubKey
, // mallocd and RETURNED
169 CSSM_CSP_HANDLE
*cspHand
); // RETURNED
172 * Given two certs, verify subjectCert with issuerCert. Returns
173 * CSSM_TRUE on successful verify.
174 * Only special case on error is "subject cert expired", indicated by
175 * *subjectExpired returned as CSSM_TRUE.
178 /* no longer needed */
179 CSSM_BOOL
sslVerifyCert(
181 const CSSM_DATA_PTR subjectCert
,
182 const CSSM_DATA_PTR issuerCert
,
183 CSSM_CSP_HANDLE cspHand
, // can verify with issuerCert
184 CSSM_BOOL
*subjectExpired
); // RETURNED
188 * Given a DER-encoded cert, obtain its DER-encoded subject name.
190 #if ST_KEYCHAIN_ENABLE
191 CSSM_DATA_PTR
sslGetCertSubjectName(
193 const CSSM_DATA_PTR cert
);
194 #endif ST_KEYCHAIN_ENABLE
196 #if (SSL_DEBUG && ST_KEYCHAIN_ENABLE)
197 void verifyTrustedRoots(SSLContext
*ctx
,
202 void * stAppMalloc (uint32 size
, void *allocRef
);
203 void stAppFree (void *mem_ptr
, void *allocRef
);
204 void * stAppRealloc (void *ptr
, uint32 size
, void *allocRef
);
205 void * stAppCalloc (uint32 num
, uint32 size
, void *allocRef
);
208 * Convert between SSLBuffer and CSSM_DATA, which are after all identical.
209 * No mallocs, just copy the pointer and length.
211 #define SSLBUF_TO_CSSM(sb, cd) { \
212 (cd)->Length = (sb)->length; \
213 (cd)->Data = (sb)->data; \
216 #define CSSM_TO_SSLBUF(cd, sb) { \
217 (sb)->length = (cd)->Length; \
218 (sb)->data = (cd)->Data; \
226 #endif /* _APPLE_CDSA_H_ */