]> git.saurik.com Git - apple/security.git/blob - AppleCSP/open_ssl/opensslUtils/opensslUtils.cpp
3a55eebedccba81aa11fcb3352538b173a52ce52
[apple/security.git] / AppleCSP / open_ssl / opensslUtils / opensslUtils.cpp
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 * opensslUtils.h - Support for ssleay-derived crypto modules
21 */
22
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>
33
34 #define sslUtilsDebug(args...) debug("sslUtils", ## args)
35
36 openSslException::openSslException(
37 int irtn,
38 const char *op)
39 : mIrtn(irtn)
40 {
41 if(op) {
42 char buf[300];
43 ERR_error_string(irtn, buf);
44 sslUtilsDebug("%s: %s\n", op, buf);
45 }
46 }
47
48 /* these are replacements for the ones in ssleay */
49 #define DUMP_RAND_BYTES 0
50
51 static int randDex = 1;
52
53 int RAND_bytes(unsigned char *buf,int num)
54 {
55 try {
56 cspGetRandomBytes(buf, (unsigned)num);
57 }
58 catch(...) {
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++;
64 }
65 }
66 return 1;
67 }
68
69 int RAND_pseudo_bytes(unsigned char *buf,int num)
70 {
71 return RAND_bytes(buf, num);
72 }
73
74 void RAND_add(const void *buf,int num,double entropy)
75 {
76 try {
77 cspAddEntropy(buf, (unsigned)num);
78 }
79 catch(...) {
80 }
81 }
82
83 /* replacement for mem_dbg.c */
84 int CRYPTO_mem_ctrl(int mode)
85 {
86 return 0;
87 }
88
89 /*
90 * Log error info. Returns the error code we pop off the error queue.
91 */
92 unsigned long logSslErrInfo(const char *op)
93 {
94 unsigned long e = ERR_get_error();
95 char outbuf[1024];
96 ERR_error_string(e, outbuf);
97 if(op) {
98 Security::Syslog::error("Apple CSP %s: %s", op, outbuf);
99 }
100 else {
101 Security::Syslog::error("Apple CSP %s", outbuf);
102 }
103 return e;
104 }
105
106 /*
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().
109 */
110 unsigned char *SHA1(const unsigned char *d, unsigned long n,unsigned char *md)
111 {
112 if(md == NULL) {
113 sslUtilsDebug("SHA1 with NULL md");
114 CssmError::throwMe(CSSMERR_CSP_INTERNAL_ERROR);
115 }
116 cspGenSha1Hash(d, n, md);
117 return md;
118 }
119