#include <openssl/rand.h>
#include <openssl/crypto.h>
#include <openssl/err.h>
#include <openssl/sha.h>
#include <Security/debugging.h>
#include <Security/cssmerr.h>
#include "opensslUtils.h"
#include <AppleCSP/YarrowConnection.h>
#include <AppleCSP/AppleCSPUtils.h>
#include <Security/logging.h>
#define sslUtilsDebug(args...) debug("sslUtils", ## args)
openSslException::openSslException(
int irtn,
const char *op)
: mIrtn(irtn)
{
if(op) {
char buf[300];
ERR_error_string(irtn, buf);
sslUtilsDebug("%s: %s\n", op, buf);
}
}
#define DUMP_RAND_BYTES 0
static int randDex = 1;
int RAND_bytes(unsigned char *buf,int num)
{
try {
cspGetRandomBytes(buf, (unsigned)num);
}
catch(...) {
Security::Syslog::error("Apple CSP: yarrow failure");
for(int i=0; i<num; i++) {
buf[i] = (i*3) + randDex++;
}
}
return 1;
}
int RAND_pseudo_bytes(unsigned char *buf,int num)
{
return RAND_bytes(buf, num);
}
void RAND_add(const void *buf,int num,double entropy)
{
try {
cspAddEntropy(buf, (unsigned)num);
}
catch(...) {
}
}
int CRYPTO_mem_ctrl(int mode)
{
return 0;
}
unsigned long logSslErrInfo(const char *op)
{
unsigned long e = ERR_get_error();
char outbuf[1024];
ERR_error_string(e, outbuf);
if(op) {
Security::Syslog::error("Apple CSP %s: %s", op, outbuf);
}
else {
Security::Syslog::error("Apple CSP %s", outbuf);
}
return e;
}
unsigned char *SHA1(const unsigned char *d, unsigned long n,unsigned char *md)
{
if(md == NULL) {
sslUtilsDebug("SHA1 with NULL md");
CssmError::throwMe(CSSMERR_CSP_INTERNAL_ERROR);
}
cspGenSha1Hash(d, n, md);
return md;
}