]> git.saurik.com Git - apple/security.git/blob - AppleCSP/AppleCSP/AppleCSPContext.h
b48fa4bfa70f2cea188e8ca711c44bf847e84dea
[apple/security.git] / AppleCSP / AppleCSP / AppleCSPContext.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 // AppleCSPContext.h - CSP-wide contexts
21 //
22 #ifndef _H_APPLE_CSP_CONTEXT
23 #define _H_APPLE_CSP_CONTEXT
24
25 #include <Security/utilities.h>
26 #include <Security/CSPsession.h>
27 #include "BinaryKey.h"
28
29 //
30 // Parent class for all CSPContexts implemented in this CSP.
31 // Currently the only thing we add is a reference to our
32 // creator's session.
33 //
34 class AppleCSPSession;
35
36 class AppleCSPContext : public CSPFullPluginSession::CSPContext
37 {
38 public:
39 AppleCSPContext(AppleCSPSession &session)
40 : mSession(session) {}
41
42 ~AppleCSPContext();
43
44 protected:
45 AppleCSPSession &session() { return mSession; }
46
47 /*
48 * get symmetric key bits - context.key can be either ref or raw.
49 * A convenience routine typically used by symmetric contexts'
50 * init() routines.
51 */
52 void symmetricKeyBits(
53 const Context &context,
54 CSSM_ALGORITHMS requiredAlg, // throws if this doesn't match key alg
55 CSSM_KEYUSE intendedUse, // throws if key usage doesn't match this
56 UInt8 *&keyBits, // RETURNED (not mallocd or copied)
57 UInt32 &keyLen); // RETURNED
58
59 private:
60 AppleCSPSession &mSession;
61 };
62
63 //
64 // Context for CSSM_ALGID_APPLE_YARROW.
65 //
66 class YarrowContext : public AppleCSPContext
67 {
68 public:
69 YarrowContext(AppleCSPSession &session);
70 virtual ~YarrowContext();
71 virtual void init(const Context &context, bool encoding = true);
72 void final(CssmData &out);
73 size_t outputSize(bool final, size_t inSize) { return outSize; }
74
75 private:
76 uint32 outSize;
77 };
78
79 //
80 // Classes which inherit from AppleCSPContext and which also perform
81 // key pair generation inherit from this class as well.
82 //
83 class AppleKeyPairGenContext {
84 public:
85 //
86 // Subclass implements generate(const Context &, CssmKey &,
87 // CssmKey &). That method (called from CSPFullPluginSession)
88 // allocates two subclass-specific BinaryKeys and calls this
89 // method. This will eventually call down to generate(const Context &,
90 // BinaryKey &, BinaryKey &) and optionally to
91 // BinaryKey::generateKeyBlob.
92 //
93 void generate(
94 const Context &context,
95 AppleCSPSession &session, // for ref keys
96 CssmKey &pubKey,
97 BinaryKey *pubBinKey,
98 CssmKey &privKey,
99 BinaryKey *privBinKey);
100
101 protected:
102 // Subclasses must implement this. It cooks up a key pair.
103 virtual void generate(
104 const Context &context,
105 BinaryKey &pubBinKey, // valid on successful return
106 BinaryKey &privBinKey, // ditto
107 uint32 &keySize) = 0; // ditto
108 };
109
110 //
111 // Classes which inherit from AppleCSPContext and which also perform
112 // symmetric key generation inherit from this class as well.
113 //
114 class AppleSymmKeyGenContext {
115 public:
116 //
117 // Subclass implements generate(const Context &, CssmKey &,
118 // CssmKey &). Note that the second CssmKey is a dummy
119 // argument. That method merely calls generateSymKey, allowing us
120 // to get to the associated AppleCSPSession if we need to
121 // store reference keys. We take care of all attribute and
122 // usage validation and of header formatting. Parameters for
123 // validation typlically specified in constructor via an
124 // algorithm factory.
125 //
126 AppleSymmKeyGenContext(
127 uint32 minSize, // in bits
128 uint32 maxSize, // ditto
129 bool byteSized) // true --> key size must
130 // be multiple of 8 bits
131 : minSizeInBits(minSize),
132 maxSizeInBits(maxSize),
133 mustBeByteSized(byteSized) {}
134
135 void generateSymKey(
136 const Context &context,
137 AppleCSPSession &session, // for ref keys
138 CssmKey &cssmKey); // RETURNED
139
140 private:
141 uint32 minSizeInBits;
142 uint32 maxSizeInBits;
143 bool mustBeByteSized;
144
145 };
146
147 /*
148 * Generic symmetric key generation context, for algorithms whose
149 * requirements can be expressed in min/max key size and
150 * mustBeByteSized. Such algorithms just need create one of these
151 * from an algorithm factory.
152 */
153 class AppleSymmKeyGenerator : public AppleCSPContext, private AppleSymmKeyGenContext {
154 public:
155 AppleSymmKeyGenerator(
156 AppleCSPSession &session,
157 uint32 minSize, // in bits
158 uint32 maxSize, // ditto
159 bool byteSized) : // true --> key size must
160 // be multiple of 8 bits
161 AppleCSPContext(session),
162 AppleSymmKeyGenContext(minSize, maxSize, byteSized) { }
163
164 void init(const Context &context, bool encoding = true) { }
165
166 /* this just passes the request up to AppleSymmKeyGenContext */
167 void generate(
168 const Context &context,
169 CssmKey &symKey,
170 CssmKey &dummyKey) {
171 AppleSymmKeyGenContext::generateSymKey(
172 context,
173 session(),
174 symKey);
175 }
176
177 };
178
179 #endif /* _H_APPLE_CSP_CONTEXT */