]> git.saurik.com Git - apple/security.git/blame - cdsa/cssm/cssmcontext.h
Security-30.1.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;
59
60 void mergeAttributes(const CSSM_CONTEXT_ATTRIBUTE *attributes, uint32 count);
61 CSSM_RETURN validateChange(CSSM_CONTEXT_EVENT event);
62
63 void *operator new (size_t size, CssmAllocator &alloc)
64 { return alloc.malloc(size); }
65 void operator delete (void *addr, size_t, CssmAllocator &alloc)
66 { return alloc.free(addr); }
67 static void destroy(HandleContext *context, CssmAllocator &alloc)
68 { context->~HandleContext(); alloc.free(context); }
69
70 class Maker; // deluxe builder
71
72protected:
73 // Locking protocol, courtesy of HandleObject.
74 // This locks the underlying attachment.
75 void lock();
76 bool tryLock();
77
78private:
79 void *extent; // extra storage extent in use
80};
81
82inline HandleContext &enterContext(CSSM_CC_HANDLE h)
83{
84 return findHandleAndLock<HandleContext>(h, CSSM_ERRCODE_INVALID_CONTEXT_HANDLE);
85}
86
87
88//
89// A Maker is a deluxe wrapper around Builder. It creates whole HandleContext
90// objects in one swell foop, handling object locking, construction, error
91// recovery, and all that jazz. A Maker cannot create plain Context objects.
92//
93class HandleContext::Maker : public Context::Builder {
94public:
95 Maker(CSSM_CSP_HANDLE handle)
96 : Context::Builder(findHandleAndLock<CSPAttachment>(handle, CSSM_ERRCODE_INVALID_CSP_HANDLE)),
97 attachment(static_cast<CSPAttachment &>(allocator)), // order dependency(!)
98 locker(attachment, true)
99 { attachment.finishEnter(); }
100
101 CSPAttachment &attachment;
102
103 CSSM_CC_HANDLE operator () (CSSM_CONTEXT_TYPE type,
104 CSSM_ALGORITHMS algorithm);
105
106private:
107 StLock<CountingMutex, &CountingMutex::enter, &CountingMutex::exit> locker;
108};
109
110#endif //_H_CSSMCONTEXT