2 * Copyright (c) 2000-2002 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 // cspclient - client interface to CSSM CSPs and their operations
22 #ifndef _H_CDSA_CLIENT_CSPCLIENT
23 #define _H_CDSA_CLIENT_CSPCLIENT 1
25 #include <security_cdsa_client/cssmclient.h>
26 #include <security_cdsa_utilities/context.h>
27 #include <security_cdsa_utilities/cssmacl.h>
30 namespace CssmClient
{
36 class CSPImpl
: public AttachmentImpl
39 CSPImpl(const Guid
&guid
);
40 CSPImpl(const Module
&module);
43 // the least inappropriate place for this one
44 void freeKey(CssmKey
&key
, const AccessCredentials
*cred
= NULL
, bool permanent
= false);
47 class CSP
: public Attachment
52 explicit CSP(Impl
*impl
) : Attachment(impl
) {}
53 CSP(const Guid
&guid
) : Attachment(new Impl(guid
)) {}
54 CSP(const Module
&module) : Attachment(new Impl(module)) {}
56 Impl
*operator ->() const { return &impl
<Impl
>(); }
57 Impl
&operator *() const { return impl
<Impl
>(); }
61 // A cryptographic context.
62 // Contexts always belong to CSPs (CSP attachments).
64 class Context
: public ObjectImpl
67 Context(const CSP
&csp
, CSSM_ALGORITHMS alg
= CSSM_ALGID_NONE
);
70 CSP
attachment() const { return parent
<CSP
>(); }
71 Module
module() const { return attachment()->module(); }
73 CSSM_ALGORITHMS
algorithm() const { return mAlgorithm
; }
74 void algorithm(CSSM_ALGORITHMS alg
);
76 const AccessCredentials
*cred() const { return mCred
; }
77 void cred(const CSSM_ACCESS_CREDENTIALS
*cred
);
78 void cred(const CSSM_ACCESS_CREDENTIALS
&cred
) { this->cred(&cred
); }
81 CSSM_CC_HANDLE
handle() { activate(); return mHandle
; }
83 uint32
getOutputSize(uint32 inputSize
, bool encrypt
= true);
84 void getOutputSize(CSSM_QUERY_SIZE_DATA
&sizes
, uint32 count
, bool encrypt
= true);
87 // don't use this section unless you know what you're doing!
88 void override(const ::Context
&ctx
);
91 void set(CSSM_ATTRIBUTE_TYPE type
, const T
&value
)
94 ::Context::Attr
attr(type
, value
);
95 check(CSSM_UpdateContextAttributes(mHandle
, 1, &attr
));
99 void set(CSSM_ATTRIBUTE_TYPE type
, uint32 value
)
102 ::Context::Attr
attr(type
, value
);
103 check(CSSM_UpdateContextAttributes(mHandle
, 1, &attr
));
108 void add(CSSM_ATTRIBUTE_TYPE type
, const T
&value
)
109 { activate(); set(type
, value
); }
111 void add(CSSM_ATTRIBUTE_TYPE type
, uint32 value
)
112 { activate(); set(type
, value
); }
117 virtual void init(); // Subclasses must implement if they support staged operations.
120 { activate(); if (mStaged
) CssmError::throwMe(CSSMERR_CSP_STAGED_OPERATION_IN_PROGRESS
); }
123 { if (!mStaged
) init(); }
125 const AccessCredentials
*neededCred()
126 { return AccessCredentials::needed(mCred
); }
129 CSSM_ALGORITHMS mAlgorithm
; // intended algorithm
130 CSSM_CC_HANDLE mHandle
; // CSSM CC handle
131 bool mStaged
; // staged in progress
132 const AccessCredentials
*mCred
; // if explicitly set
137 // An RccBearer holds a ResourceControlContext. Note that this is a composite
138 // of an AccessCredentials and an AclEntryInput. We allow setting the whole
139 // thing, or its two components separately. A complete rcc set (via ::rcc)
140 // overrides any components.
141 // @@@ Perhaps we should merge components into a specified rcc? Iffy, though...
142 // Note: We call the credential components "opCred" to distinguish it from
143 // the "cred" of a CredBearer; some classes are both. As a rule, the "cred" goes
144 // into the context, while the "opCred" gets passed as an argument.
148 RccBearer() : mOpCred(NULL
), mOwner(NULL
), mRcc(NULL
) { }
150 const AccessCredentials
*opCred() const { return mOpCred
; }
151 void opCred(const CSSM_ACCESS_CREDENTIALS
*cred
) { mOpCred
= AccessCredentials::overlay(cred
); }
152 void opCred(const CSSM_ACCESS_CREDENTIALS
&cred
) { this->opCred(&cred
); }
153 const AclEntryInput
*owner() const { return mOwner
; }
154 void owner(const CSSM_ACL_ENTRY_INPUT
*owner
) { mOwner
= AclEntryInput::overlay(owner
); }
155 void owner(const CSSM_ACL_ENTRY_INPUT
&owner
) { this->owner(&owner
); }
156 void owner(const CSSM_ACL_ENTRY_PROTOTYPE
*owner
);
157 void owner(const CSSM_ACL_ENTRY_PROTOTYPE
&owner
) { this->owner(&owner
); }
158 const ResourceControlContext
*rcc() const { return mRcc
; }
159 void rcc(const CSSM_RESOURCE_CONTROL_CONTEXT
*rcc
)
160 { mRcc
= ResourceControlContext::overlay(rcc
); }
161 void rcc(const CSSM_RESOURCE_CONTROL_CONTEXT
&rcc
) { this->rcc(&rcc
); }
164 const ResourceControlContext
&compositeRcc() const;
167 // an RCC contains both a cred and entryInput
168 // mCred/mAcl are only considered if mRcc is not set (NULL)
169 const AccessCredentials
*mOpCred
;
170 const AclEntryInput
*mOwner
;
171 const ResourceControlContext
*mRcc
;
173 mutable ResourceControlContext mWorkRcc
; // work area
174 mutable AclEntryInput mWorkInput
; // work area
179 // A PassThough context
181 class PassThrough
: public Context
184 PassThrough(const CSP
&csp
) : Context(csp
) { }
187 void operator () (uint32 passThroughId
, const void *inData
, void **outData
);
189 template <class TIn
, class TOut
>
190 void operator () (uint32 passThroughId
, const TIn
*inData
, TOut
**outData
)
191 { operator () (passThroughId
, (const void *)inData
, (void **)outData
); }
194 void operator () (uint32 passThroughId
, const TIn
*inData
)
195 { operator () (passThroughId
, (const void *)inData
, NULL
); }
197 const CSSM_KEY
*key() const { return mKey
; }
198 void key(const CSSM_KEY
*k
) { mKey
= k
; set(CSSM_ATTRIBUTE_KEY
, k
); }
204 const CSSM_KEY
*mKey
;
211 class Digest
: public Context
214 Digest(const CSP
&csp
, CSSM_ALGORITHMS alg
) : Context(csp
, alg
) { }
218 void digest(const CssmData
&data
, CssmData
&digest
) { this->digest(&data
, 1, digest
); }
219 void digest(const CssmData
*data
, uint32 count
, CssmData
&digest
);
222 void digest(const CssmData
&data
) { digest(&data
, 1); }
223 void digest(const CssmData
*data
, uint32 count
);
224 void operator () (CssmData
&digest
);
225 CssmData
operator () () { CssmData digest
; (*this)(digest
); return digest
; }
235 class Random
: public Context
238 Random(const CSP
&csp
, CSSM_ALGORITHMS alg
) : Context(csp
, alg
), mSeed(NULL
), mSize(1) { }
239 Random(const CSP
&csp
, CSSM_ALGORITHMS alg
, const CssmCryptoData
&seed
)
240 : Context(csp
, alg
), mSeed(&seed
), mSize(1) { }
241 Random(const CSP
&csp
, CSSM_ALGORITHMS alg
, uint32 size
)
242 : Context(csp
, alg
), mSeed(NULL
), mSize(size
) { }
243 Random(const CSP
&csp
, CSSM_ALGORITHMS alg
, const CssmCryptoData
&seed
, uint32 size
)
244 : Context(csp
, alg
), mSeed(&seed
), mSize(size
) { }
246 void seed(const CssmCryptoData
&data
);
247 void size(uint32 size
);
250 void generate(CssmData
&data
, uint32 size
= 0);
252 // alternate function-call form
253 CssmData
operator () (uint32 size
= 0)
254 { CssmData output
; generate(output
, size
); return output
; }
260 const CssmCryptoData
*mSeed
;
265 } // end namespace CssmClient
266 } // end namespace Security
268 #endif // _H_CDSA_CLIENT_CSPCLIENT