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 <Security/debugging.h>
28 #include <Security/cssmerr.h>
29 #include "opensslUtils.h"
30 #include <AppleCSP/YarrowConnection.h>
31 #include <AppleCSP/AppleCSPUtils.h>
32 #include <Security/logging.h>
34 #define sslUtilsDebug(args...) debug("sslUtils", ## args)
36 openSslException::openSslException(
43 ERR_error_string(irtn
, buf
);
44 sslUtilsDebug("%s: %s\n", op
, buf
);
48 /* these are replacements for the ones in ssleay */
49 #define DUMP_RAND_BYTES 0
51 static int randDex
= 1;
53 int RAND_bytes(unsigned char *buf
,int num
)
56 cspGetRandomBytes(buf
, (unsigned)num
);
59 /* that can only mean Yarrow failure, which we really need to
60 * cut some slack for */
61 Security::Syslog::error("Apple CSP: yarrow failure");
62 for(int i
=0; i
<num
; i
++) {
63 buf
[i
] = (i
*3) + randDex
++;
69 int RAND_pseudo_bytes(unsigned char *buf
,int num
)
71 return RAND_bytes(buf
, num
);
74 void RAND_add(const void *buf
,int num
,double entropy
)
77 cspAddEntropy(buf
, (unsigned)num
);
83 /* replacement for mem_dbg.c */
84 int CRYPTO_mem_ctrl(int mode
)
90 * Log error info. Returns the error code we pop off the error queue.
92 unsigned long logSslErrInfo(const char *op
)
94 unsigned long e
= ERR_get_error();
96 ERR_error_string(e
, outbuf
);
98 Security::Syslog::error("Apple CSP %s: %s", op
, outbuf
);
101 Security::Syslog::error("Apple CSP %s", outbuf
);
107 * Replacement for same function in openssl's sha.c, which we don't link against.
108 * The only place this is used is in DSA_generate_parameters().
110 unsigned char *SHA1(const unsigned char *d
, unsigned long n
,unsigned char *md
)
113 sslUtilsDebug("SHA1 with NULL md");
114 CssmError::throwMe(CSSMERR_CSP_INTERNAL_ERROR
);
116 cspGenSha1Hash(d
, n
, md
);