]> git.saurik.com Git - apple/security.git/blame - cdsa/cssm/cssmcontext.h
Security-176.tar.gz
[apple/security.git] / cdsa / cssm / cssmcontext.h
CommitLineData
bac41a7b
A
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// context - manage CSSM (cryptographic) contexts every which way
21//
22#ifndef _H_CSSMCONTEXT
23#define _H_CSSMCONTEXT
24
25#include "cssmint.h"
26#include "cspattachment.h"
27#include <Security/context.h>
28
29#ifdef _CPP_CSSMCONTEXT
30# pragma export on
31#endif
32
33
34//
35// A HandleContext adds handle semantics to the Context object.
36// Note that not every Context is a HandleContext - the Contexts we hand
37// to our API customers for fondling are not. Also note that a HandleContext
38// not a PODWrapper.
39// HandleContext has an allocation method taking a CssmAllocator. To destroy
40// a HandleObject, call HandleObject::destroy(the-context, the-allocator).
41// You are responsible for picking the same allocator used on construction.
42//
43// THREADS: HandleContexts are assumed to have single-thread use. That means that
44// operations on HandleContexts are NOT interlocked automatically; two users of
45// the same context must do any arbitration themselves. A HandleContext is howerver
46// safely interlocked against other objects, in particular its CSPAttachment.
47// The upshot is that you're safe using a HandleContext unless someone else is trying
48// to use the same context in parallel.
49//
50class HandleContext : public HandleObject, public Context {
51public:
52 HandleContext(CSPAttachment &attach,
53 CSSM_CONTEXT_TYPE type,
54 CSSM_ALGORITHMS algorithmId)
55 : Context(type, algorithmId), attachment(attach), extent(0) { }
56 virtual ~HandleContext();
57
58 CSPAttachment &attachment;
29654253
A
59
60 using Context::find; // guard against HandleObjec::find
bac41a7b
A
61
62 void mergeAttributes(const CSSM_CONTEXT_ATTRIBUTE *attributes, uint32 count);
63 CSSM_RETURN validateChange(CSSM_CONTEXT_EVENT event);
64
29654253 65 void *operator new (size_t size, CssmAllocator &alloc) throw(std::bad_alloc)
bac41a7b 66 { return alloc.malloc(size); }
29654253 67 void operator delete (void *addr, size_t, CssmAllocator &alloc) throw()
bac41a7b 68 { return alloc.free(addr); }
29654253 69 static void destroy(HandleContext *context, CssmAllocator &alloc) throw()
bac41a7b
A
70 { context->~HandleContext(); alloc.free(context); }
71
72 class Maker; // deluxe builder
73
29654253
A
74#if __GNUC__ > 2
75private:
76 void operator delete (void *addr) throw() { assert(0); }
77#endif
78
bac41a7b
A
79protected:
80 // Locking protocol, courtesy of HandleObject.
81 // This locks the underlying attachment.
82 void lock();
83 bool tryLock();
84
85private:
86 void *extent; // extra storage extent in use
87};
88
89inline HandleContext &enterContext(CSSM_CC_HANDLE h)
90{
91 return findHandleAndLock<HandleContext>(h, CSSM_ERRCODE_INVALID_CONTEXT_HANDLE);
92}
93
94
95//
96// A Maker is a deluxe wrapper around Builder. It creates whole HandleContext
97// objects in one swell foop, handling object locking, construction, error
98// recovery, and all that jazz. A Maker cannot create plain Context objects.
99//
100class HandleContext::Maker : public Context::Builder {
101public:
102 Maker(CSSM_CSP_HANDLE handle)
103 : Context::Builder(findHandleAndLock<CSPAttachment>(handle, CSSM_ERRCODE_INVALID_CSP_HANDLE)),
104 attachment(static_cast<CSPAttachment &>(allocator)), // order dependency(!)
105 locker(attachment, true)
106 { attachment.finishEnter(); }
107
108 CSPAttachment &attachment;
109
110 CSSM_CC_HANDLE operator () (CSSM_CONTEXT_TYPE type,
111 CSSM_ALGORITHMS algorithm);
112
113private:
114 StLock<CountingMutex, &CountingMutex::enter, &CountingMutex::exit> locker;
115};
116
117#endif //_H_CSSMCONTEXT