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 #include <security_cdsa_client/cspclient.h>
25 namespace CssmClient
{
29 // Manage CSP attachments
31 CSPImpl::CSPImpl(const Guid
&guid
) : AttachmentImpl(guid
, CSSM_SERVICE_CSP
)
35 CSPImpl::CSPImpl(const Module
&module) : AttachmentImpl(module, CSSM_SERVICE_CSP
)
45 // Delete a key explicitly
47 void CSPImpl::freeKey(CssmKey
&key
, const AccessCredentials
*cred
, bool permanent
)
49 check(CSSM_FreeKey(handle(), cred
, &key
, permanent
));
54 // Manage generic context objects
56 Context::Context(const CSP
&csp
, CSSM_ALGORITHMS alg
)
57 : ObjectImpl(csp
), mAlgorithm(alg
), mStaged(false), mCred(NULL
)
71 CssmError::throwMe(CSSM_ERRCODE_FUNCTION_NOT_IMPLEMENTED
);
74 void Context::deactivate()
79 check(CSSM_DeleteContext(mHandle
));
84 void Context::algorithm(CSSM_ALGORITHMS alg
)
87 abort(); //@@@ can't (currently?) change algorithm with active context
92 void Context::cred(const CSSM_ACCESS_CREDENTIALS
*cred
)
94 mCred
= AccessCredentials::overlay(cred
);
95 set(CSSM_ATTRIBUTE_ACCESS_CREDENTIALS
, *mCred
);
100 // Query context operation output sizes.
102 uint32
Context::getOutputSize(uint32 inputSize
, bool encrypt
/*= true*/)
104 CSSM_QUERY_SIZE_DATA data
;
105 data
.SizeInputBlock
= inputSize
;
106 getOutputSize(data
, 1, encrypt
);
107 return data
.SizeOutputBlock
;
110 void Context::getOutputSize(CSSM_QUERY_SIZE_DATA
&sizes
, uint32 count
, bool encrypt
/*= true*/)
112 check(CSSM_QuerySize(handle(), encrypt
, count
, &sizes
));
117 // The override() method of Context is an expert feature. It replaces the entire
118 // context with a context object provided. It is up to the caller to keep this context
119 // consistent with the purpose of the Context subclass he is (mis)using.
120 // This feature is currently used by the SecurityServer.
122 void Context::override(const Security::Context
&ctx
)
125 // make a valid context object (it doesn't matter what kind - keep it cheap)
126 check(CSSM_CSP_CreateDigestContext(attachment()->handle(), CSSM_ALGID_NONE
, &mHandle
));
128 // now replace everything with the context data provided
129 check(CSSM_SetContext(mHandle
, &ctx
));
130 mActive
= true; // now active
137 const ResourceControlContext
&RccBearer::compositeRcc() const
139 // explicitly specified RCC wins
143 // cobble one up from the pieces
145 mWorkRcc
.input() = *mOwner
;
148 mWorkRcc
.credentials(mOpCred
);
153 void RccBearer::owner(const CSSM_ACL_ENTRY_PROTOTYPE
*owner
)
157 this->owner(mWorkInput
);
159 this->owner((AclEntryInput
*)NULL
);
164 // Manage PassThrough contexts
168 // Invoke passThrough
171 PassThrough::operator() (uint32 passThroughId
, const void *inData
, void **outData
)
173 check(CSSM_CSP_PassThrough(handle(), passThroughId
, inData
, outData
));
176 void PassThrough::activate()
179 check(CSSM_CSP_CreatePassThroughContext(attachment()->handle(), mKey
, &mHandle
));
186 // Manage Digest contexts
188 void Digest::activate()
191 check(CSSM_CSP_CreateDigestContext(attachment()->handle(), mAlgorithm
, &mHandle
));
197 void Digest::digest(const CssmData
*data
, uint32 count
, CssmData
&digest
)
201 Error::throwMe(CSSMERR_CSP_STAGED_OPERATION_IN_PROGRESS
);
202 check(CSSM_DigestData(handle(), data
, count
, &digest
));
205 void Digest::digest(const CssmData
*data
, uint32 count
)
209 check(CSSM_DigestDataInit(handle()));
212 check(CSSM_DigestDataUpdate(handle(), data
, count
));
215 void Digest::operator () (CssmData
&digest
)
218 Error::throwMe(CSSMERR_CSP_STAGED_OPERATION_NOT_STARTED
);
219 check(CSSM_DigestDataFinal(handle(), &digest
));
225 // Random number generation
227 void Random::seed(const CssmCryptoData
&seedData
)
230 set(CSSM_ATTRIBUTE_SEED
, seedData
);
233 void Random::size(uint32 sz
)
236 set(CSSM_ATTRIBUTE_OUTPUT_SIZE
, sz
);
240 void Random::activate()
243 check(CSSM_CSP_CreateRandomGenContext(attachment()->handle(), mAlgorithm
,
244 mSeed
, mSize
, &mHandle
));
250 void Random::generate(CssmData
&data
, uint32 newSize
)
255 assert(!mStaged
); // not a stage-able operation
256 check(CSSM_GenerateRandom(handle(), &data
));
259 } // end namespace CssmClient
260 } // end namespace Security