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.
20 * opensslUtils.h - Support for ssleay-derived crypto modules
23 #include <openssl/rand.h>
24 #include <openssl/crypto.h>
25 #include <openssl/err.h>
26 #include <openssl/sha.h>
27 #include <openssl/rsa.h>
28 #include <openssl/dsa.h>
29 #include <openssl/dh.h>
30 #include <openssl/err.h>
31 #include <Security/debugging.h>
32 #include <Security/cssmerr.h>
33 #include "opensslUtils.h"
34 #include <AppleCSP/YarrowConnection.h>
35 #include <AppleCSP/AppleCSPUtils.h>
36 #include <Security/logging.h>
38 #define sslUtilsDebug(args...) debug("sslUtils", ## args)
40 openSslException::openSslException(
47 ERR_error_string(irtn
, buf
);
48 sslUtilsDebug("%s: %s\n", op
, buf
);
52 /* these are replacements for the ones in ssleay */
53 #define DUMP_RAND_BYTES 0
55 static int randDex
= 1;
57 int RAND_bytes(unsigned char *buf
,int num
)
60 cspGetRandomBytes(buf
, (unsigned)num
);
63 /* that can only mean Yarrow failure, which we really need to
64 * cut some slack for */
65 Security::Syslog::error("Apple CSP: yarrow failure");
66 for(int i
=0; i
<num
; i
++) {
67 buf
[i
] = (i
*3) + randDex
++;
73 int RAND_pseudo_bytes(unsigned char *buf
,int num
)
75 return RAND_bytes(buf
, num
);
78 void RAND_add(const void *buf
,int num
,double entropy
)
81 cspAddEntropy(buf
, (unsigned)num
);
87 /* replacement for mem_dbg.c */
88 int CRYPTO_mem_ctrl(int mode
)
94 * Log error info. Returns the error code we pop off the error queue.
96 unsigned long logSslErrInfo(const char *op
)
98 unsigned long e
= ERR_get_error();
100 ERR_error_string(e
, outbuf
);
102 Security::Syslog::error("Apple CSP %s: %s", op
, outbuf
);
105 Security::Syslog::error("Apple CSP %s", outbuf
);
111 * Replacement for same function in openssl's sha.c, which we don't link against.
112 * The only place this is used is in DSA_generate_parameters().
114 unsigned char *SHA1(const unsigned char *d
, unsigned long n
,unsigned char *md
)
117 sslUtilsDebug("SHA1 with NULL md");
118 CssmError::throwMe(CSSMERR_CSP_INTERNAL_ERROR
);
120 cspGenSha1Hash(d
, n
, md
);
127 unsigned long e
= logSslErrInfo(op
);
128 CSSM_RETURN cerr
= CSSM_OK
;
130 /* try to parse into something meaningful */
131 int reason
= ERR_GET_REASON(e
);
132 int lib
= ERR_GET_LIB(e
);
134 /* first try the global ones */
136 case ERR_R_MALLOC_FAILURE
:
137 cerr
= CSSMERR_CSP_MEMORY_ERROR
; break;
138 case ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
:
139 /* internal */ break;
140 case ERR_R_PASSED_NULL_PARAMETER
:
141 cerr
= CSSMERR_CSP_INVALID_POINTER
; break;
142 case ERR_R_NESTED_ASN1_ERROR
:
143 case ERR_R_BAD_ASN1_OBJECT_HEADER
:
144 case ERR_R_BAD_GET_ASN1_OBJECT_CALL
:
145 case ERR_R_EXPECTING_AN_ASN1_SEQUENCE
:
146 case ERR_R_ASN1_LENGTH_MISMATCH
:
147 case ERR_R_MISSING_ASN1_EOS
:
148 /* ASN - shouldn't happen, right? */
149 cerr
= CSSMERR_CSP_INTERNAL_ERROR
; break;
153 if(cerr
!= CSSM_OK
) {
154 CssmError::throwMe(cerr
);
157 /* now the lib-specific ones */
160 /* all indicate serious internal error...right? */
161 cerr
= CSSMERR_CSP_INTERNAL_ERROR
; break;
164 case RSA_R_ALGORITHM_MISMATCH
:
165 cerr
= CSSMERR_CSP_ALGID_MISMATCH
; break;
166 case RSA_R_BAD_SIGNATURE
:
167 cerr
= CSSMERR_CSP_VERIFY_FAILED
; break;
168 case RSA_R_DATA_TOO_LARGE
:
169 case RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE
:
170 case RSA_R_DATA_TOO_SMALL
:
171 case RSA_R_DATA_TOO_SMALL_FOR_KEY_SIZE
:
172 case RSA_R_DIGEST_TOO_BIG_FOR_RSA_KEY
:
173 cerr
= CSSMERR_CSP_INPUT_LENGTH_ERROR
; break;
174 case RSA_R_KEY_SIZE_TOO_SMALL
:
175 cerr
= CSSMERR_CSP_INVALID_ATTR_KEY_LENGTH
; break;
176 case RSA_R_PADDING_CHECK_FAILED
:
177 cerr
= CSSMERR_CSP_INVALID_DATA
; break;
178 case RSA_R_RSA_OPERATIONS_NOT_SUPPORTED
:
179 cerr
= CSSMERR_CSP_FUNCTION_NOT_IMPLEMENTED
; break;
180 case RSA_R_UNKNOWN_ALGORITHM_TYPE
:
181 cerr
= CSSMERR_CSP_INVALID_ALGORITHM
; break;
182 case RSA_R_WRONG_SIGNATURE_LENGTH
:
183 cerr
= CSSMERR_CSP_VERIFY_FAILED
; break;
185 cerr
= CSSMERR_CSP_INTERNAL_ERROR
; break;
190 case DSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE
:
191 cerr
= CSSMERR_CSP_INPUT_LENGTH_ERROR
; break;
193 cerr
= CSSMERR_CSP_INTERNAL_ERROR
; break;
197 /* actually none of the DH errors make sense at the CDSA level */
198 cerr
= CSSMERR_CSP_INTERNAL_ERROR
;
201 cerr
= CSSMERR_CSP_INTERNAL_ERROR
; break;
203 CssmError::throwMe(cerr
);
207 * given an openssl-style error, throw appropriate CssmError.
209 void throwOpensslErr(int irtn
)
212 CssmError::throwMe(CSSMERR_CSP_INTERNAL_ERROR
);