1/*
2 * Copyright (c) 2004 Apple Computer, Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24//
25//
26// WARNING! HERE BE DRAGONS!
27// This code involves moderately arcane magic including (but not limited to)
28// dancing macros paired off with self-maintaining stack objects. Don't take
29// anything for granted! Be very afraid of ALL-CAPS names. Your best bet is
30// probably to stick with the existing patterns.
31//
32#ifndef _H_TDTRANSIT
33#define _H_TDTRANSIT
34
35#include <security_tokend_client/tdclient.h>
36#include <security_cdsa_utilities/cssmwalkers.h>
37#include <SecurityTokend/SecTokend.h>
38#include "tokend_types.h"
39#include "tokend.h"
40
41namespace Security {
42namespace Tokend {
43
44
45// stock leading argument profile used by all calls
46#define TOKEND_ARGS mServicePort, mReplyPort, &rcode
47
48// IPC wraps the actual MIG call
49#define IPC(statement) \
50	{ CSSM_RETURN rcode; check(statement); if (rcode != CSSM_OK) CssmError::throwMe(rcode); }
51
52// pass mandatory or optional CssmData arguments into an IPC call
53#define DATA(arg)			arg.data(), arg.length()
54#define OPTIONALDATA(arg)	(arg ? arg->data() : NULL), (arg ? arg->length() : 0)
55
56// pass structured arguments in/out of IPC calls. See "data walkers" for details
57#define COPY(copy)			copy, copy.length(), copy
58#define COPYFLAT(copy)		copy, copy##Length, copy
59#define COPY_OUT(copy)		&copy, &copy##Length, &copy##Base
60#define COPY_OUT_DECL(type,name) type *name, *name##Base; mach_msg_type_number_t name##Length
61
62
63//
64// DataOutput manages an output CssmData argument.
65//
66class DataOutput {
67public:
68	DataOutput(CssmData &arg, Allocator &alloc)
69		: argument(arg), allocator(alloc) { mData = NULL; mLength = 0; }
70	~DataOutput();
71
72	void **data() { return &mData; }
73	mach_msg_type_number_t *length() { return &mLength; }
74
75	CssmData &argument;
76	Allocator &allocator;
77
78private:
79	void *mData;
80	mach_msg_type_number_t mLength;
81};
82
83
84//
85// Bundle up a Context for IPC transmission
86//
87class SendContext {
88public:
89	SendContext(const Context &ctx);
90	~SendContext() { Allocator::standard().free(attributes); }
91
92	const Context &context;
93	CSSM_CONTEXT_ATTRIBUTE *attributes;
94	size_t attributeSize;
95};
96
97#define CONTEXT(ctx)	ctx.context, ctx.attributes, ctx.attributes, ctx.attributeSize
98
99
100//
101// A PodWrapper for TOKEND_RETURN_DATA (used in the tokend APIs)
102//
103class TokendReturnData : public PodWrapper<TokendReturnData, TOKEND_RETURN_DATA> {
104public:
105};
106
107
108}	// namespace Tokend
109}	// namespace Security
110
111#endif //_H_TDTRANSIT
112