2 * Copyright (c) 2000-2001 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/cspclient.h>
24 using namespace CssmClient
;
28 // Manage CSP attachments
30 CSPImpl::CSPImpl(const Guid
&guid
) : AttachmentImpl(guid
, CSSM_SERVICE_CSP
)
34 CSPImpl::CSPImpl(const Module
&module) : AttachmentImpl(module, CSSM_SERVICE_CSP
)
44 // Delete a key explicitly
46 void CSPImpl::freeKey(CssmKey
&key
, const AccessCredentials
*cred
, bool permanent
)
48 check(CSSM_FreeKey(handle(), cred
, &key
, permanent
));
53 // Manage generic context objects
55 CssmClient::Context::Context(const CSP
&csp
, CSSM_ALGORITHMS alg
)
56 : ObjectImpl(csp
), mAlgorithm(alg
), mStaged(false)
60 CssmClient::Context::~Context()
68 void CssmClient::Context::init()
70 CssmError::throwMe(CSSM_ERRCODE_FUNCTION_NOT_IMPLEMENTED
);
73 void CssmClient::Context::deactivate()
78 check(CSSM_DeleteContext(mHandle
));
83 void CssmClient::Context::algorithm(CSSM_ALGORITHMS alg
)
86 abort(); //@@@ can't (currently?) change algorithm with active context
92 // The override() method of Context is an expert feature. It replaces the entire
93 // context with a context object provided. It is up to the caller to keep this context
94 // consistent with the purpose of the Context subclass he is (mis)using.
95 // This feature is currently used by the SecurityServer.
97 void CssmClient::Context::override(const Security::Context
&ctx
)
100 // make a valid context object (it doesn't matter what kind - keep it cheap)
101 check(CSSM_CSP_CreateDigestContext(attachment()->handle(), CSSM_ALGID_NONE
, &mHandle
));
103 // now replace everything with the context data provided
104 check(CSSM_SetContext(mHandle
, &ctx
));
105 mActive
= true; // now active
110 // Manage Digest contexts
112 void Digest::activate()
115 check(CSSM_CSP_CreateDigestContext(attachment()->handle(), mAlgorithm
, &mHandle
));
121 void Digest::digest(const CssmData
*data
, uint32 count
, CssmData
&digest
)
125 Error::throwMe(CSSMERR_CSP_STAGED_OPERATION_IN_PROGRESS
);
126 check(CSSM_DigestData(handle(), data
, count
, &digest
));
129 void Digest::digest(const CssmData
*data
, uint32 count
)
133 check(CSSM_DigestDataInit(handle()));
136 check(CSSM_DigestDataUpdate(handle(), data
, count
));
139 void Digest::operator () (CssmData
&digest
)
142 Error::throwMe(CSSMERR_CSP_STAGED_OPERATION_NOT_STARTED
);
143 check(CSSM_DigestDataFinal(handle(), &digest
));
149 // Random number generation
151 void Random::seed(const CssmCryptoData
&seedData
)
154 set(CSSM_ATTRIBUTE_SEED
, seedData
);
157 void Random::size(uint32 sz
)
160 set(CSSM_ATTRIBUTE_OUTPUT_SIZE
, sz
);
164 void Random::activate()
167 check(CSSM_CSP_CreateRandomGenContext(attachment()->handle(), mAlgorithm
,
168 mSeed
, mSize
, &mHandle
));
174 void Random::generate(CssmData
&data
, uint32 newSize
)
179 assert(!mStaged
); // not a stage-able operation
180 check(CSSM_GenerateRandom(handle(), &data
));