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/cssmclient.h>
26 #include <Security/context.h>
27 #include <Security/cssmacl.h>
38 class CSPImpl
: public AttachmentImpl
41 CSPImpl(const Guid
&guid
);
42 CSPImpl(const Module
&module);
45 // the least inappropriate place for this one
46 void freeKey(CssmKey
&key
, const AccessCredentials
*cred
= NULL
, bool permanent
= false);
49 class CSP
: public Attachment
54 explicit CSP(Impl
*impl
) : Attachment(impl
) {}
55 CSP(const Guid
&guid
) : Attachment(new Impl(guid
)) {}
56 CSP(const Module
&module) : Attachment(new Impl(module)) {}
58 Impl
*operator ->() const { return &impl
<Impl
>(); }
59 Impl
&operator *() const { return impl
<Impl
>(); }
63 // A cryptographic context.
64 // Contexts always belong to CSPs (CSP attachments).
66 class Context
: public ObjectImpl
69 Context(const CSP
&csp
, CSSM_ALGORITHMS alg
= CSSM_ALGID_NONE
);
72 CSP
Context::attachment() const { return parent
<CSP
>(); }
73 Module
Context::module() const { return attachment()->module(); }
75 CSSM_ALGORITHMS
algorithm() const { return mAlgorithm
; }
76 void algorithm(CSSM_ALGORITHMS alg
);
79 CSSM_CC_HANDLE
handle() { activate(); return mHandle
; }
81 uint32
getOutputSize(uint32 inputSize
, bool encrypt
= true);
82 void getOutputSize(CSSM_QUERY_SIZE_DATA
&sizes
, uint32 count
, bool encrypt
= true);
85 // don't use this section unless you know what you're doing!
86 void override(const ::Context
&ctx
);
89 void set(CSSM_ATTRIBUTE_TYPE type
, const T
&value
)
92 ::Context::Attr
attr(type
, value
);
93 check(CSSM_UpdateContextAttributes(mHandle
, 1, &attr
));
97 void set(CSSM_ATTRIBUTE_TYPE type
, uint32 value
)
100 ::Context::Attr
attr(type
, value
);
101 check(CSSM_UpdateContextAttributes(mHandle
, 1, &attr
));
106 void add(CSSM_ATTRIBUTE_TYPE type
, const T
&value
)
107 { activate(); set(type
, value
); }
109 void add(CSSM_ATTRIBUTE_TYPE type
, uint32 value
)
110 { activate(); set(type
, value
); }
113 CSSM_ALGORITHMS mAlgorithm
; // intended algorithm
114 CSSM_CC_HANDLE mHandle
; // CSSM CC handle
115 bool mStaged
; // staged in progress
119 virtual void init(); // Subclasses must implement if they support staged operations.
122 { activate(); if (mStaged
) CssmError::throwMe(CSSMERR_CSP_STAGED_OPERATION_IN_PROGRESS
); }
125 { if (!mStaged
) init(); }
130 // A PassThough context
132 class PassThrough
: public Context
135 PassThrough(const CSP
&csp
) : Context(csp
) { }
138 void operator () (uint32 passThroughId
, const void *inData
, void **outData
);
140 const CSSM_KEY
*key() const { return mKey
; }
141 void key(const CSSM_KEY
*k
) { mKey
= k
; set(CSSM_ATTRIBUTE_KEY
, k
); }
147 const CSSM_KEY
*mKey
;
154 class Digest
: public Context
157 Digest(const CSP
&csp
, CSSM_ALGORITHMS alg
) : Context(csp
, alg
) { }
161 void digest(const CssmData
&data
, CssmData
&digest
) { this->digest(&data
, 1, digest
); }
162 void digest(const CssmData
*data
, uint32 count
, CssmData
&digest
);
165 void digest(const CssmData
&data
) { digest(&data
, 1); }
166 void digest(const CssmData
*data
, uint32 count
);
167 void operator () (CssmData
&digest
);
168 CssmData
operator () () { CssmData digest
; (*this)(digest
); return digest
; }
178 class Random
: public Context
181 Random(const CSP
&csp
, CSSM_ALGORITHMS alg
) : Context(csp
, alg
), mSeed(NULL
), mSize(1) { }
182 Random(const CSP
&csp
, CSSM_ALGORITHMS alg
, const CssmCryptoData
&seed
)
183 : Context(csp
, alg
), mSeed(&seed
), mSize(1) { }
184 Random(const CSP
&csp
, CSSM_ALGORITHMS alg
, uint32 size
)
185 : Context(csp
, alg
), mSeed(NULL
), mSize(size
) { }
186 Random(const CSP
&csp
, CSSM_ALGORITHMS alg
, const CssmCryptoData
&seed
, uint32 size
)
187 : Context(csp
, alg
), mSeed(&seed
), mSize(size
) { }
189 void seed(const CssmCryptoData
&data
);
190 void size(uint32 size
);
193 void generate(CssmData
&data
, uint32 size
= 0);
195 // alternate function-call form
196 CssmData
operator () (uint32 size
= 0)
197 { CssmData output
; generate(output
, size
); return output
; }
203 const CssmCryptoData
*mSeed
;
208 } // end namespace CssmClient
210 } // end namespace Security
212 #endif // _H_CDSA_CLIENT_CSPCLIENT