2 * Copyright (c) 2000-2002,2011-2012,2014 Apple 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()
76 StLock
<Mutex
> _(mActivateMutex
);
80 check(CSSM_DeleteContext(mHandle
));
85 void Context::algorithm(CSSM_ALGORITHMS alg
)
88 abort(); //@@@ can't (currently?) change algorithm with active context
93 void Context::cred(const CSSM_ACCESS_CREDENTIALS
*cred
)
95 mCred
= AccessCredentials::overlay(cred
);
96 set(CSSM_ATTRIBUTE_ACCESS_CREDENTIALS
, *mCred
);
101 // Query context operation output sizes.
103 uint32
Context::getOutputSize(uint32 inputSize
, bool encrypt
/*= true*/)
105 CSSM_QUERY_SIZE_DATA data
;
106 data
.SizeInputBlock
= inputSize
;
107 getOutputSize(data
, 1, encrypt
);
108 return data
.SizeOutputBlock
;
111 void Context::getOutputSize(CSSM_QUERY_SIZE_DATA
&sizes
, uint32 count
, bool encrypt
/*= true*/)
113 check(CSSM_QuerySize(handle(), encrypt
, count
, &sizes
));
118 // The override() method of Context is an expert feature. It replaces the entire
119 // context with a context object provided. It is up to the caller to keep this context
120 // consistent with the purpose of the Context subclass he is (mis)using.
121 // This feature is currently used by the SecurityServer.
123 void Context::override(const Security::Context
&ctx
)
126 // make a valid context object (it doesn't matter what kind - keep it cheap)
127 check(CSSM_CSP_CreateDigestContext(attachment()->handle(), CSSM_ALGID_NONE
, &mHandle
));
129 // now replace everything with the context data provided
130 check(CSSM_SetContext(mHandle
, &ctx
));
131 mActive
= true; // now active
138 const ResourceControlContext
&RccBearer::compositeRcc() const
140 // explicitly specified RCC wins
144 // cobble one up from the pieces
146 mWorkRcc
.input() = *mOwner
;
149 mWorkRcc
.credentials(mOpCred
);
154 void RccBearer::owner(const CSSM_ACL_ENTRY_PROTOTYPE
*owner
)
158 this->owner(mWorkInput
);
160 this->owner((AclEntryInput
*)NULL
);
165 // Manage PassThrough contexts
169 // Invoke passThrough
172 PassThrough::operator() (uint32 passThroughId
, const void *inData
, void **outData
)
174 check(CSSM_CSP_PassThrough(handle(), passThroughId
, inData
, outData
));
177 void PassThrough::activate()
179 StLock
<Mutex
> _(mActivateMutex
);
181 check(CSSM_CSP_CreatePassThroughContext(attachment()->handle(), mKey
, &mHandle
));
188 // Manage Digest contexts
190 void Digest::activate()
192 StLock
<Mutex
> _(mActivateMutex
);
194 check(CSSM_CSP_CreateDigestContext(attachment()->handle(), mAlgorithm
, &mHandle
));
200 void Digest::digest(const CssmData
*data
, uint32 count
, CssmData
&digest
)
204 Error::throwMe(CSSMERR_CSP_STAGED_OPERATION_IN_PROGRESS
);
205 check(CSSM_DigestData(handle(), data
, count
, &digest
));
208 void Digest::digest(const CssmData
*data
, uint32 count
)
212 check(CSSM_DigestDataInit(handle()));
215 check(CSSM_DigestDataUpdate(handle(), data
, count
));
218 void Digest::operator () (CssmData
&digest
)
221 Error::throwMe(CSSMERR_CSP_STAGED_OPERATION_NOT_STARTED
);
222 check(CSSM_DigestDataFinal(handle(), &digest
));
228 // Random number generation
230 void Random::seed(const CssmCryptoData
&seedData
)
233 set(CSSM_ATTRIBUTE_SEED
, seedData
);
236 void Random::size(uint32 sz
)
239 set(CSSM_ATTRIBUTE_OUTPUT_SIZE
, sz
);
243 void Random::activate()
245 StLock
<Mutex
> _(mActivateMutex
);
247 check(CSSM_CSP_CreateRandomGenContext(attachment()->handle(), mAlgorithm
,
248 mSeed
, mSize
, &mHandle
));
254 void Random::generate(CssmData
&data
, uint32 newSize
)
259 assert(!mStaged
); // not a stage-able operation
260 check(CSSM_GenerateRandom(handle(), &data
));
263 } // end namespace CssmClient
264 } // end namespace Security