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 * YarrowServer - Yarrow RNG server object, OSX version
22 #include "YarrowServer_OSX.h"
23 #include <mach/mach_error.h>
24 #include <sys/errno.h>
25 #include <stdio.h> // debug
26 #include <yarrowMigTypes.h>
27 #include "MacYarrow_OSX.h"
28 #include <CoreServices/../Frameworks/CarbonCore.framework/Headers/MacErrors.h>
29 #include <Security/cssmalloc.h>
33 #define ysprintf(x) printf x
38 using MachPlusPlus::MachServer
;
41 * A timer for this module. Just one, and it's always active subsequent to startup.
43 class YarrowTimer
: public MachServer::Timer
{
45 YarrowTimer(MachPlusPlus::MachServer
&server
) :
50 void scheduleTimer(unsigned msFromNow
);
52 MachPlusPlus::MachServer
&mServer
; // to which we do setTimer()
56 * Timeout event, the sole purpose of this class. Pass on to MacYarrow module.
58 void YarrowTimer::action()
60 unsigned nextTimeout
= yarrowTimerEvent();
61 scheduleTimer(nextTimeout
);
64 void YarrowTimer::scheduleTimer(unsigned msFromNow
)
66 mServer
.setTimer(this, Time::Interval(msFromNow
/ 1000.0));
69 /* global, one per process, therefore one per system */
70 static YarrowTimer
*yarrowTimer
;
71 static CssmAllocator
*cssmAlloc
; // for temp alloc in
72 // yarrow_server_getRandomBytes()
74 YarrowServer::YarrowServer(MachPlusPlus::MachServer
&globalServer
, const char *entropyFilePath
) :
75 MachServer(YARROW_SERVER_NAME
)
77 #ifdef TEMPORARY_SEMANTICS
78 MutexLocker
ml (gYarrowMutex
);
80 unsigned firstTimeout
;
81 yarrowTimer
= new YarrowTimer(globalServer
);
82 cssmAlloc
= &CssmAllocator::standard();
83 yarrowServerInit(entropyFilePath
, &firstTimeout
);
84 yarrowTimer
->scheduleTimer(firstTimeout
);
89 * Clean up the server object
91 YarrowServer::~YarrowServer()
93 delete yarrowTimer
; // FIXME - is this safe? Does MachServer do this?
97 * Thread::run() specific to this class, needed because both MachServer
98 * and Thread have a run() method. Called from main().
100 void YarrowServer::runYarrow()
106 * Run the server. This will not return until the server is forced to exit.
108 void YarrowServer::action()
110 ysprintf(("YarrowServer: running MachServer\n"));
116 // The server run-loop function, called out from MachServer
118 boolean_t
yarrow_server(mach_msg_header_t
*, mach_msg_header_t
*);
120 boolean_t
YarrowServer::handle(mach_msg_header_t
*in
, mach_msg_header_t
*out
)
122 return yarrow_server(in
, out
);
127 // Handling dead-port notifications
128 // FIXME - how is this used?
130 void YarrowServer::notifyDeadName(MachPlusPlus::Port port
)
132 // forcibly end the Connection
133 // FIXME....endConnection(port, true);
137 * Functions called from server side of MIG interface.
138 * As far as I can tell, MIG doesn't generate
139 * prototypes for the server side...FIXME....
143 #define UCSP_ARGS mach_port_t sport, mach_port_t rport, OSStatus *rcode
146 yarrow_server_addEntropy(
149 mach_msg_type_number_t bytesCnt
,
152 unsigned nextTimeout
;
153 ysprintf(("yarrow server addEntropy(%02X %02X %02X %02X...) called\n",
154 ((UInt8
*)bytes
)[0], ((UInt8
*)bytes
)[1], ((UInt8
*)bytes
)[2],
155 ((UInt8
*)bytes
)[3]));
156 *rcode
= yarrowAddEntropy(static_cast<UInt8
*>(bytes
), bytesCnt
, entBits
,
158 if(nextTimeout
!= 0) {
159 yarrowTimer
->scheduleTimer(nextTimeout
);
166 yarrow_server_getRandomBytes(
168 UInt32 numBytes
, // in
170 mach_msg_type_number_t
*bytesCnt
) // out
173 * We have to allocate here; MIG does a virtual copy back to
174 * client. MachServer releases later.
178 tempPtr
= cssmAlloc
->malloc(numBytes
);
181 return unix_err(ENOMEM
);
183 MachPlusPlus::MachServer::active().releaseWhenDone(*cssmAlloc
, tempPtr
);
184 *rcode
= yarrowGetRandomBytes(reinterpret_cast<UInt8
*>(tempPtr
), numBytes
);
185 if(*rcode
== noErr
) {
186 *bytes
= reinterpret_cast<Data
>(tempPtr
);
187 *bytesCnt
= numBytes
;
192 ysprintf(("yarrow server getRandomBytes called; data %02X %02X %02X %02X...\n",
193 ((UInt8
*)*bytes
)[0], ((UInt8
*)*bytes
)[1], ((UInt8
*)*bytes
)[2],
194 ((UInt8
*)*bytes
)[3]));