YarrowServer_OSX.cpp [plain text]
#include "YarrowServer_OSX.h"
#include <mach/mach_error.h>
#include <sys/errno.h>
#include <stdio.h> // debug
#include <yarrowMigTypes.h>
#include "MacYarrow_OSX.h"
#include <CoreServices/../Frameworks/CarbonCore.framework/Headers/MacErrors.h>
#include <Security/cssmalloc.h>
#define YS_DEBUG 0
#if YS_DEBUG
#define ysprintf(x) printf x
#else
#define ysprintf(x)
#endif
using MachPlusPlus::MachServer;
class YarrowTimer : public MachServer::Timer {
public:
YarrowTimer(MachPlusPlus::MachServer &server) :
MachServer::Timer(),
mServer(server) {}
void action();
void scheduleTimer(unsigned msFromNow);
private:
MachPlusPlus::MachServer &mServer; };
void YarrowTimer::action()
{
unsigned nextTimeout = yarrowTimerEvent();
scheduleTimer(nextTimeout);
}
void YarrowTimer::scheduleTimer(unsigned msFromNow)
{
mServer.setTimer(this, Time::Interval(msFromNow / 1000.0));
}
static YarrowTimer *yarrowTimer;
static CssmAllocator *cssmAlloc;
YarrowServer::YarrowServer(MachPlusPlus::MachServer &globalServer, const char *entropyFilePath) :
MachServer(YARROW_SERVER_NAME)
{
#ifdef TEMPORARY_SEMANTICS
MutexLocker ml (gYarrowMutex);
#endif
unsigned firstTimeout;
yarrowTimer = new YarrowTimer(globalServer);
cssmAlloc = &CssmAllocator::standard();
yarrowServerInit(entropyFilePath, &firstTimeout);
yarrowTimer->scheduleTimer(firstTimeout);
}
YarrowServer::~YarrowServer()
{
delete yarrowTimer; }
void YarrowServer::runYarrow()
{
Thread::run();
}
void YarrowServer::action()
{
ysprintf(("YarrowServer: running MachServer\n"));
MachServer::run();
}
boolean_t yarrow_server(mach_msg_header_t *, mach_msg_header_t *);
boolean_t YarrowServer::handle(mach_msg_header_t *in, mach_msg_header_t *out)
{
return yarrow_server(in, out);
}
void YarrowServer::notifyDeadName(MachPlusPlus::Port port)
{
}
#define UCSP_ARGS mach_port_t sport, mach_port_t rport, OSStatus *rcode
kern_return_t
yarrow_server_addEntropy(
UCSP_ARGS,
Data bytes,
mach_msg_type_number_t bytesCnt,
UInt32 entBits)
{
unsigned nextTimeout;
ysprintf(("yarrow server addEntropy(%02X %02X %02X %02X...) called\n",
((UInt8 *)bytes)[0], ((UInt8 *)bytes)[1], ((UInt8 *)bytes)[2],
((UInt8 *)bytes)[3]));
*rcode = yarrowAddEntropy(static_cast<UInt8 *>(bytes), bytesCnt, entBits,
&nextTimeout);
if(nextTimeout != 0) {
yarrowTimer->scheduleTimer(nextTimeout);
}
return KERN_SUCCESS;
}
kern_return_t
yarrow_server_getRandomBytes(
UCSP_ARGS,
UInt32 numBytes, Data *bytes, mach_msg_type_number_t *bytesCnt) {
void *tempPtr;
try {
tempPtr = cssmAlloc->malloc(numBytes);
}
catch(...) {
return unix_err(ENOMEM);
}
MachPlusPlus::MachServer::active().releaseWhenDone(*cssmAlloc, tempPtr);
*rcode = yarrowGetRandomBytes(reinterpret_cast<UInt8 *>(tempPtr), numBytes);
if(*rcode == noErr) {
*bytes = reinterpret_cast<Data>(tempPtr);
*bytesCnt = numBytes;
}
else {
*bytesCnt = 0;
}
ysprintf(("yarrow server getRandomBytes called; data %02X %02X %02X %02X...\n",
((UInt8 *)*bytes)[0], ((UInt8 *)*bytes)[1], ((UInt8 *)*bytes)[2],
((UInt8 *)*bytes)[3]));
return KERN_SUCCESS;
}