]> git.saurik.com Git - apple/security.git/blob - cdsa/cdsa_client/cspclient.h
e17c5d730b22fd8873b032612a6aa20acc9da9cd
[apple/security.git] / cdsa / cdsa_client / cspclient.h
1 /*
2 * Copyright (c) 2000-2001 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 public:
82 // don't use this section unless you know what you're doing!
83 void override(const ::Context &ctx);
84
85 template <class T>
86 void set(CSSM_ATTRIBUTE_TYPE type, const T &value)
87 {
88 if (isActive()) {
89 ::Context::Attr attr(type, value);
90 check(CSSM_UpdateContextAttributes(handle(), 1, &attr));
91 }
92 }
93
94 void set(CSSM_ATTRIBUTE_TYPE type, uint32 value)
95 {
96 if (isActive()) {
97 ::Context::Attr attr(type, value);
98 check(CSSM_UpdateContextAttributes(handle(), 1, &attr));
99 }
100 }
101
102 template <class T>
103 void add(CSSM_ATTRIBUTE_TYPE type, const T &value)
104 { activate(); set(type, value); }
105
106 void add(CSSM_ATTRIBUTE_TYPE type, uint32 value)
107 { activate(); set(type, value); }
108
109 protected:
110 CSSM_ALGORITHMS mAlgorithm; // intended algorithm
111 CSSM_CC_HANDLE mHandle; // CSSM CC handle
112 bool mStaged; // staged in progress
113
114 void deactivate();
115
116 virtual void init(); // Subclasses must implement if they support staged operations.
117
118 void unstaged()
119 { activate(); if (mStaged) CssmError::throwMe(CSSMERR_CSP_STAGED_OPERATION_IN_PROGRESS); }
120
121 void staged()
122 { if (!mStaged) init(); }
123 };
124
125
126 //
127 // A Digest context
128 //
129 class Digest : public Context
130 {
131 public:
132 Digest(const CSP &csp, CSSM_ALGORITHMS alg) : Context(csp, alg) { }
133
134 public:
135 // integrated
136 void digest(const CssmData &data, CssmData &digest) { this->digest(&data, 1, digest); }
137 void digest(const CssmData *data, uint32 count, CssmData &digest);
138
139 // staged
140 void digest(const CssmData &data) { digest(&data, 1); }
141 void digest(const CssmData *data, uint32 count);
142 void operator () (CssmData &digest);
143 CssmData operator () () { CssmData digest; (*this)(digest); return digest; }
144
145 protected:
146 void activate();
147 };
148
149
150 //
151 // A [P]RNG context
152 //
153 class Random : public Context
154 {
155 public:
156 Random(const CSP &csp, CSSM_ALGORITHMS alg) : Context(csp, alg), mSeed(NULL), mSize(1) { }
157 Random(const CSP &csp, CSSM_ALGORITHMS alg, const CssmCryptoData &seed)
158 : Context(csp, alg), mSeed(&seed), mSize(1) { }
159 Random(const CSP &csp, CSSM_ALGORITHMS alg, uint32 size)
160 : Context(csp, alg), mSeed(NULL), mSize(size) { }
161 Random(const CSP &csp, CSSM_ALGORITHMS alg, const CssmCryptoData &seed, uint32 size)
162 : Context(csp, alg), mSeed(&seed), mSize(size) { }
163
164 void seed(const CssmCryptoData &data);
165 void size(uint32 size);
166
167 public:
168 void generate(CssmData &data, uint32 size = 0);
169
170 // alternate function-call form
171 CssmData operator () (uint32 size = 0)
172 { CssmData output; generate(output, size); return output; }
173
174 protected:
175 void activate();
176
177 private:
178 const CssmCryptoData *mSeed;
179 uint32 mSize;
180 };
181
182
183 } // end namespace CssmClient
184
185 } // end namespace Security
186
187 #endif // _H_CDSA_CLIENT_CSPCLIENT