]> git.saurik.com Git - apple/security.git/blob - cdsa/cdsa_client/cspclient.h
Security-177.tar.gz
[apple/security.git] / cdsa / cdsa_client / cspclient.h
1 /*
2 * Copyright (c) 2000-2002 Apple Computer, Inc. All Rights Reserved.
3 *
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
8 * using this file.
9 *
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.
16 */
17
18
19 //
20 // cspclient - client interface to CSSM CSPs and their operations
21 //
22 #ifndef _H_CDSA_CLIENT_CSPCLIENT
23 #define _H_CDSA_CLIENT_CSPCLIENT 1
24
25 #include <Security/cssmclient.h>
26 #include <Security/context.h>
27 #include <Security/cssmacl.h>
28
29 namespace Security
30 {
31
32 namespace CssmClient
33 {
34
35 //
36 // A CSP attachment
37 //
38 class CSPImpl : public AttachmentImpl
39 {
40 public:
41 CSPImpl(const Guid &guid);
42 CSPImpl(const Module &module);
43 virtual ~CSPImpl();
44
45 // the least inappropriate place for this one
46 void freeKey(CssmKey &key, const AccessCredentials *cred = NULL, bool permanent = false);
47 };
48
49 class CSP : public Attachment
50 {
51 public:
52 typedef CSPImpl Impl;
53
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)) {}
57
58 Impl *operator ->() const { return &impl<Impl>(); }
59 Impl &operator *() const { return impl<Impl>(); }
60 };
61
62 //
63 // A cryptographic context.
64 // Contexts always belong to CSPs (CSP attachments).
65 //
66 class Context : public ObjectImpl
67 {
68 public:
69 Context(const CSP &csp, CSSM_ALGORITHMS alg = CSSM_ALGID_NONE);
70 ~Context();
71
72 CSP Context::attachment() const { return parent<CSP>(); }
73 Module Context::module() const { return attachment()->module(); }
74
75 CSSM_ALGORITHMS algorithm() const { return mAlgorithm; }
76 void algorithm(CSSM_ALGORITHMS alg);
77
78 public:
79 CSSM_CC_HANDLE handle() { activate(); return mHandle; }
80
81 uint32 getOutputSize(uint32 inputSize, bool encrypt = true);
82 void getOutputSize(CSSM_QUERY_SIZE_DATA &sizes, uint32 count, bool encrypt = true);
83
84 public:
85 // don't use this section unless you know what you're doing!
86 void override(const ::Context &ctx);
87
88 template <class T>
89 void set(CSSM_ATTRIBUTE_TYPE type, const T &value)
90 {
91 if (isActive()) {
92 ::Context::Attr attr(type, value);
93 check(CSSM_UpdateContextAttributes(mHandle, 1, &attr));
94 }
95 }
96
97 void set(CSSM_ATTRIBUTE_TYPE type, uint32 value)
98 {
99 if (isActive()) {
100 ::Context::Attr attr(type, value);
101 check(CSSM_UpdateContextAttributes(mHandle, 1, &attr));
102 }
103 }
104
105 template <class T>
106 void add(CSSM_ATTRIBUTE_TYPE type, const T &value)
107 { activate(); set(type, value); }
108
109 void add(CSSM_ATTRIBUTE_TYPE type, uint32 value)
110 { activate(); set(type, value); }
111
112 protected:
113 CSSM_ALGORITHMS mAlgorithm; // intended algorithm
114 CSSM_CC_HANDLE mHandle; // CSSM CC handle
115 bool mStaged; // staged in progress
116
117 void deactivate();
118
119 virtual void init(); // Subclasses must implement if they support staged operations.
120
121 void unstaged()
122 { activate(); if (mStaged) CssmError::throwMe(CSSMERR_CSP_STAGED_OPERATION_IN_PROGRESS); }
123
124 void staged()
125 { if (!mStaged) init(); }
126 };
127
128
129 //
130 // A PassThough context
131 //
132 class PassThrough : public Context
133 {
134 public:
135 PassThrough(const CSP &csp) : Context(csp) { }
136
137 public:
138 void operator () (uint32 passThroughId, const void *inData, void **outData);
139
140 template <class TIn, class TOut>
141 void operator () (uint32 passThroughId, const TIn *inData, TOut **outData)
142 { operator () (passThroughId, (const void *)inData, (void **)outData); }
143
144 template <class TIn>
145 void operator () (uint32 passThroughId, const TIn *inData)
146 { operator () (passThroughId, (const void *)inData, NULL); }
147
148 const CSSM_KEY *key() const { return mKey; }
149 void key(const CSSM_KEY *k) { mKey = k; set(CSSM_ATTRIBUTE_KEY, k); }
150
151 protected:
152 void activate();
153
154 protected:
155 const CSSM_KEY *mKey;
156 };
157
158
159 //
160 // A Digest context
161 //
162 class Digest : public Context
163 {
164 public:
165 Digest(const CSP &csp, CSSM_ALGORITHMS alg) : Context(csp, alg) { }
166
167 public:
168 // integrated
169 void digest(const CssmData &data, CssmData &digest) { this->digest(&data, 1, digest); }
170 void digest(const CssmData *data, uint32 count, CssmData &digest);
171
172 // staged
173 void digest(const CssmData &data) { digest(&data, 1); }
174 void digest(const CssmData *data, uint32 count);
175 void operator () (CssmData &digest);
176 CssmData operator () () { CssmData digest; (*this)(digest); return digest; }
177
178 protected:
179 void activate();
180 };
181
182
183 //
184 // A [P]RNG context
185 //
186 class Random : public Context
187 {
188 public:
189 Random(const CSP &csp, CSSM_ALGORITHMS alg) : Context(csp, alg), mSeed(NULL), mSize(1) { }
190 Random(const CSP &csp, CSSM_ALGORITHMS alg, const CssmCryptoData &seed)
191 : Context(csp, alg), mSeed(&seed), mSize(1) { }
192 Random(const CSP &csp, CSSM_ALGORITHMS alg, uint32 size)
193 : Context(csp, alg), mSeed(NULL), mSize(size) { }
194 Random(const CSP &csp, CSSM_ALGORITHMS alg, const CssmCryptoData &seed, uint32 size)
195 : Context(csp, alg), mSeed(&seed), mSize(size) { }
196
197 void seed(const CssmCryptoData &data);
198 void size(uint32 size);
199
200 public:
201 void generate(CssmData &data, uint32 size = 0);
202
203 // alternate function-call form
204 CssmData operator () (uint32 size = 0)
205 { CssmData output; generate(output, size); return output; }
206
207 protected:
208 void activate();
209
210 private:
211 const CssmCryptoData *mSeed;
212 uint32 mSize;
213 };
214
215
216 } // end namespace CssmClient
217
218 } // end namespace Security
219
220 #endif // _H_CDSA_CLIENT_CSPCLIENT