]> git.saurik.com Git - apple/security.git/blob - cdsa/cdsa_client/cspclient.h
Security-54.1.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 const CSSM_KEY *key() const { return mKey; }
141 void key(const CSSM_KEY *k) { mKey = k; set(CSSM_ATTRIBUTE_KEY, k); }
142
143 protected:
144 void activate();
145
146 protected:
147 const CSSM_KEY *mKey;
148 };
149
150
151 //
152 // A Digest context
153 //
154 class Digest : public Context
155 {
156 public:
157 Digest(const CSP &csp, CSSM_ALGORITHMS alg) : Context(csp, alg) { }
158
159 public:
160 // integrated
161 void digest(const CssmData &data, CssmData &digest) { this->digest(&data, 1, digest); }
162 void digest(const CssmData *data, uint32 count, CssmData &digest);
163
164 // staged
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; }
169
170 protected:
171 void activate();
172 };
173
174
175 //
176 // A [P]RNG context
177 //
178 class Random : public Context
179 {
180 public:
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) { }
188
189 void seed(const CssmCryptoData &data);
190 void size(uint32 size);
191
192 public:
193 void generate(CssmData &data, uint32 size = 0);
194
195 // alternate function-call form
196 CssmData operator () (uint32 size = 0)
197 { CssmData output; generate(output, size); return output; }
198
199 protected:
200 void activate();
201
202 private:
203 const CssmCryptoData *mSeed;
204 uint32 mSize;
205 };
206
207
208 } // end namespace CssmClient
209
210 } // end namespace Security
211
212 #endif // _H_CDSA_CLIENT_CSPCLIENT