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
sslFreeKey(CSSM_CSP_HANDLE cspHand
,
52 #if ST_KEYCHAIN_ENABLE
54 #else /* !ST_KEYCHAIN_ENABLE */
55 /* fixme - will we need kcItem as a CL field ptr? */
57 #endif /* ST_KEYCHAIN_ENABLE*/
59 extern SSLErr
attachToCsp(SSLContext
*ctx
);
60 extern SSLErr
attachToCl(SSLContext
*ctx
);
61 extern SSLErr
attachToTp(SSLContext
*ctx
);
62 extern SSLErr
attachToAll(SSLContext
*ctx
);
63 extern SSLErr
detachFromAll(SSLContext
*ctx
);
65 extern CSSM_DATA_PTR
stMallocCssmData(uint32 size
);
66 extern void stFreeCssmData(CSSM_DATA_PTR data
, CSSM_BOOL freeStruct
);
67 extern SSLErr
stSetUpCssmData(CSSM_DATA_PTR data
, uint32 length
);
71 * Common RNG function; replaces SSLRef's SSLRandomFunc
73 extern SSLErr
sslRand(
78 * Given a DER-encoded cert, obtain its public key as a CSSM_KEY_PTR.
80 extern SSLErr
sslPubKeyFromCert(
82 const SSLBuffer
*derCert
,
83 CSSM_KEY_PTR
*pubKey
, // RETURNED
84 CSSM_CSP_HANDLE
*cspHand
); // RETURNED
87 * Verify a cert chain.
89 extern SSLErr
sslVerifyCertChain(
91 const SSLCertificate
*certChain
);
94 * Raw RSA sign/verify.
98 const CSSM_KEY_PTR privKey
,
99 CSSM_CSP_HANDLE cspHand
,
100 const UInt8
*plainText
,
102 UInt8
*sig
, // mallocd by caller; RETURNED
103 UInt32 sigLen
, // available
104 UInt32
*actualBytes
); // RETURNED
106 SSLErr
sslRsaRawVerify(
108 const CSSM_KEY_PTR pubKey
,
109 CSSM_CSP_HANDLE cspHand
,
110 const UInt8
*plainText
,
113 UInt32 sigLen
); // available
118 SSLErr
sslRsaEncrypt(
120 const CSSM_KEY_PTR pubKey
,
121 CSSM_CSP_HANDLE cspHand
,
122 const UInt8
*plainText
,
124 UInt8
*cipherText
, // mallocd by caller; RETURNED
125 UInt32 cipherTextLen
, // available
126 UInt32
*actualBytes
); // RETURNED
127 SSLErr
sslRsaDecrypt(
129 const CSSM_KEY_PTR privKey
,
130 CSSM_CSP_HANDLE cspHand
,
131 const UInt8
*cipherText
,
132 UInt32 cipherTextLen
,
133 UInt8
*plainText
, // mallocd by caller; RETURNED
134 UInt32 plainTextLen
, // available
135 UInt32
*actualBytes
); // RETURNED
138 * Obtain size of key in bytes.
140 extern UInt32
sslKeyLengthInBytes(
141 const CSSM_KEY_PTR key
);
144 * Get raw key bits from an RSA public key.
146 SSLErr
sslGetPubKeyBits(
148 const CSSM_KEY_PTR pubKey
,
149 CSSM_CSP_HANDLE cspHand
,
150 SSLBuffer
*modulus
, // data mallocd and RETURNED
151 SSLBuffer
*exponent
); // data mallocd and RETURNED
154 * Given raw RSA key bits, cook up a CSSM_KEY_PTR. Used in
155 * Server-initiated key exchange.
157 SSLErr
sslGetPubKeyFromBits(
159 const SSLBuffer
*modulus
,
160 const SSLBuffer
*exponent
,
161 CSSM_KEY_PTR
*pubKey
, // mallocd and RETURNED
162 CSSM_CSP_HANDLE
*cspHand
); // RETURNED
165 * Given two certs, verify subjectCert with issuerCert. Returns
166 * CSSM_TRUE on successful verify.
167 * Only special case on error is "subject cert expired", indicated by
168 * *subjectExpired returned as CSSM_TRUE.
171 /* no longer needed */
172 CSSM_BOOL
sslVerifyCert(
174 const CSSM_DATA_PTR subjectCert
,
175 const CSSM_DATA_PTR issuerCert
,
176 CSSM_CSP_HANDLE cspHand
, // can verify with issuerCert
177 CSSM_BOOL
*subjectExpired
); // RETURNED
181 * Given a DER-encoded cert, obtain its DER-encoded subject name.
183 #if ST_KEYCHAIN_ENABLE
184 CSSM_DATA_PTR
sslGetCertSubjectName(
186 const CSSM_DATA_PTR cert
);
187 #endif ST_KEYCHAIN_ENABLE
189 #if (SSL_DEBUG && ST_KEYCHAIN_ENABLE)
190 void verifyTrustedRoots(SSLContext
*ctx
,
195 void * stAppMalloc (uint32 size
, void *allocRef
);
196 void stAppFree (void *mem_ptr
, void *allocRef
);
197 void * stAppRealloc (void *ptr
, uint32 size
, void *allocRef
);
198 void * stAppCalloc (uint32 num
, uint32 size
, void *allocRef
);
201 * Convert between SSLBuffer and CSSM_DATA, which are after all identical.
202 * No mallocs, just copy the pointer and length.
204 #define SSLBUF_TO_CSSM(sb, cd) { \
205 (cd)->Length = (sb)->length; \
206 (cd)->Data = (sb)->data; \
209 #define CSSM_TO_SSLBUF(cd, sb) { \
210 (sb)->length = (cd)->Length; \
211 (sb)->data = (cd)->Data; \
219 #endif /* _APPLE_CDSA_H_ */